PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.2.0
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.2.0
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-wp-convertkit.php

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

431 lines 10.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Class ConvertKit
11 *
12 * @package ConvertKit
13 * @author ConvertKit
14 */
15 class WP_ConvertKit {
16
17 /**
18 * Holds the class object.
19 *
20 * @since 1.9.6
21 *
22 * @var object
23 */
24 public static $instance;
25
26 /**
27 * Holds singleton initialized classes that include
28 * action and filter hooks.
29 *
30 * @since 1.9.6
31 *
32 * @var array
33 */
34 private $classes = array();
35
36 /**
37 * Constructor. Acts as a bootstrap to load the rest of the plugin
38 *
39 * @since 1.9.6
40 */
41 public function __construct() {
42
43 // Initialize class(es) to register hooks.
44 $this->initialize_admin();
45 $this->initialize_admin_or_frontend_editor();
46 $this->initialize_cli_cron();
47 $this->initialize_frontend();
48 $this->initialize_global();
49
50 // Load language files.
51 add_action( 'init', array( $this, 'load_language_files' ) );
52
53 }
54
55 /**
56 * Initialize classes for the WordPress Administration interface
57 *
58 * @since 1.9.6
59 */
60 private function initialize_admin() {
61
62 // Bail if this request isn't for the WordPress Administration interface.
63 if ( ! is_admin() ) {
64 return;
65 }
66
67 $this->classes['admin_bulk_edit'] = new ConvertKit_Admin_Bulk_Edit();
68 $this->classes['admin_category'] = new ConvertKit_Admin_Category();
69 $this->classes['admin_notices'] = new ConvertKit_Admin_Notices();
70 $this->classes['admin_post'] = new ConvertKit_Admin_Post();
71 $this->classes['admin_quick_edit'] = new ConvertKit_Admin_Quick_Edit();
72 $this->classes['admin_refresh_resources'] = new ConvertKit_Admin_Refresh_Resources();
73 $this->classes['admin_restrict_content'] = new ConvertKit_Admin_Restrict_Content();
74 $this->classes['admin_settings'] = new ConvertKit_Admin_Settings();
75 $this->classes['admin_setup_wizard_plugin'] = new ConvertKit_Admin_Setup_Wizard_Plugin();
76 $this->classes['admin_setup_wizard_restrict_content'] = new ConvertKit_Admin_Setup_Wizard_Restrict_Content();
77 $this->classes['admin_tinymce'] = new ConvertKit_Admin_TinyMCE();
78 $this->classes['admin_user'] = new ConvertKit_Admin_User();
79
80 /**
81 * Initialize integration classes for the WordPress Administration interface.
82 *
83 * @since 1.9.6
84 */
85 do_action( 'convertkit_initialize_admin' );
86
87 }
88
89 /**
90 * Initialize classes for the WordPress Administration interface or a frontend Page Builder
91 *
92 * @since 1.9.6
93 */
94 private function initialize_admin_or_frontend_editor() {
95
96 // Bail if this request isn't for the WordPress Administration interface and isn't for a frontend Page Builder.
97 if ( ! $this->is_admin_or_frontend_editor() ) {
98 return;
99 }
100
101 /**
102 * Initialize integration classes for the WordPress Administration interface or a frontend Page Builder.
103 *
104 * @since 1.9.6
105 */
106 do_action( 'convertkit_initialize_admin_or_frontend_editor' );
107
108 }
109
110 /**
111 * Initialize classes for WP-CLI and WP-Cron
112 *
113 * @since 2.5.2
114 */
115 private function initialize_cli_cron() {
116
117 // Bail if this isn't a CLI or CRON request.
118 if ( ! $this->is_cli() && ! $this->is_cron() ) {
119 return;
120 }
121
122 /**
123 * Initialize integration classes for WP-CLI and WP-Cron.
124 *
125 * @since 1.9.6
126 */
127 do_action( 'convertkit_initialize_cli_cron' );
128
129 }
130
131 /**
132 * Initialize classes for the frontend web site
133 *
134 * @since 1.9.6
135 */
136 private function initialize_frontend() {
137
138 // Bail if this request isn't for the frontend web site.
139 if ( is_admin() ) {
140 return;
141 }
142
143 $this->classes['output'] = new ConvertKit_Output();
144 $this->classes['output_restrict_content'] = new ConvertKit_Output_Restrict_Content();
145
146 /**
147 * Initialize integration classes for the frontend web site.
148 *
149 * @since 1.9.6
150 */
151 do_action( 'convertkit_initialize_frontend' );
152
153 }
154
155 /**
156 * Initialize classes required globally, across the WordPress Administration, CLI, Cron and Frontend
157 * web site.
158 *
159 * @since 1.9.6
160 */
161 private function initialize_global() {
162
163 $this->classes['ajax'] = new ConvertKit_AJAX();
164 $this->classes['blocks_convertkit_broadcasts'] = new ConvertKit_Block_Broadcasts();
165 $this->classes['blocks_convertkit_content'] = new ConvertKit_Block_Content();
166 $this->classes['blocks_convertkit_form_trigger'] = new ConvertKit_Block_Form_Trigger();
167 $this->classes['blocks_convertkit_form'] = new ConvertKit_Block_Form();
168 $this->classes['blocks_convertkit_product'] = new ConvertKit_Block_Product();
169 $this->classes['block_formatter_form_link'] = new ConvertKit_Block_Formatter_Form_Link();
170 $this->classes['block_formatter_product_link'] = new ConvertKit_Block_Formatter_Product_Link();
171 $this->classes['elementor'] = new ConvertKit_Elementor();
172 $this->classes['gutenberg'] = new ConvertKit_Gutenberg();
173 $this->classes['review_request'] = new ConvertKit_Review_Request( 'ConvertKit', 'convertkit', CONVERTKIT_PLUGIN_PATH );
174 $this->classes['preview_output'] = new ConvertKit_Preview_Output();
175 $this->classes['setup'] = new ConvertKit_Setup();
176 $this->classes['shortcodes'] = new ConvertKit_Shortcodes();
177 $this->classes['widgets'] = new ConvertKit_Widgets();
178
179 // Run the setup's update process on WordPress' init hook.
180 // Doing this sooner may result in errors with WordPress functions that are not yet
181 // available to the update routine.
182 add_action( 'init', array( $this, 'update' ) );
183
184 /**
185 * Initialize integration classes for the frontend web site.
186 *
187 * @since 1.9.6
188 */
189 do_action( 'convertkit_initialize_global' );
190
191 }
192
193 /**
194 * Runs the Plugin's update routine, which checks if
195 * the Plugin has just been updated to a newer version,
196 * and if so runs any specific processes that might be needed.
197 *
198 * @since 1.9.7.4
199 */
200 public function update() {
201
202 $this->get_class( 'setup' )->update();
203
204 }
205
206 /**
207 * Improved version of WordPress' is_admin(), which includes whether we're
208 * editing on the frontend using a Page Builder.
209 *
210 * @since 1.9.6
211 *
212 * @return bool Is Admin or Frontend Editor Request
213 */
214 public function is_admin_or_frontend_editor() {
215
216 // If we're in the wp-admin, return true.
217 if ( is_admin() ) {
218 return true;
219 }
220
221 // Pro.
222 if ( array_key_exists( 'REQUEST_URI', $_SERVER ) ) {
223 if ( strpos( sanitize_text_field( $_SERVER['REQUEST_URI'] ), '/pro/' ) !== false ) {
224 return true;
225 }
226 if ( strpos( sanitize_text_field( $_SERVER['REQUEST_URI'] ), '/x/' ) !== false ) {
227 return true;
228 }
229 if ( strpos( sanitize_text_field( $_SERVER['REQUEST_URI'] ), 'cornerstone-endpoint' ) !== false ) {
230 return true;
231 }
232 }
233
234 // If the request global exists, check for specific request keys which tell us
235 // that we're using a frontend editor.
236 // Avada Live.
237 // phpcs:disable WordPress.Security.NonceVerification.Recommended
238 if ( array_key_exists( 'fb-edit', $_REQUEST ) ) {
239 return true;
240 }
241
242 // Beaver Builder.
243 if ( array_key_exists( 'fl_builder', $_REQUEST ) ) {
244 return true;
245 }
246
247 // Brizy.
248 if ( array_key_exists( 'brizy-edit', $_REQUEST ) ) {
249 return true;
250 }
251
252 // Cornerstone (AJAX).
253 if ( array_key_exists( '_cs_nonce', $_REQUEST ) ) {
254 return true;
255 }
256
257 // Divi.
258 if ( array_key_exists( 'et_fb', $_REQUEST ) ) {
259 return true;
260 }
261
262 // Elementor.
263 if ( array_key_exists( 'action', $_REQUEST ) && sanitize_text_field( $_REQUEST['action'] ) === 'elementor' ) {
264 return true;
265 }
266
267 // Kallyas.
268 if ( array_key_exists( 'zn_pb_edit', $_REQUEST ) ) {
269 return true;
270 }
271
272 // Oxygen.
273 if ( array_key_exists( 'ct_builder', $_REQUEST ) ) {
274 return true;
275 }
276
277 // Thrive Architect.
278 if ( array_key_exists( 'tve', $_REQUEST ) ) {
279 return true;
280 }
281
282 // Visual Composer.
283 if ( array_key_exists( 'vcv-editable', $_REQUEST ) ) {
284 return true;
285 }
286
287 // WPBakery Page Builder.
288 if ( array_key_exists( 'vc_editable', $_REQUEST ) ) {
289 return true;
290 }
291
292 // Zion Builder.
293 if ( array_key_exists( 'action', $_REQUEST ) && sanitize_text_field( $_REQUEST['action'] ) === 'zion_builder_active' ) {
294 return true;
295 }
296
297 // Assume we're not in the Administration interface.
298 $is_admin_or_frontend_editor = false;
299
300 /**
301 * Filters whether the current request is a WordPress Administration / Frontend Editor request or not.
302 *
303 * Page Builders can set this to true to allow ConvertKit to load its administration functionality.
304 *
305 * @since 1.9.6
306 *
307 * @param bool $is_admin_or_frontend_editor Is WordPress Administration / Frontend Editor request.
308 */
309 $is_admin_or_frontend_editor = apply_filters( 'convertkit_is_admin_or_frontend_editor', $is_admin_or_frontend_editor );
310
311 // phpcs:enable
312
313 // Return filtered result.
314 return $is_admin_or_frontend_editor;
315
316 }
317
318 /**
319 * Detects if the request is through the WP-CLI
320 *
321 * @since 1.9.6
322 *
323 * @return bool Is WP-CLI Request
324 */
325 public function is_cli() {
326
327 if ( ! defined( 'WP_CLI' ) ) {
328 return false;
329 }
330 if ( ! WP_CLI ) {
331 return false;
332 }
333
334 return true;
335
336 }
337
338 /**
339 * Detects if the request is through the WP CRON
340 *
341 * @since 1.9.6
342 *
343 * @return bool Is WP CRON Request
344 */
345 public function is_cron() {
346
347 if ( ! defined( 'DOING_CRON' ) ) {
348 return false;
349 }
350 if ( ! DOING_CRON ) {
351 return false;
352 }
353
354 return true;
355
356 }
357
358 /**
359 * Loads plugin textdomain
360 *
361 * @since 1.0.0
362 */
363 public function load_language_files() {
364
365 load_plugin_textdomain( 'convertkit', false, basename( dirname( CONVERTKIT_PLUGIN_FILE ) ) . '/languages/' ); // @phpstan-ignore-line.
366
367 }
368
369 /**
370 * Returns the given class
371 *
372 * @since 1.9.6
373 *
374 * @param string $name Class Name.
375 * @return object Class Object
376 */
377 public function get_class( $name ) {
378
379 // If the class hasn't been loaded, throw a WordPress die screen
380 // to avoid a PHP fatal error.
381 if ( ! isset( $this->classes[ $name ] ) ) {
382 // Define the error.
383 $error = new WP_Error(
384 'convertkit_get_class',
385 sprintf(
386 /* translators: %1$s: PHP class name */
387 __( 'ConvertKit Error: Could not load Plugin class <strong>%1$s</strong>', 'convertkit' ),
388 $name
389 )
390 );
391
392 // Depending on the request, return or display an error.
393 // Admin UI.
394 if ( is_admin() ) {
395 wp_die(
396 esc_attr( $error->get_error_message() ),
397 esc_html__( 'ConvertKit Error', 'convertkit' ),
398 array(
399 'back_link' => true,
400 )
401 );
402 }
403
404 // Cron / CLI.
405 return $error;
406 }
407
408 // Return the class object.
409 return $this->classes[ $name ];
410
411 }
412
413 /**
414 * Returns the singleton instance of the class.
415 *
416 * @since 1.1.6
417 *
418 * @return object Class.
419 */
420 public static function get_instance() {
421
422 if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) { // @phpstan-ignore-line.
423 self::$instance = new self();
424 }
425
426 return self::$instance;
427
428 }
429
430 }
431