Skip to content
Open
Show file tree
Hide file tree
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 Mar 31, 2026
9cf187f
feat: add role export handler class
omaraelhawary Mar 31, 2026
aff09c4
feat: add role import handler class
omaraelhawary Mar 31, 2026
fea85c4
feat: integrate role import and export into the admin roles page
omaraelhawary Mar 31, 2026
fcd93db
fix: initialize new settings in Role_Import class
omaraelhawary Mar 31, 2026
9fb9f00
refactor: remove redundant role update logic in Role_Import class
omaraelhawary Mar 31, 2026
10056aa
refactor: sanitize non-string values in Role_Import class settings
omaraelhawary Mar 31, 2026
b0d43f2
feat: enhance role import validation and prevent accidental overwrites
omaraelhawary Mar 31, 2026
a105a81
feat: add role existence check during import process
omaraelhawary Mar 31, 2026
39c96dd
fix: improve role overwrite prevention logic in Role_Import class
omaraelhawary Apr 1, 2026
f894531
fix: enhance role overwrite prevention and sanitize non-string settin…
omaraelhawary Apr 1, 2026
84abefc
refactor: remove unused global variable reference in Role_Import class
omaraelhawary Apr 1, 2026
09059be
fix: enhance role protection during import process
omaraelhawary Apr 1, 2026
180539d
fix: address security and correctness issues in role import/export
omaraelhawary Apr 1, 2026
1f7dc06
refactor: improve UI interaction and structure in roles management
omaraelhawary Apr 1, 2026
72630e2
feat: implement import/export functionality in roles management
omaraelhawary Apr 1, 2026
4d81312
fix: improve role label sanitization and protection logic during import
omaraelhawary Apr 1, 2026
2732c9c
fix: handle JSON read errors during role import process
omaraelhawary Apr 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions admin/class-role-export.php
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();
Loading