/includes/skip/elements/forms/wp-file.php

  1. <?php 
  2. /** 
  3. * Skip WP Fileupload field 
  4. * @package Skip\Forms 
  5. * @since 1.0.0 
  6. * @ignore 
  7. */ 
  8.  
  9. namespace skip\v1_0_0; 
  10.  
  11. class WP_File extends Textfield{ 
  12.  
  13. /** 
  14. * Constructor 
  15. * @since 1.0.0 
  16. * @param string $name Name of WP File Field. 
  17. * @param array/string $args List of Arguments. 
  18. */ 
  19. function __construct( $name, $label = FALSE, $args = array() ) { 
  20.  
  21. $defaults = array( 
  22. 'wp_browse' => __( 'Add Media' ) 
  23. ); 
  24.  
  25. $args = wp_parse_args( $args, $defaults ); 
  26. $this->wp_browse = $args[ 'wp_browse' ]; 
  27.  
  28. parent::__construct( $name, $label, $args ); 
  29.  
  30. /** 
  31. * Rendering WP File field 
  32. * @package Skip 
  33. * @since 1.0 
  34. * @return string $html Returns The HTML Code. 
  35. */  
  36. public function render() { 
  37. global $skip_javascripts; 
  38.  
  39. $skip_javascripts[] = '  
  40. $("#skip_filebutton_' . $this->params[ 'id' ] . '").click(function() { 
  41.  
  42. if ( typeof wp !== "undefined" && wp.media && wp.media.editor ) { 
  43. wp.media.editor.open( "#skip_filebutton_' . $this->params[ 'id' ] . '" ); 
  44.  
  45. wp.media.editor.send.attachment = function( props, attachment ) { 
  46. console.log( attachment ); 
  47.  
  48. if( attachment.url.match(/\.jpg$/i) || attachment.url.match(/\.gif$/i) || attachment.url.match(/\.png$/i) ) { 
  49. preview_file = attachment.url; 
  50. }else{ 
  51. preview_file = attachment.icon; 
  52.  
  53. $( "#skip_file_' . $this->params[ 'id' ] . '" ).val( attachment.url ); 
  54. $( "#skip_filepreview_' . $this->params[ 'id' ] . '" ).attr( "src", preview_file );  
  55. $( "#skip_filename_' . $this->params[ 'id' ] . ' a" ).text( preview_file.replace( /.*\//, "" ) ); 
  56. $( "#skip_filename_' . $this->params[ 'id' ] . ' a" ).attr( "href", preview_file );  
  57. $( "#skip_filepreview_src_' . $this->params[ 'id' ] . '" ).val( preview_file ); 
  58.  
  59. }); 
  60.  
  61. $( "#skip_filepreview_' . $this->params[ 'id' ] . '" ).attr( "src", $( "#skip_filepreview_src_' . $this->params[ 'id' ] . '" ).val() ); 
  62.  
  63. '; 
  64.  
  65.  
  66. $html = $this->before_element; 
  67. $html.= '<div class="skip_file ui-state-default ui-corner-all">'; 
  68. $html.= '<div class="skip_filepreview">'; 
  69. $html.= '<img id="skip_filepreview_' . $this->params[ 'id' ] . '" class="skip_filepreview_image" />'; 
  70. if( isset( $this->value ) )  
  71. $html.= '<div class="skip_filename" id="skip_filename_' . $this->params[ 'id' ] . '"><a href="' . $this->value . '" target="_blank">' . basename( $this->value ) . '</a></div>'; 
  72.  
  73. $html.= '</div>'; 
  74. $html.= '<div class="skip_fileuploader">'; 
  75. $html.= '<input id="skip_file_' . $this->params[ 'id' ] . '" class="skip_file_name" type="text" name="' . $this->params[ 'name' ] . '" value="' . $this->value . '" />'; 
  76. $html.= '<input id="skip_filebutton_' . $this->params[ 'id' ] . '" class="skip_file_button" type="button" value="' . $this->wp_browse . '" />'; 
  77. $html.= hidden( $this->name . '_icon_src', array( 'id' => 'skip_filepreview_src_' . $this->params[ 'id' ] ) ); 
  78. $html.= '</div>'; 
  79. $html.= '</div>'; 
  80. $html.= $this->after_element; 
  81.  
  82. return $html; 
  83.  
  84.  
  85. /** 
  86. * Fileupload getter Function 
  87. * @see skip_wp_file() 
  88. * @ignore 
  89. */ 
  90. function get_wp_file( $name, $label = FALSE, $args = array(), $return = 'html' ) { 
  91. $file = new WP_File( $name, $label, $args ); 
  92. return $file->render(); 
  93.  
  94. /** 
  95. * <pre>skip_wp_file( $name, $args )</pre> 
  96. *  
  97. * Adding a File Field. 
  98. *  
  99. * <b>Default Usage</b> 
  100. * <code> 
  101. * skip_wp_file( 'myfile' ); 
  102. * </code> 
  103. * This will create an automated saved file field. 
  104. *  
  105. * <b>Parameters</b> 
  106. *  
  107. * <code> 
  108. * $name // (string) (required) The name of the field. 
  109. * $args // (array/string) (optional) Values for further settings. 
  110. * </code> 
  111. *  
  112. * <b>$args Settings</b> 
  113. *  
  114. * <ul> 
  115. * <li>id (string) ID if the HTML Element.</li>  
  116. * <li>label (string) Label for Element.</li>  
  117. * <li>classes (string) Name of CSS Classes which will be inserted into HTML seperated by empty space.</li> 
  118. * <li>before_element (string) Content before the element.</li> 
  119. * <li>after_element (string) Content after the element.</li> 
  120. * <li>save (boolean) TRUE if value of field have to be saved in Database, FALSE if not (default TRUE).</li> 
  121. * </ul> 
  122. *  
  123. * <b>Example</b> 
  124. *  
  125. * Creating a labeled WordPress Upload field in an automatic saved form. 
  126. * <code> 
  127. * skip_form_start( 'myformname' ); 
  128. *  
  129. * $args = array( 
  130. * 'id' = 'myelementid',  
  131. * 'label' => 'My File' 
  132. * ); 
  133. * skip_wp_file( 'myfile', $args ); 
  134. *  
  135. * skip_form_end(); 
  136. * </code> 
  137. *  
  138. * Getting back the saved data. 
  139. * <code> 
  140. * $filename = skip_value( 'myformname', 'myfile' ); 
  141. * </code> 
  142. * @package Skip\Forms 
  143. * @since 1.0 
  144. * @param string $name Name of File field. 
  145. * @param array/string $args List of Arguments. 
  146. */ 
  147. function wp_file( $name, $label = FALSE, $args = array() ) { 
  148. echo get_wp_file( $name, $label, $args ); 
.