PluginProbe
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager / trunk
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager vtrunk
0.0.11 0.0.10 0.0.9 0.0.8 0.0.7 0.0.6 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5
cookiez / modules / script / rest / update-script.php

update-script.php in Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager trunk, at modules/script/rest/update-script.php

152 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Cookiez\Modules\Script\Rest;
4
5 use Cookiez\Classes\Rest\{
6 Sanitizer,
7 Validator,
8 };
9 use Cookiez\Modules\Script\Classes\{
10 Enums\Script_Blocking_Mode,
11 Route_Base,
12 };
13 use Cookiez\Modules\Script\Classes\Dto\Script_DTO;
14 use Cookiez\Modules\Script\Components\Script;
15 use Cookiez\Classes\Enums\Cookie_Category;
16
17 use RuntimeException;
18 use Throwable;
19 use WP_REST_Response;
20
21 if ( ! defined( 'ABSPATH' ) ) {
22 exit;
23 }
24
25 /**
26 * Class Update_Script
27 * REST endpoint for updating a managed script
28 */
29 class Update_Script extends Route_Base {
30 public string $path = '(?P<id>\d+)';
31
32 public function get_methods(): array {
33 return [ 'PUT', 'PATCH' ];
34 }
35
36 public function get_name(): string {
37 return 'update-script';
38 }
39
40 protected function sanitize_fields(): array {
41 return [
42 'id' => Sanitizer::absint(),
43 'cookie_id' => Sanitizer::absint(),
44 'name' => Sanitizer::text(),
45 'value' => Sanitizer::text(),
46 'description' => Sanitizer::textarea(),
47 'category' => Sanitizer::text(),
48 'blocking_mode' => Sanitizer::text(),
49 ];
50 }
51
52 protected function validate_fields(): array {
53 return [
54 'id' => ( new Validator() )
55 ->number( [
56 'min' => 1,
57 'message' => esc_html__( 'Expected a positive number', 'cookiez' ),
58 ] ),
59 'cookie_id' => ( new Validator() )
60 ->number( [
61 'min' => 1,
62 'message' => esc_html__( 'Expected a positive number', 'cookiez' ),
63 ] )
64 ->nullable(),
65 'name' => ( new Validator() )
66 ->string( [
67 'message' => esc_html__( 'Expected a string', 'cookiez' ),
68 ] )
69 ->nullable(),
70 'value' => ( new Validator() )
71 ->string( [
72 'message' => esc_html__( 'Expected a string', 'cookiez' ),
73 ] )
74 ->nullable(),
75 'description' => ( new Validator() )
76 ->string( [
77 'message' => esc_html__( 'Expected a string', 'cookiez' ),
78 ] )
79 ->nullable(),
80 'category' => ( new Validator() )
81 ->enum(
82 Cookie_Category::class,
83 [ 'message' => esc_html__( 'Expected a valid category value', 'cookiez' ) ]
84 )
85 ->nullable(),
86 'blocking_mode' => ( new Validator() )
87 ->enum(
88 Script_Blocking_Mode::class,
89 [ 'message' => esc_html__( 'Expected a valid blocking mode value', 'cookiez' ) ]
90 )
91 ->nullable(),
92 ];
93 }
94
95 public function PUT(): WP_REST_Response {
96 return $this->dispatch( 'replace' );
97 }
98
99 public function PATCH(): WP_REST_Response {
100 return $this->dispatch( 'patch' );
101 }
102
103 private function dispatch( string $method ): WP_REST_Response {
104 try {
105 $error = $this->verify_capability();
106
107 if ( $error ) {
108 return $error;
109 }
110
111 $validation_errors = $this->validate( $this->params );
112
113 if ( ! empty( $validation_errors ) ) {
114 return $this->respond_validation_error( $validation_errors );
115 }
116
117 $script_id = (int) $this->params['id'];
118 $dto = Script_DTO::from_rest_api( $this->params );
119
120 try {
121 ( new Script() )->{ $method }( $script_id, $dto );
122 } catch ( RuntimeException $e ) {
123 if ( 'script_not_found' === $e->getMessage() ) {
124 return $this->respond_error_json( [
125 'message' => esc_html__( 'Script not found', 'cookiez' ),
126 'code' => 'script_not_found',
127 ], 404 );
128 }
129
130 if ( 'script_duplicate' === $e->getMessage() ) {
131 return $this->respond_validation_error( [
132 'value' => esc_html__( 'A script with this pattern already exists', 'cookiez' ),
133 ] );
134 }
135
136 throw $e;
137 }
138
139 return $this->respond_success_json( [
140 'message' => esc_html__( 'Script updated successfully', 'cookiez' ),
141 'script_id' => $script_id,
142 ] );
143
144 } catch ( Throwable $t ) {
145 return $this->respond_error_json( [
146 'message' => $t->getMessage(),
147 'code' => 'internal_server_error',
148 ], 500 );
149 }
150 }
151 }
152