route.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Widgets\Init; |
| 4 | |
| 5 | use ShopEngine\Base\Api; |
| 6 | defined('ABSPATH') || exit; |
| 7 | class Route extends Api |
| 8 | { |
| 9 | |
| 10 | public function config() { |
| 11 | |
| 12 | $this->prefix = 'widgets_partials'; |
| 13 | $this->param = ""; |
| 14 | } |
| 15 | |
| 16 | public function get_filter_cat_products() { |
| 17 | |
| 18 | $data = $this->request->get_params(); |
| 19 | |
| 20 | $products = $data['products']; |
| 21 | $order = isset($data['order']) ? $data['order'] : 'DESC'; |
| 22 | $order_by = isset($data['order_by']) ? $data['order_by'] : 'date'; |
| 23 | $settings = isset($data['settings']) ? $data['settings'] : null; |
| 24 | |
| 25 | if(empty($products)) { |
| 26 | |
| 27 | echo "<p class='error'>" . esc_html__('No products were found.', 'shopengine') . "</p>"; |
| 28 | |
| 29 | exit(); |
| 30 | } |
| 31 | |
| 32 | $args = array( |
| 33 | 'post_type' => 'product', |
| 34 | 'order' => $order, |
| 35 | 'orderby' => $order_by, |
| 36 | 'post__in' => $products, |
| 37 | 'posts_per_page' => 50 |
| 38 | ); |
| 39 | |
| 40 | |
| 41 | $query = new \WP_Query($args); |
| 42 | |
| 43 | if($query->have_posts()) : |
| 44 | while($query->have_posts()) : $query->the_post(); |
| 45 | |
| 46 | $default_content = [ |
| 47 | 'image' => 0, |
| 48 | 'category' => 0, |
| 49 | 'title' => 0, |
| 50 | 'rating' => 0, |
| 51 | 'price' => 0, |
| 52 | 'description' => 0, |
| 53 | 'buttons' => 0, |
| 54 | ]; |
| 55 | |
| 56 | |
| 57 | |
| 58 | $content = (!empty($view) ? $view : $default_content); |
| 59 | asort($content, SORT_NUMERIC); |
| 60 | |
| 61 | if( $settings['shopengine_is_cats'] != 'yes' ) unset($content['category']); |
| 62 | if( $settings['shopengine_is_details'] != 'yes' ) { |
| 63 | unset($content['description']); |
| 64 | } |
| 65 | if( $settings['shopengine_is_product_rating'] != 'yes' ) unset($content['rating']); |
| 66 | |
| 67 | |
| 68 | ?> <div class='shopengine-single-product-item'> <?php |
| 69 | foreach($content as $key => $value) { |
| 70 | $function = '_product_' . $key; |
| 71 | \ShopEngine\Utils\Helper::$function($settings); |
| 72 | } |
| 73 | ?> </div> <?php |
| 74 | |
| 75 | endwhile; |
| 76 | endif; |
| 77 | |
| 78 | wp_reset_postdata(); |
| 79 | |
| 80 | exit(); |
| 81 | } |
| 82 | |
| 83 | public function post_checkout_login() { |
| 84 | |
| 85 | // Verify nonce for CSRF protection |
| 86 | $nonce = $this->request->get_header('X-WP-Nonce'); |
| 87 | if (empty($nonce) || !wp_verify_nonce($nonce, 'wp_rest')) { |
| 88 | return new \WP_Error('rest_forbidden', esc_html__('Invalid nonce.', 'shopengine'), array('status' => 403)); |
| 89 | } |
| 90 | |
| 91 | // The wp_rest nonce above is not session-bound for logged-out requests, so it |
| 92 | // can be harvested from any public page and replayed cross-origin. Require the |
| 93 | // request to actually originate from this site as the real CSRF guard. |
| 94 | $source = $this->request->get_header('Origin'); |
| 95 | if (empty($source)) { |
| 96 | $source = $this->request->get_header('Referer'); |
| 97 | } |
| 98 | $source_host = $source ? wp_parse_url($source, PHP_URL_HOST) : ''; |
| 99 | if (empty($source_host) || strcasecmp($source_host, wp_parse_url(home_url(), PHP_URL_HOST)) !== 0) { |
| 100 | return new \WP_Error('rest_forbidden', esc_html__('Invalid request origin.', 'shopengine'), array('status' => 403)); |
| 101 | } |
| 102 | |
| 103 | $data = $this->request->get_params(); |
| 104 | if(isset($data['rememberme'])) { |
| 105 | $data['rememberme'] = $data['rememberme'] == 'true' ? true : false; |
| 106 | } |
| 107 | $user = wp_signon($data); |
| 108 | if(is_wp_error($user)) { |
| 109 | return new \WP_Error('failed_login', $user->get_error_message(), ['status' => 404]); |
| 110 | } |
| 111 | return true; |
| 112 | } |
| 113 | } |
| 114 |