add-post-profile-wall.php
11 months ago
add-post-to-collection.php
11 months ago
add-post-wall.php
11 months ago
assign-listing-plan-to-user.php
6 months ago
change-membership-plan.php
7 months ago
claim-post.php
11 months ago
delete-post.php
7 months ago
delete-wall-post.php
11 months ago
find-post.php
7 months ago
follow-post.php
11 months ago
get-address-by-coordinates.php
2 weeks ago
get-coordinates-by-address.php
2 weeks ago
get-member-by-email.php
10 months ago
get-member-by-id.php
10 months ago
get-member-by-profile-id.php
10 months ago
get-post-by-id.php
11 months ago
get-posts-by-voxel-field.php
11 months ago
new-collection-post.php
1 year ago
new-post.php
1 year ago
new-profile.php
11 months ago
remove-post-from-collection.php
11 months ago
send-custom-in-app-notification.php
2 years ago
send-direct-message.php
11 months ago
send-email.php
11 months ago
set-collection-post-verified.php
11 months ago
set-custom-priority.php
11 months ago
set-post-verified.php
11 months ago
set-profile-verified.php
11 months ago
update-collection-post.php
1 year ago
update-post.php
11 months ago
update-profile.php
11 months ago
update-voxel-post-field.php
10 months ago
update-voxel-taxonomy.php
10 months ago
assign-listing-plan-to-user.php
285 lines
| 1 | <?php |
| 2 | /** |
| 3 | * AssignListingPlanToUser. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category AssignListingPlanToUser |
| 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\Voxel\Actions; |
| 15 | |
| 16 | use SureTriggers\Integrations\AutomateAction; |
| 17 | use SureTriggers\Traits\SingletonLoader; |
| 18 | |
| 19 | /** |
| 20 | * AssignListingPlanToUser |
| 21 | * |
| 22 | * @category AssignListingPlanToUser |
| 23 | * @package SureTriggers |
| 24 | * @author BSF <username@example.com> |
| 25 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 26 | * @link https://www.brainstormforce.com/ |
| 27 | * @since 1.0.0 |
| 28 | */ |
| 29 | class AssignListingPlanToUser extends AutomateAction { |
| 30 | |
| 31 | /** |
| 32 | * Integration type. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | public $integration = 'Voxel'; |
| 37 | |
| 38 | /** |
| 39 | * Action name. |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | public $action = 'voxel_assign_listing_plan_to_user'; |
| 44 | |
| 45 | use SingletonLoader; |
| 46 | |
| 47 | /** |
| 48 | * Register action. |
| 49 | * |
| 50 | * @param array $actions action data. |
| 51 | * @return array |
| 52 | */ |
| 53 | public function register( $actions ) { |
| 54 | $actions[ $this->integration ][ $this->action ] = [ |
| 55 | 'label' => __( 'Assign Listing Plan to User', 'suretriggers' ), |
| 56 | 'action' => 'voxel_assign_listing_plan_to_user', |
| 57 | 'function' => [ $this, 'action_listener' ], |
| 58 | ]; |
| 59 | |
| 60 | return $actions; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Action listener. |
| 65 | * |
| 66 | * @param int $user_id user_id. |
| 67 | * @param int $automation_id automation_id. |
| 68 | * @param array $fields fields. |
| 69 | * @param array $selected_options selectedOptions. |
| 70 | * |
| 71 | * @return array |
| 72 | */ |
| 73 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 74 | global $wpdb; |
| 75 | |
| 76 | // Check if required Voxel classes exist. |
| 77 | if ( ! class_exists( 'Voxel\User' ) || ! class_exists( 'Voxel\Modules\Paid_Listings\Listing_Plan' ) ) { |
| 78 | return [ |
| 79 | 'success' => false, |
| 80 | 'message' => __( 'Voxel paid listings module is not available.', 'suretriggers' ), |
| 81 | ]; |
| 82 | } |
| 83 | |
| 84 | if ( ! class_exists( 'Voxel\Cart_Item' ) || ! class_exists( 'Voxel\Product_Types\Cart\Direct_Cart' ) ) { |
| 85 | return [ |
| 86 | 'success' => false, |
| 87 | 'message' => __( 'Voxel cart classes are not available.', 'suretriggers' ), |
| 88 | ]; |
| 89 | } |
| 90 | |
| 91 | if ( ! class_exists( 'Voxel\Utils\Config_Schema\Schema' ) ) { |
| 92 | return [ |
| 93 | 'success' => false, |
| 94 | 'message' => __( 'Voxel schema class is not available.', 'suretriggers' ), |
| 95 | ]; |
| 96 | } |
| 97 | |
| 98 | // Get user by email. |
| 99 | $user_email = isset( $selected_options['wp_user_email'] ) ? $selected_options['wp_user_email'] : ''; |
| 100 | if ( is_email( $user_email ) ) { |
| 101 | $user = get_user_by( 'email', $user_email ); |
| 102 | $user_id = $user ? $user->ID : 0; |
| 103 | } |
| 104 | |
| 105 | if ( empty( $user_id ) ) { |
| 106 | return [ |
| 107 | 'success' => false, |
| 108 | 'message' => __( 'User not found.', 'suretriggers' ), |
| 109 | ]; |
| 110 | } |
| 111 | |
| 112 | // Get the listing plan key. |
| 113 | $plan_key = isset( $selected_options['listing_plan_key'] ) ? $selected_options['listing_plan_key'] : ''; |
| 114 | if ( empty( $plan_key ) ) { |
| 115 | return [ |
| 116 | 'success' => false, |
| 117 | 'message' => __( 'Listing plan key is required.', 'suretriggers' ), |
| 118 | ]; |
| 119 | } |
| 120 | |
| 121 | // Get the Voxel user. |
| 122 | $voxel_user = \Voxel\User::get( $user_id ); |
| 123 | if ( ! $voxel_user ) { |
| 124 | return [ |
| 125 | 'success' => false, |
| 126 | 'message' => __( 'Voxel user not found.', 'suretriggers' ), |
| 127 | ]; |
| 128 | } |
| 129 | |
| 130 | // Get the listing plan. |
| 131 | $plan = \Voxel\Modules\Paid_Listings\Listing_Plan::get( $plan_key ); |
| 132 | if ( ! $plan ) { |
| 133 | return [ |
| 134 | 'success' => false, |
| 135 | 'message' => __( 'Listing plan not found.', 'suretriggers' ), |
| 136 | ]; |
| 137 | } |
| 138 | |
| 139 | // Set product field to free. |
| 140 | add_filter( |
| 141 | 'voxel/paid-listings/registered-product-field', |
| 142 | function ( $field ) { |
| 143 | $value = $field->get_value(); |
| 144 | $value['product_type'] = 'voxel:listing_plan_payment'; |
| 145 | $value['base_price'] = [ 'amount' => 0 ]; |
| 146 | $field->_set_value( $value ); |
| 147 | return $field; |
| 148 | } |
| 149 | ); |
| 150 | |
| 151 | // Create cart with listing plan. |
| 152 | $cart_item = \Voxel\Cart_Item::create( |
| 153 | [ |
| 154 | 'product' => [ |
| 155 | 'post_id' => $plan->get_product_id(), |
| 156 | 'field_key' => 'voxel:listing_plan', |
| 157 | ], |
| 158 | ] |
| 159 | ); |
| 160 | |
| 161 | $cart = new \Voxel\Product_Types\Cart\Direct_Cart(); |
| 162 | $cart->add_item( $cart_item ); |
| 163 | |
| 164 | // Prepare order details. |
| 165 | $order_details = \Voxel\Utils\Config_Schema\Schema::optimize_for_storage( |
| 166 | [ |
| 167 | 'cart' => [ |
| 168 | 'type' => $cart->get_type(), |
| 169 | 'items' => array_map( |
| 170 | function ( $item ) { |
| 171 | return $item->get_value_for_storage(); |
| 172 | }, |
| 173 | $cart->get_items() |
| 174 | ), |
| 175 | ], |
| 176 | 'pricing' => [ |
| 177 | 'currency' => $cart->get_currency(), |
| 178 | 'subtotal' => 0, |
| 179 | 'total' => 0, |
| 180 | ], |
| 181 | ] |
| 182 | ); |
| 183 | |
| 184 | // Get testmode value. |
| 185 | $testmode = ( function_exists( '\Voxel\is_test_mode' ) && \Voxel\is_test_mode() ) ? 1 : 0; |
| 186 | |
| 187 | // Get current time. |
| 188 | $created_at = function_exists( '\Voxel\utc' ) ? \Voxel\utc()->format( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s' ); |
| 189 | |
| 190 | // Insert order. |
| 191 | $order_result = $wpdb->insert( |
| 192 | $wpdb->prefix . 'vx_orders', |
| 193 | [ |
| 194 | 'customer_id' => $voxel_user->get_id(), |
| 195 | 'vendor_id' => null, |
| 196 | 'status' => 'completed', |
| 197 | 'shipping_status' => null, |
| 198 | 'payment_method' => 'offline_payment', |
| 199 | 'transaction_id' => null, |
| 200 | 'details' => wp_json_encode( $order_details ), |
| 201 | 'parent_id' => null, |
| 202 | 'testmode' => $testmode, |
| 203 | 'created_at' => $created_at, |
| 204 | ] |
| 205 | ); |
| 206 | |
| 207 | if ( false === $order_result ) { |
| 208 | return [ |
| 209 | 'success' => false, |
| 210 | 'message' => __( 'Could not create order.', 'suretriggers' ), |
| 211 | ]; |
| 212 | } |
| 213 | |
| 214 | $order_id = $wpdb->insert_id; |
| 215 | |
| 216 | // Insert order item. |
| 217 | $item_details = \Voxel\Utils\Config_Schema\Schema::optimize_for_storage( |
| 218 | $cart_item->get_order_item_config() |
| 219 | ); |
| 220 | |
| 221 | $item_result = $wpdb->insert( |
| 222 | $wpdb->prefix . 'vx_order_items', |
| 223 | [ |
| 224 | 'order_id' => $order_id, |
| 225 | 'post_id' => $cart_item->get_post()->get_id(), |
| 226 | 'product_type' => $cart_item->get_product_type()->get_key(), |
| 227 | 'field_key' => $cart_item->get_product_field()->get_key(), |
| 228 | 'details' => wp_json_encode( $item_details ), |
| 229 | ] |
| 230 | ); |
| 231 | |
| 232 | if ( false === $item_result ) { |
| 233 | return [ |
| 234 | 'success' => false, |
| 235 | 'message' => __( 'Could not create order item.', 'suretriggers' ), |
| 236 | ]; |
| 237 | } |
| 238 | |
| 239 | $order_item_id = $wpdb->insert_id; |
| 240 | |
| 241 | // Finalize order. |
| 242 | if ( class_exists( 'Voxel\Order' ) ) { |
| 243 | $order = \Voxel\Order::get( $order_id ); |
| 244 | if ( $order ) { |
| 245 | $order->set_transaction_id( sprintf( 'offline_%d', $order->get_id() ) ); |
| 246 | $order->save(); |
| 247 | |
| 248 | // Trigger Voxel hook. |
| 249 | do_action( 'voxel/paid-listings/backend/assigned-package', $order, $voxel_user, $plan ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // Get package limits for response. |
| 254 | $limits_summary = []; |
| 255 | if ( class_exists( 'Voxel\Modules\Paid_Listings\Listing_Package' ) ) { |
| 256 | $package = \Voxel\Modules\Paid_Listings\Listing_Package::get( $order_item_id ); |
| 257 | if ( $package ) { |
| 258 | foreach ( $package->get_limits() as $limit ) { |
| 259 | $limits_summary[] = [ |
| 260 | 'post_types' => $limit['post_types'], |
| 261 | 'total' => $limit['total'], |
| 262 | 'used' => $limit['usage']['count'], |
| 263 | 'available' => $limit['total'] - $limit['usage']['count'], |
| 264 | 'mark_verified' => $limit['mark_verified'], |
| 265 | ]; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | return [ |
| 271 | 'success' => true, |
| 272 | 'message' => __( 'Listing plan assigned successfully.', 'suretriggers' ), |
| 273 | 'user_id' => $voxel_user->get_id(), |
| 274 | 'user_email' => $voxel_user->get_email(), |
| 275 | 'plan_key' => $plan->get_key(), |
| 276 | 'plan_label' => $plan->get_label(), |
| 277 | 'package_id' => $order_item_id, |
| 278 | 'limits' => $limits_summary, |
| 279 | 'supported_types' => $plan->get_supported_post_types(), |
| 280 | ]; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | AssignListingPlanToUser::get_instance(); |
| 285 |