PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/omega/vendor/oxygenna/oxygenna-framework/inc/OxygennaOptions.php

https://github.com/amang-cuelogic/wp_test
PHP | 389 lines | 239 code | 28 blank | 122 comment | 37 complexity | 41a29d95333b5c5b186c8143a556d3d0 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, LGPL-2.1, GPL-3.0, GPL-2.0, Apache-2.0
  1. <?php
  2. /**
  3. * Theme Options Builder
  4. *
  5. * @package ThemeFramework
  6. * @subpackage Options
  7. * @since 1.0
  8. *
  9. * @copyright (c) 2014 Oxygenna.com
  10. * @license http://wiki.envato.com/support/legal-terms/licensing-terms/
  11. * @version 1.2
  12. */
  13. /**
  14. * Main theme options class
  15. *
  16. **/
  17. class OxygennaOptions
  18. {
  19. /**
  20. * Stores all option pages / sections / fields
  21. *
  22. * @var array
  23. **/
  24. private $theme;
  25. /**
  26. * stores all theme options
  27. *
  28. * @var array
  29. **/
  30. private $options;
  31. /**
  32. * Main constructor
  33. *
  34. * @return void
  35. * @since 1.0
  36. **/
  37. public function __construct($theme)
  38. {
  39. $this->theme = $theme;
  40. // admin init
  41. add_action('admin_init', array(&$this, 'admin_init'));
  42. // load all option pages into admin menu
  43. add_action('admin_menu', array(&$this, 'create_option_pages'), 1);
  44. }
  45. /**
  46. * Setup theme options
  47. *
  48. * @return void
  49. * @since 1.0
  50. **/
  51. public function admin_init()
  52. {
  53. // check for default options
  54. $this->options = get_option(THEME_SHORT . '-options');
  55. if ($this->options === false) {
  56. $this->create_default_options();
  57. } else {
  58. // check for missing default options
  59. $this->create_missing_default_options();
  60. }
  61. // register theme settings
  62. register_setting(THEME_SHORT . '-options', THEME_SHORT . '-options', array(&$this, 'validate_options'));
  63. // create settings
  64. // create default options
  65. foreach ($this->theme->option_pages as $page) {
  66. foreach ($page['sections'] as $section_id => $section) {
  67. add_settings_section(
  68. $section_id,
  69. $section['title'],
  70. array(&$this, 'section_description'),
  71. $page['slug']
  72. );
  73. foreach ($section['fields'] as $field) {
  74. add_settings_field(
  75. $field['id'],
  76. $field['name'],
  77. array(&$this, 'render_option'),
  78. $page['slug'],
  79. $section_id,
  80. $field
  81. );
  82. }
  83. }
  84. }
  85. }
  86. /**
  87. * Checks all default options are set for missing options
  88. *
  89. * @return void
  90. * @since 1.1
  91. **/
  92. public function create_missing_default_options()
  93. {
  94. // create default options for missing ones
  95. foreach ($this->theme->option_pages as $page) {
  96. foreach ($page['sections'] as $section) {
  97. foreach ($section['fields'] as $field) {
  98. if (isset($field['default'])) {
  99. if (!isset($this->options[$field['id']])) {
  100. $this->options[$field['id']] = $field['default'];
  101. }
  102. }
  103. }
  104. }
  105. }
  106. // save default options
  107. update_option(THEME_SHORT . '-options', $this->options);
  108. }
  109. /**
  110. * Displays the section description
  111. *
  112. * @return void
  113. * @since 1.0
  114. **/
  115. public function section_description($section_data)
  116. {
  117. foreach ($this->theme->option_pages as $page) {
  118. foreach ($page['sections'] as $section_id => $section) {
  119. if ($section_id == $section_data['id']) {
  120. if (isset($section['header'])) {
  121. echo '<p class="section-description">' . $section['header'] . '</p>';
  122. }
  123. break;
  124. }
  125. }
  126. }
  127. }
  128. /**
  129. * Validates all options on save
  130. *
  131. * @return void
  132. * @since 1.0
  133. **/
  134. public function validate_options($new_options)
  135. {
  136. // reset button was not pressed , so we validate and update the options
  137. if (! isset($_POST['reset'])) {
  138. foreach ($this->theme->option_pages as $page) {
  139. foreach ($page['sections'] as $section) {
  140. foreach ($section['fields'] as $field) {
  141. // has this field been saved?
  142. if (isset($new_options[$field['id']])) {
  143. // does it need validation?
  144. if (isset($field['validation'])) {
  145. $validators = explode('|', $field['validation']);
  146. foreach ($validators as $validation) {
  147. // load class for validation
  148. $class_file = OXY_TF_DIR . 'options/validation/Oxygenna' . ucfirst($validation) . '.php';
  149. if (file_exists($class_file)) {
  150. require_once $class_file;
  151. $validator_class = 'Oxy' . ucwords($validation);
  152. if (class_exists($validator_class)) {
  153. $validator = new $validator_class();
  154. $this->options = $validator->validate($field, $this->options, $new_options);
  155. }
  156. }
  157. }
  158. } else {
  159. // no validation so just save whatever we get
  160. $this->options[$field['id']] = $new_options[$field['id']];
  161. }
  162. }
  163. }
  164. }
  165. }
  166. } else {// reset defaults button was pressed , so we reset this page to default options
  167. $url = parse_url(wp_get_referer());
  168. parse_str($url['query'], $path);
  169. $pagename = $path['page'];
  170. foreach ($this->theme->option_pages as $page) {
  171. if ($page['slug'] == $pagename) {
  172. foreach ($page['sections'] as $section) {
  173. foreach ($section['fields'] as $field) {
  174. if (isset($field['default'])) {
  175. $this->options[$field['id']] = $field['default'];
  176. }
  177. }
  178. }
  179. break;
  180. }
  181. }
  182. }
  183. // special case for options that we create conditionally on the fly.
  184. if (isset($new_options['unregistered'])) {
  185. $this->options['unregistered'] = $new_options['unregistered'];
  186. }
  187. return $this->options;
  188. }
  189. /**
  190. * Creates default options
  191. *
  192. * @return void
  193. * @since 1.0
  194. **/
  195. public function create_default_options()
  196. {
  197. $this->options = array();
  198. // create default options
  199. foreach ($this->theme->option_pages as $page) {
  200. foreach ($page['sections'] as $section) {
  201. foreach ($section['fields'] as $field) {
  202. if (isset($field['default'])) {
  203. $this->options[$field['id']] = $field['default'];
  204. }
  205. }
  206. }
  207. }
  208. // save default options
  209. add_option(THEME_SHORT . '-options', $this->options);
  210. }
  211. /**
  212. * Creates option page
  213. *
  214. * @return void
  215. * @since 1.0
  216. **/
  217. public function create_option_pages()
  218. {
  219. // create a new page array using hooks for keys
  220. $pages = array();
  221. // create menus and save returned hook value
  222. $main_menu_slug = null;
  223. foreach ($this->theme->option_pages as $page_data) {
  224. if ($page_data['main_menu'] == true) {
  225. $main_menu_slug = $page_data['slug'];
  226. $hook = add_menu_page($page_data['page_title'], THEME_NAME, 'manage_options', $page_data['slug'], array(&$this , 'option_page_html'), 'dashicons-marker');
  227. $hook = add_submenu_page($main_menu_slug, $page_data['page_title'], $page_data['menu_title'], 'manage_options', $page_data['slug'], array(&$this , 'option_page_html'));
  228. } else {
  229. $hook = add_submenu_page($main_menu_slug, $page_data['page_title'], $page_data['menu_title'], 'manage_options', $page_data['slug'], array(&$this , 'option_page_html'));
  230. }
  231. // store page data using new hook
  232. $pages[$hook] = $page_data;
  233. add_action('load-'.$hook, array(&$this, 'option_page_loaded'));
  234. }
  235. // now store the pages with hooks
  236. $this->theme->option_pages = $pages;
  237. // add action to enqueue scripts for each page
  238. add_action('admin_enqueue_scripts', array(&$this, 'enqueue_scripts'));
  239. }
  240. public function option_page_loaded()
  241. {
  242. if (isset($_GET['settings-updated']) && $_GET['settings-updated'] == true) {
  243. do_action('oxy-options-updated-' . $_GET['page']);
  244. }
  245. }
  246. /**
  247. * Enqueues scripts needed for each option page
  248. *
  249. * @return void
  250. * @since 1.0
  251. **/
  252. public function enqueue_scripts($hook)
  253. {
  254. // if we are on an option page enqueue script and style for options
  255. if (isset($this->theme->option_pages[$hook])) {
  256. // always enqueue base option css
  257. wp_enqueue_style('oxy-option-page');
  258. wp_enqueue_style('jquery-oxygenna-ui-theme');
  259. wp_enqueue_script('theme-options-page', OXY_TF_URI . 'assets/javascripts/theme-options-page.js', array('jquery', 'jquery-ui-tooltip'));
  260. }
  261. // load any option page css
  262. if (isset($this->theme->option_pages[$hook]['stylesheets'])) {
  263. foreach ($this->theme->option_pages[$hook]['stylesheets'] as $css) {
  264. wp_enqueue_style($css['handle'], $css['src'], $css['deps']);
  265. }
  266. }
  267. // load any option page js
  268. if (isset($this->theme->option_pages[$hook]['javascripts'])) {
  269. foreach ($this->theme->option_pages[$hook]['javascripts'] as $js) {
  270. wp_enqueue_script($js['handle'], $js['src'], $js['deps']);
  271. if (isset($js['localize'])) {
  272. wp_localize_script($js['handle'], $js['localize']['object_handle'], $js['localize']['data']);
  273. }
  274. }
  275. }
  276. // now load any option specific js / css
  277. foreach ($this->theme->option_pages as $page_hook => $page_data) {
  278. if ($page_hook == $hook) {
  279. foreach ($page_data['sections'] as $section) {
  280. foreach ($section['fields'] as $field) {
  281. if (isset($field['type'])) {
  282. $new_field = OxygennaOptions::create_option($field);
  283. if ($new_field !== false) {
  284. $new_field->enqueue();
  285. }
  286. }
  287. }
  288. }
  289. }
  290. }
  291. }
  292. /**
  293. * Displays the option page
  294. *
  295. * @return void
  296. * @since 1.0
  297. **/
  298. public function option_page_html()
  299. { ?>
  300. <?php do_action($_GET['page'] . '-before-page'); ?>
  301. <div class="wrap">
  302. <div class="icon32">
  303. <img src="<?php echo OXY_TF_URI . 'images/oxygenna.png' ?>" alt="Oxygenna logo">
  304. </div>
  305. <h2><?php echo get_admin_page_title(); ?></h2>
  306. <?php settings_errors(); ?>
  307. <div id="ajax-errors-here"></div>
  308. <form method="post" action="options.php">
  309. <?php settings_fields(THEME_SHORT . '-options'); ?>
  310. <?php do_settings_sections($_GET['page']); ?>
  311. <div class="submit-footer">
  312. <?php submit_button(); ?>
  313. <?php submit_button(__('Restore Defaults', 'omega-admin-td'), 'secondary', 'reset'); ?>
  314. </div>
  315. </form>
  316. </div>
  317. <?php do_action($_GET['page'] . '-after-page'); ?>
  318. <?php
  319. }
  320. /**
  321. * Creates a single option
  322. *
  323. * @return void
  324. * @since 1.0
  325. **/
  326. public function render_option($field)
  327. {
  328. $value = isset($this->options[$field['id']]) ? $this->options[$field['id']] : null;
  329. $attr = array('name' => THEME_SHORT . '-options[' . $field['id'] . ']');
  330. // create new field
  331. $form_field = OxygennaOptions::create_option($field, $value, $attr);
  332. $form_field->render();
  333. if (isset($field['desc'])) {
  334. echo '</td><td>';
  335. echo '<a class="description" title="' . $field['desc'] . '"><img src="' . OXY_TF_URI . 'assets/images/what.png" alt="Tooltip"></a>';
  336. }
  337. }
  338. /**
  339. * Creates a nice new field for you
  340. *
  341. * @return object field false on error
  342. * @since 1.0
  343. **/
  344. public static function create_option($field, $value = '', $attr = array())
  345. {
  346. if (isset($field['type'])) {
  347. // load class for option type
  348. // if class-file is set by plugin then use the custom class file
  349. $class_file = isset($field['class-file']) ? $field['class-file'] : OXY_TF_DIR . 'inc/options/fields/' . $field['type'] . '/Oxygenna' . ucfirst($field['type']) . '.php';
  350. //print_r($class_file);die();
  351. if (file_exists($class_file)) {
  352. require_once $class_file;
  353. $option_class = 'Oxygenna' . ucwords($field['type']);
  354. if (class_exists($option_class)) {
  355. return new $option_class($field, $value, $attr);
  356. }
  357. }
  358. }
  359. return false;
  360. }
  361. }