[ 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(
 118              $wpdb->gp_meta,
 119              array(
 120                  'object_type' => $object_type,
 121                  'object_id'   => $object_id,
 122                  'meta_key'    => $meta_key,
 123                  'meta_value'  => $_meta_value,
 124              )
 125          );
 126  
 127          // If the insert failed, return false, otherwise return the id of the inserted row.
 128          if ( false === $result ) {
 129              return false;
 130          } else {
 131              $ret = $wpdb->insert_id;
 132          }
 133      } elseif ( $cur->meta_value !== $meta_value ) {
 134          $result = $wpdb->update(
 135              $wpdb->gp_meta,
 136              array( 'meta_value' => $_meta_value ),
 137              array(
 138                  'object_type' => $object_type,
 139                  'object_id'   => $object_id,
 140                  'meta_key'    => $meta_key,
 141              )
 142          );
 143  
 144          // If the update failed, return false.
 145          if ( false === $result ) {
 146              return false;
 147          }
 148      }
 149  
 150      wp_cache_delete( $cache_object_id, $object_type );
 151  
 152      return $ret;
 153  }
 154  
 155  /**
 156   * Deletes meta data from the database.
 157   *
 158   * @since 1.0.0
 159   *
 160   * @param int    $object_id  ID of the object metadata is for.
 161   * @param string $meta_key   Metadata key.
 162   * @param mixed  $meta_value The value to store.
 163   * @param string $type       The object type.
 164   * @param bool   $global     Overrides the requirement of $object_id to be a number OR not empty.
 165   *
 166   * @return bool
 167   */
 168  function gp_delete_meta( $object_id = 0, $meta_key, $meta_value, $type, $global = false ) {
 169      global $wpdb;
 170  
 171      if ( ! is_numeric( $object_id ) || empty( $object_id ) && ! $global ) {
 172          return false;
 173      }
 174  
 175      $cache_object_id = $object_id = (int) $object_id;
 176      $object_type     = $type;
 177      $meta_key        = gp_sanitize_meta_key( $meta_key );
 178  
 179      $meta_tuple = compact( 'object_type', 'object_id', 'meta_key', 'meta_value', 'type' );
 180  
 181      /**
 182       * Filter the meta data before it gets deleted.
 183       *
 184       * @since 1.0.0
 185       *
 186       * @param array $meta_tuple Key value pairs of database columns and their values according to delete meta values from the database.
 187       */
 188      $meta_tuple = apply_filters( 'gp_delete_meta', $meta_tuple );
 189      extract( $meta_tuple, EXTR_OVERWRITE );
 190  
 191      $meta_value = maybe_serialize( $meta_value );
 192  
 193      if ( empty( $meta_value ) ) {
 194          $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 );
 195      } else {
 196          $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 );
 197      }
 198  
 199      if ( ! $meta_id = $wpdb->get_var( $meta_sql ) ) { // WPCS: unprepared SQL ok.
 200          return false;
 201      }
 202  
 203      $wpdb->query( $wpdb->prepare( "DELETE FROM `$wpdb->gp_meta` WHERE `meta_id` = %d", $meta_id ) );
 204  
 205      wp_cache_delete( $cache_object_id, $object_type );
 206  
 207      return true;
 208  }


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