PluginProbe
Plugin Load Filter / 2.4.0
Plugin Load Filter v2.4.0
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.0, at mu-plugins/plf-filter.php

494 lines 23.9 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.0
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 create_initial_taxonomies();
133 create_initial_post_types();
134 return $this->plf_filter( 'active_sitewide_plugins', $default);
135 }
136
137 //active plugins Filter
138 function active_plugins( $default = false) {
139 return $this->plf_filter( 'active_plugins', $default);
140 }
141
142 //Jetpack module Filter
143 function active_jetmodules( $default = false) {
144 return $this->plf_filter( 'jetpack_active_modules', $default);
145 }
146
147 //Celtispack module Filter
148 function active_celtismodules( $default = false) {
149 return $this->plf_filter( 'celtispack_active_modules', $default);
150 }
151
152 //Post Format Type, Custom Post Type Data Cache for parse request
153 function cache_post_type() {
154 if (!is_admin() || ( defined('DOING_AJAX') && DOING_AJAX ) || (defined('DOING_CRON') && DOING_CRON))
155 return;
156
157 global $wp;
158 $public_query_vars = (!empty($wp->public_query_vars))? $wp->public_query_vars : array();;
159 $post_type_query_vars = array();
160 foreach ( get_post_types( array(), 'objects' ) as $post_type => $t ){
161 if ( $t->query_var )
162 $post_type_query_vars[$t->query_var] = $post_type;
163 }
164 $queryable_post_types = get_post_types( array('publicly_queryable' => true) );
165
166 $data = get_option('plf_queryvars');
167 if(!empty($post_type_query_vars) && !empty($queryable_post_types)){
168 $data['public_query_vars'] = $public_query_vars;
169 $data['post_type_query_vars'] = $post_type_query_vars;
170 $data['queryable_post_types'] = $queryable_post_types;
171 update_option('plf_queryvars', $data);
172 }
173 else if(!empty($data['post_type_query_vars']) || !empty($data['queryable_post_types'])){
174 delete_option('plf_queryvars');
175 }
176 }
177
178 //parse_request Action Hook for Custom Post Type query add
179 function parse_request( &$args ) {
180 if (did_action( 'plugins_loaded' ) === 0 ) {
181 $data = get_option('plf_queryvars');
182 if(!empty($data['post_type_query_vars']) && !empty($data['queryable_post_types'])){
183 $post_type_query_vars = $data['post_type_query_vars'];
184 $queryable_post_types = $data['queryable_post_types'];
185
186 $args->public_query_vars = $data['public_query_vars'];
187 if ( isset( $args->matched_query ) ) {
188 parse_str($args->matched_query, $perma_query_vars);
189 }
190 foreach ( $args->public_query_vars as $wpvar ) {
191 if ( isset( $args->extra_query_vars[$wpvar] ) )
192 $args->query_vars[$wpvar] = $args->extra_query_vars[$wpvar];
193 elseif ( isset( $_POST[$wpvar] ) )
194 $args->query_vars[$wpvar] = $_POST[$wpvar];
195 elseif ( isset( $_GET[$wpvar] ) )
196 $args->query_vars[$wpvar] = $_GET[$wpvar];
197 elseif ( isset( $perma_query_vars[$wpvar] ) )
198 $args->query_vars[$wpvar] = $perma_query_vars[$wpvar];
199
200 if ( !empty( $args->query_vars[$wpvar] ) ) {
201 if ( ! is_array( $args->query_vars[$wpvar] ) ) {
202 $args->query_vars[$wpvar] = (string) $args->query_vars[$wpvar];
203 } else {
204 foreach ( $args->query_vars[$wpvar] as $vkey => $v ) {
205 if ( !is_object( $v ) ) {
206 $args->query_vars[$wpvar][$vkey] = (string) $v;
207 }
208 }
209 }
210 if ( isset($post_type_query_vars[$wpvar] ) ) {
211 $args->query_vars['post_type'] = $post_type_query_vars[$wpvar];
212 $args->query_vars['name'] = $args->query_vars[$wpvar];
213 }
214 }
215 }
216
217 // Limit publicly queried post_types to those that are publicly_queryable
218 if ( isset( $args->query_vars['post_type']) ) {
219 if ( ! is_array( $args->query_vars['post_type'] ) ) {
220 if ( ! in_array( $args->query_vars['post_type'], $queryable_post_types ) )
221 unset( $args->query_vars['post_type'] );
222 } else {
223 $args->query_vars['post_type'] = array_intersect( $args->query_vars['post_type'], $queryable_post_types );
224 }
225 }
226 }
227 }
228 }
229
230
231 //Plugin Load Filter Main (active plugins/modules filtering)
232 function plf_filter( $option, $default = false) {
233 if ( defined( 'WP_SETUP_CONFIG' ) )
234 return false;
235
236 //Admin mode exclude
237 if(is_admin())
238 return false;
239
240 if ( ! defined( 'WP_INSTALLING' ) ) {
241 global $wpdb, $current_site;
242 // if ( ! wp_installing() ) { // WP4.4
243 if ( is_multisite() && $option === 'active_sitewide_plugins' ) {
244 //get_network_option() current site ID
245 $network_id = $current_site->id;
246
247 // prevent non-existent options from triggering multiple queries
248 $notoptions_key = "$network_id:notoptions";
249 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
250 if ( isset( $notoptions[ $option ] ) ) {
251 return apply_filters( 'default_site_option_' . $option, $default, $option );
252 }
253
254 $cache_key = "$network_id:$option";
255 $value = wp_cache_get( $cache_key, 'site-options' );
256
257 if ( ! isset( $value ) || false === $value ) {
258 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
259
260 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
261 if ( is_object( $row ) ) {
262 $value = $row->meta_value;
263 $value = maybe_unserialize( $value );
264 wp_cache_set( $cache_key, $value, 'site-options' );
265 } else {
266 if ( ! is_array( $notoptions ) ) {
267 $notoptions = array();
268 }
269 $notoptions[ $option ] = true;
270 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
271
272 /** This filter is documented in wp-includes/option.php */
273 $value = apply_filters( 'default_site_option_' . $option, $default, $option );
274 }
275 }
276 }
277 else {
278 // prevent non-existent options from triggering multiple queries
279 $notoptions = wp_cache_get( 'notoptions', 'options' );
280 if ( isset( $notoptions[$option] ) )
281 return apply_filters( 'default_option_' . $option, $default );
282
283 $alloptions = wp_load_alloptions();
284 if ( isset( $alloptions[$option] ) ) {
285 $value = $alloptions[$option];
286 } else {
287 $value = wp_cache_get( $option, 'options' );
288
289 if ( false === $value ) {
290 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
291
292 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
293 if ( is_object( $row ) ) {
294 $value = $row->option_value;
295 wp_cache_add( $option, $value, 'options' );
296 } else { // option does not exist, so we must cache its non-existence
297 if ( ! is_array( $notoptions ) ) {
298 $notoptions = array();
299 }
300 $notoptions[$option] = true;
301 wp_cache_set( 'notoptions', $notoptions, 'options' );
302
303 /** This filter is documented in wp-includes/option.php */
304 return apply_filters( 'default_option_' . $option, $default, $option );
305 }
306 }
307 }
308 }
309 } else {
310 return false;
311 }
312
313 //Equal treatment for when the wp_is_mobile is not yet available(wp-include/vars.php wp_is_mobile)
314 if ( empty($_SERVER['HTTP_USER_AGENT']) ) {
315 $is_mobile = false;
316 } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.)
317 || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
318 || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
319 || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
320 || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
321 || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
322 || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) {
323 $is_mobile = true;
324 } else {
325 $is_mobile = false;
326 }
327 $is_mobile = apply_filters( 'custom_is_mobile' , $is_mobile );
328
329 $filter = $this->filter;
330 //get_option is called many times, intermediate processing data to cache
331 $keyid = md5('plf_'. (string)$is_mobile . $_SERVER['REQUEST_URI']);
332 if(!empty($this->cache[$keyid][$option])){
333 return $this->cache[$keyid][$option];
334 }
335
336 //Before plugins loaded, it does not use conditional branch such as is_home, to set wp_query, wp in temporary query
337 if(empty($GLOBALS['wp_the_query'])){
338 $GLOBALS['wp_the_query'] = new WP_Query();
339 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
340 $GLOBALS['wp_rewrite'] = new WP_Rewrite();
341 $GLOBALS['wp'] = new WP();
342 //Post Format, Custom Post Type support
343 add_action('parse_request', array(&$this, 'parse_request'));
344 $GLOBALS['wp']->parse_request('');
345 $GLOBALS['wp']->query_posts();
346 }
347 //Only available display pages (login, cron, ajax request ... excluded)
348 //downloadmanager plugin downloadlink request [home]/?wpdmact=XXXXXX exclude home GET query
349 global $wp_query;
350 if(!(function_exists('is_embed') && is_embed())){
351 if((is_home() || is_front_page() || is_archive() || is_search() || is_singular()) == false
352 || (is_home() && !empty($_GET))
353 || (is_singular() && empty($wp_query->post))){
354 return false;
355 }
356 }
357
358 $us_value = ($option === 'active_sitewide_plugins')? array_keys( (array)$value ) : maybe_unserialize( $value );
359 $new_value = array();
360 foreach ( $us_value as $item ) {
361 $unload = false;
362 //Jetpack module slug / Celtiapack module slug / Plugin php file name
363 if($option === 'jetpack_active_modules'){
364 $p_key = 'jetpack_module/' . $item;
365 }
366 elseif($option === 'celtispack_active_modules'){
367 $p_key = 'celtispack_module/' . str_replace( '.php', '', basename( $item) );
368 }
369 else {
370 $p_key = $item;
371 }
372 //admin mode filter
373 if(!empty($filter['_admin']['plugins'])){
374 if(in_array($p_key, array_map("trim", explode(',', $filter['_admin']['plugins']))))
375 $unload = true;
376 }
377 //page filter
378 if(!$unload){
379 if(!empty($filter['_pagefilter']['plugins'])){
380 if(in_array($p_key, array_map("trim", explode(',', $filter['_pagefilter']['plugins'])))){
381 $unload = true;
382
383 //desktop/mobile device disable filter
384 $single_opt = false;
385 $dis_dev = true;
386 $devtype = ($is_mobile)? 'mobile' : 'desktop';
387 if(is_singular() && is_object($wp_query->post)){
388 $myfilter = get_post_meta( $wp_query->post->ID, '_plugin_load_filter', true );
389 //For ver2.2.0 compatibility, set 'plugins' data to 'desktop' and 'mobile'
390 if(!empty($myfilter['plugins'])){
391 $myfilter['desktop'] = $myfilter['plugins'];
392 $myfilter['mobile'] = $myfilter['plugins'];
393 unset($myfilter['plugins']);
394 }
395 $default = array( 'filter' => 'default', 'desktop' => '', 'mobile' => '');
396 $single_opt = (!empty($myfilter))? $myfilter : $default;
397 $single_opt = wp_parse_args( $single_opt, $default);
398 if($single_opt['filter'] === 'include'){
399 if(false !== strpos($single_opt[$devtype], $p_key))
400 $dis_dev = false;
401 else if(strpos($p_key, 'jetpack/') !== false && strpos($single_opt[$devtype], 'jetpack_module/') !== false)
402 $dis_dev = false;
403 else if(strpos($p_key, 'celtispack/') !== false && strpos($single_opt[$devtype], 'celtispack_module/') !== false)
404 $dis_dev = false;
405 }
406 }
407 if(empty($single_opt) || $single_opt['filter'] === 'default'){
408 if(!empty($filter['group'][$devtype]['plugins'])){
409 if(false !== strpos($filter['group'][$devtype]['plugins'], $p_key))
410 $dis_dev = false;
411 else if(strpos($p_key, 'jetpack/') !== false && strpos($filter['group'][$type]['plugins'], 'jetpack_module/') !== false)
412 $dis_dev = false;
413 else if(strpos($p_key, 'celtispack/') !== false && strpos($filter['group'][$type]['plugins'], 'celtispack_module/') !== false)
414 $dis_dev = false;
415 }
416 }
417 if(!$dis_dev){
418 //oEmbed Content API
419 if(function_exists('is_embed') && is_embed()){
420 $type = 'content-card';
421 if(!empty($filter['group'][$type]['plugins'])){
422 if(false !== strpos($filter['group'][$type]['plugins'], $p_key))
423 $unload = false;
424 else if(strpos($p_key, 'jetpack/') !== false && strpos($filter['group'][$type]['plugins'], 'jetpack_module/') !== false)
425 $unload = false;
426 else if(strpos($p_key, 'celtispack/') !== false && strpos($filter['group'][$type]['plugins'], 'celtispack_module/') !== false)
427 $unload = false;
428 }
429 }
430 else {
431 $pgfopt = false;
432 if(is_singular()){
433 if(!empty($single_opt) && $single_opt['filter'] === 'include'){
434 $pgfopt = true;
435 if(false !== strpos($single_opt[$devtype], $p_key)){
436 $unload = false;
437 }
438 else {
439 //Enable plugin because plugin module is selected
440 if(strpos($p_key, 'jetpack/') !== false && strpos($single_opt[$devtype], 'jetpack_module/') !== false)
441 $unload = false;
442 else if(strpos($p_key, 'celtispack/') !== false && strpos($single_opt[$devtype], 'celtispack_module/') !== false)
443 $unload = false;
444
445 }
446 }
447 }
448 if($pgfopt === false){
449 $type = false;
450 if(is_home() || is_front_page())
451 $type = 'home';
452 elseif(is_archive())
453 $type = 'archive';
454 elseif(is_search())
455 $type = 'search';
456 elseif(is_attachment())
457 $type = 'attachment';
458 elseif(is_page())
459 $type = 'page';
460 elseif(is_single()){ //Post & Custom Post
461 $type = get_post_type( $wp_query->post);
462 if($type === 'post'){
463 $fmt = get_post_format( $wp_query->post);
464 $type = ($fmt === 'standard' || $fmt == false)? 'post' : "post-$fmt";
465 }
466 }
467 if($type !== false && !empty($filter['group'][$type]['plugins'])){
468 if(in_array($p_key, array_map("trim", explode(',', $filter['group'][$type]['plugins']))))
469 $unload = false;
470 else {
471 if(strpos($p_key, 'jetpack/') !== false && strpos($filter['group'][$type]['plugins'], 'jetpack_module/') !== false)
472 $unload = false;
473 else if(strpos($p_key, 'celtispack/') !== false && strpos($filter['group'][$type]['plugins'], 'celtispack_module/') !== false)
474 $unload = false;
475 }
476 }
477 }
478 }
479 }
480 }
481 }
482 }
483 if(!$unload) {
484 if($option === 'active_sitewide_plugins')
485 $new_value[$item] = $value[$item];
486 else
487 $new_value[] = $item;
488 }
489 }
490 $this->cache[$keyid][$option] = $new_value;
491 return $new_value;
492 }
493 }
494