add-affiliate-to-group.php
5 months ago
create-affiliate-group.php
5 months ago
create-affiliate.php
5 months ago
create-referral.php
5 months ago
delete-affiliate-group.php
5 months ago
get-affiliate-group.php
5 months ago
get-affiliate.php
5 months ago
remove-affiliate-from-group.php
5 months ago
update-affiliate-group.php
5 months ago
update-affiliate-status.php
5 months ago
update-affiliate-status.php
186 lines
| 1 | <?php |
| 2 | /** |
| 3 | * UpdateAffiliateStatus. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category UpdateAffiliateStatus |
| 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\FluentAffiliate\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use SureTriggers\Integrations\AutomateAction; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | |
| 20 | /** |
| 21 | * UpdateAffiliateStatus |
| 22 | * |
| 23 | * @category UpdateAffiliateStatus |
| 24 | * @package SureTriggers |
| 25 | * @author BSF <username@example.com> |
| 26 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 27 | * @link https://www.brainstormforce.com/ |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | class UpdateAffiliateStatus extends AutomateAction { |
| 31 | |
| 32 | /** |
| 33 | * Integration type. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | public $integration = 'FluentAffiliate'; |
| 38 | |
| 39 | /** |
| 40 | * Action name. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | public $action = 'fluentaffiliate_update_affiliate_status'; |
| 45 | |
| 46 | use SingletonLoader; |
| 47 | |
| 48 | /** |
| 49 | * Register a action. |
| 50 | * |
| 51 | * @param array $actions actions. |
| 52 | * @return array |
| 53 | */ |
| 54 | public function register( $actions ) { |
| 55 | |
| 56 | $actions[ $this->integration ][ $this->action ] = [ |
| 57 | 'label' => __( 'Update Affiliate Status', 'suretriggers' ), |
| 58 | 'action' => $this->action, |
| 59 | 'function' => [ $this, 'action_listener' ], |
| 60 | ]; |
| 61 | return $actions; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Action listener. |
| 66 | * |
| 67 | * @param int $user_id user_id. |
| 68 | * @param int $automation_id automation_id. |
| 69 | * @param array $fields fields. |
| 70 | * @param array $selected_options selectedOptions. |
| 71 | * @return array|void |
| 72 | * @throws Exception Exception. |
| 73 | */ |
| 74 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 75 | |
| 76 | // Check if FluentAffiliate Affiliate model exists. |
| 77 | if ( ! class_exists( 'FluentAffiliate\App\Models\Affiliate' ) ) { |
| 78 | return [ |
| 79 | 'status' => 'error', |
| 80 | 'message' => __( 'FluentAffiliate plugin is not installed or activated.', 'suretriggers' ), |
| 81 | ]; |
| 82 | } |
| 83 | |
| 84 | // Extract selected options. |
| 85 | $affiliate_id = isset( $selected_options['affiliate_id'] ) ? absint( $selected_options['affiliate_id'] ) : 0; |
| 86 | $new_status = isset( $selected_options['status'] ) ? sanitize_text_field( $selected_options['status'] ) : ''; |
| 87 | |
| 88 | // Validate new status. |
| 89 | if ( empty( $new_status ) ) { |
| 90 | return [ |
| 91 | 'status' => 'error', |
| 92 | 'message' => __( 'Status is required.', 'suretriggers' ), |
| 93 | ]; |
| 94 | } |
| 95 | |
| 96 | // Validate status value. |
| 97 | $allowed_statuses = [ 'active', 'pending', 'inactive' ]; |
| 98 | if ( ! in_array( $new_status, $allowed_statuses, true ) ) { |
| 99 | return [ |
| 100 | 'status' => 'error', |
| 101 | 'message' => sprintf( __( 'Invalid status. Allowed values: %s', 'suretriggers' ), implode( ', ', $allowed_statuses ) ), |
| 102 | ]; |
| 103 | } |
| 104 | |
| 105 | // Get affiliate by affiliate ID. |
| 106 | if ( empty( $affiliate_id ) ) { |
| 107 | return [ |
| 108 | 'status' => 'error', |
| 109 | 'message' => __( 'Affiliate ID is required.', 'suretriggers' ), |
| 110 | ]; |
| 111 | } |
| 112 | |
| 113 | $affiliate = \FluentAffiliate\App\Models\Affiliate::find( $affiliate_id ); |
| 114 | |
| 115 | // Validate affiliate found. |
| 116 | if ( ! $affiliate ) { |
| 117 | return [ |
| 118 | 'status' => 'error', |
| 119 | 'message' => __( 'Affiliate not found.', 'suretriggers' ), |
| 120 | ]; |
| 121 | } |
| 122 | |
| 123 | // Check if status is already the same. |
| 124 | if ( $affiliate->status === $new_status ) { |
| 125 | return [ |
| 126 | 'status' => 'error', |
| 127 | 'message' => sprintf( __( 'Affiliate status is already %s.', 'suretriggers' ), $new_status ), |
| 128 | ]; |
| 129 | } |
| 130 | |
| 131 | // Store old status for hook. |
| 132 | $old_status = $affiliate->status; |
| 133 | |
| 134 | // Update affiliate status. |
| 135 | try { |
| 136 | $affiliate->status = $new_status; |
| 137 | $affiliate->save(); |
| 138 | |
| 139 | // Fire status change hook. |
| 140 | do_action( 'fluent_affiliate/affiliate_status_to_' . $new_status, $affiliate, $old_status ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 141 | |
| 142 | // Get WordPress user context. |
| 143 | $user_data = []; |
| 144 | if ( isset( $affiliate->user_id ) ) { |
| 145 | $wp_user = get_user_by( 'ID', $affiliate->user_id ); |
| 146 | if ( false !== $wp_user ) { |
| 147 | $user_data = [ |
| 148 | 'user_id' => $wp_user->ID, |
| 149 | 'user_login' => $wp_user->user_login, |
| 150 | 'user_email' => $wp_user->user_email, |
| 151 | 'first_name' => $wp_user->first_name, |
| 152 | 'last_name' => $wp_user->last_name, |
| 153 | 'display_name' => $wp_user->display_name, |
| 154 | ]; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Prepare response data. |
| 159 | $context = array_merge( |
| 160 | $user_data, |
| 161 | [ |
| 162 | 'affiliate_id' => $affiliate->id, |
| 163 | 'old_status' => $old_status, |
| 164 | 'new_status' => $affiliate->status, |
| 165 | 'payment_email' => isset( $affiliate->payment_email ) ? $affiliate->payment_email : '', |
| 166 | 'rate_type' => isset( $affiliate->rate_type ) ? $affiliate->rate_type : '', |
| 167 | 'commission_rate' => isset( $affiliate->commission_rate ) ? $affiliate->commission_rate : '', |
| 168 | 'updated_at' => isset( $affiliate->updated_at ) ? $affiliate->updated_at : '', |
| 169 | 'status' => 'success', |
| 170 | ] |
| 171 | ); |
| 172 | |
| 173 | return $context; |
| 174 | |
| 175 | } catch ( \Exception $e ) { |
| 176 | return [ |
| 177 | 'status' => 'error', |
| 178 | 'message' => sprintf( __( 'Error updating affiliate status: %s', 'suretriggers' ), $e->getMessage() ), |
| 179 | ]; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | } |
| 184 | |
| 185 | UpdateAffiliateStatus::get_instance(); |
| 186 |