\WP_REST_Server::READABLE, 'callback' => [$this, 'materials'], 'permission_callback' => '__return_true']); register_rest_route('ds82-3dps/v1', '/materials/(?P[A-Za-z0-9_:\-.]+)', [ 'methods' => \WP_REST_Server::READABLE, 'callback' => [$this, 'material'], 'permission_callback' => '__return_true', 'args' => ['identifier' => ['sanitize_callback' => 'sanitize_text_field']], ]); if (class_exists('WooCommerce')) { register_rest_route('ds82-3dps/v1', '/products/(?P\d+)/viewer-config', [ 'methods' => \WP_REST_Server::READABLE, 'callback' => [$this, 'product_config'], 'permission_callback' => [$this, 'can_view_product'], 'args' => ['id' => ['sanitize_callback' => 'absint']], ]); } } public function materials(): \WP_REST_Response { $data = []; foreach ($this->materials->all(false) as $identifier => $material) { $data[$identifier] = $material->jsonSerialize(); } return rest_ensure_response($data); } public function material(\WP_REST_Request $request): \WP_REST_Response|\WP_Error { $material = $this->materials->find((string) $request['identifier']); if (!$material) { return new \WP_Error('ds82_3dps_material_not_found', __('Material not found.', '3d-print-showcase'), ['status' => 404]); } return rest_ensure_response($material->jsonSerialize()); } public function product_config(\WP_REST_Request $request): \WP_REST_Response|\WP_Error { $config = (new ViewerConfig($this->materials))->for_product(absint($request['id'])); if (!$config) { return new \WP_Error('ds82_3d_print_showcase_unavailable', __('Viewer is not available for this product.', '3d-print-showcase'), ['status' => 404]); } return rest_ensure_response($config); } public function can_view_product(\WP_REST_Request $request): bool { $productId = absint($request['id']); return 'publish' === get_post_status($productId) || current_user_can('edit_post', $productId); } }