[ Index ]

MailPress 544

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

title

Body

[close]

/mp-content/add-ons/ -> MailPress_form.php (source)

   1  <?php
   2  if (class_exists('MailPress') && !class_exists('MailPress_form') )
   3  {
   4  /*
   5  Plugin Name: MailPress_form
   6  Plugin URI: http://blog.mailpress.org/tutorials/add-ons/form/
   7  Description: Forms (for contact forms only !)
   8  Version: 5.4.4
   9  */
  10  
  11  /** for admin plugin pages */
  12  define ('MailPress_page_forms',    'mailpress_forms');
  13  define ('MailPress_page_fields',    MailPress_page_forms . '&file=fields');
  14  define ('MailPress_page_templates',    MailPress_page_forms . '&file=templates');
  15  
  16  /** for admin plugin urls */
  17  $mp_file = 'admin.php';
  18  define ('MailPress_forms',      $mp_file . '?page=' . MailPress_page_forms);
  19  define ('MailPress_fields',     $mp_file . '?page=' . MailPress_page_fields);
  20  define ('MailPress_templates',$mp_file . '?page=' . MailPress_page_templates);
  21  
  22  /** for mysql */
  23  global $wpdb;
  24  $wpdb->mp_forms  = $wpdb->prefix . 'mailpress_forms';
  25  $wpdb->mp_fields = $wpdb->prefix . 'mailpress_formfields';
  26  
  27  class MailPress_form
  28  {
  29      const prefix = 'mp_';
  30  
  31  	function __construct()
  32      {
  33  
  34  // for shortcode
  35          add_shortcode('mailpress_form',     array(__CLASS__, 'shortcode'));
  36  
  37  // for field_type captcha_gd
  38          add_action('mp_action_1ahctpac',    array(__CLASS__, 'mp_action_captcha_gd1'));
  39          add_action('mp_action_2ahctpac',    array(__CLASS__, 'mp_action_captcha_gd2'));
  40  
  41  // for wp admin
  42          if (is_admin())
  43          {
  44          // install
  45              register_activation_hook(plugin_basename(__FILE__),     array(__CLASS__, 'install'));
  46          // for role & capabilities
  47              add_filter('MailPress_capabilities',         array(__CLASS__, 'capabilities'), 1, 1);
  48          // for load admin page
  49              add_filter('MailPress_load_admin_page',         array(__CLASS__, 'load_admin_page'), 10, 1);
  50          }
  51  
  52  // for ajax
  53          add_action('mp_action_add_form',     array(__CLASS__, 'mp_action_add_form'));
  54          add_action('mp_action_delete_form', array(__CLASS__, 'mp_action_delete_form'));
  55          add_action('mp_action_dim_form',     array(__CLASS__, 'mp_action_dim_form'));
  56          add_action('mp_action_add_field',     array(__CLASS__, 'mp_action_add_field'));
  57          add_action('mp_action_delete_field',array(__CLASS__, 'mp_action_delete_field'));
  58          add_action('mp_action_dim_field',     array(__CLASS__, 'mp_action_dim_field'));
  59  
  60          add_action('mp_action_ifview',     array(__CLASS__, 'mp_action_ifview'));
  61      }
  62  
  63  ////  Shortcode  ////
  64  
  65  	public static function shortcode($options=false)
  66      {
  67          return MP_Form::form($options['id']);
  68      }
  69  
  70  ////  Captcha's  ////
  71  
  72  	public static function mp_action_captcha_gd1()
  73      {
  74          include  (MP_ABSPATH . 'mp-includes/class/options/form/field_types/captcha_gd1/captcha/captcha.php');
  75      }
  76  	public static function mp_action_captcha_gd2()
  77      {
  78          include  (MP_ABSPATH . 'mp-includes/class/options/form/field_types/captcha_gd2/captcha/captcha.php');
  79      }
  80  
  81  ////  ADMIN  ////
  82  ////  ADMIN  ////
  83  ////  ADMIN  ////
  84  ////  ADMIN  ////
  85  
  86  // install
  87  	public static function install() 
  88      {
  89          wp_clear_scheduled_hook('mp_purge_form');
  90          include  (MP_ABSPATH . 'mp-admin/includes/install/form.php');
  91      }
  92      
  93  // for role & capabilities
  94  	public static function capabilities($capabilities)
  95      {
  96          $capabilities['MailPress_manage_forms'] = array(    'name'      => __('Forms', MP_TXTDOM),
  97                                              'group'     => 'admin',
  98                                              'menu'      => 90,
  99      
 100                                              'parent'    => false,
 101                                              'page_title'=> __('MailPress Forms', MP_TXTDOM),
 102                                              'menu_title'=> __('Forms', MP_TXTDOM),
 103                                              'page'      => MailPress_page_forms,
 104                                              'func'    => array('MP_AdminPage', 'body')
 105                                      );
 106          return $capabilities;
 107      }
 108  
 109  // for load admin page
 110  	public static function load_admin_page($hub)
 111      {
 112          $hub[MailPress_page_forms]     = 'forms';
 113          $hub[MailPress_page_fields]     = 'form_fields';
 114          $hub[MailPress_page_templates]= 'form_templates';
 115          return $hub;
 116      }
 117  
 118  // for ajax in forms page
 119  	public static function mp_action_add_form() 
 120      {
 121          if (!current_user_can('MailPress_manage_forms')) die('-1');
 122  
 123          if ( '' === trim($_POST['label']) )
 124          {
 125              $x = new WP_Ajax_Response( array(    'what' => 'form', 
 126                                      'id' => new WP_Error( 'label', __('You did not enter a valid label.', MP_TXTDOM) )
 127                                     ) );
 128              $x->send();
 129          }
 130          if ( '' === trim($_POST['settings']['recipient']['toemail']) || !is_email(($_POST['settings']['recipient']['toemail'])) )
 131          {
 132              $x = new WP_Ajax_Response( array(    'what' => 'form', 
 133                                      'id' => new WP_Error( 'settings[recipient][toemail]', __('You did not enter a valid email.', MP_TXTDOM) )
 134                                     ) );
 135              $x->send();
 136          }
 137  
 138          $form = MP_Form::insert( $_POST );
 139  
 140          if ( !$form )
 141          {
 142              $x = new WP_Ajax_Response( array(    'what' => 'form', 
 143                                      'id'   => $form
 144                                    ) );
 145              $x->send();
 146          }
 147  
 148          if ( !$form || (!$form = MP_Form::get( $form )) )     MP_::mp_die('0');
 149  
 150          $form = MP_Form::get($form->id);
 151  
 152          include  (MP_ABSPATH . 'mp-admin/forms.php');
 153          $x = new WP_Ajax_Response( array(    'what' => 'form', 
 154                                  'id' => $form->id, 
 155                                  'data' => MP_AdminPage::get_row( $form->id, array() ), 
 156                                  'supplemental' => array('name' => $form->description, 'show-link' => sprintf(__( 'form <a href="#%s">%s</a> added' , MP_TXTDOM), 'form-' . $form->id, $form->description))
 157                                ) );
 158          $x->send();
 159          break;
 160      }
 161  
 162  	public static function mp_action_dim_form() // duplicate
 163      {
 164          $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 165  
 166          $form = MP_Form::duplicate($id);
 167  
 168          if ( !$form )
 169          {
 170              $x = new WP_Ajax_Response( array(    'what' => 'form', 
 171                                      'id' => new WP_Error( __CLASS__ . '::mp_action_dim_form', __('Problems trying to duplicate form.', MP_TXTDOM), array( 'form' => 'form_description' ) ), 
 172                                    ) );
 173              $x->send();
 174          }
 175  
 176          if ( !$form || (!$form = MP_Form::get( $form )) )     MP_::mp_die('0');
 177  
 178          include  (MP_ABSPATH . 'mp-admin/forms.php');
 179          $x = new WP_Ajax_Response( array(    'what' => 'form', 
 180                                  'id' => $form->id, 
 181                                  'data' => MP_AdminPage::get_row( $form->id, array() ), 
 182                                  'supplemental' => array('name' => $form->description, 'show-link' => sprintf(__( 'form <a href="#%s">%s</a> added' , MP_TXTDOM), 'form-' . $form->id, $form->description))
 183                                ) );
 184          $x->send();
 185          break;
 186      }
 187  
 188  	public static function mp_action_delete_form() 
 189      {
 190          $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 191          MP_::mp_die( MP_Form::delete($id) ? '1' : '0' );
 192      }
 193  
 194  // for ajax in fields page
 195  	public static function mp_action_add_field() 
 196      {
 197          if (!current_user_can('MailPress_manage_forms')) die('-1');
 198  
 199          if ( '' === trim($_POST['label']) )
 200          {
 201              $x = new WP_Ajax_Response( array(    'what' => 'field', 
 202                                      'id' => new WP_Error( 'label', __('You did not enter a valid description.', MP_TXTDOM) )
 203                                     ) );
 204              $x->send();
 205          }
 206  
 207          $field = MP_Form_field::insert( $_POST );
 208  
 209          if ( !$field )
 210          {
 211              $x = new WP_Ajax_Response( array(    'what' => 'field', 
 212                                      'id'   => $field
 213                                    ) );
 214              $x->send();
 215          }
 216  
 217          if ( !$field || (!$field = MP_Form_field::get( $field )) )     MP_::mp_die('0');
 218  
 219          $form = MP_Form::get($field->form_id);
 220          if (isset($form->settings['visitor']['mail']) && ($form->settings['visitor']['mail'] != '0'))
 221              add_filter('MailPress_form_columns_form_fields', array('MP_AdminPage', 'add_incopy_column'), 1, 1);
 222  
 223          new MP_Form_field_types();
 224          include  (MP_ABSPATH . 'mp-admin/form_fields.php');
 225          $x = new WP_Ajax_Response( array(    'what' => 'field', 
 226                                  'id' => $field->id, 
 227                                  'data' => MP_AdminPage::get_row( $field->id, array() ), 
 228                                  'supplemental' => array('name' => $field->description, 'show-link' => sprintf(__( 'field <a href="#%s">%s</a> added' , MP_TXTDOM), "field-$field->id", $field->description))
 229                                ) );
 230          $x->send();
 231          break;
 232      }
 233  
 234  	public static function mp_action_dim_field() // duplicate
 235      {
 236          $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 237  
 238          $field = MP_Form_field::duplicate($id);
 239  
 240          if ( is_wp_error($field) )  
 241          {
 242              $x = new WP_Ajax_Response( array(    'what' => 'field', 
 243                                      'id' => new WP_Error( __CLASS__ . '::mp_action_dim_field', __('Problems trying to duplicate field.', MP_TXTDOM), array( 'form-field' => 'field_description' ) ), 
 244                                    ) );
 245              $x->send();
 246          }
 247  
 248          if ( !$field || (!$field = MP_Form_field::get( $field )) )     MP_::mp_die('0');
 249  
 250          include  (MP_ABSPATH . 'mp-admin/form_fields.php');
 251          $x = new WP_Ajax_Response( array(    'what' => 'field', 
 252                                  'id' => $field->id, 
 253                                  'data' => MP_AdminPage::get_row( $field->id, array() ), 
 254                                  'supplemental' => array('name' => $field->description, 'show-link' => sprintf(__( 'field <a href="#%s">%s</a> added' , MP_TXTDOM), "field-$field->id", $field->description))
 255                                ) );
 256          $x->send();
 257          break;
 258      }
 259  
 260  	public static function mp_action_delete_field() 
 261      {
 262          $id = isset($_POST['id'])? (int) $_POST['id'] : 0;
 263          MP_::mp_die( MP_Form_field::delete($id) ? '1' : '0' );
 264      }
 265  
 266  // for preview
 267  	public static function mp_action_ifview()
 268      {
 269          $form = MP_Form::get($_GET['id']);
 270  
 271          $form_url = esc_url(admin_url(MailPress_forms . '&action=edit&id=' . $form->id));
 272          $field_url = esc_url(admin_url(MailPress_fields . '&form_id=' . $form->id));
 273          $template_url = esc_url(admin_url(MailPress_fields . '&form_id=' . $form->id));
 274  
 275          $actions['form']         = "<a href='$form_url'         class='button'>" . __('Edit form', MP_TXTDOM) . '</a>';
 276          $actions['field']     = "<a href='$field_url'     class='button'>" . __('Edit fields', MP_TXTDOM) . '</a>';
 277          $actions['template']     = "<a href='$template_url'     class='button'>" . __('Edit template', MP_TXTDOM) . '</a>';
 278          $sep = ' / ';
 279  
 280          add_action('admin_init', array(__CLASS__, 'ifview_title'));
 281  
 282          include  (MP_ABSPATH . 'mp-includes/html/form.php');
 283      }
 284  
 285  	public static function ifview_title()
 286      {
 287          $form = MP_Form::get($_GET['id']);
 288          global $title; $title = sprintf(__('Preview "%1$s"', MP_TXTDOM), $form->label);
 289      }
 290  }
 291  new MailPress_form();
 292  }


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