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

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

398 lines 9.0 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_category'] = new ConvertKit_Admin_Category();
68 $this->classes['admin_post'] = new ConvertKit_Admin_Post();
69 $this->classes['admin_settings'] = new ConvertKit_Admin_Settings();
70 $this->classes['admin_tinymce'] = new ConvertKit_Admin_TinyMCE();
71 $this->classes['admin_upgrade'] = new ConvertKit_Admin_Upgrade();
72 $this->classes['admin_user'] = new ConvertKit_Admin_User();
73
74 // Run upgrade routine.
75 add_action( 'init', array( $this->classes['admin_upgrade'], 'run' ), 2 );
76
77 /**
78 * Initialize integration classes for the WordPress Administration interface.
79 *
80 * @since 1.9.6
81 */
82 do_action( 'convertkit_initialize_admin' );
83
84 }
85
86 /**
87 * Initialize classes for the WordPress Administration interface or a frontend Page Builder
88 *
89 * @since 1.9.6
90 */
91 private function initialize_admin_or_frontend_editor() {
92
93 // Bail if this request isn't for the WordPress Administration interface and isn't for a frontend Page Builder.
94 if ( ! $this->is_admin_or_frontend_editor() ) {
95 return;
96 }
97
98 /**
99 * Initialize integration classes for the WordPress Administration interface or a frontend Page Builder.
100 *
101 * @since 1.9.6
102 */
103 do_action( 'convertkit_initialize_admin_or_frontend_editor' );
104
105 }
106
107 /**
108 * Initialize classes for WP-CLI and WP-Cron
109 *
110 * @since 2.5.2
111 */
112 private function initialize_cli_cron() {
113
114 // Bail if this isn't a CLI or CRON request.
115 if ( ! $this->is_cli() && ! $this->is_cron() ) {
116 return;
117 }
118
119 /**
120 * Initialize integration classes for WP-CLI and WP-Cron.
121 *
122 * @since 1.9.6
123 */
124 do_action( 'convertkit_initialize_cli_cron' );
125
126 }
127
128 /**
129 * Initialize classes for the frontend web site
130 *
131 * @since 1.9.6
132 */
133 private function initialize_frontend() {
134
135 // Bail if this request isn't for the frontend web site.
136 if ( is_admin() ) {
137 return;
138 }
139
140 $this->classes['output'] = new ConvertKit_Output();
141
142 /**
143 * Initialize integration classes for the frontend web site.
144 *
145 * @since 1.9.6
146 */
147 do_action( 'convertkit_initialize_frontend' );
148
149 }
150
151 /**
152 * Initialize classes required globally, across the WordPress Administration, CLI, Cron and Frontend
153 * web site.
154 *
155 * @since 1.9.6
156 */
157 private function initialize_global() {
158
159 $this->classes['ajax'] = new ConvertKit_AJAX();
160 $this->classes['blocks_convertkit_content'] = new ConvertKit_Block_Content();
161 $this->classes['blocks_convertkit_form'] = new ConvertKit_Block_Form();
162 $this->classes['gutenberg'] = new ConvertKit_Gutenberg();
163 $this->classes['review_request'] = new ConvertKit_Review_Request( 'ConvertKit', 'convertkit' );
164 $this->classes['shortcodes'] = new ConvertKit_Shortcodes();
165 $this->classes['widgets'] = new ConvertKit_Widgets();
166
167 /**
168 * Initialize integration classes for the frontend web site.
169 *
170 * @since 1.9.6
171 */
172 do_action( 'convertkit_initialize_global' );
173
174 }
175
176 /**
177 * Improved version of WordPress' is_admin(), which includes whether we're
178 * editing on the frontend using a Page Builder.
179 *
180 * @since 1.9.6
181 *
182 * @return bool Is Admin or Frontend Editor Request
183 */
184 public function is_admin_or_frontend_editor() {
185
186 // If we're in the wp-admin, return true.
187 if ( is_admin() ) {
188 return true;
189 }
190
191 // Pro.
192 if ( array_key_exists( 'REQUEST_URI', $_SERVER ) ) {
193 if ( strpos( sanitize_text_field( $_SERVER['REQUEST_URI'] ), '/pro/' ) !== false ) {
194 return true;
195 }
196 if ( strpos( sanitize_text_field( $_SERVER['REQUEST_URI'] ), '/x/' ) !== false ) {
197 return true;
198 }
199 if ( strpos( sanitize_text_field( $_SERVER['REQUEST_URI'] ), 'cornerstone-endpoint' ) !== false ) {
200 return true;
201 }
202 }
203
204 // If the request global exists, check for specific request keys which tell us
205 // that we're using a frontend editor.
206 // Avada Live.
207 if ( array_key_exists( 'fb-edit', $_REQUEST ) ) { // phpcs:ignore
208 return true;
209 }
210
211 // Beaver Builder.
212 if ( array_key_exists( 'fl_builder', $_REQUEST ) ) { // phpcs:ignore
213 return true;
214 }
215
216 // Brizy.
217 if ( array_key_exists( 'brizy-edit', $_REQUEST ) ) { // phpcs:ignore
218 return true;
219 }
220
221 // Cornerstone (AJAX).
222 if ( array_key_exists( '_cs_nonce', $_REQUEST ) ) { // phpcs:ignore
223 return true;
224 }
225
226 // Divi.
227 if ( array_key_exists( 'et_fb', $_REQUEST ) ) { // phpcs:ignore
228 return true;
229 }
230
231 // Elementor.
232 if ( array_key_exists( 'action', $_REQUEST ) && sanitize_text_field( $_REQUEST['action'] ) === 'elementor' ) { // phpcs:ignore
233 return true;
234 }
235
236 // Kallyas.
237 if ( array_key_exists( 'zn_pb_edit', $_REQUEST ) ) { // phpcs:ignore
238 return true;
239 }
240
241 // Oxygen.
242 if ( array_key_exists( 'ct_builder', $_REQUEST ) ) { // phpcs:ignore
243 return true;
244 }
245
246 // Thrive Architect.
247 if ( array_key_exists( 'tve', $_REQUEST ) ) { // phpcs:ignore
248 return true;
249 }
250
251 // Visual Composer.
252 if ( array_key_exists( 'vcv-editable', $_REQUEST ) ) { // phpcs:ignore
253 return true;
254 }
255
256 // WPBakery Page Builder.
257 if ( array_key_exists( 'vc_editable', $_REQUEST ) ) { // phpcs:ignore
258 return true;
259 }
260
261 // Zion Builder.
262 if ( array_key_exists( 'action', $_REQUEST ) && sanitize_text_field( $_REQUEST['action'] ) === 'zion_builder_active' ) { // phpcs:ignore
263 return true;
264 }
265
266 // Assume we're not in the Administration interface.
267 $is_admin_or_frontend_editor = false;
268
269 /**
270 * Filters whether the current request is a WordPress Administration / Frontend Editor request or not.
271 *
272 * Page Builders can set this to true to allow ConvertKit to load its administration functionality.
273 *
274 * @since 1.9.6
275 *
276 * @param bool $is_admin_or_frontend_editor Is WordPress Administration / Frontend Editor request.
277 */
278 $is_admin_or_frontend_editor = apply_filters( 'convertkit_is_admin_or_frontend_editor', $is_admin_or_frontend_editor ); // phpcs:ignore
279
280 // Return filtered result.
281 return $is_admin_or_frontend_editor;
282
283 }
284
285 /**
286 * Detects if the request is through the WP-CLI
287 *
288 * @since 1.9.6
289 *
290 * @return bool Is WP-CLI Request
291 */
292 public function is_cli() {
293
294 if ( ! defined( 'WP_CLI' ) ) {
295 return false;
296 }
297 if ( ! WP_CLI ) {
298 return false;
299 }
300
301 return true;
302
303 }
304
305 /**
306 * Detects if the request is through the WP CRON
307 *
308 * @since 1.9.6
309 *
310 * @return bool Is WP CRON Request
311 */
312 public function is_cron() {
313
314 if ( ! defined( 'DOING_CRON' ) ) {
315 return false;
316 }
317 if ( ! DOING_CRON ) {
318 return false;
319 }
320
321 return true;
322
323 }
324
325 /**
326 * Loads plugin textdomain
327 *
328 * @since 1.0.0
329 */
330 public function load_language_files() {
331
332 load_plugin_textdomain( 'convertkit', false, basename( dirname( CONVERTKIT_PLUGIN_FILE ) ) . '/languages/' ); // @phpstan-ignore-line.
333
334 }
335
336 /**
337 * Returns the given class
338 *
339 * @since 1.9.6
340 *
341 * @param string $name Class Name.
342 * @return object Class Object
343 */
344 public function get_class( $name ) {
345
346 // If the class hasn't been loaded, throw a WordPress die screen
347 // to avoid a PHP fatal error.
348 if ( ! isset( $this->classes[ $name ] ) ) {
349 // Define the error.
350 $error = new WP_Error(
351 'convertkit_get_class',
352 sprintf(
353 /* translators: %1$s: PHP class name */
354 __( 'ConvertKit Error: Could not load Plugin class <strong>%1$s</strong>', 'convertkit' ),
355 $name
356 )
357 );
358
359 // Depending on the request, return or display an error.
360 // Admin UI.
361 if ( is_admin() ) {
362 wp_die(
363 esc_attr( $error->get_error_message() ),
364 esc_html__( 'ConvertKit Error', 'convertkit' ),
365 array(
366 'back_link' => true,
367 )
368 );
369 }
370
371 // Cron / CLI.
372 return $error;
373 }
374
375 // Return the class object.
376 return $this->classes[ $name ];
377
378 }
379
380 /**
381 * Returns the singleton instance of the class.
382 *
383 * @since 1.1.6
384 *
385 * @return object Class.
386 */
387 public static function get_instance() {
388
389 if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) { // @phpstan-ignore-line.
390 self::$instance = new self();
391 }
392
393 return self::$instance;
394
395 }
396
397 }
398