Completed
Pull Request — master (#254)
by
unknown
02:09
created

autoptimizeConfig::__construct()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 13
nop 0
dl 0
loc 34
rs 8.4426
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
3
4
class autoptimizeConfig
5
{
6
    private $config          = null;
7
    static private $instance = null;
8
9
    private $settings_screen_do_remote_http = true;
10
11
    /**
12
     * Singleton.
13
     */
14
    private function __construct()
15
    {
16
        if ( is_admin() ) {
17
            // Add the admin page and settings.
18
			if ( is_multisite() && is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) ) {
19
				add_action( 'network_admin_menu', array( $this, 'addmenu' ) );
20
			} else {
21
				add_action( 'admin_menu', array( $this, 'addmenu' ) );
22
			}
23
            add_action( 'admin_init', array( $this, 'registersettings' ) );
24
25
            // Set meta info.
26
            if ( function_exists( 'plugin_row_meta' ) ) {
27
                // 2.8 and higher.
28
                add_filter( 'plugin_row_meta', array( $this, 'setmeta' ), 10, 2 );
29
            } elseif ( function_exists( 'post_class' ) ) {
30
                // 2.7 and lower.
31
                $plugin = plugin_basename( AUTOPTIMIZE_PLUGIN_DIR . 'autoptimize.php' );
32
                add_filter( 'plugin_action_links_' . $plugin, array( $this, 'setmeta' ) );
33
            }
34
35
            // Clean cache?
36
            if ( autoptimizeOption::get_option( 'autoptimize_cache_clean' ) ) {
37
                autoptimizeCache::clearall();
38
                autoptimizeOption::update_option( 'autoptimize_cache_clean', 0 );
39
            }
40
41
            $this->settings_screen_do_remote_http = apply_filters( 'autoptimize_settingsscreen_remotehttp', $this->settings_screen_do_remote_http );
42
        }
43
44
        // Adds the Autoptimize Toolbar to the Admin bar.
45
        // (we load outside the is_admin check so it's also displayed on the frontend toolbar).
46
        $toolbar = new autoptimizeToolbar();
0 ignored issues
show
Unused Code introduced by
$toolbar is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
47
    }
48
49
    /**
50
     * @return autoptimizeConfig
51
     */
52
    static public function instance()
53
    {
54
        // Only one instance.
55
        if ( null === self::$instance ) {
56
            self::$instance = new autoptimizeConfig();
57
        }
58
59
        return self::$instance;
60
    }
61
62
    public function show()
63
    {
64
        $conf = self::instance();
65
?>
66
<style>
67
/* title and button */
68
#ao_title_and_button:after {content:''; display:block; clear:both;}
69
#ao_adv_button{float:right;}
70
#ao_hide_adv:before, #ao_show_adv:before {
71
    display: inline-block;
72
    float: left;
73
    height: 20px;
74
    width: 35px;
75
    background: none;
76
    color: #b4b9be;
77
    font: normal 20px/26px dashicons;
78
    letter-spacing: -4px;
79
    text-align: left;
80
    speak: none;
81
    -webkit-font-smoothing: antialiased;
82
    -moz-osx-font-smoothing: grayscale;
83
}
84
#ao_hide_adv:before {
85
    content: "\f108 \f142";
86
}
87
#ao_show_adv:before {
88
    content: "\f108 \f140";
89
}
90
91
/* animate "show adv" button */
92
#ao_show_adv { animation: watchmenow 3s linear 5s 10; }
93
#ao_show_adv:hover { animation: none; }
94
@keyframes watchmenow {
95
  0% { box-shadow: unset; }
96
  100% { box-shadow: 0px 0px 20px yellow; }
97
}
98
99
/* form */
100
.itemDetail {
101
    background: #fff;
102
    border: 1px solid #ccc;
103
    padding: 15px;
104
    margin: 15px 10px 10px 0;
105
}
106
.itemTitle {
107
    margin-top: 0;
108
}
109
110
input[type=url]:invalid {color: red; border-color:red;} .form-table th{font-weight:normal;}
111
#autoptimize_main .cb_label {display: block; padding-left: 25px; text-indent: -25px;}
112
113
/* rss block */
114
#futtta_feed ul{list-style:outside;}
115
#futtta_feed {font-size:medium; margin:0px 20px;}
116
117
/* banner + unslider */
118
.autoptimize_banner {
119
    margin: 0 38px;
120
    padding-bottom: 5px;
121
}
122
.autoptimize_banner ul li {
123
    font-size:medium;
124
    text-align:center;
125
}
126
.unslider {
127
    position:relative;
128
}
129
.unslider-arrow {
130
    display: block;
131
    left: unset;
132
    margin-top: -35px;
133
    margin-left: 7px;
134
    margin-right: 7px;
135
    border-radius: 32px;
136
    background: rgba(0, 0, 0, 0.10) no-repeat 50% 50%;
137
    color: rgba(255, 255, 255, 0.8);
138
    font: normal 20px/1 dashicons;
139
    speak: none;
140
    padding: 3px 2px 3px 4px;
141
    -webkit-font-smoothing: antialiased;
142
    -moz-osx-font-smoothing: grayscale;
143
}
144
.unslider-arrow:hover {
145
    background-color: rgba(0, 0, 0, 0.20);
146
    color: #FFF;
147
}
148
.unslider-arrow.prev {
149
    padding: 3px 4px 3px 2px;
150
}
151
.unslider-arrow.next {
152
    right: 0px;
153
}
154
.unslider-arrow.prev::before {
155
    content: "\f341";
156
}
157
.unslider-arrow.next::before {
158
    content: "\f345";
159
}
160
161
/* responsive stuff: hide admin-feed on smaller screens */
162
@media (min-width: 961px) {
163
    #autoptimize_main {float:left;width:69%;}
164
    #autoptimize_admin_feed{float:right;width:30%;display:block !important;}
165
    }
