| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Gutenberg class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Registers blocks defined in the `convertkit_blocks` filter in Gutenberg. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_Gutenberg { |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor |
| 19 |
* |
| 20 |
* @since 1.9.6 |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
|
| 24 |
// Register Gutenberg Block Categories and Blocks. |
| 25 |
if ( get_bloginfo( 'version' ) >= 5.8 ) { |
| 26 |
// Filter changed in 5.8. |
| 27 |
add_filter( 'block_categories_all', array( $this, 'add_block_categories' ), 10, 2 ); |
| 28 |
} else { |
| 29 |
add_filter( 'block_categories', array( $this, 'add_block_categories' ), 10, 2 ); |
| 30 |
} |
| 31 |
|
| 32 |
// Register Gutenberg Blocks. |
| 33 |
add_action( 'init', array( $this, 'add_blocks' ) ); |
| 34 |
|
| 35 |
// Register REST API routes. |
| 36 |
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 37 |
|
| 38 |
} |
| 39 |
|
| 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 |
/** |
| 95 |
* Registers the ConvertKit Block Category. |
| 96 |
* |
| 97 |
* @since 1.9.6 |
| 98 |
* |
| 99 |
* @param array $categories Block Categories. |
| 100 |
* @param WP_Post $post WordPress Post. |
| 101 |
* @return array Block Categories |
| 102 |
*/ |
| 103 |
public function add_block_categories( $categories, $post ) { |
| 104 |
|
| 105 |
// Define block categories. |
| 106 |
$categories = array_merge( |
| 107 |
$categories, |
| 108 |
array( |
| 109 |
array( |
| 110 |
'slug' => 'convertkit', |
| 111 |
'title' => 'Kit', |
| 112 |
), |
| 113 |
) |
| 114 |
); |
| 115 |
|
| 116 |
/** |
| 117 |
* Adds block categories to the default Gutenberg Block Categories |
| 118 |
* |
| 119 |
* @since 1.9.6 |
| 120 |
* |
| 121 |
* @param array $categories Block Categories |
| 122 |
* @param WP_Post $post WordPress Post |
| 123 |
*/ |
| 124 |
$categories = apply_filters( 'convertkit_admin_gutenberg_add_block_categories', $categories, $post ); |
| 125 |
|
| 126 |
// Return filtered results. |
| 127 |
return $categories; |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Registers Blocks, so that they can be used in the Gutenberg Editor |
| 133 |
* |
| 134 |
* @since 1.9.6 |
| 135 |
*/ |
| 136 |
public function add_blocks() { |
| 137 |
|
| 138 |
// Bail if Gutenberg isn't available. |
| 139 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
// Get blocks. |
| 144 |
$blocks = convertkit_get_blocks(); |
| 145 |
|
| 146 |
// Bail if no blocks are available. |
| 147 |
if ( ! count( $blocks ) ) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
// Determine the block API version to use for registering blocks. |
| 152 |
$block_api_version = $this->get_block_api_version(); |
| 153 |
|
| 154 |
// Get registered blocks. |
| 155 |
$registered_blocks = array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() ); |
| 156 |
|
| 157 |
// Iterate through blocks, registering them. |
| 158 |
foreach ( $blocks as $block => $properties ) { |
| 159 |
|
| 160 |
// Skip if this block has already been registered. |
| 161 |
if ( in_array( 'convertkit/' . $block, $registered_blocks, true ) ) { |
| 162 |
continue; |
| 163 |
} |
| 164 |
|
| 165 |
// Register block. |
| 166 |
register_block_type( |
| 167 |
CONVERTKIT_PLUGIN_PATH . '/includes/blocks/v' . (string) $block_api_version . '/' . $block, |
| 168 |
array( |
| 169 |
'attributes' => $properties['attributes'], |
| 170 |
'editor_script' => 'convertkit-gutenberg', |
| 171 |
'render_callback' => array( |
| 172 |
$properties['render_callback'][0], |
| 173 |
$properties['render_callback'][1], |
| 174 |
), |
| 175 |
) |
| 176 |
); |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
// Enqueue block scripts and styles in the editor view. |
| 181 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 182 |
add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles' ) ); |
| 183 |
|
| 184 |
// Enqueue block scripts and styles in the editor and frontend views. |
| 185 |
add_action( 'enqueue_block_assets', array( $this, 'enqueue_scripts_editor_and_frontend' ) ); |
| 186 |
add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles_editor_and_frontend' ) ); |
| 187 |
|
| 188 |
} |
| 189 |
|
| 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 |
/** |
| 218 |
* Enqueues scripts for Gutenberg blocks in the editor view. |
| 219 |
* |
| 220 |
* @since 1.9.6 |
| 221 |
*/ |
| 222 |
public function enqueue_scripts() { |
| 223 |
|
| 224 |
// Bail if request isn't for the Admin or a Frontend Editor. |
| 225 |
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { |
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
// Get settings. |
| 230 |
$settings = new ConvertKit_Settings(); |
| 231 |
|
| 232 |
// Get blocks and block toolbar buttons. |
| 233 |
$blocks = convertkit_get_blocks(); |
| 234 |
$block_formatters = convertkit_get_block_formatters(); |
| 235 |
$pre_publish_actions = convertkit_get_pre_publish_actions(); |
| 236 |
|
| 237 |
// Enqueue Gutenberg Javascript, and set the blocks data. |
| 238 |
wp_enqueue_script( 'convertkit-gutenberg', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true ); |
| 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 |
} |
| 243 |
wp_localize_script( |
| 244 |
'convertkit-gutenberg', |
| 245 |
'convertkit_gutenberg', |
| 246 |
array( |
| 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' ), |
| 250 |
) |
| 251 |
); |
| 252 |
|
| 253 |
// Enqueue Gutenberg Block Toolbar Javascript, and set the block toolbar buttons data. |
| 254 |
wp_enqueue_script( 'convertkit-gutenberg-block-formatters', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg-block-formatters.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true ); |
| 255 |
wp_localize_script( 'convertkit-gutenberg-block-formatters', 'convertkit_block_formatters', $block_formatters ); |
| 256 |
|
| 257 |
/** |
| 258 |
* Enqueue any additional scripts for Gutenberg blocks that have been registered. |
| 259 |
* |
| 260 |
* @since 1.9.6.5 |
| 261 |
* |
| 262 |
* @param array $blocks ConvertKit Blocks. |
| 263 |
* @param array $block_formatters ConvertKit Block Formatters. |
| 264 |
*/ |
| 265 |
do_action( 'convertkit_gutenberg_enqueue_scripts', $blocks, $block_formatters ); |
| 266 |
|
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Enqueues styles for Gutenberg blocks in the editor view. |
| 271 |
* |
| 272 |
* Use wp_enqueue_style() hooked to the enqueue_block_assets hook for frontend styles: |
| 273 |
* https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/applying-styles-with-stylesheets/ |
| 274 |
* |
| 275 |
* @since 1.9.6.9 |
| 276 |
*/ |
| 277 |
public function enqueue_styles() { |
| 278 |
|
| 279 |
// Bail if request isn't for the Admin. |
| 280 |
if ( ! is_admin() ) { |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Enqueue styles for Gutenberg blocks that have been registered. |
| 286 |
* |
| 287 |
* @since 1.9.6.9 |
| 288 |
*/ |
| 289 |
do_action( 'convertkit_gutenberg_enqueue_styles' ); |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Enqueues scripts for Gutenberg blocks in both the editor and frontend view, |
| 295 |
* if the Plugin's Disable JavaScript option is not enabled. |
| 296 |
* |
| 297 |
* @since 1.9.7.6 |
| 298 |
*/ |
| 299 |
public function enqueue_scripts_editor_and_frontend() { |
| 300 |
|
| 301 |
// Don't load scripts if the Disable JS option is on. |
| 302 |
$settings = new ConvertKit_Settings(); |
| 303 |
if ( $settings->scripts_disabled() ) { |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Enqueues scripts for Gutenberg blocks in both the editor and frontend view, |
| 309 |
* if the Plugin's Disable JavaScript option is not enabled. |
| 310 |
* |
| 311 |
* @since 1.9.7.6 |
| 312 |
*/ |
| 313 |
do_action( 'convertkit_gutenberg_enqueue_scripts_editor_and_frontend' ); |
| 314 |
|
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Enqueues styles for Gutenberg blocks in both the editor and frontend view, |
| 319 |
* if the Plugin's Disable CSS option is not enabled. |
| 320 |
* |
| 321 |
* @since 1.9.7.6 |
| 322 |
*/ |
| 323 |
public function enqueue_styles_editor_and_frontend() { |
| 324 |
|
| 325 |
// Don't load styles if the Disable CSS option is on. |
| 326 |
$settings = new ConvertKit_Settings(); |
| 327 |
if ( $settings->css_disabled() ) { |
| 328 |
return; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Enqueues styles for Gutenberg blocks in both the editor and frontend view, |
| 333 |
* if the Plugin's Disable CSS option is not enabled. |
| 334 |
* |
| 335 |
* @since 1.9.7.6 |
| 336 |
*/ |
| 337 |
do_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend' ); |
| 338 |
|
| 339 |
} |
| 340 |
|
| 341 |
} |
| 342 |
|