PluginProbe
Plugin Load Filter / 4.3.0
Plugin Load Filter v4.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 4.3.0, at mu-plugins/plf-filter.php

1,492 lines 74.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: 4.3.0
6 Plugin URI: http://celtislab.net/en/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 /**
18 * Gets hash of given string.
19 *
20 * @param string $data Plain text to hash.
21 * @return string Hash of $data.
22 */
23 //wp_salt( $scheme ) を簡略化して logged_in cookie 限定
24 function plf_logged_in_hash( $data, $algo = 'md5' ) {
25 $values = array(
26 'key' => '',
27 'salt' => '',
28 );
29 foreach ( array( 'key', 'salt' ) as $type ) {
30 $const = strtoupper( "logged_in_{$type}" );
31 if ( defined( $const ) && constant( $const ) ) {
32 $values[ $type ] = constant( $const );
33 }
34 }
35 $salt = $values['key'] . $values['salt'];
36
37 // Ensure the algorithm is supported by the hash_hmac function.
38 if ( ! in_array( $algo, hash_hmac_algos(), true ) ) {
39 throw new InvalidArgumentException(
40 sprintf(
41 /* translators: 1: Name of a cryptographic hash algorithm. 2: List of supported algorithms. */
42 __( 'Unsupported hashing algorithm: %1$s. Supported algorithms are: %2$s' ),
43 $algo,
44 implode( ', ', hash_hmac_algos() )
45 )
46 );
47 }
48 return hash_hmac( $algo, $data, $salt );
49 }
50
51 if ( !function_exists('wp_get_current_user') ) :
52 /**
53 * Retrieve the current user object.
54 * @return WP_User Current user WP_User object
55 */
56 function wp_get_current_user() {
57 static $_current_user = null;
58 //ver4.2.1 Depending on the environment, a JS error may occur in the iframe in the Customizer, so an IFRAME_REQUEST check has been added.
59 if ( ! function_exists( 'wp_set_current_user' ) || (! defined( 'IFRAME_REQUEST') && did_action( 'setup_theme' ) === 0) ){
60 if ( defined( 'LOGGED_IN_COOKIE' ) && !empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) {
61 $cookie_elements = explode( '|', $_COOKIE[ LOGGED_IN_COOKIE ] );
62 if ( count( $cookie_elements ) === 4 ) {
63 list( $username, $expiration, $token, $hmac ) = $cookie_elements;
64 if ( $expiration > time() ) {
65 if (!empty($_current_user) && $_current_user->user_login === $username){
66 return $_current_user;
67 }
68 $user = get_user_by( 'login', $username );
69 if ( $user ) {
70 //WP6.8 bcrypt サポートにより $pass_frag が変更された
71 //$pass_frag = substr( $user->user_pass, 8, 4 );
72 if ( str_starts_with( $user->user_pass, '$P$' ) || str_starts_with( $user->user_pass, '$2y$' ) ) {
73 // Retain previous behaviour of phpass or vanilla bcrypt hashed passwords.
74 $pass_frag = substr( $user->user_pass, 8, 4 );
75 } else {
76 // Otherwise, use a substring from the end of the hash to avoid dealing with potentially long hash prefixes.
77 $pass_frag = substr( $user->user_pass, -4 );
78 }
79 $key = plf_logged_in_hash( $username . '|' . $pass_frag . '|' . $expiration . '|' . $token );
80 $hash = hash_hmac( 'sha256', $username . '|' . $expiration . '|' . $token, $key );
81
82 if ( hash_equals( $hash, $hmac ) ) {
83 $manager = WP_Session_Tokens::get_instance( $user->ID );
84 if ( $manager->verify( $token ) ) {
85 //この時点のユーザーは仮ユーザーデータであり $current_user は未設定とする
86 $_current_user = $user;
87 return $user;
88 }
89 }
90 }
91 }
92 }
93 }
94 //ver4.0.17 fixed https://wordpress.org/support/topic/wp_get_current_user-is-overridden/
95 //return 0;
96 $user = new WP_User( 0 );
97 return $user;
98
99 } else {
100 //$GLOBALS['wp_roles'] セット後の setup_theme アクション後に $current_user を設定する
101 //※使用プラグインの組み合わせにもよるが、想定外に早い $current_user 設定は bbpress 等の一部プラグインにおいて capability 動的追加が反映されないことがあるため
102 return _wp_get_current_user();
103 }
104 }
105 endif;
106
107 if ( !function_exists('get_userdata') ) :
108 /**
109 * Retrieve user info by user ID.
110 * @param int $user_id User ID
111 * @return WP_User|bool WP_User object on success, false on failure.
112 */
113 function get_userdata( $user_id ) {
114 return get_user_by( 'id', $user_id );
115 }
116 endif;
117
118 if ( !function_exists('get_user_by') ) :
119 /**
120 * Retrieve user info by a given field
121 * @param string $field The field to retrieve the user with. id | slug | email | login
122 * @param int|string $value A value for $field. A user ID, slug, email address, or login name.
123 * @return WP_User|bool WP_User object on success, false on failure.
124 */
125 function get_user_by( $field, $value ) {
126 $userdata = WP_User::get_data_by( $field, $value );
127
128 if ( !$userdata )
129 return false;
130
131 $user = new WP_User;
132 $user->init( $userdata );
133
134 return $user;
135 }
136 endif;
137
138 if ( !function_exists('is_user_logged_in') ) :
139 /**
140 * Checks if the current visitor is a logged in user.
141 * @return bool True if user is logged in, false if not logged in.
142 */
143 function is_user_logged_in() {
144 if ( ! function_exists( 'wp_set_current_user' ) )
145 return false;
146
147 $user = wp_get_current_user();
148
149 if ( ! $user->exists() )
150 return false;
151
152 return true;
153 }
154 endif;
155
156 /***************************************************************************
157 * Plugin Load Filter
158 **************************************************************************/
159
160 if(in_array( 'plugin-load-filter/plugin-load-filter.php', (array) get_option( 'active_plugins', array() ) )){
161 $plugin_load_filter = new Plf_filter();
162 } elseif ( is_multisite() ) {
163 $plugins = get_site_option( 'active_sitewide_plugins');
164 if ( isset($plugins['plugin-load-filter/plugin-load-filter.php']) ){
165 $plugin_load_filter = new Plf_filter();
166 }
167 }
168
169 class Plf_filter {
170 static private $filter; //Plugin Load Filter Setting option data
171 static private $rlocale;
172 static private $url_filter;
173 static private $s_url_filter;
174 static private $is_filterkey;
175 static private $iniget_active_plugins;
176 static private $iniget_active_sitewide_plugins;
177 static private $base_plugins;
178 static private $filtered_plugins;
179 static private $cache;
180 static private $url2id;
181 static private $pre_post_id;
182 static private $base_public_query_vars;
183
184 function __construct() {
185 self::$rlocale = null;
186 self::$cache = array();
187 self::$url2id = array();
188 self::$pre_post_id = 0;
189 self::$base_public_query_vars = array();
190 self::$url_filter = array();
191 self::$s_url_filter = array();
192 self::$is_filterkey = false;
193 self::$iniget_active_plugins = array();
194 self::$iniget_active_sitewide_plugins = array();
195 self::$base_plugins = array();
196 self::$filtered_plugins = array();
197 self::$filter = get_option('plf_option', array());
198 if(!empty(self::$filter)){
199 //Addon option get
200 self::$iniget_active_plugins = $plugins = get_option( 'active_plugins', array() );
201 if ( is_multisite() ) {
202 self::$iniget_active_sitewide_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
203 $plugins = array_merge( $plugins, array_keys( self::$iniget_active_sitewide_plugins ) );
204 }
205 if(!empty($plugins) && in_array( 'plugin-load-filter-addon/plugin-load-filter-addon.php', $plugins)){
206 self::$url_filter = get_option('plf_addon_options', array());
207 }
208
209 //active_plugins filter
210 if ( is_multisite() ) {
211 add_filter('pre_site_option_active_sitewide_plugins', array('Plf_filter', 'active_sitewide_plugins'));
212 add_action('update_site_option_active_sitewide_plugins', array($this, 'update_active_sitewide_plugins'), 99999, 4);
213 }
214 add_filter('pre_option_active_plugins', array('Plf_filter', 'active_plugins'));
215 add_filter('pre_option_jetpack_active_modules', array('Plf_filter', 'active_jetmodules'));
216 add_filter('pre_option_celtispack_active_modules', array('Plf_filter', 'active_celtismodules'));
217
218 add_filter('plf_singler_custom_url_to_postid', array('Plf_filter', 'custom_url_to_postid'), 10, 3);
219
220 add_action('update_option_active_plugins', array($this, 'update_active_plugins'), 99999, 3);
221 add_action('update_option_jetpack_active_modules', array($this, 'update_active_jetmodules'), 99999, 3);
222 add_action('update_option_celtispack_active_modules', array($this, 'update_active_celtismodules'), 99999, 3);
223 add_action('switch_theme', array($this, 'switch_theme'));
224
225 add_action('plugins_loaded', array($this, 'plugins_loaded'));
226 add_action('admin_init', array($this, 'cache_post_type'));
227 //admin-bar filtered status
228 if(!empty(self::$filter['admin_bar'])){
229 add_action('admin_bar_menu', array($this,'custom_bar_menus'), 201);
230 }
231 //language locale filter
232 if(!empty(self::$filter['language'])){
233 add_filter( 'locale', array('Plf_filter', 'post_locale') );
234 add_filter( 'determine_locale', array('Plf_filter', 'post_determine_locale') );
235 }
236 }
237 }
238
239 //Notice: scriptを使うと AMP でエラーとなるので target でオープン/クローズを行う
240 static function plf_filtered_result_dialog( $text ) {
241 ?>
242 <style>
243 #wpadminbar #wp-admin-bar-plf-statlink .ab-icon:before { content: '\f106'; top: 2px;}
244 #plf-status { display:none;}
245 #plf-status:target { display:block;}
246 #plf-status .dialog-overlay{ position:fixed; left:0px; top:0px; width:100%; height:100%; background-color:rgba(0,0,0,.5); z-index:999999999;}
247 #plf-status div.dialog { position:fixed; top:50%; left:50%; transform:translate(-50%, -50%); width:66%; max-width:420px; height:auto; padding:1.5em; color:#222; background:#fff; z-index:999999999;}
248 #plf-status .dialog-title { margin: 0 0 1em; font-size: 16px;}
249 #plf-status textarea { font-size: 13px;}
250 #plf-status .button-group { display:block; margin-top:2em; text-align:right; font-size:1em;}
251 #plf-status a.button { display:inline-block; text-decoration:none; margin: 0 0 0 1em; padding: .5em 1em; color: #0071a1; background: #f5f5f5; border:solid 1px #0071a1; border-radius:3px; cursor:pointer;}
252 #plf-status a.button:hover { background: #e8ffff; border-color: #016087; color: #016087;}
253 </style>
254 <div id="plf-status">
255 <div class="dialog-overlay"></div>
256 <div class="dialog">
257 <p class="dialog-title"><strong><?php echo 'Plugin Load Filter status'; ?></strong></p>
258 <div><textarea id="plf-filtered-result" style="width:100%; height:320px; margin:5px 0;"><?php echo $text; ?></textarea></div>
259 <div class="button-group">
260 <a href="#" id="plf-status-close" class="button" aria-label="Close modal">Close</a>
261 </div>
262 </div>
263 </div>
264 <?php
265 }
266
267 //filtered plugins list print
268 static function result_plugin_print( $e_plugins, $d_plugins ) {
269 $text = PHP_EOL . "[Activate Plugins]" . PHP_EOL;
270 $idx = 1;
271 foreach ($e_plugins as $key) {
272 if(!empty($key)){
273 $key = preg_replace('|/.+?\.php|', '', $key);
274 $text .= "$idx. $key" . PHP_EOL;
275 $idx++;
276 }
277 }
278 $text .= PHP_EOL . "[Deactivate Plugins]" . PHP_EOL;
279 $idx = 1;
280 foreach ($d_plugins as $key) {
281 if(!empty($key)){
282 $key = preg_replace('|/.+?\.php|', '', $key);
283 $text .= "$idx. $key" . PHP_EOL;
284 $idx++;
285 }
286 }
287 return $text;
288 }
289
290 public function custom_bar_menus($wp_admin_bar) {
291 if (current_user_can( 'activate_plugins' )) {
292 $base_plugins = self::get_base_plugins();
293 if(!empty($base_plugins)){
294 $text = '==== Plugin Load Filter status ====' . PHP_EOL;
295 $is_filterkey = self::is_filterkey();
296 if($is_filterkey !== false){
297 $text .= '[Filter] ' . $is_filterkey . PHP_EOL;
298 $e_plugins = self::get_filtered_plugins();
299 $d_plugins = array_diff( $base_plugins, $e_plugins );
300 } else {
301 $text .= '[Filter] Not Used' . PHP_EOL;
302 $e_plugins = $base_plugins;
303 $d_plugins = array();
304 }
305 ksort($e_plugins, SORT_STRING);
306 ksort($d_plugins, SORT_STRING);
307
308 $text .= self::result_plugin_print( $e_plugins, $d_plugins );
309 self::plf_filtered_result_dialog( $text );
310 $wp_admin_bar->add_menu( array(
311 'id' => 'plf-statlink',
312 'title' => '<span class="ab-icon"></span>PLF',
313 'href' => '#plf-status',
314 ));
315 }
316 }
317 }
318
319 //active_sitewide plugins Filter add ver2.4.0
320 static function active_sitewide_plugins( $default = false) {
321 return self::plf_filter( 'active_sitewide_plugins', $default);
322 }
323
324 //active plugins Filter
325 static function active_plugins( $default = false) {
326 return self::plf_filter( 'active_plugins', $default);
327 }
328
329 //Jetpack module Filter
330 static function active_jetmodules( $default = false) {
331 return self::plf_filter( 'jetpack_active_modules', $default);
332 }
333
334 //Celtispack module Filter
335 static function active_celtismodules( $default = false) {
336 return self::plf_filter( 'celtispack_active_modules', $default);
337 }
338
339 function updated_plf_stat() {
340 $default = array(
341 'post_type_query_vars' => array(),
342 'queryable_post_types' => array(),
343 //'wp_post_statuses' => array(),
344 //'wp_post_types' => array(),
345 //'wp_taxonomies' => array(),
346 'stat_change' => false,
347 );
348 $data = get_option('plf_queryvars', array());
349 $data = wp_parse_args( (array) $data, $default);
350 //plugin bulk action repeated call
351 if(empty($data['stat_change'])){
352 $data['stat_change'] = true;
353 update_option('plf_queryvars', $data, 'no');
354 }
355 }
356
357 //Plugin/Module activate or deactivate stat change
358 function update_active_sitewide_plugins( $option, $value, $old_value, $network_id ) {
359 $this->updated_plf_stat();
360 }
361 function update_active_plugins( $old_value, $value, $option ) {
362 $this->updated_plf_stat();
363 }
364 function update_active_jetmodules( $old_value, $value, $option ) {
365 $this->updated_plf_stat();
366 }
367 function update_active_celtismodules( $old_value, $value, $option ) {
368 $this->updated_plf_stat();
369 }
370 function switch_theme() {
371 $this->updated_plf_stat();
372 }
373
374 //Make taxonomies and posts available to 'plugin load filter'.
375 //force register_taxonomy (category, post_tag, post_format)
376 static function force_initial_taxonomies(){
377 global $wp_actions;
378 $wp_actions[ 'init' ] = 1;
379 create_initial_taxonomies();
380 create_initial_post_types();
381 unset($wp_actions[ 'init' ]);
382 }
383
384 //all plugins loaded
385 function plugins_loaded() {
386 //ver4.0.5 カスタムポストタイプのキャッシュデータを $wp_post_types グローバル変数には反映しないよう変更したのでこの処理は不要となるが、なんらかの影響があると嫌なのでとりあえず残しておく
387 // woocommerce が init 後に register_post_type, register_taxonomy が実行されたか判定しているので該当データを一旦クリアして init で再登録させる
388 if(post_type_exists( 'product' )){
389 if(method_exists('WC_Post_Types', 'register_post_types')){
390 unregister_post_type( 'product' );
391 //ver4.0.2 wc_register_order_type で登録されるタイプも一旦クリアする
392 if(post_type_exists( 'shop_order' )){
393 unregister_post_type( 'shop_order' );
394 }
395 if(post_type_exists( 'shop_order_refund' )){
396 unregister_post_type( 'shop_order_refund' );
397 }
398 }
399 }
400 if ( taxonomy_exists( 'product_type' ) ) {
401 if(method_exists('WC_Post_Types', 'register_taxonomies')){
402 unregister_taxonomy( 'product_type' );
403 }
404 }
405 }
406
407 //Post Format Type, Custom Post Type Data Cache for parse request
408 function cache_post_type() {
409 if (!is_admin() || self::$is_filterkey !== false || ( defined('DOING_AJAX') && DOING_AJAX ))
410 return;
411
412 $default = array(
413 'post_type_query_vars' => array(),
414 'queryable_post_types' => array(),
415 //'wp_post_statuses' => array(),
416 //'wp_post_types' => array(),
417 //'wp_taxonomies' => array(),
418 'stat_change' => false,
419 );
420 $data = get_option('plf_queryvars', array());
421 $data = wp_parse_args( (array) $data, $default);
422
423 global $plugin_page;
424 if(isset($plugin_page) && $plugin_page === 'plugin_load_filter_admin_manage_page'){
425 $data['stat_change'] = true;
426 }
427
428 //init or plugin activate or deactivate stat change
429 if(!empty($data['stat_change'])){
430 global $wp;
431 $public_query_vars = (!empty($wp->public_query_vars))? $wp->public_query_vars : array();;
432 $post_type_query_vars = array();
433 foreach ( get_post_types( array(), 'objects' ) as $post_type => $t ){
434 if (!empty($t) && $t->query_var )
435 $post_type_query_vars[$t->query_var] = $post_type;
436 }
437 $queryable_post_types = get_post_types( array('publicly_queryable' => true) );
438
439 //global $wp_post_types;
440 //global $wp_post_statuses;
441 //global $wp_taxonomies;
442 //$data['wp_post_types'] = $wp_post_types;
443 //$data['wp_post_statuses'] = $wp_post_statuses;
444 //$data['wp_taxonomies'] = $wp_taxonomies;
445 //$data['public_query_vars'] = $public_query_vars;
446 //ver4.0.11 使わないデータを取り除きオプションデータを縮小化
447 if(!empty($data['rewrite_rules'])){
448 unset($data['rewrite_rules']); //データの残骸があれば使わないので取り除く
449 }
450 if(!empty($data['wp_post_types'])){
451 unset($data['wp_post_types']);
452 }
453 if(!empty($data['wp_post_statuses'])){
454 unset($data['wp_post_statuses']);
455 }
456 if(!empty($data['wp_taxonomies'])){
457 unset($data['wp_taxonomies']);
458 }
459 if(!empty($data['public_query_vars'])){
460 unset($data['public_query_vars']);
461 }
462 $add_query_vars = array_diff( $public_query_vars, self::$base_public_query_vars );
463 $data['add_public_query_vars']= $add_query_vars;
464 $data['post_type_query_vars'] = $post_type_query_vars;
465 $data['queryable_post_types'] = $queryable_post_types;
466 $data['stat_change'] = false;
467 update_option('plf_queryvars', $data, 'no');
468 }
469 }
470
471 /**
472 * Filters the locale for the current request.
473 *
474 * @param string $locale The locale.
475 */
476 static function post_locale( $locale ) {
477 if(empty(self::$rlocale)) {
478 $pid = 0;
479 if(isset($_SERVER['REQUEST_URI'])){
480 if ( strpos( $_SERVER['REQUEST_URI'], 'wp-json' ) !== false || preg_match( '/(admin|wc)-ajax/', $_SERVER['REQUEST_URI'])) {
481 $refurl = wp_get_raw_referer();
482 if(!empty( $refurl )){
483 if(preg_match( '/post=([0-9]+)?/', $refurl, $match )){
484 $pid = (int)$match[1];
485 } elseif(preg_match( '/post_ID=([0-9]+)?/', $refurl, $match )){
486 $pid = (int)$match[1];
487 } else {
488 $pid = (isset(self::$url2id[$refurl]))? self::$url2id[$refurl] : url_to_postid( $refurl );
489 self::$url2id[$refurl] = (int)$pid;
490 }
491 }
492 } elseif ( strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) !== false) {
493 if ( isset( $_GET['post'] ) ) {
494 $pid = (int) $_GET['post'];
495 } elseif ( isset( $_POST['post_ID'] ) ) {
496 $pid = (int) $_POST['post_ID'];
497 }
498 } else {
499 global $wp_query;
500 if(!empty(self::$pre_post_id)){
501 $pid = self::$pre_post_id;
502 } elseif(!empty($wp_query)){
503 if(!empty($wp_query->post) && !empty($wp_query->post->ID)){
504 $pid = $wp_query->post->ID;
505 } else {
506 $refurl = wp_get_raw_referer();
507 if (!empty( $refurl )) {
508 $pid = (isset(self::$url2id[$refurl]))? self::$url2id[$refurl] : url_to_postid( $refurl );
509 self::$url2id[$refurl] = (int)$pid;
510 }
511 }
512 }
513 }
514 }
515 if(!empty($pid)){
516 $c_locale = get_post_meta( $pid, '_locale', true );
517 if(!empty($c_locale)){
518 self::$rlocale = $c_locale;
519 }
520 }
521 }
522 if(!empty(self::$rlocale)) {
523 $locale = self::$rlocale;
524 }
525 return $locale;
526 }
527
528 static function post_determine_locale( $locale ) {
529 if(!empty(self::$rlocale)) {
530 $locale = self::$rlocale;
531 }
532 return $locale;
533 }
534
535 //This filter hook for when the post ID cannot be detected from the singler URL due to using a permalink change plugin etc.
536 //Permalink Manger plugin (issue from Shawn X.)
537 //Custom Permalinks plugin
538 static function custom_url_to_postid($post_id, $url_path, $pre_active_plugins) {
539 if(empty($post_id) && !empty($pre_active_plugins)){
540 foreach ($pre_active_plugins as $key) {
541 if ( strpos($key, 'permalink-manager' ) !== false){
542 $permalink_manager_uris = (array)get_option('permalink-manager-uris', array());
543 if(empty($permalink_manager_uris)){
544 break;
545 } else {
546 foreach ($permalink_manager_uris as $pid => $slug) {
547 if(preg_match("#/{$slug}(/?$)#ui", $url_path)){
548 $post_id = $pid;
549 break 2;
550 }
551 }
552 }
553 } else if ( strpos($key, 'custom-permalinks' ) !== false){
554 $url = wp_parse_url( get_bloginfo( 'url' ) );
555 $url = isset( $url['path'] ) ? $url['path'] : '';
556 $meta_val = ltrim( substr( $url_path, strlen( $url ) ), '/' );
557
558 global $wpdb;
559 $posts = $wpdb->get_results(
560 $wpdb->prepare(
561 'SELECT p.ID, pm.meta_value, p.post_type, p.post_status ' .
562 " FROM $wpdb->posts AS p INNER JOIN $wpdb->postmeta AS pm ON (pm.post_id = p.ID) " .
563 " WHERE pm.meta_key = 'custom_permalink' " .
564 ' AND (pm.meta_value = %s OR pm.meta_value = %s) ' .
565 " AND p.post_status IN ('publish', 'private', 'inherit') " .
566 " AND p.post_type != 'nav_menu_item' " . // nav_menu_item を除外、他の post_type を許可
567 " ORDER BY FIELD(p.post_status,'publish','private','inherit')," .
568 " p.post_type LIMIT 1",
569 $meta_val,
570 $meta_val . '/'
571 )
572 );
573 if(!empty($posts[0]->ID)) {
574 $post_id = (int)$posts[0]->ID;
575 }
576 break;
577 }
578 }
579 }
580 return $post_id;
581 }
582
583 //プラグインロード前は bbPress等 カスタムポストタイプのデバッグモードでのエラー表示抑制
584 static function exclude_trigger_error( $trigger, $function, $message, $version) {
585 if (did_action( 'plugins_loaded' ) === 0) {
586 if($function === 'map_meta_cap'){
587 $trigger = false;
588 }
589 }
590 return $trigger;
591 }
592 //プラグインロード前は closed 等のカスタムステータスがまだ登録されてない状�
593 �でも取得 posts �
594 報がクリアされないよう抑制
595 static function exclude_map_meta_cap( $caps, $cap, $user_id, $args) {
596 if (did_action( 'plugins_loaded' ) === 0) {
597 if(!empty($caps) && $caps[0] === 'edit_others_posts'){
598 $caps = array();
599 }
600 }
601 return $caps;
602 }
603
604 //parse_request Action Hook for Custom Post Type query add
605 static function parse_request( &$args ) {
606 if (did_action( 'plugins_loaded' ) === 0) {
607 $data = get_option('plf_queryvars', array());
608 if(!empty($data['post_type_query_vars']) && !empty($data['queryable_post_types']) && empty($data['stat_change'])){
609 //ver4.0.5 グローバルのカスタムポストタイプに事前設定するとプラグイン�
610 での登録済みチェック処理とコンフリクトするので止める
611 //global $wp_post_statuses;
612 //global $wp_post_types;
613 //global $wp_taxonomies;
614 //$wp_post_statuses = $data['wp_post_statuses'];
615 //$wp_post_types = $data['wp_post_types'];
616 //$wp_taxonomies = $data['wp_taxonomies'];
617
618 $post_type_query_vars = $data['post_type_query_vars'];
619 $queryable_post_types = $data['queryable_post_types'];
620
621 //query_vars に query_posts() で実行するSQL用のポストタイプデータをセット
622 if(isset($data['add_public_query_vars'])){
623 //ver4.0.11 差分データにしたのでここでマージするが更新時にエラーとならないよう旧処理も残しておく
624 $args->public_query_vars = array_merge(self::$base_public_query_vars, $data['add_public_query_vars']);
625 } else {
626 $args->public_query_vars = $data['public_query_vars'];
627 }
628 if ( isset( $args->matched_query ) ) {
629 parse_str($args->matched_query, $perma_query_vars);
630 }
631
632 foreach ( $args->public_query_vars as $wpvar ) {
633 if ( isset( $args->extra_query_vars[$wpvar] ) ){
634 $args->query_vars[$wpvar] = $args->extra_query_vars[$wpvar];
635 } elseif ( isset( $_GET[ $wpvar ] ) && isset( $_POST[ $wpvar ] ) && $_GET[ $wpvar ] !== $_POST[ $wpvar ] ) {
636 wp_die( 'A variable mismatch has been detected.', 'Sorry, you are not allowed to view this item.', 400 );
637 } elseif ( isset( $_POST[$wpvar] ) ){
638 $args->query_vars[$wpvar] = $_POST[$wpvar];
639 } elseif ( isset( $_GET[$wpvar] ) ){
640 $args->query_vars[$wpvar] = $_GET[$wpvar];
641 } elseif ( isset( $perma_query_vars[$wpvar] ) ){
642 $args->query_vars[$wpvar] = $perma_query_vars[$wpvar];
643 }
644 if ( !empty( $args->query_vars[$wpvar] ) ) {
645 if ( ! is_array( $args->query_vars[$wpvar] ) ) {
646 $args->query_vars[$wpvar] = (string) $args->query_vars[$wpvar];
647 } else {
648 foreach ( $args->query_vars[$wpvar] as $vkey => $v ) {
649 if ( is_scalar( $v ) ) {
650 $args->query_vars[$wpvar][$vkey] = (string) $v;
651 }
652 }
653 }
654
655 if ( isset($post_type_query_vars[$wpvar] ) ) {
656 $args->query_vars['post_type'] = $post_type_query_vars[$wpvar];
657 $args->query_vars['name'] = $args->query_vars[$wpvar];
658 }
659 }
660 }
661
662 // Limit publicly queried post_types to those that are 'publicly_queryable'.
663 if ( isset( $args->query_vars['post_type']) ) {
664 if ( ! is_array( $args->query_vars['post_type'] ) ) {
665 if ( ! in_array( $args->query_vars['post_type'], $queryable_post_types, true ) ) {
666 unset( $args->query_vars['post_type'] );
667 }
668 } else {
669 $args->query_vars['post_type'] = array_intersect( $args->query_vars['post_type'], $queryable_post_types );
670 }
671 }
672 }
673 }
674 }
675
676 //get filter name
677 static function is_filterkey() {
678 return self::$is_filterkey;
679 }
680 //base plugins (initial activated plugins & module)
681 static function get_base_plugins() {
682 return self::$base_plugins;
683 }
684 //filtered plugins (plugins & module)
685 static function get_filtered_plugins() {
686 return self::$filtered_plugins;
687 }
688
689 /**
690 * Get URL Filter (for addon optional)
691 * @param $stat : 'active' / 'deactive' / '' = all
692 * @param $urltype : 'front' / 'admin' / '' = all
693 * @param $device : 'desktop' / 'mobile' / '' = all
694 * @return url filter data array.
695 */
696 static function get_url_filter( $stat='', $urltype='', $device='' ) {
697 $frontlist = array();
698 $adminlist = array();
699 if(!empty(self::$url_filter)){
700 foreach (self::$url_filter['filter'] as $slug => $v) {
701 if(empty($stat) || (!empty($v['stat']) && $stat === 'active') || (empty($v['stat']) && $stat === 'deactive')){
702 if(empty($device) || (!empty($v['desktop']) && $device === 'desktop') || (!empty($v['mobile']) && $device === 'mobile')){
703 if(!empty($v['type'])){
704 $item = "{$v['group']}-{$v['slug']}";
705 if($v['type'] == 'front'){
706 $frontlist[$item] = $v;
707 } elseif($v['type'] == 'admin'){
708 $adminlist[$item] = $v;
709 }
710 }
711 }
712 }
713 }
714 //group 毎に slug をソート A-Za-z 順となる
715 ksort($frontlist, SORT_STRING);
716 ksort($adminlist, SORT_STRING);
717 }
718 $list = array();
719 if(empty($urltype) || $urltype === 'front'){
720 foreach ($frontlist as $item => $v) {
721 if(!empty($v)){
722 $list[] = $v;
723 }
724 }
725 }
726 if(empty($urltype) || $urltype === 'admin'){
727 foreach ($adminlist as $item => $v) {
728 if(!empty($v)){
729 $list[] = $v;
730 }
731 }
732 }
733 return $list;
734 }
735 //active group list
736 static function get_active_group() {
737 $grouplist = array();
738 $sluglist = self::get_url_filter( 'active' );
739 foreach ($sluglist as $v) {
740 if(!empty($v['group']) && !in_array($v['group'], $grouplist)){
741 $grouplist[] = $v['group'];
742 }
743 }
744 return $grouplist;
745 }
746 //active slug filter data in group
747 static function get_slug_filter( $group ) {
748 $list = array();
749 $sluglist = self::get_url_filter( 'active' );
750 foreach ($sluglist as $v) {
751 if(!empty($v['group']) && $v['group'] == $group){
752 $list[] = $v;
753 }
754 }
755 return $list;
756 }
757
758 //Is there a term in taxonomies
759 static function is_term_in_taxonomy( $post_id, $term, $taxonomies) {
760 $ret = false;
761 $txs = explode(',', $taxonomies);
762 foreach ( $txs as $slug ) {
763 if(!empty($slug)){
764 $names = '';
765 $terms = get_the_terms( $post_id, $slug );
766 if (!empty($terms) ) {
767 foreach ( $terms as $tobj ) {
768 if(!empty($tobj)){
769 $names .= $tobj->name . ',';
770 }
771 }
772 if(strpos($names, $term ) !== false){
773 $ret = true;
774 break;
775 }
776 }
777 }
778 }
779 return $ret;
780 }
781
782 // url path, query parameter match check
783 static function is_url_match( $req_url, $parse_url, $filter ) {
784 if(empty($req_url) || empty($parse_url['path']))
785 return false;
786 $_url['url_path'] = $parse_url['path'];
787 $_url['url_q_and'] = $_url['url_q_not'] = (!empty($parse_url['query']))? $parse_url['query'] : '';
788
789 $match = array();
790 $keynum = array();
791 foreach (array('url_path', 'url_q_and', 'url_q_not') as $item) {
792 $match[$item] = 0;
793 $keynum[$item] = 0;
794 if($item === 'url_path'){
795 $reg_before = "(/?)";
796 $reg_after = "(/?$)";
797 } else {
798 $reg_before = "(^|&)";
799 $reg_after = "(=|&|$)";
800 }
801
802 $keylist = (!empty($filter[$item])) ? $filter[$item] : '';
803 $keylist = str_replace( "*", ".+?", $keylist);
804 $ar_key = (!empty($keylist))? array_filter( array_map("trim", explode(PHP_EOL, $keylist))) : array();
805 $keynum[$item] = count($ar_key);
806 if(empty($ar_key)) {
807 if($item === 'url_path')
808 $match[$item] += 1;
809 } else {
810 $ar_new = array();
811 foreach ($ar_key as $key) {
812 //v4.0.6 home (/) のみの指定は別判定しないとトレイルスラッシュで終わる�
813 �てのURLにマッチしてしまう
814 if($item === 'url_path' && $key === '/'){
815 if($_url['url_path'] === '/'){
816 $match[$item] += 1;
817 }
818 } elseif(preg_match("#{$reg_before}{$key}{$reg_after}#ui", $_url[$item])){
819 $match[$item] += 1;
820 }
821 }
822 }
823 }
824 $group = false;
825 $hit = ($match['url_path'] > 0 && $match['url_q_and'] === $keynum['url_q_and'] && $match['url_q_not'] === 0)? true : false;
826 if($hit){
827 if((strpos($_url['url_path'], 'admin-ajax.php' ) !== false || strpos($_url['url_q_and'], 'wc-ajax' ) !== false) && (int)$filter['targetpage'] === 2){
828 $action = '';
829 if(isset($_REQUEST['action'])){
830 $action = esc_attr($_REQUEST['action']);
831 } elseif(!empty( $_GET['wc-ajax'] )) {
832 $action = sanitize_text_field( wp_unslash( $_GET['wc-ajax'] ) );
833 }
834 if(!empty($filter['ajax_action'])){
835 if( $action == $filter['ajax_action'] ){
836 $group = $filter['group'];
837 }
838 } else {
839 //exclude special action : plugin_load_filter, plf_urlfilter_test
840 if (! in_array( $action, array('plugin_load_filter', 'plf_urlfilter_test')) ){
841 $group = $filter['group'];
842 }
843 }
844 } elseif(strpos($_url['url_path'], 'post-new.php' ) === false && (int)$filter['targetpage'] === 1){
845 $post_id = 0;
846 if ( isset( $_GET['post'] ) && isset( $_POST['post_ID'] ) && (int) $_GET['post'] !== (int) $_POST['post_ID'] ) {
847 } elseif ( isset( $_GET['post'] ) ) {
848 $post_id = (int) $_GET['post'];
849 } elseif ( isset( $_POST['post_ID'] ) ) {
850 $post_id = (int) $_POST['post_ID'];
851 }
852 if(empty($post_id)){
853 //クエリーパラメータ形式だとポストIDが取得できないようなので、シュミレータ以外なら $wp_query->post->ID を使う
854 global $wp_query;
855 if(!empty($wp_query)){
856 if (isset($_REQUEST['action']) && isset($_POST['test_url']) ) {
857 $post_id = (isset(self::$url2id[$req_url]))? self::$url2id[$req_url] : url_to_postid( $req_url );
858 self::$url2id[$req_url] = (int)$post_id;
859 } else {
860 if(!empty($wp_query->post) && !empty($wp_query->post->ID)){
861 $post_id = $wp_query->post->ID;
862 } else {
863 $post_id = (isset(self::$url2id[$req_url]))? self::$url2id[$req_url] : url_to_postid( $req_url );
864 self::$url2id[$req_url] = (int)$post_id;
865 }
866 }
867 }
868 $post_id = apply_filters('plf_singler_custom_url_to_postid', $post_id, $parse_url['path'], self::$base_plugins);
869 if(!empty($post_id)){
870 if(empty($wp_query->post)){
871 $r = new WP_Query( array( 'p' => $post_id, 'post_type' => 'any' ) );
872 if ($r->have_posts()) {
873 if(!empty($r->post)){
874 $wp_query->posts = $r->posts;
875 $wp_query->post = $r->post;
876 }
877 }
878 }
879 self::$url2id[$req_url] = (int)$post_id;
880 }
881 }
882 if(!empty($post_id)){
883 $post = get_post( $post_id );
884 if ( is_object($post) && !empty($post->post_type)) {
885 if(!empty($filter['post_type'])){
886 if(preg_match("#{$post->post_type}#", $filter['post_type'])){
887 if(!empty($filter['taxonomies']) && !empty($filter['term'])){
888 if(self::is_term_in_taxonomy( $post_id, $filter['term'], $filter['taxonomies'])){
889 $group = $filter['group'];
890 }
891 } else {
892 $group = $filter['group'];
893 }
894 }
895 } else {
896 if(!empty($filter['taxonomies']) && !empty($filter['term'])){
897 if(self::is_term_in_taxonomy( $post_id, $filter['term'], $filter['taxonomies'])){
898 $group = $filter['group'];
899 }
900 } else {
901 $group = $filter['group'];
902 }
903 }
904 }
905 }
906 } else {
907 $group = $filter['group'];
908 }
909 }
910 return($group);
911 }
912
913 static function plugin_keygen( $fname, $option ) {
914 $key = false;
915 if($option === 'jetpack_active_modules'){
916 $key = 'jetpack_module/' . $fname;
917 } elseif($option === 'celtispack_active_modules'){
918 $key = 'celtispack_module/' . str_replace( '.php', '', basename( $fname ) );
919 } else {
920 $key = $fname;
921 }
922 return $key;
923 }
924
925 /**
926 * Get valid plugins list for groupkey (for addon optional)
927 * @param $groupkey : url filter group name
928 * @param $filter : plf filtering setting data
929 * @param $option : option data eg. 'active_plugins', 'active_sitewide_plugins', 'jetpack_active_modules' ...
930 * @param $plugins : active plugins values before filtering
931 * @return data values after filtering
932 */
933 static function filter_to_active_plugins( $groupkey, $filter, $option, $plugins ){
934 $new_plugins = array();
935 foreach ( $plugins as $item ) {
936 if(!empty($item)){
937 $unload = false;
938 $p_key = self::plugin_keygen( $item, $option );
939 if(!empty($filter['plfurlkey'][$groupkey]['plugins'])){
940 if(false !== strpos($filter['plfurlkey'][$groupkey]['plugins'], $p_key))
941 $unload = true;
942 }
943 if(!$unload) {
944 if($option === 'active_sitewide_plugins'){
945 // v4.0.6 $new_plugins[$item] = $plugins[$item];
946 $new_plugins[$item] = $item;
947 } else {
948 $new_plugins[] = $item;
949 }
950 }
951 }
952 }
953
954 $new_plugins = apply_filters('plf_custom_changes_to_active_plugins', $new_plugins);
955 return $new_plugins;
956 }
957
958 /**
959 * Get valid plugins list for ajax acceleration filter
960 * @param $p_slugs : ajax _ajax_plf activate plugins slug data (Separate multiple with commas)
961 * @param $option : option data eg. 'active_plugins', 'active_sitewide_plugins', 'jetpack_active_modules' ...
962 * @param $plugins : active plugins values before filtering
963 * @return data values after filtering
964 */
965 static function ajaxfilter_to_active_plugins( $p_slugs, $option, $plugins ){
966 //�
967 �合用にカンマ区切りをスラッシュ区切りへ変換
968 $arslugs = array_filter( array_map("trim", explode(',', $p_slugs)));
969 $slugs = '/' . implode('/', $arslugs) . '/';
970
971 $new_plugins = array();
972 foreach ( $plugins as $item ) {
973 if(!empty($item)){
974 $unload = false;
975 $p_key = self::plugin_keygen( $item, $option );
976 $sep = strpos($p_key, '/' );
977 $p_slug = ($sep !== false)? substr($p_key, 0, $sep) : $p_key;
978 if(false === strpos($slugs, "/$p_slug/")){
979 $unload = true;
980 }
981 if(!$unload) {
982 if($option === 'active_sitewide_plugins'){
983 $new_plugins[$item] = $item;
984 } else {
985 $new_plugins[] = $item;
986 }
987 }
988 }
989 }
990 return $new_plugins;
991 }
992
993 //Plugin Load Filter Main (active plugins/modules filtering)
994 static function plf_filter( $option, $default = false) {
995 if ( defined( 'WP_SETUP_CONFIG' ) || did_action( 'mu_plugin_loaded' ) === 0)
996 return false;
997
998 //Check if the caller is wp-settings.php wp_get_active_network_plugins() / wp_get_active_and_valid_plugins()
999 if( in_array($option, array('active_plugins', 'active_sitewide_plugins' ))){
1000 $is_caller_target = false;
1001 $trace = debug_backtrace();
1002 foreach ($trace as $stp) {
1003 if(isset($stp['file']) && strpos($stp['file'], 'wp-settings.php') !== false){
1004 if(isset($stp['function']) && in_array($stp['function'], array('wp_get_active_network_plugins', 'wp_get_active_and_valid_plugins'))){
1005 $is_caller_target = true;
1006 break;
1007 }
1008 }
1009 }
1010 if( !$is_caller_target ) {
1011 return false;
1012 }
1013 }
1014
1015 if ( ! defined( 'WP_INSTALLING' ) ) {
1016 global $wpdb, $current_site;
1017 if ( is_multisite() && $option === 'active_sitewide_plugins' ) {
1018 //get_network_option() current site ID
1019 $network_id = $current_site->id;
1020
1021 // prevent non-existent options from triggering multiple queries
1022 $notoptions_key = "$network_id:notoptions";
1023 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1024 if ( isset( $notoptions[ $option ] ) ) {
1025 return apply_filters( 'default_site_option_' . $option, $default, $option );
1026 }
1027
1028 $cache_key = "$network_id:$option";
1029 $opt_value = wp_cache_get( $cache_key, 'site-options' );
1030
1031 if ( ! isset( $opt_value ) || false === $opt_value ) {
1032 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
1033
1034 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
1035 if ( is_object( $row ) ) {
1036 $opt_value = $row->meta_value;
1037 $opt_value = maybe_unserialize( $opt_value );
1038 wp_cache_set( $cache_key, $opt_value, 'site-options' );
1039 } else {
1040 if ( ! is_array( $notoptions ) ) {
1041 $notoptions = array();
1042 }
1043 $notoptions[ $option ] = true;
1044 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1045
1046 /** This filter is documented in wp-includes/option.php */
1047 $opt_value = apply_filters( 'default_site_option_' . $option, $default, $option );
1048 }
1049 }
1050 } else {
1051 // prevent non-existent options from triggering multiple queries
1052 $notoptions = wp_cache_get( 'notoptions', 'options' );
1053 if ( isset( $notoptions[$option] ) )
1054 return apply_filters( 'default_option_' . $option, $default );
1055
1056 $alloptions = wp_load_alloptions();
1057 if ( isset( $alloptions[$option] ) ) {
1058 $opt_value = $alloptions[$option];
1059 } else {
1060 $opt_value = wp_cache_get( $option, 'options' );
1061
1062 if ( false === $opt_value ) {
1063 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
1064
1065 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
1066 if ( is_object( $row ) ) {
1067 $opt_value = $row->option_value;
1068 wp_cache_add( $option, $opt_value, 'options' );
1069 } else { // option does not exist, so we must cache its non-existence
1070 if ( ! is_array( $notoptions ) ) {
1071 $notoptions = array();
1072 }
1073 $notoptions[$option] = true;
1074 wp_cache_set( 'notoptions', $notoptions, 'options' );
1075
1076 /** This filter is documented in wp-includes/option.php */
1077 return apply_filters( 'default_option_' . $option, $default, $option );
1078 }
1079 }
1080 }
1081 }
1082 } else {
1083 return false;
1084 }
1085
1086 $req_url = (isset($_SERVER['REQUEST_URI']))? $_SERVER['REQUEST_URI'] : '';
1087 $req_url = str_replace( "\\", "/", $req_url);
1088 $parse_url = parse_url($req_url);
1089 $action = (!empty($parse_url['path']) && strpos($parse_url['path'], 'admin-ajax.php' ) !== false && isset($_REQUEST['action']))? esc_attr($_REQUEST['action']) : '';
1090
1091 $act_plugins = ($option === 'active_sitewide_plugins')? array_keys( (array)$opt_value ) : maybe_unserialize( $opt_value );
1092 if($option === 'celtispack_active_modules'){
1093 if(empty(self::$base_plugins['celtispack-addon/celtispack-addon.php'])){
1094 $opt = get_option( 'celtis_addon_options', array() );
1095 if(!empty($opt['active_modules'])){
1096 $nact_plugins = array();
1097 foreach ($act_plugins as $pkey) {
1098 if(!empty($pkey)){
1099 $key = str_replace( '.php', '', basename( $pkey ));
1100 if(!empty($opt['active_modules'][$key]))
1101 continue;
1102 $nact_plugins[] = $pkey;
1103 }
1104 }
1105 $act_plugins = $nact_plugins;
1106 }
1107 }
1108 }
1109
1110 //Equal treatment for when the wp_is_mobile is not yet available(wp-include/vars.php wp_is_mobile)
1111 if ( isset( $_SERVER['HTTP_SEC_CH_UA_MOBILE'] ) ) {
1112 // This is the `Sec-CH-UA-Mobile` user agent client hint HTTP request header.
1113 // See <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Mobile>.
1114 $is_mobile = ( '?1' === $_SERVER['HTTP_SEC_CH_UA_MOBILE'] );
1115 } elseif ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1116 $is_mobile = false;
1117 } elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Mobile' ) // Many mobile devices (all iPhone, iPad, etc.)
1118 || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Android' )
1119 || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Silk/' )
1120 || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Kindle' )
1121 || str_contains( $_SERVER['HTTP_USER_AGENT'], 'BlackBerry' )
1122 || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Opera Mini' )
1123 || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Opera Mobi' ) ) {
1124 $is_mobile = true;
1125 } else {
1126 $is_mobile = false;
1127 }
1128 $is_mobile = apply_filters( 'custom_is_mobile' , $is_mobile );
1129
1130 //get_option is called many times, intermediate processing data to cache
1131 $keyid = md5('plf_'. (string)$is_mobile . $req_url . $action);
1132 if(!empty(self::$cache[$keyid][$option])){
1133 return self::$cache[$keyid][$option];
1134 }
1135
1136 //Before plugins loaded, it does not use conditional branch such as is_home, to set wp_query, wp in temporary query
1137 if(empty($GLOBALS['wp_the_query'])){
1138 // プラグイン無効化時等で rewrite_rule がクリアされると rewrite_rule が更新されるまでカスタムポストタイプを判定できずにカスタムポストタイプのページはホームへ飛ばされる
1139 // 但し、permlink structure が plain(基本) の場合は除く
1140 if(!empty(get_option( 'permalink_structure' ))){
1141 $rewrite_rules = get_option('rewrite_rules');
1142 // rewrite_rule を監視して変更された場合はプラグインのフィルタリングをスキップさせていたが、
1143 // plugin activate / deactivate を監視するように変更したのでここでは空の場合のみチェック
1144 if(empty($rewrite_rules)){
1145 return false;
1146 }
1147 // Welcart usces_itemnew 新規商品追加時のエラー (A variable mismatch has been detected.) parse_request で発生してるので特別に回避
1148 if ( isset( $_GET[ 'page' ] ) && isset( $_POST[ 'page' ] ) && $_GET[ 'page' ] !== $_POST[ 'page' ] ) {
1149 return false;
1150 }
1151 }
1152 $GLOBALS['wp_the_query'] = new WP_Query();
1153 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
1154 $GLOBALS['wp_rewrite'] = new WP_Rewrite();
1155 $GLOBALS['wp'] = new WP();
1156 self::$base_public_query_vars = $GLOBALS['wp']->public_query_vars;
1157 //register_taxonomy(category, post_tag, post_format) support for is_archive
1158 self::force_initial_taxonomies();
1159
1160 if((!empty($parse_url['path']) && (strpos($parse_url['path'], 'admin-ajax.php' ) !== false || strpos($parse_url['path'], '/wp-cron' ) !== false || strpos($parse_url['path'], '/wp-json' ) !== false)) || (!empty($parse_url['query']) && strpos($parse_url['query'], 'rest_route=' ) !== false )){
1161 //ver4.0.15 admin-ajax action:send-attachment-to-editor リクエストが query_posts() 呼び出し時にエラーとなるので ajax, rest api, cron では呼び出さず url filter 処理へ
1162 //https://wordpress.org/support/topic/amin-ajax-php-cant-add-images-in-posts/#post-16938082
1163 } else {
1164 //Post Format, Custom Post Type support
1165 add_action('parse_request', array('Plf_filter', 'parse_request'));
1166 add_filter('doing_it_wrong_trigger_error', array('Plf_filter', 'exclude_trigger_error'), 10, 4 );
1167 //custom parse request filter (for experimental development)
1168 $custom = apply_filters( 'plf_experimental_custom_parse_request', false );
1169 if ( false === $custom) {
1170 $GLOBALS['wp']->parse_request('');
1171 }
1172 $GLOBALS['wp']->query_posts();
1173 }
1174 }
1175
1176 //active plugin data
1177 foreach ($act_plugins as $key) {
1178 if(!empty($key)){
1179 $key = self::plugin_keygen( $key, $option );
1180 self::$base_plugins[$key] = $key;
1181 }
1182 }
1183
1184 $filter = self::$filter;
1185
1186 //plf Addon Extract only target data
1187 $devtype = ($is_mobile)? 'mobile' : 'desktop';
1188 $urltype = (!empty($parse_url['path']) && strpos($parse_url['path'], '/wp-admin' ) !== false)? 'admin' : 'front';
1189 if(!empty(self::$url_filter) && empty(self::$s_url_filter)){
1190 self::$s_url_filter = self::get_url_filter( 'active', $urltype, $devtype );
1191 }
1192
1193
1194 //======== The following are Admin backend page processes ========
1195
1196 if($urltype === 'admin'){
1197 //wp-admin/plugins.php or wp-admin/update-core.php request : Do not filter this request URL
1198 if(strpos($parse_url['path'], '/plugins.php' ) !== false || strpos($parse_url['path'], '/update-core.php' ) !== false){
1199 return false;
1200 }
1201 //Ajax acceleration plugin filter (for plugin developers)
1202 if(!empty($action) && !empty(self::$filter['ajax_accelfilter'])){
1203 $slugs = (isset($_REQUEST['_ajax_plf']))? wp_kses( stripslashes($_REQUEST['_ajax_plf']), 'strip' ) : '';
1204 if(!empty($slugs)){
1205 $referer = (!empty( $_SERVER['HTTP_REFERER'] )) ? wp_kses( stripslashes($_SERVER['HTTP_REFERER']), 'strip' ) : '';
1206 $parse_ref = parse_url($referer);
1207 if(!empty($parse_ref['host']) && strpos( home_url(), $parse_ref['host'] ) !== false){
1208 $new_plugins = self::ajaxfilter_to_active_plugins( $slugs, $option, $act_plugins );
1209 foreach ($new_plugins as $key) {
1210 if(!empty($key)){
1211 $key = self::plugin_keygen( $key, $option );
1212 self::$filtered_plugins[$key] = $key;
1213 }
1214 }
1215 return $new_plugins;
1216 }
1217 }
1218 }
1219 if(!empty(self::$s_url_filter)){
1220 //plf Addon Admin Backend URL filtering
1221 foreach (self::$s_url_filter as $key => $v) {
1222 if(!empty($v)){
1223 $groupkey = self::is_url_match( $req_url, $parse_url, $v );
1224 if($groupkey !== false) {
1225 self::$is_filterkey = 'url-group-filter : ' . $groupkey;
1226 $new_plugins = self::filter_to_active_plugins( $groupkey, $filter, $option, $act_plugins );
1227 self::$cache[$keyid][$option] = $new_plugins;
1228 foreach ($new_plugins as $key) {
1229 if(!empty($key)){
1230 $key = self::plugin_keygen( $key, $option );
1231 self::$filtered_plugins[$key] = $key;
1232 }
1233 }
1234 return $new_plugins;
1235 }
1236 }
1237 }
1238 }
1239 return false;
1240 }
1241
1242
1243 //======== The following are frontend page processes ========
1244
1245 global $wp_query;
1246 $unknown = false;
1247 if( ! is_embed() ){
1248 if((is_home() || is_front_page() || is_archive() || is_search() || is_singular()) == false || (is_home() && !empty($_GET))) {
1249 //bbPress users page requests are treated the same as is_home
1250 //downloadmanager plugin downloadlink request [home]/?wpdmact=XXXXXX exclude home GET query
1251 $unknown = true;
1252
1253 } elseif(is_singular() && empty($wp_query->post)){
1254 //documentroot special php file (wp-login.php, wp-cron.php, etc) これらはこの時点では singular とみなされるが post が設定されていないことで判別
1255 //フィルタリングしたい場合は urlfilter Addon を使用すること
1256 //但し、private (非�
1257 �開)ページ時は post がこの時点ではまだ設定されていないので private でクエリー発行して再確認
1258 if(!empty($parse_url['path']) && strpos( $parse_url['path'], '.php' ) === false && !empty($wp_query->query)) {
1259 //get post for private
1260 $query = $wp_query->query;
1261 $query['post_status'] = 'private';
1262 $r = new WP_Query( $query );
1263 if ($r->have_posts()) {
1264 if(!empty($r->post)){
1265 $wp_query->posts = $r->posts;
1266 $wp_query->post = $r->post;
1267 }
1268 } else {
1269 //カスタムステータスがまだ登録されてない状�
1270 �でも取得 posts �
1271 報がクリアされないよう抑制
1272 add_filter( 'map_meta_cap', array('Plf_filter', 'exclude_map_meta_cap'), 10, 4 );
1273 $query['post_status'] = 'any';
1274 $r = new WP_Query( $query );
1275 if ($r->have_posts()) {
1276 if(!empty($r->post)){
1277 $wp_query->posts = $r->posts;
1278 $wp_query->post = $r->post;
1279 }
1280 }
1281 }
1282 }
1283 if(empty($wp_query->post)){
1284 //パーマリンクをカスタムするプラグイン等により URL から post ID が所得できない場合用のフィルターフック
1285 $post_id = apply_filters('plf_singler_custom_url_to_postid', 0, $parse_url['path'], self::$base_plugins);
1286 if(!empty($post_id)){
1287 $r = new WP_Query( array( 'p' => $post_id, 'post_type' => 'any' ) );
1288 if ($r->have_posts()) {
1289 if(!empty($r->post)){
1290 $wp_query->posts = $r->posts;
1291 $wp_query->post = $r->post;
1292 }
1293 }
1294 }
1295 }
1296 if(empty($wp_query->post)){
1297 $unknown = true;
1298 }
1299 }
1300 }
1301
1302 $single_opt = array();
1303 if(is_singular()){
1304 if(is_object($wp_query->post)){
1305 self::$pre_post_id = $wp_query->post->ID; //for post_locale()
1306 $myfilter = get_post_meta( $wp_query->post->ID, '_plugin_load_filter', true );
1307 $default = array( 'filter' => 'default', 'desktop' => '', 'mobile' => '');
1308 $single_opt = (!empty($myfilter))? $myfilter : $default;
1309 $single_opt = wp_parse_args( $single_opt, $default);
1310 }
1311 }
1312 if(!empty(self::$s_url_filter)){
1313 //Addon frontend URL filtering
1314 //個別フィルター有効なシングラーは URL filtering 無効(個別フィルター優�
1315 �)
1316 if(empty($single_opt) || $single_opt['filter'] === 'default'){
1317 foreach (self::$s_url_filter as $key => $v) {
1318 if(!empty($v)){
1319 $groupkey = self::is_url_match( $req_url, $parse_url, $v );
1320 if($groupkey !== false) {
1321 self::$is_filterkey = 'url-group-filter : ' . $groupkey;
1322 $new_plugins = self::filter_to_active_plugins( $groupkey, $filter, $option, $act_plugins );
1323 self::$cache[$keyid][$option] = $new_plugins;
1324 foreach ($new_plugins as $key) {
1325 if(!empty($key)){
1326 $key = self::plugin_keygen( $key, $option );
1327 self::$filtered_plugins[$key] = $key;
1328 }
1329 }
1330 return $new_plugins;
1331 }
1332 }
1333 }
1334 }
1335 }
1336 if($unknown || empty($parse_url['path']) || strpos($parse_url['path'], '/wp-cron' ) !== false || strpos($parse_url['path'], '/wp-json' ) !== false || (!empty($parse_url['query']) && strpos($parse_url['query'], 'rest_route=' ) !== false )){
1337 //url filter 処理後、REST api, cron, フィルター対象外の不明なリクエストはフィルタリング処理を行わない
1338 return false;
1339 }
1340
1341 //plf standard filtering
1342 $type = false;
1343 if(!empty($filter['_pagefilter']['plugins'])){
1344 if(is_embed()){
1345 $type = 'content-card';
1346 } elseif(is_home() || is_front_page()){
1347 $type = 'home';
1348 } elseif(is_archive()) {
1349 $type = 'archive';
1350 } elseif(is_search()) {
1351 $type = 'search';
1352 } elseif(is_attachment()) {
1353 $type = 'attachment';
1354 } elseif(is_page()) {
1355 $type = 'page';
1356 } elseif(is_single()){ //Post & Custom Post
1357 $type = get_post_type( $wp_query->post);
1358 //bbPress private (非�
1359 �開)ページ時のポストタイプ取得
1360 if($type === false && isset($wp_query->query_vars['post_type'])){
1361 $type = $wp_query->query_vars['post_type'];
1362 }
1363 if($type === 'post'){
1364 $fmt = get_post_format( $wp_query->post);
1365 $type = ($fmt === 'standard' || $fmt == false)? 'post' : "post-$fmt";
1366 }
1367 } else {
1368 $type = 'unknown';
1369 }
1370 self::$is_filterkey = 'page-type-filter : ' . $type;
1371 if(is_singular()){
1372 if(!empty($single_opt) && $single_opt['filter'] === 'include'){
1373 self::$is_filterkey = 'page-type-filter : Single page option';
1374 }
1375 }
1376 } elseif(!empty($filter['_admin']['plugins'])){
1377 //Page Type 未指定時は admin type 除外フィルターのみ
1378 self::$is_filterkey = 'page-type-filter : admin filter';
1379 }
1380
1381 $new_plugins = array();
1382 foreach ( $act_plugins as $item ) {
1383 if(!empty($item)){
1384 $unload = false;
1385 $p_key = self::plugin_keygen( $item, $option );
1386 //admin filter
1387 if(!empty($filter['_admin']['plugins'])){
1388 if(in_array($p_key, array_map("trim", explode(',', $filter['_admin']['plugins'])))){
1389 $unload = true;
1390 }
1391 }
1392 //page filter
1393 if(!$unload){
1394 if(!empty($filter['_pagefilter']['plugins'])){
1395 if(in_array($p_key, array_map("trim", explode(',', $filter['_pagefilter']['plugins'])))){
1396 $unload = true;
1397
1398 //desktop/mobile device disable filter
1399 $dis_dev = true;
1400 if(is_singular() && is_object($wp_query->post)){
1401 if(!empty($single_opt) && $single_opt['filter'] === 'include'){
1402 if(!empty($single_opt[$devtype])){
1403 if(false !== strpos($single_opt[$devtype], $p_key))
1404 $dis_dev = false;
1405 elseif(strpos($p_key, 'jetpack/') !== false && strpos($single_opt[$devtype], 'jetpack_module/') !== false)
1406 $dis_dev = false;
1407 elseif(strpos($p_key, 'celtispack/') !== false && strpos($single_opt[$devtype], 'celtispack_module/') !== false)
1408 $dis_dev = false;
1409 }
1410 }
1411 }
1412 if(empty($single_opt) || $single_opt['filter'] === 'default'){
1413 if(!empty($filter['group'][$devtype]['plugins'])){
1414 if(false !== strpos($filter['group'][$devtype]['plugins'], $p_key))
1415 $dis_dev = false;
1416 elseif(strpos($p_key, 'jetpack/') !== false && strpos($filter['group'][$devtype]['plugins'], 'jetpack_module/') !== false)
1417 $dis_dev = false;
1418 elseif(strpos($p_key, 'celtispack/') !== false && strpos($filter['group'][$devtype]['plugins'], 'celtispack_module/') !== false)
1419 $dis_dev = false;
1420 }
1421 }
1422 if(!$dis_dev){
1423 //oEmbed Content API
1424 if(is_embed()){
1425 if(!empty($type) && !empty($filter['group'][$type]['plugins'])){
1426 if(false !== strpos($filter['group'][$type]['plugins'], $p_key))
1427 $unload = false;
1428 elseif(strpos($p_key, 'jetpack/') !== false && strpos($filter['group'][$type]['plugins'], 'jetpack_module/') !== false)
1429 $unload = false;
1430 elseif(strpos($p_key, 'celtispack/') !== false && strpos($filter['group'][$type]['plugins'], 'celtispack_module/') !== false)
1431 $unload = false;
1432 }
1433 } else {
1434 $pgfopt = false;
1435 if(is_singular()){
1436 if(!empty($single_opt) && $single_opt['filter'] === 'include'){
1437 $pgfopt = true;
1438 if(!empty($single_opt[$devtype])){
1439 if(false !== strpos($single_opt[$devtype], $p_key)){
1440 $unload = false;
1441 } else {
1442 //Enable plugin because plugin module is selected
1443 if(strpos($p_key, 'jetpack/') !== false && strpos($single_opt[$devtype], 'jetpack_module/') !== false)
1444 $unload = false;
1445 elseif(strpos($p_key, 'celtispack/') !== false && strpos($single_opt[$devtype], 'celtispack_module/') !== false)
1446 $unload = false;
1447 }
1448 }
1449 }
1450 }
1451 if($pgfopt === false){
1452 if(!empty($type) && !empty($filter['group'][$type]['plugins'])){
1453 if(in_array($p_key, array_map("trim", explode(',', $filter['group'][$type]['plugins'])))){
1454 $unload = false;
1455 } else {
1456 if(strpos($p_key, 'jetpack/') !== false && strpos($filter['group'][$type]['plugins'], 'jetpack_module/') !== false)
1457 $unload = false;
1458 else if(strpos($p_key, 'celtispack/') !== false && strpos($filter['group'][$type]['plugins'], 'celtispack_module/') !== false)
1459 $unload = false;
1460 }
1461 }
1462 }
1463 }
1464 }
1465 }
1466 }
1467 }
1468 if(!$unload) {
1469 if($option === 'active_sitewide_plugins') {
1470 // v4.0.6 $new_plugins[$item] = $opt_value[$item];
1471 $new_plugins[$item] = $item;
1472 } else {
1473 $new_plugins[] = $item;
1474 }
1475 }
1476 }
1477 }
1478
1479 //https://wordpress.org/support/topic/function-to-retriev-user-before-init-hook-is-fired/
1480 $new_plugins = apply_filters('plf_custom_changes_to_active_plugins', $new_plugins);
1481
1482 self::$cache[$keyid][$option] = $new_plugins;
1483 foreach ($new_plugins as $key) {
1484 if(!empty($key)){
1485 $key = self::plugin_keygen( $key, $option );
1486 self::$filtered_plugins[$key] = $key;
1487 }
1488 }
1489 return $new_plugins;
1490 }
1491 }
1492