Skip to content
Open
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: minor
Type: other

Atomic: Modify `/sites/%s` endpoint to rely on the privacy model, and auto-adjust local 'blog_public' option to match privacy model.
11 changes: 11 additions & 0 deletions projects/plugins/jetpack/sal/class.json-api-site-jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,19 @@ public function allowed_file_types() {
* Return site's privacy status.
*
* @return bool Is site private?
* @phan-suppress PhanUndeclaredFunction
*/
public function is_private() {
if ( ! ( new Host() )->is_woa_site() ) {
return false;
}

// Making sure wpcomsh plugin is available.
if ( function_exists( '\Private_Site\maybe_fix_private_site_option' ) && function_exists( '\Private_Site\site_is_private' ) ) {
\Private_Site\maybe_fix_private_site_option();
return \Private_Site\site_is_private();
}

return (int) $this->get_atomic_cloud_site_option( 'blog_public' ) === -1;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Atomic: Auto-adjust 'blog_public' property to match privacy model.
59 changes: 59 additions & 0 deletions projects/plugins/wpcomsh/private-site/private-site.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Private_Site;

use Automattic\Jetpack\Connection\Rest_Authentication;
use Automattic\Jetpack\Status\Host;
use Jetpack;
use WP_Error;
use WP_REST_Request;
Expand Down Expand Up @@ -260,6 +261,64 @@ function site_is_private() {
return defined( 'AT_PRIVACY_MODEL' ) && AT_PRIVACY_MODEL === 'wp_uploads';
}

/**
* Update blog_private option to match the site's actual privacy status.
* Sets it to -1 for private sites, 1 for public sites.
*/
function maybe_fix_private_site_option() {
// Privacy model only applies to WordPress.com Atomic sites.
if ( ! ( new Host() )->is_woa_site() ) {
return;
}

// Coming soon sites are a special case of public and private sites, so we skip them here.
if ( site_is_coming_soon() || site_is_public_coming_soon() ) {
return;
}

$is_private = site_is_private();
$current_value = (int) get_option( 'blog_public' );

// No action needed if site is public and option is already set to 0 (discourage indexing).
if ( ! $is_private && $current_value === 0 ) {
return;
}

$expected_value = $is_private ? -1 : 1;

if ( $current_value !== $expected_value ) {
update_option( 'blog_public', $expected_value );
register_shutdown_function( __NAMESPACE__ . '\private_site_log_option_adjusted', $current_value, $expected_value );
}
}

/**
* Save a log entry when the blog_public option is adjusted.
*
* @param int $current_value Current option value.
* @param int $new_value New option value.
*
* @return void
*/
function private_site_log_option_adjusted( $current_value, $new_value ) {
$jetpack_options = get_option( 'jetpack_options' );
$blog_id = ( is_array( $jetpack_options ) && ! empty( $jetpack_options['id'] ) ) ? $jetpack_options['id'] : 0;

$data = wp_json_encode(
array(
'feature' => 'atomic_private_site_blog_public_adjusted',
'message' => 'Value of blog_public option adjusted according to the privacy model.',
'blog_id' => $blog_id,
'properties' => array(
'current_value' => $current_value,
'new_value' => $new_value,
),
)
);

wp_remote_post( 'https://public-api.wordpress.com/rest/v1.1/logstash', array( 'body' => array( 'params' => $data ) ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is interesting, I see the same approach to logging was used in another location in wpcomsh as well. Do you think that the number of log entries will be reasonable? Also, what is the purpose of logging the value changes - is it to help debug issues that might arise on individual sites? We could see whether the option was updated via sync, but not what value at what time, so I guess if the history was important it makes sense to log. 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just for general monitoring and potential debugging, we'll see if we actually need the data.
I don't expect the number to be too big. And since only Atomic is affected, it all should work pretty fast.
Removing it wouldn't be hard either, since Atomic runs on fresh Jetpack versions.

}

/**
* Determine if site access should be blocked for various types of requests.
* This function is cached for subsequent calls so we can use it gratuitously.
Expand Down
Loading