| @@ -31,11 +31,68 @@ | ||
| 31 | 31 | |
| 32 | 32 | // Register Gutenberg Blocks. |
| 33 | 33 | add_action( 'init', array( $this, 'add_blocks' ) ); |
| 34 | 34 | |
| 35 | + // Register REST API routes. | |
| 36 | + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); | |
| 37 | + | |
| 35 | 38 | } |
| 36 | 39 | |
| 37 | 40 | /** |
| 41 | + * Register REST API routes. | |
| 42 | + * | |
| 43 | + * @since 3.1.0 | |
| 44 | + */ | |
| 45 | + public function register_routes() { | |
| 46 | + | |
| 47 | + // Register route to refresh resources andreturn all blocks registered by the Plugin, | |
| 48 | + // when the user clicks the refresh button in the Gutenberg editor. | |
| 49 | + register_rest_route( | |
| 50 | + 'kit/v1', | |
| 51 | + '/blocks', | |
| 52 | + array( | |
| 53 | + 'methods' => WP_REST_Server::READABLE, | |
| 54 | + | |
| 55 | + // Return blocks. | |
| 56 | + 'callback' => function () { | |
| 57 | + // Refresh Forms. | |
| 58 | + $forms = new ConvertKit_Resource_Forms( 'block_edit' ); | |
| 59 | + $result = $forms->refresh(); | |
| 60 | + if ( is_wp_error( $result ) ) { | |
| 61 | + // Return blocks without refreshing other resources. | |
| 62 | + return rest_ensure_response( convertkit_get_blocks() ); | |
| 63 | + } | |
| 64 | + | |
| 65 | + // Refresh Posts. | |
| 66 | + $posts = new ConvertKit_Resource_Posts( 'block_edit' ); | |
| 67 | + $result = $posts->refresh(); | |
| 68 | + if ( is_wp_error( $result ) ) { | |
| 69 | + // Return blocks without refreshing other resources. | |
| 70 | + return rest_ensure_response( convertkit_get_blocks() ); | |
| 71 | + } | |
| 72 | + | |
| 73 | + // Refresh Products. | |
| 74 | + $products = new ConvertKit_Resource_Products( 'block_edit' ); | |
| 75 | + $result = $products->refresh(); | |
| 76 | + if ( is_wp_error( $result ) ) { | |
| 77 | + // Return blocks without refreshing other resources. | |
| 78 | + return rest_ensure_response( convertkit_get_blocks() ); | |
| 79 | + } | |
| 80 | + | |
| 81 | + // Return blocks, which will now include the refreshed resources. | |
| 82 | + return rest_ensure_response( convertkit_get_blocks() ); | |
| 83 | + }, | |
| 84 | + | |
| 85 | + // Only refresh resources for users who can edit posts. | |
| 86 | + 'permission_callback' => function () { | |
| 87 | + return current_user_can( 'edit_posts' ); | |
| 88 | + }, | |
| 89 | + ) | |
| 90 | + ); | |
| 91 | + | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 38 | 95 | * Registers the ConvertKit Block Category. |
| 39 | 96 | * |
| 40 | 97 | * @since 1.9.6 |
| 41 | 98 | * |
| @@ -50,9 +107,9 @@ | ||
| 50 | 107 | $categories, |
| 51 | 108 | array( |
| 52 | 109 | array( |
| 53 | 110 | 'slug' => 'convertkit', |
| 54 | - 'title' => 'ConvertKit', | |
| 111 | + 'title' => 'Kit', | |
| 55 | 112 | ), |
| 56 | 113 | ) |
| 57 | 114 | ); |
| 58 | 115 | |
| @@ -86,12 +143,15 @@ | ||
| 86 | 143 | // Get blocks. |
| 87 | 144 | $blocks = convertkit_get_blocks(); |
| 88 | 145 | |
| 89 | 146 | // Bail if no blocks are available. |
| 90 | - if ( ! is_array( $blocks ) || ! count( $blocks ) ) { | |
| 147 | + if ( ! count( $blocks ) ) { | |
| 91 | 148 | return; |
| 92 | 149 | } |
| 93 | 150 | |
| 151 | + // Determine the block API version to use for registering blocks. | |
| 152 | + $block_api_version = $this->get_block_api_version(); | |
| 153 | + | |
| 94 | 154 | // Get registered blocks. |
| 95 | 155 | $registered_blocks = array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() ); |
| 96 | 156 | |
| 97 | 157 | // Iterate through blocks, registering them. |
| @@ -97,15 +157,15 @@ | ||
| 97 | 157 | // Iterate through blocks, registering them. |
| 98 | 158 | foreach ( $blocks as $block => $properties ) { |
| 99 | 159 | |
| 100 | 160 | // Skip if this block has already been registered. |
| 101 | - if ( is_array( $registered_blocks ) && in_array( 'convertkit/' . $block, $registered_blocks, true ) ) { | |
| 161 | + if ( in_array( 'convertkit/' . $block, $registered_blocks, true ) ) { | |
| 102 | 162 | continue; |
| 103 | 163 | } |
| 104 | 164 | |
| 105 | 165 | // Register block. |
| 106 | 166 | register_block_type( |
| 107 | - CONVERTKIT_PLUGIN_PATH . '/includes/blocks/' . $block, | |
| 167 | + CONVERTKIT_PLUGIN_PATH . '/includes/blocks/v' . (string) $block_api_version . '/' . $block, | |
| 108 | 168 | array( |
| 109 | 169 | 'attributes' => $properties['attributes'], |
| 110 | 170 | 'editor_script' => 'convertkit-gutenberg', |
| 111 | 171 | 'render_callback' => array( |
| @@ -118,9 +178,9 @@ | ||
| 118 | 178 | } |
| 119 | 179 | |
| 120 | 180 | // Enqueue block scripts and styles in the editor view. |
| 121 | 181 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 122 | - add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_styles' ) ); | |
| 182 | + add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles' ) ); | |
| 123 | 183 | |
| 124 | 184 | // Enqueue block scripts and styles in the editor and frontend views. |
| 125 | 185 | add_action( 'enqueue_block_assets', array( $this, 'enqueue_scripts_editor_and_frontend' ) ); |
| 126 | 186 | add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles_editor_and_frontend' ) ); |
| @@ -127,8 +187,35 @@ | ||
| 127 | 187 | |
| 128 | 188 | } |
| 129 | 189 | |
| 130 | 190 | /** |
| 191 | + * Determines the block API version to use for registering blocks. | |
| 192 | + * | |
| 193 | + * @since 3.2.0 | |
| 194 | + * | |
| 195 | + * @return int Block API version. | |
| 196 | + */ | |
| 197 | + public function get_block_api_version() { | |
| 198 | + | |
| 199 | + // Determine the block API version to use for registering blocks. | |
| 200 | + // WordPress supports Version 3 from WordPress 6.3: | |
| 201 | + // https://developer.wordpress.org/block-editor/reference-guides/block-api/block-api-versions/. | |
| 202 | + $block_api_version = ( version_compare( get_bloginfo( 'version' ), '6.3', '>=' ) ? 3 : 2 ); | |
| 203 | + | |
| 204 | + /** | |
| 205 | + * Determine the block API version to use for registering blocks. | |
| 206 | + * | |
| 207 | + * @since 3.1.4 | |
| 208 | + * | |
| 209 | + * @param int $block_api_version Block API version. | |
| 210 | + * @return int Block API version. | |
| 211 | + */ | |
| 212 | + $block_api_version = apply_filters( 'convertkit_gutenberg_block_api_version', $block_api_version ); | |
| 213 | + | |
| 214 | + return absint( $block_api_version ); | |
| 215 | + | |
| 216 | + } | |
| 217 | + /** | |
| 131 | 218 | * Enqueues scripts for Gutenberg blocks in the editor view. |
| 132 | 219 | * |
| 133 | 220 | * @since 1.9.6 |
| 134 | 221 | */ |
| @@ -142,19 +229,25 @@ | ||
| 142 | 229 | // Get settings. |
| 143 | 230 | $settings = new ConvertKit_Settings(); |
| 144 | 231 | |
| 145 | 232 | // Get blocks and block toolbar buttons. |
| 146 | - $blocks = convertkit_get_blocks(); | |
| 147 | - $block_formatters = convertkit_get_block_formatters(); | |
| 233 | + $blocks = convertkit_get_blocks(); | |
| 234 | + $block_formatters = convertkit_get_block_formatters(); | |
| 235 | + $pre_publish_actions = convertkit_get_pre_publish_actions(); | |
| 148 | 236 | |
| 149 | 237 | // Enqueue Gutenberg Javascript, and set the blocks data. |
| 150 | 238 | wp_enqueue_script( 'convertkit-gutenberg', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true ); |
| 151 | 239 | wp_localize_script( 'convertkit-gutenberg', 'convertkit_blocks', $blocks ); |
| 240 | + if ( count( $pre_publish_actions ) ) { | |
| 241 | + wp_localize_script( 'convertkit-gutenberg', 'convertkit_pre_publish_actions', $pre_publish_actions ); | |
| 242 | + } | |
| 152 | 243 | wp_localize_script( |
| 153 | 244 | 'convertkit-gutenberg', |
| 154 | 245 | 'convertkit_gutenberg', |
| 155 | 246 | array( |
| 156 | - 'get_blocks_nonce' => wp_create_nonce( 'convertkit_get_blocks' ), | |
| 247 | + 'ajaxurl' => rest_url( 'kit/v1/blocks' ), | |
| 248 | + 'block_api_version' => $this->get_block_api_version(), | |
| 249 | + 'get_blocks_nonce' => wp_create_nonce( 'wp_rest' ), | |
| 157 | 250 | ) |
| 158 | 251 | ); |
| 159 | 252 | |
| 160 | 253 | // Enqueue Gutenberg Block Toolbar Javascript, and set the block toolbar buttons data. |
| @@ -182,10 +275,10 @@ | ||
| 182 | 275 | * @since 1.9.6.9 |
| 183 | 276 | */ |
| 184 | 277 | public function enqueue_styles() { |
| 185 | 278 | |
| 186 | - // Bail if request isn't for the Admin or a Frontend Editor. | |
| 187 | - if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { | |
| 279 | + // Bail if request isn't for the Admin. | |
| 280 | + if ( ! is_admin() ) { | |
| 188 | 281 | return; |
| 189 | 282 | } |
| 190 | 283 | |
| 191 | 284 | /** |