-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwp-performance-score-booster.php
383 lines (301 loc) · 15.5 KB
/
wp-performance-score-booster.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
/****************************************
Plugin Name: WP Performance Score Booster
Plugin URI: https://github.com/dipakcg/wp-performance-score-booster
Description: Makes website faster, speeds up page load time, and instantly improves website performance scores in services like Google PageSpeed Insights, GTmetrix, Pingdom, and more.
Version: 2.2.3
Author: Dipak C. Gajjar
Author URI: https://dipakgajjar.com
License: GPL-2.0+
Text Domain: wp-performance-score-booster
Contributors: dipakcg (for wordpress.org submission)
****************************************/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;
include_once ( ABSPATH . 'wp-admin/includes/file.php' ); // to get get_home_path() function work
include_once ( ABSPATH . 'wp-admin/includes/plugin.php' ); // to is_plugin_active()() function work
// Define plugin version for future releases
if ( ! defined( 'WPPSB_PLUGIN_VERSION' ) ) {
define( 'WPPSB_PLUGIN_VERSION', '2.2.3' );
}
if ( ! defined( 'WPPSB_BASE' ) ) {
define( 'WPPSB_BASE', plugin_basename( __FILE__ ) ); // wp-performance-score-booster/wp-performance-score-booster.php
}
if ( ! defined( 'WPPSB_PATH' ) ) {
define( 'WPPSB_PATH', plugin_dir_path( __FILE__ ) ); // /home/user/var/www/wordpress/wp-content/plugins/wp-performance-score-booster/
}
if ( ! defined( 'WPPSB_URL' ) ) {
define( 'WPPSB_URL', plugin_dir_url( __FILE__ ) ); // http://example.com/wp-content/plugins/wp-performance-score-booster/
}
if ( ! defined( 'WPPSB_STORAGE_PATH' ) ) {
define( 'WPPSB_STORAGE_PATH', get_home_path() . 'wp-content/wp-performance-score-booster' ); // storage path to store .htaccess backups
}
require_once WPPSB_PATH . 'admin-page.php'; // admin options page.
require_once WPPSB_PATH . 'data-processing.php'; // process the data such as remove query strings, enable gzip and leverage browser caching.
require_once WPPSB_PATH . 'optimize-more.php'; // Optimize more content.
// Check if option values (from the database) exists in the database otherwise set on/active by default
global $wppsb_plugin_version, $wppsb_remove_query_strings, $wppsb_enable_gzip, $wppsb_expire_caching, $wppsb_instant_page_preload;
$wppsb_plugin_version = ( get_option( 'wppsb_plugin_version' ) ? get_option( 'wppsb_plugin_version' ) : WPPSB_PLUGIN_VERSION );
$wppsb_remove_query_strings = ( FALSE !== get_option( 'wppsb_remove_query_strings' ) ? get_option( 'wppsb_remove_query_strings' ) : 'on' );
$wppsb_enable_gzip = ( FALSE !== get_option( 'wppsb_enable_gzip' ) ? get_option( 'wppsb_enable_gzip' ) : 'on' );
$wppsb_expire_caching = ( FALSE !== get_option( 'wppsb_expire_caching' ) ? get_option( 'wppsb_expire_caching' ) : 'on' );
$wppsb_instant_page_preload = ( FALSE !== get_option( 'wppsb_instant_page_preload' ) ? get_option( 'wppsb_instant_page_preload' ) : 'on' );
function wppsb_load_plugin_textdomain() {
$domain = 'wp-performance-score-booster';
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
// wp-content/languages/plugin-name/plugin-name-de_DE.mo
load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . '/' . $domain . '-' . $locale . '.mo' );
// wp-content/plugins/plugin-name/languages/plugin-name-de_DE.mo
load_plugin_textdomain( $domain, FALSE, WPPSB_PATH . '/languages/' );
}
// Call all the functions together to hook with init()
function wppsb_master_init () {
wppsb_load_plugin_textdomain(); // load plugin textdomain for language trnaslation
wppsb_update_checker(); // Check if plugin updated
// Runs after WordPress load
do_action( 'wppsb_remove_query_string_action' );
}
add_action( 'init', 'wppsb_master_init' );
// Call all the functions together to hook with admin_init()
function wppsb_master_admin_init () {
wppsb_add_stylesheet(); // adds plugin stylesheet
wppsb_update_processor(); // Reload the config (rewrite rules) after applying plugin updates
do_action( 'wppsb_rating_system_action' );
}
add_action( 'admin_init', 'wppsb_master_admin_init' );
// Add settings link on plugin page
function dcg_settings_link( $links ) {
// $settings_link = '<a href="admin.php?page=wp-performance-score-booster">Settings</a>';
$wppsb_links = array( '<a href="options-general.php?page=wp-performance-score-booster">Settings</a>' );
// array_unshift($links, $settings_link);
$links = array_merge( $wppsb_links, $links );
return $links;
}
add_filter( 'plugin_action_links_' . WPPSB_BASE, 'dcg_settings_link' );
// Adding WordPress plugin meta links
function wppsb_plugin_meta_links( $links, $file ) {
// Create link
if ( $file === plugin_basename(__FILE__) ) {
return array_merge(
$links,
array( '<a href="https://dipakgajjar.com/product/wordpress-speed-optimization-service?utm_source=plugins%20page&utm_medium=text%20link&utm_campaign=wppsb%20plugin" style="color:#FF0000;" target="_blank">Improve your site\'s performance even more!</a>' )
);
}
return $links;
}
add_filter( 'plugin_row_meta', 'wppsb_plugin_meta_links', 10, 2 );
function wppsb_add_stylesheet() {
wp_enqueue_style( 'wppsb-stylesheet', WPPSB_URL . 'assets/css/style.min.css' );
}
// Add header
function wppsb_add_header() {
// Get the plugin version from options (in the database)
global $wppsb_plugin_version;
$head_comment = <<<EOD
<!-- Speed of this site is optimised by WP Performance Score Booster plugin v$wppsb_plugin_version - https://dipakgajjar.com/wp-performance-score-booster/ -->
EOD;
$head_comment = $head_comment . PHP_EOL;
print ( $head_comment );
}
add_action( 'wp_head', 'wppsb_add_header', 1 );
// Display admin notice to apply changes in the occurrence of update (when plugin updated)
function wppsb_apply_updates_notice() {
global $wppsb_plugin_version;
$notice_contents = '<p class="update_notice">WP Performance Score Booster has been updated to version: ' . WPPSB_PLUGIN_VERSION . '</p>';
$notice_contents .= '<p><a href="options-general.php?page=wp-performance-score-booster&apply-updates=true" class="button button-primary" id="wppsb_save_button">Apply Changes</a></p>';
?>
<div class="notice notice-success" id="wppsb_notice_div"><strong><?php _e( $notice_contents, 'wp-performance-score-booster' ); ?></strong></div>
<?php
}
// Check if plugin updated
function wppsb_update_checker() {
global $wppsb_plugin_version;
if ( $wppsb_plugin_version !== WPPSB_PLUGIN_VERSION ) {
if ( is_plugin_active( WPPSB_BASE ) ) {
add_action( 'admin_notices', 'wppsb_apply_updates_notice' );
}
}
}
// If 'Apply Updates' clicked under admin notice (in case of plugin updated)
function wppsb_update_processor() {
global $wppsb_plugin_version, $wppsb_remove_query_strings, $wppsb_enable_gzip, $wppsb_expire_caching, $wppsb_instant_page_preload;
if ( isset( $_GET['apply-updates'] ) && $_GET['apply-updates'] == 'true' ) {
if ( $wppsb_plugin_version != WPPSB_PLUGIN_VERSION ) {
update_option( 'wppsb_plugin_version', WPPSB_PLUGIN_VERSION );
if ( FALSE === get_option( 'wppsb_instant_page_preload' ) ) {
add_option( 'wppsb_instant_page_preload', $wppsb_instant_page_preload );
}
wppsb_htaccess_bakup(); // Backup .htacces before appending any rules
flush_rewrite_rules();
wppsb_save_mod_rewrite_rules( $wppsb_enable_gzip, $wppsb_expire_caching );
exit ( wp_redirect( admin_url( 'options-general.php?page=wp-performance-score-booster&update-applied=true' ) ) );
}
}
}
// Plugin rating on wordpress.org
function wppsb_rating_checker() {
// check for admin notice dismissal
if ( isset( $_POST['wppsb-already-reviewed'] ) ) {
update_option( 'wppsb_review_notice', '' );
if ( get_option( 'wppsb_activation_date' ) ) {
delete_option( 'wppsb_activation_date' );
}
}
// display admin notice after 30 days if clicked 'May be later'
if ( isset( $_POST['wppsb-review-later'] ) ) {
update_option( 'wppsb_review_notice', '' );
update_option( 'wppsb_activation_date', strtotime( 'now' ) );
}
$install_date = get_option( 'wppsb_activation_date' );
$past_date = strtotime( '-30 days' );
if ( FALSE !== get_option( 'wppsb_activation_date' ) && $past_date >= $install_date ) {
update_option( 'wppsb_review_notice', 'on' );
delete_option( 'wppsb_activation_date' );
}
}
add_action( 'wppsb_rating_system_action', 'wppsb_rating_checker' );
/* Add admin notice for requesting plugin review */
function wppsb_submit_review_notice() {
global $wppsb_plugin_version;
/* Display review plugin notice if plugin updated */
// only applies to older versions of the plugin (older than 1.9.1) where option isn't set
// As version 2.0 is a major release, let's force users to submit review on wordpress.org
if ( isset( $_GET['update-applied'] ) && $_GET['update-applied'] == 'true' ) {
if ( FALSE === get_option('wppsb_review_notice' || WPPSB_PLUGIN_VERSION == '2.0' ) ) {
update_option( 'wppsb_review_notice', "on" );
}
}
/* Check transient that's been set on plugin activation or check if user has already submitted review */
// if( get_transient( 'wppsb_submit_review_transient' ) || !get_user_meta( $user_id, 'wppsb_submit_review_dismissed' ) ) {
if( get_option( 'wppsb_review_notice') && get_option( 'wppsb_review_notice' ) == "on" ) {
$notice_contents = '<p> Thank you for using <strong>WP Performance Score Booster</strong>. </p>';
$notice_contents .= '<p> Could you please do me a BIG favour and give this plugin a 5-star rating on WordPress? It won\'t take more than a minute, and help me spread the word and boost my motivation. - Dipak C. Gajjar </p>';
$notice_contents .= '<p> <a href="#" id="letMeReview" class="button button-primary">Yes, you deserve it</a> <a href="#" id="willReviewLater" class="button button-primary">Maybe later</a> <a href="#" id="alredyReviewed" class="button button-primary">I already did it</a> <a href="#" id="noThanks" class="button button-primary">No, Thanks</a> </p>';
?>
<div class="notice notice-info is-dismissible" id="wppsb_notice_div"> <?php _e( $notice_contents, 'wp-performance-score-booster' ); ?> </div>
<script type="text/javascript">
// set jQuery in noConflict mode. This helps to mitigate conflicts between jQuery scripts. jQuery conflicts are all too common with themes and plugins.
var $j = jQuery.noConflict();
$j(document).ready( function() {
var loc = location.href;
// loc += loc.indexOf("?") === -1 ? "?" : "&";
// Yes, you deserve it
$j("#letMeReview").on('click', function() {
/*jQuery('.notice').slideUp("slow", function(){;
window.open("//wordpress.org/support/plugin/wp-performance-score-booster/reviews/?rate=5#new-post", "_blank");
});*/
$j('#wppsb_notice_div').slideUp();
$j.ajax({
url: loc,
type: 'POST',
data: {
"wppsb-review-later": ''
},
success: function(msg) {
window.open("//wordpress.org/support/plugin/wp-performance-score-booster/reviews/?rate=5#new-post", "_blank");
}
});
});
// Maybe later
$j("#willReviewLater").on('click', function() {
$j('#wppsb_notice_div').slideUp();
$j.ajax({
url: loc,
type: 'POST',
data: {
"wppsb-review-later": ''
}/* ,
success: function(msg) {
console.log("WPPSB DEBUG: Review the Plugin Later.");
} */
});
});
// I already did it
$j("#alredyReviewed").on('click', function() {
$j('#wppsb_notice_div').slideUp();
$j.ajax({
url: loc,
type: 'POST',
data: {
"wppsb-already-reviewed": ''
}
});
});
// No, thanks
$j("#noThanks").on('click', function() {
$j('#wppsb_notice_div').slideUp();
$j.ajax({
url: loc,
type: 'POST',
data: {
"wppsb-already-reviewed": ''
}
});
});
/* If top-right X button clicked */
$j(document).on('click', '#wppsb_notice_div .notice-dismiss', function() {
$j('#wppsb_notice_div').slideUp();
$j.ajax({
url: loc,
type: 'POST',
data: {
"wppsb-already-reviewed": ''
}
});
});
});
</script>
<?php
// delete_transient( 'wppsb_submit_review_transient' );
}
}
add_action( 'admin_notices', 'wppsb_submit_review_notice' );
// Calling this function will make flush_rules to be called at the end of the PHP execution
function wppsb_activate_plugin() {
global $wppsb_remove_query_strings, $wppsb_enable_gzip, $wppsb_expire_caching, $wppsb_instant_page_preload;
/* Yes, we're using add_option, not update_option intentionally just to force rewrite htaccess rules */
// Rate this plugin on wordpress.org - check for admin notice dismissal
if ( FALSE === get_option( 'wppsb_review_notice' ) ) {
add_option( 'wppsb_review_notice', "on" );
}
// Save default options value in the database
add_option( 'wppsb_plugin_version', WPPSB_PLUGIN_VERSION );
add_option( 'wppsb_remove_query_strings', $wppsb_remove_query_strings );
add_option( 'wppsb_instant_page_preload', $wppsb_instant_page_preload );
// Check if GZIP / mod_deflate is enabled in hosting server?
if ( function_exists( 'ob_gzhandler' ) || ini_get( 'zlib.output_compression' ) ) {
add_option( 'wppsb_enable_gzip', $wppsb_enable_gzip );
}
else {
add_option( 'wppsb_enable_gzip', '' );
}
add_option( 'wppsb_expire_caching', $wppsb_expire_caching );
wppsb_htaccess_bakup(); // Backup .htacces before appending any rules
flush_rewrite_rules();
wppsb_save_mod_rewrite_rules( $wppsb_enable_gzip, $wppsb_expire_caching );
}
register_activation_hook( __FILE__, 'wppsb_activate_plugin' );
// Remove filters/functions on plugin deactivation
function wppsb_deactivate_plugin() {
flush_rewrite_rules();
wppsb_save_mod_rewrite_rules( '', '' );
}
register_deactivation_hook( __FILE__, 'wppsb_deactivate_plugin' );
function wppsb_htaccess_bakup() {
if ( ! file_exists( WPPSB_STORAGE_PATH ) ) {
mkdir( WPPSB_STORAGE_PATH, 0777, true );
}
$htaccess_file = get_home_path() . '.htaccess'; // original .htaccess file
$htaccess_bak = WPPSB_STORAGE_PATH . '/.htaccess.wppsb';
if ( ! copy( $htaccess_file, $htaccess_bak ) ) {
echo 'Failed to backup .htaccess file!';
}
}
/**********************************
* DEBUG
**********************************/
/* function wppsb_save_error() {
update_option( 'wppsb_plugin_error', ob_get_contents() );
}
add_action( 'activated_plugin', 'wppsb_save_error' );
echo get_option( 'wppsb_plugin_error' ); */