| 1 |
<?php |
| 2 |
/** |
| 3 |
* Contact Form 7 Triggers |
| 4 |
* |
| 5 |
* @package WPbot_Automator |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPbot_Automator\Triggers; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Class CF7_Triggers |
| 16 |
*/ |
| 17 |
class CF7_Triggers extends Trigger { |
| 18 |
|
| 19 |
/** |
| 20 |
* Constructor |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
$this->id = 'cf7'; |
| 24 |
$this->group = __( 'Forms', 'wpbot-automator' ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Register the triggers |
| 29 |
*/ |
| 30 |
public function register() { |
| 31 |
//error_log( 'WPbot Automator - CF7_Triggers::register() called' ); |
| 32 |
add_action( 'wpcf7_mail_sent', array( $this, 'handle_cf7' ), 10, 1 ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Handle Contact Form 7. |
| 37 |
*/ |
| 38 |
public function handle_cf7( $contact_form ) { |
| 39 |
//error_log( 'WPbot Automator - handle_cf7 called for form ID: ' . $contact_form->id() ); |
| 40 |
$submission = \WPCF7_Submission::get_instance(); |
| 41 |
if ( $submission ) { |
| 42 |
$posted_data = $submission->get_posted_data(); |
| 43 |
$data = array( |
| 44 |
'form_id' => $contact_form->id(), |
| 45 |
'form_name' => $contact_form->title(), |
| 46 |
'fields' => $posted_data, |
| 47 |
); |
| 48 |
|
| 49 |
// Expose every CF7 field as a direct top-level token. |
| 50 |
// "your-name" → {your_name} AND {name} (strips leading "your_" prefix). |
| 51 |
// "your-email" → {your_email} AND {email}. |
| 52 |
foreach ( $posted_data as $key => $val ) { |
| 53 |
if ( ! is_scalar( $val ) || '' === $val ) { |
| 54 |
continue; |
| 55 |
} |
| 56 |
$safe = preg_replace( '/[^a-z0-9]+/', '_', strtolower( $key ) ); |
| 57 |
$safe = trim( $safe, '_' ); |
| 58 |
if ( ! $safe ) { |
| 59 |
continue; |
| 60 |
} |
| 61 |
$data[ $safe ] = $val; |
| 62 |
|
| 63 |
// Also register without the "your_" prefix so {name} works for "your-name". |
| 64 |
if ( 0 === strpos( $safe, 'your_' ) ) { |
| 65 |
$short = substr( $safe, 5 ); |
| 66 |
if ( $short && ! isset( $data[ $short ] ) ) { |
| 67 |
$data[ $short ] = $val; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// Semantic aliases for common field types. |
| 73 |
foreach ( $posted_data as $key => $val ) { |
| 74 |
if ( ! is_scalar( $val ) ) { |
| 75 |
continue; |
| 76 |
} |
| 77 |
$key_lower = strtolower( $key ); |
| 78 |
if ( false !== strpos( $key_lower, 'email' ) && is_email( $val ) && ! isset( $data['email'] ) ) { |
| 79 |
$data['email'] = $val; |
| 80 |
} |
| 81 |
if ( false !== strpos( $key_lower, 'name' ) ) { |
| 82 |
if ( false !== strpos( $key_lower, 'first' ) && ! isset( $data['first_name'] ) ) { |
| 83 |
$data['first_name'] = $val; |
| 84 |
} elseif ( false !== strpos( $key_lower, 'last' ) && ! isset( $data['last_name'] ) ) { |
| 85 |
$data['last_name'] = $val; |
| 86 |
} elseif ( ! isset( $data['full_name'] ) ) { |
| 87 |
$data['full_name'] = $val; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// Ensure {name} always resolves — alias from full_name if not already set. |
| 93 |
if ( ! isset( $data['name'] ) && isset( $data['full_name'] ) ) { |
| 94 |
$data['name'] = $data['full_name']; |
| 95 |
} |
| 96 |
|
| 97 |
//error_log( 'WPbot Automator - CF7 Trigger Firing with mapped data: ' . wp_json_encode( $data ) ); |
| 98 |
$this->run( array( |
| 99 |
'sub_id' => 'cf7_submit', |
| 100 |
'data' => $data, |
| 101 |
) ); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|