Changeset 2966789
- Timestamp:
- 09/14/2023 12:26:14 AM (3 years ago)
- Location:
- disable-json-api
- Files:
-
- 27 added
- 2 deleted
- 7 edited
-
tags/v1.8 (added)
-
tags/v1.8/README.md (added)
-
tags/v1.8/admin.php (added)
-
tags/v1.8/classes (added)
-
tags/v1.8/classes/admin.php (added)
-
tags/v1.8/classes/disable-rest-api.php (added)
-
tags/v1.8/classes/helpers.php (added)
-
tags/v1.8/classes/index.php (added)
-
tags/v1.8/classes/requirements-check.php (added)
-
tags/v1.8/css (added)
-
tags/v1.8/css/admin.css (added)
-
tags/v1.8/disable-json-api.php (added)
-
tags/v1.8/docs (added)
-
tags/v1.8/docs/_config.yml (added)
-
tags/v1.8/docs/index.md (added)
-
tags/v1.8/index.php (added)
-
tags/v1.8/js (added)
-
tags/v1.8/js/admin-footer.js (added)
-
tags/v1.8/js/admin-header.js (added)
-
tags/v1.8/languages (added)
-
tags/v1.8/languages/index.php (added)
-
tags/v1.8/readme.txt (added)
-
tags/v1.8/uninstall.php (added)
-
trunk/README.md (added)
-
trunk/admin.php (modified) (1 diff)
-
trunk/classes/admin.php (modified) (4 diffs)
-
trunk/classes/disable-rest-api.php (modified) (13 diffs)
-
trunk/classes/helpers.php (modified) (9 diffs)
-
trunk/classes/requirements-check.php (modified) (4 diffs)
-
trunk/disable-json-api.php (modified) (3 diffs)
-
trunk/docs (added)
-
trunk/docs/_config.yml (added)
-
trunk/docs/index.md (added)
-
trunk/functions/index.php (deleted)
-
trunk/functions/legacy.php (deleted)
-
trunk/readme.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
disable-json-api/trunk/admin.php
r2574374 r2966789 8 8 </p> 9 9 10 <hr />10 <hr/> 11 11 12 12 <div id="select-container"> 13 13 <?php esc_html_e( "Rules for", "disable-json-api" ); ?>: <select name="role" id="dra-role"> 14 14 <option value="none"><?php esc_html_e( "Unauthenticated Users", "disable-json-api" ); ?></option> 15 <?php16 $role = ( isset( $_GET['role'] ) ) ? $_GET['role'] : 'none';17 wp_dropdown_roles( $role );18 ?>15 <?php 16 $role = ( isset( $_GET['role'] ) ) ? $_GET['role'] : 'none'; 17 wp_dropdown_roles( $role ); 18 ?> 19 19 </select> 20 20 </div> 21 21 22 <hr />22 <hr/> 23 23 24 24 <form method="post" action="" id="DRA_form"> 25 <?php wp_nonce_field( 'DRA_admin_nonce' ); ?>25 <?php wp_nonce_field( 'DRA_admin_nonce' ); ?> 26 26 <input type="hidden" name="role" value="<?php echo esc_attr( $role ); ?>"> 27 27 28 28 <div id="default-allow-container"> 29 <?php DRA_Admin::display_role_default_allow( $role ); ?>29 <?php DRA_Admin::display_role_default_allow( $role ); ?> 30 30 </div> 31 31 32 <hr />32 <hr/> 33 33 34 34 <div id="route-container"> 35 <?php DRA_Admin::display_route_checkboxes( $role ); ?>36 <hr />35 <?php DRA_Admin::display_route_checkboxes( $role ); ?> 36 <hr/> 37 37 </div> 38 38 -
disable-json-api/trunk/classes/admin.php
r2474673 r2966789 1 1 <?php 2 2 3 class DRA_Admin { 3 4 … … 8 9 */ 9 10 static function display_route_checkboxes( $role = 'none' ) { 10 $all_namespaces = DRA_Helpers::get_all_rest_namespaces();11 $all_routes = DRA_Helpers::get_all_rest_routes();12 $allowed_routes = DRA_Helpers::get_allowed_routes( $role );11 $all_namespaces = DRA_Helpers::get_all_rest_namespaces(); 12 $all_routes = DRA_Helpers::get_all_rest_routes(); 13 $allowed_routes = DRA_Helpers::get_allowed_routes( $role ); 13 14 14 15 $loopCounter = 0; … … 64 65 */ 65 66 static function display_role_default_allow( $role ) { 66 $default_allow_true_checked = '';67 $default_allow_true_checked = ''; 67 68 $default_allow_false_checked = ''; 68 69 … … 76 77 /* translators: name of user role */ 77 78 echo sprintf( '<h2>%s</h2>', sprintf( esc_html__( 'Manage Rules for %s Users', 'disable-json-api' ), DRA_Helpers::get_role_name( $role ) ) ); 78 ?>79 <p style="font-style:italic;">80 <?php81 echo esc_html__( 'NOTE: New routes may be added in the future by plugins, themes, or WordPress itself.', 'disable-json-api' );82 echo '<br />';83 echo esc_html__( 'If you choose to manage access for a user role, you will have to come back and add permissions for any new routes later.', 'disable-json-api' );84 ?>79 ?> 80 <p style="font-style:italic;"> 81 <?php 82 echo esc_html__( 'NOTE: New routes may be added in the future by plugins, themes, or WordPress itself.', 'disable-json-api' ); 83 echo '<br />'; 84 echo esc_html__( 'If you choose to manage access for a user role, you will have to come back and add permissions for any new routes later.', 'disable-json-api' ); 85 ?> 85 86 </p> 86 <label><input type="radio" name="default_allow" value="0" <?php echo $default_allow_false_checked; ?>> <?php echo esc_html__( 'Manage REST API Access', 'disable-json-api' ); ?></label>87 88 <label><input type="radio" name="default_allow" value="1" <?php echo $default_allow_true_checked; ?>> <?php echo esc_html__( 'Allow Full REST API Access', 'disable-json-api' ); ?></label>87 <label><input type="radio" name="default_allow" value="0" <?php echo $default_allow_false_checked; ?>> <?php echo esc_html__( 'Manage REST API Access', 'disable-json-api' ); ?></label> 88 89 <label><input type="radio" name="default_allow" value="1" <?php echo $default_allow_true_checked; ?>> <?php echo esc_html__( 'Allow Full REST API Access', 'disable-json-api' ); ?></label> 89 90 <?php 90 91 } -
disable-json-api/trunk/classes/disable-rest-api.php
r2574374 r2966789 1 1 <?php 2 2 3 /** 3 4 * Disable_REST_API class … … 30 31 31 32 // Do logic for upgrading to 1.6 from versions less than 1.6 32 add_action( ' init', array( &$this, 'option_check' ) );33 add_action( 'wp_loaded', array( &$this, 'option_check' ) ); 33 34 34 35 // Set up admin page for plugin settings … … 73 74 */ 74 75 private function get_current_route() { 75 $rest_route = $GLOBALS['wp']->query_vars['rest_route']; 76 $rest_route = isset( $GLOBALS['wp']->query_vars['rest_route'] ) ? 77 $GLOBALS['wp']->query_vars['rest_route'] : 78 ''; 76 79 77 80 return ( empty( $rest_route ) || '/' == $rest_route ) ? … … 90 93 private function is_route_allowed( $currentRoute ) { 91 94 92 $current_options = get_option( 'disable_rest_api_options', array() );95 $current_options = get_option( 'disable_rest_api_options', array() ); 93 96 $current_user_roles = $this->get_current_user_roles(); 94 97 … … 97 100 98 101 // If we have a definition for the current user's role 99 if ( isset( $current_options['roles'][ $role] ) ) {102 if ( isset( $current_options['roles'][ $role ] ) ) { 100 103 101 104 // If any role for this user is set to Allow Full REST API Access, return true automatically 102 if ( true === $current_options['roles'][ $role]['default_allow'] ) {105 if ( true === $current_options['roles'][ $role ]['default_allow'] ) { 103 106 return true; 104 107 } … … 215 218 if ( ! DRA_Helpers::is_valid_role( $role ) ) { 216 219 add_settings_error( 'DRA-notices', esc_attr( 'settings_updated' ), esc_html__( 'Invalid user role detected when processing form. No updates have been made.', 'disable-json-api' ), 'error' ); 220 217 221 return; 218 222 } … … 232 236 // Unauthorized users default to no routes allowed. All other user roles default to allowing all routes 233 237 $rest_routes_for_setting = DRA_Helpers::build_routes_rule_for_all( $default_allow ); 234 $msg = esc_html__( 'All allowlists have been reset for this user role.', 'disable-json-api' );238 $msg = esc_html__( 'All allowlists have been reset for this user role.', 'disable-json-api' ); 235 239 236 240 } else { … … 238 242 // Get back the full list of true/false routes based on the posted routes allowed 239 243 $rest_routes_for_setting = DRA_Helpers::build_routes_rule( $rest_routes ); 240 $msg = esc_html__( 'Allowlist settings saved for this user role.', 'disable-json-api' );244 $msg = esc_html__( 'Allowlist settings saved for this user role.', 'disable-json-api' ); 241 245 242 246 } 243 247 244 248 // Save only the rules for this role back to itself 245 $arr_option['roles'][ $role] = array(246 'default_allow' => $default_allow,247 'allow_list' => $rest_routes_for_setting,249 $arr_option['roles'][ $role ] = array( 250 'default_allow' => $default_allow, 251 'allow_list' => $rest_routes_for_setting, 248 252 ); 249 253 … … 274 278 */ 275 279 private function get_wp_error( $access ) { 276 $error_message = esc_html__( 'DRA: Only authenticated users can access the REST API.', 'disable-json-api' ); 280 $dra_error_message = apply_filters( 'dra_error_message', 'DRA: Only authenticated users can access the REST API.', $access ); 281 $error_message = esc_html__( $dra_error_message, 'disable-json-api' ); 277 282 278 283 if ( is_wp_error( $access ) ) { … … 292 297 293 298 // If our new option already exists, we can bail 294 if ( get_option( 'disable_rest_api_options' ) ) {299 if ( get_option( 'disable_rest_api_options' ) ) { 295 300 return; 296 301 } … … 309 314 // Define the basic structure of our new option 310 315 $arr_option = array( 311 'version' => self::VERSION, // the current version of this plugin312 'default_allow' => true, // if a role is not specifically defined in the settings, should the default be to ALLOW the route or not?313 'roles' => array(), // array of the user roles in this install of wordpress316 'version' => self::VERSION, // the current version of this plugin 317 'default_allow' => true, // if a role is not specifically defined in the settings, should the default be to ALLOW the route or not? 318 'roles' => array(), // array of the user roles in this install of wordpress 314 319 ); 315 320 … … 325 330 // Define the "unauthenticated" rules based on the old option value (or default value of "nothing") 326 331 $arr_option['roles']['none'] = array( 327 'default_allow' => false,328 'allow_list' => $new_unauthenticated_rules,332 'default_allow' => false, 333 'allow_list' => $new_unauthenticated_rules, 329 334 ); 330 335 … … 353 358 354 359 $user = wp_get_current_user(); 360 355 361 return ( array ) $user->roles; 356 362 -
disable-json-api/trunk/classes/helpers.php
r2474673 r2966789 1 1 <?php 2 2 3 class DRA_Helpers { 3 4 … … 8 9 */ 9 10 static function get_all_rest_routes() { 10 $wp_rest_server = rest_get_server(); 11 $wp_rest_server = rest_get_server(); 12 11 13 return array_keys( $wp_rest_server->get_routes() ); 12 14 } … … 19 21 */ 20 22 static function get_all_rest_namespaces() { 21 $wp_rest_server = rest_get_server(); 23 $wp_rest_server = rest_get_server(); 24 22 25 return $wp_rest_server->get_namespaces(); 23 26 } … … 45 48 $new_value = true; 46 49 } 47 $new_rules[ esc_html($route)] = $new_value;50 $new_rules[ esc_html( $route ) ] = $new_value; 48 51 } 49 52 … … 69 72 // Loop through ALL routes, set all to the desired value 70 73 foreach ( $all_routes as $route ) { 71 $new_rules[ esc_html($route)] = $default_value;74 $new_rules[ esc_html( $route ) ] = $default_value; 72 75 } 73 76 … … 120 123 } 121 124 122 $option_rules = array();125 $option_rules = array(); 123 126 $allowed_rules = array(); 124 127 … … 129 132 $option_rules = ( array ) DRA_Helpers::build_routes_rule( $arr_option ); 130 133 131 } elseif ( isset( $arr_option['roles'][ $role]['allow_list'] ) ) {134 } elseif ( isset( $arr_option['roles'][ $role ]['allow_list'] ) ) { 132 135 133 136 // If we have a definition for the currently requested role, return it 134 $option_rules = ( array ) $arr_option['roles'][ $role]['allow_list'];137 $option_rules = ( array ) $arr_option['roles'][ $role ]['allow_list']; 135 138 136 139 } else { … … 175 178 $default_allow = ( 'none' == $role ) ? false : true; 176 179 177 if ( isset( $arr_option['roles'][ $role]['default_allow'] ) ) {178 $default_allow = $arr_option['roles'][ $role]['default_allow'];180 if ( isset( $arr_option['roles'][ $role ]['default_allow'] ) ) { 181 $default_allow = $arr_option['roles'][ $role ]['default_allow']; 179 182 } 180 183 … … 199 202 200 203 $editable_roles = get_editable_roles(); 201 if ( isset( $editable_roles[ $role] ) ) {202 return translate_user_role( $editable_roles[ $role]['name'] );204 if ( isset( $editable_roles[ $role ] ) ) { 205 return translate_user_role( $editable_roles[ $role ]['name'] ); 203 206 } 204 207 -
disable-json-api/trunk/classes/requirements-check.php
r2474673 r2966789 15 15 public function __construct( $args ) { 16 16 foreach ( array( 'title', 'php', 'wp', 'file' ) as $setting ) { 17 if ( isset( $args[ $setting] ) ) {18 $this->$setting = $args[ $setting];17 if ( isset( $args[ $setting ] ) ) { 18 $this->$setting = $args[ $setting ]; 19 19 } 20 20 } … … 26 26 add_action( 'admin_notices', array( $this, 'deactivate' ) ); 27 27 } 28 28 29 return $passes; 29 30 } … … 40 41 } else { 41 42 add_action( 'admin_notices', array( $this, 'php_version_notice' ) ); 43 42 44 return false; 43 45 } … … 59 61 } else { 60 62 add_action( 'admin_notices', array( $this, 'wp_version_notice' ) ); 63 61 64 return false; 62 65 } -
disable-json-api/trunk/disable-json-api.php
r2574374 r2966789 4 4 * Plugin URI: http://www.binarytemplar.com/disable-json-api 5 5 * Description: Disable the use of the REST API on your website to anonymous users. You can optionally enable select endpoints if you wish. Now with support for User Roles! 6 * Version: 1.7 6 * Version: 1.8 7 * Requires at least: 4.9 8 * Requires PHP: 5.6 7 9 * Author: Dave McHale 8 10 * Author URI: http://www.binarytemplar.com 11 * License: GPL2+ 12 * License URI: https://www.gnu.org/licenses/gpl-2.0.html 9 13 * Text Domain: disable-json-api 10 14 * Domain Path: /languages 11 * License: GPL2+12 15 */ 13 16 … … 28 31 $dra_requirements_check = new DRA_Requirements_Check( array( 29 32 'title' => 'Disable REST API', 30 'php' => '5. 3',31 'wp' => '4. 4',33 'php' => '5.6', 34 'wp' => '4.9', 32 35 'file' => __FILE__, 33 36 ) ); … … 41 44 remove_action( 'template_redirect', 'rest_output_link_header', 11 ); 42 45 43 // WordPress 4.7+ disables the REST API via authentication short-circuit. 44 // For versions of WordPress < 4.7, disable the REST API via filters 45 if ( version_compare( get_bloginfo( 'version' ), '4.7', '>=' ) ) { 46 // Load in extra classes 47 require_once( plugin_dir_path( __FILE__ ) . 'classes/helpers.php' ); 46 48 47 // Load in extra classes 48 require_once( plugin_dir_path( __FILE__ ) . 'classes/helpers.php' ); 49 // Only load admin classes if in admin area 50 if ( is_admin() ) { 51 require_once( plugin_dir_path( __FILE__ ) . 'classes/admin.php' ); 52 } 49 53 50 // Only load admin classes if in admin area 51 if ( is_admin() ) { 52 require_once( plugin_dir_path( __FILE__ ) . 'classes/admin.php' ); 53 } 54 55 // Load the primary Disable_REST_API class 56 require_once( plugin_dir_path( __FILE__ ) . 'classes/disable-rest-api.php' ); 57 new Disable_REST_API( __FILE__ ); 58 59 } else { 60 require_once( plugin_dir_path( __FILE__ ) . 'functions/legacy.php' ); 61 DRA_Disable_Via_Filters(); 62 } 54 // Load the primary Disable_REST_API class 55 require_once( plugin_dir_path( __FILE__ ) . 'classes/disable-rest-api.php' ); 56 new Disable_REST_API( __FILE__ ); 63 57 64 58 } -
disable-json-api/trunk/readme.txt
r2574374 r2966789 2 2 Contributors: dmchale, tangrufus 3 3 Tags: admin, api, json, REST, rest-api, disable 4 Requires at least: 4. 44 Requires at least: 4.9 5 5 Requires PHP: 5.6 6 Tested up to: 5.87 Stable tag: 1. 76 Tested up to: 6.3 7 Stable tag: 1.8 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 46 46 47 47 == Changelog == 48 49 = 1.8 = 50 * Tested up to WP v6.3 51 * Added `dra_error_message` filter so devs can customize the access error message 52 * Fixed bug that caused fatal errors if activating plugin on installations running the LearnPress plugin 53 * Changed minimum requirements to PHP 5.6 (up from 5.3) and WordPress 4.9 (up from 4.4). Adding docblock comments to support minimums. 48 54 49 55 = 1.7 = … … 100 106 == Upgrade Notice == 101 107 108 = 1.8 = 109 * Improved UI/UX of admin settings page to better manage routes 110 102 111 = 1.6 = 103 112 * By popular request... now with User Role support!
Note: See TracChangeset
for help on using the changeset viewer.