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
43 changes: 26 additions & 17 deletions client-side-media-experiments.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,33 @@
exit;
}

// Bail if client-side media processing is not enabled.
if ( function_exists( 'wp_is_client_side_media_processing_enabled' ) ) {
if ( ! wp_is_client_side_media_processing_enabled() ) {
return;
}
} elseif ( function_exists( 'gutenberg_is_client_side_media_processing_enabled' ) ) {
if ( ! gutenberg_is_client_side_media_processing_enabled() ) {
return;
}
} else {
// Neither core 7.0+ nor Gutenberg with the feature — nothing to do.
return;
}

define( 'CSME_VERSION', '0.2.0' );
define( 'CSME_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'CSME_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

require_once CSME_PLUGIN_DIR . 'includes/settings.php';
require_once CSME_PLUGIN_DIR . 'includes/cross-origin-isolation.php';
require_once CSME_PLUGIN_DIR . 'includes/heic-support.php';
/**
* Initializes the plugin once all plugins are loaded.
*
* Deferred to plugins_loaded so the Gutenberg plugin's functions are
* available regardless of plugin load order.
*/
function csme_init() {
// Bail if client-side media processing is not enabled.
if ( function_exists( 'wp_is_client_side_media_processing_enabled' ) ) {
if ( ! wp_is_client_side_media_processing_enabled() ) {
return;
}
} elseif ( function_exists( 'gutenberg_is_client_side_media_processing_enabled' ) ) {
if ( ! gutenberg_is_client_side_media_processing_enabled() ) {
return;
}
} else {
// Neither core 7.1+ nor Gutenberg with the feature - nothing to do.
return;
}

require_once CSME_PLUGIN_DIR . 'includes/settings.php';
require_once CSME_PLUGIN_DIR . 'includes/cross-origin-isolation.php';
require_once CSME_PLUGIN_DIR . 'includes/heic-support.php';
}
add_action( 'plugins_loaded', 'csme_init' );
14 changes: 7 additions & 7 deletions includes/cross-origin-isolation.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
* @return bool
*/
function csme_should_use_coep_coop() {
$chrome_version = null;
$chromium_version = null;

if ( function_exists( 'wp_get_chrome_major_version' ) ) {
$chrome_version = wp_get_chrome_major_version();
} elseif ( function_exists( 'gutenberg_get_chrome_major_version' ) ) {
$chrome_version = gutenberg_get_chrome_major_version();
if ( function_exists( 'wp_get_chromium_major_version' ) ) {
$chromium_version = wp_get_chromium_major_version();
} elseif ( function_exists( 'gutenberg_get_chromium_major_version' ) ) {
$chromium_version = gutenberg_get_chromium_major_version();
}

// DIP is used on Chrome 137+. Only use COEP/COOP when DIP is NOT active.
$use_dip = null !== $chrome_version && $chrome_version >= 137;
// DIP is used on Chromium 137+. Only use COEP/COOP when DIP is NOT active.
$use_dip = null !== $chromium_version && $chromium_version >= 137;

/**
* Filters whether to use COEP/COOP for cross-origin isolation.
Expand Down
33 changes: 7 additions & 26 deletions includes/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,20 @@
exit;
}

/**
* Returns the default value for the csme_enabled setting based on browser type.
*
* Chromium 137+ uses Document-Isolation-Policy, so COEP/COOP headers should be
* off by default. For all other browsers, the headers should be on by default.
*
* @return int 1 if COEP/COOP should be enabled by default, 0 otherwise.
*/
function csme_get_enabled_default() {
$chrome_version = null;

if ( function_exists( 'wp_get_chrome_major_version' ) ) {
$chrome_version = wp_get_chrome_major_version();
} elseif ( function_exists( 'gutenberg_get_chrome_major_version' ) ) {
$chrome_version = gutenberg_get_chrome_major_version();
}

// Chromium 137+ uses Document-Isolation-Policy; COEP/COOP headers are not needed.
$use_dip = null !== $chrome_version && $chrome_version >= 137;

return $use_dip ? 0 : 1;
}

/**
* Registers the plugin settings on the Media settings page.
*
* The csme_enabled option defaults to on; whether COEP/COOP headers are
* actually sent for a given request is decided at runtime by
* csme_should_use_coep_coop(), which detects DIP-capable browsers.
*/
function csme_register_settings() {
register_setting(
'media',
'csme_enabled',
array(
'type' => 'integer',
'default' => csme_get_enabled_default(),
'default' => 1,
'sanitize_callback' => 'csme_sanitize_enabled',
)
);
Expand Down Expand Up @@ -105,7 +86,7 @@ function csme_settings_section_callback() {
* Outputs the checkbox field for the enabled setting.
*/
function csme_enabled_field_callback() {
$enabled = get_option( 'csme_enabled', csme_get_enabled_default() );
$enabled = get_option( 'csme_enabled', 1 );
?>
<input type="hidden" name="csme_enabled" value="0" />
<label for="csme_enabled">
Expand Down Expand Up @@ -139,7 +120,7 @@ function csme_heic_enabled_field_callback() {
* @return bool Filtered value.
*/
function csme_maybe_disable_coep_coop( $use_coep_coop ) {
if ( ! get_option( 'csme_enabled', csme_get_enabled_default() ) ) {
if ( ! get_option( 'csme_enabled', 1 ) ) {
return false;
}
return $use_coep_coop;
Expand Down
59 changes: 0 additions & 59 deletions tests/Test_Get_Enabled_Default.php

This file was deleted.

51 changes: 51 additions & 0 deletions tests/Test_Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Tests for the plugin settings registration.
*
* @package ClientSideMediaExperiments
*/

class Test_Settings extends WP_UnitTestCase {

/**
* Tear down after each test.
*/
public function tear_down() {
delete_option( 'csme_enabled' );
remove_all_filters( 'csme_use_coep_coop' );
parent::tear_down();
}

/**
* The csme_enabled option defaults to 1 regardless of browser.
*
* Browser (DIP) detection happens at runtime in csme_should_use_coep_coop(),
* so the persisted option default must not depend on the User-Agent.
*/
public function test_enabled_option_defaults_to_1() {
csme_register_settings();

$registered = get_registered_settings();

$this->assertArrayHasKey( 'csme_enabled', $registered );
$this->assertSame( 1, $registered['csme_enabled']['default'] );
}

/**
* csme_maybe_disable_coep_coop() returns false when the option is off.
*/
public function test_option_off_disables_coep_coop() {
update_option( 'csme_enabled', 0 );

$this->assertFalse( csme_should_use_coep_coop() );
}

/**
* csme_maybe_disable_coep_coop() passes the value through when the option is on.
*/
public function test_option_on_keeps_coep_coop() {
update_option( 'csme_enabled', 1 );

$this->assertTrue( csme_should_use_coep_coop() );
}
}
41 changes: 33 additions & 8 deletions tests/Test_Should_Use_Coep_Coop.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,46 @@ public function tear_down() {
}

/**
* Returns true when no Chrome version function exists (Firefox/Safari).
* Returns true when no Chromium version function exists (Firefox/Safari).
*/
public function test_returns_true_when_no_chrome_version_function() {
// Neither wp_get_chrome_major_version nor gutenberg_get_chrome_major_version
public function test_returns_true_when_no_chromium_version_function() {
// Neither wp_get_chromium_major_version nor gutenberg_get_chromium_major_version
// should exist in the test environment by default.
$this->assertTrue( csme_should_use_coep_coop() );
}

/**
* Returns false when Chrome version is 137+.
* Returns false when Chromium version is 137+ (DIP is used).
*/
public function test_returns_false_when_chrome_137_or_higher() {
// Create a stub that returns 140 (>= 137, so DIP is used).
if ( ! function_exists( 'wp_get_chrome_major_version' ) ) {
function wp_get_chrome_major_version() {
public function test_returns_false_when_chromium_137_or_higher() {
if ( ! function_exists( 'wp_get_chromium_major_version' ) ) {
function wp_get_chromium_major_version() {
return 140;
}
}

$this->assertFalse( csme_should_use_coep_coop() );
}

/**
* Returns true when Chromium version is below 137 (no DIP support).
*/
public function test_returns_true_when_chromium_below_137() {
if ( ! function_exists( 'wp_get_chromium_major_version' ) ) {
function wp_get_chromium_major_version() {
return 130;
}
}

$this->assertTrue( csme_should_use_coep_coop() );
}

/**
* Falls back to the Gutenberg plugin's version function when core's is absent.
*/
public function test_falls_back_to_gutenberg_version_function() {
if ( ! function_exists( 'gutenberg_get_chromium_major_version' ) ) {
function gutenberg_get_chromium_major_version() {
return 140;
}
}
Expand Down
Loading