166
@media (max-width: 960px) {
167
    #autoptimize_main {width:100%;}
168
    #autoptimize_admin_feed {width:0%;display:none !important;}
169
}
170
@media (max-width: 782px) {
171
    #ao_hide_adv span, #ao_show_adv span {display: none;}
172
    #ao_hide_adv,#ao_show_adv {height: 34px;padding: 4px 12px 8px 8px;}
173
    #ao_hide_adv:before,#ao_show_adv:before {font-size: 25px;}
174
    #autoptimize_main input[type="checkbox"] {margin-left: 10px;}
175
    #autoptimize_main .cb_label {display: block; padding-left: 45px; text-indent: -45px;}
176
}
177
</style>
178
179
<div class="wrap">
180
181
<?php if ( version_compare( PHP_VERSION, '5.3.0' ) < 0 ) { ?>
182
<div class="notice-error notice"><?php echo '<p>' . sprintf( __( '<strong>You are using a very old version of PHP</strong> (5.2.x or older) which has <a href=%s>serious security and performance issues</a>. Support for PHP 5.5 and below will be removed in one of the next AO released, please ask your hoster to provide you with an upgrade path to 7.x.', 'autoptimize' ), '"http://blog.futtta.be/2016/03/15/why-would-you-still-be-on-php-5-2/" target="_blank"' ) . '</p>'; ?></div>
183
<?php } ?>
184
185
<?php if ( defined( 'AUTOPTIMIZE_LEGACY_MINIFIERS' ) ) { ?>
186
    <div class="notice-error notice"><p>
187
        <?php _e( "You are using the (no longer supported) AUTOPTIMIZE_LEGACY_MINIFIERS constant. Ensure your site is working properly and remove the constant, it doesn't do anything any more.", 'autoptimize' ); ?>
188
    </p></div>
189
<?php } ?>
190
191
<div id="autoptimize_main">
192
    <div id="ao_title_and_button">
193
        <h1 id="ao_title"><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?>
194
        <span id="ao_adv_button">
195
        <?php if ( autoptimizeOption::get_option( 'autoptimize_show_adv', '1' ) == '1' ) { ?>
196
            <a href="javascript:void(0);" id="ao_show_adv" class="button" style="display:none;"><span><?php _e("Show advanced settings","autoptimize") ?></span></a>
197
            <a href="javascript:void(0);" id="ao_hide_adv" class="button"><span><?php _e("Hide advanced settings","autoptimize") ?></span></a>
198
            <style>tr.ao_adv{display:table-row;} li.ao_adv{display:list-item;}</style>
199
            <?php $hiddenClass = ''; ?>
200
        <?php } else { ?>
201
            <a href="javascript:void(0);" id="ao_show_adv" class="button"><span><?php _e("Show advanced settings","autoptimize") ?></span></a>
202
            <a href="javascript:void(0);" id="ao_hide_adv" class="button" style="display:none;"><span><?php _e("Hide advanced settings","autoptimize") ?></span></a>
203
            <?php $hiddenClass = 'hidden '; ?>
204
        <?php } ?>
205
        </span>
206
        </h1>
207
    </div>
208
209
    <?php echo $this->ao_admin_tabs(); ?>
