Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.29% |
25 / 28 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Contact_Event | |
89.29% |
25 / 28 |
|
80.00% |
4 / 5 |
12.18 | |
0.00% |
0 / 1 |
| get_instance | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| created | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| updated | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
7 | |||
| deleted | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| before_delete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Contact Event. |
| 4 | * |
| 5 | * @package automattic/jetpack-crm |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\CRM\Event_Manager; |
| 9 | |
| 10 | use Automattic\Jetpack\CRM\Entities\Factories\Contact_Factory; |
| 11 | |
| 12 | /** |
| 13 | * Contact Event class. |
| 14 | * |
| 15 | * @since 6.2.0 |
| 16 | */ |
| 17 | class Contact_Event implements Event { |
| 18 | |
| 19 | /** |
| 20 | * The Contact_Event instance. |
| 21 | * |
| 22 | * @since 6.2.0 |
| 23 | * @var Contact_Event |
| 24 | */ |
| 25 | private static $instance = null; |
| 26 | |
| 27 | /** |
| 28 | * Properties that should not be notified. |
| 29 | * |
| 30 | * @since 6.2.0 |
| 31 | * @var string[] |
| 32 | */ |
| 33 | private $not_notifiable_props = array( |
| 34 | 'created', |
| 35 | 'lastupdated', |
| 36 | 'lastcontacted', |
| 37 | ); |
| 38 | |
| 39 | /** |
| 40 | * Get the singleton instance of this class. |
| 41 | * |
| 42 | * @since 6.2.0 |
| 43 | * |
| 44 | * @return Contact_Event The Contact_Event instance. |
| 45 | */ |
| 46 | public static function get_instance(): Contact_Event { |
| 47 | if ( ! self::$instance ) { |
| 48 | self::$instance = new self(); |
| 49 | } |
| 50 | |
| 51 | return self::$instance; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * A new contact was created. |
| 56 | * |
| 57 | * @since 6.2.0 |
| 58 | * |
| 59 | * @param array $contact_data The created contact data. |
| 60 | * @return void |
| 61 | */ |
| 62 | public function created( array $contact_data ): void { |
| 63 | $contact = Contact_Factory::create( $contact_data ); |
| 64 | |
| 65 | do_action( 'jpcrm_contact_created', $contact ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * The contact was updated. |
| 70 | * |
| 71 | * @since 6.2.0 |
| 72 | * |
| 73 | * @param array $contact_data The updated contact data. |
| 74 | * @param array $old_contact_data The old contact data. |
| 75 | * @return void |
| 76 | */ |
| 77 | public function updated( array $contact_data, array $old_contact_data ): void { |
| 78 | |
| 79 | // Note: Custom fields are not present in $dataArr. It's handled by addUpdateCustomField. |
| 80 | |
| 81 | // Skip social fields: tw, fb, li. They are handled/stored by the Metabox process |
| 82 | // Skip lastupdate to avoid intempestive updates |
| 83 | $fields_to_skip = array( 'tw', 'fb', 'li', 'lastupdated' ); |
| 84 | |
| 85 | $contact_updated = array(); |
| 86 | foreach ( $contact_data as $key => $value ) { |
| 87 | // Remove DB prefixes |
| 88 | $new_key = str_replace( 'zbsc_', '', $key ); |
| 89 | $new_key = str_replace( 'zbs_', '', $new_key ); |
| 90 | |
| 91 | if ( in_array( $new_key, $fields_to_skip, true ) ) { |
| 92 | continue; |
| 93 | } |
| 94 | $contact_updated[ $new_key ] = $value; |
| 95 | } |
| 96 | // Keep contact_data as is, without prefix, to pass it to the hooks |
| 97 | $contact_data = $contact_updated; |
| 98 | |
| 99 | // Clean up fields that don't exist in both arrays |
| 100 | $old_contact = array_intersect_key( $old_contact_data, $contact_updated ); |
| 101 | $contact_updated = array_intersect_key( $contact_updated, $old_contact ); |
| 102 | |
| 103 | $contact = Contact_Factory::create( $contact_updated ); |
| 104 | $previous_contact = Contact_Factory::create( $old_contact ); |
| 105 | |
| 106 | // Check for effective fields changes |
| 107 | $has_update = false; |
| 108 | foreach ( $contact_updated as $field => $value ) { |
| 109 | if ( $value !== $old_contact[ $field ] ) { |
| 110 | $has_update = true; |
| 111 | |
| 112 | // Notify only for notifiable fields |
| 113 | if ( ! in_array( $field, $this->not_notifiable_props, true ) ) { |
| 114 | do_action( 'jpcrm_contact_' . $field . '_updated', $contact, $previous_contact ); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if ( $has_update ) { |
| 120 | // General notification that contact was updated |
| 121 | do_action( 'jpcrm_contact_updated', $contact, $previous_contact ); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * A contact was deleted. |
| 127 | * |
| 128 | * @since 6.2.0 |
| 129 | * |
| 130 | * @param int $contact_id The contact ID. |
| 131 | * @return void |
| 132 | */ |
| 133 | public function deleted( int $contact_id ): void { |
| 134 | do_action( 'jpcrm_contact_deleted', $contact_id ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * A contact is about to be deleted. |
| 139 | * |
| 140 | * @since 6.2.0 |
| 141 | * |
| 142 | * @param int $contact_id The contact ID. |
| 143 | * @return void |
| 144 | */ |
| 145 | public function before_delete( int $contact_id ): void { |
| 146 | do_action( 'jpcrm_contact_before_delete', $contact_id ); |
| 147 | } |
| 148 | } |