PageRenderTime 25ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/bwp-google-xml-sitemaps/includes/bwp-option-page/includes/class-bwp-option-page.php

https://gitlab.com/Gashler/sg
PHP | 405 lines | 288 code | 52 blank | 65 comment | 82 complexity | f5992bc00722e24680e8c5b92f40fa37 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (c) 2011 Khang Minh <betterwp.net>
  4. * @license http://www.gnu.org/licenses/gpl.html GNU GENERAL PUBLIC LICENSE VERSION 3.0 OR LATER
  5. */
  6. class BWP_OPTION_PAGE {
  7. /**
  8. * The form
  9. */
  10. var $form;
  11. /**
  12. * The form name
  13. */
  14. var $form_name;
  15. /**
  16. * Tabs to build
  17. */
  18. var $form_tabs;
  19. /**
  20. * Current tab
  21. */
  22. var $current_tab;
  23. /**
  24. * This holds the form items, determining the position
  25. */
  26. var $form_items = array();
  27. /**
  28. * This holds the name for each items (an item can have more than one fields)
  29. */
  30. var $form_item_names = array();
  31. /**
  32. * This holds the form label
  33. */
  34. var $form_item_labels = array();
  35. /**
  36. * This holds the form option aka data
  37. */
  38. var $form_options = array(), $site_options = array();
  39. /**
  40. * Other things
  41. */
  42. var $domain;
  43. /**
  44. * Constructor
  45. */
  46. function __construct($form_name = 'bwp_option_page', $site_options = array(), $domain = '')
  47. {
  48. $this->form_name = $form_name;
  49. $this->site_options = $site_options;
  50. $this->domain = $domain;
  51. }
  52. /**
  53. * Init the class
  54. *
  55. * @param array $form The form array that contains everything we need to build the form
  56. * @param array $options The data array that contains all data fetched from db or by default
  57. * @param string $form_name The name of the form, change this if you have more than one forms on a page
  58. */
  59. function init($form = array(), $options = array(), $form_tabs = array())
  60. {
  61. $this->form_items = $form['items'];
  62. $this->form_item_names = $form['item_names'];
  63. $this->form_item_labels = $form['item_labels'];
  64. $this->form = $form;
  65. $this->form_options = $options;
  66. $this->form_tabs = $form_tabs;
  67. if (sizeof($this->form_tabs) == 0)
  68. $this->form_tabs = array(__('Plugin Configurations', 'bwp-option-page'));
  69. }
  70. function get_form_name()
  71. {
  72. return $this->form_name;
  73. }
  74. function set_current_tab($current_tab = 0)
  75. {
  76. $this->current_tab = $current_tab;
  77. }
  78. function get_options($options = array(), $options_default = array())
  79. {
  80. foreach ($options_default as $key => $option)
  81. {
  82. if (!in_array($key, $options))
  83. unset($options_default[$key]);
  84. }
  85. return $options_default;
  86. }
  87. function get_db_options($name = '', $options = array())
  88. {
  89. $db_options = get_option($name);
  90. if (!$db_options)
  91. update_option($name, $options);
  92. else if (array_keys($options) != array_keys($db_options))
  93. {
  94. foreach ($db_options as $key => $data)
  95. if (isset($options[$key]) && !in_array($key, $this->site_options))
  96. $options[$key] = $data;
  97. update_option($name, $options);
  98. }
  99. else
  100. {
  101. foreach ($db_options as $key => $data)
  102. if (!in_array($key, $this->site_options))
  103. $options[$key] = $data;
  104. }
  105. return $options;
  106. }
  107. function format_field($key, $option_formats)
  108. {
  109. if (!empty($option_formats[$key]))
  110. {
  111. if ('int' == $option_formats[$key])
  112. $_POST[$key] = (int) $_POST[$key];
  113. else if ('float' == $option_formats[$key])
  114. $_POST[$key] = (float) $_POST[$key];
  115. else if ('html' == $option_formats[$key])
  116. $_POST[$key] = wp_filter_post_kses($_POST[$key]);
  117. }
  118. else
  119. $_POST[$key] = strip_tags($_POST[$key]);
  120. }
  121. function kill_html_fields(&$form = array(), $ids)
  122. {
  123. $ids = (array) $ids;
  124. $in_keys = array('items', 'item_labels', 'item_names');
  125. foreach ($ids as $id)
  126. {
  127. foreach ($in_keys as $key)
  128. unset($form[$key][$id]);
  129. }
  130. }
  131. /**
  132. * Generate HTML field
  133. *
  134. * @params they explain themselves
  135. */
  136. function generate_html_field($type = '', $data = array(), $name = '', $in_section = false)
  137. {
  138. $pre_html_field = '';
  139. $post_html_field = '';
  140. $checked = 'checked="checked" ';
  141. $selected = 'selected="selected" ';
  142. $value = (isset($this->form_options[$name])) ? $this->form_options[$name] : '';
  143. $value = (!empty($this->domain) && ('textarea' == $type || 'input' == $type)) ? __($value, $this->domain) : $value;
  144. $value = ('textarea' == $type) ? esc_html($value) : esc_attr($value);
  145. $array_replace = array();
  146. $array_search = array('size', 'name', 'value', 'cols', 'rows', 'label', 'disabled', 'pre', 'post');
  147. $return_html = '';
  148. $br = (isset($this->form['inline_fields'][$name]) && is_array($this->form['inline_fields'][$name])) ? '' : "<br />\n";
  149. $pre = (!empty($data['pre'])) ? $data['pre'] : '';
  150. $post = (!empty($data['post'])) ? $data['post'] : '';
  151. switch ($type)
  152. {
  153. case 'heading':
  154. $html_field = '%s';
  155. break;
  156. case 'input':
  157. $html_field = (!$in_section) ? '%pre%<input%disabled% size="%size%" type="text" id="' . $name . '" name="' . $name . '" value="' . $value . '" /> <em>%label%</em>' : '<label for="' . $name . '">%pre%<input%disabled% size="%size%" type="text" id="' . $name . '" name="' . $name . '" value="' . $value . '" /> <em>%label%</em></label>';
  158. break;
  159. case 'select':
  160. $pre_html_field = '%pre%<select id="' . $name . '" name="' . $name . '">' . "\n";
  161. $html_field = '<option %selected%value="%value%" />%option%</option>';
  162. $post_html_field = '</select>%post%' . $br;
  163. break;
  164. case 'checkbox':
  165. $html_field = '<label for="%name%">' . '<input %checked%type="checkbox" id="%name%" name="%name%" value="yes" /> %label%</label>';
  166. break;
  167. case 'radio':
  168. $html_field = '<label>' . '<input %checked%type="radio" name="' . $name . '" value="%value%" /> %label%</label>';
  169. break;
  170. case 'textarea':
  171. $html_field = '%pre%<textarea%disabled% id="' . $name . '" name="' . $name . '" cols="%cols%" rows="%rows%">' . $value . '</textarea>%post%';
  172. break;
  173. }
  174. if (!isset($data))
  175. return;
  176. if ($type == 'heading' && !is_array($data))
  177. {
  178. $return_html .= sprintf($html_field, $data) . $br;
  179. }
  180. else if ($type == 'radio' || $type == 'checkbox' || $type == 'select')
  181. {
  182. foreach ($data as $key => $value)
  183. {
  184. // handle checkbox a little bit differently
  185. if ($type == 'checkbox')
  186. {
  187. if ($this->form_options[$value] == 'yes')
  188. $return_html .= str_replace(array('%value%', '%name%', '%label%', '%checked%'), array($value, $value, $key, $checked), $html_field) . $br;
  189. else
  190. $return_html .= str_replace(array('%value%', '%name%', '%label%', '%checked%'), array($value, $value, $key, ''), $html_field) . $br;
  191. }
  192. else if (isset($this->form_options[$name]) && $this->form_options[$name] == $value)
  193. $return_html .= str_replace(array('%value%', '%name%', '%label%', '%option%', '%checked%', '%selected%', '%pre%', '%post%'), array($value, $value, $key, $key, $checked, $selected, $pre, $post), $html_field) . $br;
  194. else
  195. $return_html .= str_replace(array('%value%', '%name%', '%label%', '%option%', '%checked%', '%selected%', '%pre%', '%post%'), array($value, $value, $key, $key, '', '', $pre, $post), $html_field) . $br;
  196. }
  197. }
  198. else
  199. {
  200. foreach ($array_search as &$keyword)
  201. {
  202. $array_replace[$keyword] = '';
  203. if (!empty($data[$keyword]))
  204. {
  205. $array_replace[$keyword] = $data[$keyword];
  206. }
  207. $keyword = '%' . $keyword . '%';
  208. }
  209. $return_html = str_replace($array_search, $array_replace, $html_field) . $br;
  210. }
  211. // inline fields
  212. $inline_html = '';
  213. if (isset($this->form['inline_fields'][$name]) && is_array($this->form['inline_fields'][$name]))
  214. {
  215. foreach ($this->form['inline_fields'][$name] as $field => $field_type)
  216. {
  217. if (isset($this->form[$field_type][$field]))
  218. $inline_html = ' ' . $this->generate_html_field($field_type, $this->form[$field_type][$field], $field, $in_section);
  219. }
  220. }
  221. // Post
  222. $post = (!empty($this->form['post'][$name])) ? ' ' . $this->form['post'][$name] : $post;
  223. return str_replace('%pre%', $pre, $pre_html_field) . $return_html . str_replace('%post%', $post, $post_html_field) . $inline_html;
  224. }
  225. /**
  226. * Generate HTML fields
  227. *
  228. * @params they explain themselves
  229. */
  230. function generate_html_fields($type, $name)
  231. {
  232. $item_label = '';
  233. $return_html = '';
  234. $item_key = array_keys($this->form_item_names, $name);
  235. $input_class = ($type == 'heading') ? 'bwp-option-page-heading-desc' : 'bwp-option-page-inputs';
  236. // An inline item can hold any HTML markup
  237. // An example is to display some kinds of button right be low the label
  238. $inline = '';
  239. if (isset($this->form['inline']) && is_array($this->form['inline']) && array_key_exists($name, $this->form['inline']))
  240. {
  241. $inline = (empty($this->form['inline'][$name])) ? '' : $this->form['inline'][$name];
  242. }
  243. $inline .= "\n";
  244. switch ($type)
  245. {
  246. case 'section':
  247. if (!isset($this->form[$name]) || !is_array($this->form[$name]))
  248. return;
  249. $item_label = '<span class="bwp-opton-page-label">' . $this->form_item_labels[$item_key[0]] . $inline . '</span>';
  250. foreach ($this->form[$name] as $section_field)
  251. {
  252. $type = $section_field[0];
  253. $name = $section_field['name'];
  254. if (isset($this->form[$section_field[0]]))
  255. {
  256. $return_html .= $this->generate_html_field($section_field[0], $this->form[$type][$name], $name, true);
  257. }
  258. }
  259. break;
  260. default:
  261. if (!isset($this->form[$type][$name]) || ($type != 'heading' && !is_array($this->form[$type][$name])))
  262. return;
  263. /*$item_label = (empty($this->form[$type][$name]['label'])) ? '<label class="bwp-opton-page-label" for="' . $name . '">' . $this->form_item_labels[$item_key[0]] . '</label>' : '<span class="bwp-opton-page-label">' . $this->form_item_labels[$item_key[0]] . '</span>';*/
  264. $item_label = ($type != 'checkbox' && $type != 'radio') ? '<label class="bwp-opton-page-label" for="' . $name . '">' . $this->form_item_labels[$item_key[0]] . $inline . '</label>' : '<span class="bwp-opton-page-label type-' . $type . '">' . $this->form_item_labels[$item_key[0]] . $inline . '</span>';
  265. $item_label = ($type == 'heading') ? '<h3>' . $this->form_item_labels[$item_key[0]] . '</h3>' . $inline : $item_label;
  266. if (isset($this->form[$type]))
  267. {
  268. $return_html = $this->generate_html_field($type, $this->form[$type][$name], $name);
  269. }
  270. break;
  271. }
  272. // A container can hold some result executed by customized script,
  273. // such as displaying something when user press the submit button
  274. $containers = '';
  275. if (isset($this->form['container']) && is_array($this->form['container']) && array_key_exists($name, $this->form['container']))
  276. {
  277. $container_array = (array) $this->form['container'][$name];
  278. foreach ($container_array as $container)
  279. {
  280. $containers .= (empty($container)) ? '<div style="display: none;"><!-- --></div>' : '<div class="bwp-clear">' . $container . '</div>' . "\n";
  281. }
  282. }
  283. $pure_return = trim(strip_tags($return_html));
  284. if (empty($pure_return) && $type == 'heading')
  285. return $item_label . $containers;
  286. else
  287. return $item_label . '<p class="' . $input_class . '">' . $return_html . '</p>' . $containers;
  288. }
  289. /**
  290. * Generate HTML form
  291. *
  292. * @see Constructor
  293. */
  294. function generate_html_form()
  295. {
  296. $return_str = '<div class="wrap" style="padding-bottom: 20px;">' . "\n";
  297. if (sizeof($this->form_tabs) >= 2)
  298. $return_str .= apply_filters('bwp-admin-form-icon', '<div class="icon32" id="icon-options-general"><br></div>' . "\n");
  299. else
  300. $return_str .= '<div class="icon32" id="icon-options-general"><br></div>';
  301. if (sizeof($this->form_tabs) >= 2)
  302. {
  303. $count = 0;
  304. $return_str .= '<h2 class="bwp-option-page-tabs">' . "\n";
  305. $return_str .= apply_filters('bwp-admin-plugin-version', '') . "\n";
  306. foreach ($this->form_tabs as $title => $link)
  307. {
  308. $count++;
  309. $active = ($count == $this->current_tab) ? ' nav-tab-active' : '';
  310. $return_str .= '<a class="nav-tab' . $active . '" href="' . $link . '">' . $title . '</a>' . "\n";
  311. }
  312. $return_str .= '</h2>' . "\n";
  313. }
  314. else if (!isset($this->form_tabs[0]))
  315. {
  316. $title = array_keys($this->form_tabs);
  317. $return_str .= '<h2>' . $title[0] . '</h2>' . "\n";
  318. }
  319. else
  320. $return_str .= '<h2>' . $this->form_tabs[0] . '</h2>' . "\n";
  321. $return_str .= apply_filters('bwp_option_before_form', '');
  322. echo $return_str;
  323. do_action('bwp_option_action_before_form');
  324. $return_str = '';
  325. $return_str .= '<form class="bwp-option-page" name="' . $this->form_name . '" method="post" action="">' . "\n";
  326. if (function_exists('wp_nonce_field'))
  327. {
  328. echo $return_str;
  329. wp_nonce_field($this->form_name);
  330. $return_str = '';
  331. }
  332. $return_str .= '<ul>' . "\n";
  333. // generate filled form
  334. if (isset($this->form_items) && is_array($this->form_items))
  335. foreach ($this->form_items as $key => $type)
  336. {
  337. if (!empty($this->form_item_names[$key]) && !empty($this->form_item_labels[$key]))
  338. {
  339. $return_str .= '<li class="bwp-clear">' . $this->generate_html_fields($type, $this->form_item_names[$key]) . '</li>' . "\n";
  340. }
  341. }
  342. $return_str .= '</ul>' . "\n";
  343. $return_str .= apply_filters('bwp_option_before_submit_button', '');
  344. echo $return_str;
  345. do_action('bwp_option_action_before_submit_button');
  346. $return_str = '';
  347. $return_str .= apply_filters('bwp_option_submit_button', '<p class="submit"><input type="submit" class="button-primary" name="submit_' . $this->form_name . '" value="' . __('Save Changes') . '" /></p>') . "\n";
  348. $return_str .= '</form>' . "\n";
  349. $return_str .= '</div>' . "\n";
  350. echo $return_str;
  351. }
  352. }
  353. ?>