210
211
<form method="post" action="<?php echo admin_url( 'options.php' ); ?>">
212
<?php settings_fields( 'autoptimize' ); ?>
213
214
<ul>
215
216
<li class="itemDetail">
217
<h2 class="itemTitle"><?php _e('JavaScript Options','autoptimize'); ?></h2>
218
<table class="form-table">
219
<tr valign="top">
220
<th scope="row"><?php _e('Optimize JavaScript Code?','autoptimize'); ?></th>
221
<td><input type="checkbox" id="autoptimize_js" name="autoptimize_js" <?php echo autoptimizeOption::get_option('autoptimize_js')?'checked="checked" ':''; ?>/></td>
222
</tr>
223
<tr valign="top" class="<?php echo $hiddenClass;?>js_sub ao_adv">
224
<th scope="row"><?php _e( 'Aggregate JS-files?', 'autoptimize' ); ?></th>
225
<td><label class="cb_label"><input type="checkbox" id="autoptimize_js_aggregate" name="autoptimize_js_aggregate" <?php echo $conf->get( 'autoptimize_js_aggregate' ) ? 'checked="checked" ':''; ?>/>
226
<?php _e( 'Aggregate all linked JS-files to have them loaded non-render blocking? If this option is off, the individual JS-files will remain in place but will be minified.', 'autoptimize' ); ?></label></td>
227
</tr>
228
<tr valign="top" class="<?php echo $hiddenClass;?>js_sub ao_adv js_aggregate">
229
<th scope="row"><?php _e('Also aggregate inline JS?','autoptimize'); ?></th>
230
<td><label class="cb_label"><input type="checkbox" name="autoptimize_js_include_inline" <?php echo autoptimizeOption::get_option('autoptimize_js_include_inline')?'checked="checked" ':''; ?>/>
231
<?php _e('Let Autoptimize also extract JS from the HTML. <strong>Warning</strong>: this can make Autoptimize\'s cache size grow quickly, so only enable this if you know what you\'re doing.','autoptimize'); ?></label></td>
232
</tr>
233
<tr valign="top" class="<?php echo $hiddenClass;?>js_sub ao_adv js_aggregate">
234
<th scope="row"><?php _e('Force JavaScript in &lt;head&gt;?','autoptimize'); ?></th>
235
<td><label class="cb_label"><input type="checkbox" name="autoptimize_js_forcehead" <?php echo autoptimizeOption::get_option('autoptimize_js_forcehead')?'checked="checked" ':''; ?>/>
236
<?php _e('Load JavaScript early, this can potentially fix some JS-errors, but makes the JS render blocking.','autoptimize'); ?></label></td>
237
</tr>
238 View Code Duplication
<?php if (autoptimizeOption::get_option('autoptimize_js_justhead')) { ?>
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
239
<tr valign="top" class="<?php echo $hiddenClass;?>js_sub ao_adv js_aggregate">
240
<th scope="row"><?php _e('Look for scripts only in &lt;head&gt;?','autoptimize'); echo ' <i>'. __('(deprecated)','autoptimize') . '</i>'; ?></th>
241
<td><label class="cb_label"><input type="checkbox" name="autoptimize_js_justhead" <?php echo autoptimizeOption::get_option('autoptimize_js_justhead')?'checked="checked" ':''; ?>/>
242
<?php _e('Mostly useful in combination with previous option when using jQuery-based templates, but might help keeping cache size under control.','autoptimize'); ?></label></td>
243
</tr>
244
<?php } ?>
245
<tr valign="top" class="<?php echo $hiddenClass;?>js_sub ao_adv">
246
<th scope="row"><?php _e('Exclude scripts from Autoptimize:','autoptimize'); ?></th>
247
<td><label><input type="text" style="width:100%;" name="autoptimize_js_exclude" value="<?php echo autoptimizeOption::get_option('autoptimize_js_exclude',"wp-includes/js/dist/, wp-includes/js/tinymce/, js/jquery/jquery.js"); ?>"/><br />
248
<?php _e('A comma-separated list of scripts you want to exclude from being optimized, for example \'whatever.js, another.js\' (without the quotes) to exclude those scripts from being aggregated by Autoptimize.','autoptimize'); ?></label></td>
249
</tr>
250
<tr valign="top" class="<?php echo $hiddenClass;?>js_sub ao_adv js_aggregate">
251
<th scope="row"><?php _e('Add try-catch wrapping?','autoptimize'); ?></th>
252
<td><label class="cb_label"><input type="checkbox" name="autoptimize_js_trycatch" <?php echo autoptimizeOption::get_option('autoptimize_js_trycatch')?'checked="checked" ':''; ?>/>
253
<?php _e('If your scripts break because of a JS-error, you might want to try this.','autoptimize'); ?></label></td>
254
</tr>
255
</table>
256
</li>
257
258
<li class="itemDetail">
259
<h2 class="itemTitle"><?php _e('CSS Options','autoptimize'); ?></h2>
260
<table class="form-table">
261
<tr valign="top">
262
<th scope="row"><?php _e('Optimize CSS Code?','autoptimize'); ?></th>
263
<td><input type="checkbox" id="autoptimize_css" name="autoptimize_css" <?php echo autoptimizeOption::get_option('autoptimize_css')?'checked="checked" ':''; ?>/></td>
264
</tr>
265
<tr class="<?php echo $hiddenClass;?>css_sub ao_adv" valign="top">
266
<th scope="row"><?php _e( 'Aggregate CSS-files?', 'autoptimize' ); ?></th>
267
<td><label class="cb_label"><input type="checkbox" id="autoptimize_css_aggregate" name="autoptimize_css_aggregate" <?php echo $conf->get( 'autoptimize_css_aggregate' ) ? 'checked="checked" ' : ''; ?>/>
268
<?php _e('Aggregate all linked CSS-files? If this option is off, the individual CSS-files will remain in place but will be minified.', 'autoptimize' ); ?></label></td>
269
</tr>
270
<tr valign="top" class="<?php echo $hiddenClass;?>css_sub ao_adv css_aggregate">
271
<th scope="row"><?php _e('Also aggregate inline CSS?','autoptimize'); ?></th>
272
<td><label class="cb_label"><input type="checkbox" name="autoptimize_css_include_inline" <?php echo autoptimizeOption::get_option('autoptimize_css_include_inline','1')?'checked="checked" ':''; ?>/>
273
<?php _e('Check this option for Autoptimize to also aggregate CSS in the HTML.','autoptimize'); ?></label></td>
274
</tr>
275
<tr class="<?php echo $hiddenClass;?>css_sub ao_adv css_aggregate" valign="top">
276
<th scope="row"><?php _e('Generate data: URIs for images?','autoptimize'); ?></th>
277
<td><label class="cb_label"><input type="checkbox" name="autoptimize_css_datauris" <?php echo autoptimizeOption::get_option('autoptimize_css_datauris')?'checked="checked" ':''; ?>/>
278
<?php _e('Enable this to include small background-images in the CSS itself instead of as separate downloads.','autoptimize'); ?></label></td>
279
</tr>
280 View Code Duplication
<?php if (autoptimizeOption::get_option('autoptimize_css_justhead')) { ?>
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
281
<tr valign="top" class="<?php echo $hiddenClass;?>css_sub ao_adv css_aggregate">
282
<th scope="row"><?php _e('Look for styles only in &lt;head&gt;?','autoptimize'); echo ' <i>'. __('(deprecated)','autoptimize') . '</i>'; ?></th>
283
<td><label class="cb_label"><input type="checkbox" name="autoptimize_css_justhead" <?php echo autoptimizeOption::get_option('autoptimize_css_justhead')?'checked="checked" ':''; ?>/>
284
<?php _e('Don\'t autoptimize CSS outside the head-section. If the cache gets big, you might want to enable this.','autoptimize'); ?></label></td>
285
</tr>
286
<?php } ?>
287
<tr valign="top" class="<?php echo $hiddenClass;?>css_sub ao_adv">
288
<th scope="row"><?php _e('Inline and Defer CSS?','autoptimize'); ?></th>
289
<td><label class="cb_label"><input type="checkbox" name="autoptimize_css_defer" id="autoptimize_css_defer" <?php echo autoptimizeOption::get_option('autoptimize_css_defer')?'checked="checked" ':''; ?>/>
290
<?php
291
_e( 'Inline "above the fold CSS" while loading the main autoptimized CSS only after page load. <a href="http://wordpress.org/plugins/autoptimize/faq/" target="_blank">Check the FAQ</a> for more info.', 'autoptimize' );
292 View Code Duplication
if ( function_exists( 'is_plugin_active' ) && ! is_plugin_active( 'autoptimize-criticalcss/ao_criticss_aas.php' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
293
    echo ' ';
294
    $critcss_install_url = network_admin_url() . 'plugin-install.php?s=autoptimize+criticalcss&tab=search&type=term';
295
    echo sprintf( __( 'This can be fully automated for different types of pages with the %s.', 'autoptimize' ), '<a href="'.$critcss_install_url.'">Autoptimize CriticalCSS Power-Up</a>' );
296
}
297
?>
298
</label></td>
299
</tr>
300
<tr valign="top" class="<?php echo $hiddenClass;?>css_sub ao_adv" id="autoptimize_css_defer_inline">
301
<th scope="row"></th>
302
<td><label><textarea rows="10" cols="10" style="width:100%;" placeholder="<?php _e('Paste the above the fold CSS here.','autoptimize'); ?>" name="autoptimize_css_defer_inline"><?php echo autoptimizeOption::get_option('autoptimize_css_defer_inline'); ?></textarea></label></td>
303
</tr>
304
<tr valign="top" class="<?php echo $hiddenClass;?>ao_adv css_sub css_aggregate">
305
<th scope="row"><?php _e('Inline all CSS?','autoptimize'); ?></th>
306
<td><label class="cb_label"><input type="checkbox" id="autoptimize_css_inline" name="autoptimize_css_inline" <?php echo autoptimizeOption::get_option('autoptimize_css_inline')?'checked="checked" ':''; ?>/>
307
<?php _e('Inlining all CSS can improve performance for sites with a low pageviews/ visitor-rate, but may slow down performance otherwise.','autoptimize'); ?></label></td>
308
</tr>
309
<tr valign="top" class="<?php echo $hiddenClass;?>ao_adv css_sub">
310
<th scope="row"><?php _e('Exclude CSS from Autoptimize:','autoptimize'); ?></th>
311
<td><label><input type="text" style="width:100%;" name="autoptimize_css_exclude" value="<?php echo autoptimizeOption::get_option('autoptimize_css_exclude','wp-content/cache/, wp-content/uploads/, admin-bar.min.css, dashicons.min.css'); ?>"/><br />
312
<?php _e('A comma-separated list of CSS you want to exclude from being optimized.','autoptimize'); ?></label></td>
313
</tr>
314
</table>
315
</li>
316
317
<li class="itemDetail">
318
<h2 class="itemTitle"><?php _e('HTML Options','autoptimize'); ?></h2>
319
<table class="form-table">
320
<tr valign="top">
321
<th scope="row"><?php _e('Optimize HTML Code?','autoptimize'); ?></th>
322
<td><input type="checkbox" id="autoptimize_html" name="autoptimize_html" <?php echo autoptimizeOption::get_option('autoptimize_html')?'checked="checked" ':''; ?>/></td>
323
</tr>
324
<tr class="<?php echo $hiddenClass;?>html_sub ao_adv" valign="top">
325
<th scope="row"><?php _e('Keep HTML comments?','autoptimize'); ?></th>
326
<td><label class="cb_label"><input type="checkbox" name="autoptimize_html_keepcomments" <?php echo autoptimizeOption::get_option('autoptimize_html_keepcomments')?'checked="checked" ':''; ?>/>
327
<?php _e('Enable this if you want HTML comments to remain in the page.','autoptimize'); ?></label></td>
328
</tr>
329
</table>
330
</li>
331
332
<li class="itemDetail">
333
<h2 class="itemTitle"><?php _e('CDN Options','autoptimize'); ?></h2>
334
<table class="form-table">
335
<tr valign="top">
336
<th scope="row"><?php _e('CDN Base URL','autoptimize'); ?></th>
337
<td><label><input id="cdn_url" type="text" name="autoptimize_cdn_url" pattern="^(https?:)?\/\/([\da-z\.-]+)\.([\da-z\.]{2,6})([\/\w \.-]*)*(:\d{2,5})?\/?$" style="width:100%" value="<?php echo esc_url(autoptimizeOption::get_option('autoptimize_cdn_url',''),array("http","https")); ?>" /><br />
338
<?php _e('Enter your CDN root URL to enable CDN for Autoptimized files. The URL can be http, https or protocol-relative (e.g. <code>//cdn.example.com/</code>). This is not needed for Cloudflare.','autoptimize'); ?></label></td>
339
</tr>
340
</table>
341
</li>
342
343
<li class="<?php echo $hiddenClass;?>itemDetail ao_adv">
344
<h2 class="itemTitle"><?php _e('Cache Info','autoptimize'); ?></h2>
345
<table class="form-table" >
346
<tr valign="top" class="<?php echo $hiddenClass;?>ao_adv">
347
<th scope="row"><?php _e('Cache folder','autoptimize'); ?></th>
348
<td><?php echo htmlentities(AUTOPTIMIZE_CACHE_DIR); ?></td>
349
</tr>
350
<tr valign="top" class="<?php echo $hiddenClass;?>ao_adv">
351
<th scope="row"><?php _e('Can we write?','autoptimize'); ?></th>
352
<td><?php echo (autoptimizeCache::cacheavail() ? __('Yes','autoptimize') : __('No','autoptimize')); ?></td>
353
</tr>
354
<tr valign="top" class="<?php echo $hiddenClass;?>ao_adv">
355
<th scope="row"><?php _e('Cached styles and scripts','autoptimize'); ?></th>
356
<td><?php
357
    $AOstatArr = autoptimizeCache::stats();
358
    if ( ! empty( $AOstatArr ) && is_array( $AOstatArr ) ) {
359
        $AOcacheSize = size_format( $AOstatArr[1], 2 );
360
        $details = '';
0 ignored issues
show
Unused Code introduced by
$details is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
361
        if ( $AOcacheSize > 0 ) {
362
            $details = ', ~' . $AOcacheSize . ' total';
0 ignored issues
show
Unused Code introduced by
$details is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
363
        }
364
        printf( __( '%1$s files, totalling %2$s Kbytes (calculated at %3$s)', 'autoptimize' ), $AOstatArr[0], $AOcacheSize, date( 'H:i e', $AOstatArr[2] ) );
365
    }
366
?></td>
367
</tr>
368
</table>
369
</li>
370
371
<li class="<?php echo $hiddenClass;?>itemDetail ao_adv">
372
<h2 class="itemTitle"><?php _e('Misc Options','autoptimize'); ?></h2>
373
<table class="form-table">
374
    <tr valign="top" class="<?php echo $hiddenClass;?>ao_adv">
375
    <th scope="row"><?php _e('Save aggregated script/css as static files?','autoptimize'); ?></th>
376
    <td><label class="cb_label"><input type="checkbox" name="autoptimize_cache_nogzip" <?php echo autoptimizeOption::get_option('autoptimize_cache_nogzip','1')?'checked="checked" ':''; ?>/>
377
    <?php _e('By default files saved are static css/js, uncheck this option if your webserver doesn\'t properly handle the compression and expiry.','autoptimize'); ?></label></td>
378
    </tr>
379
    <?php
380
    $_min_excl_class = 'ao_adv';
381
    if ( !$conf->get( 'autoptimize_css_aggregate' ) && !$conf->get( 'autoptimize_js_aggregate' ) ) {
382
        $_min_excl_class = ' hidden';
383
    }
384
    ?>
385
    <tr valign="top" id="min_excl_row" class="<?php echo $hiddenClass.$_min_excl_class; ?>">
386
        <th scope="row"><?php _e('Minify excluded CSS and JS files?','autoptimize'); ?></th>
387
        <td><label class="cb_label"><input type="checkbox" name="autoptimize_minify_excluded" <?php echo autoptimizeOption::get_option('autoptimize_minify_excluded','1')?'checked="checked" ':''; ?>/>
388
        <?php _e('When aggregating JS or CSS, excluded files that are not minified (based on filename) are by default minified by Autoptimize despite being excluded. Uncheck this option if anything breaks despite excluding.','autoptimize'); ?></label></td>
389
    </tr>
390
    <tr valign="top" class="<?php echo $hiddenClass;?>ao_adv">
391
    <th scope="row"><?php _e('Also optimize for logged in users?','autoptimize'); ?></th>
392
    <td><label class="cb_label"><input type="checkbox" name="autoptimize_optimize_logged" <?php echo autoptimizeOption::get_option('autoptimize_optimize_logged','1')?'checked="checked" ':''; ?>/>
393
    <?php _e('By default Autoptimize is also active for logged on users, uncheck not to optimize when logged in e.g. to use a pagebuilder.','autoptimize'); ?></label></td>
394
    </tr>
395
    <?php
396
    if ( function_exists("is_checkout") || function_exists("is_cart") || function_exists("edd_is_checkout") || function_exists("wpsc_is_cart") || function_exists("wpsc_is_checkout") ) {
397
    ?>
398
    <tr valign="top" class="<?php echo $hiddenClass;?>ao_adv">
399
        <th scope="row"><?php _e('Also optimize shop cart/ checkout?','autoptimize'); ?></th>
400
        <td><label class="cb_label"><input type="checkbox" name="autoptimize_optimize_checkout" <?php echo autoptimizeOption::get_option('autoptimize_optimize_checkout','1')?'checked="checked" ':''; ?>/>
401
            <?php _e('By default Autoptimize is also active on your shop\'s cart/ checkout, uncheck not to optimize those.','autoptimize'); ?></label>
402
        </td>
403
    </tr>
404
    <?php } ?>
405
</table>
406
</li>
407
408
</ul>
409
410
<input type="hidden" id="autoptimize_show_adv" name="autoptimize_show_adv" value="<?php echo autoptimizeOption::get_option('autoptimize_show_adv','1'); ?>">
411
412
<p class="submit">
413
<input type="submit" class="button-secondary" value="<?php _e('Save Changes','autoptimize') ?>" />
414
<input type="submit" class="button-primary" name="autoptimize_cache_clean" value="<?php _e('Save Changes and Empty Cache','autoptimize') ?>" />
415
</p>
416
417
</form>
418
</div>
419
<div id="autoptimize_admin_feed" class="hidden">
420
    <div class="autoptimize_banner hidden">
421
      <ul>
422
    <?php
423
    if ( $this->settings_screen_do_remote_http ) {
424
        $AO_banner = get_transient( 'autoptimize_banner' );
425
        if ( empty( $AO_banner ) ) {
426
            $banner_resp = wp_remote_get( 'https://misc.optimizingmatters.com/autoptimize_news.html?ao_ver='.AUTOPTIMIZE_PLUGIN_VERSION );
427
            if ( ! is_wp_error( $banner_resp ) ) {
428
                if ( '200' == wp_remote_retrieve_response_code( $banner_resp ) ) {
429
                    $AO_banner = wp_kses_post( wp_remote_retrieve_body( $banner_resp ) );
430
                    set_transient('autoptimize_banner', $AO_banner, DAY_IN_SECONDS);
431
                }
432
            }
433
        }
434
        echo $AO_banner;
435
    }
436
    ?>
437
        <li><?php _e("Need help? <a href='https://wordpress.org/plugins/autoptimize/faq/'>Check out the FAQ here</a>.","autoptimize"); ?></li>
438
        <li><?php _e("Happy with Autoptimize?","autoptimize"); ?><br /><a href="<?php echo network_admin_url(); ?>plugin-install.php?tab=search&type=author&s=optimizingmatters"><?php _e("Try my other plugins!","autoptimize"); ?></a></li>
439
    </ul>
440
    </div>
441
    <div style="margin-left:10px;margin-top:-5px;">
442
        <h2>
443
            <?php _e("futtta about","autoptimize") ?>
444
            <select id="feed_dropdown" >
445
                <option value="1"><?php _e("Autoptimize","autoptimize") ?></option>
446
                <option value="2"><?php _e("WordPress","autoptimize") ?></option>
447
                <option value="3"><?php _e("Web Technology","autoptimize") ?></option>
448
            </select>
449
        </h2>
450
        <div id="futtta_feed">
451
            <div id="autoptimizefeed">
452
                <?php $this->getFutttaFeeds("http://feeds.feedburner.com/futtta_autoptimize"); ?>
453
            </div>
454
            <div id="wordpressfeed">
455
                <?php $this->getFutttaFeeds("http://feeds.feedburner.com/futtta_wordpress"); ?>
456
            </div>
457
            <div id="webtechfeed">
458
                <?php $this->getFutttaFeeds("http://feeds.feedburner.com/futtta_webtech"); ?>
459
            </div>
460
        </div>
461
    </div>
462
    <div style="float:right;margin:50px 15px;"><a href="http://blog.futtta.be/2013/10/21/do-not-donate-to-me/" target="_blank"><img width="100px" height="85px" src="<?php echo plugins_url().'/'.plugin_basename(dirname(__FILE__)).'/external/do_not_donate_smallest.png'; ?>" title="<?php _e("Do not donate for this plugin!","autoptimize"); ?>"></a></div>
463
</div>
464
465
<script type="text/javascript">
466
    var feed = new Array;
467
    feed[1]="autoptimizefeed";
468
    feed[2]="wordpressfeed";
469
    feed[3]="webtechfeed";
470
    cookiename="autoptimize_feed";
471
472
    jQuery(document).ready(function() {
473
        check_ini_state();
474
475
        jQuery('#autoptimize_admin_feed').fadeTo("slow",1).show();
476
        jQuery('.autoptimize_banner').unslider({autoplay:true, delay:3500, infinite: false, arrows:{prev:'<a class="unslider-arrow prev"></a>', next:'<a class="unslider-arrow next"></a>'}}).fadeTo("slow",1).show();
477
478
        jQuery( "#feed_dropdown" ).change(function() {
479
            jQuery("#futtta_feed").fadeTo(0,0);
480
            jQuery("#futtta_feed").fadeTo("slow",1);
481
        });
482
483
        jQuery( "#ao_show_adv" ).click(function() {
484
            jQuery( "#ao_show_adv" ).hide();
485
            jQuery( "#ao_hide_adv" ).show();
486
            jQuery( ".ao_adv" ).removeClass("hidden");
487
            jQuery( ".ao_adv" ).show("slow");
488
            if (jQuery("#autoptimize_css").attr('checked')) {
489
                jQuery(".css_sub:visible").fadeTo("fast",1);
490
                if (!jQuery("#autoptimize_css_defer").attr('checked')) {
491
                    jQuery("#autoptimize_css_defer_inline").hide();
492
                }
493
            }
494
            if (jQuery("#autoptimize_js").attr('checked')) {
495
                jQuery(".js_sub:visible").fadeTo("fast",1);
496
            }
497
            check_ini_state();
498
            jQuery( "input#autoptimize_show_adv" ).val("1");
499
        });
500
501
        jQuery( "#ao_hide_adv" ).click(function() {
502
            jQuery( "#ao_hide_adv" ).hide();
503
            jQuery( "#ao_show_adv" ).show();
504
            jQuery( ".ao_adv" ).hide("slow");
505
            jQuery( ".ao_adv" ).addClass("hidden");
506
            if (!jQuery("#autoptimize_css").attr('checked')) {
507
                jQuery(".css_sub:visible").fadeTo("fast",.33);
508
            }
509
            if (!jQuery("#autoptimize_js").attr('checked')) {
510
                jQuery(".js_sub:visible").fadeTo("fast",.33);
511
            }
512
            check_ini_state();
513
            jQuery( "input#autoptimize_show_adv" ).val("0");
514
        });
515
516
        jQuery( "#autoptimize_html" ).change(function() {
517
            if (this.checked) {
518
                jQuery(".html_sub:visible").fadeTo("fast",1);
519
            } else {
520
                jQuery(".html_sub:visible").fadeTo("fast",.33);
521
            }
522
        });
523
524
        jQuery( "#autoptimize_js" ).change(function() {
525
            if (this.checked) {
526
                jQuery(".js_sub:visible").fadeTo("fast",1);
527
            } else {
528
                jQuery(".js_sub:visible").fadeTo("fast",.33);
529
            }
530
        });
531
532
        jQuery( "#autoptimize_js_aggregate" ).change(function() {
533
            if (this.checked && jQuery("#autoptimize_js").attr('checked')) {
534
                jQuery(".js_aggregate:visible").fadeTo("fast",1);
535
                jQuery( "#min_excl_row" ).show();
536
            } else {
537
                jQuery(".js_aggregate:visible").fadeTo("fast",.33);
538
                if ( jQuery( "#autoptimize_css_aggregate" ).prop('checked') == false ) {
539
                    jQuery( "#min_excl_row" ).hide();
540
                }
541
            }
542
        });
543
544
        jQuery( "#autoptimize_css" ).change(function() {
545
            if (this.checked) {
546
                jQuery(".css_sub:visible").fadeTo("fast",1);
547
            } else {
548
                jQuery(".css_sub:visible").fadeTo("fast",.33);
549
            }
550
        });
551
552
        jQuery( "#autoptimize_css_aggregate" ).change(function() {
553
            if (this.checked && jQuery("#autoptimize_css").attr('checked')) {
554
                jQuery(".css_aggregate:visible").fadeTo("fast",1);
555
                jQuery( "#min_excl_row" ).show();
556
            } else {
557
                jQuery(".css_aggregate:visible").fadeTo("fast",.33);
558
                if ( jQuery( "#autoptimize_js_aggregate" ).prop('checked') == false ) {
559
                    jQuery( "#min_excl_row" ).hide();
560
                }
561
            }
562
        });
563
564
        jQuery( "#autoptimize_css_inline" ).change(function() {
565
            if (this.checked) {
566
                jQuery("#autoptimize_css_defer").prop("checked",false);
567
                jQuery("#autoptimize_css_defer_inline").hide("slow");
568
            }
569
        });
570
571
        jQuery( "#autoptimize_css_defer" ).change(function() {
572
            if (this.checked) {
573
                jQuery("#autoptimize_css_inline").prop("checked",false);
574
                jQuery("#autoptimize_css_defer_inline").show("slow");
575
            } else {
576
                jQuery("#autoptimize_css_defer_inline").hide("slow");
577
            }
578
        });
579
580
        jQuery("#feed_dropdown").change(function() { show_feed(jQuery("#feed_dropdown").val()) });
581
        feedid=jQuery.cookie(cookiename);
582
        if(typeof(feedid) !== "string") feedid=1;
583
        show_feed(feedid);
584
    })
585
586
    // validate cdn_url.
587
    var cdn_url=document.getElementById("cdn_url");
588
    cdn_url_baseCSS=cdn_url.style.cssText;
589
    if ("validity" in cdn_url) {
590
        jQuery("#cdn_url").focusout(function (event) {
591
        if (cdn_url.validity.valid) {
592
            cdn_url.style.cssText=cdn_url_baseCSS;
593
        } else {
594
            cdn_url.style.cssText=cdn_url_baseCSS+"border:1px solid #f00;color:#f00;box-shadow: 0 0 2px #f00;";
595
        }});
596
    }
597
598
    function check_ini_state() {
599
        if (!jQuery("#autoptimize_css_defer").attr('checked')) {
600
            jQuery("#autoptimize_css_defer_inline").hide();
601
        }
602
        if (!jQuery("#autoptimize_html").attr('checked')) {
603
            jQuery(".html_sub:visible").fadeTo('fast',.33);
604
        }
605
        if (!jQuery("#autoptimize_css").attr('checked')) {
606
            jQuery(".css_sub:visible").fadeTo('fast',.33);
607
        }
608
        if (!jQuery("#autoptimize_css_aggregate").attr('checked')) {
609
            jQuery(".css_aggregate:visible").fadeTo('fast',.33);
610
        }
611
        if (!jQuery("#autoptimize_js").attr('checked')) {
612
            jQuery(".js_sub:visible").fadeTo('fast',.33);
613
        }
614
        if (!jQuery("#autoptimize_js_aggregate").attr('checked')) {
615
            jQuery(".js_aggregate:visible").fadeTo('fast',.33);
616
        }
617
    }
618
619
    function show_feed(id) {
620
        jQuery('#futtta_feed').children().hide();
621
        jQuery('#'+feed[id]).show();
622
        jQuery("#feed_dropdown").val(id);
623
        jQuery.cookie(cookiename,id,{ expires: 365 });
624
    }
625
</script>
626
</div>
627
628
<?php
629
    }
