siteguard-admin-filter.php
1 month ago
siteguard-base.php
1 month ago
siteguard-captcha.php
1 month ago
siteguard-config.php
11 months ago
siteguard-disable-author-query.php
2 weeks ago
siteguard-disable-pingback.php
1 month ago
siteguard-disable-xmlrpc.php
1 month ago
siteguard-htaccess.php
2 weeks ago
siteguard-login-alert.php
1 month ago
siteguard-login-history.php
1 month ago
siteguard-login-lock.php
1 month ago
siteguard-rename-login.php
1 week ago
siteguard-updates-notify.php
1 month ago
siteguard-waf-exclude-rule.php
1 month ago
siteguard-disable-author-query.php
63 lines
| 1 | <?php |
| 2 | |
| 3 | class SiteGuard_Disable_Author_Query extends SiteGuard_Base { |
| 4 | |
| 5 | function __construct() { |
| 6 | global $siteguard_config; |
| 7 | if ( '1' === $siteguard_config->get( 'block_author_query_enable' ) ) { |
| 8 | add_action( 'init', array( $this, 'handler_author_query' ) ); |
| 9 | if ( '1' == $siteguard_config->get( 'disable_restapi_enable' ) ) { |
| 10 | add_filter( 'rest_pre_dispatch', array( $this, 'handler_deny_rest_api' ), 10, 3 ); |
| 11 | } |
| 12 | } |
| 13 | } |
| 14 | function init() { |
| 15 | global $siteguard_config; |
| 16 | $siteguard_config->set( 'block_author_query_enable', '0' ); |
| 17 | $siteguard_config->set( 'disable_restapi_enable', '0' ); |
| 18 | $siteguard_config->set( 'disable_restapi_exclude', 'oembed,contact-form-7,akismet' ); |
| 19 | $siteguard_config->update(); |
| 20 | } |
| 21 | function handler_author_query() { |
| 22 | if ( isset( $_SERVER['REQUEST_URI'] ) ) { |
| 23 | $request_uri = urldecode( $_SERVER['REQUEST_URI'] ); |
| 24 | if ( ! is_admin() && preg_match( '/[?&]author=[0-9]+/i', $request_uri ) ) { |
| 25 | wp_safe_redirect( home_url() ); |
| 26 | exit; |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | private function normalize_rest_api_namespace( $namespace ) { |
| 31 | $namespace = trim( $namespace ); |
| 32 | return trim( $namespace, '/' ); |
| 33 | } |
| 34 | private function is_excluded_rest_api_route( $route, $namespace ) { |
| 35 | $namespace = $this->normalize_rest_api_namespace( $namespace ); |
| 36 | if ( '' === $namespace ) { |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | $route = '/' . ltrim( $route, '/' ); |
| 41 | return $route === "/$namespace" || strpos( $route, "/$namespace/" ) === 0; |
| 42 | } |
| 43 | function handler_deny_rest_api( $result, $wp_rest_server, $request ) { |
| 44 | global $siteguard_config; |
| 45 | $exclude_app = preg_split( '/,/', $siteguard_config->get( 'disable_restapi_exclude' ) ); |
| 46 | |
| 47 | $route = $request->get_route(); |
| 48 | foreach ( $exclude_app as $app ) { |
| 49 | if ( $this->is_excluded_rest_api_route( $route, $app ) ) { |
| 50 | return $result; |
| 51 | } |
| 52 | } |
| 53 | if ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ) ) { |
| 54 | return $result; |
| 55 | } |
| 56 | return new WP_Error( |
| 57 | 'rest_disabled', |
| 58 | __( 'The REST API on this site has been disabled.' ), |
| 59 | array( 'status' => rest_authorization_required_code() ) |
| 60 | ); |
| 61 | } |
| 62 | } |
| 63 |