wp/wp-content/plugins/option-tree/includes/class-ot-settings.php
changeset 11 bf1778c34b9a
equal deleted inserted replaced
10:372f2766ea20 11:bf1778c34b9a
       
     1 <?php
       
     2 /**
       
     3  * OptionTree Settings.
       
     4  *
       
     5  * @package OptionTree
       
     6  */
       
     7 
       
     8 if ( ! defined( 'OT_VERSION' ) ) {
       
     9 	exit( 'No direct script access allowed' );
       
    10 }
       
    11 
       
    12 if ( ! class_exists( 'OT_Settings' ) ) {
       
    13 
       
    14 	/**
       
    15 	 * OptionTree Settings class.
       
    16 	 *
       
    17 	 * This class loads all the methods and helpers specific to a Settings page.
       
    18 	 */
       
    19 	class OT_Settings {
       
    20 
       
    21 		/**
       
    22 		 * An array of options.
       
    23 		 *
       
    24 		 * @var array
       
    25 		 */
       
    26 		private $options;
       
    27 
       
    28 		/**
       
    29 		 * Page hook for targeting admin page.
       
    30 		 *
       
    31 		 * @var string
       
    32 		 */
       
    33 		private $page_hook;
       
    34 
       
    35 		/**
       
    36 		 * Constructor
       
    37 		 *
       
    38 		 * @param array $args An array of options.
       
    39 		 *
       
    40 		 * @access public
       
    41 		 * @since  2.0
       
    42 		 */
       
    43 		public function __construct( $args ) {
       
    44 
       
    45 			$this->options = $args;
       
    46 
       
    47 			// Return early if not viewing an admin page or no options.
       
    48 			if ( ! is_admin() || ! is_array( $this->options ) ) {
       
    49 				return false;
       
    50 			}
       
    51 
       
    52 			// Load everything.
       
    53 			$this->hooks();
       
    54 		}
       
    55 
       
    56 		/**
       
    57 		 * Execute the WordPress Hooks
       
    58 		 *
       
    59 		 * @access public
       
    60 		 * @since  2.0
       
    61 		 */
       
    62 		public function hooks() {
       
    63 
       
    64 			/**
       
    65 			 * Filter the `admin_menu` action hook priority.
       
    66 			 *
       
    67 			 * @since 2.5.0
       
    68 			 *
       
    69 			 * @param int $priority The priority. Default '10'.
       
    70 			 */
       
    71 			$priority = apply_filters( 'ot_admin_menu_priority', 10 );
       
    72 
       
    73 			// Add pages & menu items.
       
    74 			add_action( 'admin_menu', array( $this, 'add_page' ), $priority );
       
    75 
       
    76 			// Register sections.
       
    77 			add_action( 'admin_init', array( $this, 'add_sections' ) );
       
    78 
       
    79 			// Register settings.
       
    80 			add_action( 'admin_init', array( $this, 'add_settings' ) );
       
    81 
       
    82 			// Reset options.
       
    83 			add_action( 'admin_init', array( $this, 'reset_options' ), 10 );
       
    84 
       
    85 			// Initialize settings.
       
    86 			add_action( 'admin_init', array( $this, 'initialize_settings' ), 11 );
       
    87 		}
       
    88 
       
    89 		/**
       
    90 		 * Loads each admin page
       
    91 		 *
       
    92 		 * @return bool
       
    93 		 *
       
    94 		 * @access public
       
    95 		 * @since  2.0
       
    96 		 */
       
    97 		public function add_page() {
       
    98 
       
    99 			// Loop through options.
       
   100 			foreach ( (array) $this->options as $option ) {
       
   101 
       
   102 				// Loop through pages.
       
   103 				foreach ( (array) $this->get_pages( $option ) as $page ) {
       
   104 
       
   105 					/**
       
   106 					 * Theme Check... stop nagging me about this kind of stuff.
       
   107 					 * The damn admin pages are required for OT to function, duh!
       
   108 					 */
       
   109 					$theme_check_bs  = 'add_menu_' . 'page'; // phpcs:ignore
       
   110 					$theme_check_bs2 = 'add_submenu_' . 'page'; // phpcs:ignore
       
   111 
       
   112 					// Load page in WP top level menu.
       
   113 					if ( ! isset( $page['parent_slug'] ) || empty( $page['parent_slug'] ) ) {
       
   114 						$page_hook = $theme_check_bs(
       
   115 							$page['page_title'],
       
   116 							$page['menu_title'],
       
   117 							$page['capability'],
       
   118 							$page['menu_slug'],
       
   119 							array( $this, 'display_page' ),
       
   120 							$page['icon_url'],
       
   121 							$page['position']
       
   122 						);
       
   123 
       
   124 						// Load page in WP sub menu.
       
   125 					} else {
       
   126 						$page_hook = $theme_check_bs2(
       
   127 							$page['parent_slug'],
       
   128 							$page['page_title'],
       
   129 							$page['menu_title'],
       
   130 							$page['capability'],
       
   131 							$page['menu_slug'],
       
   132 							array( $this, 'display_page' )
       
   133 						);
       
   134 					}
       
   135 
       
   136 					// Only load if not a hidden page.
       
   137 					if ( ! isset( $page['hidden_page'] ) ) {
       
   138 
       
   139 						// Associate $page_hook with page id.
       
   140 						$this->page_hook[ $page['id'] ] = $page_hook;
       
   141 
       
   142 						// Add scripts.
       
   143 						add_action( 'admin_print_scripts-' . $page_hook, array( $this, 'scripts' ) );
       
   144 
       
   145 						// Add styles.
       
   146 						add_action( 'admin_print_styles-' . $page_hook, array( $this, 'styles' ) );
       
   147 
       
   148 						// Add contextual help.
       
   149 						add_action( 'load-' . $page_hook, array( $this, 'help' ) );
       
   150 					}
       
   151 				}
       
   152 			}
       
   153 
       
   154 			return false;
       
   155 		}
       
   156 
       
   157 		/**
       
   158 		 * Loads the scripts
       
   159 		 *
       
   160 		 * @access public
       
   161 		 * @since  2.0
       
   162 		 */
       
   163 		public function scripts() {
       
   164 			ot_admin_scripts();
       
   165 		}
       
   166 
       
   167 		/**
       
   168 		 * Loads the styles
       
   169 		 *
       
   170 		 * @access public
       
   171 		 * @since  2.0
       
   172 		 */
       
   173 		public function styles() {
       
   174 			ot_admin_styles();
       
   175 		}
       
   176 
       
   177 		/**
       
   178 		 * Loads the contextual help for each page
       
   179 		 *
       
   180 		 * @return bool
       
   181 		 *
       
   182 		 * @access public
       
   183 		 * @since  2.0
       
   184 		 */
       
   185 		public function help() {
       
   186 			$screen = get_current_screen();
       
   187 
       
   188 			// Loop through options.
       
   189 			foreach ( (array) $this->options as $option ) {
       
   190 
       
   191 				// Loop through pages.
       
   192 				foreach ( (array) $this->get_pages( $option ) as $page ) {
       
   193 
       
   194 					// Verify page.
       
   195 					if ( ! isset( $page['hidden_page'] ) && $screen->id === $this->page_hook[ $page['id'] ] ) {
       
   196 
       
   197 						// Set up the help tabs.
       
   198 						if ( ! empty( $page['contextual_help']['content'] ) ) {
       
   199 							foreach ( $page['contextual_help']['content'] as $contextual_help ) {
       
   200 								$screen->add_help_tab(
       
   201 									array(
       
   202 										'id'      => esc_attr( $contextual_help['id'] ),
       
   203 										'title'   => esc_attr( $contextual_help['title'] ),
       
   204 										'content' => htmlspecialchars_decode( $contextual_help['content'] ),
       
   205 									)
       
   206 								);
       
   207 							}
       
   208 						}
       
   209 
       
   210 						// Set up the help sidebar.
       
   211 						if ( ! empty( $page['contextual_help']['sidebar'] ) ) {
       
   212 							$screen->set_help_sidebar( htmlspecialchars_decode( $page['contextual_help']['sidebar'] ) );
       
   213 						}
       
   214 					}
       
   215 				}
       
   216 			}
       
   217 
       
   218 			return false;
       
   219 		}
       
   220 
       
   221 		/**
       
   222 		 * Loads the content for each page
       
   223 		 *
       
   224 		 * @access public
       
   225 		 * @since  2.0
       
   226 		 */
       
   227 		public function display_page() {
       
   228 			$screen = get_current_screen();
       
   229 
       
   230 			// Loop through settings.
       
   231 			foreach ( (array) $this->options as $option ) {
       
   232 
       
   233 				// Loop through pages.
       
   234 				foreach ( (array) $this->get_pages( $option ) as $page ) {
       
   235 
       
   236 					// Verify page.
       
   237 					if ( ! isset( $page['hidden_page'] ) && $screen->id === $this->page_hook[ $page['id'] ] ) {
       
   238 
       
   239 						$show_buttons = isset( $page['show_buttons'] ) && false === $page['show_buttons'] ? false : true;
       
   240 
       
   241 						// Update active layout content.
       
   242 						if ( isset( $_REQUEST['settings-updated'] ) && true === filter_var( wp_unslash( $_REQUEST['settings-updated'] ), FILTER_VALIDATE_BOOLEAN ) ) { // phpcs:ignore
       
   243 
       
   244 							$layouts = get_option( ot_layouts_id() );
       
   245 
       
   246 							// Has active layout.
       
   247 							if ( isset( $layouts['active_layout'] ) ) {
       
   248 								$option_tree                          = get_option( $option['id'], array() );
       
   249 								$layouts[ $layouts['active_layout'] ] = ot_encode( $option_tree );
       
   250 								update_option( ot_layouts_id(), $layouts );
       
   251 							}
       
   252 						}
       
   253 
       
   254 						echo '<div class="wrap settings-wrap" id="page-' . esc_attr( $page['id'] ) . '">';
       
   255 
       
   256 						echo '<h2>' . wp_kses_post( $page['page_title'] ) . '</h2>';
       
   257 
       
   258 						echo ot_alert_message( $page ); // phpcs:ignore
       
   259 
       
   260 						settings_errors( 'option-tree' );
       
   261 
       
   262 						// Header.
       
   263 						echo '<div id="option-tree-header-wrap">';
       
   264 
       
   265 						echo '<ul id="option-tree-header">';
       
   266 
       
   267 						$link = '<a href="https://wordpress.org/plugins/option-tree/" target="_blank">' . esc_html__( 'OptionTree', 'option-tree' ) . '</a>';
       
   268 						echo '<li id="option-tree-logo">' . wp_kses_post( apply_filters( 'ot_header_logo_link', $link, $page['id'] ) ) . '</li>';
       
   269 
       
   270 						echo '<li id="option-tree-version"><span>' . esc_html( apply_filters( 'ot_header_version_text', 'OptionTree ' . OT_VERSION, $page['id'] ) ) . '</span></li>';
       
   271 
       
   272 						// Add additional theme specific links here.
       
   273 						do_action( 'ot_header_list', $page['id'] );
       
   274 
       
   275 						echo '</ul>';
       
   276 
       
   277 						// Layouts form.
       
   278 						if ( 'ot_theme_options' === $page['id'] && true === OT_SHOW_NEW_LAYOUT ) {
       
   279 							ot_theme_options_layouts_form();
       
   280 						}
       
   281 
       
   282 						echo '</div>';
       
   283 
       
   284 						// Remove forms on the custom settings pages.
       
   285 						if ( $show_buttons ) {
       
   286 
       
   287 							echo '<form action="options.php" method="post" id="option-tree-settings-api">';
       
   288 
       
   289 							settings_fields( $option['id'] );
       
   290 						} else {
       
   291 
       
   292 							echo '<div id="option-tree-settings-api">';
       
   293 						}
       
   294 
       
   295 						// Sub Header.
       
   296 						echo '<div id="option-tree-sub-header">';
       
   297 
       
   298 						if ( $show_buttons ) {
       
   299 							echo '<button class="option-tree-ui-button button button-primary right">' . esc_html( $page['button_text'] ) . '</button>';
       
   300 						}
       
   301 
       
   302 						echo '</div>';
       
   303 
       
   304 						// Navigation.
       
   305 						echo '<div class="ui-tabs">';
       
   306 
       
   307 						// Check for sections.
       
   308 						if ( isset( $page['sections'] ) && 0 < count( $page['sections'] ) ) {
       
   309 
       
   310 							echo '<ul class="ui-tabs-nav">';
       
   311 
       
   312 							// Loop through page sections.
       
   313 							foreach ( (array) $page['sections'] as $section ) {
       
   314 								echo '<li id="tab_' . esc_attr( $section['id'] ) . '"><a href="#section_' . esc_attr( $section['id'] ) . '">' . wp_kses_post( $section['title'] ) . '</a></li>';
       
   315 							}
       
   316 
       
   317 							echo '</ul>';
       
   318 						}
       
   319 
       
   320 						// Sections.
       
   321 						echo '<div id="poststuff" class="metabox-holder">';
       
   322 
       
   323 						echo '<div id="post-body">';
       
   324 
       
   325 						echo '<div id="post-body-content">';
       
   326 
       
   327 						$this->do_settings_sections( isset( $_GET['page'] ) ? $_GET['page'] : '' ); // phpcs:ignore
       
   328 
       
   329 						echo '</div>';
       
   330 
       
   331 						echo '</div>';
       
   332 
       
   333 						echo '</div>';
       
   334 
       
   335 						echo '<div class="clear"></div>';
       
   336 
       
   337 						echo '</div>';
       
   338 
       
   339 						// Buttons.
       
   340 						if ( $show_buttons ) {
       
   341 
       
   342 							echo '<div class="option-tree-ui-buttons">';
       
   343 
       
   344 							echo '<button class="option-tree-ui-button button button-primary right">' . esc_html( $page['button_text'] ) . '</button>';
       
   345 
       
   346 							echo '</div>';
       
   347 						}
       
   348 
       
   349 						echo $show_buttons ? '</form>' : '</div>';
       
   350 
       
   351 						// Reset button.
       
   352 						if ( $show_buttons ) {
       
   353 
       
   354 							echo '<form method="post" action="' . esc_url_raw( str_replace( '&settings-updated=true', '', $_SERVER['REQUEST_URI'] ) ) . '">'; // phpcs:ignore
       
   355 
       
   356 							// Form nonce.
       
   357 							wp_nonce_field( 'option_tree_reset_form', 'option_tree_reset_nonce' );
       
   358 
       
   359 							echo '<input type="hidden" name="action" value="reset" />';
       
   360 
       
   361 							echo '<button type="submit" class="option-tree-ui-button button button-secondary left reset-settings" title="' . esc_html__( 'Reset Options', 'option-tree' ) . '">' . esc_html__( 'Reset Options', 'option-tree' ) . '</button>';
       
   362 
       
   363 							echo '</form>';
       
   364 						}
       
   365 
       
   366 						echo '</div>';
       
   367 					}
       
   368 				}
       
   369 			}
       
   370 
       
   371 			return false;
       
   372 		}
       
   373 
       
   374 		/**
       
   375 		 * Adds sections to the page
       
   376 		 *
       
   377 		 * @return bool
       
   378 		 *
       
   379 		 * @access public
       
   380 		 * @since  2.0
       
   381 		 */
       
   382 		public function add_sections() {
       
   383 
       
   384 			// Loop through options.
       
   385 			foreach ( (array) $this->options as $option ) {
       
   386 
       
   387 				// Loop through pages.
       
   388 				foreach ( (array) $this->get_pages( $option ) as $page ) {
       
   389 
       
   390 					// Loop through page sections.
       
   391 					foreach ( (array) $this->get_sections( $page ) as $section ) {
       
   392 
       
   393 						// Add each section.
       
   394 						add_settings_section(
       
   395 							$section['id'],
       
   396 							$section['title'],
       
   397 							array( $this, 'display_section' ),
       
   398 							$page['menu_slug']
       
   399 						);
       
   400 
       
   401 					}
       
   402 				}
       
   403 			}
       
   404 
       
   405 			return false;
       
   406 		}
       
   407 
       
   408 		/**
       
   409 		 * Callback for add_settings_section()
       
   410 		 *
       
   411 		 * @access public
       
   412 		 * @since  2.0
       
   413 		 */
       
   414 		public function display_section() {
       
   415 			/* currently pointless */
       
   416 		}
       
   417 
       
   418 		/**
       
   419 		 * Add settings the the page
       
   420 		 *
       
   421 		 * @return bool
       
   422 		 *
       
   423 		 * @access public
       
   424 		 * @since  2.0
       
   425 		 */
       
   426 		public function add_settings() {
       
   427 
       
   428 			// Loop through options.
       
   429 			foreach ( (array) $this->options as $option ) {
       
   430 
       
   431 				register_setting( $option['id'], $option['id'], array( $this, 'sanitize_callback' ) );
       
   432 
       
   433 				// Loop through pages.
       
   434 				foreach ( (array) $this->get_pages( $option ) as $page ) {
       
   435 
       
   436 					// Loop through page settings.
       
   437 					foreach ( (array) $this->get_the_settings( $page ) as $setting ) {
       
   438 
       
   439 						// Skip if missing setting ID, label, or section.
       
   440 						if ( ! isset( $setting['id'] ) || ! isset( $setting['label'] ) || ! isset( $setting['section'] ) ) {
       
   441 							continue;
       
   442 						}
       
   443 
       
   444 						// Add get_option param to the array.
       
   445 						$setting['get_option'] = $option['id'];
       
   446 
       
   447 						// Add each setting.
       
   448 						add_settings_field(
       
   449 							$setting['id'],
       
   450 							$setting['label'],
       
   451 							array( $this, 'display_setting' ),
       
   452 							$page['menu_slug'],
       
   453 							$setting['section'],
       
   454 							$setting
       
   455 						);
       
   456 					}
       
   457 				}
       
   458 			}
       
   459 
       
   460 			return false;
       
   461 		}
       
   462 
       
   463 		/**
       
   464 		 * Callback for add_settings_field() to build each setting by type
       
   465 		 *
       
   466 		 * @param  array $args Setting object array.
       
   467 		 *
       
   468 		 * @access public
       
   469 		 * @since  2.0
       
   470 		 */
       
   471 		public function display_setting( $args = array() ) {
       
   472 			extract( $args ); // phpcs:ignore
       
   473 
       
   474 			// Get current saved data.
       
   475 			$options = get_option( $get_option, false );
       
   476 
       
   477 			// Set field value.
       
   478 			$field_value = isset( $options[ $id ] ) ? $options[ $id ] : '';
       
   479 
       
   480 			// Set standard value.
       
   481 			if ( isset( $std ) ) {
       
   482 				$field_value = ot_filter_std_value( $field_value, $std );
       
   483 			}
       
   484 
       
   485 			// Allow the descriptions to be filtered before being displayed.
       
   486 			$desc = apply_filters( 'ot_filter_description', ( isset( $desc ) ? $desc : '' ), $id );
       
   487 
       
   488 			// Build the arguments array.
       
   489 			$_args = array(
       
   490 				'type'               => $type,
       
   491 				'field_id'           => $id,
       
   492 				'field_name'         => $get_option . '[' . $id . ']',
       
   493 				'field_value'        => $field_value,
       
   494 				'field_desc'         => $desc,
       
   495 				'field_std'          => isset( $std ) ? $std : '',
       
   496 				'field_rows'         => isset( $rows ) && ! empty( $rows ) ? $rows : 15,
       
   497 				'field_post_type'    => isset( $post_type ) && ! empty( $post_type ) ? $post_type : 'post',
       
   498 				'field_taxonomy'     => isset( $taxonomy ) && ! empty( $taxonomy ) ? $taxonomy : 'category',
       
   499 				'field_min_max_step' => isset( $min_max_step ) && ! empty( $min_max_step ) ? $min_max_step : '0,100,1',
       
   500 				'field_condition'    => isset( $condition ) && ! empty( $condition ) ? $condition : '',
       
   501 				'field_operator'     => isset( $operator ) && ! empty( $operator ) ? $operator : 'and',
       
   502 				'field_class'        => isset( $class ) ? $class : '',
       
   503 				'field_choices'      => isset( $choices ) && ! empty( $choices ) ? $choices : array(),
       
   504 				'field_settings'     => isset( $settings ) && ! empty( $settings ) ? $settings : array(),
       
   505 				'post_id'            => ot_get_media_post_ID(),
       
   506 				'get_option'         => $get_option,
       
   507 			);
       
   508 
       
   509 			// Limit DB queries for Google Fonts.
       
   510 			if ( 'google-fonts' === $type ) {
       
   511 				ot_fetch_google_fonts();
       
   512 				ot_set_google_fonts( $id, $field_value );
       
   513 			}
       
   514 
       
   515 			// Get the option HTML.
       
   516 			echo ot_display_by_type( $_args ); // phpcs:ignore
       
   517 		}
       
   518 
       
   519 		/**
       
   520 		 * Sets the option standards if nothing yet exists.
       
   521 		 *
       
   522 		 * @access public
       
   523 		 * @since  2.0
       
   524 		 */
       
   525 		public function initialize_settings() {
       
   526 
       
   527 			// Loop through options.
       
   528 			foreach ( (array) $this->options as $option ) {
       
   529 
       
   530 				// Skip if option is already set.
       
   531 				if ( isset( $option['id'] ) && get_option( $option['id'], false ) ) {
       
   532 					return false;
       
   533 				}
       
   534 
       
   535 				$defaults = array();
       
   536 
       
   537 				// Loop through pages.
       
   538 				foreach ( (array) $this->get_pages( $option ) as $page ) {
       
   539 
       
   540 					// Loop through page settings.
       
   541 					foreach ( (array) $this->get_the_settings( $page ) as $setting ) {
       
   542 
       
   543 						if ( isset( $setting['std'] ) ) {
       
   544 
       
   545 							$defaults[ $setting['id'] ] = ot_validate_setting( $setting['std'], $setting['type'], $setting['id'] );
       
   546 						}
       
   547 					}
       
   548 				}
       
   549 
       
   550 				update_option( $option['id'], $defaults );
       
   551 			}
       
   552 
       
   553 			return false;
       
   554 		}
       
   555 
       
   556 		/**
       
   557 		 * Sanitize callback for register_setting()
       
   558 		 *
       
   559 		 * @param mixed $input The setting input.
       
   560 		 * @return string
       
   561 		 *
       
   562 		 * @access public
       
   563 		 * @since  2.0
       
   564 		 */
       
   565 		public function sanitize_callback( $input ) {
       
   566 
       
   567 			// Store the post global for use later.
       
   568 			$post_global = $_POST; // phpcs:ignore
       
   569 
       
   570 			// Loop through options.
       
   571 			foreach ( (array) $this->options as $option ) {
       
   572 
       
   573 				// Loop through pages.
       
   574 				foreach ( (array) $this->get_pages( $option ) as $page ) {
       
   575 
       
   576 					// Loop through page settings.
       
   577 					foreach ( (array) $this->get_the_settings( $page ) as $setting ) {
       
   578 
       
   579 						// Verify setting has a type & value.
       
   580 						if ( isset( $setting['type'] ) && isset( $input[ $setting['id'] ] ) ) {
       
   581 
       
   582 							// Get the defaults.
       
   583 							$current_settings = get_option( ot_settings_id() );
       
   584 							$current_options  = get_option( $option['id'] );
       
   585 
       
   586 							// Validate setting.
       
   587 							if ( is_array( $input[ $setting['id'] ] ) && in_array( $setting['type'], array( 'list-item', 'slider' ), true ) ) {
       
   588 
       
   589 								// Required title setting.
       
   590 								$required_setting = array(
       
   591 									array(
       
   592 										'id'        => 'title',
       
   593 										'label'     => __( 'Title', 'option-tree' ),
       
   594 										'desc'      => '',
       
   595 										'std'       => '',
       
   596 										'type'      => 'text',
       
   597 										'rows'      => '',
       
   598 										'class'     => 'option-tree-setting-title',
       
   599 										'post_type' => '',
       
   600 										'choices'   => array(),
       
   601 									),
       
   602 								);
       
   603 
       
   604 								// Convert the settings to an array.
       
   605 								$settings = isset( $post_global[ $setting['id'] . '_settings_array' ] ) ? ot_decode( $post_global[ $setting['id'] . '_settings_array' ] ) : array();
       
   606 
       
   607 								// Settings are empty for some odd reason get the defaults.
       
   608 								if ( empty( $settings ) ) {
       
   609 									$settings = 'slider' === $setting['type'] ? ot_slider_settings( $setting['id'] ) : ot_list_item_settings( $setting['id'] );
       
   610 								}
       
   611 
       
   612 								// Merge the two settings arrays.
       
   613 								$settings = array_merge( $required_setting, $settings );
       
   614 
       
   615 								// Create an empty WPML id array.
       
   616 								$wpml_ids = array();
       
   617 
       
   618 								foreach ( $input[ $setting['id'] ] as $k => $setting_array ) {
       
   619 
       
   620 									$has_value = false;
       
   621 									foreach ( $settings as $sub_setting ) {
       
   622 
       
   623 										// Setup the WPML ID.
       
   624 										$wpml_id = $setting['id'] . '_' . $sub_setting['id'] . '_' . $k;
       
   625 
       
   626 										// Add id to array.
       
   627 										$wpml_ids[] = $wpml_id;
       
   628 
       
   629 										/* verify sub setting has a type & value */
       
   630 										if ( isset( $sub_setting['type'] ) && isset( $input[ $setting['id'] ][ $k ][ $sub_setting['id'] ] ) ) {
       
   631 
       
   632 											// Validate setting.
       
   633 											$input[ $setting['id'] ][ $k ][ $sub_setting['id'] ] = ot_validate_setting( $input[ $setting['id'] ][ $k ][ $sub_setting['id'] ], $sub_setting['type'], $sub_setting['id'], $wpml_id );
       
   634 											$has_value = true;
       
   635 										}
       
   636 									}
       
   637 
       
   638 									if ( ! $has_value ) {
       
   639 										unset( $input[ $setting['id'] ][ $k ] );
       
   640 									}
       
   641 								}
       
   642 							} elseif ( is_array( $input[ $setting['id'] ] ) && 'social-links' === $setting['type'] ) {
       
   643 
       
   644 								// Convert the settings to an array.
       
   645 								$settings = isset( $post_global[ $setting['id'] . '_settings_array' ] ) ? ot_decode( $post_global[ $setting['id'] . '_settings_array' ] ) : array();
       
   646 
       
   647 								// Settings are empty get the defaults.
       
   648 								if ( empty( $settings ) ) {
       
   649 									$settings = ot_social_links_settings( $setting['id'] );
       
   650 								}
       
   651 
       
   652 								// Create an empty WPML id array.
       
   653 								$wpml_ids = array();
       
   654 
       
   655 								foreach ( $input[ $setting['id'] ] as $k => $setting_array ) {
       
   656 
       
   657 									$has_value = false;
       
   658 									foreach ( $settings as $sub_setting ) {
       
   659 
       
   660 										// Setup the WPML ID.
       
   661 										$wpml_id = $setting['id'] . '_' . $sub_setting['id'] . '_' . $k;
       
   662 
       
   663 										// Add id to array.
       
   664 										$wpml_ids[] = $wpml_id;
       
   665 
       
   666 										// Verify sub setting has a type & value.
       
   667 										if ( isset( $sub_setting['type'] ) && isset( $input[ $setting['id'] ][ $k ][ $sub_setting['id'] ] ) ) {
       
   668 
       
   669 											if ( 'href' === $sub_setting['id'] ) {
       
   670 												$sub_setting['type'] = 'url';
       
   671 											}
       
   672 
       
   673 											// Validate setting.
       
   674 											$input_safe = ot_validate_setting( $input[ $setting['id'] ][ $k ][ $sub_setting['id'] ], $sub_setting['type'], $sub_setting['id'], $wpml_id );
       
   675 
       
   676 											if ( ! empty( $input_safe ) ) {
       
   677 												$input[ $setting['id'] ][ $k ][ $sub_setting['id'] ] = $input_safe;
       
   678 												$has_value = true;
       
   679 											}
       
   680 										}
       
   681 									}
       
   682 
       
   683 									if ( ! $has_value ) {
       
   684 										unset( $input[ $setting['id'] ][ $k ] );
       
   685 									}
       
   686 								}
       
   687 							} else {
       
   688 								$input[ $setting['id'] ] = ot_validate_setting( $input[ $setting['id'] ], $setting['type'], $setting['id'], $setting['id'] );
       
   689 							}
       
   690 						}
       
   691 
       
   692 						// Unregister WPML strings that were deleted from lists and sliders.
       
   693 						if ( isset( $current_settings['settings'] ) && isset( $setting['type'] ) && in_array( $setting['type'], array( 'list-item', 'slider' ), true ) ) {
       
   694 
       
   695 							if ( ! isset( $wpml_ids ) ) {
       
   696 								$wpml_ids = array();
       
   697 							}
       
   698 
       
   699 							foreach ( $current_settings['settings'] as $check_setting ) {
       
   700 
       
   701 								if ( $setting['id'] === $check_setting['id'] && ! empty( $current_options[ $setting['id'] ] ) ) {
       
   702 
       
   703 									foreach ( $current_options[ $setting['id'] ] as $key => $value ) {
       
   704 
       
   705 										foreach ( $value as $ckey => $cvalue ) {
       
   706 
       
   707 											$id = $setting['id'] . '_' . $ckey . '_' . $key;
       
   708 
       
   709 											if ( ! in_array( $id, $wpml_ids, true ) ) {
       
   710 												ot_wpml_unregister_string( $id );
       
   711 											}
       
   712 										}
       
   713 									}
       
   714 								}
       
   715 							}
       
   716 						}
       
   717 
       
   718 						/* unregister WPML strings that were deleted from social links */
       
   719 						if ( isset( $current_settings['settings'] ) && isset( $setting['type'] ) && 'social-links' === $setting['type'] ) {
       
   720 
       
   721 							if ( ! isset( $wpml_ids ) ) {
       
   722 								$wpml_ids = array();
       
   723 							}
       
   724 
       
   725 							foreach ( $current_settings['settings'] as $check_setting ) {
       
   726 
       
   727 								if ( $setting['id'] === $check_setting['id'] && ! empty( $current_options[ $setting['id'] ] ) ) {
       
   728 
       
   729 									foreach ( $current_options[ $setting['id'] ] as $key => $value ) {
       
   730 
       
   731 										foreach ( $value as $ckey => $cvalue ) {
       
   732 
       
   733 											$id = $setting['id'] . '_' . $ckey . '_' . $key;
       
   734 
       
   735 											if ( ! in_array( $id, $wpml_ids, true ) ) {
       
   736 												ot_wpml_unregister_string( $id );
       
   737 											}
       
   738 										}
       
   739 									}
       
   740 								}
       
   741 							}
       
   742 						}
       
   743 					}
       
   744 				}
       
   745 			}
       
   746 
       
   747 			return $input;
       
   748 		}
       
   749 
       
   750 		/**
       
   751 		 * Helper function to get the pages array for an option
       
   752 		 *
       
   753 		 * @param  array $option Option array.
       
   754 		 * @return mixed
       
   755 		 *
       
   756 		 * @access public
       
   757 		 * @since  2.0
       
   758 		 */
       
   759 		public function get_pages( $option = array() ) {
       
   760 
       
   761 			if ( empty( $option ) ) {
       
   762 				return false;
       
   763 			}
       
   764 
       
   765 			// Check for pages.
       
   766 			if ( isset( $option['pages'] ) && ! empty( $option['pages'] ) ) {
       
   767 
       
   768 				// Return pages array.
       
   769 				return $option['pages'];
       
   770 
       
   771 			}
       
   772 
       
   773 			return false;
       
   774 		}
       
   775 
       
   776 		/**
       
   777 		 * Helper function to get the sections array for a page
       
   778 		 *
       
   779 		 * @param  array $page Page array.
       
   780 		 * @return mixed
       
   781 		 *
       
   782 		 * @access public
       
   783 		 * @since  2.0
       
   784 		 */
       
   785 		public function get_sections( $page = array() ) {
       
   786 
       
   787 			if ( empty( $page ) ) {
       
   788 				return false;
       
   789 			}
       
   790 
       
   791 			// Check for sections.
       
   792 			if ( isset( $page['sections'] ) && ! empty( $page['sections'] ) ) {
       
   793 
       
   794 				// Return sections array.
       
   795 				return $page['sections'];
       
   796 
       
   797 			}
       
   798 
       
   799 			return false;
       
   800 		}
       
   801 
       
   802 		/**
       
   803 		 * Helper function to get the settings array for a page
       
   804 		 *
       
   805 		 * @param  array $page Page array.
       
   806 		 * @return mixed
       
   807 		 *
       
   808 		 * @access public
       
   809 		 * @since  2.0
       
   810 		 */
       
   811 		public function get_the_settings( $page = array() ) {
       
   812 
       
   813 			if ( empty( $page ) ) {
       
   814 				return false;
       
   815 			}
       
   816 
       
   817 			/* check for settings */
       
   818 			if ( isset( $page['settings'] ) && ! empty( $page['settings'] ) ) {
       
   819 
       
   820 				/* return settings array */
       
   821 				return $page['settings'];
       
   822 
       
   823 			}
       
   824 
       
   825 			return false;
       
   826 		}
       
   827 
       
   828 		/**
       
   829 		 * Prints out all settings sections added to a particular settings page
       
   830 		 *
       
   831 		 * @global $wp_settings_sections Storage array of all settings sections added to admin pages.
       
   832 		 * @global $wp_settings_fields   Storage array of settings fields and info about their pages/sections.
       
   833 		 *
       
   834 		 * @param  string $page The slug name of the page whos settings sections you want to output.
       
   835 		 * @return string
       
   836 		 *
       
   837 		 * @access public
       
   838 		 * @since  2.0
       
   839 		 */
       
   840 		public function do_settings_sections( $page ) {
       
   841 			global $wp_settings_sections, $wp_settings_fields;
       
   842 
       
   843 			if ( ! isset( $wp_settings_sections ) || ! isset( $wp_settings_sections[ $page ] ) ) {
       
   844 				return false;
       
   845 			}
       
   846 
       
   847 			foreach ( (array) $wp_settings_sections[ $page ] as $section ) {
       
   848 
       
   849 				if ( ! isset( $section['id'] ) ) {
       
   850 					continue;
       
   851 				}
       
   852 
       
   853 				$section_id = $section['id'];
       
   854 
       
   855 				echo '<div id="section_' . esc_attr( $section_id ) . '" class="postbox ui-tabs-panel">';
       
   856 
       
   857 				call_user_func( $section['callback'], $section );
       
   858 
       
   859 				if ( ! isset( $wp_settings_fields ) || ! isset( $wp_settings_fields[ $page ] ) || ! isset( $wp_settings_fields[ $page ][ $section_id ] ) ) {
       
   860 					continue;
       
   861 				}
       
   862 
       
   863 				echo '<div class="inside">';
       
   864 
       
   865 				/**
       
   866 				 * Hook to insert arbitrary markup before the `do_settings_fields` method.
       
   867 				 *
       
   868 				 * @since 2.6.0
       
   869 				 *
       
   870 				 * @param string $page       The page slug.
       
   871 				 * @param string $section_id The section ID.
       
   872 				 */
       
   873 				do_action( 'ot_do_settings_fields_before', $page, $section_id );
       
   874 
       
   875 				$this->do_settings_fields( $page, $section_id );
       
   876 
       
   877 				/**
       
   878 				 * Hook to insert arbitrary markup after the `do_settings_fields` method.
       
   879 				 *
       
   880 				 * @since 2.6.0
       
   881 				 *
       
   882 				 * @param string $page       The page slug.
       
   883 				 * @param string $section_id The section ID.
       
   884 				 */
       
   885 				do_action( 'ot_do_settings_fields_after', $page, $section_id );
       
   886 
       
   887 				echo '</div>';
       
   888 
       
   889 				echo '</div>';
       
   890 			}
       
   891 
       
   892 		}
       
   893 
       
   894 		/**
       
   895 		 * Print out the settings fields for a particular settings section
       
   896 		 *
       
   897 		 * @global $wp_settings_fields Storage array of settings fields and their pages/sections
       
   898 		 *
       
   899 		 * @param  string $page    Slug title of the admin page who's settings fields you want to show.
       
   900 		 * @param  string $section Slug title of the settings section who's fields you want to show.
       
   901 		 * @return string
       
   902 		 *
       
   903 		 * @access public
       
   904 		 * @since  2.0
       
   905 		 */
       
   906 		public function do_settings_fields( $page, $section ) {
       
   907 			global $wp_settings_fields;
       
   908 
       
   909 			if ( ! isset( $wp_settings_fields ) || ! isset( $wp_settings_fields[ $page ] ) || ! isset( $wp_settings_fields[ $page ][ $section ] ) ) {
       
   910 				return;
       
   911 			}
       
   912 
       
   913 			foreach ( (array) $wp_settings_fields[ $page ][ $section ] as $field ) {
       
   914 
       
   915 				$conditions = '';
       
   916 
       
   917 				if ( isset( $field['args']['condition'] ) && ! empty( $field['args']['condition'] ) ) {
       
   918 
       
   919 					$conditions  = ' data-condition="' . esc_attr( $field['args']['condition'] ) . '"';
       
   920 					$conditions .= isset( $field['args']['operator'] ) && in_array( $field['args']['operator'], array( 'and', 'AND', 'or', 'OR' ), true ) ? ' data-operator="' . esc_attr( $field['args']['operator'] ) . '"' : '';
       
   921 				}
       
   922 
       
   923 				// Build the setting CSS class.
       
   924 				if ( isset( $field['args']['class'] ) && ! empty( $field['args']['class'] ) ) {
       
   925 
       
   926 					$classes = explode( ' ', $field['args']['class'] );
       
   927 
       
   928 					foreach ( $classes as $key => $value ) {
       
   929 						$classes[ $key ] = $value . '-wrap';
       
   930 					}
       
   931 
       
   932 					$class = 'format-settings ' . implode( ' ', $classes );
       
   933 				} else {
       
   934 
       
   935 					$class = 'format-settings';
       
   936 				}
       
   937 
       
   938 				echo '<div id="setting_' . esc_attr( $field['id'] ) . '" class="' . esc_attr( $class ) . '"' . $conditions . '>'; // phpcs:ignore
       
   939 
       
   940 				echo '<div class="format-setting-wrap">';
       
   941 
       
   942 				if ( 'textblock' !== $field['args']['type'] && ! empty( $field['title'] ) ) {
       
   943 
       
   944 					echo '<div class="format-setting-label">';
       
   945 
       
   946 					echo '<h3 class="label">' . wp_kses_post( $field['title'] ) . '</h3>';
       
   947 
       
   948 					echo '</div>';
       
   949 				}
       
   950 
       
   951 				call_user_func( $field['callback'], $field['args'] );
       
   952 
       
   953 				echo '</div>';
       
   954 
       
   955 				echo '</div>';
       
   956 			}
       
   957 		}
       
   958 
       
   959 		/**
       
   960 		 * Resets page options before the screen is displayed
       
   961 		 *
       
   962 		 * @access public
       
   963 		 * @since  2.0
       
   964 		 *
       
   965 		 * @return bool
       
   966 		 */
       
   967 		public function reset_options() {
       
   968 
       
   969 			// Check for reset action.
       
   970 			if ( isset( $_POST['option_tree_reset_nonce'] ) && wp_verify_nonce( $_POST['option_tree_reset_nonce'], 'option_tree_reset_form' ) ) { // phpcs:ignore
       
   971 
       
   972 				// Loop through options.
       
   973 				foreach ( (array) $this->options as $option ) {
       
   974 
       
   975 					// Loop through pages.
       
   976 					foreach ( (array) $this->get_pages( $option ) as $page ) {
       
   977 
       
   978 						// Verify page.
       
   979 						if ( isset( $_GET['page'] ) && $_GET['page'] === $page['menu_slug'] ) {
       
   980 
       
   981 							// Reset options.
       
   982 							delete_option( $option['id'] );
       
   983 						}
       
   984 					}
       
   985 				}
       
   986 			}
       
   987 			return false;
       
   988 		}
       
   989 	}
       
   990 
       
   991 }
       
   992 
       
   993 if ( ! function_exists( 'ot_register_settings' ) ) {
       
   994 
       
   995 	/**
       
   996 	 * This method instantiates the settings class & builds the UI.
       
   997 	 *
       
   998 	 * @uses OT_Settings()
       
   999 	 *
       
  1000 	 * @param array $args Array of arguments to create settings.
       
  1001 	 *
       
  1002 	 * @access public
       
  1003 	 * @since  2.0
       
  1004 	 */
       
  1005 	function ot_register_settings( $args ) {
       
  1006 		if ( ! $args ) {
       
  1007 			return;
       
  1008 		}
       
  1009 
       
  1010 		new OT_Settings( $args );
       
  1011 	}
       
  1012 }