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

235 lines 6.0 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.
145 $blocks = convertkit_get_blocks();
146
147 // Enqueue Gutenberg Javascript, and set the blocks data.
148 wp_enqueue_script( 'convertkit-gutenberg', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
149 wp_localize_script( 'convertkit-gutenberg', 'convertkit_blocks', $blocks );
150
151 /**
152 * Enqueue any additional scripts for Gutenberg blocks that have been registered.
153 *
154 * @since 1.9.6.5
155 *
156 * @param array $blocks ConvertKit Blocks.
157 */
158 do_action( 'convertkit_gutenberg_enqueue_scripts', $blocks );
159
160 }
161
162 /**
163 * Enqueues styles for Gutenberg blocks in the editor view.
164 *
165 * Use wp_enqueue_style() hooked to the enqueue_block_assets hook for frontend styles:
166 * https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/applying-styles-with-stylesheets/
167 *
168 * @since 1.9.6.9
169 */
170 public function enqueue_styles() {
171
172 // Bail if request isn't for the Admin or a Frontend Editor.
173 if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
174 return;
175 }
176
177 /**
178 * Enqueue styles for Gutenberg blocks that have been registered.
179 *
180 * @since 1.9.6.9
181 */
182 do_action( 'convertkit_gutenberg_enqueue_styles' );
183
184 }
185
186 /**
187 * Enqueues scripts for Gutenberg blocks in both the editor and frontend view,
188 * if the Plugin's Disable JavaScript option is not enabled.
189 *
190 * @since 1.9.7.6
191 */
192 public function enqueue_scripts_editor_and_frontend() {
193
194 // Don't load scripts if the Disable JS option is on.
195 $settings = new ConvertKit_Settings();
196 if ( $settings->scripts_disabled() ) {
197 return;
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 do_action( 'convertkit_gutenberg_enqueue_scripts_editor_and_frontend' );
207
208 }
209
210 /**
211 * Enqueues styles for Gutenberg blocks in both the editor and frontend view,
212 * if the Plugin's Disable CSS option is not enabled.
213 *
214 * @since 1.9.7.6
215 */
216 public function enqueue_styles_editor_and_frontend() {
217
218 // Don't load styles if the Disable CSS option is on.
219 $settings = new ConvertKit_Settings();
220 if ( $settings->css_disabled() ) {
221 return;
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 do_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend' );
231
232 }
233
234 }
235