woocommerce
/
includes
/
rest-api
/
Controllers
/
Version3
/
class-wc-rest-layout-templates-controller.php
class-wc-rest-controller.php
1 year ago
class-wc-rest-coupons-controller.php
4 years ago
class-wc-rest-crud-controller.php
7 months ago
class-wc-rest-customer-downloads-controller.php
5 years ago
class-wc-rest-customers-controller.php
3 years ago
class-wc-rest-data-continents-controller.php
4 months ago
class-wc-rest-data-controller.php
5 years ago
class-wc-rest-data-countries-controller.php
4 months ago
class-wc-rest-data-currencies-controller.php
4 months ago
class-wc-rest-layout-templates-controller.php
2 years ago
class-wc-rest-network-orders-controller.php
5 years ago
class-wc-rest-order-notes-controller.php
5 years ago
class-wc-rest-order-refunds-controller.php
2 months ago
class-wc-rest-orders-controller.php
2 months ago
class-wc-rest-payment-gateways-controller.php
1 year ago
class-wc-rest-paypal-buttons-controller.php
5 months ago
class-wc-rest-paypal-standard-controller.php
3 months ago
class-wc-rest-paypal-webhooks-controller.php
3 months ago
class-wc-rest-posts-controller.php
2 years ago
class-wc-rest-product-attribute-terms-controller.php
5 years ago
class-wc-rest-product-attributes-controller.php
2 years ago
class-wc-rest-product-brands-controller.php
1 year ago
class-wc-rest-product-categories-controller.php
3 months ago
class-wc-rest-product-custom-fields-controller.php
1 year ago
class-wc-rest-product-reviews-controller.php
1 year ago
class-wc-rest-product-shipping-classes-controller.php
1 year ago
class-wc-rest-product-tags-controller.php
5 years ago
class-wc-rest-product-variations-controller.php
1 month ago
class-wc-rest-products-catalog-controller.php
7 months ago
class-wc-rest-products-controller.php
2 months ago
class-wc-rest-refunds-controller.php
2 years ago
class-wc-rest-report-coupons-totals-controller.php
5 years ago
class-wc-rest-report-customers-totals-controller.php
5 years ago
class-wc-rest-report-orders-totals-controller.php
2 years ago
class-wc-rest-report-products-totals-controller.php
5 years ago
class-wc-rest-report-reviews-totals-controller.php
5 years ago
class-wc-rest-report-sales-controller.php
5 years ago
class-wc-rest-report-top-sellers-controller.php
5 years ago
class-wc-rest-reports-controller.php
5 years ago
class-wc-rest-setting-options-controller.php
4 months ago
class-wc-rest-settings-controller.php
5 years ago
class-wc-rest-shipping-methods-controller.php
5 years ago
class-wc-rest-shipping-zone-locations-controller.php
5 years ago
class-wc-rest-shipping-zone-methods-controller.php
5 years ago
class-wc-rest-shipping-zones-controller-base.php
5 years ago
class-wc-rest-shipping-zones-controller.php
5 years ago
class-wc-rest-system-status-controller.php
5 years ago
class-wc-rest-system-status-tools-controller.php
5 years ago
class-wc-rest-tax-classes-controller.php
5 years ago
class-wc-rest-taxes-controller.php
5 years ago
class-wc-rest-terms-controller.php
1 month ago
class-wc-rest-variations-controller.php
9 months ago
class-wc-rest-webhooks-controller.php
5 years ago
class-wc-rest-layout-templates-controller.php
147 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Layout Templates controller |
| 4 | * |
| 5 | * Handles requests to /layout-templates. |
| 6 | * |
| 7 | * @package WooCommerce\RestApi |
| 8 | * @since 8.6.0 |
| 9 | */ |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | use Automattic\WooCommerce\LayoutTemplates\LayoutTemplateRegistry; |
| 14 | |
| 15 | /** |
| 16 | * REST API Layout Templates controller class. |
| 17 | */ |
| 18 | class WC_REST_Layout_Templates_Controller extends WC_REST_Controller { |
| 19 | |
| 20 | /** |
| 21 | * Endpoint namespace. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | protected $namespace = 'wc/v3'; |
| 26 | |
| 27 | /** |
| 28 | * Route base. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | protected $rest_base = 'layout-templates'; |
| 33 | |
| 34 | /** |
| 35 | * Register the routes for template layouts. |
| 36 | */ |
| 37 | public function register_routes() { |
| 38 | register_rest_route( |
| 39 | $this->namespace, |
| 40 | '/' . $this->rest_base, |
| 41 | array( |
| 42 | 'methods' => WP_REST_Server::READABLE, |
| 43 | 'callback' => array( $this, 'get_items' ), |
| 44 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 45 | 'args' => array( |
| 46 | 'area' => array( |
| 47 | 'description' => __( 'Area to get templates for.', 'woocommerce' ), |
| 48 | 'type' => 'string', |
| 49 | 'default' => '', |
| 50 | ), |
| 51 | ), |
| 52 | ) |
| 53 | ); |
| 54 | |
| 55 | register_rest_route( |
| 56 | $this->namespace, |
| 57 | '/' . $this->rest_base . '/(?P<id>\w[\w\s\-]*)', |
| 58 | array( |
| 59 | 'args' => array( |
| 60 | 'id' => array( |
| 61 | 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
| 62 | 'type' => 'string', |
| 63 | ), |
| 64 | ), |
| 65 | array( |
| 66 | 'methods' => WP_REST_Server::READABLE, |
| 67 | 'callback' => array( $this, 'get_item' ), |
| 68 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
| 69 | 'args' => array(), |
| 70 | ), |
| 71 | ) |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Check if a given request has access to read template layouts. |
| 77 | * |
| 78 | * @param WP_REST_Request $request The request. |
| 79 | */ |
| 80 | public function get_items_permissions_check( $request ): bool { |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Check if a given request has access to read a template layout. |
| 86 | * |
| 87 | * @param WP_REST_Request $request The request. |
| 88 | */ |
| 89 | public function get_item_permissions_check( $request ): bool { |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Handle request for template layouts. |
| 95 | * |
| 96 | * @param WP_REST_Request $request The request. |
| 97 | */ |
| 98 | public function get_items( $request ) { |
| 99 | $layout_templates = $this->get_layout_templates( |
| 100 | array( |
| 101 | 'area' => $request['area'], |
| 102 | ) |
| 103 | ); |
| 104 | |
| 105 | $response = rest_ensure_response( $layout_templates ); |
| 106 | |
| 107 | return $response; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Handle request for a single template layout. |
| 112 | * |
| 113 | * @param WP_REST_Request $request The request. |
| 114 | */ |
| 115 | public function get_item( $request ) { |
| 116 | $layout_templates = $this->get_layout_templates( |
| 117 | array( |
| 118 | 'id' => $request['id'], |
| 119 | ) |
| 120 | ); |
| 121 | |
| 122 | if ( count( $layout_templates ) !== 1 ) { |
| 123 | return new WP_Error( 'woocommerce_rest_layout_template_invalid_id', __( 'Invalid layout template ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
| 124 | } |
| 125 | |
| 126 | $response = rest_ensure_response( current( $layout_templates ) ); |
| 127 | |
| 128 | return $response; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get layout templates. |
| 133 | * |
| 134 | * @param array $query_params Query params. |
| 135 | */ |
| 136 | private function get_layout_templates( array $query_params ): array { |
| 137 | $layout_template_registry = wc_get_container()->get( LayoutTemplateRegistry::class ); |
| 138 | |
| 139 | return array_map( |
| 140 | function( $layout_template ) { |
| 141 | return $layout_template->to_json(); |
| 142 | }, |
| 143 | $layout_template_registry->instantiate_layout_templates( $query_params ) |
| 144 | ); |
| 145 | } |
| 146 | } |
| 147 |