-
Notifications
You must be signed in to change notification settings - Fork 3
/
jetpack-boost.php
265 lines (225 loc) · 8.25 KB
/
jetpack-boost.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
<?php
/**
* Jetpack Boost Plugin
*
* @link https://automattic.com
* @since 0.1.0
*
* @wordpress-plugin
* Plugin Name: Jetpack Boost
* Plugin URI: https://jetpack.com/boost
* Description: Boost your WordPress site's performance, from the creators of Jetpack
* Version: 3.6.2-alpha
* Author: Automattic - Jetpack Site Speed team
* Author URI: https://jetpack.com/boost/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: jetpack-boost
* Domain Path: /languages
* Requires at least: 6.6
* Requires PHP: 7.2
*
* @package automattic/jetpack-boost
*/
namespace Automattic\Jetpack_Boost;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
define( 'JETPACK_BOOST_VERSION', '3.6.2-alpha' );
define( 'JETPACK_BOOST_SLUG', 'jetpack-boost' );
if ( ! defined( 'JETPACK_BOOST_CLIENT_NAME' ) ) {
define( 'JETPACK_BOOST_CLIENT_NAME', 'jetpack-boost-wp-plugin' );
}
define( 'JETPACK_BOOST_DIR_PATH', __DIR__ );
define( 'JETPACK_BOOST_PATH', __FILE__ );
if ( ! defined( 'JETPACK_BOOST_PLUGIN_BASE' ) ) {
define( 'JETPACK_BOOST_PLUGIN_BASE', plugin_basename( __FILE__ ) );
}
if ( ! defined( 'JETPACK_BOOST_PLUGIN_FILENAME' ) ) {
define( 'JETPACK_BOOST_PLUGIN_FILENAME', basename( __FILE__ ) );
}
if ( ! defined( 'JETPACK_BOOST_REST_NAMESPACE' ) ) {
define( 'JETPACK_BOOST_REST_NAMESPACE', 'jetpack-boost/v1' );
}
// For use in situations where you want additional namespacing.
if ( ! defined( 'JETPACK_BOOST_REST_PREFIX' ) ) {
define( 'JETPACK_BOOST_REST_PREFIX', '' );
}
if ( ! defined( 'JETPACK__WPCOM_JSON_API_BASE' ) ) {
define( 'JETPACK__WPCOM_JSON_API_BASE', 'https://public-api.wordpress.com' );
}
if ( ! defined( 'JETPACK_BOOST_PLUGINS_DIR_URL' ) ) {
define( 'JETPACK_BOOST_PLUGINS_DIR_URL', plugin_dir_url( __FILE__ ) );
}
/**
* Setup autoloading
*/
$boost_packages_path = JETPACK_BOOST_DIR_PATH . '/vendor/autoload_packages.php';
if ( is_readable( $boost_packages_path ) ) {
require_once $boost_packages_path;
if ( method_exists( \Automattic\Jetpack\Assets::class, 'alias_textdomains_from_file' ) ) {
\Automattic\Jetpack\Assets::alias_textdomains_from_file( JETPACK_BOOST_DIR_PATH . '/jetpack_vendor/i18n-map.php' );
}
} else {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
/** @noinspection ForgottenDebugOutputInspection */
error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
sprintf(
/* translators: Placeholder is a link to a support document. */
__( 'Your installation of Jetpack Boost is incomplete. If you installed Jetpack Boost from GitHub, please refer to this document to set up your development environment: %1$s', 'jetpack-boost' ),
'https://github.com/Automattic/jetpack/blob/trunk/docs/development-environment.md'
)
);
}
// Add a red bubble notification to My Jetpack if the installation is bad.
add_filter(
'my_jetpack_red_bubble_notification_slugs',
function ( $slugs ) {
$slugs['jetpack-boost-plugin-bad-installation'] = array(
'data' => array(
'plugin' => 'Jetpack Boost',
),
);
return $slugs;
}
);
/**
* Outputs an admin notice for folks running Jetpack Boost without having run composer install.
*
* @since 1.2.0
*/
function jetpack_boost_admin_missing_files() {
if ( get_current_screen()->id !== 'plugins' ) {
return;
}
$message = sprintf(
wp_kses(
/* translators: Placeholder is a link to a support document. */
__( 'Your installation of Jetpack Boost is incomplete. If you installed Jetpack Boost from GitHub, please refer to <a href="%1$s" target="_blank" rel="noopener noreferrer">this document</a> to set up your development environment. Jetpack Boost must have Composer dependencies installed and built via the build command.', 'jetpack-boost' ),
array(
'a' => array(
'href' => array(),
'target' => array(),
'rel' => array(),
),
)
),
'https://github.com/Automattic/jetpack/blob/trunk/docs/development-environment.md#building-your-project'
);
wp_admin_notice(
$message,
array(
'type' => 'error',
'dismissible' => true,
)
);
}
add_action( 'admin_notices', __NAMESPACE__ . '\\jetpack_boost_admin_missing_files' );
return;
}
/**
* Setup Minify service.
*/
// Potential improvement: Make concat URL dir configurable
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$request_path = explode( '?', wp_unslash( $_SERVER['REQUEST_URI'] ) )[0];
// Handling JETPACK_BOOST_STATIC_PREFIX constant inline to avoid loading the minify module until we know we want it.
$static_prefix = defined( 'JETPACK_BOOST_STATIC_PREFIX' ) ? JETPACK_BOOST_STATIC_PREFIX : '/_jb_static/';
if ( $static_prefix === substr( $request_path, -strlen( $static_prefix ) ) ) {
define( 'JETPACK_BOOST_CONCAT_USE_WP', true );
require_once JETPACK_BOOST_DIR_PATH . '/serve-minified-content.php';
exit;
}
}
require plugin_dir_path( __FILE__ ) . 'app/class-jetpack-boost.php';
/**
* Begins execution of the plugin.
*
* @since 0.1.0
*/
function run_jetpack_boost() {
new Jetpack_Boost();
}
add_action( 'plugins_loaded', '\Automattic\Jetpack_Boost\run_jetpack_boost', 1 );
register_activation_hook( __FILE__, array( 'Automattic\Jetpack_Boost\Jetpack_Boost', 'activate' ) );
// Redirect to plugin page when the plugin is activated.
add_action( 'activated_plugin', __NAMESPACE__ . '\jetpack_boost_plugin_activation' );
/**
* Redirects to plugin page when the plugin is activated
*
* @access public
* @static
*
* @param string $plugin Path to the plugin file relative to the plugins directory.
*/
function jetpack_boost_plugin_activation( $plugin ) {
if (
JETPACK_BOOST_PLUGIN_BASE === $plugin &&
( new \Automattic\Jetpack\Paths() )->is_current_request_activating_plugin_from_plugins_screen( JETPACK_BOOST_PLUGIN_BASE )
) {
wp_safe_redirect( esc_url( admin_url( 'admin.php?page=jetpack-boost' ) ) );
exit;
}
}
/**
* Extra tweaks to make Jetpack Boost work better with others, that need to be loaded early.
*/
function include_compatibility_files_early() {
// Since Page Optimize allows its functionality to be disabled on plugins_loaded (10)
// we need to do this earlier.
if ( function_exists( 'page_optimize_init' ) ) {
require_once __DIR__ . '/compatibility/page-optimize.php';
}
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\include_compatibility_files_early', 1 );
/**
* Extra tweaks to make Jetpack Boost work better with others.
*/
function include_compatibility_files() {
if ( class_exists( 'Jetpack' ) ) {
require_once __DIR__ . '/compatibility/jetpack.php';
}
if ( class_exists( 'WooCommerce' ) ) {
require_once __DIR__ . '/compatibility/woocommerce.php';
}
if ( class_exists( '\Google\Web_Stories\Plugin' ) ) {
require_once __DIR__ . '/compatibility/web-stories.php';
}
if ( defined( '\Elementor\TemplateLibrary\Source_Local::CPT' ) || defined( '\Elementor\Modules\LandingPages\Module::CPT' ) ) {
require_once __DIR__ . '/compatibility/elementor.php';
}
if ( function_exists( 'amp_is_request' ) ) {
require_once __DIR__ . '/compatibility/amp.php';
}
if ( function_exists( 'wp_cache_is_enabled' ) ) {
require_once __DIR__ . '/compatibility/wp-super-cache.php';
}
if ( class_exists( '\Yoast\WP\SEO\Main' ) ) {
require_once __DIR__ . '/compatibility/yoast.php';
}
if ( function_exists( 'aioseo' ) ) {
require_once __DIR__ . '/compatibility/aioseo.php';
}
// Exclude known scripts that causes problem when concatenated.
require_once __DIR__ . '/compatibility/js-concatenate.php';
// Migrate from WP Super Cache
require_once __DIR__ . '/compatibility/wp-super-cache-migration.php';
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\include_compatibility_files' );
register_uninstall_hook( __FILE__, 'Automattic\Jetpack_Boost\jetpack_boost_uninstall' );
/**
* Clean up when uninstalling Jetpack Boost
*/
function jetpack_boost_uninstall() {
$boost = new Jetpack_Boost();
$boost->uninstall();
}
/**
* Previous version compatibility files
*/
require_once __DIR__ . '/compatibility/boost-1.3.1.php';
require_once __DIR__ . '/compatibility/score-prompt.php';
require_once __DIR__ . '/wp-js-data-sync.php';