Skip to content
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
19 changes: 15 additions & 4 deletions .github/workflows/playground-preview-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,20 @@ jobs:
BLUEPRINT_RTC=$(transform "$PLUGIN_URL" "$SEEDER_URL" "$HELPER_URL" blueprint-rtc.json)
BLUEPRINT_MS_RTC=$(transform "$PLUGIN_URL" "$SEEDER_URL" "$HELPER_URL" blueprint-multisite-rtc.json)

# The settings buttons are the post-locks sites, landing on the
# screens that hold the Presence switches instead of the post list.
BLUEPRINT_SETTINGS=$(jq '.landingPage = "/wp-admin/options-general.php"' <<< "$BLUEPRINT")
BLUEPRINT_MS_SETTINGS=$(jq '.landingPage = "/wp-admin/network/settings.php"' <<< "$BLUEPRINT_MS")

encode() { printf '%s' "$1" | base64 -w0; }

{
echo "url=https://playground.wordpress.net/#$(encode "$BLUEPRINT")"
echo "url_multisite=https://playground.wordpress.net/#$(encode "$BLUEPRINT_MS")"
echo "url_rtc=https://playground.wordpress.net/#$(encode "$BLUEPRINT_RTC")"
echo "url_multisite_rtc=https://playground.wordpress.net/#$(encode "$BLUEPRINT_MS_RTC")"
echo "url_settings=https://playground.wordpress.net/#$(encode "$BLUEPRINT_SETTINGS")"
echo "url_multisite_settings=https://playground.wordpress.net/#$(encode "$BLUEPRINT_MS_SETTINGS")"
} >> "$GITHUB_OUTPUT"

