Changeset 2307562
- Timestamp:
- 05/19/2020 12:13:21 AM (6 years ago)
- Location:
- mobile-builder
- Files:
-
- 2 added
- 4 edited
-
tags/1.0.0/.gitignore (added)
-
trunk/.gitignore (added)
-
trunk/api/class-mobile-builder-cart.php (modified) (13 diffs)
-
trunk/api/class-mobile-builder-vendor.php (modified) (7 diffs)
-
trunk/includes/class-mobile-builder.php (modified) (1 diff)
-
trunk/public/class-mobile-builder-public.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
mobile-builder/trunk/api/class-mobile-builder-cart.php
r2303204 r2307562 33 33 private $version; 34 34 35 private $namespace; 36 35 37 /** 36 38 * Initialize the class and set its properties. … … 45 47 $this->plugin_name = $plugin_name; 46 48 $this->version = $version; 49 $this->namespace = $plugin_name . '/v' . intval( $version ); 47 50 48 51 } … … 54 57 */ 55 58 public function add_api_routes() { 56 $namespace = $this->plugin_name . '/v' . intval( $this->version ); 57 58 register_rest_route( $namespace, 'cart', array( 59 60 register_rest_route( $this->namespace, 'cart', array( 59 61 array( 60 62 'methods' => WP_REST_Server::READABLE, … … 63 65 array( 64 66 'methods' => WP_REST_Server::CREATABLE, 65 'callback' => array( $this, ' add_to_cart' ),67 'callback' => array( $this, 'mobile_builder_add_to_cart' ), 66 68 ) 67 69 ) ); 68 70 69 register_rest_route( $ namespace, 'update-shipping', array(71 register_rest_route( $this->namespace, 'update-shipping', array( 70 72 'methods' => WP_REST_Server::CREATABLE, 71 73 'callback' => array( $this, 'update_shipping' ), 72 74 ) ); 73 75 74 register_rest_route( $namespace, 'cart-total', array( 76 register_rest_route( $this->namespace, 'update-order-review', array( 77 'methods' => WP_REST_Server::CREATABLE, 78 'callback' => array( $this, 'mobile_builder_update_order_review' ), 79 ) ); 80 81 register_rest_route( $this->namespace, 'checkout', array( 82 'methods' => WP_REST_Server::CREATABLE, 83 'callback' => array( $this, 'mobile_builder_checkout' ), 84 ) ); 85 86 register_rest_route( $this->namespace, 'cart-total', array( 75 87 'methods' => WP_REST_Server::READABLE, 76 88 'callback' => array( $this, 'get_total' ), 77 89 ) ); 78 90 79 register_rest_route( $ namespace, 'shipping-methods', array(91 register_rest_route( $this->namespace, 'shipping-methods', array( 80 92 'methods' => WP_REST_Server::READABLE, 81 93 'callback' => array( $this, 'shipping_methods' ), 82 94 ) ); 83 95 84 register_rest_route( $ namespace, 'set-quantity', array(96 register_rest_route( $this->namespace, 'set-quantity', array( 85 97 'methods' => WP_REST_Server::CREATABLE, 86 98 'callback' => array( $this, 'set_quantity' ), 87 99 ) ); 88 100 89 register_rest_route( $ namespace, 'remove-cart-item', array(101 register_rest_route( $this->namespace, 'remove-cart-item', array( 90 102 'methods' => WP_REST_Server::CREATABLE, 91 103 'callback' => array( $this, 'remove_cart_item' ), 92 104 ) ); 93 105 94 register_rest_route( $ namespace, 'add-discount', array(106 register_rest_route( $this->namespace, 'add-discount', array( 95 107 'methods' => WP_REST_Server::CREATABLE, 96 108 'callback' => array( $this, 'add_discount' ), 97 109 ) ); 98 110 99 register_rest_route( $ namespace, 'remove-coupon', array(111 register_rest_route( $this->namespace, 'remove-coupon', array( 100 112 'methods' => WP_REST_Server::CREATABLE, 101 113 'callback' => array( $this, 'remove_coupon' ), … … 104 116 } 105 117 118 public function simulate_as_not_rest( $is_rest_api_request ) { 119 120 if ( empty( $_SERVER['REQUEST_URI'] ) ) { 121 return $is_rest_api_request; 122 } 123 124 if ( false === strpos( $_SERVER['REQUEST_URI'], $this->namespace ) ) { 125 return $is_rest_api_request; 126 } 127 128 return false; 129 } 130 131 public function mobile_builder_woocommerce_persistent_cart_enabled() { 132 return false; 133 } 134 106 135 /** 107 136 * @throws Exception 108 137 * @since 1.0.0 109 138 */ 110 public function rnlab_pre_car_rest_api() { 139 public function mobile_builder_pre_car_rest_api() { 140 111 141 if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.6.0', '>=' ) && WC()->is_rest_api_request() ) { 112 142 require_once( WC_ABSPATH . 'includes/wc-cart-functions.php' ); … … 114 144 115 145 // Disable cookie authentication REST check and only if site is secure. 116 // Todo: Check only our api 117 if ( is_ssl() && true ) { 146 if ( is_ssl() ) { 118 147 remove_filter( 'rest_authentication_errors', 'rest_cookie_check_errors', 100 ); 119 148 } … … 134 163 */ 135 164 if ( is_null( WC()->customer ) ) { 165 136 166 $customer_id = strval( get_current_user_id() ); 137 167 … … 163 193 $items = WC()->cart->get_cart(); 164 194 165 166 195 foreach ( $items as $cart_item_key => $cart_item ) { 167 $_product = $cart_item['data']; 196 $_product = $cart_item['data']; 197 $vendor_id = wcfm_get_vendor_id_by_post( $_product->get_id() ); 168 198 169 199 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $_product->get_id() ), 'single-post-thumbnail' ); … … 174 204 $items[ $cart_item_key ]['name'] = $_product->get_name(); 175 205 $items[ $cart_item_key ]['price'] = WC()->cart->get_product_price( $_product ); 206 $items[ $cart_item_key ]['vendor_id'] = $vendor_id; 207 $items[ $cart_item_key ]['store'] = $vendor_id ? $store_user = get_user_meta( $vendor_id, 'wcfmmp_profile_settings', true ) : null; 176 208 } 177 209 … … 193 225 * @since 1.0.0 194 226 */ 195 public function add_to_cart( $request ) { 227 public function mobile_builder_add_to_cart( $request ) { 228 229 // Login before add to cart 230 if ( get_current_user_id() == 0 ) { 231 return new WP_Error( 232 'mobile_builder_add_to_cart', 233 'Login to add to cart!', 234 array( 235 'status' => 403, 236 ) 237 ); 238 } 239 196 240 try { 197 241 $product_id = $request->get_param( 'product_id' ); … … 215 259 'status' => 403, 216 260 ) ); 217 } catch ( \Throwable $e ) {218 //do something when Throwable is thrown219 return new WP_Error( 'add_to_cart', $e->getMessage(), array(220 'status' => 403,221 ) );222 261 } 223 262 } … … 262 301 } 263 302 303 public function mobile_builder_update_order_review( $request ) { 304 // check_ajax_referer( 'update-order-review', 'security' ); 305 306 wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true ); 307 308 if ( WC()->cart->is_empty() && ! is_customize_preview() && apply_filters( 'woocommerce_checkout_update_order_review_expired', true ) ) { 309 return new WP_Error( 404, 'Sorry, your session has expired.' ); 310 } 311 312 // do_action( 'woocommerce_checkout_update_order_review', $request->get_param( 'post_data') ) ? wp_unslash( $request->get_param( 'post_data') ) : '' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized 313 314 $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' ); 315 $posted_shipping_methods = $request->get_param( 'shipping_method' ) ? wc_clean( wp_unslash( $request->get_param( 'shipping_method' ) ) ) : array(); 316 317 if ( is_array( $posted_shipping_methods ) ) { 318 foreach ( $posted_shipping_methods as $i => $value ) { 319 $chosen_shipping_methods[ $i ] = $value; 320 } 321 } 322 323 WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods ); 324 WC()->session->set( 'chosen_payment_method', empty( $request->get_param( 'shipping_method' ) ) ? '' : wc_clean( wp_unslash( $request->get_param( 'shipping_method' ) ) ) ); 325 WC()->customer->set_props( 326 array( 327 'billing_country' => $request->get_param( 'country' ) ? wc_clean( wp_unslash( $request->get_param( 'country' ) ) ) : null, 328 'billing_state' => $request->get_param( 'state' ) ? wc_clean( wp_unslash( $request->get_param( 'state' ) ) ) : null, 329 'billing_postcode' => $request->get_param( 'postcode' ) ? wc_clean( wp_unslash( $request->get_param( 'postcode' ) ) ) : null, 330 'billing_city' => $request->get_param( 'city' ) ? wc_clean( wp_unslash( $request->get_param( 'city' ) ) ) : null, 331 'billing_address_1' => $request->get_param( 'address' ) ? wc_clean( wp_unslash( $request->get_param( 'address' ) ) ) : null, 332 'billing_address_2' => $request->get_param( 'address_2' ) ? wc_clean( wp_unslash( $request->get_param( 'address_2' ) ) ) : null, 333 'billing_company' => $request->get_param( 'company' ) ? wc_clean( wp_unslash( $request->get_param( 'company' ) ) ) : null, 334 ) 335 ); 336 337 if ( wc_ship_to_billing_address_only() ) { 338 WC()->customer->set_props( 339 array( 340 'shipping_country' => $request->get_param( 'country' ) ? wc_clean( wp_unslash( $request->get_param( 'country' ) ) ) : null, 341 'shipping_state' => $request->get_param( 'state' ) ? wc_clean( wp_unslash( $request->get_param( 'state' ) ) ) : null, 342 'shipping_postcode' => $request->get_param( 'postcode' ) ? wc_clean( wp_unslash( $request->get_param( 'postcode' ) ) ) : null, 343 'shipping_city' => $request->get_param( 'city' ) ? wc_clean( wp_unslash( $request->get_param( 'city' ) ) ) : null, 344 'shipping_address_1' => $request->get_param( 'address' ) ? wc_clean( wp_unslash( $request->get_param( 'address' ) ) ) : null, 345 'shipping_address_2' => $request->get_param( 'address_2' ) ? wc_clean( wp_unslash( $request->get_param( 'address_2' ) ) ) : null, 346 'shipping_company' => $request->get_param( 'company' ) ? wc_clean( wp_unslash( $request->get_param( 'company' ) ) ) : null, 347 ) 348 ); 349 } else { 350 WC()->customer->set_props( 351 array( 352 'shipping_country' => $request->get_param( 's_country' ) ? wc_clean( wp_unslash( $request->get_param( 's_country' ) ) ) : null, 353 'shipping_state' => $request->get_param( 's_state' ) ? wc_clean( wp_unslash( $request->get_param( 's_state' ) ) ) : null, 354 'shipping_postcode' => $request->get_param( 's_postcode' ) ? wc_clean( wp_unslash( $request->get_param( 's_postcode' ) ) ) : null, 355 'shipping_city' => $request->get_param( 's_city' ) ? wc_clean( wp_unslash( $request->get_param( 's_city' ) ) ) : null, 356 'shipping_address_1' => $request->get_param( 's_address' ) ? wc_clean( wp_unslash( $request->get_param( 's_address' ) ) ) : null, 357 'shipping_address_2' => $request->get_param( 's_address_2' ) ? wc_clean( wp_unslash( $request->get_param( 's_address_2' ) ) ) : null, 358 'shipping_company' => $request->get_param( 's_company' ) ? wc_clean( wp_unslash( $request->get_param( 's_company' ) ) ) : null, 359 ) 360 ); 361 } 362 363 if ( $request->get_param( 'has_full_address' ) && wc_string_to_bool( wc_clean( wp_unslash( $request->get_param( 'has_full_address' ) ) ) ) ) { 364 WC()->customer->set_calculated_shipping( true ); 365 } else { 366 WC()->customer->set_calculated_shipping( false ); 367 } 368 369 WC()->customer->save(); 370 371 // Calculate shipping before totals. This will ensure any shipping methods that affect things like taxes are chosen prior to final totals being calculated. Ref: #22708. 372 WC()->cart->calculate_shipping(); 373 WC()->cart->calculate_totals(); 374 375 // Get order review fragment. 376 ob_start(); 377 woocommerce_order_review(); 378 $woocommerce_order_review = ob_get_clean(); 379 380 // Get checkout payment fragment. 381 ob_start(); 382 woocommerce_checkout_payment(); 383 $woocommerce_checkout_payment = ob_get_clean(); 384 385 // Get messages if reload checkout is not true. 386 $reload_checkout = isset( WC()->session->reload_checkout ) ? true : false; 387 if ( ! $reload_checkout ) { 388 $messages = wc_print_notices( true ); 389 } else { 390 $messages = ''; 391 } 392 393 unset( WC()->session->refresh_totals, WC()->session->reload_checkout ); 394 395 wp_send_json( 396 array( 397 'result' => empty( $messages ) ? 'success' : 'failure', 398 'messages' => $messages, 399 'reload' => $reload_checkout, 400 'nonce' => wp_create_nonce( 'woocommerce-process_checkout' ), 401 'fragments' => apply_filters( 402 'woocommerce_update_order_review_fragments', 403 array( 404 '.woocommerce-checkout-review-order-table' => $woocommerce_order_review, 405 '.woocommerce-checkout-payment' => $woocommerce_checkout_payment, 406 ) 407 ), 408 ) 409 ); 410 } 411 412 public function mobile_builder_checkout() { 413 414 wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true ); 415 WC()->checkout()->process_checkout(); 416 wp_die( 0 ); 417 } 418 264 419 /** 265 420 * Get shipping methods. … … 274 429 // array( 275 430 // 'shipping_country' => 'VE', 276 // 'shipping_state' => isset( $_POST['state'] ) ? wc_clean( wp_unslash( $_POST['state']) ) : null,277 // 'shipping_postcode' => isset( $_POST['postcode'] ) ? wc_clean( wp_unslash( $_POST['postcode']) ) : null,278 // 'shipping_city' => isset( $_POST['city'] ) ? wc_clean( wp_unslash( $_POST['city']) ) : null,279 // 'shipping_address_1' => isset( $_POST['address'] ) ? wc_clean( wp_unslash( $_POST['address']) ) : null,280 // 'shipping_address_2' => isset( $_POST['address_2'] ) ? wc_clean( wp_unslash( $_POST['address_2']) ) : null,431 // 'shipping_state' => $request->get_param( 'state') ) ? wc_clean( wp_unslash( $request->get_param( 'state') ) ) : null, 432 // 'shipping_postcode' => $request->get_param( 'postcode') ) ? wc_clean( wp_unslash( $request->get_param( 'postcode') ) ) : null, 433 // 'shipping_city' => $request->get_param( 'city') ) ? wc_clean( wp_unslash( $request->get_param( 'city') ) ) : null, 434 // 'shipping_address_1' => $request->get_param( 'address') ) ? wc_clean( wp_unslash( $request->get_param( 'address') ) ) : null, 435 // 'shipping_address_2' => $request->get_param( 'address_2') ) ? wc_clean( wp_unslash( $request->get_param( 'address_2') ) ) : null, 281 436 // ) 282 437 // ); -
mobile-builder/trunk/api/class-mobile-builder-vendor.php
r2304807 r2307562 149 149 // Mobile Banner URL 150 150 $mobile_banner_url = $store['mobile_banner'] ? wp_get_attachment_url( $store['mobile_banner'] ) : ''; 151 152 $shipping_methods = WCFMmp_Shipping_Zone::get_shipping_methods( 0, $key ); 151 153 152 154 $store_user = wcfmmp_get_store( $key ); … … 165 167 'count' => $store_user->get_total_review_count(), 166 168 'avg' => $store_user->get_avg_review_rating(), 167 ) 169 ), 170 'shipping_methods' => array_column( $shipping_methods, 'id' ), 168 171 ) ); 169 172 } … … 193 196 */ 194 197 public function vendor( $request ) { 195 $id = $request->get_param( 'id' ); 198 199 $params = $request->get_params(); 200 201 $id = $params['id']; 202 $wcfmmp_radius_lat = $params['wcfmmp_radius_lat']; 203 $wcfmmp_radius_lng = $params['wcfmmp_radius_lng']; 204 196 205 $store = get_user_meta( $id, 'wcfmmp_profile_settings', true ); 197 206 $store_user = wcfmmp_get_store( $id ); … … 209 218 $mobile_banner_url = $store['mobile_banner'] ? wp_get_attachment_url( $store['mobile_banner'] ) : ''; 210 219 220 $shipping_methods = WCFMmp_Shipping_Zone::get_shipping_methods( 0, $id ); 221 222 $distance_matrix = array(); 223 224 if ( $wcfmmp_radius_lat && $wcfmmp_radius_lng && $store['store_lat'] && $store['store_lng'] ) { 225 $origin_string = $store['store_lat'] . ',' . $store['store_lng']; 226 $destinations_string = "$wcfmmp_radius_lat,$wcfmmp_radius_lng"; 227 $key = MBD_GOOGLE_API_KEY; 228 $distance_matrix = mobile_builder_distance_matrix( $origin_string, $destinations_string, $key ); 229 } 230 211 231 return array_merge( $store, array( 232 'id' => $id, 212 233 'gravatar' => $gravatar_url, 213 234 'list_banner_url' => $list_banner_url, … … 217 238 'total_review_rating' => $store_user->get_total_review_rating(), 218 239 'total_review_count' => $store_user->get_total_review_count(), 240 'shipping_methods' => array_column( $shipping_methods, 'id' ), 241 'matrix' => $distance_matrix[0]->elements, 219 242 ) ); 220 243 … … 256 279 global $wpdb; 257 280 258 $lat = $_GET['lat'];259 $lng = $_GET['lng'];281 $lat = $_GET['lat']; 282 $lng = $_GET['lng']; 260 283 $distance = ! empty( $_GET['radius'] ) ? esc_sql( $_GET['radius'] ) : 50; 261 284 … … 270 293 271 294 $args['fields'] .= ", ROUND( {$earth_radius} * acos( cos( radians( {$lat} ) ) * cos( radians( gmw_locations.latitude ) ) * cos( radians( gmw_locations.longitude ) - radians( {$lng} ) ) + sin( radians( {$lat} ) ) * sin( radians( gmw_locations.latitude ) ) ),1 ) AS distance"; 272 $args['join'] .= " INNER JOIN {$wpdb->base_prefix}gmw_locations gmw_locations ON $wpdb->posts.ID = gmw_locations.object_id ";295 $args['join'] .= " INNER JOIN {$wpdb->base_prefix}gmw_locations gmw_locations ON $wpdb->posts.ID = gmw_locations.object_id "; 273 296 274 297 // calculate the between point. -
mobile-builder/trunk/includes/class-mobile-builder.php
r2304807 r2307562 208 208 // Cart 209 209 $plugin_cart = new Mobile_Builder_Cart( $this->get_plugin_name(), $this->get_version() ); 210 $this->loader->add_action( 'wp_loaded', $plugin_cart, ' rnlab_pre_car_rest_api', 5 );210 $this->loader->add_action( 'wp_loaded', $plugin_cart, 'mobile_builder_pre_car_rest_api', 5 ); 211 211 $this->loader->add_action( 'rest_api_init', $plugin_cart, 'add_api_routes', 10 ); 212 // $this->loader->add_filter( 'woocommerce_is_rest_api_request', $plugin_cart, 'simulate_as_not_rest', 10 ); 213 $this->loader->add_filter( 'woocommerce_persistent_cart_enabled', $plugin_cart, 'mobile_builder_woocommerce_persistent_cart_enabled' ); 212 214 213 215 // Vendor -
mobile-builder/trunk/public/class-mobile-builder-public.php
r2304807 r2307562 126 126 'methods' => WP_REST_Server::CREATABLE, 127 127 'callback' => array( $this, 'login' ), 128 ) ); 129 130 register_rest_route( $namespace, 'logout', array( 131 'methods' => WP_REST_Server::READABLE, 132 'callback' => array( $this, 'logout' ), 128 133 ) ); 129 134 … … 1484 1489 1485 1490 /** 1491 * 1492 * Log out user 1493 * 1494 * @return array 1495 * @since 1.0.0 1496 */ 1497 public function logout() { 1498 wp_logout(); 1499 1500 return array( "success" => true ); 1501 } 1502 1503 /** 1486 1504 * Do login with with otp 1487 1505 */
Note: See TracChangeset
for help on using the changeset viewer.