PluginProbe
Xpro Theme Builder For Elementor – FREE / trunk
Xpro Theme Builder For Elementor – FREE vtrunk
xpro-theme-builder / plugin.php

plugin.php in Xpro Theme Builder For Elementor – FREE trunk, at plugin.php

474 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use Elementor\Core\Files\CSS\Post;
4 use Elementor\Frontend;
5 use Elementor\Post_CSS_File;
6 use ElementorPro\Plugin;
7 use Xpro_Theme_Builder\Lib\Xpro_Target_Rules_Fields;
8 use XproElementorAddons\Inc\Xpro_Elementor_Module_List;
9 use XproElementorAddons\Libs\Xpro_Elementor_Dashboard;
10
11 /**
12 * Class Xpro_Theme_Builder_Main
13 */
14 class Xpro_Theme_Builder_Main {
15
16 /**
17 * Instance of Xpro_Theme_Builder_Main
18 *
19 * @var Xpro_Theme_Builder_Main
20 */
21 private static $instance = null;
22
23 /**
24 * Instance of Elementor Frontend class.
25 *
26 * @var Frontend()
27 */
28 private static $elementor_instance;
29 /**
30 * Current theme template
31 *
32 * @var String
33 */
34 public $template;
35
36 /**
37 * Instance of Xpro_Theme_Builder_Main
38 *
39 * @return Xpro_Theme_Builder_Main Instance of Xpro_Theme_Builder_Main
40 */
41 public static function instance() {
42 if ( ! isset( self::$instance ) ) {
43 self::$instance = new self();
44 self::$instance->init();
45 }
46
47 return self::$instance;
48 }
49
50 public function init() {
51
52 $this->template = get_template();
53
54 self::$elementor_instance = ( defined( 'ELEMENTOR_VERSION' ) && is_callable( 'Elementor\Plugin::instance' ) ) ? Elementor\Plugin::instance() : '';
55
56 if ( self::$elementor_instance ) {
57
58 $this->includes();
59
60 if ( 'xpro' === $this->template ) {
61 require XPRO_THEME_BUILDER_DIR . 'themes/class-xpro-compatibility.php';
62 } elseif ( 'astra' === $this->template ) {
63 require XPRO_THEME_BUILDER_DIR . 'themes/class-astra-compatibility.php';
64 } elseif ( 'generatepress' === $this->template ) {
65 require XPRO_THEME_BUILDER_DIR . 'themes/class-generatepress-compatibility.php';
66 } elseif ( 'megaone' === $this->template ) {
67 require XPRO_THEME_BUILDER_DIR . 'themes/class-megaone-compatibility.php';
68 } elseif ( 'oceanwp' === $this->template ) {
69 require XPRO_THEME_BUILDER_DIR . 'themes/class-oceanwp-compatibility.php';
70 } else {
71 require XPRO_THEME_BUILDER_DIR . 'themes/class-default-compatibility.php';
72 }
73
74 // Scripts and styles.
75 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
76 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
77 add_filter( 'body_class', array( $this, 'body_class' ) );
78
79 //Plugin Row Meta & Link
80 add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 );
81 add_filter( 'plugin_action_links_' . XPRO_THEME_BUILDER_BASE, array( $this, 'plugin_action_links' ) );
82
83 add_shortcode( 'xpro_theme_builder_template', array( $this, 'render_template' ) );
84
85 //Add Elementor Modules
86 add_action( 'init', array( $this, 'register_modules' ) );
87
88 // Register Document Type
89 add_action( 'elementor/documents/register', array( $this, 'register_elementor_document_type' ) );
90
91 //Comment Shortcode
92 add_shortcode( 'xpro_comments_template', array( $this, 'xpro_theme_builder_comments_template' ) );
93
94 }
95
96 }
97
98 /**
99 * Loads the globally required files for the plugin.
100 */
101 public function includes() {
102
103 require_once XPRO_THEME_BUILDER_DIR . 'admin/class-xpro-admin.php';
104 require_once XPRO_THEME_BUILDER_DIR . 'admin/class-xpro-rest-api.php';
105
106 require_once XPRO_THEME_BUILDER_DIR . 'inc/xpro-functions.php';
107
108 // Load Target rules.
109 require_once XPRO_THEME_BUILDER_DIR . 'lib/target-rule/class-xpro-target-rules-fields.php';
110
111 // Load WPML & Polylang Compatibility if WPML is installed and activated.
112 if ( defined( 'ICL_SITEPRESS_VERSION' ) || defined( 'POLYLANG_BASENAME' ) ) {
113 require_once XPRO_THEME_BUILDER_DIR . 'inc/wpml-compatibility.php';
114 }
115
116 }
117
118 /**
119 * Prints the Header content.
120 */
121 public static function get_header_content() {
122 if ( self::$elementor_instance ) {
123 echo self::$elementor_instance->frontend->get_builder_content_for_display( get_xpro_theme_builder_header_id() ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
124 }
125 }
126
127 /**
128 * Prints the Footer content.
129 */
130 public static function get_footer_content() {
131 if ( self::$elementor_instance ) {
132 echo self::$elementor_instance->frontend->get_builder_content_for_display( get_xpro_theme_builder_footer_id() ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
133 }
134 }
135
136 /**
137 * Prints the Before Footer content.
138 */
139 public static function get_singular_content() {
140 if ( self::$elementor_instance ) {
141 echo self::$elementor_instance->frontend->get_builder_content_for_display( xpro_theme_builder_get_singular_id() ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
142 }
143 }
144
145 /**
146 * Prints the Before Footer content.
147 */
148 public static function get_archive_content() {
149 if ( self::$elementor_instance ) {
150 echo self::$elementor_instance->frontend->get_builder_content_for_display( xpro_theme_builder_get_archive_id() ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
151 }
152 }
153
154 /**
155 * Get option for the plugin settings
156 *
157 * @param mixed $setting Option name.
158 * @param mixed $default Default value to be received if the option value is not stored in the option.
159 *
160 * @return mixed.
161 */
162 public static function get_settings( $setting = '', $default = '' ) {
163 if ( 'type_header' === $setting || 'type_footer' === $setting || 'type_singular' === $setting || 'type_archive' === $setting ) {
164 $templates = self::get_template_id( $setting );
165 $template = ! is_array( $templates ) ? $templates : $templates[0];
166 $template = apply_filters( "xpro_theme_builder_get_settings_{$setting}", $template );
167 return $template;
168 }
169 }
170
171 /**
172 * Get header or footer template id based on the meta query.
173 *
174 * @param String $type Type of the template header/footer.
175 *
176 * @return Mixed Returns the header or footer template id if found, else returns string ''.
177 */
178 public static function get_template_id( $type ) {
179
180 $option = array(
181 'location' => 'xpro_theme_builder_target_include_locations',
182 'exclusion' => 'xpro_theme_builder_target_exclude_locations',
183 'users' => 'xpro_theme_builder_target_user_roles',
184 );
185
186 $xpro_theme_builder_templates = Xpro_Target_Rules_Fields::get_instance()->get_posts_by_conditions( 'xpro-themer', $option );
187
188 foreach ( $xpro_theme_builder_templates as $template ) {
189 if ( get_post_meta( absint( $template['id'] ), 'xpro_theme_builder_template_type', true ) === $type ) {
190 return $template['id'];
191 }
192 }
193
194 return '';
195 }
196
197 /**
198 * Enqueue styles and scripts.
199 */
200 public function enqueue_scripts() {
201
202 $all_modules = Xpro_Elementor_Module_List::instance()->get_list();
203 $active_modules = Xpro_Elementor_Dashboard::instance()->utils->get_option( 'xpro_elementor_module_list', array_keys( $all_modules ) );
204
205 wp_enqueue_style( 'xpro-theme-builder', XPRO_THEME_BUILDER_URL . 'assets/css/xpro-theme-builder.css', null, XPRO_THEME_BUILDER_VER );
206 wp_enqueue_script( 'xpro-theme-builder', XPRO_THEME_BUILDER_URL . 'assets/js/xpro-theme-builder.js', array( 'jquery' ), XPRO_THEME_BUILDER_VER, true );
207
208 wp_script_add_data( 'xpro-theme-builder', 'async', true );
209
210 //Frontend Panel
211 if ( is_user_logged_in() && current_user_can( 'edit_posts' ) && is_array( $active_modules ) && in_array( 'theme-builder', $active_modules, true ) && ! ( self::$elementor_instance && \Elementor\Plugin::$instance->preview->is_preview_mode() ) ) {
212
213 global $user_ID;
214
215 wp_enqueue_style( 'xpro-theme-builder-frontend', XPRO_THEME_BUILDER_URL . 'admin/assets/css/xpro-frontend.css', array(), XPRO_THEME_BUILDER_VER );
216 wp_enqueue_script( 'xpro-theme-builder-frontend', XPRO_THEME_BUILDER_URL . 'admin/assets/js/xpro-frontend.js', array(), XPRO_THEME_BUILDER_VER, true );
217 wp_enqueue_script( 'xpro-theme-builder-frontend-chunk', XPRO_THEME_BUILDER_URL . 'admin/assets/js/xpro-chunk.js', array(), XPRO_THEME_BUILDER_VER, true );
218 wp_enqueue_script( 'xpro-theme-builder-frontend-main', XPRO_THEME_BUILDER_URL . 'admin/assets/js/xpro-main.js', array(), XPRO_THEME_BUILDER_VER, true );
219 wp_localize_script(
220 'xpro-theme-builder-frontend',
221 'XproThemeBuilderApi',
222 array(
223 'ApiUrl' => get_rest_url(),
224 'siteUrl' => get_site_url(),
225 'adminUrl' => get_admin_url(),
226 'user' => $user_ID,
227 'nonce' => wp_create_nonce( 'wp_rest' ),
228 )
229 );
230 }
231
232 if ( class_exists( '\Elementor\Plugin' ) ) {
233 $elementor = \Elementor\Plugin::instance();
234 $elementor->frontend->enqueue_styles();
235 }
236
237 if ( class_exists( '\ElementorPro\Plugin' ) ) {
238 if (method_exists(Plugin::instance(), 'enqueue_styles')) {
239 $elementor_pro = Plugin::instance();
240 $elementor_pro->enqueue_styles();
241 }
242 }
243
244 if ( self::$elementor_instance && xpro_theme_builder_header_enabled() ) {
245 if ( class_exists( '\Elementor\Core\Files\CSS\Post' ) ) {
246 $css_file = new Post( get_xpro_theme_builder_header_id() );
247 } elseif ( class_exists( '\Elementor\Post_CSS_File' ) ) {
248 $css_file = new Post_CSS_File( get_xpro_theme_builder_header_id() );
249 }
250
251 $css_file->enqueue();
252 }
253
254 if ( self::$elementor_instance && xpro_theme_builder_footer_enabled() ) {
255 if ( class_exists( '\Elementor\Core\Files\CSS\Post' ) ) {
256 $css_file = new Post( get_xpro_theme_builder_footer_id() );
257 } elseif ( class_exists( '\Elementor\Post_CSS_File' ) ) {
258 $css_file = new Post_CSS_File( get_xpro_theme_builder_footer_id() );
259 }
260
261 $css_file->enqueue();
262 }
263
264 if ( self::$elementor_instance && xpro_theme_builder_is_singular_enabled() ) {
265 if ( class_exists( '\Elementor\Core\Files\CSS\Post' ) ) {
266 $css_file = new Post( xpro_theme_builder_get_singular_id() );
267 } elseif ( class_exists( '\Elementor\Post_CSS_File' ) ) {
268 $css_file = new Post_CSS_File( xpro_theme_builder_get_singular_id() );
269 }
270 $css_file->enqueue();
271 }
272
273 if ( self::$elementor_instance && xpro_theme_builder_is_archive_enabled() ) {
274 if ( class_exists( '\Elementor\Core\Files\CSS\Post' ) ) {
275 $css_file = new Post( xpro_theme_builder_get_archive_id() );
276 } elseif ( class_exists( '\Elementor\Post_CSS_File' ) ) {
277 $css_file = new Post_CSS_File( xpro_theme_builder_get_archive_id() );
278 }
279 $css_file->enqueue();
280 }
281
282 }
283
284 /**
285 * Load admin styles on header footer elementor edit screen.
286 */
287 public function enqueue_admin_scripts() {
288
289 global $pagenow;
290 $screen = get_current_screen();
291
292 wp_enqueue_style( 'xpro-theme-builder-admin', XPRO_THEME_BUILDER_URL . 'admin/assets/css/xpro-admin.css', array(), XPRO_THEME_BUILDER_VER );
293
294 if ( ( 'xpro-themer' === $screen->id && ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) ) || ( 'edit.php' === $pagenow && 'edit-xpro-themer' === $screen->id ) ) {
295
296 wp_enqueue_script( 'xpro-theme-builder-admin', XPRO_THEME_BUILDER_URL . 'admin/assets/js/xpro-admin.js', array( 'jquery' ), XPRO_THEME_BUILDER_VER, true );
297
298 }
299 }
300
301 /**
302 * Adds classes to the body tag conditionally.
303 *
304 * @param Array $classes array with class names for the body tag.
305 *
306 * @return Array array with class names for the body tag.
307 */
308 public function body_class( $classes ) {
309
310 $classes[] = 'xpro-theme-builder-template';
311
312 return $classes;
313 }
314
315 /**
316 * Callback to shortcode.
317 *
318 * @param array $atts attributes for shortcode.
319 */
320 public function render_template( $atts ) {
321
322 $atts = shortcode_atts(
323 array(
324 'id' => '',
325 ),
326 $atts,
327 'xpro_theme_builder_template'
328 );
329
330 $id = ! empty( $atts['id'] ) ? apply_filters( 'xpro_theme_builder_render_template_id', intval( $atts['id'] ) ) : '';
331
332 if ( empty( $id ) ) {
333 return '';
334 }
335
336 $post = get_post( $id );
337 if ( ! $post ) {
338 wp_die( esc_html__( 'Post not found.', 'xpro-theme-builder' ), 404 );
339 }
340 if ( ! empty( $post->post_password ) && post_password_required( $post ) ) {
341 wp_die( esc_html__( 'This post is password protected.', 'xpro-theme-builder' ), 403 );
342 }
343 if ( $post->post_status === 'trash' ) {
344 wp_die( esc_html__( 'You are not allowed to access this post (Trashed Post).', 'xpro-theme-builder' ), 403 );
345 }
346 $post_status = $post->post_status;
347 $status_labels = [
348 'private' => 'Private Post',
349 'draft' => 'Draft Post',
350 'pending' => 'Pending Post',
351 'trash' => 'Trashed Post',
352 ];
353 if (
354 in_array( $post_status, array_keys( $status_labels ), true ) &&
355 ! current_user_can( 'read_post', $id )
356 ) {
357 $label = $status_labels[ $post_status ];
358 /* translators: %s: Post label. */
359 wp_die( sprintf(esc_html__( 'You are not allowed to access this post(%s).', 'xpro-theme-builder' ), esc_html( $label ) ), 403 );
360 }
361
362 if ( self::$elementor_instance ) {
363 if ( class_exists( '\Elementor\Core\Files\CSS\Post' ) ) {
364 $css_file = new Post( $id );
365 } elseif ( class_exists( '\Elementor\Post_CSS_File' ) ) {
366 // Load elementor styles.
367 $css_file = new Post_CSS_File( $id );
368 }
369 $css_file->enqueue();
370 }
371
372 if ( self::$elementor_instance ) {
373 return self::$elementor_instance->frontend->get_builder_content_for_display( $id );
374 }
375
376 }
377
378 /**
379 * Plugin row meta.
380 *
381 * Adds row meta links to the plugin list table
382 *
383 * Fired by `plugin_row_meta` filter.
384 *
385 * @param array $plugin_meta An array of the plugin's metadata, including
386 * the version, author, author URI, and plugin URI.
387 * @param string $plugin_file Path to the plugin file, relative to the plugins
388 * directory.
389 *
390 * @return array An array of plugin row meta links.
391 * @since 1.0.0
392 * @access public
393 *
394 */
395 public function plugin_row_meta( $plugin_meta, $plugin_file ) {
396 if ( XPRO_THEME_BUILDER_BASE === $plugin_file ) {
397 $row_meta = array( 'docs' => '<a href="https://elementor.wpxpro.com/docs/" aria-label="' . esc_attr( esc_html__( 'View Documentation', 'xpro-theme-builder' ) ) . '" target="_blank">' . esc_html__( 'Documentation', 'xpro-theme-builder' ) . '</a>' );
398 $plugin_meta = array_merge( $plugin_meta, $row_meta );
399 }
400
401 return $plugin_meta;
402 }
403
404 /**
405 * Plugin action links.
406 *
407 * Adds action links to the plugin list table
408 *
409 * Fired by `plugin_action_links` filter.
410 *
411 * @param array $links An array of plugin action links.
412 *
413 * @return array An array of plugin action links.
414 * @since 1.0.0
415 * @access public
416 *
417 */
418 public function plugin_action_links( $links ) {
419 $settings_link = sprintf( '<a href="%1$s">%2$s</a>', admin_url( 'edit.php?post_type=xpro-themer' ), esc_html__( 'Settings', 'xpro-theme-builder' ) );
420 array_unshift( $links, $settings_link );
421 $links['rate_us'] = sprintf( '<a href="%1$s" target="_blank" class="xpro-elementor-addons-gopro">%2$s</a>', 'https://wordpress.org/plugins/xpro-theme-builder/#reviews', esc_html__( 'Rate Us', 'xpro-theme-builder' ) );
422 return $links;
423 }
424
425 /**
426 * Register Modules
427 *
428 * Register Modules Settings.
429 *
430 * @since 1.0.0
431 * @access public
432 */
433
434 public function register_modules() {
435 include_once XPRO_THEME_BUILDER_DIR . '/inc/header-sticky.php';
436 }
437
438 /**
439 * Register Document Type
440 *
441 * Register Modules Settings.
442 *
443 * @since 1.0.0
444 * @access public
445 */
446
447 public function register_elementor_document_type( $documents_manager ) {
448 if ( get_post_type() === 'xpro-themer' ) {
449 update_post_meta( get_the_ID(), '_elementor_template_type', 'xpro-themer' );
450 }
451 include_once XPRO_THEME_BUILDER_DIR . '/inc/preview-settings.php';
452 $documents_manager->register_document_type( Xpro_Theme_Builder_Settings::get_type(), Xpro_Theme_Builder_Settings::get_class_full_name() );
453 }
454
455 /**
456 * Create Shortcode for Comment
457 *
458 * @since 1.0.0
459 * @access public
460 */
461
462 public function xpro_theme_builder_comments_template() {
463 if ( ( comments_open() || get_comments_number() ) ) {
464 comments_template();
465 }
466 }
467
468
469
470 }
471
472 // Instantiate Xpro_Theme_Builder_Main Class
473 Xpro_Theme_Builder_Main::instance();
474