Skip to content

Status: Add is_p2_site and get_wpcom_site_id helper functions to Status package #44512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Status: Add two new helper functions for p2 and wpcom site id checks, plus tests
28 changes: 28 additions & 0 deletions projects/packages/status/src/class-host.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,34 @@ public function is_wpcom_platform() {
return $this->is_wpcom_simple() || $this->is_woa_site();
}

/**
* Determine if this is a P2 site.
* This covers both P2 and P2020 themes.
*
* @return bool
*/
public function is_p2_site() {
$site_id = $this->get_wpcom_site_id();
if ( ! $site_id ) {
return false;
}
return str_contains( get_stylesheet(), 'pub/p2' ) || ( function_exists( '\WPForTeams\is_wpforteams_site' ) && \WPForTeams\is_wpforteams_site( $site_id ) );
}

/**
* Get the current site's WordPress.com ID.
*
* @return mixed The site's WordPress.com ID.
*/
public function get_wpcom_site_id() {
if ( $this->is_wpcom_simple() ) {
return get_current_blog_id();
} elseif ( class_exists( 'Jetpack' ) && \Jetpack::is_connection_ready() ) {
return \Jetpack_Options::get_option( 'id' );
}
return false;
}

/**
* Add all wordpress.com environments to the safe redirect allowed list.
*
Expand Down
54 changes: 53 additions & 1 deletion projects/packages/status/tests/php/Host_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ private function setup_atomic_constants() {
Constants::set_constant( 'ATOMIC_SITE_ID', 999 );
}

/**
* Sets up the site as a WPCOM Simple site.
*/
private function setup_wpcom_simple_constants() {
Constants::set_constant( 'IS_WPCOM', true );
}

/**
* Tests if WoA Site based on constant
*/
Expand Down Expand Up @@ -102,7 +109,7 @@ public function test_false_for_not_atomic() {
* Tests if a Simple Site based on constant
*/
public function test_simple_site_based_on_constant() {
Constants::set_constant( 'IS_WPCOM', true );
$this->setup_wpcom_simple_constants();
$this->assertTrue( $this->host_obj->is_wpcom_simple() );
$this->assertTrue( $this->host_obj->is_wpcom_platform() );
}
Expand Down Expand Up @@ -176,4 +183,49 @@ public static function get_source_query_params() {
'invalid' => array( 'invalid-param', '' ),
);
}

/**
* Tests that is_p2_site() returns true when the stylesheet contains 'pub/p2'.
*/
public function test_is_p2_site_true_if_stylesheet_contains_pub_p2() {
// Make is_wpcom_simple true so get_wpcom_site_id returns a value.
$this->setup_wpcom_simple_constants();
Functions\when( 'get_stylesheet' )->justReturn( 'pub/p2-theme' );
$this->assertTrue( $this->host_obj->is_p2_site() );
}

/**
* Tests that is_p2_site() returns true when the WPForTeams function exists and returns true.
*/
public function test_is_p2_site_true_if_wpforteams_function_exists_and_true() {
// Mock get_wpcom_site_id to ensure we are testing all existing functions within is_p2_site().
$host = $this->getMockBuilder( Host::class )
->onlyMethods( array( 'get_wpcom_site_id' ) )
->getMock();
$host->method( 'get_wpcom_site_id' )->willReturn( 123 );

Functions\when( 'get_stylesheet' )->justReturn( 'not-p2-theme' );
Functions\when( 'function_exists' )->alias(
function ( $fn ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
return $fn === '\WPForTeams\is_wpforteams_site';
}
);
Functions\when( '\WPForTeams\is_wpforteams_site' )->justReturn( true );

$this->assertTrue( $host->is_p2_site() );
}

/**
* Tests that is_p2_site() returns false when neither the stylesheet nor the WPForTeams function indicate a P2 site.
*/
public function test_is_p2_site_false_if_no_conditions_met() {
$this->setup_wpcom_simple_constants();
Functions\when( 'get_stylesheet' )->justReturn( 'not-p2-theme' );
Functions\when( 'function_exists' )->alias(
function ( $fn ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
return false;
}
);
$this->assertFalse( $this->host_obj->is_p2_site() );
}
}
9 changes: 8 additions & 1 deletion projects/packages/status/tests/php/patchwork.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{
"redefinable-internals": [ "defined", "constant", "headers_sent", "headers_list", "header" ]
"redefinable-internals": [
"defined",
"constant",
"headers_sent",
"headers_list",
"header",
"function_exists"
]
}
Loading