630
631
    public function addmenu()
632
    {
633
		if ( is_multisite() && is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) ) {
634
			$hook = add_submenu_page( 'settings.php',  __( 'Autoptimize Options', 'autoptimize' ), 'Autoptimize', 'manage_network_options', 'autoptimize', array( $this, 'show' ) );
635
		} else {
636
			$hook = add_options_page( __( 'Autoptimize Options', 'autoptimize' ), 'Autoptimize', 'manage_options', 'autoptimize', array( $this, 'show' ) );
637
		}
638
		
639
        add_action( 'admin_print_scripts-' . $hook, array( $this, 'autoptimize_admin_scripts' ) );
640
        add_action( 'admin_print_styles-' . $hook, array( $this, 'autoptimize_admin_styles' ) );
641
    }
642
643
    public function autoptimize_admin_scripts()
644
    {
645
        wp_enqueue_script( 'jqcookie', plugins_url( '/external/js/jquery.cookie.min.js', __FILE__ ), array( 'jquery' ), null, true );
646
        wp_enqueue_script( 'unslider', plugins_url( '/external/js/unslider-min.js', __FILE__ ), array( 'jquery' ), null, true );
647
    }
648
649
    public function autoptimize_admin_styles()
650
    {
651
        wp_enqueue_style( 'unslider', plugins_url( '/external/js/unslider.css', __FILE__ ) );
652
        wp_enqueue_style( 'unslider-dots', plugins_url( '/external/js/unslider-dots.css', __FILE__ ) );
653
    }
