[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/ -> misc.php (source)

   1  <?php
   2  
   3  /**
   4   * Retrieves a value from $_POST.
   5   *
   6   * @param string       $key     Name of post value.
   7   * @param string|array $default Optional. Value to return if `$_POST[ $key ]` doesn't exist. Default empty.
   8   * @return string|array Value of `$_POST[ $key ]` if exists or `$default`.
   9   */
  10  function gp_post( $key, $default = '' ) {
  11      // phpcs:ignore WordPress.Security.NonceVerification -- Helper to retrieve item from the request.
  12      return wp_unslash( gp_array_get( $_POST, $key, $default ) );
  13  }
  14  
  15  /**
  16   * Retrieves a value from $_GET.
  17   *
  18   * @param string       $key     Name of get value.
  19   * @param string|array $default Optional. Value to return if `$_GET[ $key ]` doesn't exist. Default empty.
  20   * @return string|array Value of `$_GET[ $key ]` if exists or `$default`.
  21   */
  22  function gp_get( $key, $default = '' ) {
  23      // phpcs:ignore WordPress.Security.NonceVerification -- Helper to retrieve item from the request.
  24      return wp_unslash( gp_array_get( $_GET, $key, $default ) );
  25  }
  26  
  27  /**
  28   * Prints a nonce hidden field for route actions.
  29   *
  30   * @since 2.0.0
  31   *
  32   * @see wp_nonce_field()
  33   *
  34   * @param int|string $action Action name.
  35   * @param bool       $echo   Optional. Whether to display or return hidden form field. Default true.
  36   * @return string Nonce field HTML markup.
  37   */
  38  function gp_route_nonce_field( $action, $echo = true ) {
  39      return wp_nonce_field( $action, '_gp_route_nonce', true, $echo );
  40  }
  41  
  42  /**
  43   * Retrieves a URL with a nonce added to URL query for route actions.
  44   *
  45   * @since 2.0.0
  46   *
  47   * @see wp_nonce_url()
  48   *
  49   * @param string     $url    URL to add nonce action.
  50   * @param int|string $action Action name.
  51   * @return string Escaped URL with nonce action added.
  52   */
  53  function gp_route_nonce_url( $url, $action ) {
  54      return wp_nonce_url( $url, $action, $name = '_gp_route_nonce' );
  55  }
  56  
  57  /**
  58   * Retrieves a value from $array
  59   *
  60   * @param array  $array
  61   * @param string $key name of array value
  62   * @param mixed  $default value to return if $array[$key] doesn't exist. Default is ''
  63   * @return mixed $array[$key] if exists or $default
  64   */
  65  function gp_array_get( $array, $key, $default = '' ) {
  66      return isset( $array[ $key ] ) ? $array[ $key ] : $default;
  67  }
  68  
  69  function gp_const_get( $name, $default = '' ) {
  70      return defined( $name ) ? constant( $name ) : $default;
  71  }
  72  
  73  function gp_const_set( $name, $value ) {
  74      if ( defined( $name ) ) {
  75          return false;
  76      }
  77      define( $name, $value );
  78      return true;
  79  }
  80  
  81  
  82  function gp_member_get( $object, $key, $default = '' ) {
  83      return isset( $object->$key ) ? $object->$key : $default;
  84  }
  85  
  86  /**
  87   * Makes from an array of arrays a flat array.
  88   *
  89   * @param array $array the arra to flatten
  90   * @return array flattenned array
  91   */
  92  function gp_array_flatten( $array ) {
  93      $res = array();
  94      foreach ( $array as $value ) {
  95          $res = array_merge( $res, is_array( $value ) ? gp_array_flatten( $value ) : array( $value ) );
  96      }
  97      return $res;
  98  }
  99  
 100  /**
 101   * Passes the message set through the next redirect.
 102   *
 103   * Works best for edit requests, which want to pass error message or notice back to the listing page.
 104   *
 105   * @param string $message The message to be passed.
 106   * @param string $key     Optional. Key for the message. You can pass several messages under different keys.
 107   *                        A key has one message. The default is 'notice'.
 108   */
 109  function gp_notice_set( $message, $key = 'notice' ) {
 110      $cookie_path = '/' . ltrim( gp_url_path(), '/' ); // Make sure that the cookie path is never empty.
 111      gp_set_cookie( '_gp_notice_' . $key, $message, 0, $cookie_path );
 112  }
 113  
 114  /**
 115   * Retrieves a notice message, set by {@link gp_notice()}
 116   *
 117   * @param string $key Optional. Message key. The default is 'notice'
 118   */
 119  function gp_notice( $key = 'notice' ) {
 120      // Sanitize fields
 121      $allowed_tags = array(
 122          'a'       => array( 'href' => true ),
 123          'abbr'    => array(),
 124          'acronym' => array(),
 125          'b'       => array(),
 126          'br'      => array(),
 127          'button'  => array(
 128              'disabled' => true,
 129              'name'     => true,
 130              'type'     => true,
 131              'value'    => true,
 132          ),
 133          'em'      => array(),
 134          'i'       => array(),
 135          'img'     => array(
 136              'src'    => true,
 137              'width'  => true,
 138              'height' => true,
 139          ),
 140          'p'       => array(),
 141          'pre'     => array(),
 142          's'       => array(),
 143          'strike'  => array(),
 144          'strong'  => array(),
 145          'sub'     => array(),
 146          'sup'     => array(),
 147          'u'       => array(),
 148      );
 149  
 150      // Adds class, id, style, title, role attributes to all of the above allowed tags.
 151      $allowed_tags = array_map( '_wp_add_global_attributes', $allowed_tags );
 152  
 153      return wp_kses( gp_array_get( GP::$redirect_notices, $key ), $allowed_tags );
 154  }
 155  
 156  function gp_populate_notices() {
 157      GP::$redirect_notices = array();
 158      $prefix               = '_gp_notice_';
 159      $cookie_path          = '/' . ltrim( gp_url_path(), '/' ); // Make sure that the cookie path is never empty.
 160      foreach ( $_COOKIE as $key => $value ) {
 161          if ( gp_startswith( $key, $prefix ) && $suffix = substr( $key, strlen( $prefix ) ) ) {
 162              GP::$redirect_notices[ $suffix ] = wp_unslash( $value );
 163              gp_set_cookie( $key, '', 0, $cookie_path );
 164          }
 165      }
 166  }
 167  
 168  /**
 169   * Returns an array of arrays, where the i-th array contains the i-th element from
 170   * each of the argument arrays. The returned array is truncated in length to the length
 171   * of the shortest argument array.
 172   *
 173   * Previously this function was documented as:
 174   *
 175   *      The function works only with numerical arrays.
 176   *
 177   * However this was incorrect, this function would only return an array of arrays with
 178   * numeric basic indexes, but would process any array whether it was numeric or reference
 179   * based, using the order in which the array was created as the index value to return.
 180   *
 181   * For example:
 182   *
 183   *      $first_array[] = "First"
 184   *      $first_array[] = "Second"
 185   *      $first_array[] = "Third"
 186   *
 187   *      $second_array[0]    = "Fourth"
 188   *      $second_array[test] = "Fifth"
 189   *      $second_array[1]    = "Sixth"
 190   *
 191   *      $result = gp_array_zip( $first_array, $second_array );
 192   *
 193   * Would produce:
 194   *
 195   *      $result[0][0] = "First"
 196   *      $result[0][1] = "Fourth"
 197   *      $result[1][0] = "Second"
 198   *      $result[1][1] = "Fifth"
 199   *      $result[2][0] = "Third"
 200   *      $result[2][1] = "Sixth"
 201   *
 202   * Instead of either failing (which is probably what should have happened) or something like:
 203   *
 204   *      $result[0][0] = "First"
 205   *      $result[0][1] = "Fourth"
 206   *      $result[1][0] = "Second"
 207   *      $result[1][1] = "Sixth"
 208   *
 209   * Or some other random result.
 210   *
 211   * @param array ...$args Array arguments.
 212   * @return array|false Array on success, false on failure.
 213   */
 214  function gp_array_zip( ...$args ) {
 215      if ( empty( $args ) ) {
 216          return array();
 217      }
 218  
 219      $depth = 0;
 220  
 221      foreach ( $args as &$array ) {
 222          if ( ! is_array( $array ) ) {
 223              return false;
 224          }
 225  
 226          $array_size = count( $array );
 227          reset( $array );
 228  
 229          if ( 0 === $depth || $depth > $array_size ) {
 230              $depth = $array_size;
 231          }
 232      }
 233  
 234      $res = array();
 235  
 236      $array_count = 0;
 237  
 238      foreach ( $args as &$array ) {
 239          for ( $i = 0; $i < $depth; $i++ ) {
 240              $res[ $i ][ $array_count ] = current( $array );
 241  
 242              next( $array );
 243          }
 244  
 245          $array_count++;
 246      }
 247  
 248      return $res;
 249  }
 250  
 251  function gp_array_any( $callback, $array, $arg = null ) {
 252      foreach ( $array as $item ) {
 253          if ( is_array( $callback ) ) {
 254              if ( $callback[0]->{$callback[1]}( $item, $arg ) ) {
 255                  return true;
 256              }
 257          } else {
 258              if ( $callback( $item, $arg ) ) {
 259                  return true;
 260              }
 261          }
 262      }
 263      return false;
 264  }
 265  
 266  function gp_array_all( $callback, $array ) {
 267      foreach ( $array as $item ) {
 268          if ( ! $callback( $item ) ) {
 269              return false;
 270          }
 271      }
 272      return true;
 273  }
 274  
 275  function gp_error_log_dump( $value ) {
 276      if ( is_array( $value ) || is_object( $value ) ) {
 277          $value = print_r( $value, true );
 278      }
 279      error_log( $value );
 280  }
 281  
 282  function gp_object_has_var( $object, $var_name ) {
 283      return in_array( $var_name, array_keys( get_object_vars( $object ) ), true );
 284  }
 285  
 286  /**
 287   * Has this translation been updated since the passed timestamp?
 288   *
 289   * @param GP_Translation_Set $translation_set Translation to check
 290   * @param int                $timestamp Optional; unix timestamp to compare against. Defaults to HTTP_IF_MODIFIED_SINCE if set.
 291   * @return bool
 292   */
 293  function gp_has_translation_been_updated( $translation_set, $timestamp = 0 ) {
 294  
 295      // If $timestamp isn't set, try to default to the HTTP_IF_MODIFIED_SINCE header.
 296      if ( ! $timestamp && isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
 297          $timestamp = gp_gmt_strtotime( wp_unslash( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) );
 298      }
 299  
 300      // If nothing to compare against, then always assume there's an update available
 301      if ( ! $timestamp ) {
 302          return true;
 303      }
 304  
 305      return gp_gmt_strtotime( GP::$translation->last_modified( $translation_set ) ) > $timestamp;
 306  }
 307  
 308  
 309  /**
 310   * Delete translation set counts cache.
 311   *
 312   * @global bool $_wp_suspend_cache_invalidation
 313   *
 314   * @param int $id Translation set ID.
 315   */
 316  function gp_clean_translation_set_cache( $id ) {
 317      global $_wp_suspend_cache_invalidation;
 318  
 319      if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
 320          return;
 321      }
 322  
 323      wp_cache_delete( $id, 'translation_set_status_breakdown' );
 324      wp_cache_delete( $id, 'translation_set_last_modified' );
 325  }
 326  
 327  /**
 328   * Delete counts cache for all translation sets of a project
 329   *
 330   * @param int $project_id project ID
 331   */
 332  function gp_clean_translation_sets_cache( $project_id ) {
 333      $translation_sets = GP::$translation_set->by_project_id( $project_id );
 334  
 335      if ( ! $translation_sets ) {
 336          return;
 337      }
 338  
 339      foreach ( $translation_sets as $set ) {
 340          gp_clean_translation_set_cache( $set->id );
 341      }
 342  }
 343  
 344  
 345  /**
 346   * Checks if the passed value is empty.
 347   *
 348   * @param string $value The value you want to check.
 349   * @return bool
 350   */
 351  function gp_is_empty( $value ) {
 352      return empty( $value );
 353  }
 354  
 355  /**
 356   * Checks if the passed value is an empty string.
 357   *
 358   * @param string $value The value you want to check.
 359   * @return bool
 360   */
 361  function gp_is_empty_string( $value ) {
 362      return '' === $value;
 363  }
 364  
 365  /**
 366   * Checks if the passed value isn't an empty string.
 367   *
 368   * @param string $value The value you want to check.
 369   * @return bool
 370   */
 371  function gp_is_not_empty_string( $value ) {
 372      return '' !== $value;
 373  }
 374  
 375  /**
 376   * Checks if the passed value is a positive integer.
 377   *
 378   * @param int $value The value you want to check.
 379   * @return bool
 380   */
 381  function gp_is_positive_int( $value ) {
 382      return (int) $value > 0;
 383  }
 384  
 385  /**
 386   * Checks if the passed value is an integer.
 387   *
 388   * @param int|string $value The value you want to check.
 389   * @return bool
 390   */
 391  function gp_is_int( $value ) {
 392      return (bool) preg_match( '/^-?\d+$/', $value );
 393  }
 394  
 395  /**
 396   * Checks if the passed value is null.
 397   *
 398   * @param string $value The value you want to check.
 399   * @return bool
 400   */
 401  function gp_is_null( $value ) {
 402      return null === $value;
 403  }
 404  
 405  /**
 406   * Checks if the passed value is not null.
 407   *
 408   * @param string $value The value you want to check.
 409   * @return bool
 410   */
 411  function gp_is_not_null( $value ) {
 412      return null !== $value;
 413  }
 414  
 415  /**
 416   * Checks if the passed value is between the start and end value or is the same.
 417   *
 418   * @param string $value The value you want to check.
 419   * @param string $value The lower value you want to check against.
 420   * @param string $value The upper value you want to check against.
 421   * @return bool
 422   */
 423  function gp_is_between( $value, $start, $end ) {
 424      return $value >= $start && $value <= $end;
 425  }
 426  
 427  /**
 428   * Checks if the passed value is between the start and end value.
 429   *
 430   * @param string $value The value you want to check.
 431   * @return bool
 432   */
 433  function gp_is_between_exclusive( $value, $start, $end ) {
 434      return $value > $start && $value < $end;
 435  }
 436  
 437  /**
 438   * Acts the same as core PHP setcookie() but its arguments are run through the gp_set_cookie filter.
 439   *
 440   * If the filter returns false, setcookie() isn't called.
 441   *
 442   * @param string $name    The name of the cookie.
 443   * @param mixed  ...$args Additional arguments to be passed to setcookie().
 444   */
 445  function gp_set_cookie( $name, ...$args ) {
 446      array_unshift( $args, $name );
 447  
 448      /**
 449       * Filter whether GlotPress should set a cookie.
 450       *
 451       * If the filter returns false, a cookie will not be set.
 452       *
 453       * @since 1.0.0
 454       *
 455       * @param array $args {
 456       *     The cookie that is about to be set.
 457       *
 458       *     @type string $name    The name of the cookie.
 459       *     @type string $value   The value of the cookie.
 460       *     @type int    $expires The time the cookie expires.
 461       *     @type string $path    The path on the server in which the cookie will be available on.
 462       * }
 463       */
 464      $args = apply_filters( 'gp_set_cookie', $args );
 465      if ( false === $args ) {
 466          return;
 467      }
 468  
 469      setcookie( ...$args );
 470  }
 471  
 472  /**
 473   * Converts a string represented time/date to a utime int, adding a GMT offset if not found.
 474   *
 475   * @since 1.0.0
 476   *
 477   * @param string $string The string representation of the time to convert.
 478   * @return int
 479   */
 480  function gp_gmt_strtotime( $string ) {
 481      if ( is_numeric( $string ) ) {
 482          return $string;
 483      }
 484      if ( ! is_string( $string ) ) {
 485          return -1;
 486      }
 487  
 488      if ( stristr( $string, 'utc' ) || stristr( $string, 'gmt' ) || stristr( $string, '+0000' ) ) {
 489          return strtotime( $string );
 490      }
 491  
 492      if ( -1 == $time = strtotime( $string . ' +0000' ) ) {
 493          return strtotime( $string );
 494      }
 495  
 496      return $time;
 497  }
 498  
 499  /**
 500   * Determines the format to use based on the selected format type or by auto detection based on the file name.
 501   *
 502   * Used during import of translations and originals.
 503   *
 504   * @param string $selected_format The format that the user selected on the import page.
 505   * @param string $filename The filname that was uploaded by the user.
 506   * @return object|null A GP_Format child object or null if not found.
 507   */
 508  function gp_get_import_file_format( $selected_format, $filename ) {
 509      $format = gp_array_get( GP::$formats, $selected_format, null );
 510  
 511      if ( ! $format ) {
 512          $matched_ext_len = 0;
 513  
 514          foreach ( GP::$formats as $format_entry ) {
 515              $format_extensions = $format_entry->get_file_extensions();
 516  
 517              foreach ( $format_extensions as $extension ) {
 518                  $current_ext_len = strlen( $extension );
 519  
 520                  if ( gp_endswith( $filename, $extension ) && $current_ext_len > $matched_ext_len ) {
 521                      $format          = $format_entry;
 522                      $matched_ext_len = $current_ext_len;
 523                  }
 524              }
 525          }
 526      }
 527  
 528      return $format;
 529  }
 530  
 531  /**
 532   * Displays the GlotPress administrator option in the user profile screen for WordPress administrators.
 533   *
 534   * @since 2.0.0
 535   *
 536   * @param WP_User $user The WP_User object to display the profile for.
 537   */
 538  function gp_wp_profile_options( $user ) {
 539      if ( ! current_user_can( 'manage_options' ) ) {
 540          return;
 541      }
 542  
 543      ?>
 544      <h2 id="glotpress"><?php _e( 'GlotPress', 'glotpress' ); ?></h2>
 545  
 546      <table class="form-table">
 547          <tr id="gp-admin">
 548              <th scope="row"><?php _e( 'Administrator', 'glotpress' ); ?></th>
 549              <td>
 550                  <fieldset>
 551                      <legend class="screen-reader-text"><span><?php _e( 'GlotPress Administrator', 'glotpress' ); ?></span></legend>
 552                      <label for="gp_administrator">
 553                          <input name="gp_administrator" type="checkbox" id="gp_administrator" value="1"<?php checked( GP::$permission->user_can( $user, 'admin' ) ); ?> />
 554                          <?php _e( 'Grant this user administrative privileges in GlotPress.', 'glotpress' ); ?>
 555                      </label>
 556                  </fieldset>
 557              </td>
 558          </tr>
 559      </table>
 560      <?php
 561  }
 562  
 563  /**
 564   * Saves the settings for the GlotPress administrator option in the user profile screen for WordPress administrators.
 565   *
 566   * @since 2.0.0
 567   *
 568   * @param int $user_id The WordPress user id to save the setting for.
 569   */
 570  function gp_wp_profile_options_update( $user_id ) {
 571      if ( ! current_user_can( 'manage_options' ) ) {
 572          return;
 573      }
 574  
 575      $is_user_gp_admin = GP::$permission->user_can( $user_id, 'admin' );
 576  
 577      if ( array_key_exists( 'gp_administrator', $_POST ) && ! $is_user_gp_admin ) {
 578          GP::$administrator_permission->create(
 579              array(
 580                  'user_id'     => $user_id,
 581                  'action'      => 'admin',
 582                  'object_type' => null,
 583              )
 584          );
 585      }
 586  
 587      if ( ! array_key_exists( 'gp_administrator', $_POST ) && $is_user_gp_admin ) {
 588          $current_perm = GP::$administrator_permission->find_one(
 589              array(
 590                  'user_id' => $user_id,
 591                  'action'  => 'admin',
 592              )
 593          );
 594          $current_perm->delete();
 595      }
 596  }
 597  
 598  /**
 599   * Returns a multi-dimensional array with the sort by types, descriptions and SQL query for each.
 600   *
 601   * @since 2.1.0
 602   *
 603   * @return array An array of sort by field types.
 604   */
 605  function gp_get_sort_by_fields() {
 606      $sort_fields = array(
 607          'original_date_added'    => array(
 608              'title'       => __( 'Date added (original)', 'glotpress' ),
 609              'sql_sort_by' => 'o.date_added %s',
 610          ),
 611          'translation_date_added' => array(
 612              'title'       => __( 'Date added (translation)', 'glotpress' ),
 613              'sql_sort_by' => 't.date_added %s',
 614          ),
 615          'original'               => array(
 616              'title'       => __( 'Original string', 'glotpress' ),
 617              'sql_sort_by' => 'o.singular %s',
 618          ),
 619          'translation'            => array(
 620              'title'       => __( 'Translation', 'glotpress' ),
 621              'sql_sort_by' => 't.translation_0 %s',
 622          ),
 623          'priority'               => array(
 624              'title'       => __( 'Priority', 'glotpress' ),
 625              'sql_sort_by' => 'o.priority %s, o.date_added DESC',
 626          ),
 627          'references'             => array(
 628              'title'       => __( 'Filename in source', 'glotpress' ),
 629              'sql_sort_by' => 'o.references',
 630          ),
 631          'random'                 => array(
 632              'title'       => __( 'Random', 'glotpress' ),
 633              'sql_sort_by' => 'o.priority DESC, RAND()',
 634          ),
 635      );
 636  
 637      /**
 638       * Filter the sort by list to allow plugins to add or remove sort by types.
 639       *
 640       * Plugins can add, remove or resort the sort by types array which is used to create
 641       * the sort by drop down in the translations page.
 642       *
 643       * @since 2.1.0
 644       *
 645       * @param array $sort_fields {
 646       *     A list of sort by types.
 647       *
 648       *     @type array $sort_type An array with two keys, 'title' is a translated description of the key and 'sql_sort_by' which is a partial SQL SORT BY clause to use.
 649       * }
 650       */
 651      return apply_filters( 'gp_sort_by_fields', $sort_fields );
 652  }


Generated: Thu Jul 9 01:01:52 2020 Cross-referenced by PHPXref 0.7.1