Plugin Directory

Changeset 3186851


Ignore:
Timestamp:
11/13/2024 01:35:42 AM (14 months ago)
Author:
enomoto celtislab
Message:

ver4.2.0 Added filter hook plf_singler_custom_url_to_postid
Adjusted timing $current_user setting when calling wp_get_current_user and etc

Location:
plugin-load-filter/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • plugin-load-filter/trunk/mu-plugins/plf-filter.php

    r3066500 r3186851  
    33  Plugin Name: plugin load filter [plf-filter]
    44  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.1.1
     5  Version: 4.2.0
    66  Plugin URI: http://celtislab.net/en/wp-plugin-load-filter
    77  Author: enomoto@celtislab
     
    4343 */
    4444function wp_get_current_user() {
    45     if ( ! function_exists( 'wp_set_current_user' ) ){
     45    static $_current_user = null;
     46    if ( did_action( 'setup_theme' ) === 0 ){
    4647        if ( defined( 'LOGGED_IN_COOKIE' ) && !empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) {
    4748            $cookie_elements = explode( '|', $_COOKIE[ LOGGED_IN_COOKIE ] );
     
    4950                list( $username, $expiration, $token, $hmac ) = $cookie_elements;
    5051                if ( $expiration > time() ) {
     52                    if (!empty($_current_user) && $_current_user->user_login === $username){
     53                        return $_current_user;
     54                    }
    5155                    $user = get_user_by( 'login', $username );
    52                     if ( $user ) {                   
     56                    if ( $user ) {                       
    5357                        $pass_frag = substr( $user->user_pass, 8, 4 );
    54 
    5558                        $key = plf_logged_in_hash( $username . '|' . $pass_frag . '|' . $expiration . '|' . $token );
    5659
     
    6265                            $manager = WP_Session_Tokens::get_instance( $user->ID );
    6366                            if ( $manager->verify( $token ) ) {
     67                                //この時点のユーザーは仮ユーザーデータであり $current_user は未設定とする
     68                                $_current_user = $user;
    6469                                return $user;
    6570                            }                           
     
    7580       
    7681    } else {
     82        //$GLOBALS['wp_roles'] セット後の setup_theme アクション後に $current_user を設定する
     83        //※使用プラグインの組み合わせにもよるが、想定外に早い $current_user 設定は bbpress 等の一部プラグインにおいて capability 動的追加が反映されないことがあるため
    7784        return _wp_get_current_user();
    7885    }
     
    190197            add_filter('pre_option_jetpack_active_modules', array('Plf_filter', 'active_jetmodules'));
    191198            add_filter('pre_option_celtispack_active_modules', array('Plf_filter', 'active_celtismodules'));
     199
     200            add_filter('plf_singler_custom_url_to_postid', array('Plf_filter', 'custom_url_to_postid'), 10, 3);
    192201           
    193202            add_action('update_option_active_plugins', array($this, 'update_active_plugins'), 99999, 3);
     
    506515    }
    507516
     517    //This filter hook for when the post ID cannot be detected from the singler URL due to using a permalink change plugin etc.
     518    //Permalink Manger plugin (issue from Shawn X.)
     519    //Custom Permalinks plugin
     520    static function custom_url_to_postid($post_id, $url_path, $pre_active_plugins) {
     521        if(empty($post_id) && !empty($pre_active_plugins)){
     522            foreach ($pre_active_plugins as $key) {
     523                if ( strpos($key, 'permalink-manager' ) !== false){
     524                    $permalink_manager_uris = (array)get_option('permalink-manager-uris', array());
     525                    if(empty($permalink_manager_uris)){
     526                        break;
     527                    } else {
     528                        foreach ($permalink_manager_uris as $pid => $slug) {
     529                            if(preg_match("#/{$slug}(/?$)#ui", $url_path)){
     530                                $post_id = $pid;
     531                                break 2;
     532                            }               
     533                        }                                                           
     534                    }
     535                } else if ( strpos($key, 'custom-permalinks' ) !== false){
     536                    $url      = wp_parse_url( get_bloginfo( 'url' ) );
     537                    $url      = isset( $url['path'] ) ? $url['path'] : '';
     538                    $meta_val = ltrim( substr( $url_path, strlen( $url ) ), '/' );                 
     539
     540                    global $wpdb;
     541                    $posts = $wpdb->get_results(
     542                        $wpdb->prepare(
     543                            'SELECT p.ID, pm.meta_value, p.post_type, p.post_status ' .
     544                            " FROM $wpdb->posts AS p INNER JOIN $wpdb->postmeta AS pm ON (pm.post_id = p.ID) " .
     545                            " WHERE pm.meta_key = 'custom_permalink' " .
     546                            ' AND (pm.meta_value = %s OR pm.meta_value = %s) ' .
     547                            " AND p.post_status IN ('publish', 'private', 'inherit') " .
     548                            " AND p.post_type != 'nav_menu_item' " . // nav_menu_item を除外、他の post_type を許可
     549                            " ORDER BY FIELD(p.post_status,'publish','private','inherit')," .
     550                            " p.post_type LIMIT 1",
     551                            $meta_val,
     552                            $meta_val . '/'
     553                        )
     554                    );
     555                    if(!empty($posts[0]->ID)) {
     556                        $post_id = (int)$posts[0]->ID;
     557                    }
     558                    break;               
     559                }               
     560            }                             
     561        }
     562        return $post_id;
     563    }
     564   
    508565    //プラグインロード前は bbPress等 カスタムポストタイプのデバッグモードでのエラー表示抑制
    509566    static function exclude_trigger_error( $trigger, $function, $message, $version) {
     
    787844                        }                       
    788845                    }
     846                    $post_id = apply_filters('plf_singler_custom_url_to_postid', $post_id, $parse_url['path'], self::$base_plugins);
     847                    if(!empty($post_id)){
     848                        if(empty($wp_query->post)){
     849                            $r = new WP_Query( array( 'p' => $post_id, 'post_type' => 'any' ) );                           
     850                            if ($r->have_posts()) {
     851                                if(!empty($r->post)){
     852                                    $wp_query->posts = $r->posts;
     853                                    $wp_query->post = $r->post;
     854                                }
     855                            }                           
     856                        }                       
     857                        self::$url2id[$req_url] = (int)$post_id;
     858                    }                   
    789859                }
    790860                if(!empty($post_id)){
     
    10161086       
    10171087        //Equal treatment for when the wp_is_mobile is not yet available(wp-include/vars.php wp_is_mobile)
    1018         if ( empty($_SERVER['HTTP_USER_AGENT']) ) {
     1088        if ( isset( $_SERVER['HTTP_SEC_CH_UA_MOBILE'] ) ) {
     1089            // This is the `Sec-CH-UA-Mobile` user agent client hint HTTP request header.
     1090            // See <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Mobile>.
     1091            $is_mobile = ( '?1' === $_SERVER['HTTP_SEC_CH_UA_MOBILE'] );
     1092        } elseif ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
    10191093            $is_mobile = false;
    1020         } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.)
    1021             || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
    1022             || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
    1023             || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
    1024             || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
    1025             || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
    1026             || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) {
     1094        } elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Mobile' ) // Many mobile devices (all iPhone, iPad, etc.)
     1095            || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Android' )
     1096            || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Silk/' )
     1097            || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Kindle' )
     1098            || str_contains( $_SERVER['HTTP_USER_AGENT'], 'BlackBerry' )
     1099            || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Opera Mini' )
     1100            || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Opera Mobi' ) ) {
    10271101                $is_mobile = true;
    10281102        } else {
     
    11821256                }
    11831257                if(empty($wp_query->post)){
     1258                    //パーマリンクをカスタムするプラグイン等により URL から post ID が所得できない場合用のフィルターフック
     1259                    $post_id = apply_filters('plf_singler_custom_url_to_postid', 0, $parse_url['path'], self::$base_plugins);
     1260                    if(!empty($post_id)){
     1261                        $r = new WP_Query( array( 'p' => $post_id, 'post_type' => 'any' ) );                           
     1262                        if ($r->have_posts()) {
     1263                            if(!empty($r->post)){
     1264                                $wp_query->posts = $r->posts;
     1265                                $wp_query->post = $r->post;
     1266                            }
     1267                        }                           
     1268                    }
     1269                }               
     1270                if(empty($wp_query->post)){                   
    11841271                    $unknown = true;
    11851272                }               
     
    11881275       
    11891276        $single_opt = array();
    1190         if(is_singular() && is_object($wp_query->post)){
    1191             self::$pre_post_id = $wp_query->post->ID;   //for post_locale()
    1192             $myfilter = get_post_meta( $wp_query->post->ID, '_plugin_load_filter', true );
    1193             $default = array( 'filter' => 'default', 'desktop' => '', 'mobile' => '');
    1194             $single_opt = (!empty($myfilter))? $myfilter : $default;
    1195             $single_opt = wp_parse_args( $single_opt, $default);
     1277        if(is_singular()){
     1278            if(is_object($wp_query->post)){
     1279                self::$pre_post_id = $wp_query->post->ID;   //for post_locale()
     1280                $myfilter = get_post_meta( $wp_query->post->ID, '_plugin_load_filter', true );
     1281                $default = array( 'filter' => 'default', 'desktop' => '', 'mobile' => '');
     1282                $single_opt = (!empty($myfilter))? $myfilter : $default;
     1283                $single_opt = wp_parse_args( $single_opt, $default);               
     1284            }           
    11961285        }
    11971286        if(!empty(self::$s_url_filter)){
  • plugin-load-filter/trunk/plugin-load-filter.php

    r3066500 r3186851  
    33  Plugin Name: plugin load filter
    44  Description: Dynamically activate the selected plugins for each page. Response will be faster by filtering plugins.
    5   Version: 4.1.1
     5  Version: 4.2.0
    66  Plugin URI: https://celtislab.net/en/wp-plugin-load-filter
    77  Author: enomoto@celtislab
    88  Author URI: https://celtislab.net/
    99  Requires at least: 5.3
    10   Tested up to: 6.5
     10  Tested up to: 6.7
    1111  Requires PHP: 7.2
    1212  License: GPLv2
     
    183183    public function __construct() {
    184184
    185         load_plugin_textdomain('plf', false, basename( dirname( __FILE__ ) ).'/languages' );
    186 
     185        add_action('init', function(){ load_plugin_textdomain('plf', false, basename( dirname( __FILE__ ) ).'/languages' ); }, 1);
     186       
    187187        self::$filter = get_option('plf_option', array());
    188188        if(empty(self::$filter['optver']) || self::$filter['optver'] < '2'){
  • plugin-load-filter/trunk/readme.txt

    r3066500 r3186851  
    33Tags: dynamic deactivate plugins, disable plugins, performance, Locale switching
    44Requires at least: 5.3
    5 Tested up to: 6.5
     5Tested up to: 6.7
    66Requires PHP: 7.2
    7 Stable tag: 4.1.1
     7Stable tag: 4.2.0
    88Donate link: https://celtislab.net/en/wp-plugin-load-filter-addon/
    99License: GPLv2
     
    8282== Changelog ==
    8383
     84= 4.2.0 =
     85* 2024-11-12
     86* tested WP6.7
     87* Adjusted timing $current_user setting when calling wp_get_current_user is after setup_theme action.
     88* Added filter hook plf_singler_custom_url_to_postid for when post ID cannot be detected from the singler URL due to using a permalink change plugin etc.
     89* plf_singler_custom_url_to_postid filter hook has been used to get Post ID from custom URL of Permalink Manger or Custom Permalinks plugins.
     90* Fixed the call to load_plugin_textdomain to init hook.
     91
     92
    8493= 4.1.1 =
    8594* 2024-4-8
Note: See TracChangeset for help on using the changeset viewer.