\n[woocommerce_checkout]\n"; /** * Hooks. */ public static function init(): void { add_action( 'admin_init', array( self::class, 'sync' ) ); add_action( 'aiolos_settings_saved', array( self::class, 'sync' ) ); add_action( 'admin_notices', array( self::class, 'notice' ) ); } /** * The checkout page id. */ private static function page_id(): int { return function_exists( 'wc_get_page_id' ) ? max( 0, (int) wc_get_page_id( 'checkout' ) ) : 0; } /** * Whether a page's content holds the Checkout block and not the shortcode. * * @param string $content Content. */ public static function is_block( string $content ): bool { return has_block( 'woocommerce/checkout', $content ) && ! has_shortcode( $content, 'woocommerce_checkout' ); } /** * Whether the checkout page shows WooCommerce's Checkout block (Aiolos Checkout is off, or in * test mode, and the page holds the block). */ public static function uses_block(): bool { static $uses = null; if ( null === $uses ) { $page = self::page_id(); $uses = $page && self::is_block( (string) get_post_field( 'post_content', $page ) ); } return $uses; } /** * Whether customers see Aiolos Checkout. */ private static function wanted(): bool { return Settings::enabled( 'checkout_enabled' ) && ! Settings::enabled( 'checkout_admins_only' ); } /** * Switches or restores the page as the settings need. */ public static function sync(): void { // Only someone who may edit the page, on a screen (not on every admin-ajax or heartbeat call). if ( wp_doing_ajax() || ! current_user_can( 'edit_pages' ) ) { return; } $page = self::page_id(); if ( ! $page ) { return; } $content = (string) get_post_field( 'post_content', $page ); if ( self::wanted() ) { if ( self::is_block( $content ) ) { self::switch_to_shortcode( $page, $content ); } return; } self::restore(); } /** * Puts the shortcode in place of the block, keeping the block content. * * @param int $page Page id. * @param string $content Current content. */ public static function switch_to_shortcode( int $page, string $content ): void { update_post_meta( $page, self::BACKUP, wp_slash( $content ) ); $done = wp_update_post( array( 'ID' => $page, 'post_content' => self::SHORTCODE, ), true ); if ( ! is_wp_error( $done ) ) { update_option( self::SWITCHED, $page, false ); update_option( self::NOTICE, 'switched', false ); } } /** * Brings the block back if this plugin switched the page and it was not edited since. */ public static function restore(): void { $page = (int) get_option( self::SWITCHED, 0 ); if ( ! $page ) { return; } $backup = (string) get_post_meta( $page, self::BACKUP, true ); $current = trim( (string) get_post_field( 'post_content', $page ) ); delete_option( self::SWITCHED ); if ( '' === $backup || trim( self::SHORTCODE ) !== $current ) { return; } $done = wp_update_post( array( 'ID' => $page, 'post_content' => wp_slash( $backup ), ), true ); if ( ! is_wp_error( $done ) ) { update_option( self::NOTICE, 'restored', false ); } } /** * Says once what was done. */ public static function notice(): void { $what = (string) get_option( self::NOTICE, '' ); if ( '' === $what || ! current_user_can( 'manage_woocommerce' ) ) { return; } delete_option( self::NOTICE ); $text = 'switched' === $what ? __( 'Your checkout page used the Checkout block. So that every payment gateway works in Aiolos Checkout, it now holds the classic checkout shortcode. The block comes back by itself if you turn Aiolos Checkout off.', 'aiolos-checkout-for-woocommerce' ) : __( 'Aiolos Checkout is off, so your checkout page has its Checkout block back.', 'aiolos-checkout-for-woocommerce' ); echo '
' . esc_html__( 'Aiolos Checkout', 'aiolos-checkout-for-woocommerce' ) . ': ' . esc_html( $text ) . '