[ Index ]

MailPress 7.2

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/ -> MP_WP_Meta.class.php (source)

   1  <?php
   2  class MP_WP_Meta
   3  {
   4  	public static function _get_called_class()
   5      {
   6          return get_called_class();
   7      }
   8  
   9  	public static function _get_meta_table()
  10      {
  11          global $wpdb;
  12          $tables = array ( 'mail' => $wpdb->mp_mailmeta, 'user' => $wpdb->mp_usermeta, );
  13  
  14          $_class = self::_get_called_class();
  15  
  16          return $tables[$_class::object_type];
  17      }
  18  
  19  	public static function _get_meta_column()
  20      {
  21          $_class = self::_get_called_class();
  22  
  23          return 'mp_' . $_class::object_type . '_id';
  24      }
  25  
  26  
  27  	public static function add( $object_id, $meta_key = false, $meta_value, $unique = false ) 
  28      {
  29          $meta_table  = self::_get_meta_table();
  30          $meta_column = self::_get_meta_column();
  31  
  32          if ( !is_numeric( $object_id ) || !$meta_key || !$meta_table ) return false;
  33  
  34          $data[$meta_column] = $object_id;                                $format[] = '%d';
  35          $data['meta_key']   = stripslashes( $meta_key );                    $format[] = '%s';
  36          $data['meta_value'] = maybe_serialize( stripslashes_deep( $meta_value ) );    $format[] = '%s';
  37  
  38          global $wpdb;
  39  
  40          if ( $unique && $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $meta_table WHERE meta_key = %s AND $meta_column = %d", $data['meta_key'], $data[$meta_column] ) ) )
  41          return false;
  42  
  43          $wpdb->insert( $meta_table, $data, $format );
  44  
  45          return $wpdb->insert_id;
  46      }
  47  
  48  	public static function update( $object_id, $meta_key = false, $meta_value = '', $prev_value = '' ) 
  49      {
  50          $meta_table  = self::_get_meta_table();
  51          $meta_column = self::_get_meta_column();
  52  
  53          if ( !is_numeric( $object_id ) || !$meta_key || !$meta_table ) return false;
  54  
  55          $data['meta_value']  = maybe_serialize( stripslashes_deep( $meta_value ) );$format[] = '%s';
  56  
  57          $where[$meta_column] = $object_id;                                $where_format[] = '%d';
  58          $where['meta_key']   = stripslashes( $meta_key );                    $where_format[] = '%s';
  59          if ( !empty( $prev_value ) ) {
  60              $where['meta_value']  = maybe_serialize( $prev_value );            $where_format[] = '%s';
  61          }
  62  
  63          global $wpdb;
  64          $wpdb->update( $meta_table, $data, $where, $format, $where_format );
  65  
  66          return true;
  67      }
  68  
  69  	public static function delete( $object_id, $meta_key = false , $meta_value = '' ) 
  70      {
  71          $meta_table  = self::_get_meta_table();
  72          $meta_column = self::_get_meta_column();
  73  
  74          if ( !is_numeric( $object_id ) || !$meta_table ) return false;
  75  
  76          $meta_key   = stripslashes( $meta_key );
  77          $meta_value = maybe_serialize( stripslashes_deep( $meta_value ) );
  78  
  79          global $wpdb;
  80  
  81          if ( !empty( $meta_value ) )     $wpdb->query( $wpdb->prepare( "DELETE FROM $meta_table WHERE $meta_column = %d AND meta_key = %s AND meta_value = %s", $object_id, $meta_key, $meta_value ) );
  82          elseif ( $meta_key )             $wpdb->query( $wpdb->prepare( "DELETE FROM $meta_table WHERE $meta_column = %d AND meta_key = %s", $object_id, $meta_key ) );
  83          else                      $wpdb->query( $wpdb->prepare( "DELETE FROM $meta_table WHERE $meta_column = %d", $object_id ) );
  84  
  85          return true;
  86      }
  87  
  88  	public static function get( $object_id, $meta_key = false, $meta_value = '' ) 
  89      {
  90          $meta_table  = self::_get_meta_table();
  91          $meta_column = self::_get_meta_column();
  92  
  93          if ( !is_numeric( $object_id ) || !$meta_table ) return false;
  94  
  95          global $wpdb;
  96  
  97          if ( $meta_key ) 
  98          {
  99              if ( empty( $meta_value ) ) 
 100                  $metas = $wpdb->get_col( $wpdb->prepare( "SELECT meta_value FROM $meta_table WHERE $meta_column = %d AND meta_key = %s", $object_id, $meta_key ) );
 101              else
 102                  $metas = $wpdb->get_col( $wpdb->prepare( "SELECT meta_value FROM $meta_table WHERE $meta_column = %d AND meta_key = %s AND meta_value = %s", $object_id, $meta_key, $meta_value ) );
 103          }
 104          else
 105          {
 106              $metas = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM $meta_table WHERE $meta_column = %d", $object_id ) );
 107          }
 108  
 109          if ( empty( $metas ) ) return ( empty( $meta_key ) ) ? array() : '';
 110  
 111          $metas = array_map( 'maybe_unserialize', $metas );
 112  
 113          if ( count( $metas ) == 1 )     return $metas[0];
 114          else                    return $metas;
 115      }
 116  
 117  
 118  	public static function has( $object_id , $meta_key = false ) 
 119      {
 120          $meta_table  = self::_get_meta_table();
 121          $meta_column = self::_get_meta_column();
 122  
 123          if ( !is_numeric( $object_id ) || !$meta_table ) return false;
 124  
 125          global $wpdb;
 126  
 127          $x = ( $meta_key ) ? "AND meta_key = '".$meta_key."'" : ''; 
 128  
 129          return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $meta_table WHERE $meta_column = %d $x ORDER BY meta_key, meta_id", $object_id ), ARRAY_A );
 130      }
 131  
 132  
 133  	public static function update_by_id( $meta_id, $meta_key, $meta_value ) 
 134      {
 135          $meta_table  = self::_get_meta_table();
 136  
 137          if ( !is_numeric( $meta_id ) || !$meta_table ) return false;
 138  
 139          $data['meta_value'] = maybe_serialize( stripslashes_deep( $meta_value ) );    $format[] = '%s';
 140          $data['meta_key']   = stripslashes( $meta_key );                    $format[] = '%s';
 141  
 142          $where['meta_id']   = $meta_id;                                    $where_format[] = '%d';
 143  
 144          global $wpdb;
 145          $wpdb->update( $meta_table, $data, $where, $format, $where_format );
 146  
 147          return true;
 148      }
 149  
 150  	public static function delete_by_id( $meta_id ) 
 151      {
 152          $meta_table  = self::_get_meta_table();
 153  
 154          if ( !is_numeric( $meta_id ) || !$meta_table ) return false;
 155  
 156          global $wpdb;
 157          $wpdb->query( $wpdb->prepare( "DELETE FROM $meta_table WHERE meta_id = %d", $meta_id ) );
 158  
 159          return true;
 160      }
 161  
 162  	public static function get_by_id( $meta_id ) 
 163      {
 164          $meta_table  = self::_get_meta_table();
 165  
 166          if ( !is_numeric( $meta_id ) || !$meta_table ) return false;
 167  
 168          global $wpdb;
 169          $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $meta_table WHERE meta_id = %d", $meta_id ) );
 170          if ( $meta )    $meta->meta_value = maybe_unserialize( $meta->meta_value );
 171  
 172          return $meta;
 173      }
 174  
 175  
 176  	public static function get_replacements( $object_id )
 177      {
 178          if ( !is_numeric( $object_id ) ) return array();
 179  
 180          $metas = self::get( $object_id );
 181  
 182          if ( !$metas ) return array();
 183          if ( !is_array( $metas ) ) $metas = array( $metas );
 184  
 185          $replacements = array();
 186          foreach ( $metas as $meta )
 187          {
 188              if ( $meta->meta_key[0] == '_' ) continue;
 189              $replacements['{{' . $meta->meta_key . '}}'] = $meta->meta_value;
 190          }
 191          
 192          $_class = self::_get_called_class();
 193  
 194          return apply_filters( 'MailPress_replacements_' . $_class::object_type, $replacements );
 195      }
 196  
 197  
 198  	public static function add_meta( $object_id )
 199      {
 200          $_class = self::_get_called_class();
 201  
 202          $object_id = ( int ) $object_id;
 203  
 204          $post_ = filter_input_array( INPUT_POST );
 205  
 206          $metakeyselect     = isset( $post_['metakeyselect'] ) ? trim( $post_['metakeyselect'] ) : '';
 207          $metakeyinput     = isset( $post_['metakeyinput'] )  ? trim( $post_['metakeyinput'] )  : '';
 208          $meta_value     = isset( $post_['metavalue'] )     ? trim( $post_['metavalue'] )     : '';
 209  
 210          if ( ( '0' === $meta_value || !empty( $meta_value ) ) && ( ( ( '#NONE#' != $metakeyselect ) && !empty( $metakeyselect ) ) || !empty( $metakeyinput ) ) )
 211          {
 212              // We have a key/value pair. If both the select and the
 213              // input for the key have data, the input takes precedence:
 214  
 215              if ( '#NONE#' != $metakeyselect )                $meta_key = $metakeyselect;
 216              if ( $metakeyinput )                            $meta_key = $metakeyinput; // default
 217              if ( in_array( $meta_key, $_class::get_protected() ) )    return false;
 218  
 219              return self::add( $object_id, $meta_key, $meta_value );
 220          }
 221          return false;
 222      }
 223  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1