| 1 |
<?php |
| 2 |
/** |
| 3 |
* User Product |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\Thrive_Apprentice\Actions\User_Product |
| 6 |
* @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class AutomatorWP_Thrive_Apprentice_User_Product extends AutomatorWP_Integration_Action { |
| 13 |
|
| 14 |
public $integration = 'thrive_apprentice'; |
| 15 |
public $action = 'thrive_apprentice_user_product'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Register the trigger |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
public function register() { |
| 23 |
|
| 24 |
automatorwp_register_action( $this->action, array( |
| 25 |
'integration' => $this->integration, |
| 26 |
'label' => __( 'Enroll user to a product', 'automatorwp' ), |
| 27 |
'select_option' => __( 'Enroll user to <strong>a product</strong>', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Product title. */ |
| 29 |
'edit_label' => sprintf( __( 'Enroll user to %1$s', 'automatorwp' ), '{term}' ), |
| 30 |
/* translators: %1$s: Product title. */ |
| 31 |
'log_label' => sprintf( __( 'Enroll user to %1$s', 'automatorwp' ), '{term}' ), |
| 32 |
'options' => array( |
| 33 |
'term' => automatorwp_utilities_term_option( array( |
| 34 |
'name' => __( 'Product:', 'automatorwp-thrive-apprentice' ), |
| 35 |
'option_none_label' => __( 'all products', 'automatorwp-thrive-apprentice' ), |
| 36 |
'option_custom' => true, |
| 37 |
'option_custom_desc' => __( 'Product ID', 'automatorwp-thrive-apprentice' ), |
| 38 |
'taxonomy' => 'tva_product', |
| 39 |
) ), |
| 40 |
), |
| 41 |
) ); |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Action execution function |
| 47 |
* |
| 48 |
* @since 1.0.0 |
| 49 |
* |
| 50 |
* @param stdClass $action The action object |
| 51 |
* @param int $user_id The user ID |
| 52 |
* @param array $action_options The action's stored options (with tags already passed) |
| 53 |
* @param stdClass $automation The action's automation object |
| 54 |
*/ |
| 55 |
public function execute( $action, $user_id, $action_options, $automation ) { |
| 56 |
|
| 57 |
global $wpdb; |
| 58 |
|
| 59 |
// Shorthand |
| 60 |
$product_id = $action_options['term']; |
| 61 |
|
| 62 |
// Check specific product |
| 63 |
if( $product_id !== 'any' ) { |
| 64 |
|
| 65 |
$product = get_term( $product_id, 'tva_product' ); |
| 66 |
|
| 67 |
// Bail if course doesn't exists |
| 68 |
if( ! $product ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$products = array( $product_id ); |
| 73 |
|
| 74 |
} else { |
| 75 |
|
| 76 |
$terms = get_terms( array( |
| 77 |
'taxonomy' => 'tva_product', |
| 78 |
'hide_empty' => false, |
| 79 |
) ); |
| 80 |
|
| 81 |
foreach ( $terms as $term ) { |
| 82 |
$products[] = $term->term_id; |
| 83 |
} |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
// Enroll user in products |
| 88 |
foreach( $products as $product_id ) { |
| 89 |
TVA_Customer::enrol_user_to_product( $user_id, $product_id ); |
| 90 |
} |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
new AutomatorWP_Thrive_Apprentice_User_Product(); |