| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* CF7 Extension |
| 5 |
* |
| 6 |
* @package NotificationX\Extensions |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace NotificationX\Extensions\WPF; |
| 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 |
use NotificationX\Admin\Entries; |
| 17 |
|
| 18 |
/** |
| 19 |
* WPForms Extension |
| 20 |
* @method static WPForms get_instance($args = null) |
| 21 |
*/ |
| 22 |
class WPForms extends Extension { |
| 23 |
/** |
| 24 |
* Instance of WPForms |
| 25 |
* |
| 26 |
* @var WPForms |
| 27 |
*/ |
| 28 |
use GetInstance; |
| 29 |
|
| 30 |
public $priority = 10; |
| 31 |
public $id = 'wpf'; |
| 32 |
public $img = ''; |
| 33 |
public $doc_link = 'https://notificationx.com/docs/contact-form-submission-alert/'; |
| 34 |
public $types = 'form'; |
| 35 |
public $module = 'modules_wpf'; |
| 36 |
public $module_priority = 9; |
| 37 |
public $class = '\WPForms_Form_Handler'; |
| 38 |
public $post_type = 'wpforms'; |
| 39 |
|
| 40 |
/** |
| 41 |
* Initially Invoked when initialized. |
| 42 |
*/ |
| 43 |
public function __construct() { |
| 44 |
parent::__construct(); |
| 45 |
} |
| 46 |
|
| 47 |
public function init_extension() |
| 48 |
{ |
| 49 |
$this->title = __('WPForms', 'notificationx'); |
| 50 |
$this->module_title = __('WPForms', 'notificationx'); |
| 51 |
} |
| 52 |
|
| 53 |
public function init() { |
| 54 |
parent::init(); |
| 55 |
add_action('wpforms_process_complete', array($this, 'save_new_records'), 10, 4); |
| 56 |
} |
| 57 |
|
| 58 |
public function init_fields(){ |
| 59 |
parent::init_fields(); |
| 60 |
add_filter('nx_form_list', [$this, 'nx_form_list'], 9); |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* This functions is hooked |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function admin_actions() { |
| 70 |
parent::admin_actions(); |
| 71 |
add_filter("nx_can_entry_{$this->id}", array($this, 'can_entry'), 10, 3); |
| 72 |
} |
| 73 |
/** |
| 74 |
* This functions is hooked |
| 75 |
* |
| 76 |
* @hooked nx_public_action |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function public_actions() { |
| 80 |
parent::public_actions(); |
| 81 |
|
| 82 |
add_filter("nx_filtered_data_{$this->id}", array($this, 'filter_by_form'), 11, 3); |
| 83 |
} |
| 84 |
|
| 85 |
public function source_error_message($messages) { |
| 86 |
if (!$this->class_exists()) { |
| 87 |
$url = admin_url('plugin-install.php?s=WPForms&tab=search&type=term'); |
| 88 |
$messages[$this->id] = [ |
| 89 |
'message' => sprintf( '%s <a href="%s" target="_blank">%s</a> %s', |
| 90 |
__( 'You have to install', 'notificationx' ), |
| 91 |
$url, |
| 92 |
__( 'WP Forms', 'notificationx' ), |
| 93 |
__( 'plugin first.', 'notificationx' ) |
| 94 |
), |
| 95 |
'html' => true, |
| 96 |
'type' => 'error', |
| 97 |
'rules' => Rules::is('source', $this->id), |
| 98 |
]; |
| 99 |
} |
| 100 |
return $messages; |
| 101 |
} |
| 102 |
|
| 103 |
public function nx_form_list($forms) { |
| 104 |
$forms = GlobalFields::get_instance()->normalize_fields($this->get_forms(), 'source', $this->id, $forms); |
| 105 |
return $forms; |
| 106 |
} |
| 107 |
|
| 108 |
public function get_forms() { |
| 109 |
$forms = Helper::get_post_titles_by_search($this->post_type, '', 10, ['prefix' => $this->key()]); |
| 110 |
return $forms; |
| 111 |
} |
| 112 |
|
| 113 |
public function restResponse($args) { |
| 114 |
// Check if inputValue is provided |
| 115 |
if (!empty($args['inputValue'])) { |
| 116 |
// Get the forms that match the inputValue and have the key prefix |
| 117 |
$forms = Helper::get_post_titles_by_search($this->post_type, $args['inputValue'], 10, ['prefix' => $this->key()]); |
| 118 |
// Normalize the fields and return as an indexed array |
| 119 |
$result = array_values(GlobalFields::get_instance()->normalize_fields($forms, 'source', $this->id)); |
| 120 |
return $result; |
| 121 |
} |
| 122 |
|
| 123 |
if (isset($args['form_id'])) { |
| 124 |
if( is_array( $args['form_id'] ) ) { |
| 125 |
$form_id = intval($args['form_id']['value']); |
| 126 |
}else{ |
| 127 |
$form_id = intval($args['form_id']); |
| 128 |
} |
| 129 |
|
| 130 |
$form = get_post($form_id); |
| 131 |
|
| 132 |
$keys = $this->keys_generator($form->post_content); |
| 133 |
|
| 134 |
$returned_keys = array(); |
| 135 |
|
| 136 |
if (is_array($keys) && !empty($keys)) { |
| 137 |
foreach ($keys as $key => $value) { |
| 138 |
$returned_keys[] = array( |
| 139 |
// 'text' => ucwords( str_replace( '_', ' ', str_replace( '-', ' ', $key ) ) ), |
| 140 |
'label' => $value, |
| 141 |
'value' => "tag_$key", |
| 142 |
); |
| 143 |
} |
| 144 |
return $returned_keys; |
| 145 |
} |
| 146 |
} |
| 147 |
wp_send_json_error([]); |
| 148 |
} |
| 149 |
|
| 150 |
public function check_label($field) { |
| 151 |
$returned_field = ''; |
| 152 |
if (isset($field['label']) && !empty($field['label'])) { |
| 153 |
$returned_field = $field['label']; |
| 154 |
return $returned_field; |
| 155 |
} |
| 156 |
if (isset($field['type'])) { |
| 157 |
$returned_field = ucfirst($field['type']); |
| 158 |
return $returned_field; |
| 159 |
} |
| 160 |
return $returned_field; |
| 161 |
} |
| 162 |
|
| 163 |
public function keys_generator($fieldsString) { |
| 164 |
$fields = array(); |
| 165 |
$fieldsdata = json_decode($fieldsString, true); |
| 166 |
if (!empty($fieldsdata) && isset($fieldsdata['fields']) && !empty($fieldsdata['fields'])) { |
| 167 |
foreach ($fieldsdata['fields'] as $key => $fielditem) { |
| 168 |
// if (NotificationX_Helper::filter_contactform_key_names($fielditem['label'])){ |
| 169 |
if (isset($fielditem['type']) && $fielditem['type'] === 'name') { |
| 170 |
$format = explode('-', $fielditem['format']); |
| 171 |
foreach ($format as $fKey) { |
| 172 |
$fields[$key . '_' . $fKey . '_name'] = ucfirst($fKey) . ' Name'; |
| 173 |
} |
| 174 |
} |
| 175 |
$fields[$key . "_" . $fielditem['type']] = $this->check_label($fielditem); |
| 176 |
// } |
| 177 |
} |
| 178 |
} |
| 179 |
return $fields; |
| 180 |
} |
| 181 |
|
| 182 |
public function save_new_records($fields, $entry, $form_data, $entry_id) { |
| 183 |
foreach ($fields as $field) { |
| 184 |
if ($field['type'] === 'checkbox') { |
| 185 |
continue; |
| 186 |
} |
| 187 |
if ($field['type'] === 'name') { |
| 188 |
if (!empty($entry['fields'][$field['id']]) && is_array($entry['fields'][$field['id']])) { |
| 189 |
foreach ($entry['fields'][$field['id']] as $nKey => $n) { |
| 190 |
$data[$field['id'] . '_' . $nKey . '_name'] = $n; |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
if ($field['type'] === 'email') { |
| 195 |
$data['email'] = $field['value']; |
| 196 |
} |
| 197 |
$data[$field['id'] . '_' . $field['type']] = $field['value']; |
| 198 |
} |
| 199 |
$data['title'] = $form_data['settings']['form_title']; |
| 200 |
$data['timestamp'] = time(); |
| 201 |
$data['id'] = $form_data['id']; |
| 202 |
if (!empty($data)) { |
| 203 |
$key = $this->key($form_data['id']); |
| 204 |
$this->save([ |
| 205 |
'source' => $this->id, |
| 206 |
'entry_key' => $key, |
| 207 |
'data' => $data, |
| 208 |
]); |
| 209 |
return true; |
| 210 |
} |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
public function key($key = '') { |
| 215 |
$key = $this->id . '_' . $key; |
| 216 |
return $key; |
| 217 |
} |
| 218 |
|
| 219 |
public function saved_post($post, $data, $nx_id) { |
| 220 |
$this->get_notification_ready($data); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* This function responsible for making ready the notifications for the first time |
| 225 |
* we have made a notification. |
| 226 |
* |
| 227 |
* @param string $type |
| 228 |
* @param array $data |
| 229 |
* @return void |
| 230 |
*/ |
| 231 |
public function get_notification_ready($data = array()) { |
| 232 |
if( !wpforms()->is_pro() ){ |
| 233 |
return []; |
| 234 |
} |
| 235 |
|
| 236 |
if( !empty( $data['__form_list']['value'] ) ) { |
| 237 |
$form_list = explode('_',$data['__form_list']['value']); |
| 238 |
if( !empty( $form_list[1] ) ) { |
| 239 |
$wpform_entries = wpforms()->entry->get_entries( [ 'form_id' => $form_list[1] ] ); |
| 240 |
$form_data = wpforms()->get( 'form' )->get( $form_list[1] ); |
| 241 |
if( count( $wpform_entries ) > 0 ) { |
| 242 |
$entries = []; |
| 243 |
foreach ($wpform_entries as $entry) { |
| 244 |
$fields = wpforms_decode($entry->fields); |
| 245 |
if( !empty( $fields ) ) { |
| 246 |
$entry_data = []; |
| 247 |
foreach ($fields as $field) { |
| 248 |
$sepereated_fields = ['name','email']; |
| 249 |
$sepereated_name_fields = ['first','middle','last']; |
| 250 |
if ($field['type'] === 'checkbox') { |
| 251 |
continue; |
| 252 |
} |
| 253 |
if ($field['type'] === 'name' ) { |
| 254 |
if (!empty($fields[$field['id']]) && is_array($fields[$field['id']])) { |
| 255 |
foreach ( $fields[$field['id']] as $nKey => $n) { |
| 256 |
if( in_array($nKey, $sepereated_name_fields) ) { |
| 257 |
$entry_data[$field['id'] . '_' . $nKey . '_name'] = $n; |
| 258 |
} |
| 259 |
} |
| 260 |
$entry_data[$field['id'] . '_name'] = $fields[$field['id']]['first']. ' '. $fields[$field['id']]['middle']. ' '. $fields[$field['id']]['last']. ' '; |
| 261 |
} |
| 262 |
} |
| 263 |
if ( $field['type'] === 'email' ) { |
| 264 |
$entry_data['email'] = $field['value']; |
| 265 |
} |
| 266 |
if( !in_array( $field['type'], $sepereated_fields ) ) { |
| 267 |
$entry_data[$field['id'] . '_' . $field['type']] = $field['value']; |
| 268 |
} |
| 269 |
} |
| 270 |
$entry_data['title'] = $form_data->post_title ? $form_data->post_title : ''; |
| 271 |
$entry_data['timestamp'] = time(); |
| 272 |
$entry_data['id'] = $data['nx_id']; |
| 273 |
$entry_data['entry__id'] = $entry->entry_id; |
| 274 |
if( $this->is_entry_exists( (int) $data['nx_id'], $entry->entry_id ) ) { |
| 275 |
continue; |
| 276 |
} |
| 277 |
if (!empty($entry_data)) { |
| 278 |
$key = $this->key($form_list[1]); |
| 279 |
$entries[] = [ |
| 280 |
'nx_id' => $data['nx_id'], |
| 281 |
'source' => $this->id, |
| 282 |
'entry_key' => $key, |
| 283 |
'data' => $entry_data, |
| 284 |
]; |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
$this->update_notifications($entries); |
| 289 |
} |
| 290 |
|
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
public function is_entry_exists( $nx_id, $entry_id ) { |
| 296 |
$entries = Entries::get_instance()->get_entries($nx_id); |
| 297 |
$filteredData = array_filter($entries, function ($item) use ($entry_id) { |
| 298 |
return $item['entry__id'] === $entry_id; |
| 299 |
}); |
| 300 |
return $filteredData ? true : false; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Limit entry by selected form in 'Select a Form'; |
| 305 |
* |
| 306 |
* @param bool $return |
| 307 |
* @param array $entry |
| 308 |
* @param array $settings |
| 309 |
* @return boolean |
| 310 |
*/ |
| 311 |
public function can_entry($return, $entry, $settings){ |
| 312 |
if(!empty($settings['form_list']) && !empty($entry['entry_key'])){ |
| 313 |
$selected_form = $settings['form_list']; |
| 314 |
$form_id = $entry['entry_key']; |
| 315 |
if($selected_form != $form_id){ |
| 316 |
return false; |
| 317 |
} |
| 318 |
} |
| 319 |
return $return; |
| 320 |
} |
| 321 |
|
| 322 |
public function filter_by_form($data, $settings){ |
| 323 |
if( empty( $settings['form_list'] )) { |
| 324 |
return $data; |
| 325 |
} |
| 326 |
|
| 327 |
$new_data = []; |
| 328 |
|
| 329 |
if( ! empty( $data ) ) { |
| 330 |
foreach( $data as $key => $entry ) { |
| 331 |
$selected_form = $settings['form_list']; |
| 332 |
$form_id = $entry['entry_key']; |
| 333 |
if($selected_form == $form_id){ |
| 334 |
$new_data[] = $entry; |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
return $new_data; |
| 339 |
} |
| 340 |
|
| 341 |
public function doc() { |
| 342 |
return sprintf(__('<p>Make sure that you have <a target="_blank" href="%1$s">WPForms 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> |
| 343 |
<p>🎦 <a target="_blank" href="%3$s">Watch video tutorial</a> to learn quickly</p> |
| 344 |
<p>👉 NotificationX <a target="_blank" href="%4$s">Integration with WPForms</a></p> |
| 345 |
<p><strong>Recommended Blogs:</strong></p> |
| 346 |
<p>🔥Hacks to Increase Your <a target="_blank" href="%5$s">WordPress Contact Forms Submission Rate</a> Using NotificationX</p>', 'notificationx'), |
| 347 |
'https://wordpress.org/plugins/wpforms-lite/', |
| 348 |
'https://notificationx.com/docs/wpforms/', |
| 349 |
'https://www.youtube.com/watch?v=8tk7_ZawJN8', |
| 350 |
'https://notificationx.com/integrations/wpforms/', |
| 351 |
'https://notificationx.com/blog/wordpress-contact-forms/' |
| 352 |
); |
| 353 |
} |
| 354 |
} |
| 355 |
|