/includes/skip/elements/forms/import.php

  1. <?php 
  2. /** 
  3. * Import Button 
  4. *  
  5. * Shows a Button to import data for a form 
  6. *  
  7. * @package Skip\Forms 
  8. * @since 1.0 
  9. * @ignore 
  10. */ 
  11.  
  12. namespace skip\v1_0_0; 
  13.  
  14. class Import_Button extends WP_Fileuploader{ 
  15. /** 
  16. * Constructor 
  17. * 
  18. * @package Skip 
  19. * @since 1.0 
  20. *  
  21. * @param string $name Name of colorfield 
  22. * @param array $args Array of [ $id , $extra Extra colorfield code, option_groupOption group to save data, $before_textfield Code before colorfield, $after_textfield Code after colorfield ] 
  23. */ 
  24. function __construct( $name, $args = array() ) { 
  25. global $post, $skip_form_instance_option_group; 
  26.  
  27. $defaults = array( 
  28. 'id' => substr( md5 ( time() * rand() ), 0, 10 ),  
  29. 'extra' => '',  
  30. 'before_element' => '',  
  31. 'uploader' => 'file',  
  32. 'after_element' => '',  
  33. 'option_group' => $skip_form_instance_option_group 
  34. ); 
  35.  
  36. // Adding file actions 
  37. // add_filter( 'sanitize_option_' . $skip_form_instance_option_group . '_values', array( $this , 'validate_actions' ), 9999 ); 
  38.  
  39. $args = wp_parse_args( $args, $defaults ); 
  40. extract( $args , EXTR_SKIP ); 
  41.  
  42. $this->id = $id; 
  43. $this->delete = TRUE; 
  44. $this->insert_attachement = FALSE; 
  45.  
  46. $this->done_import = FALSE; 
  47.  
  48. parent::__construct( $name, $args ); 
  49.  
  50. function validate_actions( $input ) { 
  51. global $skip_form_instance_option_group; 
  52.  
  53. // If error occured 
  54. if( $_FILES[ $skip_form_instance_option_group . '_values' ][ 'error' ][ $this->wp_name ] != 0 ) { 
  55. $input[ $this->wp_name ] = $this->value; 
  56.  
  57. }else{ 
  58. $file[ 'tmp_name' ] = $_FILES[ $skip_form_instance_option_group . '_values' ][ 'tmp_name' ][ $this->wp_name ]; 
  59. $input = import_values( $skip_form_instance_option_group, $file[ 'tmp_name' ] );  
  60.  
  61. return $input; 
  62.  
  63. function get_html() { 
  64. $import_button = form_button( __( 'Import settings', 'skip_framework' ), array( 'name' => 'import_settings' ) );  
  65. $this->after_element = $import_button . $this->after_element; 
  66. $html = parent::get_html(); 
  67.  
  68. return $html; 
  69. /** 
  70. * Importing values from a file 
  71. * @package Skip\Forms 
  72. * @since 1.0 
  73. * @ignore 
  74. */ 
  75. function import_values( $option_group, $file_name ) { 
  76.  
  77. if( !file_exists( $file_name ) ) 
  78. return FALSE; 
  79.  
  80. $file_data = implode ( '', file ( $file_name ) ); 
  81.  
  82. $values = unserialize( $file_data ); 
  83.  
  84. return $values; 
  85. /** 
  86. * Import Button 
  87. * @package Skip\Forms 
  88. * @since 1.0 
  89. */  
  90. function import_button( $name, $args, $return = 'echo' ) { 
  91. $import_button = new Import_Button( $name, $args ); 
  92. return element_return( $import_button, $return ); 
.