PluginProbe ʕ •ᴥ•ʔ
ShopPress – Shop Builder for Elementor and WooCommerce / trunk
ShopPress – Shop Builder for Elementor and WooCommerce vtrunk
shop-press / Plugin.php
shop-press Last commit date
Admin 1 week ago Elementor 1 week ago Modules 1 week ago Templates 1 week ago blocks 1 week ago build 1 week ago includes 1 week ago languages 1 week ago public 1 week ago vendor 1 week ago Assets.php 1 year ago Plugin.php 1 week ago Settings.php 2 years ago changelog.txt 1 week ago readme.txt 1 week ago shop-press.php 1 week ago
Plugin.php
450 lines
1 <?php
2 /**
3 * Plugin.
4 *
5 * @package ShopPress
6 */
7
8 namespace ShopPress;
9
10 defined( 'ABSPATH' ) || exit;
11
12 use ShopPress\Admin;
13 use ShopPress\Compatibility;
14 use ShopPress\Templates;
15 use ShopPress\Elementor;
16 use ShopPress\Modules\MenuCart;
17 use ShopPress\Modules\Wishlist\Main;
18
19 final class Plugin {
20 /**
21 * Instance of this class.
22 *
23 * @since 1.0.0
24 */
25 public static $instance;
26
27 /**
28 * Provides access to a single instance of a module using the singleton pattern.
29 *
30 * @since 1.0.0
31 *
32 * @return object
33 */
34 public static function instance() {
35 if ( self::$instance === null ) {
36 self::$instance = new self();
37 }
38 return self::$instance;
39 }
40
41 /**
42 * Constructor.
43 *
44 * @since 1.0.0
45 */
46 public function __construct() {
47 $this->load_dependencies();
48 Assets::init();
49
50 if ( is_admin() ) {
51 Modules\AdminMessage::get_instance();
52 Admin\Page::init();
53 }
54
55 // TODO: improve this line
56 if ( ! in_array( 'woocommerce/woocommerce.php', (array) get_option( 'active_plugins', array() ), true ) && ! is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) ) {
57 return;
58 }
59
60 $this->hooks();
61 $this->init_shoppress();
62 }
63
64 /**
65 * Init hooks.
66 *
67 * @since 1.0.0
68 */
69 private function hooks() {
70 add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );
71 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ), 99 );
72 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue' ), 99 );
73 add_action( 'init', array( $this, 'init_compatibility' ) );
74 add_action( 'admin_init', array( __CLASS__, 'upgrade_default_options' ), 99 );
75 add_action( 'init', array( __CLASS__, 'rewrite_rules' ), 99 );
76 add_filter( 'render_block', array( __CLASS__, 'replace_blocks_with_shortcodes' ), 10, 2 );
77 add_filter( 'woocommerce_add_to_cart_fragments', array( __CLASS__, 'fragment' ), 99, 1 );
78 add_action( 'shoppress/builder/before_load_template', array( __CLASS__, 'setup_postdata' ), 9, 1 );
79 add_action( 'shoppress/builder/after_load_template', array( __CLASS__, 'reset_postdata' ), 9, 1 );
80 }
81
82 /**
83 * Load the localization file.
84 *
85 * @since 1.2.0
86 */
87 public function load_plugin_textdomain() {
88 // phpcs:ignore PluginCheck.CodeAnalysis.DiscouragedFunctions.load_plugin_textdomainFound -- Required for plugin translations.
89 load_plugin_textdomain( 'shop-press', false, basename( __DIR__ ) . '/languages' );
90 }
91
92 /**
93 * Enqueue style.
94 *
95 * @since 1.0.0
96 */
97 public function enqueue() {
98
99 wp_enqueue_script( 'sp-frontend' );
100 wp_enqueue_style( 'sp-frontend' );
101
102 if ( is_rtl() ) {
103 wp_enqueue_style( 'sp-frontend-rtl' );
104 }
105
106 wp_localize_script(
107 'sp-frontend',
108 'shoppress_frontend',
109 apply_filters(
110 'shoppress_frontend_localize',
111 array(
112 'ajax' => array(
113 'url' => admin_url( 'admin-ajax.php' ),
114 'nonce' => wp_create_nonce( 'shoppress_nonce' ),
115 ),
116 'i18n' => array(
117 'days' => __( 'Days', 'shop-press' ),
118 'hours' => __( 'Hours', 'shop-press' ),
119 'minutes' => __( 'Minutes', 'shop-press' ),
120 'seconds' => __( 'Seconds', 'shop-press' ),
121 'star' => __( 'Star', 'shop-press' ),
122 'stars' => __( 'Stars', 'shop-press' ),
123 'add_to_cart' => __( 'Add to Cart', 'shop-press' ),
124 ),
125 )
126 )
127 );
128 }
129
130 /**
131 * Admin enqueue style and script.
132 *
133 * @since 1.3.3
134 */
135 public function admin_enqueue() {
136
137 wp_enqueue_script( 'sp-backend' );
138 wp_enqueue_style( 'sp-backend' );
139
140 wp_localize_script(
141 'sp-backend',
142 'shoppress_backend',
143 apply_filters(
144 'shoppress_backend_localize',
145 array(
146 'ajax' => array(
147 'url' => admin_url( 'admin-ajax.php' ),
148 'nonce' => wp_create_nonce( 'shoppress_backend_nonce' ),
149 ),
150 'i18n' => array(),
151 )
152 )
153 );
154 }
155
156 /**
157 * Load the dependencies.
158 *
159 * @since 1.0.0
160 */
161 private function load_dependencies() {
162 if (file_exists(SHOPPRESS_PATH . 'auto-update.php')) {
163 require_once SHOPPRESS_PATH . 'auto-update.php';
164 }
165 // General functions
166 require_once SHOPPRESS_PATH . 'includes/functions/functions-general.php';
167
168 // Embedded Styler (Elementor styling control).
169 if ( file_exists( SHOPPRESS_PATH . 'includes/styler/styler.php' ) ) {
170 require_once SHOPPRESS_PATH . 'includes/styler/styler.php';
171 }
172 }
173
174 /**
175 * Load the shoppress dependencies.
176 *
177 * @since 1.0.0
178 */
179 private function init_shoppress() {
180 Admin\Main::init();
181 Templates\Main::init();
182 BlockEditor\Integration::init();
183 Elementor\Integration::init();
184 Modules\RecentlyViewedProducts::init();
185 Modules\AjaxSearch::init();
186
187 $modules = apply_filters(
188 'shoppress_modules',
189 array(
190 'wishlist' => Modules\Wishlist\Main::class,
191 'compare' => Modules\Compare::class,
192 'quick_view' => Modules\QuickView::class,
193 'catalog_mode' => Modules\CatalogMode::class,
194 'mobile_panel' => Modules\MobilePanel::class,
195 'size_chart' => Modules\SizeChart::class,
196 'rename_label' => Modules\RenameLabel::class,
197 'sticky_add_to_cart' => Modules\StickyAddToCart::class,
198 'single_ajax_add_to_cart' => Modules\SingleAjaxAddToCart::class,
199 'backorder' => Modules\Backorder::class,
200 'flash_sale_countdown' => Modules\FlashSalesCountdown::class,
201 'notifications' => Modules\Notifications::class,
202 'variation_swatches' => Modules\VariationSwatches\Main::class,
203 'menu_cart' => Modules\MenuCart::class,
204 'multi_step_checkout' => Modules\MultiStep::class,
205 'shopify_checkout' => Modules\Shopify::class,
206 )
207 );
208
209 foreach ( $modules as $module_id => $module_class ) {
210
211 if ( sp_is_module_active( $module_id ) ) {
212
213 if ( class_exists( $module_class ) ) {
214 $module_class::init();
215 }
216 }
217 }
218
219 $templates = apply_filters(
220 'shoppress_templates',
221 array(
222 'single' => Templates\Single\Main::class,
223 'archive' => Templates\Archive::class,
224 'shop' => Templates\Shop::class,
225 'checkout' => Templates\Checkout\Main::class,
226 'cart' => Templates\Cart::class,
227 'empty_cart' => Templates\EmptyCart::class,
228 'my_account' => Templates\MyAccount::class,
229 'thank_you' => Templates\Thankyou::class,
230 )
231 );
232
233 foreach ( $templates as $template_id => $template_class ) {
234
235 if ( sp_is_template_active( $template_id ) ) {
236
237 if ( class_exists( $template_class ) ) {
238 $template_class::init();
239 }
240 }
241 }
242
243 add_action( 'widgets_init', array( __CLASS__, 'register_shoppress_widget_layered_nav_widget' ), 99 );
244
245 do_action( 'shoppress/after_init' );
246 }
247
248 /**
249 * ShopPress compatibilities.
250 *
251 * @since 1.1.0
252 */
253 public function init_compatibility() {
254 // Init Astra theme compatibility
255 if ( defined( 'ASTRA_THEME_VERSION' ) ) {
256 Compatibility\Astra::init();
257 }
258
259 // Init Kata theme compatibility
260 if ( defined( 'KATA_VERSION' ) ) {
261 Compatibility\KataTheme::init();
262 }
263
264 // Init Storefront theme compatibility
265 if ( function_exists( 'storefront_is_woocommerce_activated' ) ) {
266 Compatibility\Storefront::init();
267 }
268 }
269
270 /**
271 * ShopPress rewrite rules.
272 *
273 * @since 1.2.0
274 */
275 public static function rewrite_rules() {
276
277 if ( 'yes' === get_option( 'sp_need_rewrite_rules', 'no' ) ) {
278
279 flush_rewrite_rules();
280
281 delete_option( 'sp_need_rewrite_rules' );
282 }
283 }
284
285 /**
286 * Add default option.
287 *
288 * @since 1.0.0
289 */
290 public static function add_sp_option() {
291 $options = Admin\DefaultOptions\Options::get_default_options();
292
293 $options = json_encode( $options );
294
295 add_option( 'sp_admin', $options );
296 }
297
298 /**
299 * Register shoppress widget layered nav
300 *
301 * @since 1.0.0
302 *
303 * @return void
304 */
305 public static function register_shoppress_widget_layered_nav_widget() {
306
307 if ( class_exists( 'WC_Widget_Layered_Nav' ) ) {
308
309 require_once SHOPPRESS_PATH . 'includes/wp-widget/LayeredNav.php';
310 register_widget( 'ShopPress_Widget_Layered_Nav' );
311 }
312 }
313
314 /**
315 * Check if Elementor plugin is active.
316 *
317 * @since 1.1.4
318 *
319 * @return bool
320 */
321 public static function is_active_elementor() {
322
323 if ( ! function_exists( 'is_plugin_active' ) ) {
324
325 include_once ABSPATH . 'wp-admin/includes/plugin.php';
326 }
327
328 return is_plugin_active( 'elementor/elementor.php' );
329 }
330
331 /**
332 * Compatibility with Woo 8.3
333 *
334 * @since 1.2.0
335 */
336 public static function replace_blocks_with_shortcodes( $content, $block ) {
337
338 if ( is_checkout() && ( sp_is_template_active( 'checkout' ) || sp_is_module_active( 'multi_step_checkout' ) ) ) {
339
340 if ( $block && $block['blockName'] === 'woocommerce/checkout' ) {
341
342 $content = '[woocommerce_checkout]';
343 }
344 }
345
346 if ( is_cart() && sp_is_template_active( 'cart' ) ) {
347
348 if ( $block && $block['blockName'] === 'woocommerce/cart' ) {
349
350 $content = '[woocommerce_cart]';
351 }
352 }
353
354 return $content;
355 }
356
357 /**
358 * Upgrade default options admin settings.
359 *
360 * @since 1.2.0
361 *
362 * @return void
363 */
364 public static function upgrade_default_options() {
365
366 $version = get_option( 'sp_admin_options_version', '1.0.0' );
367 $pro_version = get_option( 'sp_pro_admin_options_version', '1.0.0' );
368 if ( version_compare( $version, SHOPPRESS_VERSION, '>=' ) && ( defined( 'SHOPPRESS_PRO_VERSION' ) && version_compare( $pro_version, SHOPPRESS_PRO_VERSION, '>=' ) ) ) {
369 return;
370 }
371
372 $options = Admin\DefaultOptions\Options::get_default_options();
373
374 $settings = get_option( 'sp_admin', array() );
375 $settings = ! is_array( $settings ) ? json_decode( $settings, true ) : $settings;
376 foreach ( $options as $group_key => $g_options ) {
377
378 foreach ( $g_options as $s_group_key => $s_options ) {
379
380 if ( ! isset( $settings[ $group_key ][ $s_group_key ] ) ) {
381
382 $settings[ $group_key ][ $s_group_key ] = $s_options;
383 } else {
384
385 foreach ( $s_options as $s_option_key => $s_option ) {
386
387 if ( ! isset( $settings[ $group_key ][ $s_group_key ][ $s_option_key ] ) ) {
388
389 $settings[ $group_key ][ $s_group_key ][ $s_option_key ] = $s_option;
390 }
391 }
392 }
393 }
394 }
395
396 update_option( 'sp_admin', json_encode( $settings ) );
397 update_option( 'sp_admin_options_version', SHOPPRESS_VERSION );
398 if ( defined( 'SHOPPRESS_PRO_VERSION' ) ) {
399 update_option( 'sp_pro_admin_options_version', SHOPPRESS_PRO_VERSION );
400 }
401 }
402
403 public static function fragment( $fragments ) {
404
405 $cart_items_count = ! empty( WC()->cart ) ? WC()->cart->get_cart_contents_count() : 0;
406 $fragments['.sp-cart-items-count'] = '<span class="sp-cart-items-count">' . $cart_items_count . '</span>';
407 ob_start();
408 MenuCart::cart_content();
409 $fragments['.sp-mini-cart.sp-mini-cart-style-1'] = ob_get_clean();
410
411 if ( sp_is_module_active( 'wishlist' ) ) {
412
413 $count = Main::get_total_wishlist_products();
414 $fragments['.sp-wishlist-items-count'] = '<span class="sp-wishlist-items-count">' . $count . '</span>';
415 }
416
417 return $fragments;
418 }
419
420 /**
421 * Set the post data.
422 *
423 * @since 1.2.5
424 *
425 * @return void
426 */
427 public static function setup_postdata( $builder ) {
428
429 if ( in_array( $builder, Templates\Render::get_builders_need_set_post_data() ) ) {
430
431 Templates\Utils::setup_postdata();
432 }
433 }
434
435 /**
436 * Reset the post data.
437 *
438 * @since 1.2.5
439 *
440 * @return void
441 */
442 public static function reset_postdata( $builder ) {
443
444 if ( in_array( $builder, Templates\Render::get_builders_need_set_post_data() ) ) {
445
446 Templates\Utils::reset_postdata();
447 }
448 }
449 }
450