/wp-content/plugins/wordpress-content-types-generator/includes/settings/register-settings.php

https://gitlab.com/haque.mdmanzurul/barongbarong · PHP · 383 lines · 157 code · 74 blank · 152 comment · 26 complexity · d026308d70db74444447fa57a918292b MD5 · raw file

  1. <?php
  2. /**
  3. * Register Settings
  4. *
  5. */
  6. // Exit if accessed directly
  7. if ( !defined( 'ABSPATH' ) ) exit;
  8. /**
  9. * Register Settings
  10. *
  11. * Registers the required settings.
  12. *
  13. * @access private
  14. * @since 1.0
  15. * @return void
  16. */
  17. function rc_wctg_register_settings() {
  18. // Setup some default option sets
  19. $pages = get_pages();
  20. $pages_options = array( 0 => '' ); // Blank option
  21. if( $pages ) {
  22. foreach( $pages as $page ) {
  23. $pages_options[ $page->ID ] = $page->post_title;
  24. }
  25. }
  26. /* white list our settings, each in their respective section
  27. filters can be used to add more options to each section */
  28. $rc_wctg_settings = array(
  29. // General Settings
  30. 'general' => apply_filters('rc_wctg_settings_general',
  31. array(
  32. array(
  33. 'id' => 'minimum_level',
  34. 'name' => __('Minimum level', 'rc_wctg'),
  35. 'desc' => __('Choose minimum user level required to use the plugin.', 'rc_wctg'),
  36. 'type' => 'select',
  37. 'options' => array(
  38. 'administrator' => __('Administrator', 'rc_wctg'),
  39. 'editor' => __('Editor', 'rc_wctg'),
  40. 'author' => __('Author', 'rc_wctg'),
  41. 'contributor' => __('Contributor', 'rc_wctg'),
  42. 'subscriber' => __('Subscriber', 'rc_wctg'),
  43. )
  44. )
  45. )
  46. ), // End general
  47. );
  48. /*
  49. |--------------------------------------------------------------------------
  50. | Section General
  51. |--------------------------------------------------------------------------
  52. */
  53. if( false == get_option( 'rc_wctg_settings_general' ) ) {
  54. add_option( 'rc_wctg_settings_general' );
  55. }
  56. add_settings_section(
  57. 'rc_wctg_settings_general',
  58. __('General Settings', 'rc_wctg'),
  59. '__return_false',
  60. 'rc_wctg_settings_general'
  61. );
  62. foreach( $rc_wctg_settings['general'] as $option ) {
  63. add_settings_field(
  64. 'rc_wctg_settings_general[' . $option['id'] . ']',
  65. $option['name'],
  66. function_exists( 'rc_wctg_' . $option['type'] . '_callback' ) ? 'rc_wctg_' . $option['type'] . '_callback' : 'rc_wctg_missing_callback',
  67. 'rc_wctg_settings_general',
  68. 'rc_wctg_settings_general',
  69. array(
  70. 'id' => $option['id'],
  71. 'desc' => $option['desc'],
  72. 'name' => $option['name'],
  73. 'section' => 'general',
  74. 'size' => isset($option['size']) ? $option['size'] : null,
  75. 'options' => isset($option['options']) ? $option['options'] : '',
  76. 'std' => isset($option['std']) ? $option['std'] : ''
  77. )
  78. );
  79. }
  80. // Creates our settings in the options table
  81. register_setting( 'rc_wctg_settings_general', 'rc_wctg_settings_general', 'rc_wctg_settings_sanitize' );
  82. }
  83. add_action('admin_init', 'rc_wctg_register_settings');
  84. /**
  85. * Header Callback
  86. *
  87. * Renders the header.
  88. *
  89. * @access private
  90. * @since 1.0
  91. * @return void
  92. */
  93. function rc_wctg_header_callback($args) {
  94. echo '';
  95. }
  96. /**
  97. * Checkbox Callback
  98. *
  99. * Renders checkboxes.
  100. *
  101. * @access private
  102. * @since 1.0
  103. * @return void
  104. */
  105. function rc_wctg_checkbox_callback($args) {
  106. global $rc_wctg_options;
  107. $checked = isset($rc_wctg_options[$args['id']]) ? checked(1, $rc_wctg_options[$args['id']], false) : '';
  108. $html = '<input type="checkbox" id="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']" name="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']" value="1" ' . $checked . '/>';
  109. $html .= '<label for="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']"> ' . $args['desc'] . '</label>';
  110. echo $html;
  111. }
  112. /**
  113. * Multicheck Callback
  114. *
  115. * Renders multiple checkboxes.
  116. *
  117. * @access private
  118. * @since 1.0
  119. * @return void
  120. */
  121. function rc_wctg_multicheck_callback($args) {
  122. global $rc_wctg_options;
  123. foreach( $args['options'] as $key => $option ):
  124. if( isset( $rc_wctg_options[$args['id']][$key] ) ) { $enabled = $option; } else { $enabled = NULL; }
  125. echo '<input name="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . '][' . $key . ']"" id="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . '][' . $key . ']" type="checkbox" value="' . $option . '" ' . checked($option, $enabled, false) . '/>&nbsp;';
  126. echo '<label for="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . '][' . $key . ']">' . $option . '</label><br/>';
  127. endforeach;
  128. echo '<p class="description">' . $args['desc'] . '</p>';
  129. }
  130. /**
  131. * Radio Callback
  132. *
  133. * Renders radio boxes.
  134. *
  135. * @access private
  136. * @since 1.3.3
  137. * @return void
  138. */
  139. function rc_wctg_radio_callback($args) {
  140. global $rc_wctg_options;
  141. foreach($args['options'] as $key => $option) :
  142. $checked = false;
  143. if( isset( $rc_wctg_options[ $args['id'] ] ) && $rc_wctg_options[ $args['id'] ] == $key ) $checked = true;
  144. echo '<input name="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']"" id="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . '][' . $key . ']" type="radio" value="' . $key . '" ' . checked(true, $checked, false) . '/>&nbsp;';
  145. echo '<label for="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . '][' . $key . ']">' . $option . '</label><br/>';
  146. endforeach;
  147. echo '<p class="description">' . $args['desc'] . '</p>';
  148. }
  149. /**
  150. * Text Callback
  151. *
  152. * Renders text fields.
  153. *
  154. * @access private
  155. * @since 1.0
  156. * @return void
  157. */
  158. function rc_wctg_text_callback($args) {
  159. global $rc_wctg_options;
  160. if( isset( $rc_wctg_options[ $args['id'] ] ) ) { $value = $rc_wctg_options[ $args['id'] ]; } else { $value = isset( $args['std'] ) ? $args['std'] : ''; }
  161. $size = isset( $args['size'] ) && !is_null($args['size']) ? $args['size'] : 'regular';
  162. $html = '<input type="text" class="' . $args['size'] . '-text" id="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']" name="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']" value="' . esc_attr( $value ) . '"/>';
  163. $html .= '<label for="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']"> ' . $args['desc'] . '</label>';
  164. echo $html;
  165. }
  166. /**
  167. * Textarea Callback
  168. *
  169. * Renders textarea fields.
  170. *
  171. * @access private
  172. * @since 1.0
  173. * @return void
  174. */
  175. function rc_wctg_textarea_callback($args) {
  176. global $rc_wctg_options;
  177. if( isset( $rc_wctg_options[ $args['id'] ] ) ) { $value = $rc_wctg_options[ $args['id'] ]; } else { $value = isset( $args['std'] ) ? $args['std'] : ''; }
  178. $size = isset( $args['size'] ) && !is_null($args['size']) ? $args['size'] : 'regular';
  179. $html = '<textarea class="large-text" cols="50" rows="5" id="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']" name="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']">' . esc_textarea( $value ) . '</textarea>';
  180. $html .= '<label for="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']"> ' . $args['desc'] . '</label>';
  181. echo $html;
  182. }
  183. /**
  184. * Password Callback
  185. *
  186. * Renders password fields.
  187. *
  188. * @access private
  189. * @since 1.3
  190. * @return void
  191. */
  192. function rc_wctg_password_callback($args) {
  193. global $rc_wctg_options;
  194. if( isset( $rc_wctg_options[ $args['id'] ] ) ) { $value = $rc_wctg_options[ $args['id'] ]; } else { $value = isset( $args['std'] ) ? $args['std'] : ''; }
  195. $size = isset( $args['size'] ) && !is_null($args['size']) ? $args['size'] : 'regular';
  196. $html = '<input type="password" class="' . $args['size'] . '-text" id="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']" name="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']" value="' . esc_attr( $value ) . '"/>';
  197. $html .= '<label for="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']"> ' . $args['desc'] . '</label>';
  198. echo $html;
  199. }
  200. /**
  201. * Missing Callback
  202. *
  203. * If a function is missing for settings callbacks alert the user.
  204. *
  205. * @access private
  206. * @since 1.3.1
  207. * @return void
  208. */
  209. function rc_wctg_missing_callback($args) {
  210. printf( __( 'The callback function used for the <strong>%s</strong> setting is missing.', 'rc_wctg' ), $args['id'] );
  211. }
  212. /**
  213. * Select Callback
  214. *
  215. * Renders select fields.
  216. *
  217. * @access private
  218. * @since 1.0
  219. * @return void
  220. */
  221. function rc_wctg_select_callback($args) {
  222. global $rc_wctg_options;
  223. $html = '<select id="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']" name="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']"/>';
  224. foreach( $args['options'] as $option => $name ) {
  225. $selected = isset( $rc_wctg_options[ $args['id'] ] ) ? selected( $option, $rc_wctg_options[$args['id']], false ) : '';
  226. $html .= '<option value="' . $option . '" ' . $selected . '>' . $name . '</option>';
  227. }
  228. $html .= '</select>';
  229. $html .= '<label for="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']"> ' . $args['desc'] . '</label>';
  230. echo $html;
  231. }
  232. /**
  233. * Rich Editor Callback
  234. *
  235. * Renders rich editor fields.
  236. *
  237. * @access private
  238. * @since 1.0
  239. * @return void
  240. */
  241. function rc_wctg_rich_editor_callback($args) {
  242. global $rc_wctg_options, $wp_version;
  243. if( isset( $rc_wctg_options[ $args['id'] ] ) ) { $value = $rc_wctg_options[ $args['id'] ]; } else { $value = isset( $args['std'] ) ? $args['std'] : ''; }
  244. if( $wp_version >= 3.3 && function_exists('wp_editor')) {
  245. $html = wp_editor( $value, 'rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']', array( 'textarea_name' => 'rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']' ) );
  246. } else {
  247. $html = '<textarea class="large-text" rows="10" id="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']" name="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']">' . esc_textarea( $value ) . '</textarea>';
  248. }
  249. $html .= '<br/><label for="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']"> ' . $args['desc'] . '</label>';
  250. echo $html;
  251. }
  252. /**
  253. * Upload Callback
  254. *
  255. * Renders upload fields.
  256. *
  257. * @access private
  258. * @since 1.0
  259. * @return void
  260. */
  261. function rc_wctg_upload_callback($args) {
  262. global $rc_wctg_options;
  263. if( isset( $rc_wctg_options[ $args['id'] ] ) ) { $value = $rc_wctg_options[$args['id']]; } else { $value = isset($args['std']) ? $args['std'] : ''; }
  264. $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
  265. $html = '<input type="text" class="' . $args['size'] . '-text rc_wctg_upload_field" id="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']" name="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']" value="' . esc_attr( $value ) . '"/>';
  266. $html .= '<span>&nbsp;<input type="button" class="rc_wctg_upload_image_button button-secondary" value="' . __('Upload File', 'rc_wctg') . '"/></span>';
  267. $html .= '<label for="rc_wctg_settings_' . $args['section'] . '[' . $args['id'] . ']"> ' . $args['desc'] . '</label>';
  268. echo $html;
  269. }
  270. /**
  271. * Hook Callback
  272. *
  273. * Adds a do_action() hook in place of the field
  274. *
  275. * @access private
  276. * @since 1.0.8.2
  277. * @return void
  278. */
  279. function rc_wctg_hook_callback( $args ) {
  280. do_action( 'rc_wctg_' . $args['id'] );
  281. }
  282. /**
  283. * Settings Sanitization
  284. *
  285. * Adds a settings error (for the updated message)
  286. * At some point this will validate input
  287. *
  288. * @access private
  289. * @since 1.0.8.2
  290. * @return void
  291. */
  292. function rc_wctg_settings_sanitize( $input ) {
  293. add_settings_error( 'rc_rmb-notices', '', __('Settings Updated', 'rc_wctg'), 'updated' );
  294. return $input;
  295. }
  296. /**
  297. * Get Settings
  298. *
  299. * Retrieves all plugin settings and returns them
  300. * as a combined array.
  301. *
  302. * @access public
  303. * @since 1.0
  304. * @return array
  305. */
  306. function rc_wctg_get_settings() {
  307. $general_settings = is_array(get_option('rc_wctg_settings_general')) ? get_option('rc_wctg_settings_general') : array();
  308. return array_merge($general_settings);
  309. }