PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.5.7
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.5.7
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.5.7, at includes/class-convertkit-gutenberg.php

253 lines 6.9 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_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_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 $pre_publish_actions = convertkit_get_pre_publish_actions();
149
150 // Enqueue Gutenberg Javascript, and set the blocks data.
151 wp_enqueue_script( 'convertkit-gutenberg', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
152 wp_localize_script( 'convertkit-gutenberg', 'convertkit_blocks', $blocks );
153 if ( count( $pre_publish_actions ) ) {
154 wp_localize_script( 'convertkit-gutenberg', 'convertkit_pre_publish_actions', $pre_publish_actions );
155 }
156 wp_localize_script(
157 'convertkit-gutenberg',
158 'convertkit_gutenberg',
159 array(
160 'get_blocks_nonce' => wp_create_nonce( 'convertkit_get_blocks' ),
161 )
162 );
163
164 // Enqueue Gutenberg Block Toolbar Javascript, and set the block toolbar buttons data.
165 wp_enqueue_script( 'convertkit-gutenberg-block-formatters', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg-block-formatters.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
166 wp_localize_script( 'convertkit-gutenberg-block-formatters', 'convertkit_block_formatters', $block_formatters );
167
168 /**
169 * Enqueue any additional scripts for Gutenberg blocks that have been registered.
170 *
171 * @since 1.9.6.5
172 *
173 * @param array $blocks ConvertKit Blocks.
174 * @param array $block_formatters ConvertKit Block Formatters.
175 */
176 do_action( 'convertkit_gutenberg_enqueue_scripts', $blocks, $block_formatters );
177
178 }
179
180 /**
181 * Enqueues styles for Gutenberg blocks in the editor view.
182 *
183 * Use wp_enqueue_style() hooked to the enqueue_block_assets hook for frontend styles:
184 * https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/applying-styles-with-stylesheets/
185 *
186 * @since 1.9.6.9
187 */
188 public function enqueue_styles() {
189
190 // Bail if request isn't for the Admin.
191 if ( ! is_admin() ) {
192 return;
193 }
194
195 /**
196 * Enqueue styles for Gutenberg blocks that have been registered.
197 *
198 * @since 1.9.6.9
199 */
200 do_action( 'convertkit_gutenberg_enqueue_styles' );
201
202 }
203
204 /**
205 * Enqueues scripts for Gutenberg blocks in both the editor and frontend view,
206 * if the Plugin's Disable JavaScript option is not enabled.
207 *
208 * @since 1.9.7.6
209 */
210 public function enqueue_scripts_editor_and_frontend() {
211
212 // Don't load scripts if the Disable JS option is on.
213 $settings = new ConvertKit_Settings();
214 if ( $settings->scripts_disabled() ) {
215 return;
216 }
217
218 /**
219 * Enqueues scripts for Gutenberg blocks in both the editor and frontend view,
220 * if the Plugin's Disable JavaScript option is not enabled.
221 *
222 * @since 1.9.7.6
223 */
224 do_action( 'convertkit_gutenberg_enqueue_scripts_editor_and_frontend' );
225
226 }
227
228 /**
229 * Enqueues styles for Gutenberg blocks in both the editor and frontend view,
230 * if the Plugin's Disable CSS option is not enabled.
231 *
232 * @since 1.9.7.6
233 */
234 public function enqueue_styles_editor_and_frontend() {
235
236 // Don't load styles if the Disable CSS option is on.
237 $settings = new ConvertKit_Settings();
238 if ( $settings->css_disabled() ) {
239 return;
240 }
241
242 /**
243 * Enqueues styles for Gutenberg blocks in both the editor and frontend view,
244 * if the Plugin's Disable CSS option is not enabled.
245 *
246 * @since 1.9.7.6
247 */
248 do_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend' );
249
250 }
251
252 }
253