PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.2.11
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.2.11
3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 All 156 releases
notificationx / includes / Extensions / NJF / NinjaForms.php

NinjaForms.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 3.2.11, at includes/Extensions/NJF/NinjaForms.php

417 lines 15.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * CF7 Extension
5 *
6 * @package NotificationX\Extensions
7 */
8
9 namespace NotificationX\Extensions\NJF;
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 * NinjaForms Extension
19 * @method static NinjaForms get_instance($args = null)
20 */
21 class NinjaForms extends Extension {
22 /**
23 * Instance of NinjaForms
24 *
25 * @var NinjaForms
26 */
27 use GetInstance;
28
29 public $priority = 15;
30 public $id = 'njf';
31 public $img = '';
32 public $doc_link = 'https://notificationx.com/docs/contact-form-submission-alert/';
33 public $types = 'form';
34 public $module = 'modules_njf';
35 public $module_priority = 10;
36 public $class = 'Ninja_Forms';
37
38 /**
39 * Initially Invoked when initialized.
40 */
41 public function __construct() {
42 parent::__construct();
43 }
44
45 public function init_extension()
46 {
47 $this->title = __('Ninja Forms', 'notificationx');
48 $this->module_title = __('Ninja Forms', 'notificationx');
49 }
50
51 public function init() {
52 parent::init();
53
54 add_action('ninja_forms_after_submission', array($this, 'save_new_records'));
55 }
56
57 public function init_fields(){
58 parent::init_fields();
59 add_filter('nx_form_list', [$this, 'nx_form_list'], 9);
60
61 }
62
63 /**
64 * This functions is hooked
65 *
66 * @return void
67 */
68 public function admin_actions() {
69 parent::admin_actions();
70
71 add_filter("nx_can_entry_{$this->id}", array($this, 'can_entry'), 10, 3);
72 }
73
74 /**
75 * This functions is hooked
76 *
77 * @hooked nx_public_action
78 * @return void
79 */
80 public function public_actions() {
81 parent::public_actions();
82
83 }
84
85 public function source_error_message($messages) {
86 if (!$this->class_exists()) {
87 $url = admin_url('plugin-install.php?s=ninja+forms&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 __( 'Ninja 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);
105 return array_merge($forms, $_forms);
106 }
107
108 public function get_forms() {
109 $forms = [];
110 if (!class_exists('Ninja_Forms')) {
111 return [];
112 }
113 global $wpdb;
114 $form_result = $wpdb->get_results('SELECT id, title FROM `' . $wpdb->prefix . 'nf3_forms` ORDER BY title');
115 if (!empty($form_result)) {
116 foreach ($form_result as $form) {
117 $key = $this->key($form->id);
118 $forms[$key] = $form->title;
119 }
120 }
121
122 return $forms;
123 }
124
125 public function saved_post($post, $data, $nx_id) {
126 $this->delete_notification(null, $nx_id);
127 $this->get_notification_ready($data);
128 }
129
130 /**
131 * This function responsible for making ready the notifications for the first time
132 * we have made a notification.
133 *
134 * @param string $type
135 * @param array $data
136 * @return void
137 */
138 public function get_notification_ready($data = array()) {
139 $form_list = !empty($data['__form_list']['value']) ? $data['__form_list']['value'] : (!empty($data['form_list']['value']) ? $data['form_list']['value'] : null);
140 if( !empty($form_list) ) {
141 $form_list = explode('_',$form_list);
142 $submissions = $this->get_submissions($form_list[1], $data);
143 if( count( $submissions ) > 0 ) {
144 $entries = [];
145 foreach ( $submissions as $submission ) {
146 if( !empty( $submission ) ) {
147 if (!empty($submission)) {
148 $key = $this->key($form_list[1]);
149 $entries[] = [
150 'nx_id' => $data['nx_id'],
151 'source' => $this->id,
152 'entry_key' => $key,
153 'data' => $submission,
154 ];
155 }
156 }
157 }
158 $this->update_notifications($entries);
159 }
160 }
161 }
162
163 public function get_submissions( $form_id, $data ) {
164 $subs = Ninja_Forms()->form( $form_id )->get_subs( array(), FALSE );
165 $fields = Ninja_Forms()->form( $form_id )->get_fields();
166 $hidden_field_types = apply_filters( 'nf_sub_hidden_field_types', array() );
167 $display_from = !empty( $data['display_from'] ) ? intval( $data['display_from'] ) : 30;
168 $cutoff_timestamp = strtotime("-{$display_from} days");
169
170 foreach( $subs as $sub ){
171 $timestamp = strtotime( $sub->get_sub_date('Y-m-d H:i') );
172 // Skip submissions older than $display_from days
173 if ($timestamp < $cutoff_timestamp) {
174 continue;
175 }
176 $value[ 'title' ] = $sub->get_form_title();
177 $value[ 'timestamp' ] = $timestamp;
178
179 // boolean - does this submission use a repeater
180 $hasRepeater = false;
181 // How many repeater submissions does this submission have
182 $submissionCount = 0;
183 // Ids of fields in the repeater
184 $fieldsetFieldIds=[];
185
186 foreach ($fields as $field_id => $field) {
187 // Bypass existing method if fieldset repeater
188 if('repeater'===$field->get_setting('type')){
189 $hasRepeater = true;
190
191 $fieldsetSubmission= $sub->get_field_value( $field_id );
192 $fieldsetSettings = $field->get_settings();
193 $fieldsetLabels = Ninja_Forms()->fieldsetRepeater
194 ->getFieldsetLabels($field_id, $fieldsetSettings, true);
195
196 foreach($fieldsetLabels as $fieldsetFieldId =>$fieldsetFieldLabel){
197
198 $fieldsetFieldIds[]=$fieldsetFieldId;
199
200 $field_labels[$fieldsetFieldId]= \WPN_Helper::maybe_escape_csv_column( $fieldsetFieldLabel );
201
202 $fieldType = Ninja_Forms()->fieldsetRepeater->getFieldtype($fieldsetFieldId, $fieldsetSettings);
203
204 $fieldsetFieldSubmissionCollection=Ninja_Forms()->fieldsetRepeater
205 ->extractSubmissionsByFieldsetField($fieldsetFieldId, $fieldsetSubmission);
206
207 $submissionCount = count($fieldsetFieldSubmissionCollection);
208
209 foreach ($fieldsetFieldSubmissionCollection as &$fieldsetFieldSubmission) {
210
211 if(is_array($fieldsetFieldSubmission['value'])){
212
213 $fieldsetFieldSubmission['value']= implode(', ',$fieldsetFieldSubmission['value']);
214 }
215 }
216
217
218 $value[$fieldsetFieldId]= array_column($fieldsetFieldSubmissionCollection,'value');
219 }
220
221 }else{
222 if (!is_int($field_id)) continue;
223 if( in_array( $field->get_setting( 'type' ), $hidden_field_types ) ) continue;
224
225 if ( $field->get_setting( 'admin_label' ) ) {
226 $field_labels[ $field->get_id() ] = \WPN_Helper::maybe_escape_csv_column( $field->get_setting( 'admin_label' ) );
227 } else {
228 $field_labels[ $field->get_id() ] = \WPN_Helper::maybe_escape_csv_column( $field->get_setting( 'label' ) );
229 }
230
231 $field_value = maybe_unserialize( $sub->get_field_value( $field_id ) );
232
233 $field_value = apply_filters('nf_subs_export_pre_value', $field_value, $field_id);
234 $field_value = apply_filters('ninja_forms_subs_export_pre_value', $field_value, $field_id, $form_id);
235 $field_value = apply_filters( 'ninja_forms_subs_export_field_value_' . $field->get_setting( 'type' ), $field_value, $field );
236
237 if ( is_array($field_value ) ) {
238 $field_value = implode( ',', $field_value );
239 }
240
241 $value[ $field_labels[ $field->get_id() ] ] = $field_value;
242
243 }
244 }
245
246 if(!$hasRepeater){
247 $value_array[] = $value;
248 }else{
249 // The the submission has repeater fields, create an indexed array first
250 $repeatingValueArray=[];
251 $index = 0;
252
253 do {
254 // iterate each column in the row 'value'
255 foreach($value as $fieldId=>$columnValue){
256
257 // If the column in the row value is not a repeater
258 // fieldset field, simply copy it into a new row of the
259 // repeating value array
260 if(!in_array($fieldId,$fieldsetFieldIds)){
261 $repeatingValueArray[$index][]=$columnValue;
262 }else{
263
264 // If the column in the row value is a repeater
265 // fieldset field, copy the next submission index value
266
267
268 $repeatingValueArray[$index][]=$columnValue[$index];
269 }
270 }
271 // at the end of the row value columns, increment the index
272 // until all the submission index values are added
273 $index++;
274 } while ($index < $submissionCount);
275
276 // After iterating the row value once for each submission index,
277 // add the repeatingValueArray to the value array
278
279 $value_array[]=$repeatingValueArray;
280 }
281
282 }
283 return $value_array;
284 }
285
286 public function restResponse($args) {
287 if (!class_exists('Ninja_Forms')) {
288 return [];
289 }
290
291 global $wpdb;
292 $table_name = $wpdb->prefix . 'nf3_forms';
293 if (!empty($args['inputValue'])) {
294 $limit = 10;
295 // Prepare the query with a LIKE condition
296 $query = $wpdb->prepare(
297 "SELECT id, title FROM {$table_name} WHERE title LIKE %s LIMIT %d",
298 '%' . $wpdb->esc_like($args['inputValue']) . '%',$limit
299 );
300 // Execute the query and retrieve the results
301 $form_result = $wpdb->get_results($query);
302 if (!empty($form_result)) {
303 foreach ($form_result as $form) {
304 $key = $this->key($form->id);
305 $forms[$key] = $form->title;
306 }
307 }
308 $result = array_values(GlobalFields::get_instance()->normalize_fields($forms, 'source', $this->id));
309 return $result;
310 }
311
312 if (isset($args['form_id'])) {
313 if( is_array( $args['form_id'] ) ) {
314 $form_id = intval($args['form_id']['value']);
315 }else{
316 $form_id = intval($args['form_id']);
317 }
318 $queryresult = $wpdb->get_results('SELECT meta_value FROM `' . $wpdb->prefix . 'nf3_form_meta` WHERE parent_id = ' . $form_id . ' AND meta_key = "formContentData"');
319
320 if(isset($queryresult[0]) && isset($queryresult[0]->meta_value)){
321 $formdata = $queryresult[0]->meta_value;
322
323 $keys = $this->keys_generator($formdata);
324
325 $returned_keys = array();
326
327 if (is_array($keys) && !empty($keys)) {
328 foreach ($keys as $key) {
329 $returned_keys[] = array(
330 'label' => ucwords(str_replace('_', ' ', str_replace('-', ' ', $key))),
331 'value' => "tag_$key",
332 );
333 }
334
335 return $returned_keys;
336 }
337 }
338 }
339 wp_send_json_error([]);
340 }
341
342 public function keys_generator($fieldsString) {
343 $fields = array();
344 $fieldsdata = unserialize($fieldsString);
345 if (!empty($fieldsdata)) {
346 foreach ($fieldsdata as $field) {
347 if(!is_string($field)){
348 $field = !empty($field['cells'][0]['fields'][0]) ? $field['cells'][0]['fields'][0] : null;
349 }
350 if ($field && Helper::filter_contactform_key_names($field)) {
351 $fields[] = Helper::rename_contactform_key_names($field);
352 }
353 }
354 }
355 return $fields;
356 }
357
358 public function save_new_records($form_data) {
359 foreach ($form_data['fields'] as $field) {
360 $arr = Helper::rename_contactform_key_names($field['key']);
361 $data[$arr] = $field['value'];
362 }
363 $data['title'] = $form_data['settings']['title'];
364 $data['timestamp'] = time();
365
366 if (!empty($data)) {
367 $key = $this->key($form_data['form_id']);
368 $this->save([
369 'source' => $this->id,
370 'entry_key' => $key,
371 'data' => $data,
372 ]);
373 return true;
374 }
375 return false;
376 }
377
378 public function key($key) {
379 $key = $this->id . '_' . $key;
380 return $key;
381 }
382
383 /**
384 * Limit entry by selected form in 'Select a Form';
385 *
386 * @param [type] $return
387 * @param [type] $entry
388 * @param [type] $settings
389 * @return boolean
390 */
391 public function can_entry($return, $entry, $settings){
392 if(!empty($settings['form_list']) && !empty($entry['entry_key'])){
393 $selected_form = $settings['form_list'];
394 $form_id = $entry['entry_key'];
395 if($selected_form != $form_id){
396 return false;
397 }
398
399 }
400 return $return;
401 }
402
403 public function doc() {
404 return sprintf(__('<p>Make sure that you have <a target="_blank" href="%1$s">Ninja Forms 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>
405 <p>🎦 <a target="_blank" href="%3$s">Watch video tutorial</a> to learn quickly</p>
406 <p>👉 NotificationX <a target="_blank" href="%4$s">Integration with Ninja Forms</a></p>
407 <p><strong>Recommended Blog:</strong></p>
408 <p>🔥 Hacks to Increase Your <a target="_blank" href="%5$s">WordPress Contact Forms Submission Rate</a> Using NotificationX</p>', 'notificationx'),
409 'https://wordpress.org/plugins/ninja-forms/',
410 'https://notificationx.com/docs/ninja-forms/',
411 'https://www.youtube.com/watch?v=Ibv84iGcBHE',
412 'https://notificationx.com/integrations/ninja-forms/',
413 'https://notificationx.com/blog/wordpress-contact-forms/'
414 );
415 }
416 }
417