<?php
namespace skip\v1_0_0;
class WP_File extends Textfield{
function __construct( $name, $label = FALSE, $args = array() ) {
$defaults = array(
'wp_browse' => __( 'Add Media' )
);
$args = wp_parse_args( $args, $defaults );
$this->wp_browse = $args[ 'wp_browse' ];
parent::__construct( $name, $label, $args );
}
public function render() {
global $skip_javascripts;
$skip_javascripts[] = '
$("#skip_filebutton_' . $this->params[ 'id' ] . '").click(function() {
if ( typeof wp !== "undefined" && wp.media && wp.media.editor ) {
wp.media.editor.open( "#skip_filebutton_' . $this->params[ 'id' ] . '" );
wp.media.editor.send.attachment = function( props, attachment ) {
console.log( attachment );
if( attachment.url.match(/\.jpg$/i) || attachment.url.match(/\.gif$/i) || attachment.url.match(/\.png$/i) ) {
preview_file = attachment.url;
}else{
preview_file = attachment.icon;
}
$( "#skip_file_' . $this->params[ 'id' ] . '" ).val( attachment.url );
$( "#skip_filepreview_' . $this->params[ 'id' ] . '" ).attr( "src", preview_file );
$( "#skip_filename_' . $this->params[ 'id' ] . ' a" ).text( preview_file.replace( /.*\
$( "#skip_filename_' . $this->params[ 'id' ] . ' a" ).attr( "href", preview_file );
$( "#skip_filepreview_src_' . $this->params[ 'id' ] . '" ).val( preview_file );
}
}
});
$( "#skip_filepreview_' . $this->params[ 'id' ] . '" ).attr( "src", $( "#skip_filepreview_src_' . $this->params[ 'id' ] . '" ).val() );
';
$html = $this->before_element;
$html.= '<div class="skip_file ui-state-default ui-corner-all">';
$html.= '<div class="skip_filepreview">';
$html.= '<img id="skip_filepreview_' . $this->params[ 'id' ] . '" class="skip_filepreview_image" />';
if( isset( $this->value ) )
$html.= '<div class="skip_filename" id="skip_filename_' . $this->params[ 'id' ] . '"><a href="' . $this->value . '" target="_blank">' . basename( $this->value ) . '</a></div>';
$html.= '</div>';
$html.= '<div class="skip_fileuploader">';
$html.= '<input id="skip_file_' . $this->params[ 'id' ] . '" class="skip_file_name" type="text" name="' . $this->params[ 'name' ] . '" value="' . $this->value . '" />';
$html.= '<input id="skip_filebutton_' . $this->params[ 'id' ] . '" class="skip_file_button" type="button" value="' . $this->wp_browse . '" />';
$html.= hidden( $this->name . '_icon_src', array( 'id' => 'skip_filepreview_src_' . $this->params[ 'id' ] ) );
$html.= '</div>';
$html.= '</div>';
$html.= $this->after_element;
return $html;
}
}
function get_wp_file( $name, $label = FALSE, $args = array(), $return = 'html' ) {
$file = new WP_File( $name, $label, $args );
return $file->render();
}
function wp_file( $name, $label = FALSE, $args = array() ) {
echo get_wp_file( $name, $label, $args );
}