PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/pods/ui/fields/plupload.php

https://bitbucket.org/mshmsh5000/wp-demo
PHP | 311 lines | 236 code | 66 blank | 9 comment | 50 complexity | befb697e04346734e6f30f316f29d35c MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. global $post_ID;
  3. wp_enqueue_script( 'pods-handlebars' );
  4. wp_enqueue_script( 'jquery-ui-core' );
  5. wp_enqueue_script( 'jquery-ui-sortable' );
  6. wp_enqueue_script( 'plupload-all' );
  7. wp_enqueue_script( 'pods-attach' );
  8. wp_enqueue_style( 'pods-attach' );
  9. $field_file = PodsForm::field_loader( 'file' );
  10. $attributes = array();
  11. $attributes = PodsForm::merge_attributes( $attributes, $name, $form_field_type, $options );
  12. $css_id = $attributes[ 'id' ];
  13. $uri_hash = wp_create_nonce( 'pods_uri_' . $_SERVER[ 'REQUEST_URI' ] );
  14. $uid = @session_id();
  15. if ( is_user_logged_in() )
  16. $uid = 'user_' . get_current_user_id();
  17. $field_nonce = wp_create_nonce( 'pods_upload_' . ( !is_object( $pod ) ? '0' : $pod->pod_id ) . '_' . $uid . '_' . $uri_hash . '_' . $options[ 'id' ] );
  18. $file_limit = 1;
  19. if ( 'multi' == pods_var( $form_field_type . '_format_type', $options, 'single' ) )
  20. $file_limit = (int) pods_var( $form_field_type . '_limit', $options, 0 );
  21. $plupload_init = array(
  22. 'runtimes' => 'html5,silverlight,flash,html4',
  23. 'browse_button' => $css_id . '-upload',
  24. 'url' => admin_url( 'admin-ajax.php', 'relative' ) . '?pods_ajax=1',
  25. 'file_data_name' => 'Filedata',
  26. 'multiple_queues' => false,
  27. 'max_file_size' => wp_max_upload_size() . 'b',
  28. 'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ),
  29. 'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
  30. 'filters' => array( array( 'title' => __( 'Allowed Files', 'pods' ), 'extensions' => '*' ) ),
  31. 'multipart' => true,
  32. 'urlstream_upload' => true,
  33. 'multipart_params' => array(
  34. '_wpnonce' => $field_nonce,
  35. 'action' => 'pods_upload',
  36. 'method' => 'upload',
  37. 'pod' => ( !is_object( $pod ) ? '0' : $pod->pod_id ),
  38. 'field' => $options[ 'id' ],
  39. 'uri' => $uri_hash
  40. ),
  41. );
  42. $limit_file_type = pods_var( $form_field_type . '_type', $options, 'images' );
  43. $title_editable = pods_var( $form_field_type . '_edit_title', $options, 0 );
  44. if ( 'images' == $limit_file_type )
  45. $limit_types = 'jpg,png,gif';
  46. elseif ( 'video' == $limit_file_type )
  47. $limit_types = 'mpg,mov,flv,mp4';
  48. elseif ( 'audio' == $limit_file_type )
  49. $limit_types = 'mp3,m4a,wav,wma';
  50. elseif ( 'text' == $limit_file_type )
  51. $limit_types = 'txt,rtx,csv,tsv';
  52. elseif ( 'any' == $limit_file_type )
  53. $limit_types = '';
  54. else
  55. $limit_types = pods_var( $form_field_type . '_allowed_extensions', $options, '', null, true );
  56. $limit_types = trim( str_replace( array( ' ', '.', "\n", "\t", ';' ), array( '', ',', ',', ',' ), $limit_types ), ',' );
  57. if ( pods_wp_version( '3.5' ) ) {
  58. $mime_types = wp_get_mime_types();
  59. if ( in_array( $limit_file_type, array( 'images', 'audio', 'video' ) ) ) {
  60. $new_limit_types = array();
  61. foreach ( $mime_types as $type => $mime ) {
  62. if ( 0 === strpos( $mime, $limit_file_type ) ) {
  63. $type = explode( '|', $type );
  64. $new_limit_types = array_merge( $new_limit_types, $type );
  65. }
  66. }
  67. if ( !empty( $new_limit_types ) )
  68. $limit_types = implode( ',', $new_limit_types );
  69. }
  70. elseif ( 'any' != $limit_file_type ) {
  71. $new_limit_types = array();
  72. $limit_types = explode( ',', $limit_types );
  73. foreach ( $limit_types as $k => $limit_type ) {
  74. $found = false;
  75. foreach ( $mime_types as $type => $mime ) {
  76. if ( 0 === strpos( $mime, $limit_type ) ) {
  77. $type = explode( '|', $type );
  78. foreach ( $type as $t ) {
  79. if ( !in_array( $t, $new_limit_types ) )
  80. $new_limit_types[] = $t;
  81. }
  82. $found = true;
  83. }
  84. }
  85. if ( !$found )
  86. $new_limit_types[] = $limit_type;
  87. }
  88. if ( !empty( $new_limit_types ) )
  89. $limit_types = implode( ', ', $new_limit_types );
  90. }
  91. }
  92. if ( !empty( $limit_types ) )
  93. $plupload_init[ 'filters' ][ 0 ][ 'extensions' ] = $limit_types;
  94. if ( is_admin() && !empty( $post_ID ) )
  95. $plupload_init[ 'multipart_params' ][ 'post_id' ] = (int) $post_ID;
  96. $plupload_init = apply_filters( 'plupload_init', $plupload_init );
  97. if ( empty( $value ) )
  98. $value = array();
  99. else
  100. $value = (array) $value;
  101. ?>
  102. <div<?php PodsForm::attributes( array( 'class' => $attributes[ 'class' ] ), $name, $form_field_type, $options ); ?>>
  103. <table class="form-table pods-metabox pods-form-ui-table-type-<?php echo $form_field_type; ?>" id="<?php echo $css_id; ?>">
  104. <tbody>
  105. <tr class="form-field">
  106. <td>
  107. <ul class="pods-files pods-files-list"><?php // no extra space in ul or CSS:empty won't work
  108. foreach ( $value as $val ) {
  109. $attachment = get_post( $val );
  110. if ( empty( $attachment ) )
  111. continue;
  112. $thumb = wp_get_attachment_image_src( $val, 'thumbnail', true );
  113. $title = $attachment->post_title;
  114. if ( 0 == $title_editable )
  115. $title = basename( $attachment->guid );
  116. echo $field_file->markup( $attributes, $file_limit, $title_editable, $val, $thumb[ 0 ], $title );
  117. }
  118. ?></ul>
  119. <a class="button pods-file-add plupload-add" id="<?php echo $css_id; ?>-upload" href="" tabindex="2"><?php echo pods_var_raw( $form_field_type . '_add_button', $options, __( 'Add File', 'pods' ) ); ?></a>
  120. <ul class="pods-files pods-files-queue"></ul>
  121. </td>
  122. </tr>
  123. </tbody>
  124. </table>
  125. </div>
  126. <script type="text/x-handlebars" id="<?php echo $css_id; ?>-handlebars">
  127. <?php echo $field_file->markup( $attributes, $file_limit, $title_editable ); ?>
  128. </script>
  129. <script type="text/x-handlebars" id="<?php echo $css_id; ?>-progress-template">
  130. <li class="pods-file" id="{{id}}">
  131. <ul class="pods-file-meta media-item">
  132. <li class="pods-file-col pods-progress">
  133. <div class="progress-bar">&nbsp;</div>
  134. </li>
  135. <li class="pods-file-col pods-file-name">{{filename}}</li>
  136. </ul>
  137. </li>
  138. </script>
  139. <script>
  140. jQuery( function ( $ ) {
  141. <?php if ( 1 != $file_limit ) { ?>
  142. // init sortable
  143. $( '#<?php echo esc_js( $css_id ); ?> ul.pods-files-list' ).sortable( {
  144. containment : 'parent',
  145. axis: 'y',
  146. scrollSensitivity : 40,
  147. tolerance : 'pointer',
  148. opacity : 0.6
  149. } );
  150. <?php } ?>
  151. // hook delete links
  152. $( '#<?php echo esc_js( $css_id ); ?>' ).on( 'click', 'li.pods-file-delete', function () {
  153. var podsfile = $( this ).parent().parent();
  154. podsfile.slideUp( function () {
  155. // check to see if this was the only entry
  156. if ( podsfile.parent().children().length == 1 ) { // 1 because we haven't removed our target yet
  157. podsfile.parent().hide();
  158. }
  159. // remove the entry
  160. $(this).remove();
  161. } );
  162. } );
  163. var pods_uploader_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?> = new plupload.Uploader( <?php echo json_encode( $plupload_init ); ?> ),
  164. list_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?> = $( '#<?php echo esc_js( $css_id ); ?> ul.pods-files-list' ),
  165. queue_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?> = $( '#<?php echo esc_js( $css_id ); ?> ul.pods-files-queue' ),
  166. maxFiles_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?> = <?php echo esc_js( $file_limit ); ?>;
  167. pods_uploader_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?>.init();
  168. // Plupload FilesAdded Event Handler
  169. pods_uploader_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?>.bind( 'FilesAdded', function ( up, files ) {
  170. // Hide any existing files (for use in single/limited field configuration)
  171. if ( 1 == maxFiles_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?> ) {
  172. jQuery( '#<?php echo $css_id; ?> ul.pods-files-list li.pods-file' ).remove();
  173. jQuery( '#<?php echo $css_id; ?> ul.pods-files-list' ).hide();
  174. }
  175. jQuery.each( files, function ( index, file ) {
  176. var binding = { id : file.id, filename : file.name },
  177. tmpl = Handlebars.compile( $( '#<?php echo esc_js( $css_id ); ?>-progress-template' ).html() ),
  178. html = tmpl( binding );
  179. queue_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?>.append( html );
  180. //$('#' + file.id).show('slide', {direction: 'up'}, 1000);
  181. $( '#' + file.id ).fadeIn( 800 );
  182. jQuery( '#<?php echo $css_id; ?> ul.pods-files-queue' ).show();
  183. } );
  184. up.refresh();
  185. up.start();
  186. } );
  187. // Plupload UploadProgress Event Handler
  188. pods_uploader_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?>.bind( 'UploadProgress', function ( up, file ) {
  189. var prog_bar = $( '#' + file.id ).find( '.progress-bar' );
  190. prog_bar.css( 'width', file.percent + '%' );
  191. } );
  192. // Plupload FileUploaded Event Handler
  193. pods_uploader_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?>.bind( 'FileUploaded', function ( up, file, resp ) {
  194. var file_div = jQuery( '#' + file.id ),
  195. response = resp.response;
  196. if ( "Error: " == resp.response.substr( 0, 7 ) ) {
  197. response = response.substr( 7 );
  198. if ( window.console ) console.log( response );
  199. file_div.append( response );
  200. }
  201. else if ( "<e>" == resp.response.substr( 0, 3 ) ) {
  202. response = response.substr( 3 );
  203. if ( window.console ) console.log( response );
  204. file_div.append( response );
  205. }
  206. else {
  207. var json = response.match( /{.*}$/ );
  208. if ( 0 < json.length )
  209. json = jQuery.parseJSON( json[ 0 ] );
  210. else
  211. json = {};
  212. if ( 'object' != typeof json || jQuery.isEmptyObject( json ) ) {
  213. if ( window.console ) console.log( response );
  214. if ( window.console ) console.log( json );
  215. file_div.append( '<?php echo esc_js( __( 'There was an issue with the file upload, please try again.', 'pods' ) ); ?>' );
  216. return;
  217. }
  218. file_div.fadeOut( 800, function () {
  219. list_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?>.show();
  220. if ( $( this ).parent().children().length == 1 )
  221. jQuery( '#<?php echo $css_id; ?> ul.pods-files-queue' ).hide();
  222. $( this ).remove();
  223. } );
  224. var binding = {
  225. id : json.ID,
  226. icon : json.thumbnail,
  227. name : json.post_title
  228. };
  229. var tmpl = Handlebars.compile( $( 'script#<?php echo esc_js( $css_id ); ?>-handlebars' ).html() );
  230. var html = tmpl( binding );
  231. list_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?>.prepend( html );
  232. list_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?>.find( 'li.pods-file:first' ).slideDown( 'fast' );
  233. var items = list_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?>.find( 'li.pods-file' ),
  234. itemCount = items.size();
  235. if ( 0 < maxFiles_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?> && itemCount > maxFiles_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?> ) {
  236. items.each( function ( idx, elem ) {
  237. if ( idx + 1 > maxFiles_<?php echo pods_clean_name( $attributes[ 'name' ] ); ?> ) {
  238. jQuery( elem ).remove();
  239. }
  240. } );
  241. }
  242. }
  243. } );
  244. } );
  245. </script>