PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.8.2
GenerateBlocks v1.8.2
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / includes / class-rest.php
generateblocks / includes Last commit date
blocks 2 years ago class-do-css.php 2 years ago class-dynamic-content.php 2 years ago class-enqueue-css.php 3 years ago class-legacy-attributes.php 4 years ago class-map-deprecated-attributes.php 2 years ago class-plugin-update.php 5 years ago class-query-loop.php 3 years ago class-render-blocks.php 3 years ago class-rest.php 2 years ago class-settings.php 4 years ago dashboard.php 3 years ago defaults.php 2 years ago functions.php 2 years ago general.php 2 years ago
class-rest.php
261 lines
1 <?php
2 /**
3 * Rest API functions
4 *
5 * @package GenerateBlocks
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Class GenerateBlocks_Rest
14 */
15 class GenerateBlocks_Rest extends WP_REST_Controller {
16 /**
17 * Instance.
18 *
19 * @access private
20 * @var object Instance
21 */
22 private static $instance;
23
24 /**
25 * Namespace.
26 *
27 * @var string
28 */
29 protected $namespace = 'generateblocks/v';
30
31 /**
32 * Version.
33 *
34 * @var string
35 */
36 protected $version = '1';
37
38 /**
39 * Onboarding meta key.
40 *
41 * @var string
42 */
43 const ONBOARDING_META_KEY = 'generateblocks_onboarding';
44
45 /**
46 * Initiator.
47 *
48 * @return object initialized object of class.
49 */
50 public static function get_instance() {
51 if ( ! isset( self::$instance ) ) {
52 self::$instance = new self();
53 }
54
55 return self::$instance;
56 }
57
58 /**
59 * GenerateBlocks_Rest constructor.
60 */
61 public function __construct() {
62 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
63 }
64
65 /**
66 * Register rest routes.
67 */
68 public function register_routes() {
69 $namespace = $this->namespace . $this->version;
70
71 // Update Settings.
72 register_rest_route(
73 $namespace,
74 '/settings/',
75 array(
76 'methods' => WP_REST_Server::EDITABLE,
77 'callback' => array( $this, 'update_settings' ),
78 'permission_callback' => array( $this, 'update_settings_permission' ),
79 )
80 );
81
82 // Regenerate CSS Files.
83 register_rest_route(
84 $namespace,
85 '/regenerate_css_files/',
86 array(
87 'methods' => WP_REST_Server::EDITABLE,
88 'callback' => array( $this, 'regenerate_css_files' ),
89 'permission_callback' => array( $this, 'update_settings_permission' ),
90 )
91 );
92
93 register_rest_route(
94 $namespace,
95 '/onboarding/',
96 array(
97 'methods' => WP_REST_Server::EDITABLE,
98 'callback' => array( $this, 'onboarding' ),
99 'permission_callback' => array( $this, 'onboarding_permission' ),
100 )
101 );
102 }
103
104 /**
105 * Get edit options permissions.
106 *
107 * @return bool
108 */
109 public function update_settings_permission() {
110 return current_user_can( 'manage_options' );
111 }
112
113 /**
114 * Sanitize our options.
115 *
116 * @since 1.2.0
117 * @param string $name The setting name.
118 * @param mixed $value The value to save.
119 */
120 public function sanitize_value( $name, $value ) {
121 $callbacks = apply_filters(
122 'generateblocks_option_sanitize_callbacks',
123 array(
124 'container_width' => 'absint',
125 'css_print_method' => 'sanitize_text_field',
126 'sync_responsive_previews' => 'rest_sanitize_boolean',
127 'disable_google_fonts' => 'rest_sanitize_boolean',
128 )
129 );
130
131 $callback = $callbacks[ $name ];
132
133 if ( ! is_callable( $callback ) ) {
134 return sanitize_text_field( $value );
135 }
136
137 return $callback( $value );
138 }
139
140 /**
141 * Update Settings.
142 *
143 * @param WP_REST_Request $request request object.
144 *
145 * @return mixed
146 */
147 public function update_settings( WP_REST_Request $request ) {
148 $current_settings = get_option( 'generateblocks', array() );
149 $new_settings = $request->get_param( 'settings' );
150
151 foreach ( $new_settings as $name => $value ) {
152 // Skip if the option hasn't changed.
153 if ( isset( $current_settings[ $name ] ) && $current_settings[ $name ] === $new_settings[ $name ] ) {
154 unset( $new_settings[ $name ] );
155 continue;
156 }
157
158 // Only save options that we know about.
159 if ( ! array_key_exists( $name, generateblocks_get_option_defaults() ) ) {
160 unset( $new_settings[ $name ] );
161 continue;
162 }
163
164 // Sanitize our value.
165 $new_settings[ $name ] = $this->sanitize_value( $name, $value );
166 }
167
168 if ( empty( $new_settings ) ) {
169 return $this->success( __( 'No changes found.', 'generateblocks' ) );
170 }
171
172 if ( is_array( $new_settings ) ) {
173 update_option( 'generateblocks', array_merge( $current_settings, $new_settings ) );
174 }
175
176 return $this->success( __( 'Settings saved.', 'generateblocks' ) );
177 }
178
179 /**
180 * Regenerate CSS Files.
181 *
182 * @param WP_REST_Request $request request object.
183 *
184 * @return mixed
185 */
186 public function regenerate_css_files( WP_REST_Request $request ) {
187 update_option( 'generateblocks_dynamic_css_posts', array() );
188
189 return $this->success( __( 'CSS files regenerated.', 'generateblocks' ) );
190 }
191
192 /**
193 * Mark an onboard as "viewed" by the user.
194 *
195 * @param WP_REST_Request $request request object.
196 *
197 * @return WP_REST_Response The response.
198 */
199 public function onboarding( WP_REST_Request $request ) {
200 $user_id = get_current_user_id();
201 $onboard = get_user_meta( $user_id, self::ONBOARDING_META_KEY, true );
202 $key = $request->get_param( 'key' );
203
204 if ( ! $onboard ) {
205 $onboard = array();
206 }
207
208 $onboard[ $key ] = true;
209
210 update_user_meta( get_current_user_id(), self::ONBOARDING_META_KEY, $onboard );
211
212 return new WP_REST_Response( array( 'success' => true ), 200 );
213 }
214
215 /**
216 * Get onboarding edit permission.
217 *
218 * @return bool
219 */
220 public function onboarding_permission() {
221 return current_user_can( 'edit_posts' );
222 }
223
224 /**
225 * Success rest.
226 *
227 * @param mixed $response response data.
228 * @return mixed
229 */
230 public function success( $response ) {
231 return new WP_REST_Response(
232 array(
233 'success' => true,
234 'response' => $response,
235 ),
236 200
237 );
238 }
239
240 /**
241 * Error rest.
242 *
243 * @param mixed $code error code.
244 * @param mixed $response response data.
245 * @return mixed
246 */
247 public function error( $code, $response ) {
248 return new WP_REST_Response(
249 array(
250 'error' => true,
251 'success' => false,
252 'error_code' => $code,
253 'response' => $response,
254 ),
255 401
256 );
257 }
258 }
259
260 GenerateBlocks_Rest::get_instance();
261