← All changes
|
integrations/restrict-content-pro/includes/actions/add-membership.php
+184
-0
5.7.2
→
6.0.4
View file →
| @@ -1,0 +1,184 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Add Membership | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\Restrict_Content_Pro\Actions\Add_Membership | |
| 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_Restrict_Content_Pro_Add_Membership extends AutomatorWP_Integration_Action { | |
| 13 | + | |
| 14 | + public $integration = 'restrict_content_pro'; | |
| 15 | + public $action = 'restrict_content_pro_add_membership'; | |
| 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' => __( 'Add membership level to user', 'automatorwp' ), | |
| 27 | + 'select_option' => __( 'Add <strong>membership level</strong> to user', 'automatorwp' ), | |
| 28 | + /* translators: %1$s: Membership. */ | |
| 29 | + 'edit_label' => sprintf( __( 'Add %1$s to user', 'automatorwp' ), '{membership}' ), | |
| 30 | + /* translators: %1$s: Membership. */ | |
| 31 | + 'log_label' => sprintf( __( 'Add %1$s to user', 'automatorwp' ), '{membership}' ), | |
| 32 | + 'options' => array( | |
| 33 | + 'membership' => array( | |
| 34 | + 'from' => 'membership', | |
| 35 | + 'fields' => array( | |
| 36 | + 'membership' => array( | |
| 37 | + 'name' => __( 'Membership Level:', 'automatorwp' ), | |
| 38 | + 'type' => 'select', | |
| 39 | + 'classes' => 'automatorwp-ajax-selector', | |
| 40 | + 'option_none' => true, | |
| 41 | + 'option_none_value' => 'any', | |
| 42 | + 'option_none_label' => __( 'a membership level', 'automatorwp' ), | |
| 43 | + 'option_custom' => true, | |
| 44 | + 'option_custom_value' => 'custom', | |
| 45 | + 'option_custom_label' => __( 'Use a custom value', 'automatorwp' ), | |
| 46 | + 'option_custom_desc' => '', | |
| 47 | + 'attributes' => array( | |
| 48 | + 'data-action' => 'automatorwp_restrict_content_pro_get_memberships', | |
| 49 | + 'data-option-none' => true, | |
| 50 | + 'data-option-none-value' => 'any', | |
| 51 | + 'data-option-none-label' => __( 'a membership level', 'automatorwp' ), | |
| 52 | + 'data-option-custom' => true, | |
| 53 | + 'data-option-custom-value' => 'custom', | |
| 54 | + 'data-option-custom-label' => __( 'Use a custom value', 'automatorwp' ), | |
| 55 | + 'data-placeholder' => '', | |
| 56 | + ), | |
| 57 | + 'options_cb' => 'automatorwp_restrict_content_pro_options_cb_membership', | |
| 58 | + 'default' => 'any' | |
| 59 | + ), | |
| 60 | + 'membership_custom' => automatorwp_utilities_custom_field( array( | |
| 61 | + 'option_custom_desc' => __( 'Membership ID', 'automatorwp' ), | |
| 62 | + ) ), | |
| 63 | + 'status' => array( | |
| 64 | + 'name' => __( 'Status:', 'automatorwp' ), | |
| 65 | + 'type' => 'select', | |
| 66 | + 'options' => array( | |
| 67 | + 'active' => __( 'Active', 'automatorwp' ), | |
| 68 | + 'pending' => __( 'Pending', 'automatorwp' ), | |
| 69 | + 'expired' => __( 'Expired', 'automatorwp' ), | |
| 70 | + 'cancelled' => __( 'Cancelled', 'automatorwp' ), | |
| 71 | + ), | |
| 72 | + 'default' => 'active' | |
| 73 | + ), | |
| 74 | + 'expiration' => array( | |
| 75 | + 'name' => __( 'Expiration Date:', 'automatorwp' ), | |
| 76 | + 'desc' => __( 'Enter the membership expiration date in format YYYY-MM-DD. Leave empty for lifetime.', 'automatorwp' ), | |
| 77 | + 'type' => 'text', | |
| 78 | + 'default' => '' | |
| 79 | + ), | |
| 80 | + ) | |
| 81 | + ) | |
| 82 | + ), | |
| 83 | + ) ); | |
| 84 | + | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * Action execution function | |
| 89 | + * | |
| 90 | + * @since 1.0.0 | |
| 91 | + * | |
| 92 | + * @param stdClass $action The action object | |
| 93 | + * @param int $user_id The user ID | |
| 94 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 95 | + * @param stdClass $automation The action's automation object | |
| 96 | + */ | |
| 97 | + public function execute( $action, $user_id, $action_options, $automation ) { | |
| 98 | + | |
| 99 | + // Shorthand | |
| 100 | + $level_id = $action_options['membership']; | |
| 101 | + $status = $action_options['status']; | |
| 102 | + $expiration_date = $action_options['expiration']; | |
| 103 | + | |
| 104 | + // Bail if not membership level has been configured | |
| 105 | + if( empty( $level_id ) || $level_id === 'any' ) { | |
| 106 | + return; | |
| 107 | + } | |
| 108 | + | |
| 109 | + $customer = rcp_get_customer_by_user_id( $user_id ); | |
| 110 | + | |
| 111 | + // Create a new customer record if one does not exist | |
| 112 | + if ( empty( $customer ) ) { | |
| 113 | + $customer_id = rcp_add_customer( array( | |
| 114 | + 'user_id' => absint( $user_id ), | |
| 115 | + 'date_registered' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ) | |
| 116 | + ) ); | |
| 117 | + } else { | |
| 118 | + $customer_id = $customer->get_id(); | |
| 119 | + } | |
| 120 | + | |
| 121 | + $membership_args = array( | |
| 122 | + 'customer_id' => absint( $customer_id ), | |
| 123 | + 'user_id' => $user_id, | |
| 124 | + 'object_id' => $level_id, | |
| 125 | + 'status' => $status, | |
| 126 | + 'created_date' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ), | |
| 127 | + 'gateway' => 'manual', | |
| 128 | + 'subscription_key' => rcp_generate_subscription_key() | |
| 129 | + ); | |
| 130 | + | |
| 131 | + switch ( $status ) { | |
| 132 | + case 'expired' : | |
| 133 | + // Set the expiration date if expired | |
| 134 | + $membership_args['expiration_date'] = date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ); | |
| 135 | + break; | |
| 136 | + case 'cancelled' : | |
| 137 | + // Set the cancellation date if cancelled | |
| 138 | + $membership_args['cancellation_date'] = date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ); | |
| 139 | + break; | |
| 140 | + case 'active' : | |
| 141 | + if ( ! empty( $expiration_date ) ) { | |
| 142 | + $membership_args['expiration_date'] = date( 'Y-m-d H:i:s', strtotime( $expiration_date ) ); | |
| 143 | + } | |
| 144 | + break; | |
| 145 | + } | |
| 146 | + | |
| 147 | + $membership_id = rcp_add_membership( $membership_args ); | |
| 148 | + | |
| 149 | + // Add membership meta to designate this as a generated record from this action | |
| 150 | + rcp_add_membership_meta( $membership_id, 'automatorwp_restrict_content_action_id', $action->id ); | |
| 151 | + | |
| 152 | + $membership = rcp_get_membership( $membership_id ); | |
| 153 | + | |
| 154 | + // Generate a transaction ID | |
| 155 | + $auth_key = defined( 'AUTH_KEY' ) ? AUTH_KEY : ''; | |
| 156 | + $transaction_id = strtolower( md5( $membership_args['subscription_key'] . date( 'Y-m-d H:i:s' ) . $auth_key . uniqid( 'rcp', true ) ) ); | |
| 157 | + | |
| 158 | + // Create a corresponding payment record. | |
| 159 | + $payment_args = array( | |
| 160 | + 'subscription' => rcp_get_subscription_name( $membership_args['object_id'] ), | |
| 161 | + 'object_id' => $membership_args['object_id'], | |
| 162 | + 'date' => $membership_args['created_date'], | |
| 163 | + 'amount' => $membership->get_initial_amount(), | |
| 164 | + 'subtotal' => $membership->get_initial_amount(), | |
| 165 | + 'user_id' => $user_id, | |
| 166 | + 'subscription_key' => $membership_args['subscription_key'], | |
| 167 | + 'transaction_id' => $transaction_id, | |
| 168 | + 'status' => 'pending' == $membership_args['status'] ? 'pending' : 'complete', | |
| 169 | + 'gateway' => 'manual', | |
| 170 | + 'customer_id' => $customer_id, | |
| 171 | + 'membership_id' => $membership_id | |
| 172 | + ); | |
| 173 | + | |
| 174 | + $rcp_payments = new RCP_Payments(); | |
| 175 | + $payment_id = $rcp_payments->insert( $payment_args ); | |
| 176 | + | |
| 177 | + // Add payment meta to designate this as a generated record from this action | |
| 178 | + $rcp_payments->add_meta( $payment_id, 'automatorwp_restrict_content_action_id', $action->id ); | |
| 179 | + | |
| 180 | + } | |
| 181 | + | |
| 182 | +} | |
| 183 | + | |
| 184 | +new AutomatorWP_Restrict_Content_Pro_Add_Membership(); | |