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
|
<?php if ( ! defined( 'ABSPATH' ) ) { exit; } if( ! class_exists( 'Penci_Amp_Compatibility' ) ): class Penci_Amp_Compatibility{
public static $plugins = array();
public static function init(){
add_action( 'wp', array( __CLASS__, 'rocket_disable_options' ) );
add_action( 'init', array( __CLASS__, 'pre_wpml_hooks' ) );
$is_penci_amp = is_penci_amp(); if ( ! $is_penci_amp ) { return; }
self::$plugins = array_flip( wp_get_active_and_valid_plugins() );
// WordPress Fastest Cache if ( isset( self::$plugins[ WP_PLUGIN_DIR . '/wp-fastest-cache/wpFastestCache.php' ] ) && ! isset( $GLOBALS["wp_fastest_cache_options"] ) ) { self::fastest_cache(); }
// Convert Plug plugin if ( class_exists( 'Convert_Plug' ) ) { add_filter( 'after_setup_theme', array( __CLASS__, 'convert_plug' ) ); }
// Above The Fold Plugin if ( class_exists( 'Abovethefold' ) ) { if ( ! defined( 'DONOTABTF' ) ) { define( 'DONOTABTF', TRUE ); } $GLOBALS['Abovethefold']->disable = TRUE;
self::remove_class_action( 'init', 'Abovethefold_Optimization', 'html_output_hook', 99999 ); self::remove_class_action( 'wp_head', 'Abovethefold_Optimization', 'header', 1 ); self::remove_class_action( 'wp_print_footer_scripts', 'Abovethefold_Optimization', 'footer', 99999 ); }
if ( class_exists( 'WP_Optimize' ) ) { self::remove_class_action( 'plugins_loaded', 'WP_Optimize', 'plugins_loaded', 1 ); }
self::$plugins = NULL;
// WPML add_action( 'plugins_loaded', array( __CLASS__, 'plugins_loaded' ) );
add_action( 'template_redirect', array( __CLASS__, 'pre_wpml_hooks' ) );
// Polylang add_filter( 'pll_check_canonical_url', '__return_false' );
// wp-rocket add_filter( 'rocket_cache_search','__return_false' ); }
public static function pre_wpml_hooks(){ global $wpml_language_resolution;
$site_press = isset( $GLOBALS['sitepress'] ) ? $GLOBALS['sitepress'] : ''; if ( ! $site_press || ! $site_press instanceof SitePress ) { return; }
$language_negotiation_type = $site_press->get_setting( 'language_negotiation_type' );
if ( $language_negotiation_type == '1' ) { add_filter( 'penci_amp_pre_exclude_subdir', array( $wpml_language_resolution, 'get_active_language_codes' ) ); } }
public static function plugins_loaded(){ if ( function_exists( 'custom_permalinks_request' ) ) { add_filter( 'request', array( __CLASS__,'custom_permalinks' ), 15 ); }
add_filter( 'run_ngg_resource_manager', '__return_false', 999 );
if ( defined( 'WPML_PLUGIN_BASENAME' ) && WPML_PLUGIN_BASENAME ) {
add_action( 'wpml_is_redirected', '__return_false' ); } }
public static function custom_permalinks( $query_vars ){ $amp_query_vars = defined( 'AMP_QUERY_VAR' ) ? AMP_QUERY_VAR : 'amp'; $path = self::get_wp_installation_slug();
if ( ! ( preg_match( "#^$path/*$amp_query_vars/(.*?)/*$#", $_SERVER['REQUEST_URI'], $matched ) || preg_match( "#^$path/*(.*?)/$amp_query_vars/*$#", $_SERVER['REQUEST_URI'], $matched ) ) ) { return $query_vars; }
if ( empty( $matched[1] ) ) { return $query_vars; }
remove_filter( 'request', array( __CLASS__,'custom_permalinks' ), 15 );
$_SERVER['REQUEST_URI'] = '/' . $matched[1] . '/'; $query_vars ['amp'] = '1'; $_REQUEST['amp'] = '1';
if ( $new_qv = custom_permalinks_request( $query_vars ) ) {
$new_qv['amp'] = '1'; remove_filter( 'template_redirect', 'custom_permalinks_redirect', 5 );
return $new_qv; }
return $query_vars; }
public static function get_wp_installation_slug() {
static $path;
if ( $path ) { return $path; }
$abspath_pre = str_replace( '\\', '/', ABSPATH ); $abspath_pre = str_replace( '/usr', '', $abspath_pre );
$fname_dir = dirname( $_SERVER['SCRIPT_FILENAME'] );
if ( $fname_dir . '/' == $abspath_pre ) { $path = preg_replace( '#/[^/]*$#i', '', $_SERVER['PHP_SELF'] );
} elseif ( false !== strpos( $_SERVER['SCRIPT_FILENAME'], $abspath_pre ) ) { $directory = str_replace( ABSPATH, '', $fname_dir ); $path = preg_replace( '#/' . preg_quote( $directory, '#' ) . '/[^/]*$#i', '', $_SERVER['REQUEST_URI'] ); } elseif ( '' !== $fname_dir && false !== strpos( $abspath_pre, $fname_dir ) ) { $subdirectory = substr( $abspath_pre, strpos( $abspath_pre, $fname_dir ) + strlen( $fname_dir ) ); $path = preg_replace( '#/[^/]*$#i', '', $_SERVER['REQUEST_URI'] ) . $subdirectory; } else { $path = $_SERVER['REQUEST_URI']; }
if ( is_multisite() && ! is_main_site() ) { $current_site_url = get_site_url(); $append_path = str_replace( get_site_url( get_current_site()->blog_id ), '', $current_site_url );
if ( $append_path !== $current_site_url ) { $path .= $append_path; } }
return $path; }
public static function fastest_cache(){ $options = get_option( "WpFastestCache" );
if ( $options ) {
$GLOBALS["wp_fastest_cache_options"] = json_decode( $options );
unset( $GLOBALS["wp_fastest_cache_options"]->wpFastestCacheRenderBlocking ); unset( $GLOBALS["wp_fastest_cache_options"]->wpFastestCacheCombineJsPowerFul ); unset( $GLOBALS["wp_fastest_cache_options"]->wpFastestCacheMinifyJs ); unset( $GLOBALS["wp_fastest_cache_options"]->wpFastestCacheCombineJs ); unset( $GLOBALS["wp_fastest_cache_options"]->wpFastestCacheCombineCss ); unset( $GLOBALS["wp_fastest_cache_options"]->wpFastestCacheLazyLoad ); unset( $GLOBALS["wp_fastest_cache_options"]->wpFastestCacheGoogleFonts );
} else { $GLOBALS["wp_fastest_cache_options"] = array(); } }
public static function convert_plug(){ self::remove_class_filter( 'the_content', 'Convert_Plug', 'cp_add_content', 10 ); }
public static function rocket_disable_options(){ if ( is_penci_amp() ) { if ( function_exists( 'wp_resource_hints' ) ) { remove_filter( 'wp_resource_hints', 'rocket_dns_prefetch', 10, 2 ); } else { remove_filter( 'rocket_buffer', 'rocket_dns_prefetch_buffer', 12 ); }
remove_filter( 'rocket_buffer', 'rocket_exclude_deferred_js', 11 ); remove_filter( 'rocket_buffer', 'rocket_minify_process', 13 ); remove_filter( 'rocket_buffer', 'rocket_defer_js', 14 );
if( class_exists( 'Rocket_Critical_CSS' ) ){ self::remove_class_filter( 'rocket_buffer', 'Rocket_Critical_CSS', 'async_css', 10 ); self::remove_class_filter( 'rocket_buffer', 'Rocket_Critical_CSS', 'insert_critical_css_buffer', 10 ); }
remove_filter( 'rocket_buffer', 'rocket_minify_html', 20 );
add_filter( 'do_rocket_lazyload', '__return_false' );
// this filter is documented in inc/front/protocol.php. $do_rocket_protocol_rewrite = apply_filters( 'do_rocket_protocol_rewrite', false );
if( function_exists( 'get_rocket_option' ) ){ if ( ( get_rocket_option( 'do_cloudflare', 0 ) && get_rocket_option( 'cloudflare_protocol_rewrite', 0 ) || $do_rocket_protocol_rewrite ) ) { remove_filter( 'rocket_buffer', 'rocket_protocol_rewrite', PHP_INT_MAX ); remove_filter( 'wp_calculate_image_srcset', 'rocket_protocol_rewrite_srcset', PHP_INT_MAX ); } }
remove_action( 'wp_footer', '__rocket_insert_minify_js_in_footer', PHP_INT_MAX );
add_filter( 'pre_get_rocket_option_purge_cron_interval', function(){ return 0; } ); add_filter( 'pre_get_rocket_option_purge_cron_unit', function(){ return 'SECOND_IN_SECONDS'; } ); } }
/** * Remove Class Filter Without Access to Class Object * Copyright: https://gist.github.com/tripflex/c6518efc1753cf2392559866b4bd1a53 * * @return bool Whether the function is removed. */ public static function remove_class_filter( $tag, $class_name = '', $method_name = '', $priority = 10 ) { global $wp_filter; // Check that filter actually exists first if ( ! isset( $wp_filter[ $tag ] ) ) { return FALSE; } /** * If filter config is an object, means we're using WordPress 4.7+ and the config is no longer * a simple array, rather it is an object that implements the ArrayAccess interface. * * To be backwards compatible, we set $callbacks equal to the correct array as a reference (so $wp_filter is updated) * * @see https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/ */ if ( is_object( $wp_filter[ $tag ] ) && isset( $wp_filter[ $tag ]->callbacks ) ) { // Create $fob object from filter tag, to use below $fob = $wp_filter[ $tag ]; $callbacks = &$wp_filter[ $tag ]->callbacks; } else { $callbacks = &$wp_filter[ $tag ]; } // Exit if there aren't any callbacks for specified priority if ( ! isset( $callbacks[ $priority ] ) || empty( $callbacks[ $priority ] ) ) { return FALSE; } // Loop through each filter for the specified priority, looking for our class & method foreach ( (array) $callbacks[ $priority ] as $filter_id => $filter ) { // Filter should always be an array - array( $this, 'method' ), if not goto next if ( ! isset( $filter['function'] ) || ! is_array( $filter['function'] ) ) { continue; } // If first value in array is not an object, it can't be a class if ( ! is_object( $filter['function'][0] ) ) { continue; } // Method doesn't match the one we're looking for, goto next if ( $filter['function'][1] !== $method_name ) { continue; } // Method matched, now let's check the Class if ( get_class( $filter['function'][0] ) === $class_name ) { // WordPress 4.7+ use core remove_filter() since we found the class object if ( isset( $fob ) ) { // Handles removing filter, reseting callback priority keys mid-iteration, etc. $fob->remove_filter( $tag, $filter['function'], $priority ); } else { // Use legacy removal process (pre 4.7) unset( $callbacks[ $priority ][ $filter_id ] ); // and if it was the only filter in that priority, unset that priority if ( empty( $callbacks[ $priority ] ) ) { unset( $callbacks[ $priority ] ); } // and if the only filter for that tag, set the tag to an empty array if ( empty( $callbacks ) ) { $callbacks = array(); } // Remove this filter from merged_filters, which specifies if filters have been sorted unset( $GLOBALS['merged_filters'][ $tag ] ); } return TRUE; } } return FALSE; }
public static function remove_class_action( $tag, $class_name = '', $method_name = '', $priority = 10 ) { self::remove_class_filter( $tag, $class_name, $method_name, $priority ); } }
Penci_Amp_Compatibility::init(); endif;
|