Skip to content

Commit

Permalink
added increment processor
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCramer committed Apr 4, 2015
1 parent 1b79120 commit 9980ec4
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 12 deletions.
118 changes: 106 additions & 12 deletions classes/core.php
Expand Up @@ -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'])){
Expand Down Expand Up @@ -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'])){
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'])){
Expand All @@ -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.
*
Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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'] = '<pre>' . $meta['meta_value'] . '</pre>';
$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'])){
Expand All @@ -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,
Expand Down Expand Up @@ -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 .= '<div id="caldera_notices_'.$current_form_count.'" data-spinner="'. admin_url( 'images/spinner.gif' ).'">';
if(!empty($notices)){
// do notices
Expand Down
19 changes: 19 additions & 0 deletions processors/increment/config.php
@@ -0,0 +1,19 @@
<div class="caldera-config-group">
<label><?php echo __('Increment Start', 'caldera-forms'); ?> </label>
<div class="caldera-config-field">
{{#unless start}}
<input class="block-input field-config required" type="number" name="{{_name}}[start]" value="{{start}}">
<p><?php echo __('Number to start incrementing.', 'caldera-forms'); ?></p>
{{else}}
<p><?php echo __('Incremenets started at {{start}}. to reset, delete this and insert a new increment processor.', 'caldera-forms'); ?></p>
<input type="hidden" name="{{_name}}[start]" value="{{start}}">
{{/unless}}
</div>
</div>
<div class="caldera-config-group">
<label><?php echo __('Increment Field', 'caldera-forms'); ?> </label>
<div class="caldera-config-field">
{{{_field slug="field" type="hidden" exclude="system,variables"}}}
<p><?php echo __('If you want to show the value in the entries, Select a Hidden field in form to capture the value to.', 'caldera-forms'); ?></p>
</div>
</div>

0 comments on commit 9980ec4

Please sign in to comment.