Add block/shortcode usage to Cart and Checkout pages in system report (#48300)
* Refactored get_pages() to reduce the amount of get_post() used, avoid unnecessary ifs, and fixed a bug where the classic shortcode block would trigger a false block usage value. * Added block/shortcode usage to the system report for the Cart & Checkout pages. Also added a warning when a page contains both experiences. * Added support info for templates out of sync not loading the page content.
This commit is contained in:
parent
29e5c96a5a
commit
5b2b83cd71
|
@ -0,0 +1,4 @@
|
|||
Significance: minor
|
||||
Type: enhancement
|
||||
|
||||
Add information about block/shortcode/template usage on Cart and Checkout pages to the WC system report.
|
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
use Automattic\Jetpack\Constants;
|
||||
use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils;
|
||||
use Automattic\WooCommerce\Utilities\RestApiUtil;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
@ -873,10 +874,38 @@ if ( 0 < $mu_plugins_count ) :
|
|||
echo '<mark class="error"><span class="dashicons dashicons-warning"></span> ' . ( $_page['block_required'] ? sprintf( esc_html__( 'Page does not contain the %1$s shortcode or the %2$s block.', 'woocommerce' ), esc_html( $_page['shortcode'] ), esc_html( $_page['block'] ) ) : sprintf( esc_html__( 'Page does not contain the %s shortcode.', 'woocommerce' ), esc_html( $_page['shortcode'] ) ) ) . '</mark>'; /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */
|
||||
$found_error = true;
|
||||
}
|
||||
|
||||
// Warn merchants if both the shortcode and block are present, which will be a confusing shopper experience.
|
||||
if ( $_page['shortcode_present'] && $_page['block_present'] ) {
|
||||
/* Translators: %1$s: shortcode text, %2$s: block slug. */
|
||||
echo '<mark class="error"><span class="dashicons dashicons-warning"></span> ' . sprintf( esc_html__( 'Page contains both the %1$s shortcode and the %2$s block.', 'woocommerce' ), esc_html( $_page['shortcode'] ), esc_html( $_page['block'] ) ) . '</mark>'; /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */
|
||||
$found_error = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $found_error ) {
|
||||
echo '<mark class="yes">#' . absint( $_page['page_id'] ) . ' - ' . esc_html( str_replace( home_url(), '', get_permalink( $_page['page_id'] ) ) ) . '</mark>';
|
||||
|
||||
$additional_info = '';
|
||||
|
||||
// We only state the used type on the Checkout and the Cart page.
|
||||
if ( in_array( $_page['block'], array( 'woocommerce/checkout', 'woocommerce/cart' ), true ) ) {
|
||||
// We check first if, in a blocks theme, the template content does not load the page content.
|
||||
if ( CartCheckoutUtils::is_overriden_by_custom_template_content( $_page['block'] ) ) {
|
||||
$additional_info = __( "This page's content is overridden by custom template content", 'woocommerce' );
|
||||
} elseif ( $_page['shortcode_present'] ) {
|
||||
/* Translators: %1$s: shortcode text. */
|
||||
$additional_info = sprintf( __( 'Contains the <strong>%1$s</strong> shortcode', 'woocommerce' ), esc_html( $_page['shortcode'] ) );
|
||||
} elseif ( $_page['block_present'] ) {
|
||||
/* Translators: %1$s: block slug. */
|
||||
$additional_info = sprintf( __( 'Contains the <strong>%1$s</strong> block', 'woocommerce' ), esc_html( $_page['block'] ) );
|
||||
}
|
||||
|
||||
if ( ! empty( $additional_info ) ) {
|
||||
$additional_info = '<mark class="no"> - <span class="dashicons dashicons-info"></span> ' . $additional_info . '</mark>';
|
||||
}
|
||||
}
|
||||
|
||||
echo '<mark class="yes">#' . absint( $_page['page_id'] ) . ' - ' . esc_html( str_replace( home_url(), '', get_permalink( $_page['page_id'] ) ) ) . '</mark>' . $additional_info; /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */
|
||||
}
|
||||
|
||||
echo '</td></tr>';
|
||||
|
|
|
@ -1407,36 +1407,39 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
|
|||
$shortcode_required = false;
|
||||
$block_present = false;
|
||||
$block_required = false;
|
||||
$page = false;
|
||||
|
||||
// Page checks.
|
||||
if ( $page_id ) {
|
||||
$page_set = true;
|
||||
}
|
||||
if ( get_post( $page_id ) ) {
|
||||
$page = get_post( $page_id );
|
||||
|
||||
if ( $page ) {
|
||||
$page_exists = true;
|
||||
}
|
||||
if ( 'publish' === get_post_status( $page_id ) ) {
|
||||
|
||||
if ( 'publish' === $page->post_status ) {
|
||||
$page_visible = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Shortcode checks.
|
||||
if ( $values['shortcode'] && get_post( $page_id ) ) {
|
||||
if ( $values['shortcode'] && $page ) {
|
||||
$shortcode_required = true;
|
||||
$page = get_post( $page_id );
|
||||
if ( strstr( $page->post_content, $values['shortcode'] ) ) {
|
||||
if ( has_shortcode( $page->post_content, trim( $values['shortcode'], '[]' ) ) ) {
|
||||
$shortcode_present = true;
|
||||
}
|
||||
|
||||
// Compatibility with the classic shortcode block which can be used instead of shortcodes.
|
||||
if ( ! $shortcode_present && ( 'woocommerce/checkout' === $values['block'] || 'woocommerce/cart' === $values['block'] ) ) {
|
||||
$shortcode_present = has_block( 'woocommerce/classic-shortcode', $page->post_content );
|
||||
}
|
||||
}
|
||||
|
||||
// Block checks.
|
||||
if ( $values['block'] && get_post( $page_id ) ) {
|
||||
if ( $values['block'] && $page ) {
|
||||
$block_required = true;
|
||||
$block_present = WC_Blocks_Utils::has_block_in_page( $page_id, $values['block'] );
|
||||
|
||||
// Compatibility with the classic shortcode block which can be used instead of shortcodes.
|
||||
if ( ! $block_present && ( 'woocommerce/checkout' === $values['block'] || 'woocommerce/cart' === $values['block'] ) ) {
|
||||
$block_present = WC_Blocks_Utils::has_block_in_page( $page_id, 'woocommerce/classic-shortcode', true );
|
||||
}
|
||||
$block_present = has_block( $values['block'], $page->post_content );
|
||||
}
|
||||
|
||||
// Wrap up our findings into an output array.
|
||||
|
|
|
@ -44,6 +44,32 @@ class CartCheckoutUtils {
|
|||
return $checkout_page_id && has_block( 'woocommerce/checkout', $checkout_page_id );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the template overriding the page loads the page content or not.
|
||||
* Templates by default load the page content, but if that block is deleted the content can get out of sync with the one presented in the page editor.
|
||||
*
|
||||
* @param string $block The block to check.
|
||||
*
|
||||
* @return bool true if the template has out of sync content.
|
||||
*/
|
||||
public static function is_overriden_by_custom_template_content( string $block ): bool {
|
||||
|
||||
$block = str_replace( 'woocommerce/', '', $block );
|
||||
|
||||
if ( wc_current_theme_is_fse_theme() ) {
|
||||
$templates_from_db = BlockTemplateUtils::get_block_templates_from_db( array( 'page-' . $block ) );
|
||||
foreach ( $templates_from_db as $template ) {
|
||||
if ( ! has_block( 'woocommerce/page-content-wrapper', $template->content ) ) {
|
||||
// Return true if the template does not load the page content via the woocommerce/page-content-wrapper block.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets country codes, names, states, and locale information.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue