| 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.1 |
| 8 |
* Author URI: http://www.presslabs.com/ |
| 9 |
*/ |
| 10 |
|
| 11 |
//-------------------------------------------------------------------- |
| 12 |
|
| 13 |
function autover_activate() { |
| 14 |
add_option('autover_dev_mode', array('1','1') ); |
| 15 |
} |
| 16 |
register_activation_hook(__FILE__,'autover_activate'); |
| 17 |
|
| 18 |
//-------------------------------------------------------------------- |
| 19 |
|
| 20 |
function autover_deactivate() { |
| 21 |
delete_option('autover_dev_mode'); |
| 22 |
} |
| 23 |
register_deactivation_hook(__FILE__,'autover_deactivate'); |
| 24 |
|
| 25 |
//-------------------------------------------------------------------- |
| 26 |
// |
| 27 |
// Add settings link on plugin page. |
| 28 |
// |
| 29 |
function autover_settings_link($links) { |
| 30 |
$plugin = plugin_basename(__FILE__); |
| 31 |
$settings_link = '<a href="tools.php?page='.$plugin.'">Settings</a>'; |
| 32 |
array_unshift($links, $settings_link); |
| 33 |
|
| 34 |
return $links; |
| 35 |
} |
| 36 |
|
| 37 |
$plugin = plugin_basename(__FILE__); |
| 38 |
add_filter("plugin_action_links_$plugin", 'autover_settings_link' ); |
| 39 |
|
| 40 |
//-------------------------------------------------------------------- |
| 41 |
// |
| 42 |
// Return the string between 'start' and 'end' from 'conent'. |
| 43 |
// |
| 44 |
function autover_str_between( $start, $end, $content ) { |
| 45 |
$r = explode($start, $content); |
| 46 |
|
| 47 |
if (isset($r[1])) { |
| 48 |
$r = explode($end, $r[1]); |
| 49 |
return $r[0]; |
| 50 |
} |
| 51 |
|
| 52 |
return ''; |
| 53 |
} |
| 54 |
|
| 55 |
//-------------------------------------------------------------------- |
| 56 |
// |
| 57 |
// Return the file with the new version. |
| 58 |
// |
| 59 |
function autover_version_filter($src) { |
| 60 |
// |
| 61 |
// Parse the url of the input file. |
| 62 |
// |
| 63 |
$src_parsed = parse_url( $src ); |
| 64 |
$src_path = $src_parsed["path"]; |
| 65 |
|
| 66 |
// |
| 67 |
// Extract the modification time of the input file. |
| 68 |
// |
| 69 |
$filename = $_SERVER['DOCUMENT_ROOT'] . $src_path; |
| 70 |
$timestamp_version = filemtime( $filename ); |
| 71 |
|
| 72 |
// |
| 73 |
// If the file is not on the server then return the input file. |
| 74 |
// |
| 75 |
if ( $timestamp_version == '' ) return $src; |
| 76 |
|
| 77 |
// |
| 78 |
// Remove the old version if exist. |
| 79 |
// |
| 80 |
$src_query = autover_str_between( '?', '#', $src . '#' ); // Find the query. |
| 81 |
$src_with_no_query = str_replace( $src_query, '', $src ); // Remove query if exist. |
| 82 |
$src_with_no_query = str_replace( '?', '', $src_with_no_query ); // Remove '?' char. |
| 83 |
|
| 84 |
// |
| 85 |
// Create the new version. |
| 86 |
// |
| 87 |
$src_with_new_version = $src_with_no_query . '?ver=' . $timestamp_version; |
| 88 |
|
| 89 |
return $src_with_new_version; |
| 90 |
} |
| 91 |
|
| 92 |
// |
| 93 |
// ASctivate/deactivate the filter if the options are set. |
| 94 |
// |
| 95 |
$autover_dev_mode = get_option('autover_dev_mode', array('','') ); |
| 96 |
|
| 97 |
if ( $autover_dev_mode[0] > '' ) |
| 98 |
add_filter( 'style_loader_src', 'autover_version_filter', 10, 1 ); |
| 99 |
|
| 100 |
if ( $autover_dev_mode[1] > '' ) |
| 101 |
add_filter( 'script_loader_src', 'autover_version_filter', 10, 1 ); |
| 102 |
|
| 103 |
//-------------------------------------------------------------------- |
| 104 |
|
| 105 |
function autover_update_options() { |
| 106 |
$autover_dev_mode_value = array( '', '' ); |
| 107 |
|
| 108 |
$status = false; |
| 109 |
if ( isset( $_POST['autover_dev_mode_style'] ) ) { |
| 110 |
$autover_dev_mode_value[0] = '1'; |
| 111 |
$status = true; |
| 112 |
} |
| 113 |
if ( isset( $_POST['autover_dev_mode_script'] ) ) { |
| 114 |
$autover_dev_mode_value[1] = '1'; |
| 115 |
$status = true; |
| 116 |
} |
| 117 |
update_option('autover_dev_mode', $autover_dev_mode_value); |
| 118 |
//if ( ($autover_dev_mode_value[0]=='') && ($autover_dev_mode_value[1]=='') ) |
| 119 |
//$status = false; |
| 120 |
|
| 121 |
if ($status) { ?> |
| 122 |
<div id="message" class="updated fade"> |
| 123 |
<p><strong>Saved options!</strong></p> |
| 124 |
</div> |
| 125 |
<?php } else { ?> |
| 126 |
<div id="message" class="error fade"> |
| 127 |
<p> |
| 128 |
<strong> |
| 129 |
<span style="color:brown;"> |
| 130 |
This plugin is currently not used! |
| 131 |
</span> |
| 132 |
</strong> |
| 133 |
</p> |
| 134 |
</div> |
| 135 |
<?php } |
| 136 |
} |
| 137 |
|
| 138 |
//-------------------------------------------------------------------- |
| 139 |
|
| 140 |
function autover_options() { |
| 141 |
if ( isset( $_POST['submit_settings'] ) ) { |
| 142 |
autover_update_options(); |
| 143 |
} |
| 144 |
?> |
| 145 |
|
| 146 |
<div class="wrap"> |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
<div id="icon-tools" class="icon32"> </div> |
| 151 |
<h2>Settings</h2> |
| 152 |
|
| 153 |
|
| 154 |
<?php |
| 155 |
$autover_dev_mode = get_option('autover_dev_mode'); |
| 156 |
$dev_mode_checked = array('',''); |
| 157 |
for ( $k = 0; $k < 2; $k++ ) |
| 158 |
if ( $autover_dev_mode[$k] == '1' ) |
| 159 |
$dev_mode_checked[$k] = ' checked="checked"'; |
| 160 |
?> |
| 161 |
|
| 162 |
<form method="post"> |
| 163 |
<table class="form-table"> |
| 164 |
<tbody> |
| 165 |
<tr valign="top"> |
| 166 |
<th scope="row"> |
| 167 |
<label for="autover_dev_mode">Developer mode</label> |
| 168 |
</th> |
| 169 |
<td> |
| 170 |
<fieldset> |
| 171 |
|
| 172 |
<legend class="screen-reader-text"><span>Developer mode</span></legend> |
| 173 |
<label for="autover_dev_mode_style"> |
| 174 |
<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];?>> |
| 175 |
<span>CSS files</span> |
| 176 |
</label><br /> |
| 177 |
|
| 178 |
<label for="autover_dev_mode_script"> |
| 179 |
<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];?>> |
| 180 |
<span>JavaScript files</span> |
| 181 |
</label><br /> |
| 182 |
|
| 183 |
<p class="description">The file type you want to rewrite the version.</p> |
| 184 |
|
| 185 |
</fieldset> |
| 186 |
</td> |
| 187 |
</tr> |
| 188 |
|
| 189 |
<tr valign="top"> |
| 190 |
<td> |
| 191 |
</td> |
| 192 |
</tr> |
| 193 |
</tbody> |
| 194 |
</table> |
| 195 |
|
| 196 |
<p class="submit"> |
| 197 |
<input type="submit" class="button button-primary" name="submit_settings" value="Save Changes"> |
| 198 |
</p> |
| 199 |
|
| 200 |
</form> |
| 201 |
|
| 202 |
|
| 203 |
<p> |
| 204 |
<h3><span style="color:red;font-weight:bold;">IMPORTANT !!!</span></h3> |
| 205 |
If you want to use the functionality of this plugin you must add |
| 206 |
<strong>CSS styles</strong> and <strong>JS scripts</strong> with WordPress function |
| 207 |
<strong>'wp_enqueue_script'</strong> and <strong>'wp_enqueue_style'</strong>. |
| 208 |
</p> |
| 209 |
|
| 210 |
|
| 211 |
<p> |
| 212 |
<h3><span style="color:black;font-weight:bold;">Example:</span></h3> |
| 213 |
|
| 214 |
<h3><span style="color:green;font-weight:bold;">YES</span></h3> |
| 215 |
<img src="<?php echo plugins_url('/img/wp-enqueue.png', __FILE__); ?>" alt="wp-enqueue" title="CORRECT CODE"> |
| 216 |
|
| 217 |
<h3><span style="color:red;font-weight:bold;">NO</span></h3> |
| 218 |
<img src="<?php echo plugins_url('/img/wp-head.png', __FILE__); ?>" alt="wp-head" title="DO NOT USE THIS CODE"> |
| 219 |
|
| 220 |
<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 <link> tag</span> from <strong>'header.php'</strong> which refer to your <strong>'style.css'</strong>.</h3> |
| 221 |
<img src="<?php echo plugins_url('/img/mythemename.png', __FILE__); ?>" alt="mythemename" title="add this code to 'functions.php' file"> |
| 222 |
</p> |
| 223 |
|
| 224 |
|
| 225 |
</div><!-- .wrap --> |
| 226 |
<?php } |
| 227 |
|
| 228 |
//-------------------------------------------------------------------- |
| 229 |
|
| 230 |
function autover_menu() { |
| 231 |
add_management_page( |
| 232 |
'AutoVer - Options', //'custom menu title', |
| 233 |
'AutoVer', //'custom menu', |
| 234 |
'administrator', //'add_users', |
| 235 |
__FILE__, //$menu_slug, |
| 236 |
'autover_options' |
| 237 |
); |
| 238 |
} |
| 239 |
add_action('admin_menu', 'autover_menu'); |
| 240 |
|
| 241 |
?> |
| 242 |
|