654
655
    public function registersettings() {
656
        register_setting( 'autoptimize', 'autoptimize_html' );
657
        register_setting( 'autoptimize', 'autoptimize_html_keepcomments' );
658
        register_setting( 'autoptimize', 'autoptimize_js' );
659
        register_setting( 'autoptimize', 'autoptimize_js_aggregate' );
660
        register_setting( 'autoptimize', 'autoptimize_js_exclude' );
661
        register_setting( 'autoptimize', 'autoptimize_js_trycatch' );
662
        register_setting( 'autoptimize', 'autoptimize_js_justhead' );
663
        register_setting( 'autoptimize', 'autoptimize_js_forcehead' );
664
        register_setting( 'autoptimize', 'autoptimize_js_include_inline' );
665
        register_setting( 'autoptimize', 'autoptimize_css' );
666
        register_setting( 'autoptimize', 'autoptimize_css_aggregate' );
667
        register_setting( 'autoptimize', 'autoptimize_css_exclude' );
668
        register_setting( 'autoptimize', 'autoptimize_css_justhead' );
669
        register_setting( 'autoptimize', 'autoptimize_css_datauris' );
670
        register_setting( 'autoptimize', 'autoptimize_css_defer' );
671
        register_setting( 'autoptimize', 'autoptimize_css_defer_inline' );
672
        register_setting( 'autoptimize', 'autoptimize_css_inline' );
673
        register_setting( 'autoptimize', 'autoptimize_css_include_inline' );
674
        register_setting( 'autoptimize', 'autoptimize_cdn_url' );
675
        register_setting( 'autoptimize', 'autoptimize_cache_clean' );
676
        register_setting( 'autoptimize', 'autoptimize_cache_nogzip' );
677
        register_setting( 'autoptimize', 'autoptimize_show_adv' );
678
        register_setting( 'autoptimize', 'autoptimize_optimize_logged' );
679
        register_setting( 'autoptimize', 'autoptimize_optimize_checkout' );
680
        register_setting( 'autoptimize', 'autoptimize_minify_excluded' );
681
    }
