PluginProbe
BlockSpare – Gutenberg Blocks, AI Content Generator & Site Builder for News, Magazine & Blogs / 5.0.1
BlockSpare – Gutenberg Blocks, AI Content Generator & Site Builder for News, Magazine & Blogs v5.0.1
5.0.1 4.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.1.2 2.5.0 2.5.3 All 39 releases
blockspare / inc / theme-builder / class-blockspare-tb-templates-controller.php

class-blockspare-tb-templates-controller.php in BlockSpare – Gutenberg Blocks, AI Content Generator & Site Builder for News, Magazine & Blogs 5.0.1, at inc/theme-builder/class-blockspare-tb-templates-controller.php

183 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 defined('ABSPATH') || exit;
5
6 class Blockspare_TB_Templates_Controller extends WP_REST_Posts_Controller
7 {
8
9 const ALLOWED_CONDITION_TYPES = array(
10 'entire_site',
11 'front_page',
12 'single',
13 'archive',
14 'category',
15 'author',
16 'tag',
17 'date_archive',
18 'search',
19 '404',
20 // ... add the rest of your real types here
21 );
22
23 public function __construct()
24 {
25 parent::__construct(Blockspare_TB_Template_Post_Type::SLUG);
26 }
27
28 /**
29 * Registers routes (parent's CRUD routes) plus our custom meta fields.
30 */
31 public function register_routes()
32 {
33 parent::register_routes();
34 $this->register_condition_fields();
35 }
36
37
38 public function get_items_permissions_check($request)
39 {
40 return current_user_can('edit_pages');
41 }
42
43 public function get_item_permissions_check($request)
44 {
45 return current_user_can('edit_pages');
46 }
47
48 /**
49 * Shared write-permission check.
50 */
51 private function can_edit_post($post)
52 {
53 return current_user_can('edit_post', $post->ID);
54 }
55
56 private function permission_error()
57 {
58 return new WP_Error(
59 'stbldr_rest_cannot_edit',
60 __('Sorry, you are not allowed to edit this template.', 'blockspare'),
61 array('status' => rest_authorization_required_code())
62 );
63 }
64
65 private function read_permission_error()
66 {
67 return new WP_Error(
68 'stbldr_rest_cannot_read',
69 __('Sorry, you are not allowed to view this template.', 'blockspare'),
70 array('status' => rest_authorization_required_code())
71 );
72 }
73
74 private function register_condition_fields()
75 {
76 register_rest_field(
77 Blockspare_TB_Template_Post_Type::SLUG,
78 'conditions',
79 array(
80 'get_callback' => function ($post) {
81
82 if (! current_user_can('edit_pages')) {
83 return $this->read_permission_error();
84 }
85
86 $raw = get_post_meta($post['id'], '_stbldr_conditions', true);
87 $decoded = $raw ? json_decode($raw, true) : null;
88 return is_array($decoded) ? $decoded : array(
89 'include' => array(),
90 'exclude' => array(),
91 );
92 },
93 'update_callback' => function ($value, $post) {
94 if (! $this->can_edit_post($post)) {
95 return $this->permission_error();
96 }
97
98 $sanitized = $this->sanitize_conditions_payload($value);
99 update_post_meta($post->ID, '_stbldr_conditions', wp_json_encode($sanitized));
100 return true;
101 },
102 'schema' => array(
103 'description' => __('Include/exclude display condition rules.', 'blockspare'),
104 'type' => 'object',
105 ),
106 )
107 );
108
109 register_rest_field(
110 Blockspare_TB_Template_Post_Type::SLUG,
111 'priority',
112 array(
113 'get_callback' => function ($post) {
114 if (! current_user_can('edit_pages')) {
115 return $this->read_permission_error();
116 }
117
118 $value = get_post_meta($post['id'], '_stbldr_priority', true);
119 return '' === $value ? 10 : (int) $value;
120 },
121 'update_callback' => function ($value, $post) {
122 if (! $this->can_edit_post($post)) {
123 return $this->permission_error();
124 }
125
126 update_post_meta($post->ID, '_stbldr_priority', absint($value));
127 return true;
128 },
129 'schema' => array(
130 'description' => __('Lower number = higher priority when multiple templates match.', 'blockspare'),
131 'type' => 'integer',
132 ),
133 )
134 );
135 }
136
137 private function sanitize_conditions_payload($value)
138 {
139 $value = is_array($value) ? $value : array();
140
141 $clean = array(
142 'include' => array(),
143 'exclude' => array(),
144 );
145
146 foreach (array('include', 'exclude') as $bucket) {
147 foreach ((array) (isset($value[$bucket]) ? $value[$bucket] : array()) as $rule) {
148 if (! is_array($rule) || empty($rule['type'])) {
149 continue;
150 }
151
152 $type = sanitize_key($rule['type']);
153
154 if (! in_array($type, self::ALLOWED_CONDITION_TYPES, true)) {
155 continue;
156 }
157
158 $settings = array();
159 foreach ((array) (isset($rule['settings']) ? $rule['settings'] : array()) as $key => $val) {
160 if (is_array($val)) {
161 continue;
162 }
163
164 if (is_bool($val)) {
165 $settings[sanitize_key($key)] = $val;
166 } elseif (is_numeric($val)) {
167 $settings[sanitize_key($key)] = absint($val);
168 } else {
169 $settings[sanitize_key($key)] = sanitize_text_field((string) $val);
170 }
171 }
172
173 $clean[$bucket][] = array(
174 'type' => $type,
175 'settings' => $settings,
176 );
177 }
178 }
179
180 return $clean;
181 }
182 }
183