Plugin Directory

Changeset 3205648


Ignore:
Timestamp:
12/10/2024 01:00:27 PM (16 months ago)
Author:
pluginsandsnippets
Message:

trunk 1.0.30

Location:
simple-page-access-restriction/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • simple-page-access-restriction/trunk/changelog.txt

    r3128968 r3205648  
     1= Version 1.0.30 - December 12, 2024 =
     2* Fix: Exclude restricted posts from GET and REST search queries
     3
    14= Version 1.0.29 - July 31, 2024 =
    25* Fix: Allow query parameters in the redirect URL
  • simple-page-access-restriction/trunk/readme.txt

    r3128968 r3205648  
    88Tested up to: 6.5
    99Requires PHP: 5.6
    10 Stable Tag: 1.0.29
     10Stable Tag: 1.0.30
    1111License: GPL v2 or later
    1212License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    116116
    117117== Changelog ==
     118= Version 1.0.30 - December 12, 2024 =
     119* Fix: Exclude restricted posts from GET and REST search queries
     120
    118121= Version 1.0.29 - July 31, 2024 =
    119122* Fix: Allow query parameters in the redirect URL
  • simple-page-access-restriction/trunk/simple-page-access-restriction.php

    r3128968 r3205648  
    44 * Plugin URI:        https://www.pluginsandsnippets.com/downloads/simple-page-access-restriction/
    55 * Description:       This plugin offers a simple way to restrict visits to select pages only to logged-in users and allows for page redirection to a defined (login) page of your choice.
    6  * Version:           1.0.29
     6 * Version:           1.0.30
    77 * Author:            Plugins & Snippets
    88 * Author URI:        https://www.pluginsandsnippets.com/
     
    3939        private static $instance;
    4040        private static $admin_instance;
    41        
    42         public function __construct() {
    43                
    44         }
    45 
     41
     42        public function __construct() {}
    4643
    4744        /**
     
    8077
    8178            // Plugin related constants
    82             define( 'SIMPLE_PAGE_ACCESS_RESTRICTION_VER', '1.0.29' );
     79            define( 'SIMPLE_PAGE_ACCESS_RESTRICTION_VER', '1.0.30' );
    8380            define( 'SIMPLE_PAGE_ACCESS_RESTRICTION_NAME', 'Simple Page Access Restriction' );
    8481            define( 'SIMPLE_PAGE_ACCESS_RESTRICTION_DIR', trailingslashit( plugin_dir_path( __FILE__ ) ) );
     
    130127         */
    131128        private function hooks() {
     129            add_action( 'init', array( $this, 'add_rest_api_filters' ) );
     130            add_filter( 'pre_get_posts', array( $this, 'exclude_restricted_posts' ) );
    132131            add_action( 'template_redirect', array( $this, 'check_page_access' ), 1 );
    133             add_action( 'init', array( $this, 'add_rest_api_filters' ) );
     132        }
     133
     134        /**
     135         * Change the GET and REST search queries
     136         * to exclude restricted posts.
     137         *
     138         * @param WP_Query $query The query.
     139         * @return WP_Query The query.
     140         */
     141        public function exclude_restricted_posts( $query ) {
     142            // Check if the request is for the backend.
     143            if ( is_admin() ) {
     144                // Return the query.
     145                return $query;
     146            }
     147
     148            // Set the restrict.
     149            $restrict = false;
     150
     151            // Check if the request is for the main search query.
     152            if ( $query->is_search && $query->is_main_query() ) {
     153                // Set the restrict.
     154                $restrict = true;
     155            }
     156
     157            // Check if the request is a REST request.
     158            if ( defined( 'REST_REQUEST' ) && REST_REQUEST && isset( $query->query_vars['s'] ) ) {
     159                // Set the restrict.
     160                $restrict = true;
     161            }
     162
     163            // Check the restrict.
     164            if ( $restrict ) {
     165                // Set the meta_query.
     166                $meta_query = array(
     167                    'relation' => 'OR',
     168                    array(
     169                        array(
     170                            'key'     => 'page_access_restricted',
     171                            'value'   => '0',
     172                            'compare' => '=',
     173                        ),
     174                    ),
     175                    array(
     176                        'key'     => 'page_access_restricted',
     177                        'compare' => 'NOT EXISTS',
     178                    ),
     179                );
     180
     181                // Set the query.
     182                $query->set( 'meta_query', $meta_query );
     183            }
     184
     185            // Return the query.
     186            return $query;
    134187        }
    135188
Note: See TracChangeset for help on using the changeset viewer.