acf_field_role_selector

The Advanced Custom Fields: User Role Selector acf field role selector class.

Defined (2)

The class is defined in the following location(s).

/role_selector-v4.php  
  1. class acf_field_role_selector extends acf_field { 
  2.  
  3. var $settings,  
  4. $defaults; 
  5.  
  6.  
  7. /** 
  8. * __construct 
  9. * Set name / label needed for actions / filters 
  10. * @since 3.6 
  11. * @date 23/01/13 
  12. */ 
  13.  
  14. function __construct() { 
  15.  
  16. $this->name = 'role_selector'; 
  17. $this->label = __( 'User Role Selector', 'acf' ); 
  18. $this->category = __( 'Relational', 'acf' ); 
  19. $this->defaults = array( 
  20. 'return_value' => 'name',  
  21. 'field_type' => 'checkbox',  
  22. ); 
  23.  
  24. parent::__construct(); 
  25.  
  26. $this->settings = array( 
  27. 'path' => apply_filters( 'acf/helpers/get_path', __FILE__ ),  
  28. 'dir' => apply_filters( 'acf/helpers/get_dir', __FILE__ ),  
  29. 'version' => '1.0.0' 
  30. ); 
  31.  
  32.  
  33.  
  34. /** 
  35. * create_options() 
  36. * Create extra options for your field. This is rendered when editing a field. 
  37. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  38. * @type action 
  39. * @since 3.6 
  40. * @date 23/01/13 
  41. * @param $field - an array holding all the field's data 
  42. */ 
  43.  
  44. function create_options( $field ) { 
  45.  
  46. $field = array_merge( $this->defaults, $field ); 
  47. $key = $field['name']; 
  48.  
  49. ?> 
  50. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  51. <td class="label"> 
  52. <label><?php _e( 'Return Format', 'acf' ); ?></label> 
  53. <p class="description"><?php _e( 'Specify the returned value on front end', 'acf' ); ?></p> 
  54. </td> 
  55. <td> 
  56. <?php 
  57.  
  58. do_action( 'acf/create_field', array( 
  59. 'type' => 'radio',  
  60. 'name' => 'fields[' . $key . '][return_value]',  
  61. 'value' => $field['return_value'],  
  62. 'layout' => 'horizontal',  
  63. 'choices' => array( 
  64. 'name' => __( 'Role Name', 'acf' ),  
  65. 'object' => __( 'Role Object', 'acf' ),  
  66. )); 
  67.  
  68. ?> 
  69. </td> 
  70. </tr> 
  71.  
  72. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  73. <td class="label"> 
  74. <label><?php _e( 'Field Type', 'acf' ); ?></label> 
  75. </td> 
  76. <td> 
  77. <?php 
  78.  
  79. do_action('acf/create_field', array( 
  80. 'type' => 'select',  
  81. 'name' => 'fields[' . $key . '][field_type]',  
  82. 'value' => $field['field_type'],  
  83. 'choices' => array( 
  84. __( 'Multiple Values', 'acf' ) => array( 
  85. 'checkbox' => __( 'Checkbox', 'acf' ),  
  86. 'multi_select' => __( 'Multi Select', 'acf' ) 
  87. ),  
  88. __( 'Single Value', 'acf' ) => array( 
  89. 'radio' => __( 'Radio Buttons', 'acf' ),  
  90. 'select' => __( 'Select', 'acf' ) 
  91. )); 
  92.  
  93. ?> 
  94. </td> 
  95. </tr> 
  96. <?php 
  97.  
  98.  
  99.  
  100. /** 
  101. * create_field() 
  102. * Create the HTML interface for your field 
  103. * @param $field - an array holding all the field's data 
  104. * @type action 
  105. * @since 3.6 
  106. * @date 23/01/13 
  107. */ 
  108.  
  109. function create_field( $field ) { 
  110. global $wp_roles; 
  111. $all_roles = $wp_roles->roles; 
  112.  
  113. $selected_roles = array(); 
  114. if( !empty( $field['value'] ) && 'object' == $field['return_value'] ) { 
  115. foreach( $field['value'] as $value ) { 
  116. $selected_roles[] = $value->name; 
  117. else { 
  118. $selected_roles = $field['value']; 
  119.  
  120.  
  121. if( $field['field_type'] == 'select' || $field['field_type'] == 'multi_select' ) : 
  122. $multiple = ( $field['field_type'] == 'multi_select' ) ? 'multiple="multiple"' : ''; 
  123. ?> 
  124.  
  125. <select name='<?php echo $field['name'] ?>[]' <?php echo $multiple ?>> 
  126. <?php 
  127. foreach( $all_roles as $role => $data ) : 
  128. $selected = ( in_array( $role, $selected_roles ) ) ? 'selected="selected"' : ''; 
  129. ?> 
  130. <option <?php echo $selected ?> value='<?php echo $role ?>'><?php echo $data['name'] ?></option> 
  131. <?php endforeach; ?> 
  132.  
  133. </select> 
  134. <?php 
  135. elseif( $field['field_type'] == 'radio' ) : 
  136. echo '<ul class="acf-radio-list radio vertical">'; 
  137. foreach( $all_roles as $role => $data ) : 
  138. $checked = ( in_array( $role, $selected_roles ) ) ? 'checked="checked"' : ''; 
  139. ?> 
  140. <label><input <?php echo $checked ?> type="radio" name="<?php echo $field['name'] ?>" value="<?php echo $role ?>"><?php echo $data['name'] ?></label> 
  141. <?php 
  142. endforeach; 
  143. echo '</ul>'; 
  144. else : 
  145. echo '<ul class="acf-checkbox-list checkbox vertical">'; 
  146. foreach( $all_roles as $role => $data ) : 
  147. $checked = ( in_array( $role, $selected_roles ) ) ? 'checked="checked"' : ''; 
  148. ?> 
  149. <li><label><input <?php echo $checked ?> type="checkbox" class="checkbox" name="<?php echo $field['name'] ?>[]" value="<?php echo $role ?>"><?php echo $data['name'] ?></label></li> 
  150. <?php 
  151. endforeach; 
  152. echo '</ul>'; 
  153. endif; 
  154.  
  155.  
  156. /** 
  157. * format_value() 
  158. * This filter is appied to the $value after it is loaded from the db and before it is passed to the create_field action 
  159. * @type filter 
  160. * @since 3.6 
  161. * @date 23/01/13 
  162. * @param $value - the value which was loaded from the database 
  163. * @param $post_id - the $post_id from which the value was loaded 
  164. * @param $field - the field array holding all the field options 
  165. * @return $value - the modified value 
  166. */ 
  167.  
  168. function format_value($value, $post_id, $field) 
  169. if( $field['return_value'] == 'object' ) 
  170. foreach( $value as $key => $name ) { 
  171. $value[$key] = get_role( $name ); 
  172. return $value; 
  173.  
  174.  
  175. /** 
  176. * format_value_for_api() 
  177. * This filter is appied to the $value after it is loaded from the db and before it is passed back to the api functions such as the_field 
  178. * @type filter 
  179. * @since 3.6 
  180. * @date 23/01/13 
  181. * @param $value - the value which was loaded from the database 
  182. * @param $post_id - the $post_id from which the value was loaded 
  183. * @param $field - the field array holding all the field options 
  184. * @return $value - the modified value 
  185. */ 
  186.  
  187. function format_value_for_api($value, $post_id, $field) 
  188.  
  189. // format 
  190. if( $field['return_value'] == 'object' ) 
  191. foreach( $value as $key => $name ) { 
  192. $value[$key] = get_role( $name ); 
  193.  
  194. return $value; 
  195.  
  196.  
/role_selector-v3.php  
  1. class acf_field_role_selector extends acf_Field 
  2.  
  3. var $settings,  
  4. $defaults; 
  5.  
  6.  
  7. /**-------------------------------------------------------------------------------------- 
  8. * Constructor 
  9. * - This function is called when the field class is initalized on each page. 
  10. * - Here you can add filters / actions and setup any other functionality for your field 
  11. * @author Elliot Condon 
  12. * @since 2.2.0 
  13. *-------------------------------------------------------------------------------------*/ 
  14.  
  15. function __construct($parent) { 
  16.  
  17. parent::__construct($parent); 
  18.  
  19. $this->name = 'role_selector'; 
  20. $this->title = __( 'User Role Selector', 'acf' ); 
  21. $this->defaults = array( 
  22. 'return_value' => 'name',  
  23. 'field_type' => 'checkbox',  
  24. ); 
  25.  
  26. $this->settings = array( 
  27. 'path' => $this->helpers_get_path(__FILE__),  
  28. 'dir' => $this->helpers_get_dir(__FILE__),  
  29. 'version' => '1.0.0' 
  30. ); 
  31.  
  32.  
  33.  
  34. /** 
  35. * helpers_get_path 
  36. * @description: calculates the path (works for plugin / theme folders) 
  37. * @since: 3.6 
  38. * @created: 30/01/13 
  39. */ 
  40.  
  41. function helpers_get_path($file) { 
  42. return trailingslashit(dirname($file)); 
  43.  
  44.  
  45. /** 
  46. * helpers_get_dir 
  47. * @description: calculates the directory (works for plugin / theme folders) 
  48. * @since: 3.6 
  49. * @created: 30/01/13 
  50. */ 
  51.  
  52. function helpers_get_dir($file) 
  53. $dir = trailingslashit(dirname($file)); 
  54. $count = 0; 
  55.  
  56.  
  57. // sanitize for Win32 installs 
  58. $dir = str_replace('\\', '/', $dir); 
  59.  
  60.  
  61. // if file is in plugins folder 
  62. $wp_plugin_dir = str_replace('\\', '/', WP_PLUGIN_DIR); 
  63. $dir = str_replace($wp_plugin_dir, WP_PLUGIN_URL, $dir, $count); 
  64.  
  65.  
  66. if($count < 1) 
  67. // if file is in wp-content folder 
  68. $wp_content_dir = str_replace('\\', '/', WP_CONTENT_DIR); 
  69. $dir = str_replace($wp_content_dir, WP_CONTENT_URL, $dir, $count); 
  70.  
  71.  
  72. if($count < 1) 
  73. // if file is in ??? folder 
  74. $wp_dir = str_replace('\\', '/', ABSPATH); 
  75. $dir = str_replace($wp_dir, site_url('/'), $dir); 
  76.  
  77. return $dir; 
  78.  
  79.  
  80. /**-------------------------------------------------------------------------------------- 
  81. * create_options 
  82. * - this function is called from core/field_meta_box.php to create extra options 
  83. * for your field 
  84. * @params 
  85. * - $key (int) - the $_POST obejct key required to save the options to the field 
  86. * - $field (array) - the field object 
  87. * @author Elliot Condon 
  88. * @since 2.2.0 
  89. *-------------------------------------------------------------------------------------*/ 
  90.  
  91. function create_options($key, $field) 
  92. // defaults? 
  93. $field = array_merge($this->defaults, $field); 
  94.  
  95.  
  96.  
  97. // Create Field Options HTML 
  98. ?> 
  99. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  100. <td class="label"> 
  101. <label><?php _e( 'Return Format', 'acf' ); ?></label> 
  102. <p class="description"><?php _e( 'Specify the returned value on front end', 'acf' ); ?></p> 
  103. </td> 
  104. <td> 
  105. <?php 
  106.  
  107. do_action( 'acf/create_field', array( 
  108. 'type' => 'radio',  
  109. 'name' => 'fields[' . $key . '][return_value]',  
  110. 'value' => $field['return_value'],  
  111. 'layout' => 'horizontal',  
  112. 'choices' => array( 
  113. 'name' => __( 'Role Name', 'acf' ),  
  114. 'object' => __( 'Role Object', 'acf' ),  
  115. )); 
  116.  
  117. ?> 
  118. </td> 
  119. </tr> 
  120.  
  121. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  122. <td class="label"> 
  123. <label><?php _e( 'Field Type', 'acf' ); ?></label> 
  124. </td> 
  125. <td> 
  126. <?php 
  127.  
  128. do_action('acf/create_field', array( 
  129. 'type' => 'select',  
  130. 'name' => 'fields[' . $key . '][field_type]',  
  131. 'value' => $field['field_type'],  
  132. 'choices' => array( 
  133. __( 'Multiple Values', 'acf' ) => array( 
  134. 'checkbox' => __( 'Checkbox', 'acf' ),  
  135. 'multi_select' => __( 'Multi Select', 'acf' ) 
  136. ),  
  137. __( 'Single Value', 'acf' ) => array( 
  138. 'radio' => __( 'Radio Buttons', 'acf' ),  
  139. 'select' => __( 'Select', 'acf' ) 
  140. )); 
  141.  
  142. ?> 
  143. </td> 
  144. </tr> 
  145. <?php 
  146.  
  147.  
  148. /**-------------------------------------------------------------------------------------- 
  149. * create_field 
  150. * - this function is called on edit screens to produce the html for this field 
  151. * @author Elliot Condon 
  152. * @since 2.2.0 
  153. *-------------------------------------------------------------------------------------*/ 
  154.  
  155. function create_field( $field ) 
  156. global $wp_roles; 
  157. $all_roles = $wp_roles->roles; 
  158.  
  159. $selected_roles = array(); 
  160. if( !empty( $field['value'] ) && $field['return_value'] == 'object' ) { 
  161. foreach( $field['value'] as $value ) { 
  162. $selected_roles[] = $value->name; 
  163. else { 
  164. $selected_roles = $field['value']; 
  165.  
  166.  
  167. if( $field['field_type'] == 'select' || $field['field_type'] == 'multi_select' ) : 
  168. $multiple = ( $field['field_type'] == 'multi_select' ) ? 'multiple="multiple"' : ''; 
  169. ?> 
  170.  
  171. <select name='<?php echo $field['name'] ?>[]' <?php echo $multiple ?>> 
  172. <?php 
  173. foreach( $all_roles as $role => $data ) : 
  174. $selected = ( in_array( $role, $selected_roles ) ) ? 'selected="selected"' : ''; 
  175. ?> 
  176. <option <?php echo $selected ?> value='<?php echo $role ?>'><?php echo $data['name'] ?></option> 
  177. <?php endforeach; ?> 
  178.  
  179. </select> 
  180. <?php 
  181. elseif( $field['field_type'] == 'radio' ) : 
  182. echo '<ul class="acf-radio-list radio vertical">'; 
  183. foreach( $all_roles as $role => $data ) : 
  184. $checked = ( in_array( $role, $selected_roles ) ) ? 'checked="checked"' : ''; 
  185. ?> 
  186. <label><input <?php echo $checked ?> type="radio" name="<?php echo $field['name'] ?>" value="<?php echo $role ?>"><?php echo $data['name'] ?></label> 
  187. <?php 
  188. endforeach; 
  189. echo '</ul>'; 
  190. else : 
  191. echo '<ul class="acf-checkbox-list checkbox vertical">'; 
  192. foreach( $all_roles as $role => $data ) : 
  193. $checked = ( in_array( $role, $selected_roles ) ) ? 'checked="checked"' : ''; 
  194. ?> 
  195. <li><label><input <?php echo $checked ?> type="checkbox" class="checkbox" name="<?php echo $field['name'] ?>[]" value="<?php echo $role ?>"><?php echo $data['name'] ?></label></li> 
  196. <?php 
  197. endforeach; 
  198. echo '</ul>'; 
  199. endif; 
  200.  
  201.  
  202. /**-------------------------------------------------------------------------------------- 
  203. * get_value 
  204. * - called from the edit page to get the value of your field. This function is useful 
  205. * if your field needs to collect extra data for your create_field() function. 
  206. * @params 
  207. * - $post_id (int) - the post ID which your value is attached to 
  208. * - $field (array) - the field object. 
  209. * @author Elliot Condon 
  210. * @since 2.2.0 
  211. *-------------------------------------------------------------------------------------*/ 
  212.  
  213. function get_value($post_id, $field) 
  214. // Note: This function can be removed if not used 
  215.  
  216. // get value 
  217. $value = parent::get_value($post_id, $field); 
  218.  
  219. if( $field['return_value'] == 'object' ) 
  220. foreach( $value as $key => $name ) { 
  221. $value[$key] = get_role( $name ); 
  222. return $value; 
  223.  
  224.  
  225.  
  226. /**-------------------------------------------------------------------------------------- 
  227. * get_value_for_api 
  228. * - called from your template file when using the API functions (get_field, etc). 
  229. * This function is useful if your field needs to format the returned value 
  230. * @params 
  231. * - $post_id (int) - the post ID which your value is attached to 
  232. * - $field (array) - the field object. 
  233. * @author Elliot Condon 
  234. * @since 3.0.0 
  235. *-------------------------------------------------------------------------------------*/ 
  236.  
  237. function get_value_for_api($post_id, $field) 
  238. // Note: This function can be removed if not used 
  239.  
  240. // get value 
  241. $value = $this->get_value($post_id, $field); 
  242.  
  243. // format 
  244. if( $field['return_value'] == 'object' ) 
  245. foreach( $value as $key => $name ) { 
  246. $value[$key] = get_role( $name ); 
  247.  
  248. // return value 
  249. return $value; 
  250.  
  251.