From 9980ec4c52e4a65dbc7a1069365dba3c1f7996a2 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Sat, 4 Apr 2015 09:38:05 +0200 Subject: [PATCH] added increment processor --- classes/core.php | 118 ++++++++++++++++++++++++++++---- processors/increment/config.php | 19 +++++ 2 files changed, 125 insertions(+), 12 deletions(-) create mode 100644 processors/increment/config.php diff --git a/classes/core.php b/classes/core.php index 3a5b61231..c3e5fef69 100644 --- a/classes/core.php +++ b/classes/core.php @@ -393,7 +393,7 @@ public static function save_field_data($field, $entry_id, $form){ } public static function save_final_form($form){ - global $wpdb, $processed_meta; + global $wpdb, $processed_meta, $transdata; // check submit type (new or update) if(isset($_POST['_cf_frm_edt'])){ @@ -538,6 +538,9 @@ public static function save_final_form($form){ return; } + // add entry ID to transient data + $transdata['entry_id'] = $entryid; + // do mailer! $sendername = __('Caldera Forms Notification', 'caldera-forms'); if(!empty($form['mailer']['sender_name'])){ @@ -576,13 +579,13 @@ public static function save_final_form($form){ ); // if added a bcc - if( !empty( $form['mailer']['bcc_to'] ) ){ - $mail['headers'][] = self::do_magic_tags( 'Bcc: ' . $form['mailer']['bcc_to'] ); + if( !empty( trim( $form['mailer']['bcc_to'] ) ) ){ + $mail['headers'][] = self::do_magic_tags( 'Bcc: ' . trim( $form['mailer']['bcc_to'] ) ); } // if added a replyto - if( !empty( $form['mailer']['reply_to'] ) ){ - $mail['headers'][] = self::do_magic_tags( 'Reply-To: <' . $form['mailer']['reply_to'] . '>' ); + if( !empty( trim( $form['mailer']['reply_to'] ) ) ){ + $mail['headers'][] = self::do_magic_tags( 'Reply-To: <' . trim( $form['mailer']['reply_to'] ) . '>' ); } // Filter Mailer first as not to have user input be filtered @@ -724,8 +727,14 @@ public static function save_final_form($form){ } if( ! empty( $mail ) ){ + + // is send debug enabled? + if( !empty( $form['debug_mailer'] ) ){ + add_action( 'phpmailer_init', array( 'Caldera_Forms', 'debug_mail_send' ), 1000 ); + } + + if( wp_mail( (array) $mail['recipients'], $mail['subject'], stripslashes( $mail['message'] ), $headers, $mail['attachments'] )){ - if(wp_mail( (array) $mail['recipients'], $mail['subject'], stripslashes( $mail['message'] ), $headers, $mail['attachments'] )){ // kill attachment. if(!empty($csvfile['file'])){ if(file_exists($csvfile['file'])){ @@ -742,7 +751,38 @@ public static function save_final_form($form){ } } + /** + * creates a send log to debug mailer problems + * + */ + public static function debug_mail_send( $phpmailer ) { + global $transdata, $wpdb; + // this is a hack since there is not filter / action for a failed mail... yet + //$phpmailer->SMTPDebug = 3; + ob_start(); + $phpmailer->SMTPDebug = 3; + try { + $phpmailer->Send(); + } catch ( phpmailerException $e ) { + print_r( $phpmailer->ErrorInfo ); + } + print_r( $phpmailer ); + $phpmailer->SMTPDebug = 0; + $result = ob_get_clean(); + + $meta_entry = array( + 'entry_id' => $transdata['entry_id'], + 'process_id' => '_debug_log', + 'meta_key' => 'debug_log', + 'meta_value' => $result + ); + + $wpdb->insert($wpdb->prefix . 'cf_form_entry_meta', $meta_entry); + + + + } /** * Return an instance of this class. * @@ -877,6 +917,17 @@ public function get_form_processors($processors){ "description" => __("Redirects user to URL on successful submit", 'caldera-forms'), "template" => CFCORE_PATH . "processors/redirect/config.php", "single" => false + ), + 'increment_capture' => array( + "name" => __('Increment Value', 'caldera-forms'), + "description" => __("Increment a value per entry.", 'caldera-forms'), + "processor" => array( $this, 'increment_value' ), + "template" => CFCORE_PATH . "processors/increment/config.php", + "single" => true, + "conditionals" => false, + "magic_tags" => array( + 'increment_value' + ) ) ); // akismet @@ -895,6 +946,23 @@ public function get_form_processors($processors){ } + // incremenet value process + public function increment_value( $config, $form ){ + + // get increment value; + $increment_value = get_option('_increment_' . $config['processor_id'], $config['start'] ); + + update_option( '_increment_' . $config['processor_id'], $increment_value + 1 ); + + if( !empty( $config['field'] ) ){ + self::set_field_data( $config['field'], $increment_value, $form ); + } + + return array('increment_value' => $increment_value ); + + } + + static public function akismet_scanner($config, $form){ global $post; @@ -2311,20 +2379,36 @@ static public function get_entry_meta($entry_id, $form, $type = null){ $entry_meta = array(); $entry_meta_data = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . $wpdb->prefix ."cf_form_entry_meta` WHERE `entry_id` = %d", $entry_id), ARRAY_A); + if(!empty($entry_meta_data)){ $processors = apply_filters( 'caldera_forms_get_form_processors', array() ); foreach($entry_meta_data as $meta_index=>$meta){ + // is json? - if( false !== strpos($meta['meta_value'], '{') || false !== strpos($meta['meta_value'], '[') ){ - $meta['meta_value'] = json_decode($meta['meta_value'], ARRAY_A); - }else{ - $meta['meta_value'] = $meta['meta_value']; + $is_json = @json_decode($meta['meta_value'], ARRAY_A); + if( !empty( $is_json ) ){ + $meta['meta_value'] = $is_json; } $group = 'meta'; $meta = apply_filters( 'caldera_forms_get_entry_meta', $meta, $form); - if(isset($form['processors'][$meta['process_id']])){ + if( isset( $form['processors'][$meta['process_id']] ) || $meta['process_id'] == '_debug_log' ){ + + if( $meta['process_id'] == '_debug_log' ){ + $meta['meta_value'] = '
' . $meta['meta_value'] . '
'; + $entry_meta['debug'] = array( + 'name' => __('Mailer Debug', 'caldera-forms'), + 'data' => array( + '_debug_log' => array( + 'entry' => array( + 'log' => $meta + ) + ) + ) + ); + continue; + } $process_config = array(); if(isset($form['processors'][$meta['process_id']]['config'])){ @@ -2347,7 +2431,12 @@ static public function get_entry_meta($entry_id, $form, $type = null){ if(isset($form['processors'][$meta['process_id']]['type'])){ $meta_name = $processors[$form['processors'][$meta['process_id']]['type']]['name']; }else{ - $meta_name = $meta['process_id']; + if( $meta['process_id'] == '_debug_log' ){ + $meta_name = __('Mailer Debug', 'caldera-forms'); + }else{ + $meta_name = $meta['process_id']; + } + } $entry_meta[$group] = array( 'name' => $meta_name, @@ -3926,6 +4015,11 @@ static public function render_form($atts, $entry_id = null){ $notices = apply_filters( 'caldera_forms_render_notices', $notices, $form); + // set debug notice + if( !empty( $form['mailer']['enable_mailer'] ) && !empty( $form['debug_mailer'] ) ){ + $notices['error'] = array( 'note' => __('WARNING: Form is in Mailer Debug mode. Disable before going live.', 'caldera-forms') ); + } + $out .= '
'; if(!empty($notices)){ // do notices diff --git a/processors/increment/config.php b/processors/increment/config.php new file mode 100644 index 000000000..207b0855a --- /dev/null +++ b/processors/increment/config.php @@ -0,0 +1,19 @@ +
+ +
+ {{#unless start}} + +

+ {{else}} +

+ + {{/unless}} +
+
+
+ +
+ {{{_field slug="field" type="hidden" exclude="system,variables"}}} +

+
+
\ No newline at end of file