PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.5.2
GenerateBlocks v1.5.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
class-do-css.php 4 years ago class-dynamic-content.php 4 years ago class-enqueue-css.php 4 years ago class-legacy-attributes.php 4 years ago class-plugin-update.php 5 years ago class-query-loop.php 4 years ago class-render-blocks.php 4 years ago class-rest.php 4 years ago class-settings.php 4 years ago dashboard.php 4 years ago defaults.php 4 years ago functions.php 4 years ago general.php 4 years ago generate-css.php 4 years ago
class-rest.php
211 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 * Initiator.
40 *
41 * @return object initialized object of class.
42 */
43 public static function get_instance() {
44 if ( ! isset( self::$instance ) ) {
45 self::$instance = new self();
46 }
47
48 return self::$instance;
49 }
50
51 /**
52 * GenerateBlocks_Rest constructor.
53 */
54 public function __construct() {
55 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
56 }
57
58 /**
59 * Register rest routes.
60 */
61 public function register_routes() {
62 $namespace = $this->namespace . $this->version;
63
64 // Update Settings.
65 register_rest_route(
66 $namespace,
67 '/settings/',
68 array(
69 'methods' => WP_REST_Server::EDITABLE,
70 'callback' => array( $this, 'update_settings' ),
71 'permission_callback' => array( $this, 'update_settings_permission' ),
72 )
73 );
74
75 // Regenerate CSS Files.
76 register_rest_route(
77 $namespace,
78 '/regenerate_css_files/',
79 array(
80 'methods' => WP_REST_Server::EDITABLE,
81 'callback' => array( $this, 'regenerate_css_files' ),
82 'permission_callback' => array( $this, 'update_settings_permission' ),
83 )
84 );
85 }
86
87 /**
88 * Get edit options permissions.
89 *
90 * @return bool
91 */
92 public function update_settings_permission() {
93 return current_user_can( 'manage_options' );
94 }
95
96 /**
97 * Sanitize our options.
98 *
99 * @since 1.2.0
100 * @param string $name The setting name.
101 * @param mixed $value The value to save.
102 */
103 public function sanitize_value( $name, $value ) {
104 $callbacks = apply_filters(
105 'generateblocks_option_sanitize_callbacks',
106 array(
107 'container_width' => 'absint',
108 'css_print_method' => 'sanitize_text_field',
109 'sync_responsive_previews' => 'rest_sanitize_boolean',
110 )
111 );
112
113 $callback = $callbacks[ $name ];
114
115 if ( ! is_callable( $callback ) ) {
116 return sanitize_text_field( $value );
117 }
118
119 return $callback( $value );
120 }
121
122 /**
123 * Update Settings.
124 *
125 * @param WP_REST_Request $request request object.
126 *
127 * @return mixed
128 */
129 public function update_settings( WP_REST_Request $request ) {
130 $current_settings = get_option( 'generateblocks', array() );
131 $new_settings = $request->get_param( 'settings' );
132
133 foreach ( $new_settings as $name => $value ) {
134 // Skip if the option hasn't changed.
135 if ( isset( $current_settings[ $name ] ) && $current_settings[ $name ] === $new_settings[ $name ] ) {
136 unset( $new_settings[ $name ] );
137 continue;
138 }
139
140 // Only save options that we know about.
141 if ( ! array_key_exists( $name, generateblocks_get_option_defaults() ) ) {
142 unset( $new_settings[ $name ] );
143 continue;
144 }
145
146 // Sanitize our value.
147 $new_settings[ $name ] = $this->sanitize_value( $name, $value );
148 }
149
150 if ( empty( $new_settings ) ) {
151 return $this->success( __( 'No changes found.', 'generateblocks' ) );
152 }
153
154 if ( is_array( $new_settings ) ) {
155 update_option( 'generateblocks', array_merge( $current_settings, $new_settings ) );
156 }
157
158 return $this->success( __( 'Settings saved.', 'generateblocks' ) );
159 }
160
161 /**
162 * Regenerate CSS Files.
163 *
164 * @param WP_REST_Request $request request object.
165 *
166 * @return mixed
167 */
168 public function regenerate_css_files( WP_REST_Request $request ) {
169 update_option( 'generateblocks_dynamic_css_posts', array() );
170
171 return $this->success( __( 'CSS files regenerated.', 'generateblocks' ) );
172 }
173
174 /**
175 * Success rest.
176 *
177 * @param mixed $response response data.
178 * @return mixed
179 */
180 public function success( $response ) {
181 return new WP_REST_Response(
182 array(
183 'success' => true,
184 'response' => $response,
185 ),
186 200
187 );
188 }
189
190 /**
191 * Error rest.
192 *
193 * @param mixed $code error code.
194 * @param mixed $response response data.
195 * @return mixed
196 */
197 public function error( $code, $response ) {
198 return new WP_REST_Response(
199 array(
200 'error' => true,
201 'success' => false,
202 'error_code' => $code,
203 'response' => $response,
204 ),
205 401
206 );
207 }
208 }
209
210 GenerateBlocks_Rest::get_instance();
211