Conditions | 38 |
Paths | > 20000 |
Total Lines | 236 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
93 | public function ao_criticalcsssettings_page() |
||
94 | { |
||
95 | // these are not OO yet, simply require for now. |
||
96 | require_once( 'critcss-inc/admin_settings_rules.php' ); |
||
97 | require_once( 'critcss-inc/admin_settings_queue.php' ); |
||
98 | require_once( 'critcss-inc/admin_settings_key.php' ); |
||
99 | require_once( 'critcss-inc/admin_settings_adv.php' ); |
||
100 | require_once( 'critcss-inc/admin_settings_explain.php' ); |
||
101 | |||
102 | // fetch all options at once and populate them individually explicitely as globals. |
||
103 | $all_options = autoptimizeCriticalCSSBase::fetch_options(); |
||
104 | foreach ( $all_options as $_option => $_value ) { |
||
105 | global ${$_option}; |
||
106 | ${$_option} = $_value; |
||
107 | } |
||
108 | ?> |
||
109 | <div class="wrap"> |
||
110 | <div id="autoptimize_main"> |
||
111 | <div id="ao_title_and_button"> |
||
112 | <h1><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1> |
||
113 | </div> |
||
114 | |||
115 | <?php |
||
116 | // Print AO settings tabs. |
||
117 | echo autoptimizeConfig::ao_admin_tabs(); |
||
118 | |||
119 | // Make sure dir to write ao_ccss exists and is writable. |
||
120 | if ( ! is_dir( AO_CCSS_DIR ) ) { |
||
121 | $mkdirresp = @mkdir( AO_CCSS_DIR, 0775, true ); // @codingStandardsIgnoreLine |
||
122 | $fileresp = file_put_contents( AO_CCSS_DIR . 'index.html', '<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>' ); |
||
123 | if ( ( ! $mkdirresp ) || ( ! $fileresp ) ) { |
||
124 | ?> |
||
125 | <div class="notice-error notice"><p> |
||
126 | <?php |
||
127 | _e( 'Could not create the required directory. Make sure the webserver can write to the wp-content directory.', 'autoptimize' ); |
||
128 | ?> |
||
129 | </p></div> |
||
130 | <?php |
||
131 | } |
||
132 | } |
||
133 | |||
134 | // Check for Autoptimize. |
||
135 | if ( ! empty( $ao_ccss_key ) && ! $ao_css_defer ) { |
||
136 | ?> |
||
137 | <div class="notice-error notice"><p> |
||
138 | <?php |
||
139 | _e( "Oops! Please <strong>activate the \"Inline and Defer CSS\" option</strong> on Autoptimize's main settings page to use this power-up.", 'autoptimize' ); |
||
140 | ?> |
||
141 | </p></div> |
||
142 | <?php |
||
143 | return; |
||
144 | } |
||
145 | |||
146 | // check if WordPress cron is disabled and warn if so. |
||
147 | if ( ! empty( $ao_ccss_key ) && defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON && PAnD::is_admin_notice_active( 'i-know-about-disable-cron-forever' ) ) { |
||
148 | ?> |
||
149 | <div data-dismissible="i-know-about-disable-cron-forever" class="notice-warning notice is-dismissible"><p> |
||
150 | <?php |
||
151 | _e( 'WordPress cron (for task scheduling) seems to be disabled. Have a look at <a href="https://wordpress.org/plugins/autoptimize-criticalcss/faq/" target="_blank">the FAQ</a> or the info in the Job Queue instructions if all jobs remain in "N" status and no rules are created.', 'autoptimize' ); |
||
152 | ?> |
||
153 | </p></div> |
||
154 | <?php |
||
155 | } |
||
156 | |||
157 | // warn if it looks as though the queue processing job looks isn't running |
||
158 | // but store result in transient as to not to have to go through 2 arrays each and every time. |
||
159 | $_warn_cron = get_transient( 'ao_ccss_cronwarning' ); |
||
160 | if ( ! empty( $ao_ccss_key ) && false === $_warn_cron ) { |
||
161 | $_jobs_all_new = true; |
||
162 | $_oldest_job_timestamp = microtime( true ); // now. |
||
163 | $_jobs_too_old = true; |
||
164 | |||
165 | // go over queue array. |
||
166 | if ( empty( $ao_ccss_queue ) ) { |
||
167 | // no jobs, then no warning. |
||
168 | $_jobs_all_new = false; |
||
169 | } else { |
||
170 | foreach ( $ao_ccss_queue as $job ) { |
||
171 | if ( $job['jctime'] < $_oldest_job_timestamp ) { |
||
172 | // we need to catch the oldest job's timestamp. |
||
173 | $_oldest_job_timestamp = $job['jctime']; |
||
174 | } |
||
175 | |||
176 | if ( 'NEW' !== $job['jqstat'] && 'firstrun' !== $job['ljid'] ) { |
||
177 | // we have a non-"NEW" job which is not our pending firstrun job either, break the loop. |
||
178 | $_jobs_all_new = false; |
||
179 | break; |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | // is the oldest job too old (4h)? |
||
185 | if ( $_oldest_job_timestamp > microtime( true ) - 60 * 60 * 4 ) { |
||
186 | $_jobs_too_old = false; |
||
187 | } |
||
188 | |||
189 | if ( $_jobs_all_new && ! $this->ao_ccss_has_autorules() && $_jobs_too_old ) { |
||
190 | $_warn_cron = 'on'; |
||
191 | $_transient_multiplier = 1; // store for 1 hour. |
||
192 | } else { |
||
193 | $_warn_cron = 'off'; |
||
194 | $_transient_multiplier = 4; // store for 4 hours. |
||
195 | } |
||
196 | // and set transient. |
||
197 | set_transient( 'ao_ccss_cronwarning', $_warn_cron, $_transient_multiplier * HOUR_IN_SECONDS ); |
||
198 | } |
||
199 | |||
200 | if ( ! empty( $ao_ccss_key ) && 'on' == $_warn_cron && PAnD::is_admin_notice_active( 'i-know-about-cron-1' ) ) { |
||
201 | ?> |
||
202 | <div data-dismissible="i-know-about-cron-1" class="notice-warning notice is-dismissible"><p> |
||
203 | <?php |
||
204 | _e( 'It looks like there might be a problem with WordPress cron (task scheduling). Have a look at <a href="https://wordpress.org/plugins/autoptimize-criticalcss/faq/" target="_blank">the FAQ</a> or the info in the Job Queue instructions if all jobs remain in "N" status and no rules are created.', 'autoptimize' ); |
||
205 | ?> |
||
206 | </p></div> |
||
207 | <?php |
||
208 | } elseif ( ! empty( $ao_ccss_key ) && '2' == $ao_ccss_keyst && 'on' != $_warn_cron && ! $this->ao_ccss_has_autorules() ) { |
||
209 | ?> |
||
210 | <div class="notice-success notice"><p> |
||
211 | <?php |
||
212 | _e( 'Great, Autoptimize will now automatically start creating new critical CSS rules, you should see those appearing below in the next couple of hours.', 'autoptimize' ); |
||
213 | ?> |
||
214 | </p></div> |
||
215 | <?php |
||
216 | } |
||
217 | |||
218 | // warn if service is down. |
||
219 | if ( ! empty( $ao_ccss_key ) && ! empty( $ao_ccss_servicestatus ) && is_array( $ao_ccss_servicestatus ) && 'down' === $ao_ccss_servicestatus['critcss']['status'] ) { |
||
220 | ?> |
||
221 | <div class="notice-warning notice"><p> |
||
222 | <?php |
||
223 | _e( 'The critical CSS service has been reported to be down. Although no new rules will be created for now, this does not prevent existing rules from being applied.', 'autoptimize' ); |
||
224 | ?> |
||
225 | </p></div> |
||
226 | <?php |
||
227 | } |
||
228 | |||
229 | // Settings Form. |
||
230 | ?> |
||
231 | <form id="settings" method="post" action="options.php"> |
||
232 | <?php |
||
233 | settings_fields( 'ao_ccss_options_group' ); |
||
234 | |||
235 | // Get API key status. |
||
236 | $key = autoptimizeCriticalCSSCore::ao_ccss_key_status( true ); |
||
237 | |||
238 | if ( $this->is_multisite_network_admin() ) { |
||
239 | ?> |
||
240 | <ul id="key-panel"> |
||
241 | <li class="itemDetail"> |
||
242 | <?php |
||
243 | // translators: the placesholder is for a line of code in wp-config.php. |
||
244 | echo sprintf( __( '<p>Critical CSS settings cannot be set at network level as critical CSS is specific to each sub-site.</p><p>You can however provide the critical CSS API key for use by all sites by adding this your wp-config.php as %s</p>', 'autoptimize' ), '<br/><code>define(\'AUTOPTIMIZE_CRITICALCSS_API_KEY\', \'eyJhbGmorestringsherexHa7MkOQFtDFkZgLmBLe-LpcHx4\');</code>' ); |
||
245 | ?> |
||
246 | </li> |
||
247 | </ul> |
||
248 | <?php |
||
249 | } else { |
||
250 | if ( 'valid' == $key['status'] ) { |
||
251 | // If key status is valid, render other panels. |
||
252 | // Render rules section. |
||
253 | ao_ccss_render_rules(); |
||
254 | // Render queue section. |
||
255 | ao_ccss_render_queue(); |
||
256 | // Render advanced panel. |
||
257 | ao_ccss_render_adv(); |
||
258 | } else { |
||
259 | // But if key is other than valid, add hidden fields to persist settings when submitting form |
||
260 | // Show explanation of why and how to get a API key. |
||
261 | ao_ccss_render_explain(); |
||
262 | |||
263 | // Get viewport size. |
||
264 | $viewport = autoptimizeCriticalCSSCore::ao_ccss_viewport(); |
||
265 | |||
266 | // Add hidden fields. |
||
267 | echo "<input class='hidden' name='autoptimize_ccss_rules' value='" . $ao_ccss_rules_raw . "'>"; |
||
268 | echo "<input class='hidden' name='autoptimize_ccss_queue' value='" . $ao_ccss_queue_raw . "'>"; |
||
269 | echo '<input class="hidden" name="autoptimize_ccss_viewport[w]" value="' . $viewport['w'] . '">'; |
||
270 | echo '<input class="hidden" name="autoptimize_ccss_viewport[h]" value="' . $viewport['h'] . '">'; |
||
271 | echo '<input class="hidden" name="autoptimize_ccss_finclude" value="' . $ao_ccss_finclude . '">'; |
||
272 | echo '<input class="hidden" name="autoptimize_ccss_rlimit" value="' . $ao_ccss_rlimit . '">'; |
||
273 | echo '<input class="hidden" name="autoptimize_ccss_debug" value="' . $ao_ccss_debug . '">'; |
||
274 | echo '<input class="hidden" name="autoptimize_ccss_noptimize" value="' . $ao_ccss_noptimize . '">'; |
||
275 | echo '<input class="hidden" name="autoptimize_css_defer_inline" value="' . esc_attr( $ao_css_defer_inline ) . '">'; |
||
276 | echo '<input class="hidden" name="autoptimize_ccss_loggedin" value="' . $ao_ccss_loggedin . '">'; |
||
277 | echo '<input class="hidden" name="autoptimize_ccss_forcepath" value="' . $ao_ccss_forcepath . '">'; |
||
278 | } |
||
279 | // Render key panel unconditionally. |
||
280 | ao_ccss_render_key( $ao_ccss_key, $key['status'], $key['stmsg'], $key['msg'], $key['color'] ); |
||
281 | ?> |
||
282 | <p class="submit left"> |
||
283 | <input type="submit" class="button-primary" value="<?php _e( 'Save Changes', 'autoptimize' ); ?>" /> |
||
284 | </p> |
||
285 | <?php |
||
286 | } |
||
287 | ?> |
||
288 | </form> |
||
289 | <script> |
||
290 | jQuery("form#settings").submit(function(){ |
||
291 | var input = jQuery("#autoptimize_ccss_domain"); |
||
292 | input.val(rot(input.val(), 13)); |
||
293 | }); |
||
294 | // rot JS from http://stackoverflow.com/a/617685/987044 . |
||
295 | function rot(domainstring, itype) { |
||
296 | return domainstring.toString().replace(/[a-zA-Z]/g, function (letter) { |
||
297 | return String.fromCharCode((letter <= 'Z' ? 90 : 122) >= (letter = letter.charCodeAt(0) + itype) ? letter : letter - 26); |
||
298 | }); |
||
299 | } |
||
300 | </script> |
||
301 | <form id="importSettingsForm"<?php if ( $this->is_multisite_network_admin() ) { echo ' class="hidden"'; } ?>> |
||
302 | <span id="exportSettings" class="button-secondary"><?php _e( 'Export Settings', 'autoptimize' ); ?></span> |
||
303 | <input class="button-secondary" id="importSettings" type="button" value="<?php _e( 'Import Settings', 'autoptimize' ); ?>" onclick="upload();return false;" /> |
||
304 | <input class="button-secondary" id="settingsfile" name="settingsfile" type="file" /> |
||
305 | </form> |
||
306 | <div id="importdialog"></div> |
||
307 | </div><!-- /#autoptimize_main --> |
||
308 | </div><!-- /#wrap --> |
||
309 | <?php |
||
310 | if ( ! $this->is_multisite_network_admin() ) { |
||
311 | // Include debug panel if debug mode is enable. |
||
312 | if ( $ao_ccss_debug ) { |
||
313 | ?> |
||
314 | <div id="debug"> |
||
315 | <?php |
||
316 | // Include debug panel. |
||
317 | include( 'critcss-inc/admin_settings_debug.php' ); |
||
318 | ?> |
||
319 | </div><!-- /#debug --> |
||
320 | <?php |
||
321 | } |
||
322 | echo '<script>'; |
||
323 | include( 'critcss-inc/admin_settings_rules.js.php' ); |
||
324 | include( 'critcss-inc/admin_settings_queue.js.php' ); |
||
325 | include( 'critcss-inc/admin_settings_impexp.js.php' ); |
||
326 | echo '</script>'; |
||
327 | } |
||
328 | } |
||
329 | |||
369 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.