Skip to content
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

Make devicepx library an optional theme feature #10189

Merged
merged 22 commits into from
Dec 30, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
6 changes: 6 additions & 0 deletions 3rd-party/class.jetpack-amp-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ static function init() {
return;
}

/**
* Remove this during the init hook in case users have enabled it during
* the after_setup_theme hook, which triggers before init.
*/
remove_theme_support( 'jetpack-devicepx' );
kasparsd marked this conversation as resolved.
Show resolved Hide resolved

// enable stats
if ( Jetpack::is_module_active( 'stats' ) ) {
add_action( 'amp_post_template_footer', array( 'Jetpack_AMP_Support', 'add_stats_pixel' ) );
Expand Down
13 changes: 0 additions & 13 deletions class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,6 @@ private function __construct() {
add_action( 'wp_ajax_jetpack_connection_banner', array( $this, 'jetpack_connection_banner_callback' ) );

add_action( 'wp_loaded', array( $this, 'register_assets' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) );
add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) );

// gutenberg locale
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_gutenberg_locale' ) );
Expand Down Expand Up @@ -1096,16 +1093,6 @@ function get_locale() {
return $locale;
}

/**
* Device Pixels support
* This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers.
*/
function devicepx() {
kasparsd marked this conversation as resolved.
Show resolved Hide resolved
if ( Jetpack::is_active() && ! Jetpack_AMP_Support::is_amp_request() ) {
wp_enqueue_script( 'devicepx', 'https://s0.wp.com/wp-content/js/devicepx-jetpack.js', array(), gmdate( 'oW' ), true );
}
}

/**
* Return the network_site_url so that .com knows what network this site is a part of.
* @param bool $option
Expand Down
1 change: 1 addition & 0 deletions modules/module-extras.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'theme-tools/site-breadcrumbs.php',
'theme-tools/social-menu.php',
'theme-tools/content-options.php',
'theme-tools/devicepx.php',
'custom-post-types/comics.php',
'custom-post-types/testimonial.php',
'custom-post-types/nova.php',
Expand Down
35 changes: 35 additions & 0 deletions modules/theme-tools/devicepx.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Loads the devicepx library which improves the resolution of gravatars and
* wordpress.com uploads on hi-res and zoomed browsers.
*
* This feature will only be activated for themes that declare their support.
* This can be done by adding code similar to the following during the
* 'after_setup_theme' action:
*
* add_theme_support( 'jetpack-devicepx' );
*/

/**
* Add hooks to enqueue the devicepx JS library, if enabled. The feature must
* be enabled earlier during `after_setup_theme`.
*
* @uses current_theme_supports, add_action, Jetpack::is_active
*/
function jetpack_devicepx_init() {
if ( Jetpack::is_active() && current_theme_supports( 'jetpack-devicepx' ) ) {
kasparsd marked this conversation as resolved.
Show resolved Hide resolved
add_action( 'wp_enqueue_scripts', 'jetpack_devicepx_enqueue' );
add_action( 'customize_controls_enqueue_scripts', 'jetpack_devicepx_enqueue' );
add_action( 'admin_enqueue_scripts', 'jetpack_devicepx_enqueue' );
}
}
add_action( 'init', 'jetpack_devicepx_init', 99 );
kasparsd marked this conversation as resolved.
Show resolved Hide resolved

/**
* Enqueue the devicepx JS library.
*
* @uses wp_enqueue_script
*/
function jetpack_devicepx_enqueue() {
wp_enqueue_script( 'devicepx', 'https://s0.wp.com/wp-content/js/devicepx-jetpack.js', array(), gmdate( 'oW' ), true );
kasparsd marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
<testsuite name="sync">
<directory prefix="test_" suffix=".php">tests/php/sync</directory>
</testsuite>
<testsuite name="theme-tools">
<directory prefix="test_" suffix=".php">php/modules/theme-tools</directory>
</testsuite>
<testsuite name="uninstall">
<directory prefix="test_" suffix=".php">tests/php/uninstall</directory>
</testsuite>
Expand Down
3 changes: 3 additions & 0 deletions tests/php.multisite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<testsuite name="sync">
<directory prefix="test_" suffix=".php">php/sync</directory>
</testsuite>
<testsuite name="theme-tools">
<directory prefix="test_" suffix=".php">php/modules/theme-tools</directory>
</testsuite>
<testsuite name="uninstall">
<directory prefix="test_" suffix=".php">php/uninstall</directory>
</testsuite>
Expand Down
33 changes: 33 additions & 0 deletions tests/php/modules/theme-tools/test_functions.devicepx.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

require_once JETPACK__PLUGIN_DIR . '/modules/theme-tools/devicepx.php';

/**
* @covers jetpack_devicepx_init
*/
function test_devicepx_not_enqueued_by_default() {
$this->assertFalse( current_theme_supports( 'jetpack-devicepx' ) );
}

/**
* @covers jetpack_devicepx_init
*/
function test_devicepx_can_be_enabled() {
// Enable the feature.
add_theme_supports( 'jetpack-devicepx' );

$this->assertTrue(
current_theme_supports( 'jetpack-devicepx' ),
'Themes do not support devicepx by default'
);

jetpack_devicepx_init();

$this->assertTrue(
wp_script_is( 'jetpack-devicepx', 'enqueued' ),
'devicepx script has been enquued'
);

// Remove to not mess with other tests.
remove_theme_supports( 'jetpack-devicepx' );
}