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 / create-script.php

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

129 lines 3.0 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 Enums\Script_Type,
12 Route_Base,
13 };
14 use Cookiez\Modules\Script\Classes\Dto\Script_DTO;
15 use Cookiez\Modules\Script\Components\Script;
16 use Cookiez\Classes\Enums\Cookie_Category;
17
18 use RuntimeException;
19 use Throwable;
20 use WP_REST_Response;
21
22 if ( ! defined( 'ABSPATH' ) ) {
23 exit;
24 }
25
26 /**
27 * Class Create_Script
28 * REST endpoint for creating a managed script
29 */
30 class Create_Script extends Route_Base {
31 public string $path = '';
32
33 public function get_methods(): array {
34 return [ 'POST' ];
35 }
36
37 public function get_name(): string {
38 return 'create-script';
39 }
40
41 protected function sanitize_fields(): array {
42 return [
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 'cookie_id' => ( new Validator() )
55 ->number( [
56 'min' => 1,
57 'message' => esc_html__( 'Expected a positive number', 'cookiez' ),
58 ] )
59 ->nullable(),
60 'name' => ( new Validator() )
61 ->string( [
62 'message' => esc_html__( 'Expected a string', 'cookiez' ),
63 ] )
64 ->nullable(),
65 'value' => ( new Validator() )
66 ->string( [
67 'message' => esc_html__( 'Expected a string', 'cookiez' ),
68 ] ),
69 'description' => ( new Validator() )
70 ->string( [
71 'message' => esc_html__( 'Expected a string', 'cookiez' ),
72 ] )
73 ->nullable(),
74 'category' => ( new Validator() )
75 ->enum(
76 Cookie_Category::class,
77 [ 'message' => esc_html__( 'Expected a valid category value', 'cookiez' ) ]
78 ),
79 'blocking_mode' => ( new Validator() )
80 ->enum(
81 Script_Blocking_Mode::class,
82 [ 'message' => esc_html__( 'Expected a valid blocking mode value', 'cookiez' ) ]
83 ),
84 ];
85 }
86
87 public function POST(): WP_REST_Response {
88 try {
89 $error = $this->verify_capability();
90
91 if ( $error ) {
92 return $error;
93 }
94
95 $validation_errors = $this->validate( $this->params );
96
97 if ( ! empty( $validation_errors ) ) {
98 return $this->respond_validation_error( $validation_errors );
99 }
100
101 $dto = Script_DTO::from_rest_api( $this->params );
102 $dto->type = Script_Type::INLINE;
103
104 try {
105 $created = ( new Script() )->create( $dto );
106 } catch ( RuntimeException $e ) {
107 if ( 'script_duplicate' === $e->getMessage() ) {
108 return $this->respond_validation_error( [
109 'value' => esc_html__( 'A script with this pattern already exists', 'cookiez' ),
110 ] );
111 }
112
113 throw $e;
114 }
115
116 return $this->respond_success_json( [
117 'message' => esc_html__( 'Script created successfully', 'cookiez' ),
118 'script_id' => $created->id,
119 ], 201 );
120
121 } catch ( Throwable $t ) {
122 return $this->respond_error_json( [
123 'message' => $t->getMessage(),
124 'code' => 'internal_server_error',
125 ], 500 );
126 }
127 }
128 }
129