Guest User

Untitled

a guest
Jul 16th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. <?php
  2.  
  3. class WordPress_Settings
  4. {
  5. /**
  6. *
  7. * @var array
  8. */
  9. protected $_registry;
  10.  
  11. /**
  12. * Option Name
  13. *
  14. * @var string
  15. */
  16. protected $_optionName;
  17.  
  18. /**
  19. * Option values
  20. *
  21. * @var array
  22. */
  23. protected $_values;
  24.  
  25. protected $_latestSection = 'default';
  26.  
  27.  
  28. public function __construct(array $options = array())
  29. {
  30. $options = array_merge(array(
  31. 'sanitize_callback' => '',
  32. 'sections' => array(),
  33. 'fields' => array()
  34. ), $options);
  35.  
  36. $this->_registry = $options;
  37.  
  38. $this->_values = get_option($this->_registry['option_name']);
  39. }
  40.  
  41. public function init()
  42. {
  43. register_setting($this->_registry['option_group'], $this->_registry['option_name'], $this->_registry['sanitize_callback']);
  44.  
  45. foreach ($this->_registry['sections'] as $section) {
  46. add_settings_section($section['id'], $section['title'], $section['callback'], $this->_registry['option_name']);
  47. }
  48.  
  49. foreach ($this->_registry['fields'] as $field) {
  50. add_settings_field($field['id'], $field['title'], $field['callback'], $this->_registry['option_name'], $field['section']);
  51. }
  52. }
  53.  
  54. /**
  55. * Adds a new section
  56. *
  57. * @param array $options
  58. * @return WordPress_Settings
  59. */
  60. public function addSection(array $options)
  61. {
  62. if (!function_exists($options['callback'])) {
  63. $options['callback'] = $this->fnEcho($options['callback']);
  64. }
  65.  
  66. $this->_registry['sections'][] = $options;
  67.  
  68. $this->_latestSection = $options['id'];
  69.  
  70. return $this;
  71. }
  72.  
  73. /**
  74. * Adds a field
  75. *
  76. * @param array $options
  77. * @return WordPress_Settings
  78. */
  79. public function addField(array $options)
  80. {
  81. $options = array_merge(array(
  82. 'type' => 'text',
  83. 'section' => null
  84. ), $options);
  85.  
  86. if (in_array($options['type'], array('radio', 'select'))) {
  87. $options['callback'] = $this->{"field".ucfirst($options['type'])}($options['key'], $options['options']);
  88. } else {
  89. $options['callback'] = $this->{"field".ucfirst($options['type'])}($options['key']);
  90. }
  91.  
  92. $options['id'] = "{$this->_registry['option_name']}_{$options['key']}";
  93. $options['section'] = $options['section'] ? $options['section'] : $this->_latestSection;
  94.  
  95. $this->_registry['fields'][] = $options;
  96.  
  97. $this->_latestSection = $options['section'];
  98.  
  99. return $this;
  100. }
  101.  
  102. public function fnEcho($text)
  103. {
  104. return create_function('', "echo '$text';");
  105. }
  106.  
  107. public function fieldText($key)
  108. {
  109. $html = sprintf('<input id="%s_%s" name="%s[%s]" size="40" type="text" value="%s" />', $this->_registry['option_name'], $key, $this->_registry['option_name'], $key, $this->_values[$key]);
  110.  
  111. return $this->fnEcho($html);
  112. }
  113.  
  114. public function fieldPassword($key)
  115. {
  116. $html = sprintf('<input id="%s_%s" name="%s[%s]" size="40" type="password" value="%s" />', $this->_registry['option_name'], $key, $this->_registry['option_name'], $key, $this->_values[$key]);
  117.  
  118. return $this->fnEcho($html);
  119. }
  120.  
  121. public function fieldTextarea($key)
  122. {
  123. $html = sprintf('<textarea id="%s_%s" name="%s[%s]" rows="7" cols="50" type="textarea">%s</textarea>', $this->_registry['option_name'], $key, $this->_registry['option_name'], $key, $this->_values[$key]);
  124.  
  125. return $this->fnEcho($html);
  126. }
  127.  
  128. public function fieldCheckbox($key)
  129. {
  130. $checked = $this->_values[$key] ? 'checked="checked"' : '';
  131.  
  132. $html = sprintf('<input id="%s_%s" name="%s[%s]" type="checkbox" %s />', $this->_registry['option_name'], $key, $this->_registry['option_name'], $key, $checked);
  133.  
  134. return $this->fnEcho($html);
  135. }
  136.  
  137. public function fieldRadio($key, $options)
  138. {
  139. $html = '';
  140.  
  141. foreach($options as $option => $label) {
  142. $checked = isset($this->_values[$key]) && $this->_values[$key] == $option ? 'checked="checked"' : '';
  143.  
  144. $html .= sprintf('<label><input name="%s[%s]" type="radio" value="%s" %s />%s</label><br />', $this->_registry['option_name'], $key, $option, $checked, $label);
  145. }
  146.  
  147. return $this->fnEcho($html);
  148. }
  149.  
  150. public function fieldSelect($key, $options)
  151. {
  152. $html = sprintf('<select id="%s_%s" name="%s[%s]" style="width: 250px">', $this->_registry['option_name'], $key, $this->_registry['option_name'], $key);
  153.  
  154. foreach($options as $option => $label) {
  155. $selected = isset($this->_values[$key]) && $this->_values[$key] == $option ? 'selected="selected"' : '';
  156.  
  157. $html .= sprintf('<option value="%s" %s />%s</option>', $option, $selected, $label);
  158. }
  159.  
  160. $html .= '</select>';
  161.  
  162. return $this->fnEcho($html);
  163. }
  164.  
  165. public function addPage()
  166. {
  167. add_options_page(
  168. $this->_registry['page_title'],
  169. $this->_registry['menu_title'],
  170. $this->_registry['capability'],
  171. $this->_registry['option_name'],
  172. array($this, 'page')
  173. );
  174. }
  175.  
  176. public function page() {
  177. ?>
  178. <div class="wrap">
  179. <div class="icon32" id="icon-options-general"><br></div>
  180. <h2><?php echo $this->_registry['page_title']; ?></h2>
  181. <form action="options.php" method="post">
  182. <?php settings_fields($this->_registry['option_name']); ?>
  183. <?php do_settings_sections($this->_registry['option_name']); ?>
  184. <p class="submit">
  185. <input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
  186. </p>
  187. </form>
  188. </div>
  189. <?php
  190. }
  191. }
Add Comment
Please, Sign In to add comment