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

In Likes and Comment Likes modules, prevent blocking page load with 2 scripts #14841

Merged
merged 4 commits into from
Mar 5, 2020
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
8 changes: 6 additions & 2 deletions modules/comment-likes.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ public function frontend_init() {
}

public function load_styles_register_scripts() {
if ( ! $this->settings->is_likes_visible() ) {
return;
}

if ( ! wp_style_is( 'open-sans', 'registered' ) ) {
wp_register_style( 'open-sans', 'https://fonts.googleapis.com/css?family=Open+Sans', array(), JETPACK__VERSION );
}
Expand All @@ -138,7 +142,7 @@ public function load_styles_register_scripts() {
Assets::get_file_url_for_environment( '_inc/build/postmessage.min.js', '_inc/postmessage.js' ),
array( 'jquery' ),
JETPACK__VERSION,
false
true
);
wp_enqueue_script(
'jetpack_resize',
Expand All @@ -148,7 +152,7 @@ public function load_styles_register_scripts() {
),
array( 'jquery' ),
JETPACK__VERSION,
false
true
);
wp_enqueue_script( 'jetpack_likes_queuehandler', plugins_url( 'likes/queuehandler.js' , __FILE__ ), array( 'jquery', 'postmessage', 'jetpack_resize' ), JETPACK__VERSION, true );
}
Expand Down
10 changes: 5 additions & 5 deletions modules/likes.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ function admin_init() {
}

function action_init() {
if ( is_admin() ) {
if ( is_admin() || ! $this->settings->is_likes_visible() ) {
return;
}

Expand All @@ -280,8 +280,8 @@ class_exists( 'Jetpack_AMP_Support' )
add_filter( 'post_flair', array( &$this, 'post_likes' ), 30, 1 );
add_filter( 'post_flair_block_css', array( $this, 'post_flair_service_enabled_like' ) );

wp_enqueue_script( 'postmessage', '/wp-content/js/postmessage.js', array( 'jquery' ), JETPACK__VERSION, false );
wp_enqueue_script( 'jetpack_resize', '/wp-content/js/jquery/jquery.jetpack-resize.js', array( 'jquery' ), JETPACK__VERSION, false );
wp_enqueue_script( 'postmessage', '/wp-content/js/postmessage.js', array( 'jquery' ), JETPACK__VERSION, true );
wp_enqueue_script( 'jetpack_resize', '/wp-content/js/jquery/jquery.jetpack-resize.js', array( 'jquery' ), JETPACK__VERSION, true );
wp_enqueue_script( 'jetpack_likes_queuehandler', plugins_url( 'queuehandler.js' , __FILE__ ), array( 'jquery', 'postmessage', 'jetpack_resize' ), JETPACK__VERSION, true );
kraftbj marked this conversation as resolved.
Show resolved Hide resolved
wp_enqueue_style( 'jetpack_likes', plugins_url( 'jetpack-likes.css', __FILE__ ), array(), JETPACK__VERSION );
}
Expand All @@ -296,7 +296,7 @@ function register_scripts() {
Assets::get_file_url_for_environment( '_inc/build/postmessage.min.js', '_inc/postmessage.js' ),
array( 'jquery' ),
JETPACK__VERSION,
false
true
);
wp_register_script(
'jetpack_resize',
Expand All @@ -306,7 +306,7 @@ function register_scripts() {
),
array( 'jquery' ),
JETPACK__VERSION,
false
true
);
wp_register_script(
'jetpack_likes_queuehandler',
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<testsuite name="infinite-scroll">
<directory prefix="test_" suffix=".php">tests/php/modules/infinite-scroll</directory>
</testsuite>
<testsuite name="comment-likes">
<directory prefix="test" suffix=".php">tests/php/modules/comment-likes</directory>
</testsuite>
<testsuite name="likes">
<directory prefix="test_" suffix=".php">tests/php/modules/likes</directory>
</testsuite>
Expand Down
44 changes: 44 additions & 0 deletions tests/php/modules/comment-likes/test-class.comment-likes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Tests for the WP_Test_Comment_Likes class.
*
* @package Jetpack
* @since 8.4.0
*/

require dirname( __FILE__ ) . '/../../../../modules/comment-likes.php';

/**
* Test class for Jetpack_Comment_Likes.
*
* @since 8.4.0
*/
class WP_Test_Comment_Likes extends WP_UnitTestCase {

/**
* Test that the assets are not enqueued if likes are not visible.
*
* @since 8.4.0
*/
public function test_load_styles_register_scripts_likes_not_visible() {
$instance = Jetpack_Comment_Likes::init();
$instance->load_styles_register_scripts();

$this->assertFalse( wp_style_is( 'jetpack_likes' ) );
$this->assertFalse( wp_script_is( 'postmessage' ) );
}

/**
* Test that the assets are enqueued if likes are visible.
*
* @since 8.4.0
*/
public function test_load_styles_register_scripts_likes_visible() {
add_filter( 'wpl_is_likes_visible', '__return_true' );
$instance = Jetpack_Comment_Likes::init();
$instance->load_styles_register_scripts();

$this->assertTrue( wp_style_is( 'jetpack_likes' ) );
$this->assertTrue( wp_script_is( 'postmessage' ) );
}
}
29 changes: 29 additions & 0 deletions tests/php/modules/likes/test_class.likes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@

class WP_Test_Likes extends WP_UnitTestCase {

/**
* Test that the actions are not added if likes are not visible.
*
* @since 8.4.0
*/
public function test_action_init_likes_not_visible() {
$instance = new Jetpack_Likes();
$instance->action_init();

$this->assertFalse( has_filter( 'the_content', array( $instance, 'post_likes' ) ) );
$this->assertFalse( has_filter( 'the_excerpt', array( $instance, 'post_likes' ) ) );
}

/**
* Test that the actions are added if likes are visible.
*
* @since 8.4.0
*/
public function test_action_init_likes_visible() {
$this->go_to( get_permalink( $this->factory()->post->create() ) );
add_filter( 'wpl_is_enabled_sitewide', '__return_true' );
add_filter( 'wpl_is_single_post_disabled', '__return_true' );
$instance = new Jetpack_Likes();
$instance->action_init();

$this->assertEquals( 30, has_filter( 'the_content', array( $instance, 'post_likes' ) ) );
$this->assertEquals( 30, has_filter( 'the_excerpt', array( $instance, 'post_likes' ) ) );
}

/**
* Test if likes are rendered correctly.
*
Expand Down