-
Notifications
You must be signed in to change notification settings - Fork 40
Feat/import export roles #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
omaraelhawary
wants to merge
18
commits into
develop
Choose a base branch
from
feat/import-export-roles
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e595aef
fix: show disabled checkboxes with tooltips for non-selectable roles
omaraelhawary 9cf187f
feat: add role export handler class
omaraelhawary aff09c4
feat: add role import handler class
omaraelhawary fea85c4
feat: integrate role import and export into the admin roles page
omaraelhawary fcd93db
fix: initialize new settings in Role_Import class
omaraelhawary 9fb9f00
refactor: remove redundant role update logic in Role_Import class
omaraelhawary 10056aa
refactor: sanitize non-string values in Role_Import class settings
omaraelhawary b0d43f2
feat: enhance role import validation and prevent accidental overwrites
omaraelhawary a105a81
feat: add role existence check during import process
omaraelhawary 39c96dd
fix: improve role overwrite prevention logic in Role_Import class
omaraelhawary f894531
fix: enhance role overwrite prevention and sanitize non-string settin…
omaraelhawary 84abefc
refactor: remove unused global variable reference in Role_Import class
omaraelhawary 09059be
fix: enhance role protection during import process
omaraelhawary 180539d
fix: address security and correctness issues in role import/export
omaraelhawary 1f7dc06
refactor: improve UI interaction and structure in roles management
omaraelhawary 72630e2
feat: implement import/export functionality in roles management
omaraelhawary 4d81312
fix: improve role label sanitization and protection logic during import
omaraelhawary 2732c9c
fix: handle JSON read errors during role import process
omaraelhawary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| <?php | ||
| /** | ||
| * Handles role export. | ||
| * | ||
| * @package Members | ||
| * @subpackage Admin | ||
| * @author The MemberPress Team | ||
| * @copyright Copyright (c) 2009 - 2018, The MemberPress Team | ||
| * @link https://members-plugin.com/ | ||
| * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html | ||
| */ | ||
| namespace Members\Admin; | ||
|
|
||
| defined('ABSPATH') || exit; | ||
|
|
||
| /** | ||
| * Class that handles exporting roles and settings to a JSON file. | ||
| * | ||
| * @since 3.3.0 | ||
| * @access public | ||
| */ | ||
| final class Role_Export { | ||
|
|
||
| /** | ||
| * Holds the instances of this class. | ||
| * | ||
| * @since 3.3.0 | ||
| * @access private | ||
| * @var object | ||
| */ | ||
| private static $instance; | ||
|
|
||
| /** | ||
| * Sets up initial actions. | ||
| * | ||
| * @since 3.3.0 | ||
| * @access public | ||
| * @return void | ||
| */ | ||
| public function __construct() { | ||
|
|
||
| add_action( 'admin_post_members_export_roles', array( $this, 'handle_export' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Handles the "Export All" request via the header button. | ||
| * | ||
| * @since 3.3.0 | ||
| * @access public | ||
| * @return void | ||
| */ | ||
| public function handle_export() { | ||
|
|
||
| check_admin_referer( 'members_export_roles' ); | ||
|
|
||
| if ( ! current_user_can( 'list_roles' ) ) { | ||
| wp_die( esc_html__( 'You do not have permission to export roles.', 'members' ) ); | ||
| } | ||
|
|
||
| $this->generate_export(); | ||
| } | ||
|
|
||
| /** | ||
| * Handles a selective bulk export. Called from Roles::load(). | ||
| * | ||
| * @since 3.4.0 | ||
| * @access public | ||
| * @param array $role_slugs Array of sanitized role slugs to export. | ||
| * @return void | ||
| */ | ||
| public function handle_selective_export( $role_slugs ) { | ||
|
|
||
| if ( ! current_user_can( 'list_roles' ) ) { | ||
| wp_die( esc_html__( 'You do not have permission to export roles.', 'members' ) ); | ||
| } | ||
|
|
||
| $this->generate_export( $role_slugs ); | ||
| } | ||
|
|
||
| /** | ||
| * Generates and sends the JSON export file as a download. | ||
| * | ||
| * @since 3.4.0 | ||
| * @access private | ||
| * @param array $role_slugs Array of role slugs to export. Empty means all roles. | ||
| * @return void | ||
| */ | ||
| private function generate_export( $role_slugs = array() ) { | ||
|
|
||
| global $wp_roles; | ||
|
|
||
| // Build roles array. | ||
| $roles = array(); | ||
|
|
||
| foreach ( $wp_roles->roles as $slug => $role_data ) { | ||
|
|
||
| if ( ! empty( $role_slugs ) && ! in_array( $slug, $role_slugs, true ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| $roles[ $slug ] = array( | ||
| 'name' => $slug, | ||
| 'label' => $role_data['name'], | ||
| 'capabilities' => $role_data['capabilities'], | ||
| ); | ||
| } | ||
|
|
||
| $file_headers = get_file_data( members_plugin()->dir . 'members.php', array( 'Version' => 'Version' ) ); | ||
|
|
||
| // Build export data. Only include settings for full (non-selective) exports. | ||
| $data = array( | ||
| 'meta' => array( | ||
| 'plugin' => 'members', | ||
| 'version' => ! empty( $file_headers['Version'] ) ? $file_headers['Version'] : '', | ||
| 'export_date' => gmdate( 'c' ), | ||
| 'site_url' => site_url(), | ||
| 'wp_version' => get_bloginfo( 'version' ), | ||
| ), | ||
| 'roles' => $roles, | ||
| 'settings' => empty( $role_slugs ) ? get_option( 'members_settings', members_get_default_settings() ) : array(), | ||
| ); | ||
|
|
||
| // Send file download. | ||
| $filename = 'members-roles-export-' . gmdate( 'Y-m-d-His' ) . '.json'; | ||
|
|
||
| nocache_headers(); | ||
| header( 'Content-Type: application/json; charset=utf-8' ); | ||
| header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); | ||
|
|
||
| echo wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ); | ||
| exit; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the instance. | ||
| * | ||
| * @since 3.3.0 | ||
| * @access public | ||
| * @return object | ||
| */ | ||
| public static function get_instance() { | ||
|
|
||
| if ( ! self::$instance ) | ||
| self::$instance = new self; | ||
|
|
||
| return self::$instance; | ||
| } | ||
| } | ||
|
|
||
| Role_Export::get_instance(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.