/includes/skip/elements/forms/export.php

  1. <?php 
  2. /** 
  3. * Export Button 
  4. *  
  5. * Shows a Button to export data from a form 
  6. *  
  7. * @package Skip\Forms 
  8. * @since 1.0 
  9. * @ignore 
  10. */ 
  11.  
  12. namespace skip\v1_0_0; 
  13.  
  14. class Export_Button extends WP_Form_Button{ 
  15.  
  16. /** 
  17. * Constructor 
  18. * 
  19. * @package Skip 
  20. * @since 1.0 
  21. * @param array $args Array of [ $value Value, $args ] 
  22. */ 
  23. function __construct( $value, $args = array() ) { 
  24. global $skip_form_instance_option_group; 
  25.  
  26. $defaults = array( 
  27. 'id' => '',  
  28. 'name' => $value,  
  29. 'forms' => array(),  
  30. 'file_name' => 'export_' . date( 'Ymdhis', time() ) . '.txt',  
  31. 'extra' => '',  
  32. 'before_element' => '',  
  33. 'after_element' => '' 
  34. ); 
  35.  
  36. add_filter( 'sanitize_option_' . $skip_form_instance_option_group . '_values', array( $this , 'validate_actions' ), 9999 ); 
  37.  
  38. $args = wp_parse_args($args, $defaults); 
  39. extract( $args , EXTR_SKIP ); 
  40.  
  41. parent::__construct( $value, $args ); 
  42.  
  43. $this->lookup_name = $name; 
  44.  
  45. $this->submit = TRUE; 
  46. $this->forms = $forms; 
  47. $this->file_name = $file_name; 
  48. $this->extra = $extra; 
  49.  
  50. function validate_actions( $input ) { 
  51. global $skip_form_instance_option_group; 
  52.  
  53. if( $input[ $this->lookup_name ] != '' ) { 
  54. download_export_values( $this->forms, $this->file_name ); 
  55. $input = get_option( $skip_form_instance_option_group . '_values' ); 
  56. return $input; 
  57.  
  58. /** 
  59. * Exporting values as a string 
  60. * @package Skip\Forms 
  61. * @since 1.0 
  62. * @ignore 
  63. */ 
  64. function export_values( $option_groups ) { 
  65. foreach( $option_groups AS $option_group ) { 
  66. $values = serialize ( (array) get_values( $option_group ) ); 
  67. $serialized_val.= $values ; 
  68. return $serialized_val; 
  69. /** 
  70. * Exporting values in a file as download 
  71. * @package Skip\Forms 
  72. * @since 1.0 
  73. * @ignore 
  74. */ 
  75. function download_export_values( $option_groups, $file_name = 'export.skp' ) { 
  76. header("Content-Type: text/plain"); 
  77. header('Content-Disposition: attachment; filename="' . $file_name . '"'); 
  78. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 
  79.  
  80. echo export_values( $option_groups ); 
  81.  
  82. exit; 
  83. /** 
  84. * Export Button 
  85. * @package Skip\Forms 
  86. * @since 1.0 
  87. */ 
  88. function export_button( $value, $args, $return = 'echo' ) { 
  89. $export_button = new Export_Button( $value, $args ); 
  90. return element_return( $export_button, $return );  
.