CompatibilityService.php
| 1 | <?php |
| 2 | /** |
| 3 | * @package SureCartAppCore |
| 4 | * @author SureCart <support@surecart.com> |
| 5 | * @copyright SureCart |
| 6 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
| 7 | * @link https://surecart.com |
| 8 | */ |
| 9 | |
| 10 | namespace SureCart\WordPress; |
| 11 | |
| 12 | use SureCart\WordPress\Admin\Notices\AdminNoticesService; |
| 13 | |
| 14 | /** |
| 15 | * Provides compatibility with other plugins. |
| 16 | */ |
| 17 | class CompatibilityService { |
| 18 | /** |
| 19 | * Bootstrap the service. |
| 20 | * |
| 21 | * @return void |
| 22 | */ |
| 23 | public function bootstrap() { |
| 24 | // UAG fix. |
| 25 | add_action( 'render_block_data', [ $this, 'maybeEnqueueUAGBAssets' ] ); |
| 26 | // SC Form Shortcode fix. |
| 27 | add_filter( 'surecart/shortcode/render', [ $this, 'maybeEnqueueUAGBAssetsForShortcode' ], 5, 3 ); |
| 28 | // rankmath fix. |
| 29 | add_action( 'rank_math/head', [ $this, 'rankMathFix' ] ); |
| 30 | |
| 31 | // Yoast SEO fix. |
| 32 | add_action( 'wpseo_frontend_presenters', [ $this, 'yoastSEOFix' ] ); |
| 33 | |
| 34 | // Show gutenberg active notice. |
| 35 | add_action( 'admin_init', [ $this, 'gutenbergActiveNotice' ] ); |
| 36 | |
| 37 | // Load Blocks Global Styles if enabled by Merchant in the setting. |
| 38 | if ( (bool) get_option( 'surecart_load_block_assets_on_demand', false ) ) { |
| 39 | add_filter( 'should_load_separate_core_block_assets', '__return_true' ); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** Prevent Yoast SEO from outputing SEO meta tags on our custom pages. |
| 44 | * |
| 45 | * @param array $presenters Presenters. |
| 46 | * @return array Empty Array. |
| 47 | */ |
| 48 | public function yoastSEOFix( $presenters ) { |
| 49 | if ( is_singular( 'sc_product' ) || is_singular( 'sc_collection' ) || is_singular( 'sc_upsell' ) ) { |
| 50 | $title_presenters = array_filter( |
| 51 | $presenters, |
| 52 | function( $item ) { |
| 53 | return strpos( get_class( $item ), 'SEO\Presenters\Title_Presenter' ); |
| 54 | } |
| 55 | ); |
| 56 | return apply_filters( 'sc_wpseo_frontend_presenters', $title_presenters, $presenters ); |
| 57 | } |
| 58 | |
| 59 | return $presenters; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Prevent rankmath from outputting og:tags on our custom pages. |
| 64 | * |
| 65 | * @return void |
| 66 | */ |
| 67 | public function rankMathFix() { |
| 68 | if ( is_singular( 'sc_product' ) || is_singular( 'sc_collection' ) || is_singular( 'sc_upsell' ) ) { |
| 69 | remove_all_actions( 'rank_math/opengraph/facebook' ); |
| 70 | remove_all_actions( 'rank_math/opengraph/twitter' ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Render block data. |
| 76 | * |
| 77 | * @param array $parsed_block Block data. |
| 78 | * |
| 79 | * @return array |
| 80 | */ |
| 81 | public function maybeEnqueueUAGBAssets( $parsed_block ) { |
| 82 | // UAGB must be activated. |
| 83 | if ( ! class_exists( '\UAGB_Post_Assets' ) ) { |
| 84 | return $parsed_block; |
| 85 | } |
| 86 | |
| 87 | // must be our checkout form block. |
| 88 | if ( 'surecart/checkout-form' !== $parsed_block['blockName'] ) { |
| 89 | return $parsed_block; |
| 90 | } |
| 91 | |
| 92 | // must have an ID. |
| 93 | if ( empty( $parsed_block['attrs']['id'] ) ) { |
| 94 | return $parsed_block; |
| 95 | } |
| 96 | |
| 97 | // If Spectra Blocks are present in the form, enqueue the assets. |
| 98 | $post_assets_instance = new \UAGB_Post_Assets( $parsed_block['attrs']['id'] ); |
| 99 | $post_assets_instance->enqueue_scripts(); // This will enqueue the JS and CSS files. |
| 100 | |
| 101 | if ( ! empty( $post_assets_instance->file_generation ) && 'disabled' === $post_assets_instance->file_generation ) { |
| 102 | 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. |
| 103 | } |
| 104 | |
| 105 | return $parsed_block; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Filter SC Form Shortcode to load the Spectra Blocks Assets. |
| 110 | * |
| 111 | * @param string $output Content. |
| 112 | * @param array $attributes Shortcode attributes. |
| 113 | * @param string $name Shortcode Tag. |
| 114 | * |
| 115 | * @return array |
| 116 | */ |
| 117 | public function maybeEnqueueUAGBAssetsForShortcode( $output, $attributes, $name ) { |
| 118 | // UAGB must be activated. |
| 119 | if ( ! class_exists( '\UAGB_Post_Assets' ) ) { |
| 120 | return $output; |
| 121 | } |
| 122 | |
| 123 | // must be our form shortcode. |
| 124 | if ( 'sc_form' !== $name ) { |
| 125 | return $output; |
| 126 | } |
| 127 | |
| 128 | // must have an ID. |
| 129 | if ( empty( $attributes['id'] ) ) { |
| 130 | return $output; |
| 131 | } |
| 132 | |
| 133 | // If Spectra Blocks are present in the form, enqueue the assets. |
| 134 | $post_assets_instance = new \UAGB_Post_Assets( $attributes['id'] ); |
| 135 | $post_assets_instance->enqueue_scripts(); |
| 136 | |
| 137 | return $output; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Show the Gutenberg active notice. |
| 142 | * |
| 143 | * @return void |
| 144 | */ |
| 145 | public function gutenbergActiveNotice(): void { |
| 146 | if ( is_plugin_active( 'gutenberg/gutenberg.php' ) ) { |
| 147 | ( new AdminNoticesService() )->add( |
| 148 | [ |
| 149 | 'name' => 'gutenberg_active_notice', |
| 150 | 'type' => 'warning', |
| 151 | 'title' => esc_html__( 'SureCart', 'surecart' ), |
| 152 | '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' ) ), |
| 153 | ] |
| 154 | ); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 |