PluginProbe
Members – Membership & User Role Editor Plugin / 3.2.24
Members – Membership & User Role Editor Plugin v3.2.24
3.2.25 3.2.26 3.2.24 3.2.23 3.2.22 3.2.21 trunk 0.1 0.1.1 0.2 0.2.1 0.2.2 0.2.3 0.2.4 0.2.5 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 2.0.0 2.0.1 2.0.2 All 66 releases
members / admin / class-role-export.php

class-role-export.php in Members – Membership & User Role Editor Plugin 3.2.24, at admin/class-role-export.php

159 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles role export.
4 *
5 * @package Members
6 * @subpackage Admin
7 * @author The MemberPress Team
8 * @copyright Copyright (c) 2009 - 2018, The MemberPress Team
9 * @link https://members-plugin.com/
10 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11 */
12 namespace Members\Admin;
13
14 defined('ABSPATH') || exit;
15
16 /**
17 * Class that handles exporting roles and settings to a JSON file.
18 *
19 * @since 3.3.0
20 * @access public
21 */
22 final class Role_Export {
23
24 /**
25 * Schema version written to the export JSON `meta.schema_version` field.
26 *
27 * @since 3.4.0
28 */
29 const SCHEMA_VERSION = 1;
30
31 /**
32 * Holds the instances of this class.
33 *
34 * @since 3.3.0
35 * @access private
36 * @var object
37 */
38 private static $instance;
39
40 /**
41 * Sets up initial actions.
42 *
43 * @since 3.3.0
44 * @access public
45 * @return void
46 */
47 public function __construct() {
48
49 add_action( 'admin_post_members_export_roles', array( $this, 'handle_export' ) );
50 }
51
52 /**
53 * Handles the "Export All" request via the header button.
54 *
55 * @since 3.3.0
56 * @access public
57 * @return void
58 */
59 public function handle_export() {
60
61 check_admin_referer( 'members_export_roles' );
62
63 if ( ! current_user_can( 'list_roles' ) ) {
64 wp_die( esc_html__( 'You do not have permission to export roles.', 'members' ) );
65 }
66
67 $this->generate_export();
68 }
69
70 /**
71 * Handles a selective bulk export. Called from Roles::load().
72 *
73 * @since 3.4.0
74 * @access public
75 * @param array $role_slugs Array of sanitized role slugs to export.
76 * @return void
77 */
78 public function handle_selective_export( $role_slugs ) {
79
80 if ( ! current_user_can( 'list_roles' ) ) {
81 wp_die( esc_html__( 'You do not have permission to export roles.', 'members' ) );
82 }
83
84 $this->generate_export( $role_slugs );
85 }
86
87 /**
88 * Generates and sends the JSON export file as a download.
89 *
90 * @since 3.4.0
91 * @access private
92 * @param array $role_slugs Array of role slugs to export. Empty means all roles.
93 * @return void
94 */
95 private function generate_export( $role_slugs = array() ) {
96
97 global $wp_roles;
98
99 // Build roles array.
100 $roles = array();
101
102 foreach ( $wp_roles->roles as $slug => $role_data ) {
103
104 if ( ! empty( $role_slugs ) && ! in_array( $slug, $role_slugs, true ) ) {
105 continue;
106 }
107
108 $roles[ $slug ] = array(
109 'name' => $slug,
110 'label' => $role_data['name'],
111 'capabilities' => $role_data['capabilities'],
112 );
113 }
114
115 $file_headers = get_file_data( members_plugin()->dir . 'members.php', array( 'Version' => 'Version' ) );
116
117 // Build export data. Only include settings for full (non-selective) exports.
118 $data = array(
119 'meta' => array(
120 'plugin' => 'members',
121 'schema_version' => self::SCHEMA_VERSION,
122 'version' => ! empty( $file_headers['Version'] ) ? $file_headers['Version'] : '',
123 'export_date' => gmdate( 'c' ),
124 'site_url' => site_url(),
125 'wp_version' => get_bloginfo( 'version' ),
126 ),
127 'roles' => $roles,
128 'settings' => empty( $role_slugs ) ? get_option( 'members_settings', members_get_default_settings() ) : array(),
129 );
130
131 // Send file download.
132 $filename = 'members-roles-export-' . gmdate( 'Y-m-d-His' ) . '.json';
133
134 nocache_headers();
135 header( 'Content-Type: application/json; charset=utf-8' );
136 header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
137
138 echo wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE );
139 exit;
140 }
141
142 /**
143 * Returns the instance.
144 *
145 * @since 3.3.0
146 * @access public
147 * @return object
148 */
149 public static function get_instance() {
150
151 if ( ! self::$instance )
152 self::$instance = new self;
153
154 return self::$instance;
155 }
156 }
157
158 Role_Export::get_instance();
159