PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.4.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.4.1
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / WordPress / CompatibilityService.php
CompatibilityService.php
231 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Compatibility service.
4 *
5 * @package SureCartAppCore
6 * @author SureCart <support@surecart.com>
7 * @copyright SureCart
8 * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
9 * @link https://surecart.com
10 */
11
12 namespace SureCart\WordPress;
13
14 use SureCart\WordPress\Admin\Notices\AdminNoticesService;
15
16 /**
17 * Provides compatibility with other plugins.
18 */
19 class CompatibilityService {
20 /**
21 * Bootstrap the service.
22 *
23 * @return void
24 */
25 public function bootstrap() {
26 // UAG fix.
27 add_action( 'render_block_data', [ $this, 'maybeEnqueueUAGBAssets' ] );
28 // SC Form Shortcode fix.
29 add_filter( 'surecart/shortcode/render', [ $this, 'maybeEnqueueUAGBAssetsForShortcode' ], 5, 3 );
30 // rankmath fix.
31 add_action( 'rank_math/head', [ $this, 'rankMathFix' ] );
32
33 // Yoast SEO fix.
34 add_action( 'wpseo_frontend_presenters', [ $this, 'yoastSEOFix' ] );
35
36 // Siteground JS combine exclude.
37 add_filter( 'sgo_javascript_combine_exclude_ids', [ $this, 'sitegroundJsCombineExcludeScriptIds' ] );
38
39 // Show gutenberg active notice.
40 add_action( 'admin_init', [ $this, 'gutenbergActiveNotice' ] );
41
42 // Load Divi Compatibility CSS.
43 add_action( 'wp_enqueue_scripts', [ $this, 'diviCompatibility' ] );
44
45 // Load Blocks Global Styles if enabled by Merchant in the setting.
46 if ( (bool) get_option( 'surecart_load_block_assets_on_demand', false ) ) {
47 add_filter( 'should_load_separate_core_block_assets', '__return_true' );
48 }
49
50 add_action( 'render_block', [ $this, 'fixKadenceAccordionPaneButtonType' ], 10, 2 );
51 }
52
53 /**
54 * Adds type="button" to the button in the Kadence Accordion Pane.
55 * This prevents the button from submitting the form.
56 *
57 * @param string $content Block content.
58 * @param array $block Block data.
59 *
60 * @return string
61 */
62 public function fixKadenceAccordionPaneButtonType( $content, $block ) {
63 if ( 'kadence/pane' === $block['blockName'] ) {
64 $processor = new \WP_HTML_Tag_Processor( $content );
65 $has_tag = $processor->next_tag( 'button' );
66 if ( $has_tag ) {
67 $processor->set_attribute( 'type', 'button' );
68 }
69 return $processor->get_updated_html();
70 }
71 return $content;
72 }
73
74 /**
75 * Load Divi Compatibility CSS.
76 *
77 * @return void
78 */
79 public function diviCompatibility() {
80 if ( ! is_plugin_active( 'divi-builder/divi-builder.php' ) ) {
81 return;
82 }
83 wp_enqueue_style( 'surecart-divi-compatibility', plugins_url( 'styles/divi-compatibility.css', SURECART_PLUGIN_FILE ), '', \SureCart::plugin()->version(), 'all' );
84 }
85
86 /** Prevent Yoast SEO from outputing SEO meta tags on our custom pages.
87 *
88 * @param array $presenters Presenters.
89 * @return array Empty Array.
90 */
91 public function yoastSEOFix( $presenters ) {
92 if ( is_singular( 'sc_product' ) || is_singular( 'sc_collection' ) || is_singular( 'sc_upsell' ) ) {
93 $title_presenters = array_filter(
94 $presenters,
95 function ( $item ) {
96 return strpos( get_class( $item ), 'SEO\Presenters\Title_Presenter' );
97 }
98 );
99 return apply_filters( 'sc_wpseo_frontend_presenters', $title_presenters, $presenters );
100 }
101
102 return $presenters;
103 }
104
105 /**
106 * Prevent rankmath from outputting og:tags on our custom pages.
107 *
108 * @return void
109 */
110 public function rankMathFix() {
111 if ( is_singular( 'sc_product' ) || is_singular( 'sc_collection' ) || is_singular( 'sc_upsell' ) ) {
112 remove_all_actions( 'rank_math/opengraph/facebook' );
113 remove_all_actions( 'rank_math/opengraph/twitter' );
114 }
115 }
116
117 /**
118 * Render block data.
119 *
120 * @param array $parsed_block Block data.
121 *
122 * @return array
123 */
124 public function maybeEnqueueUAGBAssets( $parsed_block ) {
125 // UAGB must be activated.
126 if ( ! class_exists( '\UAGB_Post_Assets' ) ) {
127 return $parsed_block;
128 }
129
130 // must be our checkout form block.
131 if ( 'surecart/checkout-form' !== $parsed_block['blockName'] && 'surecart/upsell' !== $parsed_block['blockName'] ) {
132 return $parsed_block;
133 }
134
135 $post = get_post();
136 $upsell_id = isset( $post->upsell->id ) ? $post->upsell->id : null;
137
138 $id = $parsed_block['attrs']['id'] ?? $upsell_id;
139
140 if ( empty( $id ) ) {
141 return $parsed_block;
142 }
143
144 // If Spectra Blocks are present in the form, enqueue the assets.
145 $post_assets_instance = new \UAGB_Post_Assets( $id );
146 $post_assets_instance->enqueue_scripts(); // This will enqueue the JS and CSS files.
147
148 if ( ! empty( $post_assets_instance->file_generation ) && 'disabled' === $post_assets_instance->file_generation ) {
149 add_action( 'wp_footer', array( $post_assets_instance, 'print_stylesheet' ) ); // As on checkout page, the wp_head action is loaded late & Spectra prints inline CSS on that action for file_generation disabled case, we need to print the CSS on footer.
150 }
151
152 return $parsed_block;
153 }
154
155 /**
156 * Filter SC Form Shortcode to load the Spectra Blocks Assets.
157 *
158 * @param string $output Content.
159 * @param array $attributes Shortcode attributes.
160 * @param string $name Shortcode Tag.
161 *
162 * @return array
163 */
164 public function maybeEnqueueUAGBAssetsForShortcode( $output, $attributes, $name ) {
165 // UAGB must be activated.
166 if ( ! class_exists( '\UAGB_Post_Assets' ) ) {
167 return $output;
168 }
169
170 // must be our form shortcode.
171 if ( 'sc_form' !== $name ) {
172 return $output;
173 }
174
175 // must have an ID.
176 if ( empty( $attributes['id'] ) ) {
177 return $output;
178 }
179
180 // If Spectra Blocks are present in the form, enqueue the assets.
181 $post_assets_instance = new \UAGB_Post_Assets( $attributes['id'] );
182 $post_assets_instance->enqueue_scripts();
183
184 return $output;
185 }
186
187 /**
188 * Show the Gutenberg active notice.
189 *
190 * @return void
191 */
192 public function gutenbergActiveNotice(): void {
193 if ( is_plugin_active( 'gutenberg/gutenberg.php' ) ) {
194 ( new AdminNoticesService() )->add(
195 [
196 'name' => 'gutenberg_active_notice',
197 'type' => 'warning',
198 'title' => esc_html__( 'SureCart', 'surecart' ),
199 'text' => wp_kses_post( __( '<p>The Gutenberg plugin is currently active. SureCart blocks might not perform as expected within the block editor. If you encounter any issues, consider disabling the Gutenberg plugin.<p>', 'surecart' ) ),
200 ]
201 );
202 }
203 }
204
205 /**
206 * Exclude SureCart JS modules from Siteground JS combine.
207 * This is to prevent the JS modules from being combined and minified by Siteground.
208 *
209 * @param array $exclude_ids Array of JS module IDs to exclude.
210 *
211 * @return array
212 */
213 public function sitegroundJsCombineExcludeScriptIds( $exclude_ids ) {
214 // WordPress scripts which depends on interactivity.
215 $exclude_ids[] = '@wordpress/block-library/navigation/view-js-module';
216
217 // SureCart scripts.
218 $exclude_ids[] = '@surecart/product-page-js-module';
219 $exclude_ids[] = '@surecart/image-slider-js-module';
220 $exclude_ids[] = '@surecart/checkout-js-module';
221 $exclude_ids[] = '@surecart/cart-js-module';
222 $exclude_ids[] = '@surecart/a11y-js-module';
223 $exclude_ids[] = '@surecart/checkout-service-js-module';
224 $exclude_ids[] = '@surecart/checkout-events-js-module';
225 $exclude_ids[] = '@surecart/dropdown-js-module';
226 $exclude_ids[] = '@surecart/product-list-js-module';
227
228 return $exclude_ids;
229 }
230 }
231