| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\DefaultStyles; |
| 4 |
|
| 5 |
use Elementor\Core\Kits\Documents\Kit; |
| 6 |
use Elementor\Core\Utils\Api\Error_Builder; |
| 7 |
use Elementor\Core\Utils\Api\Response_Builder; |
| 8 |
use Elementor\Plugin; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
class Default_Styles_REST_API { |
| 15 |
const API_NAMESPACE = 'elementor/v1'; |
| 16 |
const API_BASE = 'default-styles'; |
| 17 |
|
| 18 |
private ?Default_Styles_Repository $repository = null; |
| 19 |
private ?Kit $kit = null; |
| 20 |
|
| 21 |
public function register_hooks() { |
| 22 |
add_action( 'rest_api_init', fn() => $this->register_routes() ); |
| 23 |
} |
| 24 |
|
| 25 |
private function get_kit(): ?Kit { |
| 26 |
if ( ! $this->kit ) { |
| 27 |
$this->kit = Plugin::$instance->kits_manager->get_active_kit(); |
| 28 |
} |
| 29 |
|
| 30 |
return $this->kit; |
| 31 |
} |
| 32 |
|
| 33 |
private function get_repository(): Default_Styles_Repository { |
| 34 |
if ( ! $this->repository ) { |
| 35 |
$this->repository = new Default_Styles_Repository( $this->get_kit() ); |
| 36 |
} |
| 37 |
|
| 38 |
return $this->repository; |
| 39 |
} |
| 40 |
|
| 41 |
private function register_routes() { |
| 42 |
$this->repository = null; |
| 43 |
$this->kit = null; |
| 44 |
|
| 45 |
register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE, [ |
| 46 |
[ |
| 47 |
'methods' => 'GET', |
| 48 |
'callback' => fn( $request ) => $this->route_wrapper( fn() => $this->all( $request ) ), |
| 49 |
'permission_callback' => fn() => current_user_can( 'edit_posts' ), |
| 50 |
], |
| 51 |
] ); |
| 52 |
|
| 53 |
register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/(?P<tag>[a-z0-9]+)', [ |
| 54 |
[ |
| 55 |
'methods' => 'GET', |
| 56 |
'callback' => fn( $request ) => $this->route_wrapper( fn() => $this->get_one( $request ) ), |
| 57 |
'permission_callback' => fn() => current_user_can( 'edit_posts' ), |
| 58 |
'args' => [ |
| 59 |
'tag' => [ |
| 60 |
'type' => 'string', |
| 61 |
'required' => true, |
| 62 |
], |
| 63 |
], |
| 64 |
], |
| 65 |
[ |
| 66 |
'methods' => 'PUT', |
| 67 |
'callback' => fn( $request ) => $this->route_wrapper( fn() => $this->put( $request ) ), |
| 68 |
'permission_callback' => fn() => current_user_can( 'manage_options' ), |
| 69 |
'args' => [ |
| 70 |
'tag' => [ |
| 71 |
'type' => 'string', |
| 72 |
'required' => true, |
| 73 |
], |
| 74 |
'variants' => [ |
| 75 |
'type' => 'array', |
| 76 |
'required' => true, |
| 77 |
], |
| 78 |
'type' => [ |
| 79 |
'type' => 'string', |
| 80 |
'enum' => [ 'class' ], |
| 81 |
'required' => false, |
| 82 |
], |
| 83 |
], |
| 84 |
], |
| 85 |
[ |
| 86 |
'methods' => 'DELETE', |
| 87 |
'callback' => fn( $request ) => $this->route_wrapper( fn() => $this->delete( $request ) ), |
| 88 |
'permission_callback' => fn() => current_user_can( 'manage_options' ), |
| 89 |
'args' => [ |
| 90 |
'tag' => [ |
| 91 |
'type' => 'string', |
| 92 |
'required' => true, |
| 93 |
], |
| 94 |
], |
| 95 |
], |
| 96 |
] ); |
| 97 |
} |
| 98 |
|
| 99 |
private function all( \WP_REST_Request $request ) { |
| 100 |
$items = $this->get_repository()->all(); |
| 101 |
|
| 102 |
return Response_Builder::make( (object) $items )->build(); |
| 103 |
} |
| 104 |
|
| 105 |
private function get_one( \WP_REST_Request $request ) { |
| 106 |
$tag = $request->get_param( 'tag' ); |
| 107 |
|
| 108 |
if ( ! Default_Styles_Repository::is_allowed_tag( $tag ) ) { |
| 109 |
return Error_Builder::make( 'invalid_tag' ) |
| 110 |
->set_status( 400 ) |
| 111 |
->set_message( 'Invalid HTML tag.' ) |
| 112 |
->build(); |
| 113 |
} |
| 114 |
|
| 115 |
$item = $this->get_repository()->get( $tag ); |
| 116 |
|
| 117 |
if ( ! $item ) { |
| 118 |
return Error_Builder::make( 'not_found' ) |
| 119 |
->set_status( 404 ) |
| 120 |
->set_message( 'Default style not found.' ) |
| 121 |
->build(); |
| 122 |
} |
| 123 |
|
| 124 |
return Response_Builder::make( $item )->build(); |
| 125 |
} |
| 126 |
|
| 127 |
private function put( \WP_REST_Request $request ) { |
| 128 |
$tag = $request->get_param( 'tag' ); |
| 129 |
|
| 130 |
if ( ! Default_Styles_Repository::is_allowed_tag( $tag ) ) { |
| 131 |
return Error_Builder::make( 'invalid_tag' ) |
| 132 |
->set_status( 400 ) |
| 133 |
->set_message( 'Invalid HTML tag.' ) |
| 134 |
->build(); |
| 135 |
} |
| 136 |
|
| 137 |
if ( ! $this->get_repository()->put( |
| 138 |
$tag, |
| 139 |
[ |
| 140 |
'type' => 'class', |
| 141 |
'variants' => $request->get_param( 'variants' ), |
| 142 |
] |
| 143 |
) ) { |
| 144 |
return Error_Builder::make( 'persist_failed' ) |
| 145 |
->set_status( 500 ) |
| 146 |
->set_message( __( 'Failed to save default style.', 'elementor' ) ) |
| 147 |
->build(); |
| 148 |
} |
| 149 |
|
| 150 |
$item = $this->get_repository()->get( $tag ); |
| 151 |
|
| 152 |
return Response_Builder::make( $item )->build(); |
| 153 |
} |
| 154 |
|
| 155 |
private function delete( \WP_REST_Request $request ) { |
| 156 |
$tag = $request->get_param( 'tag' ); |
| 157 |
|
| 158 |
if ( ! Default_Styles_Repository::is_allowed_tag( $tag ) ) { |
| 159 |
return Error_Builder::make( 'invalid_tag' ) |
| 160 |
->set_status( 400 ) |
| 161 |
->set_message( 'Invalid HTML tag.' ) |
| 162 |
->build(); |
| 163 |
} |
| 164 |
|
| 165 |
$this->get_repository()->delete( $tag ); |
| 166 |
|
| 167 |
return Response_Builder::make( [ 'deleted' => true ] )->build(); |
| 168 |
} |
| 169 |
|
| 170 |
private function route_wrapper( callable $callback ) { |
| 171 |
try { |
| 172 |
return $callback(); |
| 173 |
} catch ( \Throwable $e ) { |
| 174 |
return Error_Builder::make( 'default_styles_error' ) |
| 175 |
->set_status( 500 ) |
| 176 |
->set_message( __( 'Something went wrong', 'elementor' ) ) |
| 177 |
->build(); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|