Plugin Directory

Changeset 3198971


Ignore:
Timestamp:
11/28/2024 02:41:28 PM (16 months ago)
Author:
raldea89
Message:

1.2.8 update

Location:
simple-restrict
Files:
7 edited
2 copied

Legend:

Unmodified
Added
Removed
  • simple-restrict/tags/1.2.8/README.txt

    r3116467 r3198971  
    33Tags: restrict, hide, permission, authorization, restrict pages, hide pages, restrict content, hide content, user permission, page permission, user permissions, page
    44Requires at least: 3.4
    5 Tested up to: 6.6
    6 Stable tag: 1.2.7
     5Tested up to: 6.7
     6Stable tag: 1.2.8
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    139139= 1.2.7 - 14.02.2024 =
    140140Fixed: Security issue
     141
     142= 1.2.8 - 28.11.2024 =
     143Fixed: Security issue
  • simple-restrict/tags/1.2.8/includes/class-simple-restrict.php

    r3035727 r3198971  
    2929 */
    3030class Simple_Restrict {
    31    
     31
    3232
    3333    /**
     
    7171
    7272        $this->simple_restrict = 'simple-restrict';
    73         $this->version = '1.0.0';
     73        $this->version         = '1.2.8';
    7474
    7575        $this->load_dependencies();
     
    7777        $this->define_admin_hooks();
    7878        $this->define_public_hooks();
    79 
    8079    }
    8180
     
    102101         * core plugin.
    103102         */
    104         require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-simple-restrict-loader.php';
     103        require_once plugin_dir_path( __DIR__ ) . 'includes/class-simple-restrict-loader.php';
    105104
    106105        /**
     
    108107         * of the plugin.
    109108         */
    110         require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-simple-restrict-i18n.php';
     109        require_once plugin_dir_path( __DIR__ ) . 'includes/class-simple-restrict-i18n.php';
    111110
    112111        /**
    113112         * The class responsible for defining all actions that occur in the admin area.
    114113         */
    115         require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-simple-restrict-admin.php';
     114        require_once plugin_dir_path( __DIR__ ) . 'admin/class-simple-restrict-admin.php';
    116115
    117116        /**
     
    119118         * side of the site.
    120119         */
    121         require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-simple-restrict-public.php';
     120        require_once plugin_dir_path( __DIR__ ) . 'public/class-simple-restrict-public.php';
    122121
    123122        $this->loader = new Simple_Restrict_Loader();
    124 
    125123    }
    126124
     
    138136        $plugin_i18n = new Simple_Restrict_i18n();
    139137
    140         $this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' );
    141 
     138        $this->loader->add_action( 'init', $plugin_i18n, 'load_plugin_textdomain' );
    142139    }
    143140
     
    169166        $this->loader->add_action( 'personal_options_update', $plugin_admin, 'save_permission_checkboxes' );
    170167        $this->loader->add_action( 'edit_user_profile_update', $plugin_admin, 'save_permission_checkboxes' );
    171        
     168
    172169        // Add plugin settings menu
    173170        $this->loader->add_action( 'admin_menu', $plugin_admin, 'custom_admin_menu' );
    174171
    175         // Register new admin settings with WordPress and add them to the settings page 
     172        // Register new admin settings with WordPress and add them to the settings page
    176173        $this->loader->add_action( 'admin_init', $plugin_admin, 'simple_restrict_admin_init' );
    177        
     174
    178175        $this->loader->add_action( 'manage_users_columns', $plugin_admin, 'add_permissions_column', 10, 1 );
    179176        $this->loader->add_action( 'manage_users_custom_column', $plugin_admin, 'show_permissions_column_content', 10, 3 );
     
    181178        $this->loader->add_action( 'user_new_form', $plugin_admin, 'user_new_form_function' );
    182179        $this->loader->add_action( 'user_register', $plugin_admin, 'save_custom_user_profile_fields' );
    183 
    184180    }
    185181
     
    200196        // Check permissions and restrict content if necessary (call with wp hook instead of init hook so we can access post ID for all pages including homepage)
    201197        $this->loader->add_action( 'wp', $plugin_public, 'restrict_content' );
    202        
     198
    203199        $this->loader->add_action( 'init', $plugin_public, 'get_taxonomy_terms_object_array' );
    204        
     200
    205201        $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
    206202        $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
    207203        $this->loader->add_filter( 'rest_prepare_page', $plugin_public, 'rest_restrict', 30, 3 );
    208 
     204        $this->loader->add_action( 'pre_get_posts', $plugin_public, 'posts_args_search', 90, 1 );
    209205    }
    210206
     
    248244        return $this->version;
    249245    }
    250    
    251246}
  • simple-restrict/tags/1.2.8/public/class-simple-restrict-public.php

    r3035727 r3198971  
    11<?php
    2 
    32/**
    43 * The public-facing functionality of the plugin.
     
    4746     * Initialize the class and set its properties.
    4847     *
    49      * @param  string  $simple_restrict  The name of the plugin.
    50      * @param  string  $version          The version of this plugin.
     48     * @param  string $simple_restrict  The name of the plugin.
     49     * @param  string $version          The version of this plugin.
    5150     *
    5251     * @since    1.0.0
     
    107106            'order'      => 'ASC',
    108107        );
    109         //echo('taxonomy = '.$taxonomy);
    110108        $this->taxonomy_terms_object_array = get_terms( $taxonomy, $term_args );
    111109    }
    112110
    113     //Also defined in class-simple-restrict-admin.php
     111    // Also defined in class-simple-restrict-admin.php.
    114112    public function define_initial_variables() {
    115         $this->generic_restricted_message = __( "Sorry, this content is restricted to users who are logged in with the correct permissions.", 'simple-restrict' );
     113        $this->generic_restricted_message = __( 'Sorry, this content is restricted to users who are logged in with the correct permissions.', 'simple-restrict' );
    116114    }
    117115
     
    127125
    128126
    129     // Restrict content of specific page(s)
     127    /**
     128     * Restrict content of specific page(s).
     129     *
     130     * @param string $content The content of the page.
     131     * @return string
     132     */
    130133    public function restrict_content( $content ) {
    131         // We must prefix 'simple-restrict' to all the user metas (to not conflict with WordPress existing metas)
    132         $current_user_permissions          = array();  // User permissions will be prefixed by default
    133         $current_page_permissions          = array();  // Page permissions are user-defined, so we prefix them manually in next array
    134         $current_page_permissions_prefixed = array();  // This array will prefix each of the page permissions
    135 
    136         $postID = get_the_ID();
    137         //echo('$postID' . $postID);
    138 
    139         //echo("<br />Current page's permissions:<br />");
    140         // Create an array of the current page's permissions
    141         $page_terms_list = wp_get_post_terms( $postID, 'simple-restrict-permission', array( "fields" => "all" ) );
     134        // We must prefix 'simple-restrict' to all the user metas (to not conflict with WordPress existing metas).
     135        $current_user_permissions          = array();  // User permissions will be prefixed by default.
     136        $current_page_permissions          = array();  // Page permissions are user-defined, so we prefix them manually in next array.
     137        $current_page_permissions_prefixed = array();  // This array will prefix each of the page permissions.
     138
     139        $post_id = get_the_ID();
     140        // Create an array of the current page's permissions.
     141        $page_terms_list = wp_get_post_terms( $post_id, 'simple-restrict-permission', array( 'fields' => 'all' ) );
    142142        foreach ( $page_terms_list as $current_term ) {
    143143            if ( ! in_array( $current_term->slug, $current_page_permissions, true ) ) {
     
    146146                array_push( $current_page_permissions, $current_term->slug );
    147147                array_push( $current_page_permissions_prefixed, $current_term_slug_prefixed );
    148                 //print_r($current_page_permissions_prefixed);
    149             }
    150         }
    151         // Debug
    152         /*
    153         foreach($current_page_permissions as $current_page_permission) {
    154             echo('Page permission: '.$current_page_permission.'<br />');
    155         }
    156         foreach($current_page_permissions_prefixed as $current_page_permission) {
    157             echo('Page permission prefixed: '.$current_page_permission.'<br />');
    158         }
    159         */
    160 
    161         // If the page has no permissions required, show the content and don't bother checking user
     148            }
     149        }
     150
     151        // If the page has no permissions required, show the content and don't bother checking user.
    162152        if ( empty( $current_page_permissions ) ) {
    163153            return $content;
    164             // Otherwise check the user to see if it's permissions match the page's permissions
     154            // Otherwise check the user to see if it's permissions match the page's permissions.
    165155        } else {
    166             //echo("<br />Current user's permissions:<br />");
    167             // Create an array of the current user's permissions by cycling through all possible page permissions and putting any matches into user permissions array
     156            // Create an array of the current user's permissions by cycling through all possible page permissions and putting any matches into user permissions array.
    168157            $current_user_id = get_current_user_id();
    169             //echo('$current_user_id: ' . $current_user_id);           
    170             // Only populate user permissions if this is a registered user, otherwise leave permissions array empty
     158            // Only populate user permissions if this is a registered user, otherwise leave permissions array empty.
    171159            if ( $current_user_id != 0 ) {
    172160                foreach ( $this->taxonomy_terms_object_array as $taxonomy_object ) {
    173161                    $taxonomy_slug          = $taxonomy_object->slug;
    174162                    $taxonomy_slug_prefixed = 'simple-restrict-' . $taxonomy_slug;
    175                     //echo('$taxonomy_slug_prefixed ' . $taxonomy_slug_prefixed);
    176                     if ( esc_attr( get_the_author_meta( $taxonomy_slug_prefixed, $current_user_id ) ) == "yes" ) {
    177                         // Only add to array if it wasn't already there ($current_user_permissions values are always prefixed)
     163                    if ( 'yes' === esc_attr( get_the_author_meta( $taxonomy_slug_prefixed, $current_user_id ) ) ) {
     164                        // Only add to array if it wasn't already there ($current_user_permissions values are always prefixed).
    178165                        if ( ! in_array( $taxonomy_slug_prefixed, $current_user_permissions, true ) ) {
    179166                            array_push( $current_user_permissions, $taxonomy_slug_prefixed );
     
    182169                }
    183170            }
    184             // Debug
    185             /*
    186             foreach($current_user_permissions as $current_user_permission) {
    187                 echo('User permission: '.$current_user_permission.'<br />');
    188             }
    189             */
    190 
    191             $user_defined_restricted_message  = esc_attr( get_option( 'simple_restrict_setting_one' ) );
    192             $user_defined_restricted_message  = get_option( 'simple_restrict_setting_one' );
     171
    193172            $simple_restrict_setting_redirect = get_option( 'simple_restrict_setting_redirect' );
    194             // If the user's permissions don't match any of the page's permissions
     173            // If the user's permissions don't match any of the page's permissions.
    195174            if ( ! array_intersect( $current_page_permissions_prefixed, $current_user_permissions ) ) {
    196                 // Redirect to login or display message
     175                // Redirect to login or display message.
    197176                if ( isset( $simple_restrict_setting_redirect ) && ( $simple_restrict_setting_redirect == 1 ) ) {
    198                     header( "Location: /wp-login.php?redirect_to=" . $_SERVER['REQUEST_URI'] );
     177                    header( 'Location: /wp-login.php?redirect_to=' . $_SERVER['REQUEST_URI'] ); // phpcs:ignore
    199178                    exit;
    200179                } else {
     
    202181                }
    203182            } else {
    204                 // Otherwise show the regular content because it is restricted but the user has the permission
    205                 // (Note that $content is empty so below does nothing, and our script simply ends without a restriction)
     183                // Otherwise show the regular content because it is restricted but the user has the permission.
     184                // (Note that $content is empty so below does nothing, and our script simply ends without a restriction).
    206185                return $content;
    207186            }
     
    212191     * Restrict content of specific page(s) for REST API
    213192     *
    214      * @param  string  $response  The response object.
    215      * @param  string $post      The post object.
    216      * @param  string  $request   The request object.
     193     * @param  string $response  The response object.
     194     * @param  object $post      The post object.
     195     * @param  string $request   The request object.
    217196     *
    218197     * @since    1.0.0
    219198     */
    220199    public function rest_restrict( $response, $post, $request ) {
    221         // If this is an admin page, don't restrict content
     200        // If this is an admin page, don't restrict content.
    222201        if ( is_admin() ) {
    223202            return $response;
    224203        }
    225204
    226         // We must prefix 'simple-restrict' to all the user metas (to not conflict with WordPress existing metas)
    227         $current_page_permissions = array();  // Page permissions are user-defined, so we prefix them manually in next array
    228         $postID                   = $post->ID;
    229         // Create an array of the current page's permissions
    230         $page_terms_list = wp_get_post_terms( $postID, 'simple-restrict-permission', array( 'fields' => 'all' ) );
     205        // We must prefix 'simple-restrict' to all the user metas (to not conflict with WordPress existing metas).
     206        $current_page_permissions = array();  // Page permissions are user-defined, so we prefix them manually in next array.
     207        $post_id                  = $post->ID;
     208        // Create an array of the current page's permissions.
     209        $page_terms_list = wp_get_post_terms( $post_id, 'simple-restrict-permission', array( 'fields' => 'all' ) );
    231210        foreach ( $page_terms_list as $current_term ) {
    232211            if ( ! in_array( $current_term->slug, $current_page_permissions, true ) ) {
     
    235214        }
    236215
    237         // If the page has no permissions required, show the content and don't bother checking user
     216        // If the page has no permissions required, show the content and don't bother checking user.
    238217        if ( empty( $current_page_permissions ) ) {
    239218            return $response;
    240             // Otherwise check the user to see if it's permissions match the page's permissions
     219            // Otherwise check the user to see if it's permissions match the page's permissions.
    241220        } else {
    242221            // Check if the user has the required permissions.
     
    246225
    247226            // Send a 403 error if the content is restricted.
    248             //@todo: What can be done here is to check the request for the user's permissions and then send a 403 error if the user doesn't have the required permissions.
    249             // @todo: else return the content
     227            // @todo: What can be done here is to check the request for the user's permissions and then send a 403 error if the user doesn't have the required permissions.
     228            // @todo: else return the content.
    250229            wp_send_json_error( __( 'Sorry, this content is restricted', 'simple-restrict' ), 403 );
    251230
     
    253232        }
    254233    }
     234
     235    /**
     236     * Get all restricted pages
     237     *
     238     * @since    1.2.8
     239     */
     240    public function get_all_restricted_pages() {
     241        $terms                = get_terms( 'simple-restrict-permission' );
     242        $restricted_pages     = array();
     243        $restricted_pages_ids = array();
     244        $args                 = array(
     245            'post_type'      => 'page',
     246            'posts_per_page' => -1,
     247            'tax_query'      => array(
     248                array(
     249                    'taxonomy' => 'simple-restrict-permission',
     250                    'field'    => 'slug',
     251                    'terms'    => wp_list_pluck( $terms, 'slug' ),
     252                ),
     253            ),
     254        );
     255        $restricted_pages     = get_posts( $args );
     256        $current_user_id      = 0;
     257        if ( is_user_logged_in() ) {
     258            $current_user_id = get_current_user_id();
     259        }
     260        if ( ! empty( $restricted_pages ) ) {
     261            foreach ( $restricted_pages as $page ) {
     262                // Check if the user is logged in and has the required permissions.
     263                if ( 0 !== $current_user_id ) {
     264                    // Get the page's permissions.
     265                    $terms = wp_get_post_terms( $page->ID, 'simple-restrict-permission', array( 'fields' => 'all' ) );
     266                    foreach ( $terms as $taxonomy_object ) {
     267                        $taxonomy_slug          = $taxonomy_object->slug;
     268                        $taxonomy_slug_prefixed = 'simple-restrict-' . $taxonomy_slug;
     269                        // If the user has the required permissions, allow access.
     270                        if ( 'yes' === esc_attr( get_the_author_meta( $taxonomy_slug_prefixed, $current_user_id ) ) ) {
     271                            $access = true;
     272                        } else {
     273                            $access = false;
     274                        }
     275                    }
     276                } else { // User not logged in, so restrict access.
     277                    $access = false;
     278                }
     279
     280                if ( ! $access ) {
     281                    $restricted_pages_ids[] = $page->ID;
     282                }
     283            }
     284        }
     285
     286        return $restricted_pages_ids;
     287    }
     288
     289    /**
     290     * Restrict search results
     291     *
     292     * @param  object $query  The query object.
     293     *
     294     * @since    1.2.8
     295     */
     296    public function posts_args_search( $query ) {
     297
     298        if ( ! $query->is_search ) {
     299            return;
     300        }
     301        // Check if it's a pages query.
     302        $post_type_query = $query->get( 'post_type' );
     303        if ( '' !== $post_type_query && ( ( is_array( $post_type_query ) && ! in_array( 'page', $post_type_query, true ) ) || ( is_string( $post_type_query ) && 'page' !== $post_type_query ) ) ) {
     304            return;
     305        }
     306
     307        // Check if it's a search query or a REST request.
     308        if ( ( ! is_admin() ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST && isset( $query->query_vars['s'] ) ) ) {
     309            // Remove the filter to avoid infinite loop.
     310            remove_filter( 'pre_get_posts', array( $this, 'posts_args_search' ), 90 );
     311            $excluded_post_ids = $this->get_all_restricted_pages();
     312            // Add the filter back, as the request for the restricted pages is done.
     313            add_filter( 'pre_get_posts', array( $this, 'posts_args_search' ), 90, 1 );
     314            $query->set( 'post__not_in', $excluded_post_ids );
     315
     316        }
     317    }
    255318}
  • simple-restrict/tags/1.2.8/simple-restrict.php

    r3121456 r3198971  
    1616 * Plugin Name:       Simple Restrict
    1717 * Description:       Restrict pages based on permissions assigned to pages and granted in user profiles.
    18  * Version:           1.2.7
     18 * Version:           1.2.8
    1919 * Author:            WPChill
    2020 * Author URI:        https://wpchill.com
     
    2424 * Tested up to:      6.6
    2525 * Domain Path:       /languages
     26 *
     27 *
     28 *  Original Plugin URI:    http://www.awakensolutions.com/simple-restrict/
     29 *  Original Author URI:    http://www.awakensolutions.com
     30 *  Original Author:        Awaken Solutions Inc.
     31 *  Awaken Solutions Inc. has transferred ownership to WPChill on: 10th of February, 2024.
    2632 */
    2733
  • simple-restrict/trunk/README.txt

    r3116467 r3198971  
    33Tags: restrict, hide, permission, authorization, restrict pages, hide pages, restrict content, hide content, user permission, page permission, user permissions, page
    44Requires at least: 3.4
    5 Tested up to: 6.6
    6 Stable tag: 1.2.7
     5Tested up to: 6.7
     6Stable tag: 1.2.8
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    139139= 1.2.7 - 14.02.2024 =
    140140Fixed: Security issue
     141
     142= 1.2.8 - 28.11.2024 =
     143Fixed: Security issue
  • simple-restrict/trunk/includes/class-simple-restrict.php

    r3035727 r3198971  
    2929 */
    3030class Simple_Restrict {
    31    
     31
    3232
    3333    /**
     
    7171
    7272        $this->simple_restrict = 'simple-restrict';
    73         $this->version = '1.0.0';
     73        $this->version         = '1.2.8';
    7474
    7575        $this->load_dependencies();
     
    7777        $this->define_admin_hooks();
    7878        $this->define_public_hooks();
    79 
    8079    }
    8180
     
    102101         * core plugin.
    103102         */
    104         require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-simple-restrict-loader.php';
     103        require_once plugin_dir_path( __DIR__ ) . 'includes/class-simple-restrict-loader.php';
    105104
    106105        /**
     
    108107         * of the plugin.
    109108         */
    110         require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-simple-restrict-i18n.php';
     109        require_once plugin_dir_path( __DIR__ ) . 'includes/class-simple-restrict-i18n.php';
    111110
    112111        /**
    113112         * The class responsible for defining all actions that occur in the admin area.
    114113         */
    115         require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-simple-restrict-admin.php';
     114        require_once plugin_dir_path( __DIR__ ) . 'admin/class-simple-restrict-admin.php';
    116115
    117116        /**
     
    119118         * side of the site.
    120119         */
    121         require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-simple-restrict-public.php';
     120        require_once plugin_dir_path( __DIR__ ) . 'public/class-simple-restrict-public.php';
    122121
    123122        $this->loader = new Simple_Restrict_Loader();
    124 
    125123    }
    126124
     
    138136        $plugin_i18n = new Simple_Restrict_i18n();
    139137
    140         $this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' );
    141 
     138        $this->loader->add_action( 'init', $plugin_i18n, 'load_plugin_textdomain' );
    142139    }
    143140
     
    169166        $this->loader->add_action( 'personal_options_update', $plugin_admin, 'save_permission_checkboxes' );
    170167        $this->loader->add_action( 'edit_user_profile_update', $plugin_admin, 'save_permission_checkboxes' );
    171        
     168
    172169        // Add plugin settings menu
    173170        $this->loader->add_action( 'admin_menu', $plugin_admin, 'custom_admin_menu' );
    174171
    175         // Register new admin settings with WordPress and add them to the settings page 
     172        // Register new admin settings with WordPress and add them to the settings page
    176173        $this->loader->add_action( 'admin_init', $plugin_admin, 'simple_restrict_admin_init' );
    177        
     174
    178175        $this->loader->add_action( 'manage_users_columns', $plugin_admin, 'add_permissions_column', 10, 1 );
    179176        $this->loader->add_action( 'manage_users_custom_column', $plugin_admin, 'show_permissions_column_content', 10, 3 );
     
    181178        $this->loader->add_action( 'user_new_form', $plugin_admin, 'user_new_form_function' );
    182179        $this->loader->add_action( 'user_register', $plugin_admin, 'save_custom_user_profile_fields' );
    183 
    184180    }
    185181
     
    200196        // Check permissions and restrict content if necessary (call with wp hook instead of init hook so we can access post ID for all pages including homepage)
    201197        $this->loader->add_action( 'wp', $plugin_public, 'restrict_content' );
    202        
     198
    203199        $this->loader->add_action( 'init', $plugin_public, 'get_taxonomy_terms_object_array' );
    204        
     200
    205201        $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
    206202        $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
    207203        $this->loader->add_filter( 'rest_prepare_page', $plugin_public, 'rest_restrict', 30, 3 );
    208 
     204        $this->loader->add_action( 'pre_get_posts', $plugin_public, 'posts_args_search', 90, 1 );
    209205    }
    210206
     
    248244        return $this->version;
    249245    }
    250    
    251246}
  • simple-restrict/trunk/public/class-simple-restrict-public.php

    r3035727 r3198971  
    11<?php
    2 
    32/**
    43 * The public-facing functionality of the plugin.
     
    4746     * Initialize the class and set its properties.
    4847     *
    49      * @param  string  $simple_restrict  The name of the plugin.
    50      * @param  string  $version          The version of this plugin.
     48     * @param  string $simple_restrict  The name of the plugin.
     49     * @param  string $version          The version of this plugin.
    5150     *
    5251     * @since    1.0.0
     
    107106            'order'      => 'ASC',
    108107        );
    109         //echo('taxonomy = '.$taxonomy);
    110108        $this->taxonomy_terms_object_array = get_terms( $taxonomy, $term_args );
    111109    }
    112110
    113     //Also defined in class-simple-restrict-admin.php
     111    // Also defined in class-simple-restrict-admin.php.
    114112    public function define_initial_variables() {
    115         $this->generic_restricted_message = __( "Sorry, this content is restricted to users who are logged in with the correct permissions.", 'simple-restrict' );
     113        $this->generic_restricted_message = __( 'Sorry, this content is restricted to users who are logged in with the correct permissions.', 'simple-restrict' );
    116114    }
    117115
     
    127125
    128126
    129     // Restrict content of specific page(s)
     127    /**
     128     * Restrict content of specific page(s).
     129     *
     130     * @param string $content The content of the page.
     131     * @return string
     132     */
    130133    public function restrict_content( $content ) {
    131         // We must prefix 'simple-restrict' to all the user metas (to not conflict with WordPress existing metas)
    132         $current_user_permissions          = array();  // User permissions will be prefixed by default
    133         $current_page_permissions          = array();  // Page permissions are user-defined, so we prefix them manually in next array
    134         $current_page_permissions_prefixed = array();  // This array will prefix each of the page permissions
    135 
    136         $postID = get_the_ID();
    137         //echo('$postID' . $postID);
    138 
    139         //echo("<br />Current page's permissions:<br />");
    140         // Create an array of the current page's permissions
    141         $page_terms_list = wp_get_post_terms( $postID, 'simple-restrict-permission', array( "fields" => "all" ) );
     134        // We must prefix 'simple-restrict' to all the user metas (to not conflict with WordPress existing metas).
     135        $current_user_permissions          = array();  // User permissions will be prefixed by default.
     136        $current_page_permissions          = array();  // Page permissions are user-defined, so we prefix them manually in next array.
     137        $current_page_permissions_prefixed = array();  // This array will prefix each of the page permissions.
     138
     139        $post_id = get_the_ID();
     140        // Create an array of the current page's permissions.
     141        $page_terms_list = wp_get_post_terms( $post_id, 'simple-restrict-permission', array( 'fields' => 'all' ) );
    142142        foreach ( $page_terms_list as $current_term ) {
    143143            if ( ! in_array( $current_term->slug, $current_page_permissions, true ) ) {
     
    146146                array_push( $current_page_permissions, $current_term->slug );
    147147                array_push( $current_page_permissions_prefixed, $current_term_slug_prefixed );
    148                 //print_r($current_page_permissions_prefixed);
    149             }
    150         }
    151         // Debug
    152         /*
    153         foreach($current_page_permissions as $current_page_permission) {
    154             echo('Page permission: '.$current_page_permission.'<br />');
    155         }
    156         foreach($current_page_permissions_prefixed as $current_page_permission) {
    157             echo('Page permission prefixed: '.$current_page_permission.'<br />');
    158         }
    159         */
    160 
    161         // If the page has no permissions required, show the content and don't bother checking user
     148            }
     149        }
     150
     151        // If the page has no permissions required, show the content and don't bother checking user.
    162152        if ( empty( $current_page_permissions ) ) {
    163153            return $content;
    164             // Otherwise check the user to see if it's permissions match the page's permissions
     154            // Otherwise check the user to see if it's permissions match the page's permissions.
    165155        } else {
    166             //echo("<br />Current user's permissions:<br />");
    167             // Create an array of the current user's permissions by cycling through all possible page permissions and putting any matches into user permissions array
     156            // Create an array of the current user's permissions by cycling through all possible page permissions and putting any matches into user permissions array.
    168157            $current_user_id = get_current_user_id();
    169             //echo('$current_user_id: ' . $current_user_id);           
    170             // Only populate user permissions if this is a registered user, otherwise leave permissions array empty
     158            // Only populate user permissions if this is a registered user, otherwise leave permissions array empty.
    171159            if ( $current_user_id != 0 ) {
    172160                foreach ( $this->taxonomy_terms_object_array as $taxonomy_object ) {
    173161                    $taxonomy_slug          = $taxonomy_object->slug;
    174162                    $taxonomy_slug_prefixed = 'simple-restrict-' . $taxonomy_slug;
    175                     //echo('$taxonomy_slug_prefixed ' . $taxonomy_slug_prefixed);
    176                     if ( esc_attr( get_the_author_meta( $taxonomy_slug_prefixed, $current_user_id ) ) == "yes" ) {
    177                         // Only add to array if it wasn't already there ($current_user_permissions values are always prefixed)
     163                    if ( 'yes' === esc_attr( get_the_author_meta( $taxonomy_slug_prefixed, $current_user_id ) ) ) {
     164                        // Only add to array if it wasn't already there ($current_user_permissions values are always prefixed).
    178165                        if ( ! in_array( $taxonomy_slug_prefixed, $current_user_permissions, true ) ) {
    179166                            array_push( $current_user_permissions, $taxonomy_slug_prefixed );
     
    182169                }
    183170            }
    184             // Debug
    185             /*
    186             foreach($current_user_permissions as $current_user_permission) {
    187                 echo('User permission: '.$current_user_permission.'<br />');
    188             }
    189             */
    190 
    191             $user_defined_restricted_message  = esc_attr( get_option( 'simple_restrict_setting_one' ) );
    192             $user_defined_restricted_message  = get_option( 'simple_restrict_setting_one' );
     171
    193172            $simple_restrict_setting_redirect = get_option( 'simple_restrict_setting_redirect' );
    194             // If the user's permissions don't match any of the page's permissions
     173            // If the user's permissions don't match any of the page's permissions.
    195174            if ( ! array_intersect( $current_page_permissions_prefixed, $current_user_permissions ) ) {
    196                 // Redirect to login or display message
     175                // Redirect to login or display message.
    197176                if ( isset( $simple_restrict_setting_redirect ) && ( $simple_restrict_setting_redirect == 1 ) ) {
    198                     header( "Location: /wp-login.php?redirect_to=" . $_SERVER['REQUEST_URI'] );
     177                    header( 'Location: /wp-login.php?redirect_to=' . $_SERVER['REQUEST_URI'] ); // phpcs:ignore
    199178                    exit;
    200179                } else {
     
    202181                }
    203182            } else {
    204                 // Otherwise show the regular content because it is restricted but the user has the permission
    205                 // (Note that $content is empty so below does nothing, and our script simply ends without a restriction)
     183                // Otherwise show the regular content because it is restricted but the user has the permission.
     184                // (Note that $content is empty so below does nothing, and our script simply ends without a restriction).
    206185                return $content;
    207186            }
     
    212191     * Restrict content of specific page(s) for REST API
    213192     *
    214      * @param  string  $response  The response object.
    215      * @param  string $post      The post object.
    216      * @param  string  $request   The request object.
     193     * @param  string $response  The response object.
     194     * @param  object $post      The post object.
     195     * @param  string $request   The request object.
    217196     *
    218197     * @since    1.0.0
    219198     */
    220199    public function rest_restrict( $response, $post, $request ) {
    221         // If this is an admin page, don't restrict content
     200        // If this is an admin page, don't restrict content.
    222201        if ( is_admin() ) {
    223202            return $response;
    224203        }
    225204
    226         // We must prefix 'simple-restrict' to all the user metas (to not conflict with WordPress existing metas)
    227         $current_page_permissions = array();  // Page permissions are user-defined, so we prefix them manually in next array
    228         $postID                   = $post->ID;
    229         // Create an array of the current page's permissions
    230         $page_terms_list = wp_get_post_terms( $postID, 'simple-restrict-permission', array( 'fields' => 'all' ) );
     205        // We must prefix 'simple-restrict' to all the user metas (to not conflict with WordPress existing metas).
     206        $current_page_permissions = array();  // Page permissions are user-defined, so we prefix them manually in next array.
     207        $post_id                  = $post->ID;
     208        // Create an array of the current page's permissions.
     209        $page_terms_list = wp_get_post_terms( $post_id, 'simple-restrict-permission', array( 'fields' => 'all' ) );
    231210        foreach ( $page_terms_list as $current_term ) {
    232211            if ( ! in_array( $current_term->slug, $current_page_permissions, true ) ) {
     
    235214        }
    236215
    237         // If the page has no permissions required, show the content and don't bother checking user
     216        // If the page has no permissions required, show the content and don't bother checking user.
    238217        if ( empty( $current_page_permissions ) ) {
    239218            return $response;
    240             // Otherwise check the user to see if it's permissions match the page's permissions
     219            // Otherwise check the user to see if it's permissions match the page's permissions.
    241220        } else {
    242221            // Check if the user has the required permissions.
     
    246225
    247226            // Send a 403 error if the content is restricted.
    248             //@todo: What can be done here is to check the request for the user's permissions and then send a 403 error if the user doesn't have the required permissions.
    249             // @todo: else return the content
     227            // @todo: What can be done here is to check the request for the user's permissions and then send a 403 error if the user doesn't have the required permissions.
     228            // @todo: else return the content.
    250229            wp_send_json_error( __( 'Sorry, this content is restricted', 'simple-restrict' ), 403 );
    251230
     
    253232        }
    254233    }
     234
     235    /**
     236     * Get all restricted pages
     237     *
     238     * @since    1.2.8
     239     */
     240    public function get_all_restricted_pages() {
     241        $terms                = get_terms( 'simple-restrict-permission' );
     242        $restricted_pages     = array();
     243        $restricted_pages_ids = array();
     244        $args                 = array(
     245            'post_type'      => 'page',
     246            'posts_per_page' => -1,
     247            'tax_query'      => array(
     248                array(
     249                    'taxonomy' => 'simple-restrict-permission',
     250                    'field'    => 'slug',
     251                    'terms'    => wp_list_pluck( $terms, 'slug' ),
     252                ),
     253            ),
     254        );
     255        $restricted_pages     = get_posts( $args );
     256        $current_user_id      = 0;
     257        if ( is_user_logged_in() ) {
     258            $current_user_id = get_current_user_id();
     259        }
     260        if ( ! empty( $restricted_pages ) ) {
     261            foreach ( $restricted_pages as $page ) {
     262                // Check if the user is logged in and has the required permissions.
     263                if ( 0 !== $current_user_id ) {
     264                    // Get the page's permissions.
     265                    $terms = wp_get_post_terms( $page->ID, 'simple-restrict-permission', array( 'fields' => 'all' ) );
     266                    foreach ( $terms as $taxonomy_object ) {
     267                        $taxonomy_slug          = $taxonomy_object->slug;
     268                        $taxonomy_slug_prefixed = 'simple-restrict-' . $taxonomy_slug;
     269                        // If the user has the required permissions, allow access.
     270                        if ( 'yes' === esc_attr( get_the_author_meta( $taxonomy_slug_prefixed, $current_user_id ) ) ) {
     271                            $access = true;
     272                        } else {
     273                            $access = false;
     274                        }
     275                    }
     276                } else { // User not logged in, so restrict access.
     277                    $access = false;
     278                }
     279
     280                if ( ! $access ) {
     281                    $restricted_pages_ids[] = $page->ID;
     282                }
     283            }
     284        }
     285
     286        return $restricted_pages_ids;
     287    }
     288
     289    /**
     290     * Restrict search results
     291     *
     292     * @param  object $query  The query object.
     293     *
     294     * @since    1.2.8
     295     */
     296    public function posts_args_search( $query ) {
     297
     298        if ( ! $query->is_search ) {
     299            return;
     300        }
     301        // Check if it's a pages query.
     302        $post_type_query = $query->get( 'post_type' );
     303        if ( '' !== $post_type_query && ( ( is_array( $post_type_query ) && ! in_array( 'page', $post_type_query, true ) ) || ( is_string( $post_type_query ) && 'page' !== $post_type_query ) ) ) {
     304            return;
     305        }
     306
     307        // Check if it's a search query or a REST request.
     308        if ( ( ! is_admin() ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST && isset( $query->query_vars['s'] ) ) ) {
     309            // Remove the filter to avoid infinite loop.
     310            remove_filter( 'pre_get_posts', array( $this, 'posts_args_search' ), 90 );
     311            $excluded_post_ids = $this->get_all_restricted_pages();
     312            // Add the filter back, as the request for the restricted pages is done.
     313            add_filter( 'pre_get_posts', array( $this, 'posts_args_search' ), 90, 1 );
     314            $query->set( 'post__not_in', $excluded_post_ids );
     315
     316        }
     317    }
    255318}
  • simple-restrict/trunk/simple-restrict.php

    r3121456 r3198971  
    1616 * Plugin Name:       Simple Restrict
    1717 * Description:       Restrict pages based on permissions assigned to pages and granted in user profiles.
    18  * Version:           1.2.7
     18 * Version:           1.2.8
    1919 * Author:            WPChill
    2020 * Author URI:        https://wpchill.com
     
    2424 * Tested up to:      6.6
    2525 * Domain Path:       /languages
     26 *
     27 *
     28 *  Original Plugin URI:    http://www.awakensolutions.com/simple-restrict/
     29 *  Original Author URI:    http://www.awakensolutions.com
     30 *  Original Author:        Awaken Solutions Inc.
     31 *  Awaken Solutions Inc. has transferred ownership to WPChill on: 10th of February, 2024.
    2632 */
    2733
Note: See TracChangeset for help on using the changeset viewer.