PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.2.9
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.2.9
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / includes / class-convertkit-gutenberg.php

class-convertkit-gutenberg.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 2.2.9, at includes/class-convertkit-gutenberg.php

248 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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/' . $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 // Enqueue block scripts and styles in the editor view.
120 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
121 add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_styles' ) );
122
123 // Enqueue block scripts and styles in the editor and frontend views.
124 add_action( 'enqueue_block_assets', array( $this, 'enqueue_scripts_editor_and_frontend' ) );
125 add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles_editor_and_frontend' ) );
126
127 }
128
129 /**
130 * Enqueues scripts for Gutenberg blocks in the editor view.
131 *
132 * @since 1.9.6
133 */
134 public function enqueue_scripts() {
135
136 // Bail if request isn't for the Admin or a Frontend Editor.
137 if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
138 return;
139 }
140
141 // Get settings.
142 $settings = new ConvertKit_Settings();
143
144 // Get blocks and block toolbar buttons.
145 $blocks = convertkit_get_blocks();
146 $block_formatters = convertkit_get_block_formatters();
147
148 // Enqueue Gutenberg Javascript, and set the blocks data.
149 wp_enqueue_script( 'convertkit-gutenberg', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
150 wp_localize_script( 'convertkit-gutenberg', 'convertkit_blocks', $blocks );
151 wp_localize_script(
152 'convertkit-gutenberg',
153 'convertkit_gutenberg',
154 array(
155 'get_blocks_nonce' => wp_create_nonce( 'convertkit_get_blocks' ),
156 )
157 );
158
159 // Enqueue Gutenberg Block Toolbar Javascript, and set the block toolbar buttons data.
160 wp_enqueue_script( 'convertkit-gutenberg-block-formatters', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg-block-formatters.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
161 wp_localize_script( 'convertkit-gutenberg-block-formatters', 'convertkit_block_formatters', $block_formatters );
162
163 /**
164 * Enqueue any additional scripts for Gutenberg blocks that have been registered.
165 *
166 * @since 1.9.6.5
167 *
168 * @param array $blocks ConvertKit Blocks.
169 * @param array $block_formatters ConvertKit Block Formatters.
170 */
171 do_action( 'convertkit_gutenberg_enqueue_scripts', $blocks, $block_formatters );
172
173 }
174
175 /**
176 * Enqueues styles for Gutenberg blocks in the editor view.
177 *
178 * Use wp_enqueue_style() hooked to the enqueue_block_assets hook for frontend styles:
179 * https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/applying-styles-with-stylesheets/
180 *
181 * @since 1.9.6.9
182 */
183 public function enqueue_styles() {
184
185 // Bail if request isn't for the Admin or a Frontend Editor.
186 if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
187 return;
188 }
189
190 /**
191 * Enqueue styles for Gutenberg blocks that have been registered.
192 *
193 * @since 1.9.6.9
194 */
195 do_action( 'convertkit_gutenberg_enqueue_styles' );
196
197 }
198
199 /**
200 * Enqueues scripts for Gutenberg blocks in both the editor and frontend view,
201 * if the Plugin's Disable JavaScript option is not enabled.
202 *
203 * @since 1.9.7.6
204 */
205 public function enqueue_scripts_editor_and_frontend() {
206
207 // Don't load scripts if the Disable JS option is on.
208 $settings = new ConvertKit_Settings();
209 if ( $settings->scripts_disabled() ) {
210 return;
211 }
212
213 /**
214 * Enqueues scripts for Gutenberg blocks in both the editor and frontend view,
215 * if the Plugin's Disable JavaScript option is not enabled.
216 *
217 * @since 1.9.7.6
218 */
219 do_action( 'convertkit_gutenberg_enqueue_scripts_editor_and_frontend' );
220
221 }
222
223 /**
224 * Enqueues styles for Gutenberg blocks in both the editor and frontend view,
225 * if the Plugin's Disable CSS option is not enabled.
226 *
227 * @since 1.9.7.6
228 */
229 public function enqueue_styles_editor_and_frontend() {
230
231 // Don't load styles if the Disable CSS option is on.
232 $settings = new ConvertKit_Settings();
233 if ( $settings->css_disabled() ) {
234 return;
235 }
236
237 /**
238 * Enqueues styles for Gutenberg blocks in both the editor and frontend view,
239 * if the Plugin's Disable CSS option is not enabled.
240 *
241 * @since 1.9.7.6
242 */
243 do_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend' );
244
245 }
246
247 }
248