[ Index ]

MailPress 7.1

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

title

Body

[close]

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

   1  <?php
   2  class MP_Form
   3  {
   4      const prefix = MailPress_form::prefix;
   5  
   6  	public static function get( $form, $output = OBJECT ) 
   7      {
   8          global $wpdb;
   9  
  10          switch ( true )
  11          {
  12              case ( empty( $form ) ) :
  13                  if ( isset( $GLOBALS['mp_form'] ) )     $_form = & $GLOBALS['mp_form'];
  14                  else                    $_form = null;
  15              break;
  16              case ( is_object( $form ) ) :
  17                  wp_cache_add( $form->id, $form, 'mp_form' );
  18                  $_form = $form;
  19              break;
  20              default :
  21                  if ( isset( $GLOBALS['mp_form'] ) && ( $GLOBALS['mp_form']->id == $form ) ) 
  22                  {
  23                      $_form = & $GLOBALS['mp_form'];
  24                  } 
  25                  elseif ( ! $_form = wp_cache_get( $form, 'mp_form' ) ) 
  26                  {
  27                      $_form = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->mp_forms WHERE id = %d LIMIT 1", $form ) );
  28                      if ( $_form ) wp_cache_add( $_form->id, $_form, 'mp_form' );
  29                  }
  30              break;
  31          }
  32  
  33          if ( $_form && isset( $_form->settings ) ) $_form->settings = unserialize( $_form->settings );
  34  
  35          if ( $output == OBJECT ) {
  36              return $_form;
  37          } elseif ( $output == ARRAY_A ) {
  38              return get_object_vars( $_form );
  39          } elseif ( $output == ARRAY_N ) {
  40              return array_values( get_object_vars( $_form ) );
  41          } else {
  42              return $_form;
  43          }
  44      }
  45  
  46  	public static function get_all() 
  47      {
  48          global $wpdb;
  49  
  50          $forms= $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->mp_forms a ORDER BY label" ) );
  51  
  52          if ( $forms ) foreach ( $forms as $k => $form ) if ( !is_array( $form->settings ) ) $forms[$k]->settings = unserialize( $form->settings );
  53  
  54          return $forms;        
  55      }
  56  
  57  	public static function exists( $label ) 
  58      {
  59          global $wpdb;
  60          return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->mp_forms WHERE label = %s LIMIT 1", $label ) );
  61      }
  62  
  63  	public static function get_template( $id ) 
  64      {
  65          $form = self::get( $id );
  66          if ( $form ) return $form->template;
  67          return false;
  68      }
  69  
  70  	public static function insert( $Post_form ) 
  71      {
  72          $Post_defaults = array( 'id' => 0 );
  73          $Post_form = wp_parse_args( stripslashes_deep( $Post_form ), $Post_defaults );
  74  
  75          $Post_form['label']         = sanitize_text_field( $Post_form['label'] );
  76          $Post_form['description']    = sanitize_text_field( $Post_form['description'] );
  77  
  78          extract( $Post_form, EXTR_SKIP );
  79  
  80          if ( trim( $label ) == '' )
  81          {
  82              if ( ! $wp_error )    return 0;
  83              else                return new WP_Error( 'label', __( 'You did not enter a valid label.', 'MailPress' ) );
  84          }
  85  
  86      // theme/template
  87          $settings['recipient']['template'] = $settings['recipient']['th'][$settings['recipient']['theme']]['tm'];
  88          $settings['visitor']  ['template'] = $settings['visitor']  ['th'][$settings['visitor']  ['theme']]['tm'];
  89          unset( $settings['recipient']['th'], $settings['visitor']['th'] );
  90      // is visitor
  91          $has_visitor = $tbc_visitor = false;
  92          if ( ( isset( $settings['visitor']['mail'] ) && $settings['visitor']['mail'] ) || ( $settings['visitor']['subscription'] ) )     $has_visitor = true;
  93          if ( !$has_visitor ) unset( $settings['visitor'] );
  94          if ( ( isset( $settings['visitor']['mail'] ) && ( '1' == $settings['visitor']['mail'] ) ) )                         $tbc_visitor = true;
  95  
  96          $data = $format = $where = $where_format = array();
  97  
  98          $data['label']         = $label;            $format[] = '%s';
  99          $data['description']     = $description;        $format[] = '%s';
 100          $data['template']    = $template;        $format[] = '%s';
 101          $data['settings']     = serialize( $settings );    $format[] = '%s';
 102  
 103          // Are we updating or creating?
 104          global $wpdb;
 105          $update = ( !empty ( $id ) ) ? true : false;
 106          if ( $update )
 107          {
 108              $where['id']     = ( int ) $id;        $where_format[] = '%d';
 109              $wpdb->update( $wpdb->mp_forms, $data, $where, $format, $where_format );
 110          }
 111          else
 112          {
 113              $wpdb->insert( $wpdb->mp_forms, $data, $format );
 114              $id = $wpdb->insert_id;
 115          }
 116  
 117          if ( !$id ) return $id;
 118  
 119          MP_Form_field::check_visitor( $id, $has_visitor, $tbc_visitor );
 120  
 121          return $id;
 122      }
 123  
 124  	public static function duplicate( $id )
 125      {
 126          $form = self::get( $id );
 127          if ( !$form ) return false;
 128  
 129          do 
 130          {
 131              $sep = '_x';
 132              $label = explode( $sep, $form->label );
 133              $num = array_pop( $label );
 134              $label[] = ( is_numeric( $num ) ) ? $num + 1 : $num . $sep . '2';
 135              $form->label = implode( $sep, $label );
 136          } while ( self::exists( $form->label ) );
 137  
 138          $data = $format         = array();
 139  
 140          $data['label']         = $form->label;            $format[] = '%s';
 141          $data['description']     = $form->description;        $format[] = '%s';
 142          $data['template']        = $form->template;        $format[] = '%s';
 143          $data['settings']     = serialize( $form->settings );    $format[] = '%s';    
 144          global $wpdb;
 145          $wpdb->insert( $wpdb->mp_forms, $data, $format );
 146          $dup_id = $wpdb->insert_id;
 147  
 148          if ( $dup_id )
 149          {
 150              $fields = MP_Form_field::get_all( $id );
 151              if ( $fields ) foreach( $fields as $field ) MP_Form_field::duplicate( $field->id, $dup_id );
 152          }
 153  
 154          return $dup_id;
 155      }
 156  
 157  	public static function delete( $id )
 158      {
 159          global $wpdb;
 160          $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->mp_forms WHERE id = %d;", $id ) );
 161          MP_Form_field::delete_all( $id );
 162          return true;
 163      }
 164  
 165  //// the form /building/resetting/controlling 
 166  
 167  	public static function form( $id )
 168      {
 169          $form = self::get( $id );
 170          if ( !$form ) return '<!-- MailPress_form  ERROR : unknown form #' . $id . ' ! -->';
 171  
 172          $fields = MP_Form_field::get_all( $form->id );
 173          if ( !$fields ) return '<!-- MailPress_form  WARNING : form #' . $form->id . ' is empty ! -->';
 174  
 175          $form_templates = new MP_Form_templates();
 176          $xform_template = $form_templates->get_all();
 177          if ( !isset( $xform_template[$form->template] ) )
 178              if ( !isset( $xform_template['default'] ) ) return '<!-- MailPress_form  ERROR : form #' . $form->id . ', template : ' . $form->template . ' unknown ! -->';
 179  
 180          $post_ = filter_input_array( INPUT_POST );
 181  
 182          new MP_Form_field_types();
 183  
 184          $processed = $on_error = false;
 185          $submitted = ( ( isset( $post_[self::prefix][$id] ) ) );
 186          foreach( $fields as $k => $field ) 
 187          {
 188              $field =  MP_Form_field::get( $field->id );
 189              $fields[$k] = ( $submitted ) ? MP_Form_field_types::submitted( $field ) : $field;
 190              if ( isset( $fields[$k]->submitted['on_error'] ) ) $on_error = true;
 191          }
 192  
 193          if ( $submitted )
 194          {
 195              if ( !$on_error )    $processed = self::process( $form, $fields, $form_templates );
 196              else            unset( $form->settings['options']['reset'] );
 197          }
 198  
 199          if ( $processed && isset( $form->settings['options']['reset'] ) ) unset( $_POST[self::prefix][$id] );
 200  
 201          return self::get_form( $form, $fields, $form_templates, $submitted, $processed );
 202      }
 203  
 204  
 205  //// Processing form
 206  	public static function process( $form, $fields, $form_templates )
 207      {
 208          $array = new stdClass();
 209          $mail  = new stdClass();
 210          $attachments = array();
 211  
 212          $mail->id = $mail_main_id = MP_Mail::get_id( 'MP_Form::process' );
 213  
 214          $array->form['id']     = $form->id;
 215          $array->form['label']     = $form->label;
 216          $array->form['description']     = $form->description;
 217  
 218          $recipient_content = $visitor_content = '';
 219          foreach( $fields as $k => $field )
 220          {
 221              if ( !isset( $field->submitted ) || empty( $field->submitted ) ) continue;
 222              $incopy = ( isset( $field->settings['options']['incopy'] ) && $field->settings['options']['incopy'] );
 223  
 224              $img = ( !isset( $field->submitted['map'] ) ) ? '' : '<img align="center" style="border:none;margin:0;padding:0" src="' . $field->submitted['map'] . '" alt="" /></br>' . "\r\n";
 225  
 226              $recipient_content .= sprintf( '<p>' . "\r\n" . '<b>%1$s : </b>' . "\t\t" . ' %2$s %4$s %3$s</p>' . "\r\n",             $field->label, ( ( $field->submitted['value'] ) ? $field->submitted['text'] : ( ( empty( $field->submitted['text'] ) ) ? '' : '<small>[ ' . $field->submitted['text'] . ' ]</small> ' ) ), ( ( !empty( $field->description ) ) && ( !empty( $field->submitted['text'] ) ) ? ' <small>[ ' . $field->description . ' ]</small> ' : ' ' ), ( empty( $img ) ) ? $img : "<div>$img</div>" ) ;
 227              $visitor_content   .= ( !$incopy ) ? '' : sprintf( '<p>' . "\r\n" . '<b>%1$s : </b>' . "\t\t" . ' %3$s %2$s</p>' . "\r\n", $field->label, ( ( $field->submitted['value'] ) ? $field->submitted['text'] : ( ( empty( $field->submitted['text'] ) ) ? '' : '<small>[ ' . $field->submitted['text'] . ' ]</small> ' ) ), ( empty( $img ) ) ? $img : "<div>$img</div>" );
 228  
 229              if ( isset( $field->settings['options']['visitor_email'] ) ) { $visitor_toemail = $field->submitted['value'];     $array->field[$field->id]['options']['visitor']['email'] = true; }
 230              if ( isset( $field->settings['options']['visitor_name'] ) )  { $visitor_toname  = $field->submitted['value'];     $array->field[$field->id]['options']['visitor']['name']  = true; }
 231              if ( isset( $field->settings['options']['visitor_mail'] ) )  { $visitor_mail    = $field->submitted['value'];     $array->field[$field->id]['options']['visitor']['mail']  = true; }
 232  
 233              $array->field[$field->id]['label']         = $field->label;
 234              $array->field[$field->id]['description']     = $field->description;
 235              $array->field[$field->id]['value']         = ( isset( $field->submitted['value'] ) ) ? $field->submitted['value'] : false;
 236              if ( isset( $field->submitted['map'] ) ) 
 237                  $array->field[$field->id]['map']    = $field->submitted['map'];
 238  
 239              if ( isset( $field->submitted['file'] ) ) $attachments[] = self::handle_upload( $field->submitted['file'], $mail->id );
 240          }
 241          $subject     = sprintf( __( '[%1$s] Your form receipt "%2$s" (%3$s)', 'MailPress' ), get_option( 'blogname' ), $form->label, $form->id );
 242  
 243          $mail->Theme     = $form->settings['recipient']['theme'];
 244          if ( '0' != $form->settings['recipient']['template'] ) $mail->Template = $form->settings['recipient']['template'];
 245  
 246          if ( isset( $visitor_toemail ) )
 247          {
 248              $mail->fromemail     = $visitor_toemail;
 249              $mail->fromname   = ( isset( $visitor_toname ) ) ? $visitor_toname : $visitor_toemail;
 250          }
 251  
 252          $mail->toemail     = $form->settings['recipient']['toemail'];
 253          $mail->toname     = $form->settings['recipient']['toname'];
 254  
 255          $mail->subject     = $subject;
 256          $mail->content     = $recipient_content;    //self::get_form( $form, $fields, $form_templates ); 
 257          $mail->form     = $array;
 258  
 259          if ( !MailPress::mail( $mail ) ) return false;
 260          if ( !isset( $form->settings['visitor'] ) ) return true;
 261          
 262          if ( '0' != $form->settings['visitor']['subscription'] && isset( $visitor_toemail ) )
 263          {
 264  
 265              if ( $mp_user = MP_User::get( MP_User::get_id_by_email( $visitor_toemail ) ) )
 266              {
 267                  switch( $mp_user->status ) 
 268                  {
 269                      case 'active':
 270                      break;
 271                      case 'bounced':
 272                      break;
 273                      case 'waiting':
 274                          switch ( $form->settings['visitor']['subscription'] )
 275                          {
 276                              case '1' :    // not active
 277                              break;
 278                              case '2' :    // tbc
 279                                  MP_User::send_confirmation_subscription( $mp_user->email, $mp_user->name, $mp_user->confkey );
 280                              break;
 281                              case '3' :    // active
 282                                  MP_User::set_status( $mp_user->id, 'active' );
 283                              break;
 284                          }
 285                      break;
 286                      case 'unsubscribed':
 287                          switch ( $form->settings['visitor']['subscription'] )
 288                          {
 289                              case '1' :    // not active
 290                                  MP_User::set_status( $mp_user->id, 'waiting' );
 291                              break;
 292                              case '2' :    // tbc
 293                                  MP_User::set_status( $mp_user->id, 'waiting' );
 294                                  MP_User::send_confirmation_subscription( $mp_user->email, $mp_user->name, $mp_user->confkey );
 295                              break;
 296                              case '3' :    // active
 297                                  MP_User::set_status( $mp_user->id, 'waiting' );
 298                                  MP_User::set_status( $mp_user->id, 'active' );
 299                              break;
 300                          }
 301                      break;
 302                  }
 303                  do_action( 'MailPress_visitor_subscription', 'add', $visitor_toemail, $form );
 304              }
 305              else
 306              {
 307                  switch ( $form->settings['visitor']['subscription'] )
 308                  {
 309                      case '1' :    // not active
 310                          MP_User::insert( $visitor_toemail, $visitor_toname );
 311                      break;
 312                      case '2' :    // tbc
 313                          MP_User::add( $visitor_toemail, $visitor_toname );
 314                      break;
 315                      case '3' :    // active
 316                          MP_User::insert( $visitor_toemail, $visitor_toname, array( 'status' => 'active' ) );
 317                      break;
 318                  }
 319                  do_action( 'MailPress_visitor_subscription', 'init', $visitor_toemail, $form );
 320              }
 321          }
 322  
 323          // no mail
 324          if ( !isset( $visitor_toemail ) ) return true;
 325          // form says no copy
 326          if ( !isset( $form->settings['visitor']['mail'] ) ) return true;
 327          if ( $form->settings['visitor']['mail'] == '0' )  return true;
 328          // forms says copy but no email field in form ( !!?? )
 329          if ( !isset( $visitor_mail ) && ( $form->settings['visitor']['mail'] == '1' ) ) return true;
 330          // forms says copy tbc and visitor unchecked the option
 331          if ( isset( $visitor_mail ) && !$visitor_mail ) return true;
 332  
 333          // no valid mail
 334          if ( !MailPress::is_email( $visitor_toemail ) ) return false;
 335  
 336          $mail = new stdClass();
 337          $mail->id = MP_Mail::get_id( 'MP_Form::process2' );
 338  
 339          // duplicate attachments
 340          if ( !empty( $attachments ) )
 341          {
 342              $metas = MP_Mail_meta::has( $mail_main_id, '_MailPress_attached_file' );
 343              if ( $metas )
 344              {
 345                  foreach( $metas as $meta )
 346                  {
 347                      $meta_value = unserialize( $meta['meta_value'] );
 348                      if ( !is_file( $meta_value['file_fullpath'] ) ) continue;
 349                      MP_Mail_meta::add( $mail->id, '_MailPress_attached_file', $meta_value );
 350                  }
 351              }
 352          }
 353  
 354          $mail->Theme     = $form->settings['visitor']['theme'];
 355          if ( '0' != $form->settings['visitor']['template'] ) $mail->Template = $form->settings['visitor']['template'];
 356  
 357          $mail->toemail     = $visitor_toemail;
 358          $mail->toname     = $visitor_toname;
 359  
 360          $mail->subject     = $subject;
 361          $mail->content     = $visitor_content;
 362  
 363          return MailPress::mail( $mail );
 364      }
 365  
 366  //// Getting form
 367  	public static function get_form( $form, $fields, $form_templates, $submitted = false, $processed = false )
 368      {
 369          $message = null;
 370          if ( $submitted )
 371          {
 372              $status = ( $processed ) ? 'ok' : 'ko';
 373              $message[ $status ] = $form->settings['message'][ $status ];
 374          }
 375  
 376  
 377      // going for the message
 378          if ( !empty( $message ) )
 379          {
 380              foreach( $message as $ret => $mess ) {}
 381              $html = $form_templates->get_message_template( $form->template, $ret );
 382              if ( !$html ) $html = '{{message}}';
 383  
 384              $search = $replace = array();
 385              $search[] = '{{message}}'; $replace[] = $mess;
 386              $html = str_replace( $search, $replace, $html ); 
 387  
 388              $message = sprintf( '%1$s<!-- start message -->%2$s<!--   end message -->%1$s', "\n", $html );
 389          }
 390  
 391      // going for the form
 392          $html = $form_templates->get_form_template( $form->template );
 393          if ( !$html ) $html = '{{form}}';
 394          $search = $replace = array();
 395          $search[] = '{{label}}';     $replace[] = $form->label;
 396          $search[] = '{{description}}';$replace[] = $form->description;
 397          $search[] = '{{form}}';     $replace[] = self::build_form( $form, $fields, $form_templates );
 398          $search[] = '{{message}}';     $replace[] = $message;
 399  
 400          $html = str_replace( $search, $replace, $html ); 
 401  
 402          return sprintf( '%1$s<!-- MailPress_form : start of form #%2$s -->%1$s%3$s%1$s<!--   MailPress_form : end of form #%2$s -->%1$s%1$s', "\n", $form->id, $html );
 403      }
 404  
 405  //// Building form
 406  	public static function build_form( $form, $fields, $form_templates )
 407      {
 408      // going for the fields
 409          $i = 0;
 410          $have_groups = array();
 411          foreach( $fields as $offset => $field )
 412          {
 413      // going for fields
 414              $field_template     = $field->template;
 415              $html            = ( isset( $field->submitted['on_error'] ) ) ? $form_templates->get_field_on_error_template( $form->template, $field->template ) : $form_templates->get_field_template( $form->template, $field->template );
 416              if ( !$html ) {$html = $form_templates->get_field_template( $form->template, $field->template ); }
 417              if ( !$html ) {$html = '{{field}}'; $field_template = 'not found'; }
 418              if ( ( isset( $field->settings['attributes']['type'] ) ) && ( $field->settings['attributes']['type'] == 'hidden' ) ) {$html = '{{field}}'; $field_template = "( type='hidden' ) => overridden "; }
 419              $htmls[$offset]     = sprintf( '<!-- start field / form : %2$s, field : %3$s, type : %4$s, template : %5$s/%6$s, description : %7$s -->%1$s%8$s%1$s<!--   end field / form : %2$s, field : %3$s -->%1$s', "\n", $form->id, $field->id, $field->type, $form->template, $field_template, $field->description, self::get_field( $field, $html ) );
 420      // looking for groups
 421              if ( !$i ) { $i = 1; $prev_field = $field;}
 422              if ( ( $field->type == $prev_field->type ) && ( $field->template == $prev_field->template ) && ( $form_templates->get_group_template( $form->template, $field->template ) ) )
 423              {
 424                  switch ( true ) 
 425                  {
 426                      case ( 'radio' == $field->type ) :
 427                          if ( $field->settings['attributes']['name'] == $prev_field->settings['attributes']['name'] ) 
 428                              $have_groups[$i][$offset] = $field->id;
 429                          else
 430                          {    
 431                              if ( isset( $have_groups[$i] ) )
 432                                  if ( count( $have_groups[$i] ) < 2 )     unset( $have_groups[$i] );
 433                                  else $i++;
 434                              $have_groups[$i][$offset] = $field->id;
 435                          }
 436                      break;
 437                      default :
 438                          $have_groups[$i][$offset] = $field->id;
 439                      break;
 440                  }
 441              }
 442              else
 443              {
 444                  if ( isset( $have_groups[$i] ) )
 445                      if ( count( $have_groups[$i] ) < 2 )     unset( $have_groups[$i] );
 446                      else $i++;
 447  
 448                  $have_groups[$i][$offset] = $field->id;
 449              }
 450              $prev_field = $field;
 451          }
 452  
 453          if ( isset( $have_groups[$i] ) )
 454              if ( count( $have_groups[$i] ) < 2 )     unset( $have_groups[$i] );
 455  
 456      // going for groups
 457          foreach ( $have_groups as $offsets )
 458          {
 459              $html    = '';
 460              $count = 1;
 461              $group_template     = false;
 462              foreach ( $offsets as $offset => $v )
 463              {
 464                  $field = $fields[$offset];
 465                  if ( !$group_template ) $group_template = $form_templates->get_group_template( $form->template, $field->template );
 466                  if ( !$group_template ) break;
 467  
 468                  if ( is_array( $group_template ) )
 469                  {
 470                      if ( !isset( $group_template[$field->template] ) ) break;
 471                      if ( ( $count == 1 ) && ( isset( $group_template['before'] ) ) )                $html .= $group_template['before'];
 472                      if ( ( $count == 1 ) && ( isset( $group_template['first'] ) ) )                $html .= $group_template['first'];
 473                      elseif ( ( $count == count( $offsets ) ) && ( isset( $group_template['last'] ) ) )    $html .= $group_template['last'];
 474                      else                                                    $html .= $group_template[$field->template];
 475                      if ( ( $count == count( $offsets ) ) && ( isset( $group_template['after'] ) ) )         $html .= $group_template['after'];
 476                  }
 477                  else                                                        $html .= $group_template;
 478  
 479                  $search = $replace = array();
 480                  $search[] = '{{field}}';     $replace[] = $htmls[$offset];
 481                  $html = str_replace( $search, $replace, $html ); 
 482                  $count++;
 483              }
 484              if ( !empty( $html ) )
 485              {
 486                  $first = true;
 487                  foreach ( $offsets as $offset => $v ) if ( !$first ) { unset( $htmls[$offset] ); } else { $first = false; $htmls[$offset] = sprintf( '%1$s<!-- Start grouping fields : #%2$s -->%1$s%3$s%1$s<!-- End grouping fields : #%2$s -->%1$s', "\n", implode( ', #', $offsets ), $html ); } 
 488              }
 489          }
 490  
 491          return sprintf( '%2$s%1$s%3$s%1$s%4$s', "\n", self::get_tag( $form ), implode( "\n", $htmls ), '</form>' );
 492      }
 493  
 494  	public static function get_tag( $form )
 495      {
 496          $attributes = $form->settings['attributes'];
 497      // misc
 498          $misc = ( isset( $attributes['misc'] ) ) ? ' ' . $attributes['misc'] : '';
 499          unset( $attributes['misc'] );
 500  
 501      // opening tag
 502          $tag  = '<form' ;
 503      // id
 504          if ( false === strpos( $misc, 'id=' ) ) $attributes['id'] = self::prefix . $form->id;
 505      // method
 506          $attributes['method'] = 'post';
 507      // action
 508          if ( false === strpos( $misc, 'action=' ) ) $attributes['action'] = '';
 509      // enctype
 510          if ( MP_Form_field::have_file( $form->id ) ) $attributes['enctype'] = 'multipart/form-data';
 511      // other attributes
 512          foreach ( $attributes as $attribute => $value )
 513          {
 514              $value = trim( $value );
 515              $tag .= ( ( 'action' != $attribute ) && ( '' == trim( $value ) ) ) ? '' : self::get_attribute( $attribute, trim( $value ) );
 516          } 
 517      // closing tag
 518          return $tag . "$misc >";
 519      }
 520  	public static function get_attribute( $attr, $value ) { $quote = ( false !== strpos( $value, '"' ) ) ? "'" : '"'; return " $attr=$quote$value$quote"; }
 521  	public static function get_field( $field, $html = false, $form_template = false )
 522      {
 523          if ( !$html )
 524          {
 525              $form_templates     = new MP_Form_templates();
 526              $html            = $form_templates->get_field_template( $form_template, $field->template );
 527              if ( !$html ) $html = '{{field}}';
 528          }
 529          $search = $replace = array();
 530          $search[] = '{{label}}';     $replace[] = $field->label;
 531          $search[] = '{{description}}';$replace[] = $field->description;
 532          $search[] = '{{field_id}}';     $replace[] = MP_Form_field_types::get_id( $field );
 533          $search[] = '{{field}}';     $replace[] = MP_Form_field_types::get_tag( $field );
 534          $html = str_replace( $search, $replace, $html ); 
 535          return $html;
 536      }
 537  
 538  	public static function handle_upload( $file_id, $draft_id ) 
 539      {
 540          $overrides = array( 'test_form'=>false, 'unique_filename_callback' => 'mp_unique_filename_callback' );
 541          $time = current_time( 'mysql' );
 542  
 543          require_once ( ABSPATH . 'wp-admin/includes/file.php' );
 544  
 545          $uploaded_file = wp_handle_upload( $_FILES[$file_id], $overrides, $time );
 546  
 547          if ( isset( $uploaded_file['error'] ) )
 548              return new WP_Error( 'upload_error', $uploaded_file['error'] );
 549  
 550  // Check file path is ok
 551          $uploads = wp_upload_dir();
 552          if ( $uploads && ( false === $uploads['error'] ) )                             // Get upload directory
 553          {     
 554              if ( 0 === strpos( $uploaded_file['file'], $uploads['basedir'] ) )                 // Check that the upload base exists in the file path
 555              {
 556                  $file = str_replace( $uploads['basedir'], '', $uploaded_file['file'] );         // Remove upload dir from the file path
 557                  $file = ltrim( $file, '/' );
 558              }
 559          }
 560  
 561  // Construct the attachment array
 562          $object = array( 
 563                      'name'     => $_FILES[$file_id]['name'],
 564                      'mime_type'    => $uploaded_file['type'], 
 565                      'file'    => $file, 
 566                      'file_fullpath'    => str_replace( "\\", "/", $uploaded_file['file'] ), 
 567                      'guid'     => $uploaded_file['url']
 568                   );
 569  // Save the data
 570          return MP_Mail_meta::add( $draft_id, '_MailPress_attached_file', $object );
 571      }
 572  
 573  }


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