← All changes
|
integrations/digimember/includes/triggers/purchase-product.php
+122
-0
5.6.0
→
6.0.4
View file →
| @@ -1,0 +1,122 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Purchase Product | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\Digimember\Triggers\Purchase_Product | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +class AutomatorWP_Digimember_Purchase_Product extends AutomatorWP_Integration_Trigger { | |
| 13 | + | |
| 14 | + public $integration = 'digimember'; | |
| 15 | + public $trigger = 'digimember_purchase_product'; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Register the trigger | |
| 19 | + * | |
| 20 | + * @since 1.0.0 | |
| 21 | + */ | |
| 22 | + public function register() { | |
| 23 | + | |
| 24 | + automatorwp_register_trigger( $this->trigger, array( | |
| 25 | + 'integration' => $this->integration, | |
| 26 | + 'label' => __( 'User purchases a product', 'automatorwp' ), | |
| 27 | + 'select_option' => __( 'User purchases <strong>a product</strong>', 'automatorwp' ), | |
| 28 | + /* translators: %1$s: Product title. %2$s: Number of times. */ | |
| 29 | + 'edit_label' => sprintf( __( 'User purchases %1$s %2$s time(s)', 'automatorwp' ), '{product}', '{times}' ), | |
| 30 | + /* translators: %1$s: Product title. */ | |
| 31 | + 'log_label' => sprintf( __( 'User purchases %1$s', 'automatorwp' ), '{product}' ), | |
| 32 | + 'action' => 'digimember_purchase', | |
| 33 | + 'function' => array( $this, 'listener' ), | |
| 34 | + 'priority' => 10, | |
| 35 | + 'accepted_args' => 4, | |
| 36 | + 'options' => array( | |
| 37 | + 'product' => automatorwp_utilities_ajax_selector_option( array( | |
| 38 | + 'field' => 'product', | |
| 39 | + 'name' => __( 'Product:', 'automatorwp' ), | |
| 40 | + 'option_none_value' => 'any', | |
| 41 | + 'option_none_label' => __( 'any product', 'automatorwp' ), | |
| 42 | + 'action_cb' => 'automatorwp_digimember_get_products', | |
| 43 | + 'options_cb' => 'automatorwp_digimember_options_cb_product', | |
| 44 | + 'default' => 'any' | |
| 45 | + ) ), | |
| 46 | + 'times' => automatorwp_utilities_times_option(), | |
| 47 | + ), | |
| 48 | + 'tags' => array_merge( | |
| 49 | + automatorwp_utilities_times_tag() | |
| 50 | + ) | |
| 51 | + ) ); | |
| 52 | + | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Trigger listener | |
| 57 | + * | |
| 58 | + * @since 1.0.0 | |
| 59 | + * | |
| 60 | + * @param int $user_id | |
| 61 | + * @param int $product_id | |
| 62 | + * @param int $order_id | |
| 63 | + * @param string $reason (order_paid|order_cancelled|payment_missing) | |
| 64 | + */ | |
| 65 | + public function listener( $user_id, $product_id, $order_id, $reason ) { | |
| 66 | + | |
| 67 | + // Bail if not is a product purchase | |
| 68 | + if( $reason !== 'order_paid' ) { | |
| 69 | + return; | |
| 70 | + } | |
| 71 | + | |
| 72 | + // Trigger the product purchase | |
| 73 | + automatorwp_trigger_event( array( | |
| 74 | + 'trigger' => $this->trigger, | |
| 75 | + 'user_id' => $user_id, | |
| 76 | + 'product_id' => $product_id, | |
| 77 | + ) ); | |
| 78 | + | |
| 79 | + } | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * User deserves check | |
| 83 | + * | |
| 84 | + * @since 1.0.0 | |
| 85 | + * | |
| 86 | + * @param bool $deserves_trigger True if user deserves trigger, false otherwise | |
| 87 | + * @param stdClass $trigger The trigger object | |
| 88 | + * @param int $user_id The user ID | |
| 89 | + * @param array $event Event information | |
| 90 | + * @param array $trigger_options The trigger's stored options | |
| 91 | + * @param stdClass $automation The trigger's automation object | |
| 92 | + * | |
| 93 | + * @return bool True if user deserves trigger, false otherwise | |
| 94 | + */ | |
| 95 | + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 96 | + | |
| 97 | + // Don't deserve if product is not received | |
| 98 | + if( ! isset( $event['product_id'] ) ) { | |
| 99 | + return false; | |
| 100 | + } | |
| 101 | + | |
| 102 | + $product_id = absint( $event['product_id'] ); | |
| 103 | + | |
| 104 | + // Don't deserve if product doesn't exists | |
| 105 | + if( $product_id === 0 ) { | |
| 106 | + return false; | |
| 107 | + } | |
| 108 | + | |
| 109 | + $required_product_id = absint( $trigger_options['product'] ); | |
| 110 | + | |
| 111 | + // Don't deserve if product doesn't match with the trigger option | |
| 112 | + if( $required_product_id !== 0 && $product_id !== $required_product_id ) { | |
| 113 | + return false; | |
| 114 | + } | |
| 115 | + | |
| 116 | + return $deserves_trigger; | |
| 117 | + | |
| 118 | + } | |
| 119 | + | |
| 120 | +} | |
| 121 | + | |
| 122 | +new AutomatorWP_Digimember_Purchase_Product(); | |