| @@ -1,7 +1,9 @@ | ||
| 1 | 1 | <?php namespace TierPricingTable\Addons\Tools\API; |
| 2 | 2 | |
| 3 | 3 | use TierPricingTable\Addons\GlobalTieredPricing\CPT\GlobalTieredPricingCPT; |
| 4 | +use TierPricingTable\Addons\Tools\PricingTest\PricingTester; | |
| 5 | +use WP_Error; | |
| 4 | 6 | use WP_REST_Request; |
| 5 | 7 | |
| 6 | 8 | class ToolsEndpoints { |
| 7 | 9 | |
| @@ -42,8 +44,23 @@ | ||
| 42 | 44 | }, |
| 43 | 45 | ), |
| 44 | 46 | ) ); |
| 45 | 47 | |
| 48 | + register_rest_route( self::NAMESPACE, '/pricing_test', array( | |
| 49 | + array( | |
| 50 | + 'methods' => 'POST', | |
| 51 | + 'callback' => array( $this, 'pricingTest' ), | |
| 52 | + 'permission_callback' => function () { | |
| 53 | + return current_user_can( 'manage_woocommerce' ); | |
| 54 | + }, | |
| 55 | + 'args' => array( | |
| 56 | + 'product_id' => array( 'type' => 'integer', 'required' => true ), | |
| 57 | + 'user_id' => array( 'type' => 'integer', 'default' => 0 ), | |
| 58 | + 'rule_id' => array( 'type' => 'integer', 'default' => 0 ), | |
| 59 | + ), | |
| 60 | + ), | |
| 61 | + ) ); | |
| 62 | + | |
| 46 | 63 | register_rest_route( self::NAMESPACE, '/roles', array( |
| 47 | 64 | array( |
| 48 | 65 | 'methods' => 'GET', |
| 49 | 66 | 'callback' => array( $this, 'getRoles' ), |
| @@ -84,8 +101,33 @@ | ||
| 84 | 101 | ), |
| 85 | 102 | ) ); |
| 86 | 103 | } |
| 87 | 104 | |
| 105 | + /** | |
| 106 | + * The Pricing test: what a customer (0 = visitor) gets for a product, and which global rules apply. | |
| 107 | + */ | |
| 108 | + public function pricingTest( WP_REST_Request $request ) { | |
| 109 | + $productId = absint( $request->get_param( 'product_id' ) ); | |
| 110 | + $userId = absint( $request->get_param( 'user_id' ) ); | |
| 111 | + $ruleId = absint( $request->get_param( 'rule_id' ) ); | |
| 112 | + | |
| 113 | + if ( ! $productId ) { | |
| 114 | + return new WP_Error( 'tpt_pricing_test_product', __( 'Pick a product first.', 'tier-pricing-table' ), array( 'status' => 400 ) ); | |
| 115 | + } | |
| 116 | + | |
| 117 | + if ( $userId && ! get_user_by( 'id', $userId ) ) { | |
| 118 | + return new WP_Error( 'tpt_pricing_test_customer', __( 'The customer could not be loaded.', 'tier-pricing-table' ), array( 'status' => 400 ) ); | |
| 119 | + } | |
| 120 | + | |
| 121 | + $result = ( new PricingTester() )->run( $productId, $userId, $ruleId ); | |
| 122 | + | |
| 123 | + if ( isset( $result['error'] ) ) { | |
| 124 | + return new WP_Error( 'tpt_pricing_test', $result['error'], array( 'status' => 400 ) ); | |
| 125 | + } | |
| 126 | + | |
| 127 | + return rest_ensure_response( $result ); | |
| 128 | + } | |
| 129 | + | |
| 88 | 130 | public function getRoles() { |
| 89 | 131 | global $wp_roles; |
| 90 | 132 | if ( ! isset( $wp_roles ) ) { |
| 91 | 133 | $wp_roles = new \WP_Roles(); |
| @@ -104,9 +146,21 @@ | ||
| 104 | 146 | } |
| 105 | 147 | |
| 106 | 148 | public function cleanupData( WP_REST_Request $request ) { |
| 107 | 149 | $cleanStandard = filter_var( $request->get_param( 'clean_standard' ), FILTER_VALIDATE_BOOLEAN ); |
| 108 | - $roles = $request->get_param( 'roles' ); // array of role keys | |
| 150 | + $cleanUsers = filter_var( $request->get_param( 'clean_users' ), FILTER_VALIDATE_BOOLEAN ); | |
| 151 | + $cleanAllRoles = filter_var( $request->get_param( 'clean_all_roles' ), FILTER_VALIDATE_BOOLEAN ); | |
| 152 | + | |
| 153 | + if ( $cleanAllRoles ) { | |
| 154 | + global $wp_roles; | |
| 155 | + if ( ! isset( $wp_roles ) ) { | |
| 156 | + $wp_roles = new \WP_Roles(); | |
| 157 | + } | |
| 158 | + $roles = array_keys( $wp_roles->get_names() ); | |
| 159 | + } else { | |
| 160 | + $roles = $request->get_param( 'roles' ); // array of role keys | |
| 161 | + } | |
| 162 | + | |
| 109 | 163 | $categories = $request->get_param( 'categories' ); // array of term ids |
| 110 | 164 | $products = $request->get_param( 'products' ); // array of product ids |
| 111 | 165 | |
| 112 | 166 | $metaKeysToDelete = array(); |
| @@ -190,8 +244,45 @@ | ||
| 190 | 244 | 'message' => 'Database error during cleanup.', |
| 191 | 245 | ) ); |
| 192 | 246 | } |
| 193 | 247 | } |
| 248 | + | |
| 249 | + if ( $cleanUsers ) { | |
| 250 | + global $wpdb; | |
| 251 | + | |
| 252 | + $userMetaLikeQueries = array( | |
| 253 | + "meta_key LIKE '\_user\_%\_tiered_price\_pricing\_type'", | |
| 254 | + "meta_key LIKE '\_user\_%\_tiered_price\_regular\_price'", | |
| 255 | + "meta_key LIKE '\_user\_%\_tiered_price\_sale\_price'", | |
| 256 | + "meta_key LIKE '\_user\_%\_tiered_price\_discount'", | |
| 257 | + "meta_key LIKE '\_user\_%\_tiered_price\_discount\_type'", | |
| 258 | + "meta_key LIKE '\_user\_%\_tiered_price\_rules\_type'", | |
| 259 | + "meta_key LIKE '\_user\_%\_percentage\_price\_rules'", | |
| 260 | + "meta_key LIKE '\_user\_%\_fixed\_price\_rules'", | |
| 261 | + "meta_key LIKE '\_user\_%\_tiered_price\_minimum\_qty'", | |
| 262 | + "meta_key LIKE '\_user\_%\_tiered_price\_tax\_status'", | |
| 263 | + "meta_key LIKE '\_user\_%\_tiered_price\_tax\_class'" | |
| 264 | + ); | |
| 265 | + | |
| 266 | + $likeQuery = implode( ' OR ', $userMetaLikeQueries ); | |
| 267 | + | |
| 268 | + if ( empty( $products ) && empty( $categories ) ) { | |
| 269 | + $query = "DELETE FROM {$wpdb->postmeta} WHERE ($likeQuery)"; | |
| 270 | + $userDeletedCount = $wpdb->query( $query ); | |
| 271 | + } else { | |
| 272 | + if ( ! empty( $postIds ) ) { | |
| 273 | + $postIdsIn = implode( ',', $postIds ); | |
| 274 | + $query = "DELETE FROM {$wpdb->postmeta} WHERE ($likeQuery) AND post_id IN ($postIdsIn)"; | |
| 275 | + $userDeletedCount = $wpdb->query( $query ); | |
| 276 | + } | |
| 277 | + } | |
| 278 | + | |
| 279 | + if ( $userDeletedCount !== false ) { | |
| 280 | + $deletedCount += $userDeletedCount; | |
| 281 | + } | |
| 282 | + } | |
| 283 | + | |
| 284 | + delete_option( \TierPricingTable\Admin\Tips\Tip::SEEN_TIPS_OPTION_KEY ); | |
| 194 | 285 | |
| 195 | 286 | $message = sprintf( 'Successfully cleaned up %d product records.', $deletedCount ); |
| 196 | 287 | |
| 197 | 288 | return rest_ensure_response( array( |