2021-06-29 02:51:54 +03:00
< ? php if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
2017-07-26 14:57:52 +03:00
wp_enqueue_script ( 'postbox' );
2018-03-05 16:35:51 +02:00
wp_enqueue_media ();
2017-07-26 14:57:52 +03:00
2018-03-05 16:35:51 +02:00
/**
* UM hook
*
* @type action
* @title um_roles_add_meta_boxes
* @description Add meta boxes on add/edit UM Role
* @input_vars
* [{"var":"$meta","type":"string","desc":"Meta Box Key"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_roles_add_meta_boxes', 'function_name', 10, 1 );
* @example
* <?php
* add_action( 'um_roles_add_meta_boxes', 'my_roles_add_meta_boxes', 10, 1 );
* function my_roles_add_meta_boxes( $meta ) {
* // your code here
* }
* ?>
*/
do_action ( 'um_roles_add_meta_boxes' , 'um_role_meta' );
/**
* UM hook
*
* @type action
* @title um_roles_add_meta_boxes_um_role_meta
* @description Make add meta boxes on add/edit UM Role
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_roles_add_meta_boxes_um_role_meta', 'function_name', 10 );
* @example
* <?php
* add_action( 'um_roles_add_meta_boxes_um_role_meta', 'my_roles_add_meta_boxes', 10 );
* function my_roles_add_meta_boxes() {
* // your code here
* }
* ?>
*/
do_action ( 'um_roles_add_meta_boxes_um_role_meta' );
2017-07-26 14:57:52 +03:00
2021-06-29 02:51:54 +03:00
$data = array ();
2017-07-26 14:57:52 +03:00
$option = array ();
global $wp_roles ;
if ( ! empty ( $_GET [ 'id' ] ) ) {
2020-02-12 12:36:47 +02:00
2021-09-13 17:02:16 +03:00
// uses sanitize_title instead of sanitize_key for backward compatibility based on #906 pull-request (https://github.com/ultimatemember/ultimatemember/pull/906)
// roles e.g. "潜水艦subs" with both latin + not-UTB-8 symbols had invalid role ID
$role_id = sanitize_title ( $_GET [ 'id' ] );
2020-02-12 12:36:47 +02:00
$data = get_option ( " um_role_ { $role_id } _meta " );
2017-07-26 14:57:52 +03:00
2019-08-08 00:36:33 +03:00
if ( empty ( $data [ '_um_is_custom' ] ) ) {
2020-02-12 12:36:47 +02:00
$data [ 'name' ] = $wp_roles -> roles [ $role_id ][ 'name' ];
2019-08-08 00:36:33 +03:00
}
2017-07-26 14:57:52 +03:00
}
if ( ! empty ( $_POST [ 'role' ] ) ) {
2021-06-29 02:51:54 +03:00
$id = '' ;
2018-03-19 16:31:49 +02:00
$redirect = '' ;
2021-06-29 02:51:54 +03:00
$error = '' ;
2017-07-26 14:57:52 +03:00
2021-06-29 02:51:54 +03:00
if ( 'add' === sanitize_key ( $_GET [ 'tab' ] ) ) {
2019-07-09 18:41:40 +03:00
if ( ! wp_verify_nonce ( $_POST [ 'um_nonce' ], 'um-add-role' ) ) {
$error = __ ( 'Security Issue' , 'ultimate-member' ) . '<br />' ;
}
} else {
if ( ! wp_verify_nonce ( $_POST [ 'um_nonce' ], 'um-edit-role' ) ) {
$error = __ ( 'Security Issue' , 'ultimate-member' ) . '<br />' ;
}
}
2017-07-26 14:57:52 +03:00
2019-07-09 18:41:40 +03:00
if ( empty ( $error ) ) {
2017-07-26 14:57:52 +03:00
2021-06-29 02:51:54 +03:00
$data = UM () -> admin () -> sanitize_role_meta ( $_POST [ 'role' ] );
2021-06-17 00:57:25 +03:00
2021-06-29 02:51:54 +03:00
if ( 'add' === sanitize_key ( $_GET [ 'tab' ] ) ) {
2018-02-12 15:37:58 +02:00
2019-07-11 18:50:41 +03:00
$data [ 'name' ] = trim ( esc_html ( strip_tags ( $data [ 'name' ] ) ) );
2018-02-12 15:37:58 +02:00
2019-07-11 18:50:41 +03:00
if ( empty ( $data [ 'name' ] ) ) {
$error .= __ ( 'Title is empty!' , 'ultimate-member' ) . '<br />' ;
}
2017-07-26 14:57:52 +03:00
2021-09-13 17:28:50 +03:00
if ( preg_match ( " /^[ \ p { Latin} \ d \ -_ ]+ $ /i " , $data [ 'name' ] ) ) {
2021-09-14 18:21:41 +03:00
// uses sanitize_title instead of sanitize_key for backward compatibility based on #906 pull-request (https://github.com/ultimatemember/ultimatemember/pull/906)
// roles e.g. "潜水艦subs" with both latin + not-UTB-8 symbols had invalid role ID
2019-07-11 18:50:41 +03:00
$id = sanitize_title ( $data [ 'name' ] );
} else {
$auto_increment = UM () -> options () -> get ( 'custom_roles_increment' );
$auto_increment = ! empty ( $auto_increment ) ? $auto_increment : 1 ;
2021-06-29 02:51:54 +03:00
$id = 'custom_role_' . $auto_increment ;
2019-07-11 18:50:41 +03:00
}
2017-07-26 14:57:52 +03:00
2021-09-13 17:02:16 +03:00
$redirect = add_query_arg ( array ( 'page' => 'um_roles' , 'tab' => 'edit' , 'id' => $id , 'msg' => 'a' ), admin_url ( 'admin.php' ) );
2021-06-29 02:51:54 +03:00
} elseif ( 'edit' === sanitize_key ( $_GET [ 'tab' ] ) && ! empty ( $_GET [ 'id' ] ) ) {
2021-09-13 17:02:16 +03:00
// uses sanitize_title instead of sanitize_key for backward compatibility based on #906 pull-request (https://github.com/ultimatemember/ultimatemember/pull/906)
// roles e.g. "潜水艦subs" with both latin + not-UTB-8 symbols had invalid role ID
$id = sanitize_title ( $_GET [ 'id' ] );
2017-07-26 14:57:52 +03:00
2019-07-11 18:50:41 +03:00
$pre_role_meta = get_option ( " um_role_ { $id } _meta " , array () );
2019-07-17 12:44:39 +03:00
if ( isset ( $pre_role_meta [ 'name' ] ) ) {
$data [ 'name' ] = $pre_role_meta [ 'name' ];
}
2019-07-09 18:41:40 +03:00
2019-07-17 12:44:39 +03:00
$redirect = add_query_arg ( array ( 'page' => 'um_roles' , 'tab' => 'edit' , 'id' => $id , 'msg' => 'u' ), admin_url ( 'admin.php' ) );
2019-07-09 18:41:40 +03:00
}
2017-07-26 14:57:52 +03:00
2019-07-11 18:50:41 +03:00
2019-07-09 18:41:40 +03:00
$all_roles = array_keys ( get_editable_roles () );
2021-06-29 02:51:54 +03:00
if ( 'add' === sanitize_key ( $_GET [ 'tab' ] ) ) {
if ( in_array ( 'um_' . $id , $all_roles , true ) || in_array ( $id , $all_roles , true ) ) {
2019-07-09 18:41:40 +03:00
$error .= __ ( 'Role already exists!' , 'ultimate-member' ) . '<br />' ;
2019-07-11 18:50:41 +03:00
}
2019-07-09 18:41:40 +03:00
}
2021-06-29 02:51:54 +03:00
if ( '' === $error ) {
2019-07-09 18:41:40 +03:00
2021-06-29 02:51:54 +03:00
if ( 'add' === sanitize_key ( $_GET [ 'tab' ] ) ) {
$roles = get_option ( 'um_roles' , array () );
2019-07-09 18:41:40 +03:00
$roles [] = $id ;
2017-07-26 14:57:52 +03:00
2019-07-09 18:41:40 +03:00
update_option ( 'um_roles' , $roles );
2018-02-12 15:37:58 +02:00
2019-07-09 18:41:40 +03:00
if ( isset ( $auto_increment ) ) {
$auto_increment ++ ;
UM () -> options () -> update ( 'custom_roles_increment' , $auto_increment );
}
2018-03-19 16:31:49 +02:00
}
2017-07-26 14:57:52 +03:00
2023-06-05 19:25:57 +03:00
$role_meta = apply_filters ( 'um_role_edit_data' , $data , $id );
2019-07-09 18:41:40 +03:00
unset ( $role_meta [ 'id' ] );
2017-07-26 14:57:52 +03:00
2019-07-09 18:41:40 +03:00
update_option ( " um_role_ { $id } _meta " , $role_meta );
2017-07-26 14:57:52 +03:00
2019-07-09 18:41:40 +03:00
UM () -> user () -> remove_cache_all_users ();
2018-04-05 14:38:20 +03:00
2019-07-09 18:41:40 +03:00
um_js_redirect ( $redirect );
}
2018-03-19 16:31:49 +02:00
}
2017-07-26 14:57:52 +03:00
}
global $current_screen ;
$screen_id = $current_screen -> id ; ?>
<script type="text/javascript">
2018-03-19 16:31:49 +02:00
jQuery( document ).ready( function() {
2019-08-08 00:36:33 +03:00
postboxes.add_postbox_toggles( '<?php echo esc_js( $screen_id ); ?>' );
2018-03-19 16:31:49 +02:00
});
2017-07-26 14:57:52 +03:00
</script>
<div class="wrap">
2018-03-19 16:31:49 +02:00
<h2>
2021-06-29 02:51:54 +03:00
<?php
if ( 'add' === sanitize_key( $_GET['tab'] ) ) {
esc_html_e( 'Add New Role', 'ultimate-member' );
} elseif ( 'edit' === sanitize_key( $_GET['tab'] ) ) {
esc_html_e( 'Edit Role', 'ultimate-member' );
$add_new_link = add_query_arg(
array(
'page' => 'um_roles',
'tab' => 'add',
),
admin_url( 'admin.php' )
);
?>
<a class="add-new-h2" href="<?php echo esc_url( $add_new_link ); ?>">
<?php esc_html_e( 'Add New', 'ultimate-member' ); ?>
</a>
<?php
}
?>
2018-03-19 16:31:49 +02:00
</h2>
2021-06-29 02:51:54 +03:00
<?php
if ( ! empty( $_GET['msg'] ) ) {
switch ( sanitize_key( $_GET['msg'] ) ) {
2018-03-19 16:31:49 +02:00
case 'a':
echo '<div id="message" class="updated fade"><p>' . __( 'User Role <strong>Added</strong> Successfully.', 'ultimate-member' ) . '</p></div>';
break;
case 'u':
echo '<div id="message" class="updated fade"><p>' . __( 'User Role <strong>Updated</strong> Successfully.', 'ultimate-member' ) . '</p></div>';
break;
}
}
if ( ! empty( $error ) ) { ?>
<div id="message" class="error fade">
2021-06-29 02:51:54 +03:00
<p><?php echo $error; ?></p>
2018-03-19 16:31:49 +02:00
</div>
<?php } ?>
<form id="um_edit_role" action="" method="post">
2020-02-12 12:36:47 +02:00
<input type="hidden" name="role[id]" value="<?php echo isset( $_GET['id'] ) ? esc_attr( sanitize_key( $_GET['id'] ) ) : '' ?>" />
2021-06-29 02:51:54 +03:00
<?php if ( 'add' === sanitize_key( $_GET['tab'] ) ) { ?>
2018-03-19 16:31:49 +02:00
<input type="hidden" name="role[_um_is_custom]" value="1" />
2019-08-08 00:36:33 +03:00
<input type="hidden" name="um_nonce" value="<?php echo esc_attr( wp_create_nonce( 'um-add-role' ) ) ?>" />
2018-03-19 16:31:49 +02:00
<?php } else { ?>
<input type="hidden" name="role[_um_is_custom]" value="<?php echo ! empty( $data['_um_is_custom'] ) ? 1 : 0 ?>" />
2019-08-08 00:36:33 +03:00
<input type="hidden" name="um_nonce" value="<?php echo esc_attr( wp_create_nonce( 'um-edit-role' ) ) ?>" />
2018-03-19 16:31:49 +02:00
<?php } ?>
<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<div id="post-body-content">
<div id="titlediv">
<div id="titlewrap">
2021-06-29 02:51:54 +03:00
<?php if ( 'add' === sanitize_key( $_GET['tab'] ) ) { ?>
2018-03-19 16:31:49 +02:00
<label for="title" class="screen-reader-text"><?php _e( 'Title', 'ultimate-member' ) ?></label>
2019-08-08 00:36:33 +03:00
<input type="text" name="role[name]" placeholder="<?php esc_attr_e( 'Enter Title Here', 'ultimate-member' ) ?>" id="title" value="<?php echo isset( $data['name'] ) ? $data['name'] : '' ?>" />
2018-03-19 16:31:49 +02:00
<?php } else { ?>
2020-08-26 23:47:26 +03:00
<span style="float: left;width:100%;"><?php echo isset( $data['name'] ) ? stripslashes( $data['name'] ) : '' ?></span>
2018-03-19 16:31:49 +02:00
<?php } ?>
</div>
</div>
</div>
2021-06-29 02:51:54 +03:00
<?php
$object = array(
'data' => $data,
'option' => $option,
);
?>
2018-03-19 16:31:49 +02:00
<div id="postbox-container-1" class="postbox-container">
2021-06-29 02:51:54 +03:00
<?php do_meta_boxes( 'um_role_meta', 'side', $object ); ?>
2018-03-19 16:31:49 +02:00
</div>
<div id="postbox-container-2" class="postbox-container">
2021-06-29 02:51:54 +03:00
<?php do_meta_boxes( 'um_role_meta', 'normal', $object ); ?>
2018-03-19 16:31:49 +02:00
</div>
</div>
</div>
</form>
2021-06-17 00:57:25 +03:00
</div>