| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* CF7 Extension |
| 5 |
* |
| 6 |
* @package NotificationX\Extensions |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace NotificationX\Extensions\CF7; |
| 10 |
|
| 11 |
use NotificationX\Core\Helper; |
| 12 |
use NotificationX\Core\Rules; |
| 13 |
use NotificationX\GetInstance; |
| 14 |
use NotificationX\Extensions\Extension; |
| 15 |
use NotificationX\Extensions\GlobalFields; |
| 16 |
|
| 17 |
/** |
| 18 |
* CF7 Extension |
| 19 |
* @method static CF7 get_instance($args = null) |
| 20 |
*/ |
| 21 |
class CF7 extends Extension { |
| 22 |
/** |
| 23 |
* Instance of CF7 |
| 24 |
* |
| 25 |
* @var CF7 |
| 26 |
*/ |
| 27 |
use GetInstance; |
| 28 |
|
| 29 |
public $priority = 5; |
| 30 |
public $id = 'cf7'; |
| 31 |
public $img = ''; |
| 32 |
public $doc_link = 'https://notificationx.com/docs/contact-form-submission-alert/'; |
| 33 |
public $types = 'form'; |
| 34 |
// used in Settings > General tab |
| 35 |
public $module = 'modules_cf7'; |
| 36 |
public $module_priority = 8; |
| 37 |
// making sure if contact form plugin is active. |
| 38 |
public $class = 'WPCF7_ContactForm'; |
| 39 |
public $post_type = 'wpcf7_contact_form'; |
| 40 |
|
| 41 |
/** |
| 42 |
* Initially Invoked when initialized. |
| 43 |
*/ |
| 44 |
public function __construct() { |
| 45 |
parent::__construct(); |
| 46 |
} |
| 47 |
|
| 48 |
public function init_extension() |
| 49 |
{ |
| 50 |
$this->title = __('Contact Form 7', 'notificationx'); |
| 51 |
$this->module_title = __('Contact Form 7', 'notificationx'); |
| 52 |
} |
| 53 |
|
| 54 |
public function init(){ |
| 55 |
parent::init(); |
| 56 |
add_action('wpcf7_mail_sent', array($this, 'save_new_records')); |
| 57 |
} |
| 58 |
|
| 59 |
public function init_fields(){ |
| 60 |
parent::init_fields(); |
| 61 |
|
| 62 |
add_filter('nx_form_list', [$this, 'nx_form_list'], 9); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* This functions is hooked |
| 67 |
* |
| 68 |
* @hooked nx_public_action |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public function admin_actions() { |
| 72 |
parent::admin_actions(); |
| 73 |
add_filter("nx_can_entry_{$this->id}", array($this, 'can_entry'), 10, 3); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* This functions is hooked |
| 78 |
* |
| 79 |
* @hooked nx_public_action |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function public_actions() { |
| 83 |
parent::public_actions(); |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Error message if CF7 is disabled. |
| 89 |
* |
| 90 |
* @param array $messages |
| 91 |
* @return array |
| 92 |
*/ |
| 93 |
public function source_error_message($messages) { |
| 94 |
if(!$this->class_exists()){ |
| 95 |
$url = admin_url('plugin-install.php?s=contact+form+7&tab=search&type=term'); |
| 96 |
$messages['cf7'] = [ |
| 97 |
'message' => sprintf( |
| 98 |
'%s <a href="%s" target="_blank">%s</a> %s', |
| 99 |
__('You have to install', 'notificationx'), |
| 100 |
$url, |
| 101 |
__('Contact Form 7', 'notificationx'), |
| 102 |
__('plugin first.', 'notificationx') |
| 103 |
), |
| 104 |
'html' => true, |
| 105 |
'type' => 'error', |
| 106 |
'rules' => Rules::is('source', $this->id), |
| 107 |
]; |
| 108 |
} |
| 109 |
return $messages; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Adds available forms to the Select a Form field in Content tab. |
| 114 |
* |
| 115 |
* @param array $forms |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
public function nx_form_list($forms) { |
| 119 |
$forms = GlobalFields::get_instance()->normalize_fields($this->get_forms(), 'source', $this->id, $forms); |
| 120 |
return array_values($forms); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Get available CF7 forms name. |
| 125 |
* |
| 126 |
* 'order' => 'ASC', |
| 127 |
* |
| 128 |
* |
| 129 |
* @return array |
| 130 |
*/ |
| 131 |
public function get_forms() { |
| 132 |
$forms = Helper::get_post_titles_by_search($this->post_type, '', 10, ['prefix' => $this->key()]); |
| 133 |
return $forms; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* entry_key |
| 138 |
* |
| 139 |
* @param string $key |
| 140 |
* @return string |
| 141 |
*/ |
| 142 |
public function key($key = '') { |
| 143 |
$key = $this->id . '_' . $key; |
| 144 |
return $key; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Lists available tags in the selected form. |
| 149 |
* |
| 150 |
* @return json |
| 151 |
*/ |
| 152 |
public function restResponse($args) { |
| 153 |
$result = []; |
| 154 |
// Check if inputValue is provided |
| 155 |
if (!empty($args['inputValue'])) { |
| 156 |
// Get the forms that match the inputValue and have the key prefix |
| 157 |
$forms = Helper::get_post_titles_by_search($this->post_type, $args['inputValue'], 10, ['prefix' => $this->key()]); |
| 158 |
// Normalize the fields and return as an indexed array |
| 159 |
$result = array_values(GlobalFields::get_instance()->normalize_fields($forms, 'source', $this->id)); |
| 160 |
return $result; |
| 161 |
} |
| 162 |
|
| 163 |
if (isset($args['form_id'])) { |
| 164 |
if( is_array( $args['form_id'] ) ) { |
| 165 |
$form_id = intval($args['form_id']['value']); |
| 166 |
}else{ |
| 167 |
$form_id = intval($args['form_id']); |
| 168 |
} |
| 169 |
|
| 170 |
$form = get_post($form_id); |
| 171 |
if( !empty($form->post_content) ) { |
| 172 |
$keys = $this->keys_generator($form->post_content); |
| 173 |
$returned_keys = array(); |
| 174 |
if (is_array($keys) && !empty($keys)) { |
| 175 |
foreach ($keys as $key) { |
| 176 |
$returned_keys[] = array( |
| 177 |
'label' => ucwords(str_replace(['_', '-'], ' ', $key)), |
| 178 |
'value' => "tag_$key", |
| 179 |
); |
| 180 |
} |
| 181 |
return $returned_keys; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
} |
| 186 |
return []; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Format list of available input fields name. |
| 191 |
* |
| 192 |
* @param [type] $fieldsString |
| 193 |
* @return void |
| 194 |
*/ |
| 195 |
public function keys_generator($fieldsString) { |
| 196 |
if( empty($fieldsString) ) { |
| 197 |
return []; |
| 198 |
} |
| 199 |
$fieldsString = substr($fieldsString, 0, strpos($fieldsString, 'submit') - 2); |
| 200 |
preg_match_all('/\[(.+?)\]/', $fieldsString, $parsed_fields); |
| 201 |
$fields = array(); |
| 202 |
if (!empty($parsed_fields[1])) { |
| 203 |
foreach ($parsed_fields[1] as $field) { |
| 204 |
if (strpos($field, 'submit') !== false) { |
| 205 |
continue; |
| 206 |
} else { |
| 207 |
$fields[] = explode(' ', $field)[1]; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
return $fields; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Adding new entry to database. |
| 216 |
* |
| 217 |
* @param WPCF7_ContactForm $contact_form |
| 218 |
* @return void |
| 219 |
*/ |
| 220 |
public function save_new_records($contact_form) { |
| 221 |
$submission = \WPCF7_Submission::get_instance(); |
| 222 |
$tags = $contact_form->scan_form_tags(); |
| 223 |
$data = array(); |
| 224 |
if (!empty($tags)) { |
| 225 |
foreach ($tags as $tag) { |
| 226 |
if (!empty($tag->name)) { |
| 227 |
$tagged_value = $submission->get_posted_data($tag->name); |
| 228 |
$data[$tag->name] = $tagged_value; |
| 229 |
if (strpos(strtolower($tag->name), 'email') !== false) { |
| 230 |
$data['email'] = $tagged_value; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
$data['title'] = $contact_form->title(); |
| 235 |
$data['id'] = $contact_form->id(); |
| 236 |
$data['timestamp'] = time(); |
| 237 |
} |
| 238 |
|
| 239 |
if (!empty($data)) { |
| 240 |
$key = $this->key($contact_form->id()); |
| 241 |
$this->save([ |
| 242 |
'source' => $this->id, |
| 243 |
'entry_key' => $key, |
| 244 |
'data' => $data, |
| 245 |
]); |
| 246 |
return true; |
| 247 |
} |
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Limit entry by selected form in 'Select a Form'; |
| 253 |
* |
| 254 |
* @param bool $return |
| 255 |
* @param array $entry |
| 256 |
* @param array $settings |
| 257 |
* @return boolean |
| 258 |
*/ |
| 259 |
public function can_entry($return, $entry, $settings){ |
| 260 |
if(!empty($settings['form_list']) && !empty($entry['entry_key'])){ |
| 261 |
$selected_form = $settings['form_list']; |
| 262 |
$form_id = $entry['entry_key']; |
| 263 |
if($selected_form != $form_id){ |
| 264 |
return false; |
| 265 |
} |
| 266 |
|
| 267 |
} |
| 268 |
return $return; |
| 269 |
} |
| 270 |
|
| 271 |
// public function restResponse( $params ){ |
| 272 |
// dlog('FROM CF7'); |
| 273 |
// dlog( $params ); |
| 274 |
// } |
| 275 |
|
| 276 |
public function doc(){ |
| 277 |
// translators: links |
| 278 |
return sprintf(__('<p>Make sure that you have <a target="_blank" href="%1$s">Contact Form 7 installed & configured</a> to use its campaign & form subscriptions data. For further assistance, check out our step by step <a target="_blank" href="%2$s">documentation</a>.</p> |
| 279 |
<p>🎦 <a target="_blank" href="%3$s">Watch video tutorial</a> to learn quickly</p> |
| 280 |
<p>👉 NotificationX <a target="_blank" href="%4$s">Integration with Contact Form 7</a></p> |
| 281 |
<p><strong>Recommended Blog:</strong></p> |
| 282 |
<p>🔥 Hacks to Increase Your <a target="_blank" href="%5$s">WordPress Contact Forms Submission Rate</a> Using NotificationX</p>', 'notificationx'), |
| 283 |
'https://wordpress.org/plugins/contact-form-7/', |
| 284 |
'https://notificationx.com/docs/contact-form-submission-alert/', |
| 285 |
'https://youtu.be/SP9NXMioIK8', |
| 286 |
'https://notificationx.com/integrations/contact-form-7/', |
| 287 |
'https://notificationx.com/blog/wordpress-contact-forms/' |
| 288 |
); |
| 289 |
} |
| 290 |
} |
| 291 |
|