class-do-css.php
5 years ago
class-enqueue-css.php
5 years ago
class-plugin-update.php
5 years ago
class-render-blocks.php
5 years ago
class-rest.php
5 years ago
class-settings.php
5 years ago
dashboard.php
5 years ago
defaults.php
5 years ago
functions.php
5 years ago
general.php
5 years ago
generate-css.php
5 years ago
class-rest.php
210 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 | 'css_print_method' => 'sanitize_text_field', |
| 108 | 'sync_responsive_previews' => 'rest_sanitize_boolean', |
| 109 | ) |
| 110 | ); |
| 111 | |
| 112 | $callback = $callbacks[ $name ]; |
| 113 | |
| 114 | if ( ! is_callable( $callback ) ) { |
| 115 | return sanitize_text_field( $value ); |
| 116 | } |
| 117 | |
| 118 | return $callback( $value ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Update Settings. |
| 123 | * |
| 124 | * @param WP_REST_Request $request request object. |
| 125 | * |
| 126 | * @return mixed |
| 127 | */ |
| 128 | public function update_settings( WP_REST_Request $request ) { |
| 129 | $current_settings = get_option( 'generateblocks', array() ); |
| 130 | $new_settings = $request->get_param( 'settings' ); |
| 131 | |
| 132 | foreach ( $new_settings as $name => $value ) { |
| 133 | // Skip if the option hasn't changed. |
| 134 | if ( $current_settings[ $name ] === $new_settings[ $name ] ) { |
| 135 | unset( $new_settings[ $name ] ); |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | // Only save options that we know about. |
| 140 | if ( ! array_key_exists( $name, generateblocks_get_option_defaults() ) ) { |
| 141 | unset( $new_settings[ $name ] ); |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | // Sanitize our value. |
| 146 | $new_settings[ $name ] = $this->sanitize_value( $name, $value ); |
| 147 | } |
| 148 | |
| 149 | if ( empty( $new_settings ) ) { |
| 150 | return $this->success( __( 'No changes found.', 'generateblocks' ) ); |
| 151 | } |
| 152 | |
| 153 | if ( is_array( $new_settings ) ) { |
| 154 | update_option( 'generateblocks', array_merge( $current_settings, $new_settings ) ); |
| 155 | } |
| 156 | |
| 157 | return $this->success( __( 'Settings saved.', 'generateblocks' ) ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Regenerate CSS Files. |
| 162 | * |
| 163 | * @param WP_REST_Request $request request object. |
| 164 | * |
| 165 | * @return mixed |
| 166 | */ |
| 167 | public function regenerate_css_files( WP_REST_Request $request ) { |
| 168 | update_option( 'generateblocks_dynamic_css_posts', array() ); |
| 169 | |
| 170 | return $this->success( __( 'CSS files regenerated.', 'generateblocks' ) ); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Success rest. |
| 175 | * |
| 176 | * @param mixed $response response data. |
| 177 | * @return mixed |
| 178 | */ |
| 179 | public function success( $response ) { |
| 180 | return new WP_REST_Response( |
| 181 | array( |
| 182 | 'success' => true, |
| 183 | 'response' => $response, |
| 184 | ), |
| 185 | 200 |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Error rest. |
| 191 | * |
| 192 | * @param mixed $code error code. |
| 193 | * @param mixed $response response data. |
| 194 | * @return mixed |
| 195 | */ |
| 196 | public function error( $code, $response ) { |
| 197 | return new WP_REST_Response( |
| 198 | array( |
| 199 | 'error' => true, |
| 200 | 'success' => false, |
| 201 | 'error_code' => $code, |
| 202 | 'response' => $response, |
| 203 | ), |
| 204 | 401 |
| 205 | ); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | GenerateBlocks_Rest::get_instance(); |
| 210 |