Skip to content

Commit

Permalink
[not verified] Apply a similar change to the Comment Likes module
Browse files Browse the repository at this point in the history
Don't enqueue scripts if likes aren't enabled,
and move the dependencies to the footer,
as the script that depends on them,
'jetpack_likes_queuehandler',
is in the footer.
It doesn't seem necessary for these to be above
the footer.
  • Loading branch information
kienstra committed Mar 3, 2020
1 parent 716e19d commit 196c776
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
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
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_false' );
$instance = Jetpack_Comment_Likes::init();
$instance->load_styles_register_scripts();

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

0 comments on commit 196c776

Please sign in to comment.