[ Index ]

MailPress 544

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

title

Body

[close]

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

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


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1