feat(hero): add isServicesDescendant() helper and bypass constant

This commit is contained in:
Keith Solomon
2026-07-04 13:54:27 -05:00
parent 6d62bd73ae
commit a163b349e1
+61
View File
@@ -77,6 +77,67 @@ function hasPageHeader() {
return true;
}
/**
* Bypass constant for the inner-page hero ancestor check.
*
* When `true`, `isServicesDescendant()` returns `true` for every page.
* Set this in test setup (e.g. in `wp-config.php` or a test bootstrap) so
* test fixture pages that are not Services descendants can still render
* the hero.
*/
if ( ! defined( 'CWC_TEST_BYPASS_HERO_GATE' ) ) {
define( 'CWC_TEST_BYPASS_HERO_GATE', false );
}
/**
* Checks if the current page is the Services page, a child of it, or
* a grandchild of it. Used to gate the inner-page hero.
*
* @param WP_Post|null $post The post to check. Defaults to the global `$post`.
* @param int|null $services_id Override the Services page ID. Defaults to
* the page with slug `services`.
*
* @return bool true if the post is the Services page or a descendant up to 2 levels.
*/
function isServicesDescendant( $post = null, $services_id = null ) {
if ( CWC_TEST_BYPASS_HERO_GATE ) {
return true;
}
if ( null === $post ) {
global $post;
}
if ( ! $post instanceof WP_Post ) {
return false;
}
if ( null === $services_id ) {
$services = get_page_by_path( 'services' );
if ( ! $services ) {
return false;
}
$services_id = (int) $services->ID;
}
$services_id = (int) $services_id;
if ( (int) $post->ID === $services_id ) {
return true;
}
if ( (int) $post->post_parent === $services_id ) {
return true;
}
$parent = get_post( $post->post_parent );
if ( $parent && (int) $parent->post_parent === $services_id ) {
return true;
}
return false;
}
/** Create the Owner role.
*
* This function creates a new role named "Owner" with all the capabilities of the