PluginProbe
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager / 0.0.3
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager v0.0.3
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 / cookie / rest / create-cookie.php

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

125 lines 2.9 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\Cookie\Rest;
4
5 use Cookiez\Classes\Rest\{
6 Sanitizer,
7 Validator,
8 };
9 use Cookiez\Classes\Enums\Cookie_Category;
10 use Cookiez\Classes\Utils;
11 use Cookiez\Modules\Cookie\Classes\{
12 Enums\Cookie_Type,
13 Route_Base,
14 };
15 use Cookiez\Modules\Cookie\Classes\Dto\Cookie_DTO;
16 use Cookiez\Modules\Cookie\Components\Cookie;
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_Cookie
28 * REST endpoint for creating manual cookies
29 */
30 class Create_Cookie 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-cookie';
39 }
40
41 protected function sanitize_fields(): array {
42 return [
43 'name' => Sanitizer::text(),
44 'domain' => Sanitizer::text(),
45 'duration' => Sanitizer::int(),
46 'category' => Sanitizer::text(),
47 'description' => Sanitizer::textarea(),
48 ];
49 }
50
51 protected function validate_fields(): array {
52 return [
53 'name' => ( new Validator() )
54 ->fn(
55 fn( $value ) => Utils::is_valid_cookie_name( $value ),
56 [ 'message' => esc_html__( 'Cookie name has invalid characters', 'cookiez' ) ]
57 ),
58 'domain' => ( new Validator() )
59 ->fn(
60 fn( $value ) => Utils::is_valid_cookie_domain( $value ),
61 [ 'message' => esc_html__( 'Expected a valid domain', 'cookiez' ) ]
62 )
63 ->nullable(),
64 'duration' => ( new Validator() )
65 ->number( [
66 'min' => 1,
67 'message' => esc_html__( 'Expected a positive number', 'cookiez' ),
68 ] )
69 ->nullable(),
70 'category' => ( new Validator() )
71 ->enum(
72 Cookie_Category::class,
73 [ 'message' => esc_html__( 'Expected a valid category value', 'cookiez' ) ]
74 ),
75 'description' => ( new Validator() )
76 ->string( [
77 'message' => esc_html__( 'Expected a string', 'cookiez' ),
78 ] )
79 ->nullable(),
80 ];
81 }
82
83 public function POST(): WP_REST_Response {
84 try {
85 $error = $this->verify_capability();
86
87 if ( $error ) {
88 return $error;
89 }
90
91 $validation_errors = $this->validate( $this->params );
92
93 if ( ! empty( $validation_errors ) ) {
94 return $this->respond_validation_error( $validation_errors );
95 }
96
97 $dto = Cookie_DTO::from_rest_api( $this->params );
98 $dto->type = Cookie_Type::MANUAL;
99
100 try {
101 $created = ( new Cookie() )->create( $dto );
102 } catch ( RuntimeException $e ) {
103 if ( 'cookie_duplicate' === $e->getMessage() ) {
104 return $this->respond_error_json( [
105 'message' => esc_html__( 'Cookie with this name and domain already exists', 'cookiez' ),
106 'code' => 'bad_request',
107 ], 409 );
108 }
109
110 throw $e;
111 }
112
113 return $this->respond_success_json( [
114 'message' => esc_html__( 'Cookie created successfully', 'cookiez' ),
115 'cookie_id' => $created->id,
116 ], 201 );
117 } catch ( Throwable $t ) {
118 return $this->respond_error_json( [
119 'message' => $t->getMessage(),
120 'code' => 'internal_server_error',
121 ], 500 );
122 }
123 }
124 }
125