PageRenderTime 65ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/nonus/framework/createit/ctNHP_Options.class.php

https://github.com/alniko009/magic
PHP | 380 lines | 220 code | 91 blank | 69 comment | 38 complexity | 52eea7a9c8ba73b62064208487c7c907 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php require_once dirname(__FILE__) . '/../options/options.php';
  2. /**
  3. * Options class
  4. * @author alex
  5. */
  6. class ctNHP_Options extends NHP_Options {
  7. /**
  8. * Main groups
  9. * @var array
  10. */
  11. protected $groups;
  12. /**
  13. * @param array $sections
  14. * @param array $args
  15. * @param array $extra_tabs
  16. */
  17. function __construct($sections = array(), $args = array(), $extra_tabs = array()) {
  18. $defaultGroup = __('General', 'ct_theme');
  19. foreach ($extra_tabs as $k => $t) {
  20. if (!isset($t['group'])) {
  21. $extra_tabs[$k]['group'] = $defaultGroup;
  22. }
  23. }
  24. foreach ($sections as $key => $section) {
  25. if (!isset($section['group'])) {
  26. $section['group'] = 'General';
  27. $sections[$key] = $section;
  28. }
  29. $this->groups[$section['group']] = true;
  30. }
  31. $this->groups = array_keys($this->groups);
  32. $args['footer_credit'] = '';
  33. parent::__construct($sections, $args, $extra_tabs);
  34. }
  35. /**
  36. * Refresh options - adds defaults when option doesn't exist
  37. */
  38. function refresh() {
  39. update_option($this->args['opt_name'], $this->updateMissingValues());
  40. //get the options for use later on
  41. $this->options = get_option($this->args['opt_name'],array());
  42. }
  43. /**
  44. * Import data from string
  45. * @param $data
  46. */
  47. public function import($data){
  48. $imported_options = unserialize(trim($data, '###'));
  49. update_option($this->args['opt_name'], $imported_options);
  50. }
  51. /**
  52. * Updates missing values
  53. * @return array
  54. */
  55. protected function updateMissingValues() {
  56. $default = $this->_default_values();
  57. $currentOptions = get_option($this->args['opt_name'],array());
  58. //find new options with defined values
  59. foreach ($default as $name => $val) {
  60. //here we only add new ones
  61. if (!isset($currentOptions[$name])) {
  62. $currentOptions[$name] = $val;
  63. }
  64. }
  65. return $currentOptions;
  66. }
  67. /**
  68. * Returns setting
  69. * @param $opt_name
  70. * @param null $default
  71. * @return null
  72. */
  73. function get($opt_name, $default = null) {
  74. return ($this->options && array_key_exists($opt_name, $this->options)) ? $this->options[$opt_name] : $default;
  75. }
  76. /**
  77. * Returns options page name
  78. * @return mixed
  79. */
  80. function getOptionsPageName(){
  81. return $this->args['opt_name'];
  82. }
  83. /**
  84. * Return sections
  85. * @return array|mixed|void
  86. */
  87. public function getSections(){
  88. return $this->sections;
  89. }
  90. /**
  91. * Sets sections
  92. * @param $sections
  93. */
  94. public function setSections($sections){
  95. $this->sections = $sections;
  96. }
  97. /**
  98. * Add section at index
  99. * @param $section
  100. * @param $index
  101. * @return array
  102. */
  103. public static function insertSectionAtIndex($sections, $section, $index) {
  104. /*** get the start of the array ***/
  105. $start = array_slice($sections, 0, $index);
  106. /*** get the end of the array ***/
  107. $end = array_slice($sections, $index);
  108. /*** add the new element to the array ***/
  109. $start[] = $section;
  110. /*** glue them back together and return ***/
  111. return array_merge($start, $end);
  112. }
  113. /**
  114. * HTML OUTPUT.
  115. *
  116. * @since NHP_Options 1.0
  117. */
  118. function _options_page_html() {
  119. $groups = $this->groups;
  120. echo '<div class="wrap">';
  121. echo '<div id="' . $this->args['page_icon'] . '" class="icon32"><br/></div>';
  122. echo '<h2 id="nhp-opts-heading">' . get_admin_page_title() . '</h2>';
  123. echo (isset($this->args['intro_text'])) ? $this->args['intro_text'] : '';
  124. do_action('nhp-opts-page-before-form-' . $this->args['opt_name'],$this);
  125. echo '<form method="post" action="options.php" enctype="multipart/form-data" id="nhp-opts-form-wrapper">';
  126. settings_fields($this->args['opt_name'] . '_group');
  127. $this->options['last_tab'] = (isset($_GET['tab']) && !get_transient('nhp-opts-saved')) ? $_GET['tab'] : $this->options['last_tab'];
  128. echo '<input type="hidden" id="last_tab" name="' . $this->args['opt_name'] . '[last_tab]" value="' . $this->options['last_tab'] . '" />';
  129. echo '<div id="nhp-opts-header">';
  130. submit_button('', 'primary', '', false);
  131. submit_button(__('Reset to Defaults', 'nhp-opts'), 'secondary', $this->args['opt_name'] . '[defaults]', false);
  132. echo '<div class="clear"></div><!--clearfix-->';
  133. echo '</div>';
  134. echo '<div id="nhp-main-tabs"><ul>';
  135. foreach ($groups as $key => $main) {
  136. echo '<li><a data-rel="wrapper_' . $key . '" href="#">' . $main . '</a></li>';
  137. }
  138. echo '</ul><div class="clear"></div></div>';
  139. if (isset($_GET['settings-updated']) && $_GET['settings-updated'] == 'true' && get_transient('nhp-opts-saved') == '1') {
  140. if (isset($this->options['imported']) && $this->options['imported'] == 1) {
  141. echo '<div id="nhp-opts-imported">' . apply_filters('nhp-opts-imported-text-' . $this->args['opt_name'], __('<strong>Settings Imported!</strong>', 'nhp-opts')) . '</div>';
  142. } else {
  143. echo '<div id="nhp-opts-save">' . apply_filters('nhp-opts-saved-text-' . $this->args['opt_name'], __('<strong>Settings Saved!</strong>', 'nhp-opts')) . '</div>';
  144. }
  145. delete_transient('nhp-opts-saved');
  146. }
  147. echo '<div id="nhp-opts-field-errors">' . __('<strong><span></span> error(s) were found!</strong>', 'nhp-opts') . '</div>';
  148. echo '<div id="nhp-opts-field-warnings">' . __('<strong><span></span> warning(s) were found!</strong>', 'nhp-opts') . '</div>';
  149. echo '<div class="clear"></div><!--clearfix-->';
  150. foreach ($groups as $key => $main) {
  151. echo '<div class="nhp-main-wrapper">';
  152. echo '<div id="wrapper_' . $key . '" class="nhp-main-wrapper-items">';
  153. echo '<div class="nhp-opts-sidebar">';
  154. echo '<ul class="nhp-opts-group-menu">';
  155. foreach ($this->sections as $k => $section) {
  156. if ($section['group'] == $main) {
  157. if (!isset($section['type']) || $section['type'] != 'divider') {
  158. $icon = (!isset($section['icon'])) ? '<img src="' . $this->url . 'img/glyphicons/glyphicons_019_cogwheel.png" /> ' : '<img src="' . $section['icon'] . '" /> ';
  159. echo '<li id="' . $k . '_section_group_li" class="nhp-opts-group-tab-link-li">';
  160. echo '<a href="javascript:void(0);" id="' . $k . '_section_group_li_a" class="nhp-opts-group-tab-link-a" data-rel="' . $k . '">' . $icon . '<span>' . $section['title'] . '</span></a>';
  161. echo '</li>';
  162. } else {
  163. echo '<li class="divide">&nbsp;</li>';
  164. }
  165. }
  166. }
  167. echo '<li class="divide">&nbsp;</li>';
  168. do_action('nhp-opts-after-section-menu-items-' . $this->args['opt_name'], $this);
  169. //only for general tab
  170. if (true === $this->args['show_import_export'] && $main == 'General') {
  171. echo '<li id="import_export_default_section_group_li" class="nhp-opts-group-tab-link-li">';
  172. echo '<a href="javascript:void(0);" id="import_export_default_section_group_li_a" class="nhp-opts-group-tab-link-a" data-rel="import_export_default"><img src="' . $this->url . 'img/glyphicons/glyphicons_082_roundabout.png" /> <span>' . __('Import / Export', 'nhp-opts') . '</span></a>';
  173. echo '</li>';
  174. echo '<li class="divide">&nbsp;</li>';
  175. }
  176. //if
  177. foreach ($this->extra_tabs as $k => $tab) {
  178. if ($tab['group'] == $main) {
  179. $icon = (!isset($tab['icon'])) ? '<img src="' . $this->url . 'img/glyphicons/glyphicons_019_cogwheel.png" /> ' : '<img src="' . $tab['icon'] . '" /> ';
  180. echo '<li id="' . $k . '_section_group_li" class="nhp-opts-group-tab-link-li">';
  181. echo '<a href="javascript:void(0);" id="' . $k . '_section_group_li_a" class="nhp-opts-group-tab-link-a custom-tab" data-rel="' . $k . '">' . $icon . '<span>' . $tab['title'] . '</span></a>';
  182. echo '</li>';
  183. }
  184. }
  185. if (true === $this->args['dev_mode'] && $main == 'General') {
  186. echo '<li id="dev_mode_default_section_group_li" class="nhp-opts-group-tab-link-li">';
  187. echo '<a href="javascript:void(0);" id="dev_mode_default_section_group_li_a" class="nhp-opts-group-tab-link-a custom-tab" data-rel="dev_mode_default"><img src="' . $this->url . 'img/glyphicons/glyphicons_195_circle_info.png" /> <span>' . __('Dev Mode Info', 'nhp-opts') . '</span></a>';
  188. echo '</li>';
  189. }
  190. //if
  191. echo '</ul>';
  192. echo '</div>';
  193. echo '<div class="nhp-opts-main">';
  194. foreach ($this->sections as $k => $section) {
  195. if ($section['group'] == $main) {
  196. echo '<div id="' . $k . '_section_group' . '" class="nhp-opts-group-tab">';
  197. do_settings_sections($k . '_section_group');
  198. echo '</div>';
  199. }
  200. }
  201. if (true === $this->args['show_import_export'] && $main == 'General') {
  202. echo '<div id="import_export_default_section_group' . '" class="nhp-opts-group-tab">';
  203. echo '<h3>' . __('Import / Export Options', 'nhp-opts') . '</h3>';
  204. echo '<h4>' . __('Import Options', 'nhp-opts') . '</h4>';
  205. echo '<p><a href="javascript:void(0);" id="nhp-opts-import-code-button" class="button-secondary">Import from file</a> <a href="javascript:void(0);" id="nhp-opts-import-link-button" class="button-secondary">Import from URL</a></p>';
  206. echo '<div id="nhp-opts-import-code-wrapper">';
  207. echo '<div class="nhp-opts-section-desc">';
  208. echo '<p class="description" id="import-code-description">' . apply_filters('nhp-opts-import-file-description', __('Input your backup file below and hit Import to restore your sites options from a backup.', 'nhp-opts')) . '</p>';
  209. echo '</div>';
  210. echo '<textarea id="import-code-value" name="' . $this->args['opt_name'] . '[import_code]" class="large-text" rows="8"></textarea>';
  211. echo '</div>';
  212. echo '<div id="nhp-opts-import-link-wrapper">';
  213. echo '<div class="nhp-opts-section-desc">';
  214. echo '<p class="description" id="import-link-description">' . apply_filters('nhp-opts-import-link-description', __('Input the URL to another sites options set and hit Import to load the options from that site.', 'nhp-opts')) . '</p>';
  215. echo '</div>';
  216. echo '<input type="text" id="import-link-value" name="' . $this->args['opt_name'] . '[import_link]" class="large-text" value="" />';
  217. echo '</div>';
  218. echo '<p id="nhp-opts-import-action"><input type="submit" id="nhp-opts-import" name="' . $this->args['opt_name'] . '[import]" class="button-primary" value="' . __('Import', 'nhp-opts') . '"> <span>' . apply_filters('nhp-opts-import-warning', __('WARNING! This will overwrite any existing options, please proceed with caution!', 'nhp-opts')) . '</span></p>';
  219. echo '<div id="import_divide"></div>';
  220. echo '<h4>' . __('Export Options', 'nhp-opts') . '</h4>';
  221. echo '<div class="nhp-opts-section-desc">';
  222. echo '<p class="description">' . apply_filters('nhp-opts-backup-description', __('Here you can copy/download your themes current option settings. Keep this safe as you can use it as a backup should anything go wrong. Or you can use it to restore your settings on this site (or any other site). You also have the handy option to copy the link to yours sites settings. Which you can then use to duplicate on another site', 'nhp-opts')) . '</p>';
  223. echo '</div>';
  224. echo '<p><a href="javascript:void(0);" id="nhp-opts-export-code-copy" class="button-secondary">Copy</a> <a href="' . add_query_arg(array('feed' => 'nhpopts-' . $this->args['opt_name'], 'action' => 'download_options', 'secret' => md5(AUTH_KEY . SECURE_AUTH_KEY)), site_url()) . '" id="nhp-opts-export-code-dl" class="button-primary">Download</a> <a href="javascript:void(0);" id="nhp-opts-export-link" class="button-secondary">Copy Link</a></p>';
  225. $backup_options = $this->options;
  226. $backup_options['nhp-opts-backup'] = '1';
  227. $encoded_options = '###' . serialize($backup_options) . '###';
  228. echo '<textarea class="large-text" id="nhp-opts-export-code" rows="8">';
  229. print_r($encoded_options);
  230. echo '</textarea>';
  231. echo '<input type="text" class="large-text" id="nhp-opts-export-link-value" value="' . add_query_arg(array('feed' => 'nhpopts-' . $this->args['opt_name'], 'secret' => md5(AUTH_KEY . SECURE_AUTH_KEY)), site_url()) . '" />';
  232. echo '</div>';
  233. }
  234. foreach ($this->extra_tabs as $k => $tab) {
  235. echo '<div class="nhp-main-wrapper">';
  236. echo '<div id="wrapper_' . $key . '" class="nhp-main-wrapper-items">';
  237. echo '<div id="' . $k . '_section_group' . '" class="nhp-opts-group-tab">';
  238. echo '<h3>' . $tab['title'] . '</h3>';
  239. echo $tab['content'];
  240. echo '</div>';
  241. echo '</div>';
  242. echo '</div>';
  243. }
  244. if (true === $this->args['dev_mode']) {
  245. echo '<div id="dev_mode_default_section_group' . '" class="nhp-opts-group-tab">';
  246. echo '<h3>' . __('Dev Mode Info', 'nhp-opts') . '</h3>';
  247. echo '<div class="nhp-opts-section-desc">';
  248. echo '<textarea class="large-text" rows="24">' . print_r($this, true) . '</textarea>';
  249. echo '</div>';
  250. echo '</div>';
  251. }
  252. do_action('nhp-opts-after-section-items-' . $this->args['opt_name'], $this);
  253. echo '<div class="clear"></div><!--clearfix-->';
  254. echo '</div>';
  255. echo '<div class="clear"></div><!--clearfix-->';
  256. echo '</div>';
  257. echo '</div>';
  258. }
  259. echo '<div id="nhp-opts-footer">';
  260. if (isset($this->args['share_icons'])) {
  261. echo '<div id="nhp-opts-share">';
  262. foreach ($this->args['share_icons'] as $link) {
  263. $c = isset($link['img']) && $link['img']?'<img src="' . $link['img'] . '"/>':$link['title'];
  264. echo '<a'.(isset($link['style'])?' style="'.$link['style'].'"':'').' href="' . $link['link'] . '" title="' . $link['title'] . '" target="_blank">'.$c.'</a>';
  265. }
  266. echo '</div>';
  267. }
  268. submit_button('', 'primary', '', false);
  269. submit_button(__('Reset to Defaults', 'nhp-opts'), 'secondary', $this->args['opt_name'] . '[defaults]', false);
  270. echo '<div class="clear"></div><!--clearfix-->';
  271. echo '</div>';
  272. echo '</form>';
  273. do_action('nhp-opts-page-after-form-' . $this->args['opt_name']);
  274. echo '<div class="clear"></div><!--clearfix-->';
  275. echo '</div><!--wrap-->';
  276. }
  277. /**
  278. * Section HTML OUTPUT.
  279. *
  280. * @since NHP_Options 1.0
  281. */
  282. function _section_desc($section) {
  283. $id = rtrim($section['id'], '_section');
  284. if (isset($this->sections[$id]['desc']) && !empty($this->sections[$id]['desc'])) {
  285. echo '<div class="nhp-opts-section-desc"><p class="description">' . $this->sections[$id]['desc'] . '</p></div>';
  286. }
  287. }
  288. //function
  289. }