[ Index ]

MailPress 7.1

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

title

Body

[close]

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

   1  <?php
   2  abstract class MP_WP_Customfields
   3  {
   4  	public static function _get_called_class()
   5      {
   6          return get_called_class();
   7      }
   8  
   9  	public static function _get_meta_class()
  10      {
  11          return self::_get_that( 'class' );
  12      }
  13  
  14  	public static function _get_object_id_name()
  15      {
  16          return self::_get_that( 'id' );
  17      }
  18  
  19  	public static function _get_that( $id )
  20      {
  21          $those = array(     'user' => array( 'class' => 'MP_User_meta', 'id' => 'mp_user_id', ),
  22                          'mail' => array( 'class' => 'MP_Mail_meta', 'id' => 'mail_id'   , ),
  23          );
  24  
  25          $_class = self::_get_called_class();
  26  
  27          return ( isset( $those[$_class::object_type][$id] ) ) ? $those[$_class::object_type][$id] : false;
  28      }
  29  
  30  	public static function meta_box_browse_customfields( $object )
  31      {
  32          $_meta_class = self::_get_meta_class();
  33          $_class      = self::_get_called_class();
  34  
  35          $metas = $_meta_class::get( $object->id );
  36  
  37          $out = '';
  38          $out .= '<div id="' . $_class::object_type . '-import">' . "\r\n";
  39  
  40          if ( $metas )
  41          {
  42              if ( !is_array( $metas ) ) $metas = array( $metas );
  43  
  44              foreach ( $metas as $k => $meta )
  45              {
  46                  if ( $meta['meta_key'][0] == '_' )         unset( $metas[$k] );
  47                  if ( 'batch_send' == $meta['meta_key'] )    unset( $metas[$k] );
  48              }
  49  
  50              if ( $metas )
  51              {
  52                  $out .= '<table class="form-table">' . "\r\n";
  53                  $out .= '<thead><tr><td class="cf-std"></td><td class="cf-kv"><b>' . __( 'Key' ) . '</b></td><td class="cf-kv"><b>' . __( 'Value' ) . '</b></td></tr></thead>' . "\r\n";
  54                  $out .= '<tbody>' . "\r\n";
  55  
  56                  foreach ( $metas as $entry )
  57                  {
  58                      $out .= '<tr><td class="cf-std"></td><td class="cf-v"><input type="text" disabled="disabled" value="' . esc_attr( $entry['meta_key'] ) . '" /></td><td class="cf-v"><input type="text" disabled="disabled" value="' . esc_attr( $entry['meta_value'] ) . '" /></td></tr>' . "\r\n";
  59                  }
  60  
  61                  $out .= '<tr><td class="cf-std">&#160;</td><td class="cf-std"></td><td class="cf-std"></td></tr>' . "\r\n";
  62                  $out .= '</tbody>' . "\r\n";
  63                  $out .= '</table>' . "\r\n";
  64              }
  65              else
  66              {
  67                  $out .= __( 'No meta data', 'MailPress' );
  68              }
  69          }
  70  
  71          $out .= '</div>' . "\r\n";
  72  
  73          echo $out;
  74      }
  75  /**/
  76  	public static function meta_box_customfields( $object )
  77      {
  78          $count = 0;
  79  
  80          global $wpdb;
  81          $keys = $wpdb->get_col( "SELECT meta_key FROM $wpdb->mp_mailmeta UNION SELECT meta_key FROM $wpdb->mp_usermeta GROUP BY 1 ORDER BY 1 ASC LIMIT 30" );
  82  
  83          foreach ( $keys as $k => $v )
  84          {
  85              if ( $keys[$k][0] == '_' ) unset( $keys[$k] );
  86              if ( 'batch_send' == $v )  unset( $keys[$k] );
  87          }
  88  
  89          $_meta_class = self::_get_meta_class();
  90          $_class      = self::_get_called_class();
  91  
  92          $id = ( isset( $object->id ) ) ? $object->id : '';
  93          $metas = $_meta_class::has( $id );
  94  
  95          $out = '';
  96          $out .= '<div id="postcustomstuff">' . "\r\n";
  97          $out .= '<div id="ajax-response"></div>' . "\r\n";
  98  
  99          if ( $metas )
 100          {
 101              if ( !is_array( $metas ) ) $metas = array( $metas );
 102  
 103              foreach ( $metas as $k => $meta )
 104              {
 105                  if ( $meta['meta_key'][0] == '_' )         unset( $metas[$k] );
 106                  if ( 'batch_send' == $meta['meta_key'] )    unset( $metas[$k] );
 107              }
 108  
 109              if ( $metas )
 110              {
 111                  $out .= '<table id="list-table">' . "\r\n";
 112                  $out .= '<thead><tr><th class="left">' . __( 'Name' ) . '</th><th>' . __( 'Value' ) . '</th></tr></thead>' . "\r\n";
 113                  $out .= '<tbody id="the-list" class="list:' . $_class::object_type . 'meta">' . "\r\n";
 114                  foreach ( $metas as $entry )
 115                  {
 116                      $out .= self::meta_box_customfield_row( $entry, $count ) . "\r\n";
 117                  }
 118                  $out .= '</tbody>' . "\r\n";
 119                  $out .= '</table>' . "\r\n";
 120              }
 121              else
 122              {
 123                  $out .= '<table id="list-table" class="hidden">' . "\r\n";
 124                  $out .= '<thead><tr><th class="left">' . __( 'Name' ) . '</th><th>' . __( 'Value' ) . '</th></tr></thead>' . "\r\n";
 125                  $out .= '<tbody id="the-list" class="list:' . $_class::object_type . 'meta">' . "\r\n";
 126                  $out .= '<tr><td></td></tr>' . "\r\n";
 127                  $out .= '</tbody>' . "\r\n";
 128                  $out .= '</table>' . "\r\n";
 129              }
 130          }
 131  
 132          $out .= '<p><strong>' . __( 'Add New Custom Field:' ) . '</strong></p>' . "\r\n";
 133  
 134          $out .= '<table id="newmeta">' . "\r\n";
 135          $out .= '<thead><tr><th class="left"><label for="metakeyselect">' . __( 'Name' ) . '</label></th><th>    <label for="metavalue">' . __( 'Value' ) . '</label></th></tr></thead>' . "\r\n";
 136          $out .= '<tbody>' . "\r\n";
 137          $out .= '<tr><td id="newmetaleft" class="left">' . "\r\n";
 138          if ( $keys ) 
 139          { 
 140              $out .= '<select name="metakeyselect" id="metakeyselect" tabindex="7"><option value="#NONE#">' . __( '- Select -' ) . '</option>';
 141              foreach ( $keys as $key ) 
 142              {
 143                  $key = esc_attr( $key );
 144                  $out .= '<option value="' . $key . '">' . $key . '</option>';
 145              }
 146              $out .= '</select>' . "\r\n";
 147              $out .= '<input type="text" name="metakeyinput" id="metakeyinput" class="hide-if-js" tabindex="7" value="" />';
 148              $out .= '<a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery( \'#metakeyinput, #metakeyselect, #enternew, #cancelnew\' ).toggle();return false;"><span id="enternew">' . __( 'Enter new' ) . '</span><span id="cancelnew" class="hidden">' . __( 'Cancel' ) . '</span></a>' . "\r\n";
 149          } 
 150          else 
 151          {
 152              $out .= '<input type="text" name="metakeyinput" id="metakeyinput" tabindex="7" value="" />' . "\r\n";
 153          }
 154          $out .= '</td><td><textarea name="metavalue" id="metavalue" tabindex="8" rows="2" cols="25"></textarea></td></tr>' . "\r\n";
 155  
 156          $out .= '<tr><td colspan="2"><div class="submit"><input type="submit" name="add' . $_class::object_type . 'meta" id="addmetasub" class="add:the-list:newmeta button" tabindex="9" value="' . __( 'Add Custom Field' ) . '" />' . wp_nonce_field( 'add-' . $_class::object_type . 'meta', '_ajax_nonce', false, false ) . '</div></td></tr>' . "\r\n";
 157          $out .= '</tbody>' . "\r\n";
 158          $out .= '</table>' . "\r\n";
 159          $out .= '</div>' . "\r\n";
 160          $out .= '<p>' . __( 'Custom fields can be used to customize your mails, see help above.', 'MailPress' ) . '</p>' . "\r\n";
 161  
 162          echo $out;
 163      }
 164  
 165  	public static function meta_box_customfield_row( $entry, &$count )
 166      {
 167          if ( '_' == $entry['meta_key'] { 0 } )
 168          {
 169              return;
 170          }
 171  
 172          $_class      = self::_get_called_class();
 173  
 174          static $update_nonce = false;
 175          if ( !$update_nonce )
 176          {
 177              $update_nonce = wp_create_nonce( 'add-' . $_class::object_type . 'meta' );
 178          }
 179  
 180          ++ $count;
 181  
 182          $style = ( $count % 2 ) ? ' class="alternate"' : '';
 183      
 184          $entry['meta_key']     = esc_attr( $entry['meta_key'] );
 185          $entry['meta_value']     = esc_attr( $entry['meta_value'] ); // using a <textarea />
 186          $entry['meta_id']     = ( int ) $entry['meta_id'];
 187  
 188          $delete_nonce         = wp_create_nonce( 'delete-' . $_class::object_type . 'meta_' . $entry['meta_id'] );
 189          $out = '';
 190          $out .= '<tr id="' . $_class::object_type . 'meta-' . $entry['meta_id'] . '"' . $style . '>';
 191          $out .= '<td class="left">';
 192          $out .= '<!--<label class="hidden" for="' . $_class::object_type . '[' . $entry['meta_id'] . '][key]">' . __( 'Key' ) . '</label>-->';
 193          $out .= '<input type="text" name="' . $_class::object_type . 'meta[' . $entry['meta_id'] . '][key]" id="' . $_class::object_type . 'meta[' . $entry['meta_id'] . '][key]" tabindex="6" size="20" value="' . esc_attr( $entry['meta_key'] ) . '" />';
 194          $out .= '<div class="submit">';
 195          $out .= '<input type="submit" name="delete' . $_class::object_type . 'meta[' . $entry['meta_id'] . ']"     class="delete:the-list:' . $_class::object_type . 'meta-' . $entry['meta_id'] . '::_ajax_nonce=' . $delete_nonce . ' delete' . $_class::object_type . 'meta button" tabindex="6" value="' . esc_attr( __( 'Delete' ) ) . '" />';
 196          $out .= '<input type="submit" name="update' . $_class::object_type . 'meta"                         class="add:the-list:' . $_class::object_type . 'meta-'    . $entry['meta_id'] . '::_ajax_nonce=' . $update_nonce . ' update' . $_class::object_type . 'meta button" tabindex="6" value="' . esc_attr( __( 'Update' ) ) . '" />';
 197          $out .= '</div>';
 198          $out .= wp_nonce_field( 'change-' . $_class::object_type . 'meta', '_ajax_nonce', false, false );
 199          $out .= '</td>';
 200          $out .= '<td>';
 201          $out .= '<!--<label class="hidden" for="' . $_class::object_type . 'meta[' . $entry['meta_id'] . '][value]">' . __( 'Value' ) . '</label>-->';
 202          $out .= '<textarea name="' . $_class::object_type . 'meta[' . $entry['meta_id'] . '][value]" id="' . $_class::object_type . 'meta[' . $entry['meta_id'] . '][value]" tabindex="6" rows="2" cols="30">' . esc_html( $entry['meta_value'] ) . '</textarea>';
 203          $out .= '</td>';
 204          $out .= '</tr>';
 205  
 206          return $out;
 207      }
 208  
 209  	public static function add()
 210      {
 211          $_class      = self::_get_called_class();
 212  
 213          if ( !current_user_can( 'MailPress_' . $_class::object_type . '_custom_fields' ) )    MP_::mp_die( -1 );
 214  
 215          check_ajax_referer( 'add-' . $_class::object_type . 'meta' );
 216  
 217          $_meta_class = self::_get_meta_class();
 218  
 219          $c = 0;
 220          $object_id = ( int ) MP_WP_Ajax::$pst_[ self::_get_object_id_name() ];
 221          if ( $object_id === 0 ) MP_::mp_die();
 222  
 223          if ( isset( MP_WP_Ajax::$pst_['metakeyselect'] ) || isset( MP_WP_Ajax::$pst_['metakeyinput'] ) ) 
 224          {
 225              if ( isset( MP_WP_Ajax::$pst_['metakeyselect'] ) && ( '#NONE#' == MP_WP_Ajax::$pst_['metakeyselect'] ) && empty( MP_WP_Ajax::$pst_['metakeyinput'] ) )    MP_::mp_die( 1 );
 226              if ( !$meta_id = $_meta_class::add_meta( $object_id ) )     MP_::mp_die();
 227  
 228              $response = array( 'position'     => 1 );
 229          }
 230          else
 231          {
 232              $temp = array_keys( MP_WP_Ajax::$pst_[ $_class::object_type . 'meta' ] );
 233              $meta_id = ( int ) array_pop( $temp );
 234              $key     = MP_WP_Ajax::$pst_[ $_class::object_type . 'meta' ][$meta_id]['key'];
 235              $value   = MP_WP_Ajax::$pst_[ $_class::object_type . 'meta' ][$meta_id]['value'];
 236  
 237              if ( !$meta = $_meta_class::get_by_id( $meta_id ) )            MP_::mp_die();
 238              if ( !$_meta_class::update_by_id( $meta_id , $key, $value ) )    MP_::mp_die( 1 );
 239  
 240              $response = array( 'old_id'     => $meta_id, 'position'     => 0 );
 241          }
 242  
 243          $meta = $_meta_class::get_by_id( $meta_id );
 244          $txt_id = 'mp_' . $_class::object_type . '_id';
 245          $object_id = ( int ) $meta->$txt_id;
 246          $meta = get_object_vars( $meta );
 247  
 248          $response = array_merge( $response, array( 'what' => $_class::object_type . 'meta', 'id' => $meta_id, 'data' => self::meta_box_customfield_row( $meta, $c ), 'supplemental' => array( self::_get_object_id_name() => $object_id ) ) );
 249  
 250          $x = new WP_Ajax_Response( $response );
 251  
 252          $x->send();
 253      }
 254  
 255  	public static function delete()
 256      {
 257          $_class      = self::_get_called_class();
 258  
 259          if ( !current_user_can( 'MailPress_' . $_class::object_type . '_custom_fields' ) )    MP_::mp_die( -1 );
 260  
 261          $id = isset( MP_WP_Ajax::$pst_['id'] )? ( int ) MP_WP_Ajax::$pst_['id'] : 0;
 262          check_ajax_referer( 'delete-' . $_class::object_type . 'meta_' . $id );
 263  
 264          $_meta_class = self::_get_meta_class();
 265  
 266  
 267          MP_::mp_die( $_meta_class::delete_by_id( $id ) ? 1 : 0 );
 268      }
 269  }


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1