[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Functions for retrieving and manipulating metadata of various GlotPress object types.
   4   *
   5   * @package GlotPress
   6   * @subpackage Meta
   7   */
   8  
   9  /**
  10   * Sanitizes a key name to be used to store meta data in to the database.
  11   *
  12   * @since 1.0.0
  13   *
  14   * @param string $key Metadata key.
  15   *
  16   * @return string
  17   */
  18  function gp_sanitize_meta_key( $key ) {
  19      return preg_replace( '|[^a-z0-9_]|i', '', $key );
  20  }
  21  
  22  
  23  /**
  24   * Retrieves and returns a meta value from the database.
  25   *
  26   * @since 1.0.0
  27   *
  28   * @param string      $object_type The object type.
  29   * @param int         $object_id   ID of the object metadata is for.
  30   * @param string|null $meta_key    Optional. Metadata key. Default null.
  31   *
  32   * @return mixed|false Metadata or false.
  33   */
  34  function gp_get_meta( $object_type, $object_id, $meta_key = null ) {
  35      global $wpdb;
  36      $meta_key = gp_sanitize_meta_key( $meta_key );
  37  
  38      if ( ! $object_type ) {
  39          return false;
  40      }
  41  
  42      if ( ! is_numeric( $object_id ) || empty( $object_id ) ) {
  43          return false;
  44      }
  45      $object_id = (int) $object_id;
  46  
  47      $object_meta = wp_cache_get( $object_id, $object_type );
  48  
  49      if ( false === $object_meta ) {
  50          $db_object_meta = $wpdb->get_results( $wpdb->prepare( "SELECT `meta_key`, `meta_value` FROM `$wpdb->gp_meta` WHERE `object_type` = %s AND `object_id` = %d", $object_type, $object_id ) );
  51  
  52          $object_meta = array();
  53          foreach ( $db_object_meta as $meta ) {
  54              $object_meta[ $meta->meta_key ] = maybe_unserialize( $meta->meta_value );
  55          }
  56  
  57          wp_cache_add( $object_id, $object_meta, $object_type );
  58      }
  59  
  60      if ( $meta_key && isset( $object_meta[ $meta_key ] ) ) {
  61          return $object_meta[ $meta_key ];
  62      } elseif ( ! $meta_key ) {
  63          return $object_meta;
  64      } else {
  65          return false;
  66      }
  67  }
  68  
  69  /**
  70   * Adds and updates meta data in the database
  71   *
  72   * @since 1.0.0
  73   *
  74   * @param int    $object_id  ID of the object metadata is for.
  75   * @param string $meta_key   Metadata key.
  76   * @param mixed  $meta_value The value to store.
  77   * @param string $type       The object type.
  78   * @param bool   $global     Overrides the requirement of $object_id to be a number OR not empty.
  79   *
  80   * @return bool|int True if meta updated, false if there is an error and the id of the inserted row otherwise.
  81   */
  82  function gp_update_meta( $object_id = 0, $meta_key, $meta_value, $type, $global = false ) {
  83      global $wpdb;
  84  
  85      if ( ! is_numeric( $object_id ) || empty( $object_id ) && ! $global ) {
  86          return false;
  87      }
  88  
  89      $cache_object_id = $object_id = (int) $object_id;
  90      $object_type = $type;
  91      $meta_key = gp_sanitize_meta_key( $meta_key );
  92  
  93      $meta_tuple = compact( 'object_type', 'object_id', 'meta_key', 'meta_value', 'type' );
  94  
  95      /**
  96       * Filter the meta data before it gets updated.
  97       *
  98       * @since 1.0.0
  99       *
 100       * @param array $meta_tuple Key value pairs of database columns and their values according
 101       *                          to update meta values from the database.
 102       */
 103      $meta_tuple = apply_filters( 'gp_update_meta', $meta_tuple );
 104      extract( $meta_tuple, EXTR_OVERWRITE );
 105  
 106      $meta_value = $_meta_value = maybe_serialize( $meta_value );
 107      $meta_value = maybe_unserialize( $meta_value );
 108  
 109      $cur = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `$wpdb->gp_meta` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s", $object_type, $object_id, $meta_key ) );
 110  
 111      // Setup a default return value, if any error happens we will abort immediately and return false so it won't be used.
 112      // Otherwise we'll default to true, but this may be changed to the id of the inserted row later.
 113      $ret = true;
 114  
 115      // If no rows are returned we need to insert the meta data instead of updating it.
 116      if ( null === $cur ) {
 117          $result = $wpdb->insert( $wpdb->gp_meta, array( 'object_type' => $object_type, 'object_id' => $object_id, 'meta_key' => $meta_key, 'meta_value' => $_meta_value ) );
 118  
 119          // If the insert failed, return false, otherwise return the id of the inserted row.
 120          if ( false === $result ) {
 121              return false;
 122          } else {
 123              $ret = $wpdb->insert_id;
 124          }
 125      } else if ( $cur->meta_value !== $meta_value ) {
 126          $result = $wpdb->update( $wpdb->gp_meta, array( 'meta_value' => $_meta_value ), array( 'object_type' => $object_type, 'object_id' => $object_id, 'meta_key' => $meta_key ) );
 127  
 128          // If the update failed, return false.
 129          if ( false === $result ) {
 130              return false;
 131          }
 132      }
 133  
 134      wp_cache_delete( $cache_object_id, $object_type );
 135  
 136      return $ret;
 137  }
 138  
 139  /**
 140   * Deletes meta data from the database.
 141   *
 142   * @since 1.0.0
 143   *
 144   * @param int    $object_id  ID of the object metadata is for.
 145   * @param string $meta_key   Metadata key.
 146   * @param mixed  $meta_value The value to store.
 147   * @param string $type       The object type.
 148   * @param bool   $global     Overrides the requirement of $object_id to be a number OR not empty.
 149   *
 150   * @return bool
 151   */
 152  function gp_delete_meta( $object_id = 0, $meta_key, $meta_value, $type, $global = false ) {
 153      global $wpdb;
 154  
 155      if ( ! is_numeric( $object_id ) || empty( $object_id ) && ! $global ) {
 156          return false;
 157      }
 158  
 159      $cache_object_id = $object_id = (int) $object_id;
 160      $object_type = $type;
 161      $meta_key = gp_sanitize_meta_key( $meta_key );
 162  
 163      $meta_tuple = compact( 'object_type', 'object_id', 'meta_key', 'meta_value', 'type' );
 164  
 165      /**
 166       * Filter the meta data before it gets deleted.
 167       *
 168       * @since 1.0.0
 169       *
 170       * @param array $meta_tuple Key value pairs of database columns and their values according to delete meta values from the database.
 171       */
 172      $meta_tuple = apply_filters( 'gp_delete_meta', $meta_tuple );
 173      extract( $meta_tuple, EXTR_OVERWRITE );
 174  
 175      $meta_value = maybe_serialize( $meta_value );
 176  
 177      if ( empty( $meta_value ) ) {
 178          $meta_sql = $wpdb->prepare( "SELECT `meta_id` FROM `$wpdb->gp_meta` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s", $object_type, $object_id, $meta_key );
 179      } else {
 180          $meta_sql = $wpdb->prepare( "SELECT `meta_id` FROM `$wpdb->gp_meta` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s AND `meta_value` = %s", $object_type, $object_id, $meta_key, $meta_value );
 181      }
 182  
 183      if ( ! $meta_id = $wpdb->get_var( $meta_sql ) ) { // WPCS: unprepared SQL ok.
 184          return false;
 185      }
 186  
 187      $wpdb->query( $wpdb->prepare( "DELETE FROM `$wpdb->gp_meta` WHERE `meta_id` = %d", $meta_id ) );
 188  
 189      wp_cache_delete( $cache_object_id, $object_type );
 190  
 191      return true;
 192  }


Generated: Tue Mar 19 01:01:50 2019 Cross-referenced by PHPXref 0.7.1