Responsive_Options

The Responsive Mobile Responsive Options class.

Defined (1)

The class is defined in the following location(s).

/libraries/class-responsive-options.php  
  1. Class Responsive_Options { 
  2.  
  3. public $options_only; 
  4.  
  5. public $options; 
  6.  
  7. public $responsive_mobile_options; 
  8.  
  9. public static $static_responsive_mobile_options; 
  10.  
  11. public static $static_default_options; 
  12.  
  13. protected $default_options; 
  14.  
  15. /** 
  16. * Pulls in the arrays for the options and sets up the responsive options 
  17. * @param $options array 
  18. */ 
  19. public function __construct( $options ) { 
  20. $this->options = $options; 
  21. $this->options_only = $this->get_options_only( $this->options ); 
  22. $this->responsive_mobile_options = get_option( 'responsive_mobile_theme_options' ); 
  23. $this->default_options = $this->get_options_defaults( $this->options_only ); 
  24.  
  25. self::$static_responsive_mobile_options = $this->responsive_mobile_options; 
  26. self::$static_default_options = $this->default_options; 
  27.  
  28. $this->attributes['onclick'] = 'return confirm("' . __( 'Do you want to restore the default settings?', 'responsive-mobile' ) . __( 'All theme settings will be lost!', 'responsive-mobile' ) . __( 'Click OK to restore.', 'responsive-mobile' ) . '")'; 
  29.  
  30. add_action( 'admin_print_styles-appearance_page_theme_options', array( $this, 'admin_enqueue_scripts' ) ); 
  31. add_action( 'admin_init', array( $this, 'theme_options_init' ) ); 
  32. add_action( 'admin_menu', array( $this, 'theme_page_init' ) ); 
  33.  
  34. /** 
  35. * Init theme options page 
  36. */ 
  37. public function theme_page_init() { 
  38. // Register the page 
  39. add_theme_page( 
  40. __( 'Theme Options', 'responsive-mobile' ),  
  41. __( 'Theme Options', 'responsive-mobile' ),  
  42. 'edit_theme_options',  
  43. 'theme_options',  
  44. array( $this, 'theme_options_do_page' ) 
  45. ); 
  46.  
  47. /** 
  48. * Init theme options to white list our options 
  49. */ 
  50. public function theme_options_init() { 
  51.  
  52. register_setting( 
  53. 'responsive_mobile_options',  
  54. 'responsive_mobile_theme_options',  
  55. array( &$this, 'theme_options_validate' ) 
  56. ); 
  57.  
  58. /** 
  59. * A safe way of adding JavaScripts to a WordPress generated page. 
  60. */ 
  61. public function admin_enqueue_scripts() { 
  62. //@TODO Make sure the locations still work when in the plugin 
  63. wp_enqueue_style( 'responsive-theme-options' ); 
  64. wp_enqueue_script( 'responsive-theme-options' ); 
  65. wp_enqueue_style( 'wp-color-picker' ); 
  66. wp_enqueue_script( 'wp-color-picker' ); 
  67. wp_enqueue_script( 'jquery-drag-drop', get_template_directory_uri() . '/libraries/js/jquery-ui-1.10.4.custom.min.js', array( 'jquery' ), '20140619', true ); 
  68.  
  69. /** 
  70. * Create the theme options page container and initialise the render display method 
  71. */ 
  72. public function theme_options_do_page() { 
  73.  
  74. if ( !isset( $_REQUEST['settings-updated'] ) ) { 
  75. $_REQUEST['settings-updated'] = false; 
  76.  
  77. ?> 
  78.  
  79. <div class="wrap"> 
  80. <?php echo "<h2>" . wp_get_theme() . " " . __( 'Theme Options', 'responsive-mobile' ) . "</h2>"; ?> 
  81.  
  82.  
  83. <?php if ( false !== $_REQUEST['settings-updated'] ) : ?> 
  84. <div class="updated fade"><p><strong><?php _e( 'Options Saved', 'responsive-mobile' ); ?></strong></p></div> 
  85. <?php endif; ?> 
  86.  
  87. <?php responsive_mobile_theme_options(); // Theme Options Hook ?> 
  88.  
  89. <form method="post" action="options.php" enctype="multipart/form-data" > 
  90. <?php settings_fields( 'responsive_mobile_options' ); ?> 
  91.  
  92. <div class="settings-row"> 
  93. <?php 
  94. $this->render_display(); 
  95. ?> 
  96. </div> 
  97. <!-- .row --> 
  98. </form> 
  99. </div><!-- wrap --> 
  100. <?php 
  101.  
  102. /** 
  103. * Displays the options 
  104. * Loops through sections array 
  105. * @return string 
  106. */ 
  107. public function render_display() { 
  108.  
  109. foreach ( $this->options as $section ) { 
  110. $this->container( $section['title'], $section['fields'] ); 
  111.  
  112. /** 
  113. * Creates main sections title and container 
  114. * Loops through the sections array 
  115. * @param $title string 
  116. * @param $fields array 
  117. * @return string 
  118. */ 
  119. protected function container( $title, $fields ) { 
  120.  
  121. foreach ( $fields as $field ) { 
  122. $section[] = $this->section( $this->parse_args( $field ) ); 
  123.  
  124. $html = '<h3 class="rwd-toggle">' . esc_html( $title ) . '<a href="#"></a></h3><div class="rwd-container"><div class="rwd-block">'; 
  125.  
  126. foreach ( $section as $option ) { 
  127. $html .= $option; 
  128.  
  129. $html .= $this->save(); 
  130. $html .= '</div><!-- .rwd-block --></div><!-- .rwd-container -->'; 
  131.  
  132. echo $html; 
  133.  
  134.  
  135. /** 
  136. * Creates the title section for each option input 
  137. * @param $title string 
  138. * @param $subtitle string 
  139. * @return string 
  140. */ 
  141. protected function sub_heading( $title, $sub_title ) { 
  142.  
  143. // If width is not set or it's not set to full then go ahead and create default layout 
  144. if ( !isset( $args['width'] ) || $args['width'] != 'full' ) { 
  145. $html = '<div class="col-md-4">'; 
  146.  
  147. $html .= '<h4 class="title">' . $title . '</h4>'; 
  148.  
  149. $html .= ( '' !== $sub_title ) ? '<div class="sub-title"><p>' . $sub_title . '</p></div>' : ''; 
  150.  
  151. $html .= '</div><!-- .col-md-4 -->'; 
  152.  
  153. return $html; 
  154.  
  155.  
  156. /** 
  157. * Creates option section with inputs 
  158. * Calls option type 
  159. * @param $options array 
  160. * @return string 
  161. */ 
  162. protected function section( $options ) { 
  163.  
  164. $html = '<div class="row">'; 
  165.  
  166. $html .= $this->sub_heading( $options['title'], $options['subtitle'] ); 
  167.  
  168. // If the width is not set to full then create normal size, otherwise create full width 
  169. $html .= ( !isset( $options['width'] ) || $options['width'] != 'full' ) ? '<div class="col-md-8">' : '<div class="col-md-12">'; 
  170.  
  171. $html .= $this->{$options['type']}( $options ); 
  172.  
  173. $html .= '</div>'; 
  174.  
  175. $html .= '</div>'; 
  176.  
  177. return $html; 
  178.  
  179.  
  180. /** 
  181. * Creates text input 
  182. * @param $args array 
  183. * @return string 
  184. */ 
  185. protected function text( $args ) { 
  186.  
  187. extract( $args ); 
  188.  
  189. $value = ( !empty( $this->responsive_mobile_options[$id] ) ) ? $this->responsive_mobile_options[$id] : ''; 
  190.  
  191. $html = '<input id="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '" class="regular-text" type="text" name="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '" value="' . esc_html( $value ) . '" placeholder="' . esc_attr( $placeholder ) . '" /><label class="description" for="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '">' . esc_html( $description ) . '</label>'; 
  192.  
  193. return $html; 
  194.  
  195. /** 
  196. * Creates text input with color picker. 
  197. * @param $args array 
  198. * @return string 
  199. */ 
  200. protected function color( $args ) { 
  201.  
  202. extract( $args ); 
  203.  
  204. $value = ( !empty( $this->responsive_mobile_options[$id] ) ) ? $this->responsive_mobile_options[$id] : ''; 
  205.  
  206. $html = '<input id="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '" class="wp-color-picker regular-text" type="text" name="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '" value="' . esc_html( $value ) . '" placeholder="' . esc_attr( $placeholder ) . '" /><label class="description" for="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '">' . esc_html( $description ) . '</label>'; 
  207.  
  208. return $html; 
  209.  
  210. /** 
  211. * Creates textarea input 
  212. * @param $args array 
  213. * @return string 
  214. */ 
  215. protected function textarea( $args ) { 
  216.  
  217. extract( $args ); 
  218.  
  219. $class[] = 'large-text'; 
  220. $classes = implode( ' ', $class ); 
  221.  
  222. $description = ( '' !== $description ) ? '<label class="description" for="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '">' . esc_html( $description ) . '</label>': ''; 
  223. $heading = ( '' !== $heading ) ? '<p>' . esc_html( $heading ) . '</p>' : ''; 
  224.  
  225. $value = ( !empty( $this->responsive_mobile_options[$id] ) ) ? $this->responsive_mobile_options[$id] : ''; 
  226.  
  227. $html = $heading . '<textarea id="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '" class="' . esc_attr( $classes ) . '" cols="50" rows="30" name="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '" placeholder="' . $placeholder . '">' . esc_html( $value ) . '</textarea>' . $description; 
  228.  
  229. return $html; 
  230.  
  231. /** 
  232. * Creates export textarea input 
  233. * @param $args array 
  234. * @return string 
  235. */ 
  236. protected function export( $args ) { 
  237.  
  238. extract( $args ); 
  239. $options = esc_html( serialize( $this->responsive_mobile_options ) ); 
  240. $html = '<textarea rows="10" cols="50">' . $options . '</textarea>'; 
  241. $html .= '<br/><a class="export-option" href="data:text/octet-stream;charset=utf-8, ' . str_replace( "#", "%23", $options ) . '" download="theme-option-backup.txt">Download</a>'; 
  242.  
  243. return $html; 
  244.  
  245. /** 
  246. * Creates import textarea input 
  247. * @param $args array 
  248. * @return string 
  249. */ 
  250. protected function import( $args ) { 
  251.  
  252. extract( $args ); 
  253.  
  254. $html = '<textarea name="import" rows="10" cols="50"></textarea>'; 
  255. $html .= "<br/><input type='file' id='import_file' name='import_file' />"; 
  256.  
  257. return $html; 
  258.  
  259. /** 
  260. * Creates select dropdown input 
  261. * Loops through options 
  262. * @param $args array 
  263. * @return string 
  264. */ 
  265. protected function select( $args ) { 
  266.  
  267. extract( $args ); 
  268.  
  269. $html = '<select id="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '" name="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '">'; 
  270. foreach ( $options as $key => $value ) { 
  271. // looping through and creating all the options and making the one saved in the options as the chosen one otherwise falling back to the default 
  272. $html .= '<option' . selected( ( isset( $this->responsive_mobile_options[$id] ) ) ? $this->responsive_mobile_options[$id] : $default, $key, false ) . ' value="' . esc_attr( $key ) . '">' . esc_html( 
  273. $value 
  274. ) . 
  275. '</option>'; 
  276. $html .= '</select>'; 
  277.  
  278. return $html; 
  279.  
  280.  
  281. /** 
  282. * Creates checkbox input 
  283. * @param $args array 
  284. * @return string 
  285. */ 
  286. protected function checkbox( $args ) { 
  287.  
  288. extract( $args ); 
  289.  
  290. $checked = ( isset( $this->responsive_mobile_options[$id] ) ) ? checked( 1, esc_attr( $this->responsive_mobile_options[$id] ), false ) : checked( 0, 1 ); 
  291.  
  292. $html = '<input id="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '" name="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '" type="checkbox" value="1" ' . $checked . '/><label class="description" for="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '">' . wp_kses_post( $description ) . '</label>'; 
  293.  
  294. return $html; 
  295.  
  296. /** 
  297. * Creates a description 
  298. * @param $args 
  299. * @return string 
  300. */ 
  301. protected function description( $args ) { 
  302.  
  303. extract( $args ); 
  304.  
  305. $html = '<p>' . wp_kses_post( $description ) . '</p>'; 
  306.  
  307. return $html; 
  308.  
  309. /** 
  310. * Creates save, reset and upgrade buttons 
  311. * @return string 
  312. */ 
  313. protected function save() { 
  314. $html = '<div class="col-md-12"><p class="submit">' . get_submit_button( __( 'Save Options', 'responsive-mobile' ), 'primary', 'responsive_mobile_theme_options[submit]', false ) . ' ' . get_submit_button( __( 'Restore Defaults', 'responsive-mobile' ), 'secondary', 'responsive_mobile_theme_options[reset]', false, $this->attributes ) . '</p></div>'; 
  315.  
  316. return $html; 
  317.  
  318.  
  319. /** 
  320. * Creates editor input 
  321. * @param $args array 
  322. * @return string 
  323. */ 
  324. protected function editor( $args ) { 
  325.  
  326. extract( $args ); 
  327.  
  328. $class[] = 'large-text'; 
  329. $classes = implode( ' ', $class ); 
  330.  
  331. $value = ( !empty( $this->responsive_mobile_options[$id] ) ) ? $this->responsive_mobile_options[$id] : ''; 
  332.  
  333. $editor_settings = array( 
  334. 'textarea_name' => 'responsive_mobile_theme_options[' . $id . ']',  
  335. 'media_buttons' => true,  
  336. 'tinymce' => array( 'plugins' => 'wordpress' ),  
  337. 'editor_class' => esc_attr( $classes ) 
  338. ); 
  339.  
  340. $html = '<div class="tinymce-editor">'; 
  341. ob_start(); 
  342. $html .= wp_editor( $value, 'responsive_mobile_theme_options_' . $id . '_', $editor_settings ); 
  343. $html .= ob_get_contents(); 
  344. $html .= '<label class="description" for="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '">' . esc_html( $description ) . '</label>'; 
  345. $html .= '</div>'; 
  346. ob_clean(); 
  347.  
  348. return $html; 
  349.  
  350. protected function dragdrop( $args ) { 
  351. extract( $args ); 
  352.  
  353. $items = array(); 
  354.  
  355. $original_items = $options['items']; 
  356.  
  357. // If there is no settings saved just use the original options 
  358. if ( ! isset( $this->responsive_mobile_options[$id] ) || '' === $this->responsive_mobile_options[$id] || 'null' === $this->responsive_mobile_options[$id] ) { 
  359. $items = json_decode( $default, true ); 
  360. } else { 
  361. // Get json from saved settings and decode it 
  362. $items = json_decode( $this->responsive_mobile_options[ $id ], true ); 
  363.  
  364. foreach ( $items as $index => $item ) { 
  365. foreach ( $item as $i => $it ) { 
  366. if ( isset( $original_items[$it] ) ) { 
  367. $new_item[$index][$it] = $original_items[$it]; 
  368. // Unset it from the original list so that we can see if there are any options left over 
  369. // anything left over will be new options added after the original save 
  370. unset($original_items[$it]); 
  371.  
  372. // After unsetting the original items if there are any left over then we need to add them to the first 
  373. // select box so a user can select it 
  374. if ( !empty( $original_items ) ) { 
  375. foreach( $original_items as $key => $item ) { 
  376. $new_item[0][$key] = $item; 
  377.  
  378. $items = $new_item; 
  379.  
  380.  
  381. // Create the home dropzone to be added at the start of the dropzones array 
  382. $select_items = array( 
  383. 'select-items' => $options['items_title'] 
  384. ); 
  385.  
  386. $drop_zones = $select_items + $options['drop_zones']; 
  387.  
  388. // Create the display html 
  389. $html = '<div id="' . $id . '" class="drag-drop-container row">'; 
  390.  
  391. $i = 0; 
  392.  
  393. // Make the column size so that it fits in the row 
  394. $col_size = 12 / count( $drop_zones ); 
  395. $col_size = intval( $col_size ); 
  396.  
  397. // Loop through the drop zones to create the sections 
  398. foreach ( $drop_zones as $drop_zone => $name ) { 
  399. $html .= '<div class="items-container col-xs-' . $col_size . '">'; 
  400. $html .= '<h4>' . $name . '</h4>'; 
  401. $html .= '<ul id="' . $drop_zone . '" class="sortable">'; 
  402.  
  403. // If there are items in this drop zone then display them 
  404. if ( isset( $items[$i] ) ) { 
  405. foreach ( $items[$i] as $key => $item ) { 
  406. $html .= '<li id="' . $key . '">' . $item . '</li>'; 
  407.  
  408.  
  409. $html .= '</ul>'; 
  410. $html .= '</div>'; 
  411.  
  412. $i++; 
  413. $html .= '</div>'; 
  414.  
  415. // Hidden text box that will save data 
  416. $value = ( !empty( $this->responsive_mobile_options[$id] ) ) ? $this->responsive_mobile_options[$id] : ''; 
  417.  
  418. $html .= '<input type="hidden" id="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '" name="' . esc_attr( 'responsive_mobile_theme_options[' . $id . ']' ) . '" value="' . esc_html( $value ) . '" />'; 
  419.  
  420. return $html; 
  421.  
  422.  
  423. /** 
  424. * VALIDATION SECTION 
  425. */ 
  426.  
  427. /** 
  428. * Initialises the validation of the settings when submitted 
  429. * Called by the register_settings() 
  430. * @param $input 
  431. * @return array|mixed|void 
  432. */ 
  433. public function theme_options_validate( $input ) { 
  434.  
  435. /** 
  436. * Import functionality 
  437. * Both the copy/paste and file upload options are active. First it checks for file, if any file is uploaded then 
  438. * it processes that. Otherwise it checks if anything is sent with the textarea for the import. 
  439. */ 
  440.  
  441. global $wp_filesystem; 
  442.  
  443. // Check if any file is uplaoded 
  444. if( isset( $_FILES['import_file'] ) && $_FILES['import_file']['name'] ) { 
  445.  
  446. // Initialise WP filesystem. 
  447. WP_Filesystem( request_filesystem_credentials( 'options.php', '', false, false, null ) ); 
  448.  
  449. // Get the text of the uploaded file and trim it to remove space from either end. 
  450. $import_file_text = trim( $wp_filesystem->get_contents( $_FILES['import_file']['tmp_name'] ) ); 
  451.  
  452. if( $import_file_text ) { 
  453. $string = stripslashes( $import_file_text ); 
  454.  
  455. // check string is serialized and unserialize it 
  456. if( is_serialized( $string ) ) { 
  457. $try = unserialize( ( $string ) ); 
  458.  
  459. // make sure $try is set with the unserialized data 
  460. if( $try ) { 
  461. add_settings_error( 'responsive_mobile_theme_options', 'imported_success', __( 'Options Imported', 'responsive-mobile' ), 'updated fade' ); 
  462.  
  463. return $try; 
  464. else { 
  465. add_settings_error( 'responsive_mobile_theme_options', 'imported_failed', __( 'Invalid Data for Import', 'responsive-mobile' ), 'error fade' ); 
  466. // If no file is uploaded then check for the texarea field for improt option. 
  467. else { 
  468. if( isset( $_POST['import'] ) ) { 
  469. if( trim( $_POST['import'] ) ) { 
  470.  
  471. $string = stripslashes( trim( $_POST['import'] ) ); 
  472.  
  473. // check string is serialized and unserialize it 
  474. if( is_serialized( $string ) ) { 
  475. $try = unserialize( ( $string ) ); 
  476.  
  477. // make sure $try is set with the unserialized data 
  478. if( $try ) { 
  479. add_settings_error( 'responsive_mobile_theme_options', 'imported_success', __( 'Options Imported', 'responsive-mobile' ), 'updated fade' ); 
  480.  
  481. return $try; 
  482. else { 
  483. add_settings_error( 'responsive_mobile_theme_options', 'imported_failed', __( 'Invalid Data for Import', 'responsive-mobile' ), 'error fade' ); 
  484. /********************* End of Import Options functionality ************************/ 
  485.  
  486. $defaults = $this->default_options; 
  487. if ( isset( $input['reset'] ) ) { 
  488.  
  489. $input = $defaults; 
  490.  
  491. } else { 
  492.  
  493. // remove the submit button that gets included in the $input 
  494. unset ( $input['submit'] ); 
  495.  
  496. // add missing checkbox values that don't get added when they are unchecked 
  497. $input = $this->add_missing_checkboxes( $input ); 
  498.  
  499. $options = $this->options_only; 
  500.  
  501. $input = $input ? $input : array(); 
  502. $input = apply_filters( 'responsive_mobile_settings_sanitize', $input ); 
  503.  
  504. // Loop through each setting being saved and pass it through a sanitization filter 
  505. foreach ( $input as $key => $value ) { 
  506.  
  507. $validate = isset( $options[$key]['validate'] ) ? $options[$key]['validate'] : false; 
  508.  
  509. if ( $validate ) { 
  510. $input[$key] = $this->{'validate_' . $validate}( $value, $key ); 
  511. } else { 
  512. // TODO could do with returning error message 
  513. //return; 
  514.  
  515.  
  516.  
  517. return $input; 
  518.  
  519. /** 
  520. * Validates checkbox 
  521. * checks if the value submitted is a boolean value 
  522. * @param $input 
  523. * @param $key 
  524. * @return null 
  525. */ 
  526. protected function validate_checkbox( $input, $key ) { 
  527.  
  528. // if the input is anything other than a 1 make it a 0 
  529. if ( 1 == $input ) { 
  530. $input = 1; 
  531. } else { 
  532. $input = 0; 
  533.  
  534. return $input; 
  535.  
  536. /** 
  537. * Validates a dropdown select option 
  538. * checks that the value is available in the options, if it can't find it then to return the default 
  539. * @param $input 
  540. * @param $key 
  541. * @return mixed 
  542. */ 
  543. protected function validate_select( $input, $key ) { 
  544.  
  545. $options = $this->options_only[$key]; 
  546. $input = ( array_key_exists( $input, $options['options'] ) ? $input : $this->default_options[$key] ); 
  547.  
  548. return $input; 
  549.  
  550. /** 
  551. * Validates the editor textarea 
  552. * @param $input 
  553. * @param $key 
  554. * @return string 
  555. */ 
  556. protected function validate_editor( $input, $key ) { 
  557.  
  558. $input = wp_kses_stripslashes( $input ); 
  559.  
  560. return $input; 
  561.  
  562. /** 
  563. * Validates/sanitizes a url 
  564. * @param $input 
  565. * @param $key 
  566. * @return string 
  567. */ 
  568. protected function validate_url( $input, $key ) { 
  569.  
  570. $input = esc_url_raw( $input ); 
  571.  
  572. return $input; 
  573.  
  574. /** 
  575. * Validates/sanitizes a text input 
  576. * @param $input 
  577. * @param $key 
  578. * @return string 
  579. */ 
  580. protected function validate_text( $input, $key ) { 
  581.  
  582. $input = sanitize_text_field( $input ); 
  583.  
  584. return $input; 
  585.  
  586. /** 
  587. * Validates the css textarea 
  588. * @param $input 
  589. * @param $key 
  590. * @return string 
  591. */ 
  592. public function validate_css( $input, $key ) { 
  593.  
  594. $input = wp_kses_stripslashes( $input ); 
  595.  
  596. $input = wp_kses_post( $input ); 
  597.  
  598. return $input; 
  599.  
  600. /** 
  601. * Validates the javascript textarea 
  602. * @param $input 
  603. * @param $key 
  604. * @return string 
  605. */ 
  606. protected function validate_js( $input, $key ) { 
  607.  
  608. $input = wp_kses_stripslashes( $input ); 
  609.  
  610. return $input; 
  611.  
  612. /** 
  613. * Validates the Drag and Drop 
  614. * Checks input against defaults and if there is no match it removes it 
  615. * @param $input 
  616. * @param $key 
  617. * @return mixed|string|void 
  618. */ 
  619. protected function validate_drag_drop( $input, $key ) { 
  620.  
  621. // Get the defaults 
  622. $defaults = $this->default_options; 
  623. $defaults = json_decode( $defaults[$key], true ); 
  624.  
  625. if ( '' !== $input && 'null' !== $input ) { 
  626. $decoded = json_decode( $input, true ); 
  627.  
  628. $single_default = array(); 
  629. // break defaults down into a single array 
  630. foreach( $defaults as $default ) { 
  631. foreach( $default as $single ) { 
  632. $single_default[] = $single; 
  633.  
  634. foreach ( $decoded as $i => $items ) { 
  635. foreach ( $items as $i2 => $item ) { 
  636. if ( ! in_array( $item, $single_default ) ) { 
  637. unset( $decoded[$i][$i2] ); 
  638.  
  639.  
  640. return json_encode( $decoded ); 
  641. } else { 
  642. return json_encode( $defaults ); 
  643.  
  644. /** 
  645. * Removes the sections from the options array given in construct 
  646. * and sets the id as the key 
  647. * @param $options 
  648. */ 
  649. protected function get_options_only( $options ) { 
  650. $new_array = array(); 
  651. foreach ( $options as $option ) { 
  652. foreach ( $option['fields'] as $opt ) { 
  653. $new_array[$opt['id']] = $opt; 
  654.  
  655.  
  656. return $new_array; 
  657.  
  658. /** 
  659. * Adds missing checkboxes 
  660. * When checkboxes are not checked they are not added to database leaving some undefined indexes, this adds them in 
  661. * @param $input 
  662. * @return array 
  663. */ 
  664. protected function add_missing_checkboxes( $input ) { 
  665. $checkboxes = array(); 
  666. $new_array = array(); 
  667. $options = $this->options_only; 
  668.  
  669. foreach ( $options as $option => $value ) { 
  670. if ( 'checkbox' == $value['type'] ) { 
  671. $checkboxes[$option] = 0; 
  672.  
  673. $new_array = wp_parse_args( $input, $checkboxes ); 
  674.  
  675. return $new_array; 
  676.  
  677. /** 
  678. * Gets the defaults as key => value 
  679. * @param $options 
  680. * @return array 
  681. */ 
  682. protected function get_options_defaults( $options ) { 
  683.  
  684. $defaults = array(); 
  685. foreach ( $options as $option ) { 
  686. $defaults[$option['id']] = $option['default']; 
  687.  
  688. return $defaults; 
  689.  
  690. /** 
  691. * parses the options with the defaults to get a complete array 
  692. * @return array 
  693. */ 
  694. public static function get_parse_options() { 
  695. $options = wp_parse_args( self::$static_responsive_mobile_options, self::$static_default_options ); 
  696.  
  697. return $options; 
  698.  
  699. /** 
  700. * Makes sure that every option has all the required args 
  701. * @param $args array 
  702. * @return array 
  703. */ 
  704. protected function parse_args( $args ) { 
  705. $default_args = array( 
  706. 'title' => '',  
  707. 'subtitle' => '',  
  708. 'heading' => '',  
  709. 'type' => 'text',  
  710. 'id' => '',  
  711. 'class' => array(),  
  712. 'description' => '',  
  713. 'placeholder' => '',  
  714. 'options' => array(),  
  715. 'default' => '',  
  716. 'validate' => '',  
  717. 'options' => array() 
  718. ); 
  719.  
  720. $result = array_merge( $default_args, $args ); 
  721.  
  722. return $result;