Нет описания

role-edit.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php if ( ! defined( 'ABSPATH' ) ) {
  2. exit;
  3. }
  4. wp_enqueue_script( 'postbox' );
  5. wp_enqueue_media();
  6. /**
  7. * UM hook
  8. *
  9. * @type action
  10. * @title um_roles_add_meta_boxes
  11. * @description Add meta boxes on add/edit UM Role
  12. * @input_vars
  13. * [{"var":"$meta","type":"string","desc":"Meta Box Key"}]
  14. * @change_log
  15. * ["Since: 2.0"]
  16. * @usage add_action( 'um_roles_add_meta_boxes', 'function_name', 10, 1 );
  17. * @example
  18. * <?php
  19. * add_action( 'um_roles_add_meta_boxes', 'my_roles_add_meta_boxes', 10, 1 );
  20. * function my_roles_add_meta_boxes( $meta ) {
  21. * // your code here
  22. * }
  23. * ?>
  24. */
  25. do_action( 'um_roles_add_meta_boxes', 'um_role_meta' );
  26. /**
  27. * UM hook
  28. *
  29. * @type action
  30. * @title um_roles_add_meta_boxes_um_role_meta
  31. * @description Make add meta boxes on add/edit UM Role
  32. * @change_log
  33. * ["Since: 2.0"]
  34. * @usage add_action( 'um_roles_add_meta_boxes_um_role_meta', 'function_name', 10 );
  35. * @example
  36. * <?php
  37. * add_action( 'um_roles_add_meta_boxes_um_role_meta', 'my_roles_add_meta_boxes', 10 );
  38. * function my_roles_add_meta_boxes() {
  39. * // your code here
  40. * }
  41. * ?>
  42. */
  43. do_action( 'um_roles_add_meta_boxes_um_role_meta' );
  44. $data = array();
  45. $option = array();
  46. global $wp_roles;
  47. if ( ! empty( $_GET['id'] ) ) {
  48. // uses sanitize_title instead of sanitize_key for backward compatibility based on #906 pull-request (https://github.com/ultimatemember/ultimatemember/pull/906)
  49. // roles e.g. "潜水艦subs" with both latin + not-UTB-8 symbols had invalid role ID
  50. $role_id = sanitize_title( $_GET['id'] );
  51. $data = get_option( "um_role_{$role_id}_meta" );
  52. if ( empty( $data['_um_is_custom'] ) ) {
  53. $data['name'] = $wp_roles->roles[ $role_id ]['name'];
  54. }
  55. }
  56. if ( ! empty( $_POST['role'] ) ) {
  57. $id = '';
  58. $redirect = '';
  59. $error = '';
  60. if ( 'add' === sanitize_key( $_GET['tab'] ) ) {
  61. if ( ! wp_verify_nonce( $_POST['um_nonce'], 'um-add-role' ) ) {
  62. $error = __( 'Security Issue', 'ultimate-member' ) . '<br />';
  63. }
  64. } else {
  65. if ( ! wp_verify_nonce( $_POST['um_nonce'], 'um-edit-role' ) ) {
  66. $error = __( 'Security Issue', 'ultimate-member' ) . '<br />';
  67. }
  68. }
  69. if ( empty( $error ) ) {
  70. $data = UM()->admin()->sanitize_role_meta( $_POST['role'] );
  71. if ( 'add' === sanitize_key( $_GET['tab'] ) ) {
  72. $data['name'] = trim( esc_html( strip_tags( $data['name'] ) ) );
  73. if ( empty( $data['name'] ) ) {
  74. $error .= __( 'Title is empty!', 'ultimate-member' ) . '<br />';
  75. }
  76. if ( preg_match( "/^[\p{Latin}\d\-_ ]+$/i", $data['name'] ) ) {
  77. // uses sanitize_title instead of sanitize_key for backward compatibility based on #906 pull-request (https://github.com/ultimatemember/ultimatemember/pull/906)
  78. // roles e.g. "潜水艦subs" with both latin + not-UTB-8 symbols had invalid role ID
  79. $id = sanitize_title( $data['name'] );
  80. } else {
  81. $auto_increment = UM()->options()->get( 'custom_roles_increment' );
  82. $auto_increment = ! empty( $auto_increment ) ? $auto_increment : 1;
  83. $id = 'custom_role_' . $auto_increment;
  84. }
  85. $redirect = add_query_arg( array( 'page' => 'um_roles', 'tab' => 'edit', 'id' => $id, 'msg' => 'a' ), admin_url( 'admin.php' ) );
  86. } elseif ( 'edit' === sanitize_key( $_GET['tab'] ) && ! empty( $_GET['id'] ) ) {
  87. // uses sanitize_title instead of sanitize_key for backward compatibility based on #906 pull-request (https://github.com/ultimatemember/ultimatemember/pull/906)
  88. // roles e.g. "潜水艦subs" with both latin + not-UTB-8 symbols had invalid role ID
  89. $id = sanitize_title( $_GET['id'] );
  90. $pre_role_meta = get_option( "um_role_{$id}_meta", array() );
  91. if ( isset( $pre_role_meta['name'] ) ) {
  92. $data['name'] = $pre_role_meta['name'];
  93. }
  94. $redirect = add_query_arg( array( 'page' => 'um_roles', 'tab' => 'edit', 'id' => $id, 'msg'=> 'u' ), admin_url( 'admin.php' ) );
  95. }
  96. $all_roles = array_keys( get_editable_roles() );
  97. if ( 'add' === sanitize_key( $_GET['tab'] ) ) {
  98. if ( in_array( 'um_' . $id, $all_roles, true ) || in_array( $id, $all_roles, true ) ) {
  99. $error .= __( 'Role already exists!', 'ultimate-member' ) . '<br />';
  100. }
  101. }
  102. if ( '' === $error ) {
  103. if ( 'add' === sanitize_key( $_GET['tab'] ) ) {
  104. $roles = get_option( 'um_roles', array() );
  105. $roles[] = $id;
  106. update_option( 'um_roles', $roles );
  107. if ( isset( $auto_increment ) ) {
  108. $auto_increment++;
  109. UM()->options()->update( 'custom_roles_increment', $auto_increment );
  110. }
  111. }
  112. $role_meta = $data;
  113. unset( $role_meta['id'] );
  114. update_option( "um_role_{$id}_meta", $role_meta );
  115. UM()->user()->remove_cache_all_users();
  116. um_js_redirect( $redirect );
  117. }
  118. }
  119. }
  120. global $current_screen;
  121. $screen_id = $current_screen->id; ?>
  122. <script type="text/javascript">
  123. jQuery( document ).ready( function() {
  124. postboxes.add_postbox_toggles( '<?php echo esc_js( $screen_id ); ?>' );
  125. });
  126. </script>
  127. <div class="wrap">
  128. <h2>
  129. <?php
  130. if ( 'add' === sanitize_key( $_GET['tab'] ) ) {
  131. esc_html_e( 'Add New Role', 'ultimate-member' );
  132. } elseif ( 'edit' === sanitize_key( $_GET['tab'] ) ) {
  133. esc_html_e( 'Edit Role', 'ultimate-member' );
  134. $add_new_link = add_query_arg(
  135. array(
  136. 'page' => 'um_roles',
  137. 'tab' => 'add',
  138. ),
  139. admin_url( 'admin.php' )
  140. );
  141. ?>
  142. <a class="add-new-h2" href="<?php echo esc_url( $add_new_link ); ?>">
  143. <?php esc_html_e( 'Add New', 'ultimate-member' ); ?>
  144. </a>
  145. <?php
  146. }
  147. ?>
  148. </h2>
  149. <?php
  150. if ( ! empty( $_GET['msg'] ) ) {
  151. switch ( sanitize_key( $_GET['msg'] ) ) {
  152. case 'a':
  153. echo '<div id="message" class="updated fade"><p>' . __( 'User Role <strong>Added</strong> Successfully.', 'ultimate-member' ) . '</p></div>';
  154. break;
  155. case 'u':
  156. echo '<div id="message" class="updated fade"><p>' . __( 'User Role <strong>Updated</strong> Successfully.', 'ultimate-member' ) . '</p></div>';
  157. break;
  158. }
  159. }
  160. if ( ! empty( $error ) ) { ?>
  161. <div id="message" class="error fade">
  162. <p><?php echo $error; ?></p>
  163. </div>
  164. <?php } ?>
  165. <form id="um_edit_role" action="" method="post">
  166. <input type="hidden" name="role[id]" value="<?php echo isset( $_GET['id'] ) ? esc_attr( sanitize_key( $_GET['id'] ) ) : '' ?>" />
  167. <?php if ( 'add' === sanitize_key( $_GET['tab'] ) ) { ?>
  168. <input type="hidden" name="role[_um_is_custom]" value="1" />
  169. <input type="hidden" name="um_nonce" value="<?php echo esc_attr( wp_create_nonce( 'um-add-role' ) ) ?>" />
  170. <?php } else { ?>
  171. <input type="hidden" name="role[_um_is_custom]" value="<?php echo ! empty( $data['_um_is_custom'] ) ? 1 : 0 ?>" />
  172. <input type="hidden" name="um_nonce" value="<?php echo esc_attr( wp_create_nonce( 'um-edit-role' ) ) ?>" />
  173. <?php } ?>
  174. <?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
  175. <div id="poststuff">
  176. <div id="post-body" class="metabox-holder columns-2">
  177. <div id="post-body-content">
  178. <div id="titlediv">
  179. <div id="titlewrap">
  180. <?php if ( 'add' === sanitize_key( $_GET['tab'] ) ) { ?>
  181. <label for="title" class="screen-reader-text"><?php _e( 'Title', 'ultimate-member' ) ?></label>
  182. <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'] : '' ?>" />
  183. <?php } else { ?>
  184. <span style="float: left;width:100%;"><?php echo isset( $data['name'] ) ? stripslashes( $data['name'] ) : '' ?></span>
  185. <?php } ?>
  186. </div>
  187. </div>
  188. </div>
  189. <?php
  190. $object = array(
  191. 'data' => $data,
  192. 'option' => $option,
  193. );
  194. ?>
  195. <div id="postbox-container-1" class="postbox-container">
  196. <?php do_meta_boxes( 'um_role_meta', 'side', $object ); ?>
  197. </div>
  198. <div id="postbox-container-2" class="postbox-container">
  199. <?php do_meta_boxes( 'um_role_meta', 'normal', $object ); ?>
  200. </div>
  201. </div>
  202. </div>
  203. </form>
  204. </div>