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