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


Generated: Tue Jan 22 01:02:31 2019 Cross-referenced by PHPXref 0.7.1