682
683
    public function setmeta($links, $file = null)
684
    {
685
        // Inspired on http://wpengineer.com/meta-links-for-wordpress-plugins/.
686
        // Do it only once - saves time.
687
        static $plugin;
688
        if ( empty( $plugin ) ) {
689
            $plugin = plugin_basename( AUTOPTIMIZE_PLUGIN_DIR . 'autoptimize.php' );
690
        }
691
692
        if ( null === $file ) {
693
            // 2.7 and lower.
694
            $settings_link = sprintf( '<a href="options-general.php?page=autoptimize">%s</a>', __( 'Settings' ) );
695
            array_unshift( $links, $settings_link );
696
        } else {
697
            // 2.8 and higher.
698
            // If it's us, add the link.
699
            if ( $file === $plugin ) {
700
                $newlink = array( sprintf( '<a href="options-general.php?page=autoptimize">%s</a>', __( 'Settings' ) ) );
701
                $links = array_merge( $links, $newlink );
702
            }
703
        }
704
705
        return $links;
706
    }
707
708
    /**
709
     * @return array
710
     */
711
    public static function get_defaults()
712
    {
713
        static $config = array(
714
            'autoptimize_html' => 0,
715
            'autoptimize_html_keepcomments' => 0,
716
            'autoptimize_js' => 0,
717
            'autoptimize_js_aggregate' => 1,
718
            'autoptimize_js_exclude' => 'wp-includes/js/dist/, wp-includes/js/tinymce/, js/jquery/jquery.js',
719
            'autoptimize_js_trycatch' => 0,
720
            'autoptimize_js_justhead' => 0,
721
            'autoptimize_js_include_inline' => 0,
722
            'autoptimize_js_forcehead' => 0,
723
            'autoptimize_css' => 0,
724
            'autoptimize_css_aggregate' => 1,
725
            'autoptimize_css_exclude' => 'admin-bar.min.css, dashicons.min.css, wp-content/cache/, wp-content/uploads/',
726
            'autoptimize_css_justhead' => 0,
727
            'autoptimize_css_include_inline' => 1,
728
            'autoptimize_css_defer' => 0,
729
            'autoptimize_css_defer_inline' => '',
730
            'autoptimize_css_inline' => 0,
731
            'autoptimize_css_datauris' => 0,
732
            'autoptimize_cdn_url' => '',
733
            'autoptimize_cache_nogzip' => 1,
734
            'autoptimize_show_adv' => 1,
735
            'autoptimize_optimize_logged' => 1,
736
            'autoptimize_optimize_checkout' => 1,
737
            'autoptimize_minify_excluded' => 1,
738
        );
739
740
        return $config;
741
    }
