PluginProbe
Stream – Activity Log & Audit Trail / 4.0.2
Stream – Activity Log & Audit Trail v4.0.2
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
stream / connectors / class-connector-gravityforms.php

class-connector-gravityforms.php in Stream – Activity Log & Audit Trail 4.0.2, at connectors/class-connector-gravityforms.php

1,053 lines 27.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Connector for Gravity Forms
4 *
5 * @package WP_Stream
6 */
7
8 namespace WP_Stream;
9
10 /**
11 * Class - Connector_GravityForms
12 */
13 class Connector_GravityForms extends Connector {
14 /**
15 * Connector slug
16 *
17 * @var string
18 */
19 public $name = 'gravityforms';
20
21 /**
22 * Holds tracked plugin minimum version required
23 *
24 * @const string
25 */
26 const PLUGIN_MIN_VERSION = '1.9.14';
27
28 /**
29 * Actions registered for this connector
30 *
31 * @var array
32 */
33 public $actions = array(
34 'gform_after_save_form',
35 'gform_pre_confirmation_save',
36 'gform_pre_notification_save',
37 'gform_pre_notification_deleted',
38 'gform_pre_confirmation_deleted',
39 'gform_before_delete_form',
40 'gform_post_form_trashed',
41 'gform_post_form_restored',
42 'gform_post_form_activated',
43 'gform_post_form_deactivated',
44 'gform_post_form_duplicated',
45 'gform_post_form_views_deleted',
46 'gform_post_export_entries',
47 'gform_forms_post_import',
48 'gform_delete_lead',
49 'gform_post_note_added',
50 'gform_pre_note_deleted',
51 'gform_update_status',
52 'gform_update_is_read',
53 'gform_update_is_starred',
54 'update_option',
55 'add_option',
56 'delete_option',
57 'update_site_option',
58 'add_site_option',
59 'delete_site_option',
60 );
61
62 /**
63 * Tracked option keys
64 *
65 * @var array
66 */
67 public $options = array();
68
69 /**
70 * Tracking registered Settings, with overridden data
71 *
72 * @var array
73 */
74 public $options_override = array();
75
76 /**
77 * Check if plugin dependencies are satisfied and add an admin notice if not
78 *
79 * @return bool
80 */
81 public function is_dependency_satisfied() {
82 if ( class_exists( 'GFForms' ) && version_compare( \GFCommon::$version, self::PLUGIN_MIN_VERSION, '>=' ) ) {
83 return true;
84 }
85
86 return false;
87 }
88
89 /**
90 * Return translated connector label
91 *
92 * @return string Translated connector label
93 */
94 public function get_label() {
95 return esc_html_x( 'Gravity Forms', 'gravityforms', 'stream' );
96 }
97
98 /**
99 * Return translated action labels
100 *
101 * @return array Action label translations
102 */
103 public function get_action_labels() {
104 return array(
105 'created' => esc_html_x( 'Created', 'gravityforms', 'stream' ),
106 'updated' => esc_html_x( 'Updated', 'gravityforms', 'stream' ),
107 'exported' => esc_html_x( 'Exported', 'gravityforms', 'stream' ),
108 'imported' => esc_html_x( 'Imported', 'gravityforms', 'stream' ),
109 'added' => esc_html_x( 'Added', 'gravityforms', 'stream' ),
110 'deleted' => esc_html_x( 'Deleted', 'gravityforms', 'stream' ),
111 'trashed' => esc_html_x( 'Trashed', 'gravityforms', 'stream' ),
112 'untrashed' => esc_html_x( 'Restored', 'gravityforms', 'stream' ),
113 'duplicated' => esc_html_x( 'Duplicated', 'gravityforms', 'stream' ),
114 'activated' => esc_html_x( 'Activated', 'gravityforms', 'stream' ),
115 'deactivated' => esc_html_x( 'Deactivated', 'gravityforms', 'stream' ),
116 'views_deleted' => esc_html_x( 'Views Reset', 'gravityforms', 'stream' ),
117 'starred' => esc_html_x( 'Starred', 'gravityforms', 'stream' ),
118 'unstarred' => esc_html_x( 'Unstarred', 'gravityforms', 'stream' ),
119 );
120 }
121
122 /**
123 * Return translated context labels
124 *
125 * @return array Context label translations
126 */
127 public function get_context_labels() {
128 return array(
129 'forms' => esc_html_x( 'Forms', 'gravityforms', 'stream' ),
130 'settings' => esc_html_x( 'Settings', 'gravityforms', 'stream' ),
131 'export' => esc_html_x( 'Import/Export', 'gravityforms', 'stream' ),
132 'entries' => esc_html_x( 'Entries', 'gravityforms', 'stream' ),
133 'notes' => esc_html_x( 'Notes', 'gravityforms', 'stream' ),
134 );
135 }
136
137 /**
138 * Add action links to Stream drop row in admin list screen
139 *
140 * @filter wp_stream_action_links_{connector}
141 *
142 * @param array $links Previous links registered.
143 * @param object $record Stream record.
144 *
145 * @return array Action links
146 */
147 public function action_links( $links, $record ) {
148 if ( 'forms' === $record->context ) {
149 $links[ esc_html__( 'Edit', 'stream' ) ] = add_query_arg(
150 array(
151 'page' => 'gf_edit_forms',
152 'id' => $record->object_id,
153 ),
154 admin_url( 'admin.php' )
155 );
156 } elseif ( 'entries' === $record->context ) {
157 $links[ esc_html__( 'View', 'stream' ) ] = add_query_arg(
158 array(
159 'page' => 'gf_entries',
160 'view' => 'entry',
161 'lid' => $record->object_id,
162 'id' => $record->get_meta( 'form_id', true ),
163 ),
164 admin_url( 'admin.php' )
165 );
166 } elseif ( 'notes' === $record->context ) {
167 $links[ esc_html__( 'View', 'stream' ) ] = add_query_arg(
168 array(
169 'page' => 'gf_entries',
170 'view' => 'entry',
171 'lid' => $record->get_meta( 'lead_id', true ),
172 'id' => $record->get_meta( 'form_id', true ),
173 ),
174 admin_url( 'admin.php' )
175 );
176 } elseif ( 'settings' === $record->context ) {
177 $links[ esc_html__( 'Edit Settings', 'stream' ) ] = add_query_arg(
178 array(
179 'page' => 'gf_settings',
180 ),
181 admin_url( 'admin.php' )
182 );
183 }
184
185 return $links;
186 }
187
188 /**
189 * Register all context hooks
190 */
191 public function register() {
192 parent::register();
193
194 $this->options = array(
195 'rg_gforms_disable_css' => array(
196 'label' => esc_html_x( 'Output CSS', 'gravityforms', 'stream' ),
197 ),
198 'rg_gforms_enable_html5' => array(
199 'label' => esc_html_x( 'Output HTML5', 'gravityforms', 'stream' ),
200 ),
201 'gform_enable_noconflict' => array(
202 'label' => esc_html_x( 'No-Conflict Mode', 'gravityforms', 'stream' ),
203 ),
204 'rg_gforms_currency' => array(
205 'label' => esc_html_x( 'Currency', 'gravityforms', 'stream' ),
206 ),
207 'rg_gforms_captcha_public_key' => array(
208 'label' => esc_html_x( 'reCAPTCHA Public Key', 'gravityforms', 'stream' ),
209 ),
210 'rg_gforms_captcha_private_key' => array(
211 'label' => esc_html_x( 'reCAPTCHA Private Key', 'gravityforms', 'stream' ),
212 ),
213 'rg_gforms_key' => null,
214 );
215 }
216
217 /**
218 * Track Create/Update actions on Forms
219 *
220 * @param array $form Form data.
221 * @param bool $is_new Is this a new form?.
222 */
223 public function callback_gform_after_save_form( $form, $is_new ) {
224 $id = $form['id'];
225
226 $this->log(
227 sprintf(
228 /* translators: %1$s a form title, %2$s a status (e.g. "Contact Form", "created") */
229 __( '"%1$s" form %2$s', 'stream' ),
230 $this->get_form_title_for_message( $form ),
231 $this->get_status_for_message(
232 $is_new,
233 esc_html__( 'created', 'stream' ),
234 esc_html__( 'updated', 'stream' )
235 )
236 ),
237 array(
238 'action' => $is_new,
239 'id' => $id,
240 'title' => $form['title'],
241 ),
242 $id,
243 'forms',
244 $is_new ? 'created' : 'updated'
245 );
246 }
247
248 /**
249 * Track saving form confirmations
250 *
251 * @param array $confirmation Confirmation data.
252 * @param array $form Form data.
253 * @param bool $is_new Is this a new form?.
254 */
255 public function callback_gform_pre_confirmation_save( $confirmation, $form, $is_new = true ) {
256 if ( ! isset( $is_new ) ) {
257 $is_new = false;
258 }
259
260 $this->log(
261 sprintf(
262 /* translators: %1$s: a confirmation name, %2$s: a status, %3$s: a form title (e.g. "Email", "created", "Contact Form") */
263 __( '"%1$s" confirmation %2$s for "%3$s"', 'stream' ),
264 $this->get_name_for_message( $confirmation ),
265 $this->get_status_for_message(
266 $is_new,
267 esc_html__( 'created', 'stream' ),
268 esc_html__( 'updated', 'stream' )
269 ),
270 $this->get_form_title_for_message( $form )
271 ),
272 array(
273 'is_new' => $is_new,
274 'form_id' => $form['id'],
275 ),
276 $form['id'],
277 'forms',
278 'updated'
279 );
280
281 return $confirmation;
282 }
283
284 /**
285 * Track saving form notifications
286 *
287 * @param array $notification Notification data.
288 * @param array $form Form data.
289 * @param bool $is_new Is this a new form?.
290 * @return array
291 */
292 public function callback_gform_pre_notification_save( $notification, $form, $is_new = true ) {
293 if ( ! isset( $is_new ) ) {
294 $is_new = false;
295 }
296
297 $this->log(
298 sprintf(
299 /* translators: %1$s: a notification name, %2$s: a status, %3$s: a form title (e.g. "Email", "created", "Contact Form") */
300 __( '"%1$s" notification %2$s for "%3$s"', 'stream' ),
301 $this->get_name_for_message( $notification ),
302 $this->get_status_for_message(
303 $is_new,
304 esc_html__( 'created', 'stream' ),
305 esc_html__( 'updated', 'stream' )
306 ),
307 $this->get_form_title_for_message( $form )
308 ),
309 array(
310 'is_update' => $is_new,
311 'form_id' => $form['id'],
312 ),
313 $form['id'],
314 'forms',
315 'updated'
316 );
317
318 return $notification;
319 }
320
321 /**
322 * Track deletion of notifications
323 *
324 * @param array $notification Notification data.
325 * @param array $form Form data.
326 */
327 public function callback_gform_pre_notification_deleted( $notification, $form ) {
328 $this->log(
329 sprintf(
330 /* translators: %1$s: a notification name, %2$s: a form title (e.g. "Email", "Contact Form") */
331 __( '"%1$s" notification deleted from "%2$s"', 'stream' ),
332 $this->get_name_for_message( $notification ),
333 $this->get_form_title_for_message( $form )
334 ),
335 array(
336 'form_id' => $form['id'],
337 'notification' => $notification,
338 ),
339 $form['id'],
340 'forms',
341 'updated'
342 );
343 }
344
345 /**
346 * Track deletion of confirmations
347 *
348 * @param array $confirmation Confirmation data.
349 * @param array $form Form data.
350 */
351 public function callback_gform_pre_confirmation_deleted( $confirmation, $form ) {
352 $this->log(
353 sprintf(
354 /* translators: %1$s: a confirmation name, %2$s: a form title (e.g. "Email", "Contact Form") */
355 __( '"%1$s" confirmation deleted from "%2$s"', 'stream' ),
356 $this->get_name_for_message( $confirmation ),
357 $this->get_form_title_for_message( $form )
358 ),
359 array(
360 'form_id' => $form['id'],
361 'confirmation' => $confirmation,
362 ),
363 $form['id'],
364 'forms',
365 'updated'
366 );
367 }
368
369 /**
370 * Track status change of confirmations
371 *
372 * @param array $confirmation Confirmation data.
373 * @param array $form Form data.
374 * @param bool $is_active Is this form active?.
375 */
376 public function callback_gform_confirmation_status( $confirmation, $form, $is_active ) {
377 $this->log(
378 sprintf(
379 /* translators: %1$s: a confirmation name, %2$s: a status, %3$s: a form title (e.g. "Email", "activated", "Contact Form") */
380 __( '"%1$s" confirmation %2$s from "%3$s"', 'stream' ),
381 $this->get_name_for_message( $confirmation ),
382 $this->get_status_for_message(
383 $is_active,
384 esc_html__( 'activated', 'stream' ),
385 esc_html__( 'deactivated', 'stream' )
386 ),
387 $this->get_form_title_for_message( $form )
388 ),
389 array(
390 'form_id' => $form['id'],
391 'confirmation' => $confirmation,
392 'is_active' => $is_active,
393 ),
394 null,
395 'forms',
396 'updated'
397 );
398 }
399
400 /**
401 * Track status change of notifications
402 *
403 * @param array $notification Notification data.
404 * @param array $form Form data.
405 * @param bool $is_active Is this form active?.
406 */
407 public function callback_gform_notification_status( $notification, $form, $is_active ) {
408 $this->log(
409 sprintf(
410 /* translators: %1$s: a notification name, %2$s: a status, %3$s: a form title (e.g. "Email", "activated", "Contact Form") */
411 __( '"%1$s" notification %2$s from "%3$s"', 'stream' ),
412 $this->get_name_for_message( $notification ),
413 $this->get_status_for_message(
414 $is_active,
415 esc_html__( 'activated', 'stream' ),
416 esc_html__( 'deactivated', 'stream' )
417 ),
418 $this->get_form_title_for_message( $form )
419 ),
420 array(
421 'form_id' => $form['id'],
422 'notification' => $notification,
423 'is_active' => $is_active,
424 ),
425 $form['id'],
426 'forms',
427 'updated'
428 );
429 }
430
431 /**
432 * Track GravityForms-specific option changes.
433 *
434 * @param string $option Option key.
435 * @param string $old_value Old value.
436 * @param string $new_value New value.
437 */
438 public function callback_update_option( $option, $old_value, $new_value ) {
439 $this->check( $option, $old_value, $new_value );
440 }
441
442 /**
443 * Track GravityForms-specific option creations.
444 *
445 * @param string $option Option key.
446 * @param string $val Value.
447 */
448 public function callback_add_option( $option, $val ) {
449 $this->check( $option, null, $val );
450 }
451
452 /**
453 * Track GravityForms-specific option deletions.
454 *
455 * @param string $option Option key.
456 */
457 public function callback_delete_option( $option ) {
458 $this->check( $option, null, null );
459 }
460
461 /**
462 * Track GravityForms-specific site option changes
463 *
464 * @param string $option Option key.
465 * @param string $old_value Old value.
466 * @param string $new_value New value.
467 */
468 public function callback_update_site_option( $option, $old_value, $new_value ) {
469 $this->check( $option, $old_value, $new_value );
470 }
471
472 /**
473 * Track GravityForms-specific site option creations.
474 *
475 * @param string $option Option key.
476 * @param string $val Value.
477 */
478 public function callback_add_site_option( $option, $val ) {
479 $this->check( $option, null, $val );
480 }
481
482 /**
483 * Track GravityForms-specific site option deletions.
484 *
485 * @param string $option Option key.
486 */
487 public function callback_delete_site_option( $option ) {
488 $this->check( $option, null, null );
489 }
490
491 /**
492 * Logs GravityForms-specific (site) option activity.
493 *
494 * @param string $option Option key.
495 * @param string $old_value Old value.
496 * @param string $new_value New value.
497 */
498 public function check( $option, $old_value, $new_value ) {
499 if ( ! array_key_exists( $option, $this->options ) ) {
500 return;
501 }
502
503 if ( is_null( $this->options[ $option ] ) ) {
504 call_user_func( array( $this, 'check_' . str_replace( '-', '_', $option ) ), $old_value, $new_value );
505 } else {
506 $data = $this->options[ $option ];
507 $option_title = $data['label'];
508 $context = isset( $data['context'] ) ? $data['context'] : 'settings';
509
510 $this->log(
511 /* translators: %s: a setting title (e.g. "Language") */
512 __( '"%s" setting updated', 'stream' ),
513 compact( 'option_title', 'option', 'old_value', 'new_value' ),
514 null,
515 $context,
516 isset( $data['action'] ) ? $data['action'] : 'updated'
517 );
518 }
519 }
520
521 /**
522 * Log GravityForm license key changes
523 *
524 * @param string $old_value Old license key.
525 * @param string $new_value New license key.
526 * @return void
527 */
528 public function check_rg_gforms_key( $old_value, $new_value ) {
529 $is_update = ( $new_value && strlen( $new_value ) );
530 $option = 'rg_gforms_key';
531
532 $this->log(
533 sprintf(
534 /* translators: %s: a status (e.g. "updated") */
535 __( 'Gravity Forms license key %s', 'stream' ),
536 $this->get_status_for_message(
537 $is_update,
538 esc_html__( 'updated', 'stream' ),
539 esc_html__( 'deleted', 'stream' )
540 )
541 ),
542 compact( 'option', 'old_value', 'new_value' ),
543 null,
544 'settings',
545 $is_update ? 'updated' : 'deleted'
546 );
547 }
548
549 /**
550 * Logs form entry exports.
551 *
552 * @action gform_post_export_entries
553 *
554 * @param object $form Form data.
555 * @param string $start_date Form start date.
556 * @param string $end_date Form completion date.
557 * @param array $fields Form fields data.
558 */
559 public function callback_gform_post_export_entries( $form, $start_date, $end_date, $fields ) {
560 unset( $fields );
561 $this->log(
562 /* translators: %s: a form title (e.g. "Contact Form") */
563 __( '"%s" form entries exported', 'stream' ),
564 array(
565 'form_title' => $form['title'],
566 'form_id' => $form['id'],
567 'start_date' => empty( $start_date ) ? null : $start_date,
568 'end_date' => empty( $end_date ) ? null : $end_date,
569 ),
570 $form['id'],
571 'export',
572 'exported'
573 );
574 }
575
576 /**
577 * Logs form imports.
578 *
579 * @action gform_forms_post_import
580 *
581 * @param array $forms List of form data.
582 */
583 public function callback_gform_forms_post_import( $forms ) {
584 $forms_total = count( $forms );
585 $forms_ids = wp_list_pluck( $forms, 'id' );
586 $forms_titles = wp_list_pluck( $forms, 'title' );
587
588 $this->log(
589 /* translators: %d: a number of forms (e.g. "42") */
590 _n( '%d form imported', '%d forms imported', $forms_total, 'stream' ),
591 array(
592 'count' => $forms_total,
593 'ids' => $forms_ids,
594 'titles' => $forms_titles,
595 ),
596 null,
597 'export',
598 'imported'
599 );
600 }
601
602 /**
603 * Logs form exports
604 *
605 * @action gform_export_separator
606 *
607 * @param string $dummy Unused.
608 * @param int $form_id Form ID.
609 */
610 public function callback_gform_export_separator( $dummy, $form_id ) {
611 $form = $this->get_form( $form_id );
612
613 $this->log(
614 /* translators: %s: a form title (e.g. "Contact Form") */
615 __( '"%s" form exported', 'stream' ),
616 array(
617 'form_title' => $form['title'],
618 'form_id' => $form_id,
619 ),
620 $form_id,
621 'export',
622 'exported'
623 );
624
625 return $dummy;
626 }
627
628 /**
629 * Log bulk form exports
630 *
631 * @param string $dummy Unused.
632 * @param array $forms Form data.
633 */
634 public function callback_gform_export_options( $dummy, $forms ) {
635 $ids = wp_list_pluck( $forms, 'id' );
636 $titles = wp_list_pluck( $forms, 'title' );
637
638 $this->log(
639 /* translators: %d: a number of forms (e.g. "42") */
640 _n( 'Export process started for %d form', 'Export process started for %d forms', count( $forms ), 'stream' ),
641 array(
642 'count' => count( $forms ),
643 'ids' => $ids,
644 'titles' => $titles,
645 ),
646 null,
647 'export',
648 'imported'
649 );
650
651 return $dummy;
652 }
653
654 /**
655 * Logs lead deletions.
656 *
657 * @action gform_delete_lead
658 *
659 * @param int $lead_id Lead ID.
660 * @return void
661 */
662 public function callback_gform_delete_lead( $lead_id ) {
663 $lead = $this->get_lead( $lead_id );
664 $form = $this->get_form( $lead['form_id'] );
665
666 $this->log(
667 /* translators: %1$d: to an ID, %2$s: a form title (e.g. "42", "Contact Form") */
668 __( 'Lead #%1$d from "%2$s" deleted', 'stream' ),
669 array(
670 'lead_id' => $lead_id,
671 'form_title' => $form['title'],
672 'form_id' => $form['id'],
673 ),
674 $lead_id,
675 'entries',
676 'deleted'
677 );
678 }
679
680 /**
681 * Logs note creation on lead.
682 *
683 * @action gform_post_note_added
684 *
685 * @param int $note_id Note ID.
686 * @param int $lead_id Lead ID.
687 * @param int $user_id User ID of note author.
688 * @param string $user_name Username of note author.
689 * @param string $note Note object.
690 * @param string $note_type Note type.
691 */
692 public function callback_gform_post_note_added( $note_id, $lead_id, $user_id, $user_name, $note, $note_type ) {
693 unset( $user_id );
694 unset( $user_name );
695 unset( $note );
696 unset( $note_type );
697
698 // Skip if no entry/lead id (e.g. Save and Continue notifications).
699 if ( empty( $lead_id ) ) {
700 return;
701 }
702
703 $lead = \GFFormsModel::get_lead( $lead_id );
704 $form = $this->get_form( $lead['form_id'] );
705
706 $this->log(
707 /* translators: %1$d: an ID, %2$d: another ID, %3$s: a form title (e.g. "42", "7", "Contact Form") */
708 __( 'Note #%1$d added to lead #%2$d on "%3$s" form', 'stream' ),
709 array(
710 'note_id' => $note_id,
711 'lead_id' => $lead_id,
712 'form_title' => $form['title'],
713 'form_id' => $form['id'],
714 ),
715 $note_id,
716 'notes',
717 'added'
718 );
719 }
720
721 /**
722 * Logs note deletion
723 *
724 * @action gform_pre_note_deleted
725 *
726 * @param int $note_id Note ID.
727 * @param int $lead_id Lead ID.
728 */
729 public function callback_gform_pre_note_deleted( $note_id, $lead_id ) {
730 $lead = $this->get_lead( $lead_id );
731 $form = $this->get_form( $lead['form_id'] );
732
733 $this->log(
734 /* translators: %2$d an ID, another ID, and a form title (e.g. "42", "7", "Contact Form") */
735 __( 'Note #%1$d deleted from lead #%2$d on "%3$s" form', 'stream' ),
736 array(
737 'note_id' => $note_id,
738 'lead_id' => $lead_id,
739 'form_title' => $form['title'],
740 'form_id' => $form['id'],
741 ),
742 $note_id,
743 'notes',
744 'deleted'
745 );
746 }
747
748 /**
749 * Logs form status updates.
750 *
751 * @action gform_update_status
752 *
753 * @param int $lead_id Lead ID.
754 * @param string $status New form status.
755 * @param string $prev Old form status.
756 */
757 public function callback_gform_update_status( $lead_id, $status, $prev = '' ) {
758 $lead = $this->get_lead( $lead_id );
759 $form = $this->get_form( $lead['form_id'] );
760
761 if ( 'active' === $status && 'trash' === $prev ) {
762 $status = 'restore';
763 }
764
765 $actions = array(
766 'active' => esc_html__( 'activated', 'stream' ),
767 'spam' => esc_html__( 'marked as spam', 'stream' ),
768 'trash' => esc_html__( 'trashed', 'stream' ),
769 'restore' => esc_html__( 'restored', 'stream' ),
770 );
771
772 if ( ! isset( $actions[ $status ] ) ) {
773 return;
774 }
775
776 $this->log(
777 sprintf(
778 /* translators: %1$d: an ID, %2$s: a status, %3$s: a form title (e.g. "42", "activated", "Contact Form") */
779 __( 'Lead #%1$d %2$s on "%3$s" form', 'stream' ),
780 $lead_id,
781 $this->escape_percentages( $actions[ $status ] ),
782 $this->get_form_title_for_message( $form )
783 ),
784 array(
785 'lead_id' => $lead_id,
786 'form_title' => $form['title'],
787 'form_id' => $form['id'],
788 'status' => $status,
789 'prev' => $prev,
790 ),
791 $lead_id,
792 'entries',
793 $status
794 );
795 }
796
797 /**
798 * Callback fired when an entry is read/unread
799 *
800 * @action update_is_read
801 *
802 * @param int $lead_id Lead ID.
803 * @param string $status Status.
804 */
805 public function callback_gform_update_is_read( $lead_id, $status ) {
806 $lead = $this->get_lead( $lead_id );
807 $form = $this->get_form( $lead['form_id'] );
808
809 $this->log(
810 sprintf(
811 /* translators: %1$d: a lead ID, %2$s: a status, %3$s: a form ID, %4$s: a form title (e.g. "42", "unread", "Contact Form") */
812 __( 'Entry #%1$d marked as %2$s on form #%3$d ("%4$s")', 'stream' ),
813 $lead_id,
814 $this->get_status_for_message(
815 ! empty( $status ),
816 esc_html__( 'read', 'stream' ),
817 esc_html__( 'unread', 'stream' )
818 ),
819 $form['id'],
820 $this->get_form_title_for_message( $form )
821 ),
822 array(
823 'lead_id' => $lead_id,
824 'lead_status' => $status,
825 'form_id' => $form['id'],
826 'form_title' => $form['title'],
827 ),
828 $lead_id,
829 'entries',
830 'updated'
831 );
832 }
833
834 /**
835 * Callback fired when an entry is starred/unstarred
836 *
837 * @action gform_update_is_starred
838 *
839 * @param int $lead_id Lead ID.
840 * @param int $status Status.
841 */
842 public function callback_gform_update_is_starred( $lead_id, $status ) {
843 $lead = $this->get_lead( $lead_id );
844 $form = $this->get_form( $lead['form_id'] );
845 $status_for_message = $this->get_status_for_message(
846 ! empty( $status ),
847 esc_html__( 'starred', 'stream' ),
848 esc_html__( 'unstarred', 'stream' )
849 );
850 $action = $status_for_message;
851
852 $this->log(
853 sprintf(
854 /* translators: %1$d: an ID, %2$s: a status, %3$d: a form title (e.g. "42", "starred", "Contact Form") */
855 __( 'Entry #%1$d %2$s on form #%3$d ("%4$s")', 'stream' ),
856 $lead_id,
857 $status_for_message, // This has been escaped above.
858 $form['id'],
859 $this->get_form_title_for_message( $form )
860 ),
861 array(
862 'lead_id' => $lead_id,
863 'lead_status' => $status,
864 'form_id' => $form['id'],
865 'form_title' => $form['title'],
866 ),
867 $lead_id,
868 'entries',
869 $action
870 );
871 }
872
873 /**
874 * Callback fired when a form is deleted
875 *
876 * @action gform_before_delete_form
877 *
878 * @param int $form_id Form ID.
879 */
880 public function callback_gform_before_delete_form( $form_id ) {
881 $this->log_form_action( $form_id, 'deleted' );
882 }
883
884 /**
885 * Callback fired when a form is trashed
886 *
887 * @action gform_post_form_trashed
888 *
889 * @param int $form_id Form ID.
890 */
891 public function callback_gform_post_form_trashed( $form_id ) {
892 $this->log_form_action( $form_id, 'trashed' );
893 }
894
895 /**
896 * Callback fired when a form is restored
897 *
898 * @action gform_post_form_restored
899 *
900 * @param int $form_id Form ID.
901 */
902 public function callback_gform_post_form_restored( $form_id ) {
903 $this->log_form_action( $form_id, 'untrashed' );
904 }
905
906 /**
907 * Callback fired when a form is activated
908 *
909 * @action gform_post_form_activated
910 *
911 * @param int $form_id Form ID.
912 */
913 public function callback_gform_post_form_activated( $form_id ) {
914 $this->log_form_action( $form_id, 'activated' );
915 }
916
917 /**
918 * Callback fired when a form is deactivated
919 *
920 * @action gform_post_form_deactivated
921 *
922 * @param int $form_id Form ID.
923 */
924 public function callback_gform_post_form_deactivated( $form_id ) {
925 $this->log_form_action( $form_id, 'deactivated' );
926 }
927
928 /**
929 * Callback fired when a form is duplicated
930 *
931 * @action gform_post_form_duplicated
932 *
933 * @param int $form_id Form ID.
934 */
935 public function callback_gform_post_form_duplicated( $form_id ) {
936 $this->log_form_action( $form_id, 'duplicated' );
937 }
938
939 /**
940 * Callback fired when a form's views are reset
941 *
942 * @action gform_post_form_views_deleted
943 *
944 * @param int $form_id Form ID.
945 */
946 public function callback_gform_post_form_views_deleted( $form_id ) {
947 $this->log_form_action( $form_id, 'views_deleted' );
948 }
949
950 /**
951 * Track status change of forms
952 *
953 * @param int $form_id Form ID.
954 * @param string $action Action.
955 */
956 public function log_form_action( $form_id, $action ) {
957 $form = $this->get_form( $form_id );
958
959 if ( empty( $form ) ) {
960 return;
961 }
962
963 $actions = array(
964 'activated' => esc_html__( 'Activated', 'stream' ),
965 'deactivated' => esc_html__( 'Deactivated', 'stream' ),
966 'trashed' => esc_html__( 'Trashed', 'stream' ),
967 'untrashed' => esc_html__( 'Restored', 'stream' ),
968 'duplicated' => esc_html__( 'Duplicated', 'stream' ),
969 'deleted' => esc_html__( 'Deleted', 'stream' ),
970 'views_deleted' => esc_html__( 'Views Reset', 'stream' ),
971 );
972
973 $this->log(
974 sprintf(
975 /* translators: %1$d: an ID, %2$s: a form title, %3$s: a status (e.g. "42", "Contact Form", "Activated") */
976 __( 'Form #%1$d ("%2$s") %3$s', 'stream' ),
977 $form_id,
978 $this->get_form_title_for_message( $form ),
979 strtolower( $this->escape_percentages( $actions[ $action ] ) )
980 ),
981 array(
982 'form_id' => $form_id,
983 'form_title' => $form['title'],
984 'form_status' => strtolower( $action ),
985 ),
986 $form['id'],
987 'forms',
988 $action
989 );
990 }
991
992 /**
993 * Helper function to get a single entry
994 *
995 * @param int $lead_id Lead ID.
996 */
997 private function get_lead( $lead_id ) {
998 return \GFFormsModel::get_lead( $lead_id );
999 }
1000
1001 /**
1002 * Helper function to get a single form
1003 *
1004 * @param int $form_id Form ID.
1005 */
1006 private function get_form( $form_id ) {
1007 return \GFFormsModel::get_form_meta( $form_id );
1008 }
1009
1010 /**
1011 * Get the name from an associative array with percentages escaped.
1012 * To be used when calling $this->log().
1013 *
1014 * @param array $data_array The array with a 'name' key to be escaped.
1015 * @return string The name value with percentages escaped.
1016 */
1017 private function get_name_for_message( $data_array ) {
1018 if ( empty( $data_array['name'] ) ) {
1019 return $this->escape_percentages( __( 'This does not have a name key', 'stream' ) );
1020 }
1021
1022 return $this->escape_percentages( $data_array['name'] );
1023 }
1024
1025 /**
1026 * Get the form title from a form array with percentages escaped.
1027 * To be used when calling $this->log().
1028 *
1029 * @param array $form The form data array.
1030 * @return string The title value with percentages escaped.
1031 */
1032 private function get_form_title_for_message( $form ) {
1033 if ( empty( $form['title'] ) ) {
1034 return $this->escape_percentages( __( 'This does not have a title key', 'stream' ) );
1035 }
1036
1037 return $this->escape_percentages( $form['title'] );
1038 }
1039
1040 /**
1041 * Get the status to use in a message with percentages in translation escaped.
1042 *
1043 * @param bool $conditional Whether or not it's a new form.
1044 * @param string $true_string The string to return when the conditional is true.
1045 * @param string $false_string The string to return when the conditional is false.
1046 * @return string The status with percentages escaped.
1047 */
1048 private function get_status_for_message( $conditional, $true_string, $false_string ) {
1049 $status = $conditional ? $true_string : $false_string;
1050 return $this->escape_percentages( $status );
1051 }
1052 }
1053