blocks
3 years ago
class-do-css.php
3 years ago
class-dynamic-content.php
3 years ago
class-enqueue-css.php
3 years ago
class-legacy-attributes.php
4 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
3 years ago
class-settings.php
4 years ago
dashboard.php
3 years ago
defaults.php
3 years ago
functions.php
3 years ago
general.php
3 years ago
class-rest.php
260 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 | ) |
| 128 | ); |
| 129 | |
| 130 | $callback = $callbacks[ $name ]; |
| 131 | |
| 132 | if ( ! is_callable( $callback ) ) { |
| 133 | return sanitize_text_field( $value ); |
| 134 | } |
| 135 | |
| 136 | return $callback( $value ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Update Settings. |
| 141 | * |
| 142 | * @param WP_REST_Request $request request object. |
| 143 | * |
| 144 | * @return mixed |
| 145 | */ |
| 146 | public function update_settings( WP_REST_Request $request ) { |
| 147 | $current_settings = get_option( 'generateblocks', array() ); |
| 148 | $new_settings = $request->get_param( 'settings' ); |
| 149 | |
| 150 | foreach ( $new_settings as $name => $value ) { |
| 151 | // Skip if the option hasn't changed. |
| 152 | if ( isset( $current_settings[ $name ] ) && $current_settings[ $name ] === $new_settings[ $name ] ) { |
| 153 | unset( $new_settings[ $name ] ); |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | // Only save options that we know about. |
| 158 | if ( ! array_key_exists( $name, generateblocks_get_option_defaults() ) ) { |
| 159 | unset( $new_settings[ $name ] ); |
| 160 | continue; |
| 161 | } |
| 162 | |
| 163 | // Sanitize our value. |
| 164 | $new_settings[ $name ] = $this->sanitize_value( $name, $value ); |
| 165 | } |
| 166 | |
| 167 | if ( empty( $new_settings ) ) { |
| 168 | return $this->success( __( 'No changes found.', 'generateblocks' ) ); |
| 169 | } |
| 170 | |
| 171 | if ( is_array( $new_settings ) ) { |
| 172 | update_option( 'generateblocks', array_merge( $current_settings, $new_settings ) ); |
| 173 | } |
| 174 | |
| 175 | return $this->success( __( 'Settings saved.', 'generateblocks' ) ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Regenerate CSS Files. |
| 180 | * |
| 181 | * @param WP_REST_Request $request request object. |
| 182 | * |
| 183 | * @return mixed |
| 184 | */ |
| 185 | public function regenerate_css_files( WP_REST_Request $request ) { |
| 186 | update_option( 'generateblocks_dynamic_css_posts', array() ); |
| 187 | |
| 188 | return $this->success( __( 'CSS files regenerated.', 'generateblocks' ) ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Mark an onboard as "viewed" by the user. |
| 193 | * |
| 194 | * @param WP_REST_Request $request request object. |
| 195 | * |
| 196 | * @return WP_REST_Response The response. |
| 197 | */ |
| 198 | public function onboarding( WP_REST_Request $request ) { |
| 199 | $user_id = get_current_user_id(); |
| 200 | $onboard = get_user_meta( $user_id, self::ONBOARDING_META_KEY, true ); |
| 201 | $key = $request->get_param( 'key' ); |
| 202 | |
| 203 | if ( ! $onboard ) { |
| 204 | $onboard = array(); |
| 205 | } |
| 206 | |
| 207 | $onboard[ $key ] = true; |
| 208 | |
| 209 | update_user_meta( get_current_user_id(), self::ONBOARDING_META_KEY, $onboard ); |
| 210 | |
| 211 | return new WP_REST_Response( array( 'success' => true ), 200 ); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Get onboarding edit permission. |
| 216 | * |
| 217 | * @return bool |
| 218 | */ |
| 219 | public function onboarding_permission() { |
| 220 | return current_user_can( 'edit_posts' ); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Success rest. |
| 225 | * |
| 226 | * @param mixed $response response data. |
| 227 | * @return mixed |
| 228 | */ |
| 229 | public function success( $response ) { |
| 230 | return new WP_REST_Response( |
| 231 | array( |
| 232 | 'success' => true, |
| 233 | 'response' => $response, |
| 234 | ), |
| 235 | 200 |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Error rest. |
| 241 | * |
| 242 | * @param mixed $code error code. |
| 243 | * @param mixed $response response data. |
| 244 | * @return mixed |
| 245 | */ |
| 246 | public function error( $code, $response ) { |
| 247 | return new WP_REST_Response( |
| 248 | array( |
| 249 | 'error' => true, |
| 250 | 'success' => false, |
| 251 | 'error_code' => $code, |
| 252 | 'response' => $response, |
| 253 | ), |
| 254 | 401 |
| 255 | ); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | GenerateBlocks_Rest::get_instance(); |
| 260 |