Responsive_Options

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