Skip to content

Commit 60d4341

Browse files
authored
Merge pull request #521 from Codeinwp/fixes
Fixes
2 parents fbecc6e + 66e9c5a commit 60d4341

File tree

10 files changed

+139
-23
lines changed

10 files changed

+139
-23
lines changed

assets/vue/components/lazyload.vue

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
<toggle-button :class="'has-text-dark'"
1212
v-model="lazyloadPlaceholder"
1313
:disabled="this.$store.state.loading"
14-
:width="37"
15-
:height="20"
16-
color="#577BF9"></toggle-button>
14+
:width="37"
15+
:height="20"
16+
color="#577BF9"></toggle-button>
1717
</div>
1818
</div>
19-
<hr/>
19+
<hr/>
2020

2121
<div class="field columns optml-flex-column" >
2222
<label class="label column optml-custom-label-margin">
@@ -35,7 +35,7 @@
3535
</div>
3636

3737
</div>
38-
<hr/>
38+
<hr/>
3939
<!--Native lazy toggle-->
4040
<div class="field is-fullwidth columns">
4141
<label class="label column has-text-grey-dark optml-custom-label-margin">
@@ -50,12 +50,12 @@
5050
<toggle-button :class="'has-text-dark'"
5151
v-model="nativeLazyStatus"
5252
:disabled="this.$store.state.loading"
53-
:width="37"
54-
:height="20"
55-
color="#577BF9"></toggle-button>
53+
:width="37"
54+
:height="20"
55+
color="#577BF9"></toggle-button>
5656
</div>
5757
</div>
58-
<hr/>
58+
<hr/>
5959
<div class="field is-fullwidth columns">
6060
<label class="label column has-text-grey-dark optml-custom-label-margin">
6161
{{strings.toggle_scale}}
@@ -69,12 +69,12 @@
6969
<toggle-button :class="'has-text-dark'"
7070
v-model="scaleStatus"
7171
:disabled="this.$store.state.loading"
72-
:width="37"
73-
:height="20"
74-
color="#577BF9"></toggle-button>
72+
:width="37"
73+
:height="20"
74+
color="#577BF9"></toggle-button>
7575
</div>
7676
</div>
77-
<hr/>
77+
<hr/>
7878
<div class="field columns">
7979
<label class="label column has-text-grey-dark optml-custom-label-margin">
8080
{{strings.enable_bg_lazyload_title}}
@@ -86,12 +86,12 @@
8686
<toggle-button :class="'has-text-dark'"
8787
v-model="lazyloadBgImages"
8888
:disabled="this.$store.state.loading"
89-
:width="37"
90-
:height="20"
91-
color="#577BF9"></toggle-button>
89+
:width="37"
90+
:height="20"
91+
color="#577BF9"></toggle-button>
9292
</div>
9393
</div>
94-
<hr/>
94+
<hr/>
9595
<!--Video lazyload toggle-->
9696
<div class="field columns">
9797
<label class="label column has-text-grey-dark optml-custom-label-margin">
@@ -104,12 +104,30 @@
104104
<toggle-button :class="'has-text-dark'"
105105
v-model="lazyloadVideo"
106106
:disabled="this.$store.state.loading"
107-
:width="37"
108-
:height="20"
109-
color="#577BF9"></toggle-button>
107+
:width="37"
108+
:height="20"
109+
color="#577BF9"></toggle-button>
110110
</div>
111111
</div>
112-
<hr/>
112+
<hr/>
113+
<!--Noscript toggle-->
114+
<div class="field columns">
115+
<label class="label column has-text-grey-dark optml-custom-label-margin">
116+
{{strings.enable_noscript_title}}
117+
<p class="optml-settings-desc-margin has-text-weight-normal">
118+
{{strings.enable_noscript_desc}}
119+
</p>
120+
</label>
121+
<div class="column is-1">
122+
<toggle-button :class="'has-text-dark'"
123+
v-model="noscriptToggle"
124+
:disabled="this.$store.state.loading"
125+
:width="37"
126+
:height="20"
127+
color="#577BF9"></toggle-button>
128+
</div>
129+
</div>
130+
<hr/>
113131
<div class="field columns" v-if="showBgSelectors">
114132
<div class="column">
115133
<label class="label has-text-grey-dark optml-custom-label-margin">
@@ -200,6 +218,15 @@
200218
return !(this.site_settings.video_lazyload === 'disabled');
201219
}
202220
},
221+
noscriptToggle: {
222+
set: function (value) {
223+
this.showSave = true;
224+
this.new_data.no_script = value ? 'enabled' : 'disabled';
225+
},
226+
get: function () {
227+
return !(this.site_settings.no_script === 'disabled');
228+
}
229+
},
203230
lazyloadSelectors: {
204231
set: function ( value ) {
205232
this.showSave = true;

inc/admin.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function __construct() {
3838
add_action( 'init', [$this, 'check_domain_change'] );
3939
}
4040
add_action( 'admin_init', [ $this, 'maybe_redirect' ] );
41+
add_action( 'admin_init', [ $this, 'init_no_script' ] );
4142
if ( ! is_admin() && $this->settings->is_connected() && ! wp_next_scheduled( 'optml_daily_sync' ) ) {
4243
wp_schedule_event( time() + 10, 'daily', 'optml_daily_sync', [] );
4344
}
@@ -48,6 +49,17 @@ public function __construct() {
4849
}
4950
}
5051

52+
/**
53+
* Init no_script setup value based on whether the user is connected or not.
54+
*/
55+
public function init_no_script() {
56+
if ( $this->settings->is_connected() ) {
57+
$raw_settings = $this->settings->get_raw_settings();
58+
if ( ! isset( $raw_settings['no_script'] ) ) {
59+
$this->settings->update( 'no_script', 'enabled' );
60+
}
61+
}
62+
}
5163
/**
5264
* Checks if domain has changed
5365
*/
@@ -890,6 +902,8 @@ private function get_dashboard_strings() {
890902
'enable_bg_lazyload_title' => __( 'Enable lazyload for background images', 'optimole-wp' ),
891903
'enable_video_lazyload_desc' => __( 'Lazyload iframes/videos', 'optimole-wp' ),
892904
'enable_video_lazyload_title' => __( 'Enable lazyload for embedded videos and iframes.', 'optimole-wp' ),
905+
'enable_noscript_desc' => __( 'The noscript tag offers fallback images for browsers that can\'t handle JavaScript-based lazy loading or related features. Disabling it may resolve conflicts with other plugins or configurations and decrease HTML page size.', 'optimole-wp' ),
906+
'enable_noscript_title' => __( 'Enable noscript tag', 'optimole-wp' ),
893907
'enable_gif_replace_title' => __( 'Enable Gif to Video conversion', 'optimole-wp' ),
894908
'enable_report_title' => __( 'Enable error diagnosis tool', 'optimole-wp' ),
895909
'enable_report_desc' => __( 'Provides a troubleshooting mechanism which should help you identify any possible issues with your site using Optimole.', 'optimole-wp' ),

inc/lazyload_replacer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,9 @@ public function get_svg_for( $width, $height, $url = null ) {
467467
* @return bool Should add?
468468
*/
469469
public function should_add_noscript( $tag ) {
470+
if ( $this->settings->get( 'no_script' ) === 'disabled' ) {
471+
return false;
472+
}
470473
foreach ( self::get_ignore_noscript_flags() as $banned_string ) {
471474
if ( strpos( $tag, $banned_string ) !== false ) {
472475
return false;

inc/main.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ final class Optml_Main {
2323
*/
2424
public $manager;
2525

26+
/**
27+
* Holds the media_offload class.
28+
*
29+
* @access public
30+
* @since 1.0.0
31+
* @var Optml_Media_Offload instance.
32+
*/
33+
public $media_offload;
34+
2635
/**
2736
* Holds the rest class.
2837
*

inc/settings.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Optml_Settings {
4141
* @var boolean Whether or not the auto connect action is hooked.
4242
*/
4343
private static $auto_connect_hooked = false;
44+
4445
/**
4546
* Default settings schema.
4647
*
@@ -61,9 +62,10 @@ class Optml_Settings {
6162
'network_optimization' => 'disabled',
6263
'lazyload_placeholder' => 'disabled',
6364
'bg_replacer' => 'enabled',
64-
'video_lazyload' => 'disabled',
65+
'video_lazyload' => 'enabled',
6566
'retina_images' => 'disabled',
6667
'resize_smart' => 'disabled',
68+
'no_script' => 'disabled',
6769
'filters' => [],
6870
'cloud_sites' => [ 'all' => 'true' ],
6971
'watchers' => '',
@@ -105,6 +107,7 @@ class Optml_Settings {
105107
* Optml_Settings constructor.
106108
*/
107109
public function __construct() {
110+
108111
$this->namespace = OPTML_NAMESPACE . '_settings';
109112
$this->default_schema = apply_filters( 'optml_default_settings', $this->default_schema );
110113
$this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema );
@@ -240,6 +243,7 @@ public function parse_settings( $new_settings ) {
240243
case 'css_minify':
241244
case 'js_minify':
242245
case 'native_lazyload':
246+
case 'no_script':
243247
$sanitized_value = $this->to_map_values( $value, [ 'enabled', 'disabled' ], 'enabled' );
244248
break;
245249
case 'max_width':
@@ -419,6 +423,7 @@ public function get_site_settings() {
419423
'bg_replacer' => $this->get( 'bg_replacer' ),
420424
'video_lazyload' => $this->get( 'video_lazyload' ),
421425
'resize_smart' => $this->get( 'resize_smart' ),
426+
'no_script' => $this->get( 'no_script' ),
422427
'image_replacer' => $this->get( 'image_replacer' ),
423428
'cdn' => $this->get( 'cdn' ),
424429
'max_width' => $this->get( 'max_width' ),
@@ -596,5 +601,13 @@ public function reset() {
596601

597602
return $update;
598603
}
604+
/**
605+
* Get raw settings value.
606+
*
607+
* @return array
608+
*/
609+
public function get_raw_settings() {
610+
return get_option( $this->namespace, false );
611+
}
599612

600613
}

phpcs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<ruleset name="Optimole">
33
<description>Optimole rules for PHP_CodeSnifferr</description>
4-
4+
<ini name="error_reporting" value="E_ALL &#38; ~E_DEPRECATED" />
55
<file>.</file>
66

77
<exclude-pattern>node_modules/*</exclude-pattern>

tests/test-lazyload-class-exclusion.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function setUp() : void
2727
]);
2828

2929
$settings->update('lazyload', 'enabled');
30+
$settings->update('no_script', 'enabled');
3031
$settings->update('filters', array(
3132
Optml_Settings::FILTER_TYPE_LAZYLOAD => array (
3233
Optml_Settings::FILTER_CLASS => array (

tests/test-lazyload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function setUp() : void {
3636
$settings->update( 'lazyload', 'enabled' );
3737
$settings->update( 'native_lazyload', 'enabled' );
3838
$settings->update( 'video_lazyload', 'enabled' );
39+
$settings->update( 'no_script', 'enabled' );
3940
Optml_Url_Replacer::instance()->init();
4041
Optml_Tag_Replacer::instance()->init();
4142
Optml_Lazyload_Replacer::instance()->init();

tests/test-media.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public function setUp() : void {
139139
'whitelist' => [ 'example.com', 'example.org' ],
140140

141141
] );
142+
$settings->update( 'no_script', 'enabled' );
142143
$settings->update( 'lazyload', 'enabled' );
143144
$settings->update( 'offload_media', 'enabled' );
144145
$settings->update( 'quality', 90 );

tests/test-no-script-disabled.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* WordPress unit test plugin.
4+
*
5+
* @package Optimole-WP
6+
* @subpackage Tests
7+
* @copyright Copyright (c) 2017, ThemeIsle
8+
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9+
*/
10+
11+
/**
12+
* Class Test_Generic.
13+
*/
14+
class Test_Lazyload_No_Script extends WP_UnitTestCase
15+
{
16+
17+
public function setUp(): void
18+
{
19+
parent::setUp();
20+
$settings = new Optml_Settings();
21+
$settings->update('service_data', [
22+
'cdn_key' => 'test123',
23+
'cdn_secret' => '12345',
24+
'whitelist' => ['example.com'],
25+
26+
]);
27+
$settings->update('lazyload', 'enabled');
28+
$settings->update('native_lazyload', 'enabled');
29+
$settings->update('video_lazyload', 'enabled');
30+
$settings->update('no_script', 'disabled');
31+
Optml_Url_Replacer::instance()->init();
32+
Optml_Tag_Replacer::instance()->init();
33+
Optml_Lazyload_Replacer::instance()->init();
34+
Optml_Manager::instance()->init();
35+
}
36+
37+
//changing the setting to disabled in a test inside the lazyload class will not work due to test concurrency
38+
//moved this to a different test
39+
public function test_no_script_disabled()
40+
{
41+
$text = ' <a href="http://example.org/wp-content/uploads/2018/06/start-a-blog-1-5.png"><img class="alignnone wp-image-36442 size-full" src="http://example.org/wp-content/uploads/2018/06/start-a-blog-1-5.png" srcset="testsrcset" data-srcset="another" data-plugin-src="http://example.org/wp-content/uploads/2018/06/start-a-blog-1-5.png" alt="How to monetize a blog" width="490" height="256"></a>';
42+
43+
$replaced_content = Optml_Manager::instance()->replace_content($text);
44+
45+
$this->assertStringNotContainsString('noscript', $replaced_content);
46+
}
47+
}

0 commit comments

Comments
 (0)