PurchaseCreatedTrigger.php
3 years ago
PurchaseInvokedTrigger.php
3 years ago
PurchaseRevokedTrigger.php
3 years ago
PurchaseUpdatedTrigger.php
3 years ago
PurchaseUpdatedTrigger.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Integrations\ThriveAutomator\Triggers; |
| 4 | |
| 5 | use SureCart\Integrations\ThriveAutomator\DataFields\PreviousProductDataField; |
| 6 | use SureCart\Integrations\ThriveAutomator\DataObjects\PreviousProductDataObject; |
| 7 | use SureCart\Integrations\ThriveAutomator\DataObjects\ProductDataObject; |
| 8 | use SureCart\Integrations\ThriveAutomator\ThriveAutomatorApp; |
| 9 | use SureCart\Models\User; |
| 10 | use Thrive\Automator\Items\Data_Object; |
| 11 | use Thrive\Automator\Items\Trigger; |
| 12 | |
| 13 | /** |
| 14 | * Handles the purchase updated event. |
| 15 | */ |
| 16 | class PurchaseUpdatedTrigger extends Trigger { |
| 17 | /** |
| 18 | * Get the trigger identifier |
| 19 | * |
| 20 | * @return string |
| 21 | */ |
| 22 | public static function get_id() { |
| 23 | return 'surecart_purchase_updated'; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Get the trigger hook |
| 28 | * |
| 29 | * @return string |
| 30 | */ |
| 31 | public static function get_wp_hook() { |
| 32 | return 'surecart/purchase_updated'; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get the app id. |
| 37 | * |
| 38 | * @return string |
| 39 | */ |
| 40 | public static function get_app_id() { |
| 41 | return ThriveAutomatorApp::get_id(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the trigger provided params |
| 46 | * |
| 47 | * @return array |
| 48 | */ |
| 49 | public static function get_provided_data_objects() { |
| 50 | return [ ProductDataObject::get_id(), PreviousProductDataObject::get_id(), 'user_data' ]; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get the number of params |
| 55 | * |
| 56 | * @return int |
| 57 | */ |
| 58 | public static function get_hook_params_number() { |
| 59 | return 2; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get the trigger name |
| 64 | * |
| 65 | * @return string |
| 66 | */ |
| 67 | public static function get_name() { |
| 68 | return __( 'Subscription plan changed', 'surecart' ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get the trigger description |
| 73 | * |
| 74 | * @return string |
| 75 | */ |
| 76 | public static function get_description() { |
| 77 | return __( 'This trigger will be fired when a subscription plan is changed to something else.', 'surecart' ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the trigger logo |
| 82 | * |
| 83 | * @return string |
| 84 | */ |
| 85 | public static function get_image() { |
| 86 | return esc_url( trailingslashit( plugin_dir_url( SURECART_PLUGIN_FILE ) ) . 'app/src/Integrations/ThriveAutomator/images/icon-color.svg' ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Process params for action. |
| 91 | * |
| 92 | * @param array $params Params from the action. |
| 93 | * @return array |
| 94 | */ |
| 95 | public function process_params( $params = [] ) { |
| 96 | $data_objects = []; |
| 97 | |
| 98 | if ( ! empty( $params ) ) { |
| 99 | $data_object_classes = Data_Object::get(); |
| 100 | $product_id = $params[0]['product']; |
| 101 | $previous = $params[1]['data']['previous_attributes'] ?? []; |
| 102 | $previous_id = $previous['product'] ?? false; |
| 103 | $user = User::findByCustomerId( $params[0]['customer'] ); |
| 104 | |
| 105 | $data_objects['surecart_product_data'] = empty( $data_object_classes['surecart_product_data'] ) ? null : new $data_object_classes['surecart_product_data']( $product_id ); |
| 106 | if ( $previous_id ) { |
| 107 | $data_objects['surecart_previous_product_data'] = empty( $data_object_classes['surecart_previous_product_data'] ) ? null : new $data_object_classes['surecart_previous_product_data']( $previous_id ); |
| 108 | } |
| 109 | $data_objects['user_data'] = empty( $data_object_classes['user_data'] ) ? null : new $data_object_classes['user_data']( $user->ID ); |
| 110 | } |
| 111 | |
| 112 | return $data_objects; |
| 113 | } |
| 114 | |
| 115 | } |
| 116 |