[ Index ]

MailPress 7.1

[ 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: 7.1
   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',        add_query_arg( 'page', MailPress_page_forms,     $mp_file ) );
  19  define ( 'MailPress_fields',        add_query_arg( 'page', MailPress_page_fields,     $mp_file ) );
  20  define ( 'MailPress_templates',        add_query_arg( 'page', MailPress_page_templates,$mp_file ) );
  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', 'MailPress' ),
  97                                              'group'     => 'admin',
  98                                              'menu'      => 90,
  99      
 100                                              'parent'    => false,
 101                                              'page_title'=> __( 'MailPress Forms', 'MailPress' ),
 102                                              'menu_title'=> __( 'Forms', 'MailPress' ),
 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( MP_WP_Ajax::$pst_['label'] ) )
 124          {
 125              $x = new WP_Ajax_Response( array(     'what' => 'form', 
 126                                      'id' => new WP_Error( 'label', __( 'You did not enter a valid label.', 'MailPress' ) )
 127                                     ) );
 128              $x->send();
 129          }
 130          if ( '' === trim( MP_WP_Ajax::$pst_['settings']['recipient']['toemail'] ) || !MailPress::is_email( ( MP_WP_Ajax::$pst_['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.', 'MailPress' ) )
 134                                     ) );
 135              $x->send();
 136          }
 137  
 138          $form = MP_Form::insert( MP_WP_Ajax::$pst_ );
 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' , 'MailPress' ), 'form-' . $form->id, $form->description ) )
 157                                ) );
 158          $x->send();
 159      }
 160  
 161  	public static function mp_action_dim_form() // duplicate
 162      {
 163          $id = isset( MP_WP_Ajax::$pst_['id'] )? ( int ) MP_WP_Ajax::$pst_['id'] : 0;
 164  
 165          $form = MP_Form::duplicate( $id );
 166  
 167          if ( !$form )
 168          {
 169              $x = new WP_Ajax_Response( array(     'what' => 'form', 
 170                                      'id' => new WP_Error( __CLASS__ . '::mp_action_dim_form', __( 'Problems trying to duplicate form.', 'MailPress' ), array( 'form' => 'form_description' ) ), 
 171                                    ) );
 172              $x->send();
 173          }
 174  
 175          if ( !$form || ( !$form = MP_Form::get( $form ) ) )     MP_::mp_die( '0' );
 176  
 177          include  ( MP_ABSPATH . 'mp-admin/forms.php' );
 178          $x = new WP_Ajax_Response( array(     'what' => 'form', 
 179                                  'id' => $form->id, 
 180                                  'data' => MP_AdminPage::get_row( $form->id, array() ), 
 181                                  'supplemental' => array( 'name' => $form->description, 'show-link' => sprintf( __( 'form <a href="#%s">%s</a> added' , 'MailPress' ), 'form-' . $form->id, $form->description ) )
 182                                ) );
 183          $x->send();
 184      }
 185  
 186  	public static function mp_action_delete_form() 
 187      {
 188          $id = isset( MP_WP_Ajax::$pst_['id'] )? ( int ) MP_WP_Ajax::$pst_['id'] : 0;
 189          MP_::mp_die( MP_Form::delete( $id ) ? '1' : '0' );
 190      }
 191  
 192  // for ajax in fields page
 193  	public static function mp_action_add_field() 
 194      {
 195          if ( !current_user_can( 'MailPress_manage_forms' ) ) die( '-1' );
 196  
 197          if ( '' === trim( MP_WP_Ajax::$pst_['label'] ) )
 198          {
 199              $x = new WP_Ajax_Response( array(     'what' => 'field', 
 200                                      'id' => new WP_Error( 'label', __( 'You did not enter a valid description.', 'MailPress' ) )
 201                                     ) );
 202              $x->send();
 203          }
 204  
 205          $field = MP_Form_field::insert( MP_WP_Ajax::$pst_ );
 206  
 207          if ( !$field )
 208          {
 209              $x = new WP_Ajax_Response( array(     'what' => 'field', 
 210                                      'id'   => $field
 211                                    ) );
 212              $x->send();
 213          }
 214  
 215          if ( !$field || ( !$field = MP_Form_field::get( $field ) ) )     MP_::mp_die( '0' );
 216  
 217          $form = MP_Form::get( $field->form_id );
 218          if ( isset( $form->settings['visitor']['mail'] ) && ( $form->settings['visitor']['mail'] != '0' ) )
 219              add_filter( 'MailPress_form_columns_form_fields', array( 'MP_AdminPage', 'add_incopy_column' ), 1, 1 );
 220  
 221          new MP_Form_field_types();
 222          include  ( MP_ABSPATH . 'mp-admin/form_fields.php' );
 223          $x = new WP_Ajax_Response( array(     'what' => 'field', 
 224                                  'id' => $field->id, 
 225                                  'data' => MP_AdminPage::get_row( $field->id, array() ), 
 226                                  'supplemental' => array( 'name' => $field->description, 'show-link' => sprintf( __( 'field <a href="#%s">%s</a> added' , 'MailPress' ), "field-$field->id", $field->description ) )
 227                                ) );
 228          $x->send();
 229      }
 230  
 231  	public static function mp_action_dim_field() // duplicate
 232      {
 233          $id = isset( MP_WP_Ajax::$pst_['id'] )? ( int ) MP_WP_Ajax::$pst_['id'] : 0;
 234  
 235          $field = MP_Form_field::duplicate( $id );
 236  
 237          if ( is_wp_error( $field ) )  
 238          {
 239              $x = new WP_Ajax_Response( array(     'what' => 'field', 
 240                                      'id' => new WP_Error( __CLASS__ . '::mp_action_dim_field', __( 'Problems trying to duplicate field.', 'MailPress' ), array( 'form-field' => 'field_description' ) ), 
 241                                    ) );
 242              $x->send();
 243          }
 244  
 245          if ( !$field || ( !$field = MP_Form_field::get( $field ) ) )     MP_::mp_die( '0' );
 246  
 247          include  ( MP_ABSPATH . 'mp-admin/form_fields.php' );
 248          $x = new WP_Ajax_Response( array(     'what' => 'field', 
 249                                  'id' => $field->id, 
 250                                  'data' => MP_AdminPage::get_row( $field->id, array() ), 
 251                                  'supplemental' => array( 'name' => $field->description, 'show-link' => sprintf( __( 'field <a href="#%s">%s</a> added' , 'MailPress' ), "field-$field->id", $field->description ) )
 252                                ) );
 253          $x->send();
 254      }
 255  
 256  	public static function mp_action_delete_field() 
 257      {
 258          $id = isset( MP_WP_Ajax::$pst_['id'] )? ( int ) MP_WP_Ajax::$pst_['id'] : 0;
 259          MP_::mp_die( MP_Form_field::delete( $id ) ? '1' : '0' );
 260      }
 261  
 262  // for preview
 263  	public static function mp_action_ifview()
 264      {
 265          $form = MP_Form::get( MP_WP_Ajax::$get_['id'] );
 266  
 267          $form_url = esc_url( admin_url( add_query_arg( array('action' => 'edit', 'id' => $form->id ), MailPress_forms ) ) );
 268          $field_url = esc_url( admin_url( add_query_arg( 'form_id', $form->id, MailPress_fields ) ) );
 269          $template_url = esc_url( admin_url( add_query_arg( 'template', $form->template, MailPress_templates ) ) );
 270  
 271          $actions['form']    = '<a href="' . $form_url         . '" class="button">' . __( 'Edit form', 'MailPress' ) . '</a>';
 272          $actions['field']     = '<a href="' . $field_url        . '" class="button">' . __( 'Edit fields', 'MailPress' ) . '</a>';
 273          $actions['template']     = '<a href="' . $template_url    . '" class="button">' . __( 'Edit template', 'MailPress' ) . '</a>';
 274          $sep = ' / ';
 275  
 276          add_action( 'admin_init', array( __CLASS__, 'ifview_title' ) );
 277  
 278          include  ( MP_ABSPATH . 'mp-includes/html/form.php' );
 279      }
 280  
 281  	public static function ifview_title()
 282      {
 283          $form = MP_Form::get( MP_WP_Ajax::$get_['id'] );
 284          global $title; $title = sprintf( __( 'Preview "%1$s"', 'MailPress' ), $form->label );
 285      }
 286  }
 287  new MailPress_form();
 288  }


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