742
743
    /**
744
     * Returns default option values for autoptimizeExtra.
745
     *
746
     * @return array
747
     */
748
    public static function get_ao_extra_default_options()
749
    {
750
        $defaults = array(
751
            'autoptimize_extra_checkbox_field_1' => '0',
752
            'autoptimize_extra_checkbox_field_0' => '0',
753
            'autoptimize_extra_radio_field_4'    => '1',
754
            'autoptimize_extra_text_field_2'     => '',
755
            'autoptimize_extra_text_field_3'     => '',
756
        );
757
758
        return $defaults;
759
    }
760
761
    /**
762
     * Returns default option values for autoptimizeExtra.
763
     *
764
     * @return array
765
     */
766
    public static function get_ao_imgopt_default_options()
767
    {
768
        $defaults = array(
769
            'autoptimize_imgopt_checkbox_field_1' => '0', // imgopt off.
770
            'autoptimize_imgopt_select_field_2'   => '2', // quality glossy.
771
            'autoptimize_imgopt_checkbox_field_3' => '0', // lazy load off.
772
            'autoptimize_imgopt_checkbox_field_4' => '0', // webp off (might be removed).
773
            'autoptimize_imgopt_text_field_5'     => '',  // lazy load exclusions empty.
774
        );
775
        return $defaults;
776
    }
