| 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 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Registers the ConvertKit Block Category. |
| 39 |
* |
| 40 |
* @since 1.9.6 |
| 41 |
* |
| 42 |
* @param array $categories Block Categories. |
| 43 |
* @param WP_Post $post WordPress Post. |
| 44 |
* @return array Block Categories |
| 45 |
*/ |
| 46 |
public function add_block_categories( $categories, $post ) { |
| 47 |
|
| 48 |
// Define block categories. |
| 49 |
$categories = array_merge( |
| 50 |
$categories, |
| 51 |
array( |
| 52 |
array( |
| 53 |
'slug' => 'convertkit', |
| 54 |
'title' => 'ConvertKit', |
| 55 |
), |
| 56 |
) |
| 57 |
); |
| 58 |
|
| 59 |
/** |
| 60 |
* Adds block categories to the default Gutenberg Block Categories |
| 61 |
* |
| 62 |
* @since 1.9.6 |
| 63 |
* |
| 64 |
* @param array $categories Block Categories |
| 65 |
* @param WP_Post $post WordPress Post |
| 66 |
*/ |
| 67 |
$categories = apply_filters( 'convertkit_admin_gutenberg_add_block_categories', $categories, $post ); |
| 68 |
|
| 69 |
// Return filtered results. |
| 70 |
return $categories; |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Registers Blocks, so that they can be used in the Gutenberg Editor |
| 76 |
* |
| 77 |
* @since 1.9.6 |
| 78 |
*/ |
| 79 |
public function add_blocks() { |
| 80 |
|
| 81 |
// Bail if Gutenberg isn't available. |
| 82 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
// Get blocks. |
| 87 |
$blocks = convertkit_get_blocks(); |
| 88 |
|
| 89 |
// Bail if no blocks are available. |
| 90 |
if ( ! is_array( $blocks ) || ! count( $blocks ) ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// Get registered blocks. |
| 95 |
$registered_blocks = array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() ); |
| 96 |
|
| 97 |
// Iterate through blocks, registering them. |
| 98 |
foreach ( $blocks as $block => $properties ) { |
| 99 |
|
| 100 |
// Skip if this block has already been registered. |
| 101 |
if ( is_array( $registered_blocks ) && in_array( 'convertkit/' . $block, $registered_blocks, true ) ) { |
| 102 |
continue; |
| 103 |
} |
| 104 |
|
| 105 |
// Register block. |
| 106 |
register_block_type( |
| 107 |
CONVERTKIT_PLUGIN_PATH . '/includes/blocks/' . $block, |
| 108 |
array( |
| 109 |
'attributes' => $properties['attributes'], |
| 110 |
'editor_script' => 'convertkit-gutenberg', |
| 111 |
'render_callback' => array( |
| 112 |
$properties['render_callback'][0], |
| 113 |
$properties['render_callback'][1], |
| 114 |
), |
| 115 |
) |
| 116 |
); |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
// Enqueue block scripts and styles in the editor view. |
| 121 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 122 |
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_styles' ) ); |
| 123 |
|
| 124 |
// Enqueue block scripts and styles in the editor and frontend views. |
| 125 |
add_action( 'enqueue_block_assets', array( $this, 'enqueue_scripts_editor_and_frontend' ) ); |
| 126 |
add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles_editor_and_frontend' ) ); |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Enqueues scripts for Gutenberg blocks in the editor view. |
| 132 |
* |
| 133 |
* @since 1.9.6 |
| 134 |
*/ |
| 135 |
public function enqueue_scripts() { |
| 136 |
|
| 137 |
// Bail if request isn't for the Admin or a Frontend Editor. |
| 138 |
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
// Get settings. |
| 143 |
$settings = new ConvertKit_Settings(); |
| 144 |
|
| 145 |
// Get blocks and block toolbar buttons. |
| 146 |
$blocks = convertkit_get_blocks(); |
| 147 |
$block_formatters = convertkit_get_block_formatters(); |
| 148 |
|
| 149 |
// Enqueue Gutenberg Javascript, and set the blocks data. |
| 150 |
wp_enqueue_script( 'convertkit-gutenberg', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true ); |
| 151 |
wp_localize_script( 'convertkit-gutenberg', 'convertkit_blocks', $blocks ); |
| 152 |
wp_localize_script( |
| 153 |
'convertkit-gutenberg', |
| 154 |
'convertkit_gutenberg', |
| 155 |
array( |
| 156 |
'get_blocks_nonce' => wp_create_nonce( 'convertkit_get_blocks' ), |
| 157 |
) |
| 158 |
); |
| 159 |
|
| 160 |
// Enqueue Gutenberg Block Toolbar Javascript, and set the block toolbar buttons data. |
| 161 |
wp_enqueue_script( 'convertkit-gutenberg-block-formatters', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg-block-formatters.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true ); |
| 162 |
wp_localize_script( 'convertkit-gutenberg-block-formatters', 'convertkit_block_formatters', $block_formatters ); |
| 163 |
|
| 164 |
/** |
| 165 |
* Enqueue any additional scripts for Gutenberg blocks that have been registered. |
| 166 |
* |
| 167 |
* @since 1.9.6.5 |
| 168 |
* |
| 169 |
* @param array $blocks ConvertKit Blocks. |
| 170 |
* @param array $block_formatters ConvertKit Block Formatters. |
| 171 |
*/ |
| 172 |
do_action( 'convertkit_gutenberg_enqueue_scripts', $blocks, $block_formatters ); |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Enqueues styles for Gutenberg blocks in the editor view. |
| 178 |
* |
| 179 |
* Use wp_enqueue_style() hooked to the enqueue_block_assets hook for frontend styles: |
| 180 |
* https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/applying-styles-with-stylesheets/ |
| 181 |
* |
| 182 |
* @since 1.9.6.9 |
| 183 |
*/ |
| 184 |
public function enqueue_styles() { |
| 185 |
|
| 186 |
// Bail if request isn't for the Admin or a Frontend Editor. |
| 187 |
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Enqueue styles for Gutenberg blocks that have been registered. |
| 193 |
* |
| 194 |
* @since 1.9.6.9 |
| 195 |
*/ |
| 196 |
do_action( 'convertkit_gutenberg_enqueue_styles' ); |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Enqueues scripts for Gutenberg blocks in both the editor and frontend view, |
| 202 |
* if the Plugin's Disable JavaScript option is not enabled. |
| 203 |
* |
| 204 |
* @since 1.9.7.6 |
| 205 |
*/ |
| 206 |
public function enqueue_scripts_editor_and_frontend() { |
| 207 |
|
| 208 |
// Don't load scripts if the Disable JS option is on. |
| 209 |
$settings = new ConvertKit_Settings(); |
| 210 |
if ( $settings->scripts_disabled() ) { |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Enqueues scripts for Gutenberg blocks in both the editor and frontend view, |
| 216 |
* if the Plugin's Disable JavaScript option is not enabled. |
| 217 |
* |
| 218 |
* @since 1.9.7.6 |
| 219 |
*/ |
| 220 |
do_action( 'convertkit_gutenberg_enqueue_scripts_editor_and_frontend' ); |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Enqueues styles for Gutenberg blocks in both the editor and frontend view, |
| 226 |
* if the Plugin's Disable CSS option is not enabled. |
| 227 |
* |
| 228 |
* @since 1.9.7.6 |
| 229 |
*/ |
| 230 |
public function enqueue_styles_editor_and_frontend() { |
| 231 |
|
| 232 |
// Don't load styles if the Disable CSS option is on. |
| 233 |
$settings = new ConvertKit_Settings(); |
| 234 |
if ( $settings->css_disabled() ) { |
| 235 |
return; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Enqueues styles for Gutenberg blocks in both the editor and frontend view, |
| 240 |
* if the Plugin's Disable CSS option is not enabled. |
| 241 |
* |
| 242 |
* @since 1.9.7.6 |
| 243 |
*/ |
| 244 |
do_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend' ); |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
} |
| 249 |
|