PluginProbe
Plugin Load Filter / 2.3.0
Plugin Load Filter v2.3.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.3.0, at mu-plugins/plf-filter.php

414 lines 19.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.3.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
12 /***************************************************************************
13 * pluggable.php defined function overwrite
14 * pluggable.php read before the query_posts () is processed by the current user undetermined
15 **************************************************************************/
16 if ( !function_exists('wp_get_current_user') ) :
17 /**
18 * Retrieve the current user object.
19 * @return WP_User Current user WP_User object
20 */
21 function wp_get_current_user() {
22 if ( ! function_exists( 'wp_set_current_user' ) )
23 return 0;
24 //get_currentuserinfo is deprecated since version 4.5
25 if ( version_compare( $GLOBALS['wp_version'], '4.5', '<' ) ){
26 global $current_user;
27 get_currentuserinfo();
28 return $current_user;
29 }
30 else {
31 return _wp_get_current_user();
32 }
33 }
34 endif;
35
36 if ( !function_exists('get_userdata') ) :
37 /**
38 * Retrieve user info by user ID.
39 * @param int $user_id User ID
40 * @return WP_User|bool WP_User object on success, false on failure.
41 */
42 function get_userdata( $user_id ) {
43 return get_user_by( 'id', $user_id );
44 }
45 endif;
46
47 if ( !function_exists('get_user_by') ) :
48 /**
49 * Retrieve user info by a given field
50 * @param string $field The field to retrieve the user with. id | slug | email | login
51 * @param int|string $value A value for $field. A user ID, slug, email address, or login name.
52 * @return WP_User|bool WP_User object on success, false on failure.
53 */
54 function get_user_by( $field, $value ) {
55 $userdata = WP_User::get_data_by( $field, $value );
56
57 if ( !$userdata )
58 return false;
59
60 $user = new WP_User;
61 $user->init( $userdata );
62
63 return $user;
64 }
65 endif;
66
67 if ( !function_exists('is_user_logged_in') ) :
68 /**
69 * Checks if the current visitor is a logged in user.
70 * @return bool True if user is logged in, false if not logged in.
71 */
72 function is_user_logged_in() {
73 if ( ! function_exists( 'wp_set_current_user' ) )
74 return false;
75
76 $user = wp_get_current_user();
77
78 if ( ! $user->exists() )
79 return false;
80
81 return true;
82 }
83 endif;
84
85 /***************************************************************************
86 * Plugin Load Filter( Admin, Desktop, Mobile, Page 4types filter)
87 **************************************************************************/
88
89 $plugin_load_filter = new Plf_filter();
90
91 class Plf_filter {
92
93 private $filter = array(); //Plugin Load Filter Setting option data
94 private $cache;
95
96 function __construct() {
97 $this->filter = get_option('plf_option');
98 //v2.3.0 option change
99 if(empty($this->filter['optver']) || $this->filter['optver'] < '2'){
100 //For ver2.2.0 compatibility, set '_pagefilter' data to 'desktop' and 'mobile'
101 if(empty($this->filter['group']['desktop']) && empty($this->filter['group']['mobile'])){
102 if(!empty($this->filter['_pagefilter'])){
103 $this->filter['group']['desktop'] = $this->filter['_pagefilter'];
104 $this->filter['group']['mobile'] = $this->filter['_pagefilter'];
105 }
106 }
107 }
108
109 $this->cache = null;
110 if(!empty($this->filter)){
111 add_filter('pre_option_active_plugins', array(&$this, 'active_plugins'));
112 add_filter('pre_option_jetpack_active_modules', array(&$this, 'active_jetmodules'));
113 add_filter('pre_option_celtispack_active_modules', array(&$this, 'active_celtismodules'));
114 add_action('wp_loaded', array(&$this, 'cache_post_type'), 1);
115 }
116 }
117
118 //Active plugins Filter
119 function active_plugins( $default = false) {
120 return $this->plf_filter( 'active_plugins', $default);
121 }
122
123 //Jetpack module Filter
124 function active_jetmodules( $default = false) {
125 return $this->plf_filter( 'jetpack_active_modules', $default);
126 }
127
128 //Celtispack module Filter
129 function active_celtismodules( $default = false) {
130 return $this->plf_filter( 'celtispack_active_modules', $default);
131 }
132
133 //Post Format Type, Custom Post Type Data Cache for parse request
134 function cache_post_type() {
135 if (!is_admin() || ( defined('DOING_AJAX') && DOING_AJAX ) || (defined('DOING_CRON') && DOING_CRON))
136 return;
137
138 global $wp;
139 $public_query_vars = (!empty($wp->public_query_vars))? $wp->public_query_vars : array();;
140 $post_type_query_vars = array();
141 foreach ( get_post_types( array(), 'objects' ) as $post_type => $t ){
142 if ( $t->query_var )
143 $post_type_query_vars[$t->query_var] = $post_type;
144 }
145 $queryable_post_types = get_post_types( array('publicly_queryable' => true) );
146
147 $data = get_option('plf_queryvars');
148 if(!empty($post_type_query_vars) && !empty($queryable_post_types)){
149 $data['public_query_vars'] = $public_query_vars;
150 $data['post_type_query_vars'] = $post_type_query_vars;
151 $data['queryable_post_types'] = $queryable_post_types;
152 update_option('plf_queryvars', $data);
153 }
154 else if(!empty($data['post_type_query_vars']) || !empty($data['queryable_post_types'])){
155 delete_option('plf_queryvars');
156 }
157 }
158
159 //parse_request Action Hook for Custom Post Type query add
160 function parse_request( &$args ) {
161 if (did_action( 'plugins_loaded' ) === 0 ) {
162 $data = get_option('plf_queryvars');
163 if(!empty($data['post_type_query_vars']) && !empty($data['queryable_post_types'])){
164 $post_type_query_vars = $data['post_type_query_vars'];
165 $queryable_post_types = $data['queryable_post_types'];
166
167 $args->public_query_vars = $data['public_query_vars'];
168 if ( isset( $args->matched_query ) ) {
169 parse_str($args->matched_query, $perma_query_vars);
170 }
171 foreach ( $args->public_query_vars as $wpvar ) {
172 if ( isset( $args->extra_query_vars[$wpvar] ) )
173 $args->query_vars[$wpvar] = $args->extra_query_vars[$wpvar];
174 elseif ( isset( $_POST[$wpvar] ) )
175 $args->query_vars[$wpvar] = $_POST[$wpvar];
176 elseif ( isset( $_GET[$wpvar] ) )
177 $args->query_vars[$wpvar] = $_GET[$wpvar];
178 elseif ( isset( $perma_query_vars[$wpvar] ) )
179 $args->query_vars[$wpvar] = $perma_query_vars[$wpvar];
180
181 if ( !empty( $args->query_vars[$wpvar] ) ) {
182 if ( ! is_array( $args->query_vars[$wpvar] ) ) {
183 $args->query_vars[$wpvar] = (string) $args->query_vars[$wpvar];
184 } else {
185 foreach ( $args->query_vars[$wpvar] as $vkey => $v ) {
186 if ( !is_object( $v ) ) {
187 $args->query_vars[$wpvar][$vkey] = (string) $v;
188 }
189 }
190 }
191 if ( isset($post_type_query_vars[$wpvar] ) ) {
192 $args->query_vars['post_type'] = $post_type_query_vars[$wpvar];
193 $args->query_vars['name'] = $args->query_vars[$wpvar];
194 }
195 }
196 }
197
198 // Limit publicly queried post_types to those that are publicly_queryable
199 if ( isset( $args->query_vars['post_type']) ) {
200 if ( ! is_array( $args->query_vars['post_type'] ) ) {
201 if ( ! in_array( $args->query_vars['post_type'], $queryable_post_types ) )
202 unset( $args->query_vars['post_type'] );
203 } else {
204 $args->query_vars['post_type'] = array_intersect( $args->query_vars['post_type'], $queryable_post_types );
205 }
206 }
207 }
208 }
209 }
210
211 //Plugin Load Filter Main (active plugins/modules filtering)
212 function plf_filter( $option, $default = false) {
213 if ( defined( 'WP_SETUP_CONFIG' ) )
214 return false;
215
216 //Admin mode exclude
217 if(is_admin())
218 return false;
219
220 if ( ! defined( 'WP_INSTALLING' ) ) {
221 // prevent non-existent options from triggering multiple queries
222 $notoptions = wp_cache_get( 'notoptions', 'options' );
223 if ( isset( $notoptions[$option] ) )
224 return apply_filters( 'default_option_' . $option, $default );
225
226 $alloptions = wp_load_alloptions();
227 if ( isset( $alloptions[$option] ) ) {
228 $value = $alloptions[$option];
229 } else {
230 $value = wp_cache_get( $option, 'options' );
231
232 if ( false === $value ) {
233 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
234
235 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
236 if ( is_object( $row ) ) {
237 $value = $row->option_value;
238 wp_cache_add( $option, $value, 'options' );
239 } else { // option does not exist, so we must cache its non-existence
240 $notoptions[$option] = true;
241 wp_cache_set( 'notoptions', $notoptions, 'options' );
242
243 /** This filter is documented in wp-includes/option.php */
244 return apply_filters( 'default_option_' . $option, $default );
245 }
246 }
247 }
248 } else {
249 return false;
250 }
251
252 $filter = $this->filter;
253 //get_option is called many times, intermediate processing data to cache
254 $keyid = md5('plf_'. (string)wp_is_mobile(). $_SERVER['REQUEST_URI']);
255 if(!empty($this->cache[$keyid][$option])){
256 return apply_filters( 'option_' . $option, $this->cache[$keyid][$option]);
257 }
258
259 //Before plugins loaded, it does not use conditional branch such as is_home, to set wp_query, wp in temporary query
260 if(empty($GLOBALS['wp_the_query'])){
261 $GLOBALS['wp_the_query'] = new WP_Query();
262 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
263 $GLOBALS['wp_rewrite'] = new WP_Rewrite();
264 $GLOBALS['wp'] = new WP();
265 //Post Format, Custom Post Type support
266 add_action('parse_request', array(&$this, 'parse_request'));
267 $GLOBALS['wp']->parse_request('');
268 $GLOBALS['wp']->query_posts();
269 }
270 //Only available display pages (login, cron, ajax request ... excluded)
271 //downloadmanager plugin downloadlink request [home]/?wpdmact=XXXXXX exclude home GET query
272 global $wp_query;
273 if(!(function_exists('is_embed') && is_embed())){
274 if((is_home() || is_front_page() || is_archive() || is_search() || is_singular()) == false
275 || (is_home() && !empty($_GET))
276 || (is_singular() && empty($wp_query->post))){
277 return false;
278 }
279 }
280
281 $us_value = maybe_unserialize( $value );
282 $new_value = array();
283 foreach ( $us_value as $item ) {
284 $unload = false;
285 //Jetpack module slug / Celtiapack module slug / Plugin php file name
286 if($option === 'jetpack_active_modules'){
287 $p_key = 'jetpack_module/' . $item;
288 }
289 elseif($option === 'celtispack_active_modules'){
290 $p_key = 'celtispack_module/' . str_replace( '.php', '', basename( $item) );
291 }
292 else {
293 $p_key = $item;
294 }
295 //admin mode filter
296 if(!empty($filter['_admin']['plugins'])){
297 if(in_array($p_key, array_map("trim", explode(',', $filter['_admin']['plugins']))))
298 $unload = true;
299 }
300 //page filter
301 if(!$unload){
302 if(!empty($filter['_pagefilter']['plugins'])){
303 if(in_array($p_key, array_map("trim", explode(',', $filter['_pagefilter']['plugins'])))){
304 $unload = true;
305
306 //desktop/mobile device disable filter
307 $single_opt = false;
308 $dis_dev = true;
309 $devtype = (wp_is_mobile())? 'mobile' : 'desktop';
310 if(is_singular() && is_object($wp_query->post)){
311 $myfilter = get_post_meta( $wp_query->post->ID, '_plugin_load_filter', true );
312 //For ver2.2.0 compatibility, set 'plugins' data to 'desktop' and 'mobile'
313 if(!empty($myfilter['plugins'])){
314 $myfilter['desktop'] = $myfilter['plugins'];
315 $myfilter['mobile'] = $myfilter['plugins'];
316 unset($myfilter['plugins']);
317 }
318 $default = array( 'filter' => 'default', 'desktop' => '', 'mobile' => '');
319 $single_opt = (!empty($myfilter))? $myfilter : $default;
320 $single_opt = wp_parse_args( $single_opt, $default);
321 if($single_opt['filter'] === 'include'){
322 if(false !== strpos($single_opt[$devtype], $p_key))
323 $dis_dev = false;
324 else if(strpos($p_key, 'jetpack/') !== false && strpos($single_opt[$devtype], 'jetpack_module/') !== false)
325 $dis_dev = false;
326 else if(strpos($p_key, 'celtispack/') !== false && strpos($single_opt[$devtype], 'celtispack_module/') !== false)
327 $dis_dev = false;
328 }
329 }
330 if(empty($single_opt) || $single_opt['filter'] === 'default'){
331 if(!empty($filter['group'][$devtype]['plugins'])){
332 if(false !== strpos($filter['group'][$devtype]['plugins'], $p_key))
333 $dis_dev = false;
334 else if(strpos($p_key, 'jetpack/') !== false && strpos($filter['group'][$type]['plugins'], 'jetpack_module/') !== false)
335 $dis_dev = false;
336 else if(strpos($p_key, 'celtispack/') !== false && strpos($filter['group'][$type]['plugins'], 'celtispack_module/') !== false)
337 $dis_dev = false;
338 }
339 }
340 if(!$dis_dev){
341 //oEmbed Content API
342 if(function_exists('is_embed') && is_embed()){
343 $type = 'content-card';
344 if(!empty($filter['group'][$type]['plugins'])){
345 if(false !== strpos($filter['group'][$type]['plugins'], $p_key))
346 $unload = false;
347 else if(strpos($p_key, 'jetpack/') !== false && strpos($filter['group'][$type]['plugins'], 'jetpack_module/') !== false)
348 $unload = false;
349 else if(strpos($p_key, 'celtispack/') !== false && strpos($filter['group'][$type]['plugins'], 'celtispack_module/') !== false)
350 $unload = false;
351 }
352 }
353 else {
354 $pgfopt = false;
355 if(is_singular()){
356 if(!empty($single_opt) && $single_opt['filter'] === 'include'){
357 $pgfopt = true;
358 if(false !== strpos($single_opt[$devtype], $p_key)){
359 $unload = false;
360 }
361 else {
362 //Enable plugin because plugin module is selected
363 if(strpos($p_key, 'jetpack/') !== false && strpos($single_opt[$devtype], 'jetpack_module/') !== false)
364 $unload = false;
365 else if(strpos($p_key, 'celtispack/') !== false && strpos($single_opt[$devtype], 'celtispack_module/') !== false)
366 $unload = false;
367
368 }
369 }
370 }
371 if($pgfopt === false){
372 $type = false;
373 if(is_home() || is_front_page())
374 $type = 'home';
375 elseif(is_archive())
376 $type = 'archive';
377 elseif(is_search())
378 $type = 'search';
379 elseif(is_attachment())
380 $type = 'attachment';
381 elseif(is_page())
382 $type = 'page';
383 elseif(is_single()){ //Post & Custom Post
384 $type = get_post_type( $wp_query->post);
385 if($type === 'post'){
386 $fmt = get_post_format( $wp_query->post);
387 $type = ($fmt === 'standard' || $fmt == false)? 'post' : "post-$fmt";
388 }
389 }
390 if($type !== false && !empty($filter['group'][$type]['plugins'])){
391 if(in_array($p_key, array_map("trim", explode(',', $filter['group'][$type]['plugins']))))
392 $unload = false;
393 else {
394 if(strpos($p_key, 'jetpack/') !== false && strpos($filter['group'][$type]['plugins'], 'jetpack_module/') !== false)
395 $unload = false;
396 else if(strpos($p_key, 'celtispack/') !== false && strpos($filter['group'][$type]['plugins'], 'celtispack_module/') !== false)
397 $unload = false;
398 }
399 }
400 }
401 }
402 }
403 }
404 }
405 }
406 if(!$unload) {
407 $new_value[] = $item;
408 }
409 }
410 $this->cache[$keyid][$option] = $new_value;
411 return apply_filters( 'option_' . $option, $new_value );
412 }
413 }
414