PluginProbe
Autover / 1.3
Autover v1.3
1.9 trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8
autover / autover.php

autover.php in Autover 1.3, at autover.php

726 lines 19.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Plugin Name: AutoVer
4 * Plugin URI: http://wordpress.org/extend/plugins/autover/
5 * Description: Automatically version your CSS and JS files.
6 * Author: PressLabs
7 * Version: 1.3
8 * Author URI: http://www.presslabs.com/
9 */
10
11 require_once('simplehtmldom.php');
12
13 //--------------------------------------------------------------------
14
15 function autover_activate() {
16 autover_delete_options();
17
18 add_option('autover_dev_mode', array('1','1') );
19 add_option('autover_is_working', true );
20
21 add_option('autover_versioned_css_files', array() );
22 add_option('autover_not_versioned_css_files', array() );
23 add_option('autover_not_correct_css_files', array() );
24
25 add_option('autover_versioned_js_files', array() );
26 add_option('autover_not_versioned_js_files', array() );
27 add_option('autover_not_correct_js_files', array() );
28 }
29 register_activation_hook(__FILE__,'autover_activate');
30
31 //--------------------------------------------------------------------
32
33 function autover_deactivate() {
34 autover_delete_options();
35 }
36 register_deactivation_hook(__FILE__,'autover_deactivate');
37
38 //--------------------------------------------------------------------
39
40 function autover_delete_options() {
41 delete_option('autover_dev_mode');
42 delete_option('autover_is_working');
43
44 delete_option('autover_versioned_css_files');
45 delete_option('autover_not_versioned_css_files');
46 delete_option('autover_not_correct_css_files');
47
48 delete_option('autover_versioned_js_files');
49 delete_option('autover_not_versioned_js_files');
50 delete_option('autover_not_correct_js_files');
51 }
52
53 //--------------------------------------------------------------------
54 //
55 // Add settings link on plugin page.
56 //
57 function autover_settings_link($links) {
58 $plugin = plugin_basename(__FILE__);
59 $settings_link = '<a href="tools.php?page='.$plugin.'">'. __('Settings').'</a>';
60 array_unshift($links, $settings_link);
61
62 return $links;
63 }
64
65 $plugin = plugin_basename(__FILE__);
66 add_filter("plugin_action_links_$plugin", 'autover_settings_link' );
67
68 //--------------------------------------------------------------------
69 //
70 // Return the string between 'start' and 'end' from 'conent'.
71 //
72 function autover_str_between( $start, $end, $content ) {
73 $r = explode($start, $content);
74
75 if (isset($r[1])) {
76 $r = explode($end, $r[1]);
77 return $r[0];
78 }
79
80 return '';
81 }
82
83 //------------------------------------------------------------------------
84
85 function autover_string2link($string) {
86 return '<a href="'.$string.'">'.$string.'</a>';
87 }
88
89 //--------------------------------------------------------------------
90 //
91 // Remove the query from src.
92 //
93 function autover_remove_query($src) {
94 $src_query = autover_str_between( '?', '#', $src . '#' ); // Find the query.
95 $src_with_no_query = str_replace( $src_query, '', $src ); // Remove query if exist.
96 $src_with_no_query = str_replace( '?', '', $src_with_no_query ); // Remove '?' char.
97
98 return $src_with_no_query;
99 }
100
101 //--------------------------------------------------------------------
102 //
103 // Just for debug
104 //
105 function autover_add_incorrect_style_and_script() {
106 echo '
107 <link rel="stylesheet" type="text/css" href="'.plugins_url('/autover.css',__FILE__).'" />
108 <script src="'.plugins_url('/autover.js',__FILE__).'"></script>
109 ';
110 }
111 //add_action('wp_head', 'autover_add_incorrect_style_and_script');
112 //add_action('admin_head', 'autover_add_incorrect_style_and_script');
113
114 //--------------------------------------------------------------------
115 //
116 // Return the file with the new version.
117 //
118 function autover_version_filter($src) {
119 $out_src = $src;
120
121 //
122 // Remove the old version if exist.
123 //
124 $src_with_no_query = autover_remove_query( $out_src );
125
126 //
127 // Get the filetype of the file (JS or CSS)
128 //
129 ( strtolower( substr( $src_with_no_query, -2 ) ) == 'js' ) ? $filetype = 'js' : null ;
130 ( strtolower( substr( $src_with_no_query, -3 ) ) == 'css' ) ? $filetype = 'css' : null ;
131
132 $active = false;
133 $autover_dev_mode = get_option('autover_dev_mode', array('','') );
134 if ( (($autover_dev_mode[0] <> '') && ($filetype == 'css')) || (($autover_dev_mode[1] <> '') && ($filetype == 'js')) )
135 $active = true;
136
137 //
138 // get versioned/not_versioned files
139 //
140 $autover_versioned_files = get_option('autover_versioned_'.$filetype.'_files', array() );
141 $autover_not_versioned_files = get_option('autover_not_versioned_'.$filetype.'_files', array() );
142
143 //
144 // Parse the url of the input file.
145 //
146 $src_parsed = parse_url( $out_src );
147 $src_path = $src_parsed["path"];
148
149 $filename = $_SERVER['DOCUMENT_ROOT'] . $src_path;
150 if ( is_file( $filename ) )
151 {
152 //
153 // Extract the modification time of the input file.
154 //
155 $timestamp_version = filemtime( $filename );
156
157 if ( $timestamp_version == NULL )
158 $timestamp_version = filemtime( utf8_decode( $filename ) );
159 } else {
160 //
161 // Add not versioned files.
162 //
163 array_push($autover_not_versioned_files, $out_src);
164 $autover_not_versioned_files = array_unique($autover_not_versioned_files);
165 sort($autover_not_versioned_files);
166 update_option('autover_not_versioned_'.$filetype.'_files', $autover_not_versioned_files );
167
168 return $out_src;
169 }
170
171 //
172 // If the file is not on the server then return the input file.
173 //
174 if ( ($timestamp_version == '') || ($timestamp_version == NULL) ) {
175 //
176 // Add not versioned files.
177 //
178 array_push($autover_not_versioned_files, $out_src);
179 $autover_not_versioned_files = array_unique($autover_not_versioned_files);
180 sort($autover_not_versioned_files);
181 update_option('autover_not_versioned_'.$filetype.'_files', $autover_not_versioned_files );
182
183 return $out_src;
184 }
185
186 //
187 // Create the new version.
188 //
189 $src_with_new_version = $src_with_no_query . '?ver=' . $timestamp_version;
190
191 //
192 // Add versioned files.
193 //
194 array_push($autover_versioned_files, $src_with_no_query);
195 $autover_versioned_files = array_unique($autover_versioned_files);
196 sort($autover_versioned_files);
197 update_option('autover_versioned_'.$filetype.'_files', $autover_versioned_files );
198
199 if ( $active ) $out_src = $src_with_new_version;
200
201 return $out_src;
202 }
203
204 //
205 // Activate/deactivate the filter if the options are set.
206 //
207 //$autover_dev_mode = get_option('autover_dev_mode', array('','') );
208
209 //if ( $autover_dev_mode[0] <> '' )
210 add_filter( 'style_loader_src', 'autover_version_filter', 10, 1 );
211
212 //if ( $autover_dev_mode[1] <> '' )
213 add_filter( 'script_loader_src', 'autover_version_filter', 10, 1 );
214
215 //--------------------------------------------------------------------
216
217 function autover_update_options() {
218 $autover_dev_mode_value = array( '', '' );
219
220 $autover_is_working = false;
221 if ( isset( $_POST['autover_dev_mode_style'] ) ) {
222 $autover_dev_mode_value[0] = '1';
223 $autover_is_working = true;
224 } else {
225 delete_option('autover_versioned_css_files');
226 delete_option('autover_not_versioned_css_files');
227 delete_option('autover_not_correct_css_files');
228 }
229
230 if ( isset( $_POST['autover_dev_mode_script'] ) ) {
231 $autover_dev_mode_value[1] = '1';
232 $autover_is_working = true;
233 } else {
234 delete_option('autover_versioned_js_files');
235 delete_option('autover_not_versioned_js_files');
236 delete_option('autover_not_correct_js_files');
237 }
238
239 update_option('autover_dev_mode', $autover_dev_mode_value);
240 update_option('autover_is_working', $autover_is_working );
241
242 if ($autover_is_working) { ?>
243 <div id="message" class="updated fade">
244 <p><strong>Saved options!</strong></p>
245 </div>
246 <?php } else {
247 delete_option('autover_dev_mode');
248 delete_option('autover_versioned_css_files');
249 delete_option('autover_not_versioned_css_files');
250 delete_option('autover_not_correct_css_files');
251
252 delete_option('autover_versioned_js_files');
253 delete_option('autover_not_versioned_js_files');
254 delete_option('autover_not_correct_js_files');
255 }
256 }
257 //--------------------------------------------------------------------
258
259 function autover_options() {
260 if ( isset( $_POST['submit_settings'] ) ) {
261 autover_update_options();
262 }
263
264 isset($_GET['tab']) ? $tab = $_GET['tab'] : $tab = 'important';
265
266 ?>
267
268 <div class="wrap">
269
270 <div id="icon-tools" class="icon32">&nbsp;</div>
271 <h2 class="nav-tab-wrapper">
272 <a class="nav-tab<?php if($tab=='important')echo' nav-tab-active';?>" href="tools.php?page=autover/autover.php&tab=important"><span style="color:red;">IMPORTANT!</span></a>
273 <a class="nav-tab<?php if($tab=='settings')echo' nav-tab-active';?>" href="tools.php?page=autover/autover.php&tab=settings">Settings</a>
274 <a class="nav-tab<?php if($tab=='lists')echo' nav-tab-active';?>" href="tools.php?page=autover/autover.php&tab=lists">Lists</a>
275 </h2>
276
277
278
279
280
281
282
283 <?php if ( $tab == 'important' ) { ?>
284
285 <?php
286 $autover_is_working = get_option('autover_is_working', true);
287 if ( !$autover_is_working ) { ?>
288 <div id="message" class="error fade">
289 <p>
290 <strong>
291 <span style="color:brown;">
292 This plugin is currently not used!
293 </span>
294 </strong>
295 </p>
296 </div>
297 <?php
298 }
299 ?>
300
301
302
303 <p>
304 If you want to use the functionality of this plugin you must add
305 <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script" target="_blank"><strong>'wp_enqueue_script'</strong></a> and
306 <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_style" target="_blank"><strong>'wp_enqueue_style'</strong></a>.
307 </p>
308
309
310 <p>
311 <h3><span style="color:black;font-weight:bold;">Example:</span></h3>
312
313 <h3><span style="color:green;font-weight:bold;">YES</span></h3>
314 <p style="background:#eaeaea; padding:5px;" title="CORRECT CODE">
315 <?php
316 $string = "<?php
317 function autover_add_style_and_script() {
318 wp_enqueue_style('autover', plugins_url('/autover/mystyle.css',__FILLE_));
319 wp_enqueue_script('autover', plugins_url('/autover/myscript.js',__FILLE_));
320 }
321 add_action('wp_enqueue_scripts','autover_add_style_and_script');
322 add_action('admin_enqueue_scripts','autover_add_style_and_script');
323 ?>";
324 highlight_string($string); ?>
325 </p>
326
327 <!--img src="<?php echo plugins_url('/img/wp-enqueue.png', __FILE__); ?>" alt="wp-enqueue" title="CORRECT CODE"-->
328
329 <h3><span style="color:red;font-weight:bold;">NO</span></h3>
330 <p style="background:#eaeaea; padding:5px;" title="DO NOT USE THIS CODE">
331 <?php
332 $string = "<?php
333 function autover_add_style_and_script() {
334 echo \"<link rel='stylesheet' href='\".plugins_url('/autover/mystyle.js',__FILLE_)
335 .\"' type='test/css' media='all' />
336 <script src='\".plugins_url('/autover/mystyle.css',__FILLE_).\"'></script>
337 \";
338 }
339 add_action('wp_head','autover_add_style_and_script');
340 add_action('admin_head','autover_add_style_and_script');
341 ?>";
342 highlight_string($string); ?>
343 </p>
344
345 <!--img src="<?php echo plugins_url('/img/wp-head.png', __FILE__); ?>" alt="wp-head" title="DO NOT USE THIS CODE"-->
346
347 <h3 style="font-weight:normal;">If you want to use <strong>'wp_enqueue_style'</strong> to add your <strong>'style.css'</strong> of your theme.<br />Add the next code to your theme file <strong>'functions.php'</strong> <span style="color:red;font-weight:bold;">and remove your &lt;link&gt; tag</span> from <strong>'header.php'</strong> which refer to your <strong>'style.css'</strong>.</h3>
348 <p style="background:#eaeaea; padding:5px;" title="add this code to 'functions.php' file">
349 <?php
350 $string = "<?php
351 function mythemename_style() {
352 \$style_url = get_stylesheet_directory_uri() . '/style.css';
353
354 wp_enqueue_style('my_style_id', \$style_url, __FILE__);
355 }
356 add_action('wp_enqueue_scripts','mythemename_style');
357 ?>";
358 highlight_string($string); ?>
359 </p>
360
361 <!--img src="<?php echo plugins_url('/img/my-theme-name.png', __FILE__); ?>" alt="mythemename" title="add this code to 'functions.php' file"-->
362 </p>
363
364 <?php } ?>
365
366
367
368
369
370
371
372
373 <?php if ( $tab == 'settings' ) { ?>
374
375 <?php
376 $autover_is_working = get_option('autover_is_working', true);
377 if ( !$autover_is_working ) { ?>
378 <div id="message" class="error fade">
379 <p>
380 <strong>
381 <span style="color:brown;">
382 This plugin is currently not used!
383 </span>
384 </strong>
385 </p>
386 </div>
387 <?php
388 }
389
390 $autover_dev_mode = get_option('autover_dev_mode');
391 $dev_mode_checked = array('','');
392 for ( $k = 0; $k < 2; $k++ )
393 if ( $autover_dev_mode[$k] == '1' )
394 $dev_mode_checked[$k] = ' checked="checked"';
395 ?>
396
397 <form method="post">
398 <table class="form-table">
399 <tbody>
400 <tr valign="top">
401 <th scope="row">
402 <label for="autover_dev_mode">Developer mode</label>
403 </th>
404 <td>
405 <fieldset>
406
407 <legend class="screen-reader-text"><span>Developer mode</span></legend>
408 <label for="autover_dev_mode_style">
409 <input name="autover_dev_mode_style" id="autover_dev_mode_style" value="<?php echo$autover_dev_mode[0];?>" type="checkbox"<?php echo$dev_mode_checked[0];?>>
410 <span>CSS files</span>
411 </label><br />
412
413 <label for="autover_dev_mode_script">
414 <input name="autover_dev_mode_script" id="autover_dev_mode_script" value="<?php echo$autover_dev_mode[1];?>" type="checkbox"<?php echo$dev_mode_checked[1];?>>
415 <span>JavaScript files</span>
416 </label><br />
417
418 <p class="description">The file type you want to rewrite the version.</p>
419
420 </fieldset>
421 </td>
422 </tr>
423
424 <tr valign="top">
425 <td>
426 </td>
427 </tr>
428 </tbody>
429 </table>
430
431 <p class="submit">
432 <input type="submit" class="button button-primary" name="submit_settings" value="Save Changes">
433 </p>
434
435 </form>
436
437
438 <?php } ?>
439
440
441
442
443
444
445
446
447
448
449
450 <?php if ( $tab == 'lists' ) { ?>
451
452 <?php
453 $autover_is_working = get_option('autover_is_working', true);
454 if ( !$autover_is_working ) { ?>
455 <div id="message" class="error fade">
456 <p>
457 <strong>
458 <span style="color:brown;">
459 This plugin is currently not used!
460 </span>
461 </strong>
462 </p>
463 </div>
464 <?php
465 }
466 ?>
467
468 <?php if ( isset($_POST['reset_lists']) ) autover_reset_lists(); ?>
469
470 <?php if ( isset($_POST['refresh_lists']) ) autover_refresh_lists(); ?>
471
472 <form method="post">
473 <p class="submit">
474 Remove all data from the file lists (CSS and JS): <input type="submit" class="button button-primary" name="reset_lists" value="Reset"><br />
475 Scan the Homepage and detect the files added wrong (CSS and JS files): <input type="submit" class="button" name="refresh_lists" value="Refresh"><br />
476 </p>
477 </form>
478
479 <?php
480 if ( $autover_is_working ) {
481 autover_show_not_correct_files();
482 autover_show_versioned_files();
483 autover_show_not_versioned_files();
484 }
485 ?>
486
487 <?php } ?>
488
489
490
491
492
493
494
495 </div><!-- .wrap -->
496
497 <?php
498 }
499
500 //--------------------------------------------------------------------
501 // Remove all data from the file lists (CSS and JS).
502 function autover_reset_lists() {
503 update_option('autover_versioned_css_files', array() );
504 update_option('autover_versioned_js_files', array() );
505
506 update_option('autover_not_versioned_css_files', array() );
507 update_option('autover_not_versioned_js_files', array() );
508
509 update_option('autover_not_correct_css_files', array() );
510 update_option('autover_not_correct_js_files', array() );
511 }
512
513 //--------------------------------------------------------------------
514 // Scan the Homepage and detect the files added wrong (CSS and JS files).
515 function autover_refresh_lists() {
516 autover_scan_not_correct_files();
517 }
518
519 //--------------------------------------------------------------------
520 function autover_show_versioned_files() {
521 ?>
522
523 <?php
524 $autover_versioned_css_files = get_option('autover_versioned_css_files', array() );
525 if ( !empty( $autover_versioned_css_files ) ) { ?>
526 <fieldset>
527 <pre>
528 <legend><strong>Versioned CSS files:</strong></legend>
529 <?php
530 $k = 1;
531 $empty_list = true;
532 sort($autover_versioned_css_files);
533 foreach( $autover_versioned_css_files as $versioned_css_file ) {
534 echo $k . ") " . autover_string2link($versioned_css_file)."\n";
535 $empty_list = false;
536 $k++;
537 }
538 ?>
539 </pre>
540 </fieldset>
541 <?php if ( !$empty_list ) echo "<hr>";
542 }
543 ?>
544
545
546
547 <?php
548 $autover_versioned_js_files = get_option('autover_versioned_js_files', array() );
549 if ( !empty( $autover_versioned_js_files ) ) { ?>
550 <fieldset>
551 <pre>
552 <legend><strong>Versioned JS files:</strong></legend>
553 <?php
554 $k = 1;
555 $empty_list = true;
556 sort($autover_versioned_js_files);
557 foreach( $autover_versioned_js_files as $versioned_js_file ) {
558 echo $k . ") " . autover_string2link($versioned_js_file)."\n";
559 $empty_list = false;
560 $k++;
561 }
562 ?>
563 </pre>
564 </fieldset>
565 <?php if ( !$empty_list ) echo "<hr>";
566 }
567 ?>
568
569 <?php
570 }
571
572 //--------------------------------------------------------------------
573
574 function autover_show_not_versioned_files() {
575 ?>
576
577 <?php
578 $message = false;
579 $autover_not_versioned_css_files = get_option('autover_not_versioned_css_files', array() );
580 if ( !empty( $autover_not_versioned_css_files ) ) {
581 $message = true;
582 ?>
583 <p>From various reasons, the next files are not versioned!</p>
584 <fieldset>
585 <pre>
586 <legend><strong>Not versioned CSS files:</strong></legend>
587 <?php
588 $k = 1;
589 $empty_list = true;
590
591 sort($autover_not_versioned_css_files);
592 foreach( $autover_not_versioned_css_files as $not_versioned_css_file ) {
593 echo $k . ") " . autover_string2link($not_versioned_css_file)."\n";
594 $empty_list = false;
595 $k++;
596 }
597 ?>
598 </pre>
599 </fieldset>
600 <?php if ( !$empty_list ) echo "<hr>";
601 }
602 ?>
603
604
605
606 <?php
607 $autover_not_versioned_js_files = get_option('autover_not_versioned_js_files', array() );
608 if ( !empty( $autover_not_versioned_js_files ) ) {
609 if (!$message) { ?>
610 <p>For various reason, the next files are not versioned!</p>
611 <?php } ?>
612 <fieldset>
613 <pre>
614 <legend><strong>Not versioned JS files:</strong></legend>
615 <?php
616 $k = 1;
617 $empty_list = true;
618
619 sort($autover_not_versioned_js_files);
620 foreach( $autover_not_versioned_js_files as $not_versioned_js_file ) {
621 echo $k . ") " . autover_string2link($not_versioned_js_file)."\n";
622 $empty_list = false;
623 $k++;
624 }
625 ?>
626 </pre>
627 </fieldset>
628 <?php if ( !$empty_list ) echo "<hr>";
629 }
630 ?>
631
632 <?php
633 }
634
635 //--------------------------------------------------------------------
636
637 function autover_scan_not_correct_files() {
638 $autover_versioned_css_files = get_option('autover_versioned_css_files', array() );
639 $autover_versioned_js_files = get_option('autover_versioned_js_files', array() );
640
641 $autover_not_versioned_css_files = get_option('autover_not_versioned_css_files', array() );
642 $autover_not_versioned_js_files = get_option('autover_not_versioned_js_files', array() );
643 ?>
644
645 <?php
646 // Create DOM from URL or file
647 $html = file_get_html( get_home_url() ); //$_SERVER['HTTP_REFERER'] );
648
649 // Find all external style sheet
650 $links = array();
651 foreach($html->find('link') as $link_element) {
652 $src = autover_remove_query( $link_element->href );
653 if ( substr($src, -4) == '.css' )
654 array_push( $links, $src );
655 }
656 $autover_css_files = array_merge($autover_versioned_css_files, $autover_not_versioned_css_files);
657 $out_intersect = array_intersect( $autover_css_files, $links );
658 $out_links = array_unique( array_diff( $links, $out_intersect ) );
659
660
661 // Find all external JS scripts
662 $scripts = array();
663 foreach($html->find('script') as $script_element) {
664 $src = autover_remove_query( $script_element->src );
665 array_push( $scripts, $src );
666 }
667 $autover_js_files = array_merge($autover_versioned_js_files, $autover_not_versioned_js_files);
668 $out_intersect = array_intersect( $autover_js_files, $scripts );
669 $out_scripts = array_unique( array_diff( $scripts, $out_intersect ) );
670
671 sort($out_links); //print_r($out_links);
672 sort($out_scripts); //print_r($out_scripts);
673
674 update_option('autover_not_correct_css_files', $out_links );
675 update_option('autover_not_correct_js_files', $out_scripts );
676 }
677
678 //------------------------------------------------------------------------
679
680 function autover_show_not_correct_files() {
681 $out_links = get_option('autover_not_correct_css_files', null);
682 $out_scripts = get_option('autover_not_correct_js_files', null);
683 ?>
684 <fieldset>
685 <pre>
686 <?php if ( !empty($out_links) || !empty($out_scripts) ) { ?>
687 <legend><strong>Add the next files with correct method:</strong></legend>
688 <?php
689 $k = 1;
690 $empty_list = true;
691
692 sort($out_links);
693 foreach($out_links as $link) {
694 echo $k . ") " . autover_string2link($link) . "\n";
695 $empty_list = false;
696 $k++;
697 }
698
699 sort($out_scripts);
700 foreach($out_scripts as $script) {
701 echo $k . ") " . autover_string2link($script) . "\n";
702 $empty_list = false;
703 $k++;
704 }
705 }
706 ?>
707 </pre>
708 </fieldset>
709
710 <?php if ( !$empty_list ) echo "<hr>";
711 }
712
713 //--------------------------------------------------------------------
714
715 function autover_menu() {
716 add_management_page(
717 'AutoVer - Options', //'custom menu title',
718 'AutoVer', //'custom menu',
719 'administrator', //'add_users',
720 __FILE__, //$menu_slug,
721 'autover_options'
722 );
723 }
724 add_action('admin_menu', 'autover_menu');
725
726