- name: Post or update sticky comment
Expand All @@ -200,13 +207,17 @@ jobs:
PLAYGROUND_URL_MULTISITE: ${{ steps.playground.outputs.url_multisite }}
PLAYGROUND_URL_RTC: ${{ steps.playground.outputs.url_rtc }}
PLAYGROUND_URL_MULTISITE_RTC: ${{ steps.playground.outputs.url_multisite_rtc }}
PLAYGROUND_URL_SETTINGS: ${{ steps.playground.outputs.url_settings }}
PLAYGROUND_URL_MULTISITE_SETTINGS: ${{ steps.playground.outputs.url_multisite_settings }}
with:
script: |
const marker = '<!-- presence-api:playground-preview -->';
const url = process.env.PLAYGROUND_URL;
const urlMultisite = process.env.PLAYGROUND_URL_MULTISITE;
const urlRtc = process.env.PLAYGROUND_URL_RTC;
const urlMultisiteRtc = process.env.PLAYGROUND_URL_MULTISITE_RTC;
const urlSettings = process.env.PLAYGROUND_URL_SETTINGS;
const urlMultisiteSettings = process.env.PLAYGROUND_URL_MULTISITE_SETTINGS;
const fullSha = process.env.HEAD_SHA;
const sha = fullSha.slice(0, 7);
const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${fullSha}`;
Expand All @@ -226,10 +237,10 @@ jobs:
// emits, which the timeline's sha links are not.
`Built from <a href="${commitUrl}"><samp>${sha}</samp></a>`,
'',
'| | Post locks | RTC |',
'| --- | --- | --- |',
`| **Single site** | ${launch(url, 'Launch post locks, single site')} | ${launch(urlRtc, 'Launch RTC, single site')} |`,
`| **Multisite** | ${launch(urlMultisite, 'Launch post locks, multisite')} | ${launch(urlMultisiteRtc, 'Launch RTC, multisite')} |`,
'| | Post locks | RTC | Settings |',
'| --- | --- | --- | --- |',
`| **Single site** | ${launch(url, 'Launch post locks, single site')} | ${launch(urlRtc, 'Launch RTC, single site')} | ${launch(urlSettings, 'Launch settings, single site')} |`,
`| **Multisite** | ${launch(urlMultisite, 'Launch post locks, multisite')} | ${launch(urlMultisiteRtc, 'Launch RTC, multisite')} | ${launch(urlMultisiteSettings, 'Launch network settings, multisite')} |`,
'',
].join('\n');

Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ add_filter( 'wp_presence_recording_enabled', '__return_false' );

On multisite, `wp_presence_network_recording_enabled` does the same for every site, defaulting to the **Presence** checkbox on Network Admin > Settings. Either switch turning recording off wins.

#### `wp_presence_feature_enabled`
Whether a piece of the plugin is on for this site, passed the feature key. Default: its checkbox under **Presence** on Settings > General, on until switched off. On multisite, the same checkbox on Network Admin > Settings switches a feature off for every site. Hooks are registered when the plugin loads, so add this from a must-use plugin. Only `post-locks` can be switched off so far; the rest of the pieces follow in [#710](https://github.com/WordPress/presence-api/issues/710).
```php
add_filter( 'wp_presence_feature_enabled', function ( $enabled, $feature ) {
return 'post-locks' === $feature ? false : $enabled;
}, 10, 2 );
```

#### `wp_presence_is_agent_user`
Filters whether a user is an AI agent, for labelling its presence rows. Default: `false`, deferred to `wpai_is_agent_user()` when it is loaded (see [Agents](#agents)).

Expand Down Expand Up @@ -486,7 +494,7 @@ A consumer like Gutenberg's sync poll loop ([presence-api#444](https://github.co

## Post-lock bridge

Keeps `_edit_lock` in the post room's `_lock` row instead of post meta, so refreshing a lock no longer makes cached post queries stale. Meta short-circuits keep `wp_check_post_lock()` and other callers working, and it applies to every post type even with recording off.
Keeps `_edit_lock` in the post room's `_lock` row instead of post meta, so refreshing a lock no longer makes cached post queries stale. Meta short-circuits keep `wp_check_post_lock()` and other callers working, and it applies to every post type even with recording off. Switch off **Post locks** under **Presence** on Settings > General and locks stay in post meta, as core keeps them.

## Capability

Expand Down
16 changes: 10 additions & 6 deletions plugins/presence-api/includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@
add_filter( 'heartbeat_received', 'wp_presence_admin_heartbeat_received', 9, 3 );
add_filter( 'heartbeat_received', 'wp_presence_online_hash_heartbeat_received', 10, 2 );
add_filter( 'heartbeat_received', 'wp_presence_editor_heartbeat_received', 10, 3 );
add_filter( 'heartbeat_received', 'wp_presence_bridge_post_lock', 11, 3 );
add_filter( 'heartbeat_received', 'wp_presence_screen_heartbeat_received', 12, 3 );
add_filter( 'heartbeat_received', 'wp_presence_prime_heartbeat_locks', 5, 2 );
add_filter( 'get_post_metadata', 'wp_presence_get_post_lock', 10, 4 );
add_filter( 'the_posts', 'wp_presence_prime_post_list_locks', 10, 2 );
add_filter( 'update_post_metadata', 'wp_presence_update_post_lock', 10, 5 );
add_filter( 'delete_post_metadata', 'wp_presence_delete_post_lock', 10, 5 );
add_filter( 'site_status_tests', 'wp_presence_site_status_tests' );

// Plugin only: the check comes off when core merges the piece.
if ( wp_presence_feature_enabled( 'post-locks' ) ) {
Comment thread
josephfusco marked this conversation as resolved.
Resolved
add_filter( 'heartbeat_received', 'wp_presence_prime_heartbeat_locks', 5, 2 );
add_filter( 'heartbeat_received', 'wp_presence_bridge_post_lock', 11, 3 );
add_filter( 'get_post_metadata', 'wp_presence_get_post_lock', 10, 4 );
add_filter( 'the_posts', 'wp_presence_prime_post_list_locks', 10, 2 );
add_filter( 'update_post_metadata', 'wp_presence_update_post_lock', 10, 5 );
add_filter( 'delete_post_metadata', 'wp_presence_delete_post_lock', 10, 5 );
}

add_action( 'admin_enqueue_scripts', 'wp_presence_enqueue_stale_screen_banner' );
add_action( 'added_option', 'wp_presence_on_updated_option' );
add_action( 'updated_option', 'wp_presence_on_updated_option' );
Expand Down
133 changes: 133 additions & 0 deletions plugins/presence-api/includes/features.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
/**
* Features: the pieces of the plugin a site can switch off while core adopts
* them one at a time.
*
* Plugin only. Core has no switch for its own features, so when a piece is
* merged its entry here and the check around its hooks in default-filters.php
* go with it.
*
* Shaped like Gutenberg's experiments (lib/experimental/experiments/load.php):
* one list declared in code, one option keyed by feature, one function to ask.
* Unlike an experiment, a feature is on until someone switches it off, so a
* piece added in a later release starts on for every site that already has
* the option.
*
* @package Presence_API
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Returns the pieces a site can switch off, keyed by feature.
*
* Each piece is added here in the same change that puts a check around its
* hooks, so a checkbox never appears for something it cannot turn off.
*
* Only call this once translations can load (init or later). Gating hooks at
* load time goes through wp_presence_feature_enabled(), which never needs the
* labels.
*
* @access private
*
* @since 0.15.0
*
* @return array<string, array{label: string, description: string}> Features keyed by feature.
*/
function wp_presence_get_features() {
return array(
'post-locks' => array(
'label' => __( 'Post locks', 'presence-api' ),
'description' => __( 'Keep post locks in the presence table instead of post meta, so refreshing a lock does not make cached post queries stale.', 'presence-api' ),
),
);
}

/**
* Whether a piece of the plugin is switched on for this site.
*
* A feature with no stored choice is on. On multisite, the network can switch
* a feature off for every site, and a site cannot switch it back on, the same
* way the recording switch works.
*
* Hooks are registered once, when the plugin loads, so the answer for the
* site a request starts on holds for the whole request, including any
* switch_to_blog() inside it.
*
* Unlike wp_presence_recording_enabled(), switching a feature off does not
* stop rows being written: plugins that keep their own rows in the table only
* call the functions every feature is built on, and those are never switched
* off.
*
* @since 0.15.0
*
* @param string $feature Feature key from wp_presence_get_features().
* @return bool Whether the feature is on.
*/
function wp_presence_feature_enabled( $feature ) {
$enabled = wp_presence_feature_stored_choice( get_option( 'wp_presence_features', array() ), $feature );

if ( $enabled && is_multisite() ) {
$enabled = wp_presence_feature_stored_choice( get_site_option( 'wp_presence_network_features', array() ), $feature );
}

/**
* Filters whether a piece of the plugin is switched on for this site.
*
* Read when the plugin loads, so a callback has to be added before then,
* from a must-use plugin or an earlier-loading plugin.
*
* @since 0.15.0
*
* @param bool $enabled Whether the feature is on. Default is the stored
* choice, true when there is none.
* @param string $feature Feature key.
*/
return (bool) apply_filters( 'wp_presence_feature_enabled', $enabled, $feature );
}

/**
* Reads one feature's choice out of a stored option, on when it holds none.
*
* @access private
*
* @since 0.15.0
*
* @param mixed $stored The stored option value.
* @param string $feature Feature key.
* @return bool Whether the stored choice leaves the feature on.
*/
function wp_presence_feature_stored_choice( $stored, $feature ) {
if ( ! is_array( $stored ) || ! array_key_exists( $feature, $stored ) ) {
return true;
}

return (bool) $stored[ $feature ];
}

/**
* Casts submitted feature checkboxes to the 1 or 0 the option stores.
*
* Every known feature is written, on or off, so an unchecked box is stored as
* off rather than missing, and missing keeps meaning "never chosen". Keys that
* are not features are dropped.
*
* @access private
*
* @since 0.15.0
*
* @param mixed $value The submitted value.
* @return array<string, int> Each feature's choice, 1 for on and 0 for off.
*/
function wp_presence_sanitize_features( $value ) {
$value = is_array( $value ) ? $value : array();
$sanitized = array();

foreach ( array_keys( wp_presence_get_features() ) as $feature ) {
$sanitized[ $feature ] = empty( $value[ $feature ] ) ? 0 : 1;
}

return $sanitized;
}
6 changes: 6 additions & 0 deletions plugins/presence-api/includes/post-lock-bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ function wp_presence_delete_post_lock( $check, $post_id, $meta_key, $meta_value,
function wp_presence_prime_post_locks( $post_ids ) {
global $wpdb, $_wp_presence_post_locks;

// Only wp_presence_get_post_lock() reads what this primes, so with it
// unhooked the locks come from post meta and the query would be wasted.
if ( ! has_filter( 'get_post_metadata', 'wp_presence_get_post_lock' ) ) {
return;
}

$rooms = array();

foreach ( $post_ids as $post_id ) {
Expand Down
69 changes: 68 additions & 1 deletion plugins/presence-api/includes/settings.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* Settings: the recording switch on Settings > General and Network Settings.
* Settings: the recording and feature switches on Settings > General and
* Network Settings.
*
* @package Presence_API
*/
Expand Down Expand Up @@ -28,6 +29,17 @@ function wp_presence_register_settings() {
)
);

register_setting(
'general',
'wp_presence_features',
array(
'type' => 'object',
'default' => array(),
'sanitize_callback' => 'wp_presence_sanitize_features',
'show_in_rest' => false,
)
);

add_settings_field(
'wp_presence_recording',
__( 'Presence', 'presence-api' ),
Expand Down Expand Up @@ -74,6 +86,45 @@ function wp_presence_render_recording_field() {
<?php esc_html_e( 'Presence rows expire on their own, so switching this off empties every presence screen within a few minutes.', 'presence-api' ); ?>
</p>
<?php
wp_presence_render_feature_checkboxes( 'wp_presence_features', get_option( 'wp_presence_features', array() ) );
}

/**
* Renders a checkbox per feature, posted as an array under one option name.
*
* Each box has a hidden 0 before it, so an unchecked box still posts and is
* stored as off rather than missing, which reads as on.
*
* @access private
*
* @since 0.15.0
*
* @param string $name The option name the boxes post under.
* @param mixed $stored The stored option value.
*/
function wp_presence_render_feature_checkboxes( $name, $stored ) {
$features = wp_presence_get_features();

if ( empty( $features ) ) {
return;
}
?>
<fieldset>
<legend class="screen-reader-text"><?php esc_html_e( 'Presence features', 'presence-api' ); ?></legend>
<?php foreach ( $features as $feature => $details ) : ?>
<?php
$field_name = $name . '[' . $feature . ']';
$description_id = $name . '-' . $feature . '-description';
?>
<input type="hidden" name="<?php echo esc_attr( $field_name ); ?>" value="0" />
<label>
<input type="checkbox" name="<?php echo esc_attr( $field_name ); ?>" value="1" aria-describedby="<?php echo esc_attr( $description_id ); ?>" <?php checked( wp_presence_feature_stored_choice( $stored, $feature ) ); ?> />
<?php echo esc_html( $details['label'] ); ?>
</label>
<p class="description" id="<?php echo esc_attr( $description_id ); ?>"><?php echo esc_html( $details['description'] ); ?></p>
<?php endforeach; ?>
</fieldset>
<?php
}

/**
Expand Down Expand Up @@ -103,6 +154,17 @@ function wp_presence_render_network_settings() {
</p>
</td>
</tr>
<?php if ( wp_presence_get_features() ) : ?>
<tr>
<th scope="row"><?php esc_html_e( 'Features', 'presence-api' ); ?></th>
<td>
<?php wp_presence_render_feature_checkboxes( 'wp_presence_network_features', get_site_option( 'wp_presence_network_features', array() ) ); ?>
<p class="description">
<?php esc_html_e( 'Switching a feature off here turns it off on every site, whatever an individual site has chosen.', 'presence-api' ); ?>
</p>
</td>
</tr>
<?php endif; ?>
</table>
<?php
}
Expand All @@ -121,4 +183,9 @@ function wp_presence_save_network_settings() {
$submitted = ! empty( $_POST['wp_presence_network_recording'] );

update_site_option( 'wp_presence_network_recording', wp_presence_sanitize_checkbox( $submitted ) );

// phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Verified by core in wp-admin/network/settings.php; sanitized to known keys and 0 or 1 below.
$features = isset( $_POST['wp_presence_network_features'] ) ? wp_unslash( $_POST['wp_presence_network_features'] ) : array();

update_site_option( 'wp_presence_network_features', wp_presence_sanitize_features( $features ) );
}
2 changes: 2 additions & 0 deletions plugins/presence-api/presence-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ function () {
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/user-list.php';
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/post-list.php';
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/widgets/class-wp-presence-widget-active-posts.php';
// Plugin only, ahead of the hooks: decides which switchable pieces they register.
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/features.php';
require_once WP_PRESENCE_PLUGIN_DIR . 'includes/default-filters.php';

if ( is_multisite() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function tear_down() {
// after it the statement lands in a fresh transaction and is rolled back.
delete_option( 'wp_presence_recording' );
delete_site_option( 'wp_presence_network_recording' );
delete_option( 'wp_presence_features' );
delete_site_option( 'wp_presence_network_features' );
unset( $GLOBALS['_wp_presence_post_locks'] );

// phpcs:ignore WordPress.DB.DirectDatabaseQuery
Expand Down
Loading
Loading