777
778
    /**
779
     * Returns preload polyfill JS.
780
     *
781
     * @return string
782
     */
783
    public static function get_ao_css_preload_polyfill()
784
    {
785
        $preload_poly = apply_filters('autoptimize_css_preload_polyfill','<script data-cfasync=\'false\'>!function(t){"use strict";t.loadCSS||(t.loadCSS=function(){});var e=loadCSS.relpreload={};if(e.support=function(){var e;try{e=t.document.createElement("link").relList.supports("preload")}catch(t){e=!1}return function(){return e}}(),e.bindMediaToggle=function(t){function e(){t.media=a}var a=t.media||"all";t.addEventListener?t.addEventListener("load",e):t.attachEvent&&t.attachEvent("onload",e),setTimeout(function(){t.rel="stylesheet",t.media="only x"}),setTimeout(e,3e3)},e.poly=function(){if(!e.support())for(var a=t.document.getElementsByTagName("link"),n=0;n<a.length;n++){var o=a[n];"preload"!==o.rel||"style"!==o.getAttribute("as")||o.getAttribute("data-loadcss")||(o.setAttribute("data-loadcss",!0),e.bindMediaToggle(o))}},!e.support()){e.poly();var a=t.setInterval(e.poly,500);t.addEventListener?t.addEventListener("load",function(){e.poly(),t.clearInterval(a)}):t.attachEvent&&t.attachEvent("onload",function(){e.poly(),t.clearInterval(a)})}"undefined"!=typeof exports?exports.loadCSS=loadCSS:t.loadCSS=loadCSS}("undefined"!=typeof global?global:this);</script>');
786
        return $preload_poly;
787
    }
788
789
    /**
790
     * Returns preload JS onload handler.
791
     *
792
     * @return string
793
     */
794
    public static function get_ao_css_preload_onload()
795
    {
796
        $preload_onload = apply_filters('autoptimize_filter_css_preload_onload',"this.onload=null;this.rel='stylesheet'");
797
        return $preload_onload;
798
    }
799
800
    public function get($key)
801
    {
802
        if ( ! is_array( $this->config ) ) {
803
            // Default config.
804
            $config = self::get_defaults();
805
806
            // Override with user settings.
807
            foreach ( array_keys( $config ) as $name ) {
808
                $conf = autoptimizeOption::get_option( $name );
809
                if ( false !== $conf ) {
810
                    // It was set before!
811
                    $config[ $name ] = $conf;
812
                }
813
            }
814
815
            // Save for next call.
816
            $this->config = apply_filters( 'autoptimize_filter_get_config', $config );
817
        }
818
819
        if ( isset( $this->config[ $key ] ) ) {
820
            return $this->config[ $key ];
821
        }
822
823
        return false;
824
    }
825
826
    private function getFutttaFeeds($url) {
827
        if ( $this->settings_screen_do_remote_http ) {
828
            $rss = fetch_feed( $url );
829
            $maxitems = 0;
830
831 View Code Duplication
            if ( ! is_wp_error( $rss ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
832
                $maxitems = $rss->get_item_quantity( 7 );
833
                $rss_items = $rss->get_items( 0, $maxitems );
834
            }
835
            ?>
836
            <ul>
837
                <?php if ( $maxitems == 0 ) : ?>
838
                    <li><?php _e( 'No items', 'autoptimize' ); ?></li>
839
                <?php else : ?>
840
                    <?php foreach ( $rss_items as $item ) : ?>
0 ignored issues
show
Bug introduced by
The variable $rss_items does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
841
                        <li>
842
                            <a href="<?php echo esc_url( $item->get_permalink() ); ?>"
843
                                title="<?php printf( __( 'Posted %s', 'autoptimize' ), $item->get_date('j F Y | g:i a') ); ?>">
844
                                <?php echo esc_html( $item->get_title() ); ?>
845
                            </a>
846
                        </li>
847
                    <?php endforeach; ?>
848
                <?php endif; ?>
849
            </ul>
850
        <?php
851
        }
852
    }
853
854
    // based on http://wordpress.stackexchange.com/a/58826
855
    static function ao_admin_tabs()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
856
    {
857
        $tabs = apply_filters( 'autoptimize_filter_settingsscreen_tabs' ,array( 'autoptimize' => __( 'JS, CSS  &amp; HTML', 'autoptimize' ) ) );
858
        $tabContent = '';
859
        $tabs_count = count($tabs);
860
        if ( $tabs_count > 1 ) {
861
            if ( isset( $_GET['page'] ) ) {
862
                $currentId = $_GET['page'];
863
            } else {
864
                $currentId = "autoptimize";
865
            }
866
            $tabContent .= '<h2 class="nav-tab-wrapper">';
867
            foreach ($tabs as $tabId => $tabName) {
868
                if ( $currentId == $tabId ) {
869
                    $class = ' nav-tab-active';
870
                } else{
871
                    $class = '';
872
                }
873
                $tabContent .= '<a class="nav-tab' . $class . '" href="?page=' . $tabId . '">' . $tabName . '</a>';
874
            }
875
            $tabContent .= '</h2>';
876
        } else {
877
            $tabContent = '<hr/>';
878
        }
879
880
        return $tabContent;
881
    }
882
883
    /**
884
     * Returns true if in admin (and not in admin-ajax.php!)
885
     *
886
     * @return bool
887
     */
888
    public static function is_admin_and_not_ajax()
889
    {
890
        return ( is_admin() && ! self::doing_ajax() );
891
    }
892
893
    /**
894
     * Returns true if doing ajax.
895
     *
896
     * @return bool
897
     */
898
    protected static function doing_ajax()
899
    {
900
        if ( function_exists( 'wp_doing_ajax' ) ) {
901
            return wp_doing_ajax();
902
        }
903
        return ( defined( 'DOING_AJAX' ) && DOING_AJAX );
904
    }
905
}
906