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

/menu-icons/includes/settings.php

https://gitlab.com/hop23typhu/list-theme
PHP | 563 lines | 336 code | 83 blank | 144 comment | 20 complexity | bff5929d104126102127fc9ff9e2a601 MD5 | raw file
  1. <?php
  2. /**
  3. * Settings
  4. *
  5. * @package Menu_Icons
  6. * @author Dzikri Aziz <kvcrvt@gmail.com>
  7. */
  8. /**
  9. * Menu Icons Settings module
  10. */
  11. final class Menu_Icons_Settings {
  12. const UPDATE_KEY = 'menu-icons-settings-update';
  13. const RESET_KEY = 'menu-icons-settings-reset';
  14. const TRANSIENT_KEY = 'menu_icons_message';
  15. /**
  16. * Default setting values
  17. *
  18. * @since 0.3.0
  19. * @var array
  20. * @acess protected
  21. */
  22. protected static $defaults = array(
  23. 'global' => array(
  24. 'icon_types' => array(),
  25. ),
  26. );
  27. /**
  28. * Setting values
  29. *
  30. * @since 0.3.0
  31. * @var array
  32. * @acess protected
  33. */
  34. protected static $settings = array();
  35. /**
  36. * Get setting value
  37. *
  38. * @since 0.3.0
  39. * @return mixed
  40. */
  41. public static function get() {
  42. $args = func_get_args();
  43. return kucrut_get_array_value_deep( self::$settings, $args );
  44. }
  45. /**
  46. * Get setting values and apply sanitation
  47. *
  48. * @since 0.3.0
  49. * @acess private
  50. */
  51. private static function _get() {
  52. $settings = get_option( 'menu-icons', null );
  53. if ( is_null( $settings ) ) {
  54. $settings['global'] = self::$defaults['global'];
  55. }
  56. /**
  57. * Check icon types
  58. *
  59. * A type could be enabled in the settings but disabled by a filter,
  60. * so we need to 'fix' it here.
  61. */
  62. if ( ! empty( $settings['global']['icon_types'] ) ) {
  63. $active_types = array();
  64. $icon_types = Menu_Icons::get( 'icon_types' );
  65. foreach ( (array) $settings['global']['icon_types'] as $index => $id ) {
  66. if ( isset( $icon_types[ $id ] ) ) {
  67. $active_types[] = $id;
  68. }
  69. }
  70. if ( $settings['global']['icon_types'] !== $active_types ) {
  71. $settings['global']['icon_types'] = $active_types;
  72. update_option( 'menu-icons', $settings );
  73. }
  74. }
  75. self::$settings = $settings;
  76. }
  77. /**
  78. * Get menu settings
  79. *
  80. * @since 0.3.0
  81. * @param int $menu_id
  82. * @return array
  83. */
  84. public static function get_menu_settings( $menu_id ) {
  85. $menu_settings = self::get( sprintf( 'menu_%d', $menu_id ) );
  86. $menu_settings = apply_filters( 'menu_icons_menu_settings', $menu_settings, $menu_id );
  87. if ( ! is_array( $menu_settings ) ) {
  88. $menu_settings = array();
  89. }
  90. return $menu_settings;
  91. }
  92. /**
  93. * Settings init
  94. *
  95. * @since 0.3.0
  96. */
  97. public static function init() {
  98. self::$defaults['global']['icon_types'] = array_keys( Menu_Icons::get( 'icon_types' ) );
  99. self::_get();
  100. require_once Menu_Icons::get( 'dir' ) . 'includes/admin.php';
  101. Menu_Icons_Admin_Nav_Menus::init();
  102. add_action( 'load-nav-menus.php', array( __CLASS__, '_load_nav_menus' ), 1 );
  103. add_action( 'wp_ajax_menu_icons_update_settings', array( __CLASS__, '_ajax_menu_icons_update_settings' ) );
  104. }
  105. /**
  106. * Prepare wp-admin/nav-menus.php page
  107. *
  108. * @since 0.3.0
  109. * @wp_hook load-nav-menus.php
  110. */
  111. public static function _load_nav_menus() {
  112. add_action( 'admin_enqueue_scripts', array( __CLASS__, '_enqueue_assets' ), 99 );
  113. /**
  114. * Allow settings meta box to be disabled.
  115. *
  116. * @since 0.4.0
  117. * @param bool $disabled Defaults to FALSE
  118. */
  119. $settings_disabled = apply_filters( 'menu_icons_disable_settings', false );
  120. if ( true === $settings_disabled ) {
  121. return;
  122. }
  123. self::_maybe_update_settings();
  124. self::_add_settings_meta_box();
  125. add_action( 'admin_notices', array( __CLASS__, '_admin_notices' ) );
  126. }
  127. /**
  128. * Update settings
  129. *
  130. * @since 0.3.0
  131. * @access private
  132. * @wp_hook load-nav-menus.php
  133. */
  134. public static function _maybe_update_settings() {
  135. if ( ! empty( $_POST['menu-icons']['settings'] ) ) {
  136. check_admin_referer( self::UPDATE_KEY, self::UPDATE_KEY );
  137. $redirect_url = self::_update_settings( $_POST['menu-icons']['settings'] );
  138. wp_redirect( $redirect );
  139. }
  140. elseif ( ! empty( $_REQUEST[ self::RESET_KEY ] ) ) {
  141. check_admin_referer( self::RESET_KEY, self::RESET_KEY );
  142. wp_redirect( self::_reset_settings() );
  143. }
  144. }
  145. /**
  146. * Update settings
  147. *
  148. * @since 0.7.0
  149. * @access protected
  150. * @param array $values Settings values
  151. * @return string Redirect URL
  152. */
  153. protected static function _update_settings( $values ) {
  154. update_option(
  155. 'menu-icons',
  156. wp_parse_args(
  157. kucrut_validate( $values ),
  158. self::$settings
  159. )
  160. );
  161. set_transient( self::TRANSIENT_KEY, 'updated', 30 );
  162. $redirect_url = remove_query_arg(
  163. array( 'menu-icons-reset' ),
  164. wp_get_referer()
  165. );
  166. return $redirect_url;
  167. }
  168. /**
  169. * Reset settings
  170. *
  171. * @since 0.7.0
  172. * @access protected
  173. * @return string Redirect URL
  174. */
  175. protected static function _reset_settings() {
  176. delete_option( 'menu-icons' );
  177. set_transient( self::TRANSIENT_KEY, 'reset', 30 );
  178. $redirect_url = remove_query_arg(
  179. array( self::RESET_KEY, 'menu-icons-updated' ),
  180. wp_get_referer()
  181. );
  182. return $redirect_url;
  183. }
  184. /**
  185. * Update settings via ajax
  186. *
  187. * @since 0.7.0
  188. * @wp_hook action _ajax_menu_icons_update_settings
  189. */
  190. public static function _ajax_menu_icons_update_settings() {
  191. check_ajax_referer( self::UPDATE_KEY, self::UPDATE_KEY );
  192. if ( empty( $_POST['menu-icons']['settings'] ) ) {
  193. wp_send_json_error();
  194. }
  195. $redirect_url = self::_update_settings( $_POST['menu-icons']['settings'] );
  196. wp_send_json_success( array( 'redirectUrl' => $redirect_url ) );
  197. }
  198. /**
  199. * Print admin notices
  200. *
  201. * @since 0.3.0
  202. * @wp_hook admin_notices
  203. */
  204. public static function _admin_notices() {
  205. $messages = array(
  206. 'updated' => __( '<strong>Menu Icons Settings</strong> have been successfully updated.', 'menu-icons' ),
  207. 'reset' => __( '<strong>Menu Icons Settings</strong> have been successfully reset.', 'menu-icons' ),
  208. );
  209. $message_type = get_transient( self::TRANSIENT_KEY );
  210. if ( ! empty( $message_type ) && ! empty( $messages[ $message_type ] ) ) {
  211. printf(
  212. '<div class="updated"><p>%s</p></div>',
  213. wp_kses( $messages[ $message_type ], array( 'strong' => true ) )
  214. );
  215. }
  216. }
  217. /**
  218. * Settings meta box
  219. *
  220. * @since 0.3.0
  221. * @access private
  222. */
  223. private static function _add_settings_meta_box() {
  224. add_meta_box(
  225. 'menu-icons-settings',
  226. __( 'Menu Icons Settings', 'menu-icons' ),
  227. array( __CLASS__, '_meta_box' ),
  228. 'nav-menus',
  229. 'side',
  230. 'low',
  231. array()
  232. );
  233. }
  234. /**
  235. * Get ID of nav menu being edited
  236. *
  237. * @since %ver
  238. * @return int
  239. */
  240. public static function get_current_menu_id() {
  241. global $nav_menu_selected_id;
  242. if ( defined( 'DOING_AJAX' ) && DOING_AJAX && ! empty( $_POST['menu'] ) ) {
  243. $menu_id = absint( $_POST['menu'] );
  244. }
  245. else {
  246. $menu_id = $nav_menu_selected_id;
  247. }
  248. return $menu_id;
  249. }
  250. /**
  251. * Get settings fields
  252. *
  253. * @since 0.4.0
  254. * @param array $values Values to be applied to each field
  255. * @uses apply_filters() Calls 'menu_icons_settings_fields'.
  256. * @return array
  257. */
  258. public static function get_settings_fields( Array $values = array() ) {
  259. $fields = array(
  260. 'hide_label' => array(
  261. 'id' => 'hide_label',
  262. 'type' => 'select',
  263. 'label' => __( 'Hide Label', 'menu-icons' ),
  264. 'default' => '',
  265. 'choices' => array(
  266. array(
  267. 'value' => '',
  268. 'label' => __( 'No', 'menu-icons' ),
  269. ),
  270. array(
  271. 'value' => '1',
  272. 'label' => __( 'Yes', 'menu-icons' ),
  273. ),
  274. ),
  275. ),
  276. 'position' => array(
  277. 'id' => 'position',
  278. 'type' => 'select',
  279. 'label' => __( 'Position', 'menu-icons' ),
  280. 'default' => 'before',
  281. 'choices' => array(
  282. array(
  283. 'value' => 'before',
  284. 'label' => __( 'Before', 'menu-icons' ),
  285. ),
  286. array(
  287. 'value' => 'after',
  288. 'label' => __( 'After', 'menu-icons' ),
  289. ),
  290. ),
  291. ),
  292. );
  293. $fields = apply_filters( 'menu_icons_settings_fields', $fields );
  294. foreach ( $fields as &$field ) {
  295. if ( isset( $values[ $field['id'] ] ) ) {
  296. $field['value'] = $values[ $field['id'] ];
  297. }
  298. if ( ! isset( $field['value'] ) && isset( $field['default'] ) ) {
  299. $field['value'] = $field['default'];
  300. }
  301. }
  302. unset( $field );
  303. return $fields;
  304. }
  305. /**
  306. * Get settings sections
  307. *
  308. * @since 0.3.0
  309. * @uses apply_filters() Calls 'menu_icons_settings_sections'.
  310. * @return array
  311. */
  312. public static function get_fields() {
  313. $menu_id = self::get_current_menu_id();
  314. $icon_types = array();
  315. foreach ( Menu_Icons::get( 'icon_types' ) as $id => $props ) {
  316. $icon_types[ $id ] = $props['label'];
  317. }
  318. $sections = array(
  319. 'global' => array(
  320. 'id' => 'global',
  321. 'title' => __( 'Global', 'menu-icons' ),
  322. 'description' => __( 'Global settings', 'menu-icons' ),
  323. 'fields' => array(
  324. array(
  325. 'id' => 'icon_types',
  326. 'type' => 'checkbox',
  327. 'label' => __( 'Icon Types', 'menu-icons' ),
  328. 'choices' => $icon_types,
  329. 'value' => self::get( 'global', 'icon_types' ),
  330. ),
  331. ),
  332. 'args' => array(),
  333. ),
  334. );
  335. if ( ! empty( $menu_id ) ) {
  336. $menu_term = get_term( $menu_id, 'nav_menu' );
  337. $menu_key = sprintf( 'menu_%d', $menu_id );
  338. $menu_settings = self::get_menu_settings( $menu_id );
  339. $sections['menu'] = array(
  340. 'id' => $menu_key,
  341. 'title' => __( 'Current Menu', 'menu-icons' ),
  342. 'description' => sprintf(
  343. __( '"%s" menu settings', 'menu-icons' ),
  344. apply_filters( 'single_term_title', $menu_term->name )
  345. ),
  346. 'fields' => self::get_settings_fields( $menu_settings ),
  347. 'args' => array( 'inline_description' => true ),
  348. );
  349. }
  350. return apply_filters( 'menu_icons_settings_sections', $sections, $menu_id );
  351. }
  352. /**
  353. * Get processed settings fields
  354. *
  355. * @since 0.3.0
  356. * @access private
  357. * @return array
  358. */
  359. private static function _get_fields() {
  360. if ( ! class_exists( 'Kucrut_Form_Field' ) ) {
  361. require_once Menu_Icons::get( 'dir' ) . 'includes/library/form-fields.php';
  362. }
  363. $keys = array( 'menu-icons', 'settings' );
  364. $sections = self::get_fields();
  365. foreach ( $sections as &$section ) {
  366. $_keys = array_merge( $keys, array( $section['id'] ) );
  367. $_args = array_merge( array( 'keys' => $_keys ), $section['args'] );
  368. foreach ( $section['fields'] as &$field ) {
  369. $field = Kucrut_Form_Field::create( $field, $_args );
  370. }
  371. unset( $field );
  372. }
  373. unset( $section );
  374. return $sections;
  375. }
  376. /**
  377. * Settings meta box
  378. *
  379. * @since 0.3.0
  380. */
  381. public static function _meta_box() {
  382. ?>
  383. <div class="taxonomydiv">
  384. <ul id="menu-icons-settings-tabs" class="taxonomy-tabs add-menu-item-tabs hide-if-no-js">
  385. <?php foreach ( self::get_fields() as $section ) : ?>
  386. <?php printf(
  387. '<li><a href="#" title="%s" class="mi-settings-nav-tab" data-type="menu-icons-settings-%s">%s</a></li>',
  388. esc_attr( $section['description'] ),
  389. esc_attr( $section['id'] ),
  390. esc_html( $section['title'] )
  391. ) ?>
  392. <?php endforeach ?>
  393. <?php printf(
  394. '<li><a href="#" class="mi-settings-nav-tab" data-type="menu-icons-settings-extensions">%s</a></li>',
  395. __( 'Extensions', 'menu-icons' )
  396. ) ?>
  397. </ul>
  398. <?php foreach ( self::_get_fields() as $section_index => $section ) : ?>
  399. <div id="menu-icons-settings-<?php echo esc_attr( $section['id'] ) ?>" class="tabs-panel _<?php echo esc_attr( $section_index ) ?>">
  400. <h4 class="hide-if-js"><?php echo esc_html( $section['title'] ) ?></h4>
  401. <?php foreach ( $section['fields'] as $field ) : ?>
  402. <div class="_field">
  403. <?php printf(
  404. '<label for="%s" class="_main">%s</label>',
  405. esc_attr( $field->id ),
  406. esc_html( $field->label )
  407. ) ?>
  408. <?php $field->render() ?>
  409. </div>
  410. <?php endforeach; ?>
  411. </div>
  412. <?php endforeach; ?>
  413. <div id="menu-icons-settings-extensions" class="tabs-panel _extensions">
  414. <h4 class="hide-if-js"><?php echo esc_html__( 'Extensions', 'menu-icons' ) ?></h4>
  415. <ul>
  416. <li><a target="_blank" href="http://wordpress.org/plugins/menu-icons-icomoon/">IcoMoon</a></li>
  417. </ul>
  418. </div>
  419. </div>
  420. <p class="submitbox button-controls">
  421. <?php wp_nonce_field( self::UPDATE_KEY, self::UPDATE_KEY ) ?>
  422. <span class="list-controls">
  423. <?php printf(
  424. '<a href="%s" title="%s" class="select-all submitdelete">%s</a>',
  425. esc_url(
  426. wp_nonce_url(
  427. admin_url( '/nav-menus.php' ),
  428. self::RESET_KEY,
  429. self::RESET_KEY
  430. )
  431. ),
  432. esc_attr__( 'Discard all changes and reset to default state', 'menu-icons' ),
  433. esc_html__( 'Reset', 'menu-icons' )
  434. ) ?>
  435. </span>
  436. <span class="add-to-menu">
  437. <span class="spinner"></span>
  438. <?php submit_button(
  439. __( 'Save Settings', 'menu-icons' ),
  440. 'secondary',
  441. 'menu-item-settings-save',
  442. false
  443. ) ?>
  444. </span>
  445. </p>
  446. <?php
  447. }
  448. /**
  449. * Enqueue scripts & styles for admin page
  450. *
  451. * @since 0.3.0
  452. * @wp_hook action admin_enqueue_scripts
  453. */
  454. public static function _enqueue_assets() {
  455. $suffix = Menu_Icons::get_script_suffix();
  456. wp_enqueue_style(
  457. 'menu-icons',
  458. Menu_Icons::get( 'url' ) . 'css/admin' . $suffix . '.css',
  459. false,
  460. Menu_Icons::VERSION
  461. );
  462. wp_register_script(
  463. 'kucrut-jquery-input-dependencies',
  464. Menu_Icons::get( 'url' ) . 'js/input-dependencies' . $suffix . '.js',
  465. array( 'jquery' ),
  466. '0.1.0',
  467. true
  468. );
  469. if ( ! empty( self::$settings['global']['icon_types'] ) ) {
  470. wp_enqueue_media();
  471. }
  472. wp_enqueue_script(
  473. 'menu-icons',
  474. Menu_Icons::get( 'url' ) . 'js/admin' . $suffix . '.js',
  475. array( 'kucrut-jquery-input-dependencies' ),
  476. Menu_Icons::VERSION,
  477. true
  478. );
  479. }
  480. }