WC_REST_System_Status_V2_Controller::get_pagespublicWC 1.0

Returns a mini-report on WC pages and if they are configured correctly: Present, visible, and including the correct shortcode or block.

Method of the class: WC_REST_System_Status_V2_Controller{}

Returns

Array.

Usage

$WC_REST_System_Status_V2_Controller = new WC_REST_System_Status_V2_Controller();
$WC_REST_System_Status_V2_Controller->get_pages();

WC_REST_System_Status_V2_Controller::get_pages() code WC 10.3.6

public function get_pages() {
	// WC pages to check against.
	$check_pages = array(
		_x( 'Shop base', 'Page setting', 'woocommerce' ) => array(
			'option' => 'woocommerce_shop_page_id',
		),
		_x( 'Cart', 'Page setting', 'woocommerce' ) => array(
			'option'             => 'woocommerce_cart_page_id',
			'shortcode'          => '[' . apply_filters_deprecated( 'woocommerce_cart_shortcode_tag', array( 'woocommerce_cart' ), '8.3.0', 'woocommerce_create_pages' ) . ']',
			'block'              => 'woocommerce/cart',
			'shortcode_callback' => function ( $page ) {
				if ( $page ) {
					$shortcode = apply_filters_deprecated( 'woocommerce_cart_shortcode_tag', array( 'woocommerce_cart' ), '8.3.0', 'woocommerce_create_pages' );
					if ( has_shortcode( $page->post_content, $shortcode ) ) {
						return $shortcode;
					}
				}
				return '';
			},
			'block_callback'     => function ( $page ) {
				if ( $page ) {
					if ( has_block( 'woocommerce/cart', $page->post_content ) ) {
						return 'woocommerce/cart';
					}
					if ( CartCheckoutUtils::has_block_variation( 'woocommerce/classic-shortcode', 'shortcode', 'cart', $page->post_content ) ) {
						return 'woocommerce/classic-shortcode';
					}
				}
				return '';
			},
		),
		_x( 'Checkout', 'Page setting', 'woocommerce' ) => array(
			'option'             => 'woocommerce_checkout_page_id',
			'shortcode'          => '[' . apply_filters_deprecated( 'woocommerce_checkout_shortcode_tag', array( 'woocommerce_checkout' ), '8.3.0', 'woocommerce_create_pages' ) . ']',
			'block'              => 'woocommerce/checkout',
			'shortcode_callback' => function ( $page ) {
				if ( $page ) {
					$shortcode = apply_filters_deprecated( 'woocommerce_checkout_shortcode_tag', array( 'woocommerce_checkout' ), '8.3.0', 'woocommerce_create_pages' );
					if ( has_shortcode( $page->post_content, $shortcode ) ) {
						return $shortcode;
					}
				}
				return '';
			},
			'block_callback'     => function ( $page ) {
				if ( $page ) {
					if ( has_block( 'woocommerce/checkout', $page->post_content ) ) {
						return 'woocommerce/checkout';
					}
					if ( CartCheckoutUtils::has_block_variation( 'woocommerce/classic-shortcode', 'shortcode', 'checkout', $page->post_content ) ) {
						return 'woocommerce/classic-shortcode';
					}
				}
				return '';
			},
		),
		_x( 'My account', 'Page setting', 'woocommerce' ) => array(
			'option'    => 'woocommerce_myaccount_page_id',
			'shortcode' => '[' . apply_filters( 'woocommerce_my_account_shortcode_tag', 'woocommerce_my_account' ) . ']',
		),
		_x( 'Terms and conditions', 'Page setting', 'woocommerce' ) => array(
			'option' => 'woocommerce_terms_page_id',
		),
	);

	$pages_output = array();
	foreach ( $check_pages as $page_name => $values ) {
		$page_id            = get_option( $values['option'] );
		$page_set           = false;
		$page_exists        = false;
		$page_visible       = false;
		$shortcode_present  = false;
		$shortcode_required = false;
		$block_present      = false;
		$block_required     = false;
		$page               = false;
		$block              = '';
		$shortcode          = '';

		// Page checks.
		if ( $page_id ) {
			$page_set = true;
			$page     = get_post( $page_id );

			if ( $page ) {
				$page_exists = true;

				if ( 'publish' === $page->post_status ) {
					$page_visible = true;
				}
			}
		}

		// Shortcode checks.
		if ( $page && isset( $values['shortcode_callback'], $values['shortcode'] ) ) {
			$shortcode_required = true;
			$result             = $values['shortcode_callback']( $page );
			$shortcode          = $result ? $result : $values['shortcode'];
			$shortcode_present  = (bool) $result;
		} elseif ( $page && isset( $values['shortcode'] ) ) {
			$shortcode          = $values['shortcode'];
			$shortcode_required = true;
			$shortcode_present  = has_shortcode( $page->post_content, trim( $shortcode, '[]' ) );
		}

		// Block checks.
		if ( $page && isset( $values['block_callback'], $values['block'] ) ) {
			$block_required = true;
			$result         = $values['block_callback']( $page );
			$block          = $result ? $result : $values['block'];
			$block_present  = (bool) $result;
		} elseif ( $page && isset( $values['block'] ) ) {
			$block          = $values['block'];
			$block_required = true;
			$block_present  = has_block( $block, $page->post_content );
		}

		// Wrap up our findings into an output array.
		$pages_output[] = array(
			'page_name'          => $page_name,
			'page_id'            => $page_id,
			'page_set'           => $page_set,
			'page_exists'        => $page_exists,
			'page_visible'       => $page_visible,
			'shortcode'          => $shortcode,
			'block'              => $block,
			'shortcode_required' => $shortcode_required,
			'shortcode_present'  => $shortcode_present,
			'block_present'      => $block_present,
			'block_required'     => $block_required,
		);
	}

	return $pages_output;
}