PluginProbe
Plugin Load Filter / 2.4.1
Plugin Load Filter v2.4.1
trunk 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.3.1 2.4.0 2.4.1 2.5.1 3.3.0 4.0.6 4.1.1 4.2.0 4.3.0 4.3.1 4.4.0
plugin-load-filter / mu-plugins / plf-filter.php

plf-filter.php in Plugin Load Filter 2.4.1, at mu-plugins/plf-filter.php

524 lines 25.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: plugin load filter [plf-filter]
4 Description: Dynamically activated only plugins that you have selected in each page. [Note] plf-filter has been automatically installed / deleted by Activate / Deactivate of "load filter plugin".
5 Version: 2.4.1
6 Plugin URI: http://celtislab.net/wp_plugin_load_filter
7 Author: enomoto@celtislab
8 Author URI: http://celtislab.net/
9 License: GPLv2
10 */
11 defined( 'ABSPATH' ) || exit;
12
13 /***************************************************************************
14 * pluggable.php defined function overwrite
15 * pluggable.php read before the query_posts () is processed by the current user undetermined
16 **************************************************************************/
17 if ( !function_exists('wp_get_current_user') ) :
18 /**
19 * Retrieve the current user object.
20 * @return WP_User Current user WP_User object
21 */
22 function wp_get_current_user() {
23 if ( ! function_exists( 'wp_set_current_user' ) )
24 return 0;
25 //get_currentuserinfo is deprecated since version 4.5
26 if ( version_compare( $GLOBALS['wp_version'], '4.5', '<' ) ){
27 global $current_user;
28 get_currentuserinfo();
29 return $current_user;
30 }
31 else {
32 return _wp_get_current_user();
33 }
34 }
35 endif;
36
37 if ( !function_exists('get_userdata') ) :
38 /**
39 * Retrieve user info by user ID.
40 * @param int $user_id User ID
41 * @return WP_User|bool WP_User object on success, false on failure.
42 */
43 function get_userdata( $user_id ) {
44 return get_user_by( 'id', $user_id );
45 }
46 endif;
47
48 if ( !function_exists('get_user_by') ) :
49 /**
50 * Retrieve user info by a given field
51 * @param string $field The field to retrieve the user with. id | slug | email | login
52 * @param int|string $value A value for $field. A user ID, slug, email address, or login name.
53 * @return WP_User|bool WP_User object on success, false on failure.
54 */
55 function get_user_by( $field, $value ) {
56 $userdata = WP_User::get_data_by( $field, $value );
57
58 if ( !$userdata )
59 return false;
60
61 $user = new WP_User;
62 $user->init( $userdata );
63
64 return $user;
65 }
66 endif;
67
68 if ( !function_exists('is_user_logged_in') ) :
69 /**
70 * Checks if the current visitor is a logged in user.
71 * @return bool True if user is logged in, false if not logged in.
72 */
73 function is_user_logged_in() {
74 if ( ! function_exists( 'wp_set_current_user' ) )
75 return false;
76
77 $user = wp_get_current_user();
78
79 if ( ! $user->exists() )
80 return false;
81
82 return true;
83 }
84 endif;
85
86 /***************************************************************************
87 * Plugin Load Filter( Admin, Desktop, Mobile, Page 4types filter)
88 **************************************************************************/
89
90 if(in_array( 'plugin-load-filter/plugin-load-filter.php', (array) get_option( 'active_plugins', array() ) )){
91 $plugin_load_filter = new Plf_filter();
92 }
93 elseif ( is_multisite() ) {
94 $plugins = get_site_option( 'active_sitewide_plugins');
95 if ( isset($plugins['plugin-load-filter/plugin-load-filter.php']) ){
96 $plugin_load_filter = new Plf_filter();
97 }
98 }
99
100 class Plf_filter {
101
102 private $filter = array(); //Plugin Load Filter Setting option data
103 private $cache;
104
105 function __construct() {
106 $this->filter = get_option('plf_option');
107 //v2.3.0 option change
108 if(empty($this->filter['optver']) || $this->filter['optver'] < '2'){
109 //For ver2.2.0 compatibility, set '_pagefilter' data to 'desktop' and 'mobile'
110 if(empty($this->filter['group']['desktop']) && empty($this->filter['group']['mobile'])){
111 if(!empty($this->filter['_pagefilter'])){
112 $this->filter['group']['desktop'] = $this->filter['_pagefilter'];
113 $this->filter['group']['mobile'] = $this->filter['_pagefilter'];
114 }
115 }
116 }
117
118 $this->cache = null;
119 if(!empty($this->filter)){
120 if ( is_multisite() ) {
121 add_filter('pre_site_option_active_sitewide_plugins', array(&$this, 'active_sitewide_plugins'));
122 }
123 add_filter('pre_option_active_plugins', array(&$this, 'active_plugins'));
124 add_filter('pre_option_jetpack_active_modules', array(&$this, 'active_jetmodules'));
125 add_filter('pre_option_celtispack_active_modules', array(&$this, 'active_celtismodules'));
126 add_action('wp_loaded', array(&$this, 'cache_post_type'), 1);
127 }
128 }
129
130 //active_sitewide plugins Filter add ver2.4.0
131 function active_sitewide_plugins( $default = false) {
132 return $this->plf_filter( 'active_sitewide_plugins', $default);
133 }
134
135 //active plugins Filter
136 function active_plugins( $default = false) {
137 return $this->plf_filter( 'active_plugins', $default);
138 }
139
140 //Jetpack module Filter
141 function active_jetmodules( $default = false) {
142 return $this->plf_filter( 'jetpack_active_modules', $default);
143 }
144
145 //Celtispack module Filter
146 function active_celtismodules( $default = false) {
147 return $this->plf_filter( 'celtispack_active_modules', $default);
148 }
149
150 //Make taxonomies and posts available to 'plugin load filter'.
151 //force register_taxonomy (category, post_tag, post_format)
152 function force_initial_taxonomies(){
153 global $wp_actions;
154 $wp_actions[ 'init' ] = 1;
155 create_initial_taxonomies();
156 create_initial_post_types();
157 unset($wp_actions[ 'init' ]);
158 }
159
160 //Post Format Type, Custom Post Type Data Cache for parse request
161 function cache_post_type() {
162 if (!is_admin() || ( defined('DOING_AJAX') && DOING_AJAX ) || (defined('DOING_CRON') && DOING_CRON))
163 return;
164
165 global $wp;
166 global $wp_post_statuses;
167 $public_query_vars = (!empty($wp->public_query_vars))? $wp->public_query_vars : array();;
168 $post_type_query_vars = array();
169 foreach ( get_post_types( array(), 'objects' ) as $post_type => $t ){
170 if ( $t->query_var )
171 $post_type_query_vars[$t->query_var] = $post_type;
172 }
173 $queryable_post_types = get_post_types( array('publicly_queryable' => true) );
174
175 $data = get_option('plf_queryvars');
176 if(!empty($post_type_query_vars) && !empty($queryable_post_types)){
177 $data['public_query_vars'] = $public_query_vars;
178 $data['post_type_query_vars'] = $post_type_query_vars;
179 $data['queryable_post_types'] = $queryable_post_types;
180 $data['wp_post_statuses'] = $wp_post_statuses;
181 update_option('plf_queryvars', $data);
182 }
183 else if(!empty($data['post_type_query_vars']) || !empty($data['queryable_post_types'])){
184 delete_option('plf_queryvars');
185 }
186
187 }
188
189 //parse_request Action Hook for Custom Post Type query add
190 function parse_request( &$args ) {
191 if (did_action( 'plugins_loaded' ) === 0 ) {
192 $data = get_option('plf_queryvars');
193 if(!empty($data['post_type_query_vars']) && !empty($data['queryable_post_types'])){
194 global $wp_post_statuses;
195 $post_type_query_vars = $data['post_type_query_vars'];
196 $queryable_post_types = $data['queryable_post_types'];
197 $wp_post_statuses = $data['wp_post_statuses'];
198
199 $args->public_query_vars = $data['public_query_vars'];
200 if ( isset( $args->matched_query ) ) {
201 parse_str($args->matched_query, $perma_query_vars);
202 }
203 foreach ( $args->public_query_vars as $wpvar ) {
204 if ( isset( $args->extra_query_vars[$wpvar] ) )
205 $args->query_vars[$wpvar] = $args->extra_query_vars[$wpvar];
206 elseif ( isset( $_POST[$wpvar] ) )
207 $args->query_vars[$wpvar] = $_POST[$wpvar];
208 elseif ( isset( $_GET[$wpvar] ) )
209 $args->query_vars[$wpvar] = $_GET[$wpvar];
210 elseif ( isset( $perma_query_vars[$wpvar] ) )
211 $args->query_vars[$wpvar] = $perma_query_vars[$wpvar];
212
213 if ( !empty( $args->query_vars[$wpvar] ) ) {
214 if ( ! is_array( $args->query_vars[$wpvar] ) ) {
215 $args->query_vars[$wpvar] = (string) $args->query_vars[$wpvar];
216 } else {
217 foreach ( $args->query_vars[$wpvar] as $vkey => $v ) {
218 if ( !is_object( $v ) ) {
219 $args->query_vars[$wpvar][$vkey] = (string) $v;
220 }
221 }
222 }
223 if ( isset($post_type_query_vars[$wpvar] ) ) {
224 $args->query_vars['post_type'] = $post_type_query_vars[$wpvar];
225 $args->query_vars['name'] = $args->query_vars[$wpvar];
226 }
227 }
228 }
229
230 // Limit publicly queried post_types to those that are publicly_queryable
231 if ( isset( $args->query_vars['post_type']) ) {
232 if ( ! is_array( $args->query_vars['post_type'] ) ) {
233 if ( ! in_array( $args->query_vars['post_type'], $queryable_post_types ) )
234 unset( $args->query_vars['post_type'] );
235 } else {
236 $args->query_vars['post_type'] = array_intersect( $args->query_vars['post_type'], $queryable_post_types );
237 }
238 }
239 }
240 }
241 }
242
243
244 //Plugin Load Filter Main (active plugins/modules filtering)
245 function plf_filter( $option, $default = false) {
246 if ( defined( 'WP_SETUP_CONFIG' ) )
247 return false;
248
249 //Admin mode exclude
250 if(is_admin())
251 return false;
252
253 if ( ! defined( 'WP_INSTALLING' ) ) {
254 global $wpdb, $current_site;
255 // if ( ! wp_installing() ) { // WP4.4
256 if ( is_multisite() && $option === 'active_sitewide_plugins' ) {
257 //get_network_option() current site ID
258 $network_id = $current_site->id;
259
260 // prevent non-existent options from triggering multiple queries
261 $notoptions_key = "$network_id:notoptions";
262 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
263 if ( isset( $notoptions[ $option ] ) ) {
264 return apply_filters( 'default_site_option_' . $option, $default, $option );
265 }
266
267 $cache_key = "$network_id:$option";
268 $value = wp_cache_get( $cache_key, 'site-options' );
269
270 if ( ! isset( $value ) || false === $value ) {
271 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
272
273 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
274 if ( is_object( $row ) ) {
275 $value = $row->meta_value;
276 $value = maybe_unserialize( $value );
277 wp_cache_set( $cache_key, $value, 'site-options' );
278 } else {
279 if ( ! is_array( $notoptions ) ) {
280 $notoptions = array();
281 }
282 $notoptions[ $option ] = true;
283 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
284
285 /** This filter is documented in wp-includes/option.php */
286 $value = apply_filters( 'default_site_option_' . $option, $default, $option );
287 }
288 }
289 }
290 else {
291 // prevent non-existent options from triggering multiple queries
292 $notoptions = wp_cache_get( 'notoptions', 'options' );
293 if ( isset( $notoptions[$option] ) )
294 return apply_filters( 'default_option_' . $option, $default );
295
296 $alloptions = wp_load_alloptions();
297 if ( isset( $alloptions[$option] ) ) {
298 $value = $alloptions[$option];
299 } else {
300 $value = wp_cache_get( $option, 'options' );
301
302 if ( false === $value ) {
303 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
304
305 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
306 if ( is_object( $row ) ) {
307 $value = $row->option_value;
308 wp_cache_add( $option, $value, 'options' );
309 } else { // option does not exist, so we must cache its non-existence
310 if ( ! is_array( $notoptions ) ) {
311 $notoptions = array();
312 }
313 $notoptions[$option] = true;
314 wp_cache_set( 'notoptions', $notoptions, 'options' );
315
316 /** This filter is documented in wp-includes/option.php */
317 return apply_filters( 'default_option_' . $option, $default, $option );
318 }
319 }
320 }
321 }
322 } else {
323 return false;
324 }
325
326 //Equal treatment for when the wp_is_mobile is not yet available(wp-include/vars.php wp_is_mobile)
327 if ( empty($_SERVER['HTTP_USER_AGENT']) ) {
328 $is_mobile = false;
329 } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.)
330 || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
331 || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
332 || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
333 || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
334 || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
335 || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) {
336 $is_mobile = true;
337 } else {
338 $is_mobile = false;
339 }
340 $is_mobile = apply_filters( 'custom_is_mobile' , $is_mobile );
341
342 $filter = $this->filter;
343 //get_option is called many times, intermediate processing data to cache
344 $keyid = md5('plf_'. (string)$is_mobile . $_SERVER['REQUEST_URI']);
345 if(!empty($this->cache[$keyid][$option])){
346 return $this->cache[$keyid][$option];
347 }
348
349 //Before plugins loaded, it does not use conditional branch such as is_home, to set wp_query, wp in temporary query
350 if(empty($GLOBALS['wp_the_query'])){
351 $rewrite_rules = get_option('rewrite_rules'); //save rewrite_rules
352
353 $GLOBALS['wp_the_query'] = new WP_Query();
354 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
355 $GLOBALS['wp_rewrite'] = new WP_Rewrite();
356 $GLOBALS['wp'] = new WP();
357 //register_taxonomy(category, post_tag, post_format) support for is_archive
358 $this->force_initial_taxonomies();
359 //Post Format, Custom Post Type support
360 add_action('parse_request', array(&$this, 'parse_request'));
361 $GLOBALS['wp']->parse_request('');
362 $GLOBALS['wp']->query_posts();
363
364 //Discard the temporarily generated 'rewrite_rules'
365 update_option('rewrite_rules', $rewrite_rules); //return rewrite_rules
366 }
367 //Only available display pages (login, cron, ajax request ... excluded)
368 //downloadmanager plugin downloadlink request [home]/?wpdmact=XXXXXX exclude home GET query
369 //bbPress users page requests are treated the same as is_home
370 global $wp_query;
371 if(!(function_exists('is_embed') && is_embed())){
372
373
374 //ToDo Next Version AMP page judgment (is_amp similer is_embed) by enomoto
375 //ToDo is_sinbular() and URL "/amp(/(.*))?/?$" check ?
376
377
378
379 if((is_home() || is_front_page() || is_archive() || is_search() || is_singular()) == false
380 || (is_home() && !empty($_GET))
381 || (is_singular() && empty($wp_query->post))){
382 return false;
383 }
384 }
385
386 $us_value = ($option === 'active_sitewide_plugins')? array_keys( (array)$value ) : maybe_unserialize( $value );
387 $new_value = array();
388 foreach ( $us_value as $item ) {
389 $unload = false;
390 //Jetpack module slug / Celtiapack module slug / Plugin php file name
391 if($option === 'jetpack_active_modules'){
392 $p_key = 'jetpack_module/' . $item;
393 }
394 elseif($option === 'celtispack_active_modules'){
395 $p_key = 'celtispack_module/' . str_replace( '.php', '', basename( $item) );
396 }
397 else {
398 $p_key = $item;
399 }
400 //admin mode filter
401 if(!empty($filter['_admin']['plugins'])){
402 if(in_array($p_key, array_map("trim", explode(',', $filter['_admin']['plugins']))))
403 $unload = true;
404 }
405 //page filter
406 if(!$unload){
407 if(!empty($filter['_pagefilter']['plugins'])){
408 if(in_array($p_key, array_map("trim", explode(',', $filter['_pagefilter']['plugins'])))){
409 $unload = true;
410
411 //desktop/mobile device disable filter
412 $single_opt = false;
413 $dis_dev = true;
414 $devtype = ($is_mobile)? 'mobile' : 'desktop';
415 if(is_singular() && is_object($wp_query->post)){
416 $myfilter = get_post_meta( $wp_query->post->ID, '_plugin_load_filter', true );
417 //For ver2.2.0 compatibility, set 'plugins' data to 'desktop' and 'mobile'
418 if(!empty($myfilter['plugins'])){
419 $myfilter['desktop'] = $myfilter['plugins'];
420 $myfilter['mobile'] = $myfilter['plugins'];
421 unset($myfilter['plugins']);
422 }
423 $default = array( 'filter' => 'default', 'desktop' => '', 'mobile' => '');
424 $single_opt = (!empty($myfilter))? $myfilter : $default;
425 $single_opt = wp_parse_args( $single_opt, $default);
426 if($single_opt['filter'] === 'include'){
427 if(false !== strpos($single_opt[$devtype], $p_key))
428 $dis_dev = false;
429 else if(strpos($p_key, 'jetpack/') !== false && strpos($single_opt[$devtype], 'jetpack_module/') !== false)
430 $dis_dev = false;
431 else if(strpos($p_key, 'celtispack/') !== false && strpos($single_opt[$devtype], 'celtispack_module/') !== false)
432 $dis_dev = false;
433 }
434 }
435 if(empty($single_opt) || $single_opt['filter'] === 'default'){
436 if(!empty($filter['group'][$devtype]['plugins'])){
437 if(false !== strpos($filter['group'][$devtype]['plugins'], $p_key))
438 $dis_dev = false;
439 else if(strpos($p_key, 'jetpack/') !== false && strpos($filter['group'][$type]['plugins'], 'jetpack_module/') !== false)
440 $dis_dev = false;
441 else if(strpos($p_key, 'celtispack/') !== false && strpos($filter['group'][$type]['plugins'], 'celtispack_module/') !== false)
442 $dis_dev = false;
443 }
444 }
445 if(!$dis_dev){
446 //oEmbed Content API
447 if(function_exists('is_embed') && is_embed()){
448 $type = 'content-card';
449 if(!empty($filter['group'][$type]['plugins'])){
450 if(false !== strpos($filter['group'][$type]['plugins'], $p_key))
451 $unload = false;
452 else if(strpos($p_key, 'jetpack/') !== false && strpos($filter['group'][$type]['plugins'], 'jetpack_module/') !== false)
453 $unload = false;
454 else if(strpos($p_key, 'celtispack/') !== false && strpos($filter['group'][$type]['plugins'], 'celtispack_module/') !== false)
455 $unload = false;
456 }
457 }
458 //ToDo elseif($this->is_amp()){
459 //ToDo }
460 else {
461 $pgfopt = false;
462 if(is_singular()){
463 if(!empty($single_opt) && $single_opt['filter'] === 'include'){
464 $pgfopt = true;
465 if(false !== strpos($single_opt[$devtype], $p_key)){
466 $unload = false;
467 }
468 else {
469 //Enable plugin because plugin module is selected
470 if(strpos($p_key, 'jetpack/') !== false && strpos($single_opt[$devtype], 'jetpack_module/') !== false)
471 $unload = false;
472 else if(strpos($p_key, 'celtispack/') !== false && strpos($single_opt[$devtype], 'celtispack_module/') !== false)
473 $unload = false;
474
475 }
476 }
477 }
478 if($pgfopt === false){
479 $type = false;
480 if(is_home() || is_front_page())
481 $type = 'home';
482 elseif(is_archive())
483 $type = 'archive';
484 elseif(is_search())
485 $type = 'search';
486 elseif(is_attachment())
487 $type = 'attachment';
488 elseif(is_page())
489 $type = 'page';
490 elseif(is_single()){ //Post & Custom Post
491 $type = get_post_type( $wp_query->post);
492 if($type === 'post'){
493 $fmt = get_post_format( $wp_query->post);
494 $type = ($fmt === 'standard' || $fmt == false)? 'post' : "post-$fmt";
495 }
496 }
497 if($type !== false && !empty($filter['group'][$type]['plugins'])){
498 if(in_array($p_key, array_map("trim", explode(',', $filter['group'][$type]['plugins']))))
499 $unload = false;
500 else {
501 if(strpos($p_key, 'jetpack/') !== false && strpos($filter['group'][$type]['plugins'], 'jetpack_module/') !== false)
502 $unload = false;
503 else if(strpos($p_key, 'celtispack/') !== false && strpos($filter['group'][$type]['plugins'], 'celtispack_module/') !== false)
504 $unload = false;
505 }
506 }
507 }
508 }
509 }
510 }
511 }
512 }
513 if(!$unload) {
514 if($option === 'active_sitewide_plugins')
515 $new_value[$item] = $value[$item];
516 else
517 $new_value[] = $item;
518 }
519 }
520 $this->cache[$keyid][$option] = $new_value;
521 return $new_value;
522 }
523 }
524