updated-cct-item.php
171 lines
| 1 | <?php |
| 2 | /** |
| 3 | * UpdatedCCTItem. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category UpdatedCCTItem |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\JetEngineCCT\Triggers; |
| 15 | |
| 16 | use SureTriggers\Controllers\AutomationController; |
| 17 | use SureTriggers\Traits\SingletonLoader; |
| 18 | |
| 19 | if ( ! class_exists( 'UpdatedCCTItem' ) ) : |
| 20 | |
| 21 | /** |
| 22 | * UpdatedCCTItem |
| 23 | * |
| 24 | * @category UpdatedCCTItem |
| 25 | * @package SureTriggers |
| 26 | * @author BSF <username@example.com> |
| 27 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 28 | * @link https://www.brainstormforce.com/ |
| 29 | * @since 1.0.0 |
| 30 | * |
| 31 | * @psalm-suppress UndefinedTrait |
| 32 | */ |
| 33 | class UpdatedCCTItem { |
| 34 | |
| 35 | /** |
| 36 | * Integration type. |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | public $integration = 'JetEngineCCT'; |
| 41 | |
| 42 | /** |
| 43 | * Trigger name. |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | public $trigger = 'jet_engine_updated_cct_item'; |
| 48 | |
| 49 | use SingletonLoader; |
| 50 | |
| 51 | /** |
| 52 | * Constructor |
| 53 | * |
| 54 | * @since 1.0.0 |
| 55 | */ |
| 56 | public function __construct() { |
| 57 | add_filter( 'sure_trigger_register_trigger', [ $this, 'register' ] ); |
| 58 | add_action( 'init', [ $this, 'register_hooks' ], 99 ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Register action. |
| 63 | * |
| 64 | * @param array $triggers trigger data. |
| 65 | * @return array |
| 66 | */ |
| 67 | public function register( $triggers ) { |
| 68 | $triggers[ $this->integration ][ $this->trigger ] = [ |
| 69 | 'label' => __( 'Updated CCT Item', 'suretriggers' ), |
| 70 | 'action' => $this->trigger, |
| 71 | 'common_action' => [], |
| 72 | 'function' => [ $this, 'trigger_listener' ], |
| 73 | 'priority' => 10, |
| 74 | 'accepted_args' => 3, |
| 75 | ]; |
| 76 | |
| 77 | return $triggers; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Register hooks for all registered CCTs dynamically. |
| 82 | * JetEngine CCT hooks are slug-specific: jet-engine/custom-content-types/updated-item/{slug} |
| 83 | * |
| 84 | * @return void |
| 85 | */ |
| 86 | public function register_hooks() { |
| 87 | if ( ! class_exists( '\Jet_Engine\Modules\Custom_Content_Types\Module' ) ) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | $content_types = \Jet_Engine\Modules\Custom_Content_Types\Module::instance()->manager->get_content_types(); |
| 92 | |
| 93 | if ( empty( $content_types ) || ! is_array( $content_types ) ) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | foreach ( $content_types as $slug => $factory ) { |
| 98 | add_action( |
| 99 | 'jet-engine/custom-content-types/updated-item/' . $slug, |
| 100 | [ $this, 'trigger_listener' ], |
| 101 | 10, |
| 102 | 3 |
| 103 | ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Trigger listener for CCT item update. |
| 109 | * |
| 110 | * @param array $item Updated item data. |
| 111 | * @param array $prev_item Previous item data. |
| 112 | * @param object $item_handler Item handler instance. |
| 113 | * @return void |
| 114 | */ |
| 115 | public function trigger_listener( $item, $prev_item, $item_handler ) { |
| 116 | |
| 117 | if ( empty( $item ) ) { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | $cct_slug = ''; |
| 122 | $item_id = 0; |
| 123 | |
| 124 | if ( is_object( $item_handler ) && method_exists( $item_handler, 'get_factory' ) ) { |
| 125 | $factory = $item_handler->get_factory(); |
| 126 | if ( is_object( $factory ) && method_exists( $factory, 'get_arg' ) ) { |
| 127 | $cct_slug = $factory->get_arg( 'slug' ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if ( is_array( $item ) && isset( $item['_ID'] ) ) { |
| 132 | $item_id = $item['_ID']; |
| 133 | } |
| 134 | |
| 135 | $context = [ |
| 136 | 'cct_slug' => $cct_slug, |
| 137 | 'cct_item_id' => $item_id, |
| 138 | ]; |
| 139 | |
| 140 | if ( is_array( $item ) ) { |
| 141 | foreach ( $item as $key => $value ) { |
| 142 | $context[ $key ] = maybe_unserialize( $value ); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if ( is_array( $prev_item ) ) { |
| 147 | $previous = []; |
| 148 | foreach ( $prev_item as $key => $value ) { |
| 149 | $previous[ $key ] = maybe_unserialize( $value ); |
| 150 | } |
| 151 | $context['previous_item'] = $previous; |
| 152 | } |
| 153 | |
| 154 | AutomationController::sure_trigger_handle_trigger( |
| 155 | [ |
| 156 | 'trigger' => $this->trigger, |
| 157 | 'context' => $context, |
| 158 | ] |
| 159 | ); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Ignore false positive |
| 165 | * |
| 166 | * @psalm-suppress UndefinedMethod |
| 167 | */ |
| 168 | UpdatedCCTItem::get_instance(); |
| 169 | |
| 170 | endif; |
| 171 |