| @@ -31,11 +31,52 @@ | ||
| 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 return all blocks registered by the Plugin. | |
| 48 | + register_rest_route( | |
| 49 | + 'kit/v1', | |
| 50 | + '/blocks', | |
| 51 | + array( | |
| 52 | + 'methods' => WP_REST_Server::READABLE, | |
| 53 | + | |
| 54 | + // Refresh resources and return blocks. | |
| 55 | + 'callback' => function () { | |
| 56 | + // Refresh resources from the API, to reflect any changes. | |
| 57 | + $forms = new ConvertKit_Resource_Forms( 'block_edit' ); | |
| 58 | + $forms->refresh(); | |
| 59 | + | |
| 60 | + $posts = new ConvertKit_Resource_Posts( 'block_edit' ); | |
| 61 | + $posts->refresh(); | |
| 62 | + | |
| 63 | + $products = new ConvertKit_Resource_Products( 'block_edit' ); | |
| 64 | + $products->refresh(); | |
| 65 | + | |
| 66 | + // Return blocks. | |
| 67 | + return rest_ensure_response( convertkit_get_blocks() ); | |
| 68 | + }, | |
| 69 | + | |
| 70 | + 'permission_callback' => function () { | |
| 71 | + return current_user_can( 'edit_posts' ); | |
| 72 | + }, | |
| 73 | + ) | |
| 74 | + ); | |
| 75 | + | |
| 76 | + } | |
| 77 | + | |
| 78 | + /** | |
| 38 | 79 | * Registers the ConvertKit Block Category. |
| 39 | 80 | * |
| 40 | 81 | * @since 1.9.6 |
| 41 | 82 | * |
| @@ -50,9 +91,9 @@ | ||
| 50 | 91 | $categories, |
| 51 | 92 | array( |
| 52 | 93 | array( |
| 53 | 94 | 'slug' => 'convertkit', |
| 54 | - 'title' => 'ConvertKit', | |
| 95 | + 'title' => 'Kit', | |
| 55 | 96 | ), |
| 56 | 97 | ) |
| 57 | 98 | ); |
| 58 | 99 | |
| @@ -86,9 +127,9 @@ | ||
| 86 | 127 | // Get blocks. |
| 87 | 128 | $blocks = convertkit_get_blocks(); |
| 88 | 129 | |
| 89 | 130 | // Bail if no blocks are available. |
| 90 | - if ( ! is_array( $blocks ) || ! count( $blocks ) ) { | |
| 131 | + if ( ! count( $blocks ) ) { | |
| 91 | 132 | return; |
| 92 | 133 | } |
| 93 | 134 | |
| 94 | 135 | // Get registered blocks. |
| @@ -97,15 +138,15 @@ | ||
| 97 | 138 | // Iterate through blocks, registering them. |
| 98 | 139 | foreach ( $blocks as $block => $properties ) { |
| 99 | 140 | |
| 100 | 141 | // Skip if this block has already been registered. |
| 101 | - if ( is_array( $registered_blocks ) && in_array( 'convertkit/' . $block, $registered_blocks, true ) ) { | |
| 142 | + if ( in_array( 'convertkit/' . $block, $registered_blocks, true ) ) { | |
| 102 | 143 | continue; |
| 103 | 144 | } |
| 104 | 145 | |
| 105 | 146 | // Register block. |
| 106 | 147 | register_block_type( |
| 107 | - 'convertkit/' . $block, | |
| 148 | + CONVERTKIT_PLUGIN_PATH . '/includes/blocks/' . $block, | |
| 108 | 149 | array( |
| 109 | 150 | 'attributes' => $properties['attributes'], |
| 110 | 151 | 'editor_script' => 'convertkit-gutenberg', |
| 111 | 152 | 'render_callback' => array( |
| @@ -113,13 +154,14 @@ | ||
| 113 | 154 | $properties['render_callback'][1], |
| 114 | 155 | ), |
| 115 | 156 | ) |
| 116 | 157 | ); |
| 158 | + | |
| 117 | 159 | } |
| 118 | 160 | |
| 119 | 161 | // Enqueue block scripts and styles in the editor view. |
| 120 | 162 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 121 | - add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_styles' ) ); | |
| 163 | + add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles' ) ); | |
| 122 | 164 | |
| 123 | 165 | // Enqueue block scripts and styles in the editor and frontend views. |
| 124 | 166 | add_action( 'enqueue_block_assets', array( $this, 'enqueue_scripts_editor_and_frontend' ) ); |
| 125 | 167 | add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles_editor_and_frontend' ) ); |
| @@ -141,14 +183,26 @@ | ||
| 141 | 183 | // Get settings. |
| 142 | 184 | $settings = new ConvertKit_Settings(); |
| 143 | 185 | |
| 144 | 186 | // Get blocks and block toolbar buttons. |
| 145 | - $blocks = convertkit_get_blocks(); | |
| 146 | - $block_formatters = convertkit_get_block_formatters(); | |
| 187 | + $blocks = convertkit_get_blocks(); | |
| 188 | + $block_formatters = convertkit_get_block_formatters(); | |
| 189 | + $pre_publish_actions = convertkit_get_pre_publish_actions(); | |
| 147 | 190 | |
| 148 | 191 | // Enqueue Gutenberg Javascript, and set the blocks data. |
| 149 | 192 | wp_enqueue_script( 'convertkit-gutenberg', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true ); |
| 150 | 193 | wp_localize_script( 'convertkit-gutenberg', 'convertkit_blocks', $blocks ); |
| 194 | + if ( count( $pre_publish_actions ) ) { | |
| 195 | + wp_localize_script( 'convertkit-gutenberg', 'convertkit_pre_publish_actions', $pre_publish_actions ); | |
| 196 | + } | |
| 197 | + wp_localize_script( | |
| 198 | + 'convertkit-gutenberg', | |
| 199 | + 'convertkit_gutenberg', | |
| 200 | + array( | |
| 201 | + 'ajaxurl' => rest_url( 'kit/v1/blocks' ), | |
| 202 | + 'get_blocks_nonce' => wp_create_nonce( 'wp_rest' ), | |
| 203 | + ) | |
| 204 | + ); | |
| 151 | 205 | |
| 152 | 206 | // Enqueue Gutenberg Block Toolbar Javascript, and set the block toolbar buttons data. |
| 153 | 207 | wp_enqueue_script( 'convertkit-gutenberg-block-formatters', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg-block-formatters.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true ); |
| 154 | 208 | wp_localize_script( 'convertkit-gutenberg-block-formatters', 'convertkit_block_formatters', $block_formatters ); |
| @@ -174,10 +228,10 @@ | ||
| 174 | 228 | * @since 1.9.6.9 |
| 175 | 229 | */ |
| 176 | 230 | public function enqueue_styles() { |
| 177 | 231 | |
| 178 | - // Bail if request isn't for the Admin or a Frontend Editor. | |
| 179 | - if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { | |
| 232 | + // Bail if request isn't for the Admin. | |
| 233 | + if ( ! is_admin() ) { | |
| 180 | 234 | return; |
| 181 | 235 | } |
| 182 | 236 | |
| 183 | 237 | /** |