PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/theme WP a cương/test_wp/wp-content/themes/neighborhood/includes/swift-framework/meta-box/inc/fields/plupload-image.php

https://gitlab.com/hop23typhu/list-theme
PHP | 195 lines | 120 code | 21 blank | 54 comment | 8 complexity | edf3502303dcfbc3351030929526f7fe MD5 | raw file
  1. <?php
  2. // Prevent loading this file directly
  3. defined( 'ABSPATH' ) || exit;
  4. if ( ! class_exists( 'RWMB_Plupload_Image_Field' ) )
  5. {
  6. class RWMB_Plupload_Image_Field extends RWMB_Image_Field
  7. {
  8. /**
  9. * Add field actions
  10. *
  11. * @return void
  12. */
  13. static function add_actions()
  14. {
  15. parent::add_actions();
  16. add_action( 'wp_ajax_rwmb_plupload_image_upload', array( __CLASS__, 'handle_upload' ) );
  17. }
  18. /**
  19. * Upload
  20. * Ajax callback function
  21. *
  22. * @return string Error or (XML-)response
  23. */
  24. static function handle_upload()
  25. {
  26. global $wpdb;
  27. $post_id = is_numeric( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : 0;
  28. $field_id = isset( $_REQUEST['field_id'] ) ? $_REQUEST['field_id'] : '';
  29. check_ajax_referer( "rwmb-upload-images_{$field_id}" );
  30. // You can use WP's wp_handle_upload() function:
  31. $file = $_FILES['async-upload'];
  32. $file_attr = wp_handle_upload( $file, array( 'test_form' => false ) );
  33. //Get next menu_order
  34. $meta = get_post_meta( $post_id, $field_id, false );
  35. if( empty( $meta ) ){
  36. $next = 0;
  37. } else {
  38. $meta = implode( ',' , (array) $meta );
  39. $max = $wpdb->get_var( "
  40. SELECT MAX(menu_order) FROM {$wpdb->posts}
  41. WHERE post_type = 'attachment'
  42. AND ID in ({$meta})
  43. " );
  44. $next = is_numeric($max) ? (int) $max + 1: 0;
  45. }
  46. $attachment = array(
  47. 'guid' => $file_attr['url'],
  48. 'post_mime_type' => $file_attr['type'],
  49. 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file['name'] ) ),
  50. 'post_content' => '',
  51. 'post_status' => 'inherit',
  52. 'menu_order' => $next
  53. );
  54. // Adds file as attachment to WordPress
  55. $id = wp_insert_attachment( $attachment, $file_attr['file'], $post_id );
  56. if ( ! is_wp_error( $id ) )
  57. {
  58. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file_attr['file'] ) );
  59. // Save file ID in meta field
  60. add_post_meta( $post_id, $field_id, $id, false );
  61. RW_Meta_Box::ajax_response( self::img_html( $id ), 'success' );
  62. }
  63. exit;
  64. }
  65. /**
  66. * Enqueue scripts and styles
  67. *
  68. * @return void
  69. */
  70. static function admin_enqueue_scripts()
  71. {
  72. // Enqueue same scripts and styles as for file field
  73. parent::admin_enqueue_scripts();
  74. wp_enqueue_style( 'rwmb-plupload-image', RWMB_CSS_URL . 'plupload-image.css', array( 'wp-admin' ), RWMB_VER );
  75. wp_enqueue_script( 'rwmb-plupload-image', RWMB_JS_URL . 'plupload-image.js', array( 'jquery-ui-sortable', 'wp-ajax-response', 'plupload-all' ), RWMB_VER, true );
  76. wp_localize_script( 'rwmb-plupload-image', 'RWMB', array( 'url' => RWMB_URL ) );
  77. }
  78. /**
  79. * Get field HTML
  80. *
  81. * @param string $html
  82. * @param mixed $meta
  83. * @param array $field
  84. *
  85. * @return string
  86. */
  87. static function html( $html, $meta, $field )
  88. {
  89. if ( ! is_array( $meta ) )
  90. $meta = ( array ) $meta;
  91. // Filter to change the drag & drop box background string
  92. $i18n_drop = apply_filters( 'rwmb_plupload_image_drop_string', _x( 'Drop images here', 'image upload', 'rwmb' ), $field );
  93. $i18n_or = apply_filters( 'rwmb_plupload_image_or_string', _x( 'or', 'image upload', 'rwmb' ), $field );
  94. $i18n_select = apply_filters( 'rwmb_plupload_image_select_string', _x( 'Select Files', 'image upload', 'rwmb' ), $field );
  95. // Uploaded images
  96. // Check for max_file_uploads
  97. $classes = array( 'rwmb-drag-drop', 'drag-drop', 'hide-if-no-js', 'new-files');
  98. if ( ! empty( $field['max_file_uploads'] ) && count( $meta ) >= (int) $field['max_file_uploads'] )
  99. $classes[] = 'hidden';
  100. $html .= self::get_uploaded_images( $meta, $field );
  101. // Show form upload
  102. $html .= sprintf(
  103. '<div id="%s-dragdrop" class="%s" data-upload_nonce="%s" data-js_options="%s">
  104. <div class = "drag-drop-inside">
  105. <p class="drag-drop-info">%s</p>
  106. <p>%s</p>
  107. <p class="drag-drop-buttons"><input id="%s-browse-button" type="button" value="%s" class="button" /></p>
  108. </div>
  109. </div>',
  110. $field['id'],
  111. implode( ' ', $classes ),
  112. wp_create_nonce( "rwmb-upload-images_{$field['id']}" ),
  113. esc_attr( json_encode( $field['js_options'] ) ),
  114. $i18n_drop,
  115. $i18n_or,
  116. $field['id'],
  117. $i18n_select
  118. );
  119. return $html;
  120. }
  121. /**
  122. * Get field value
  123. * It's the combination of new (uploaded) images and saved images
  124. *
  125. * @param array $new
  126. * @param array $old
  127. * @param int $post_id
  128. * @param array $field
  129. *
  130. * @return array|mixed
  131. */
  132. static function value( $new, $old, $post_id, $field )
  133. {
  134. $new = (array) $new;
  135. return array_unique( array_merge( $old, $new ) );
  136. }
  137. /**
  138. * Normalize parameters for field
  139. *
  140. * @param array $field
  141. *
  142. * @return array
  143. */
  144. static function normalize_field( $field )
  145. {
  146. $field['js_options'] = array(
  147. 'runtimes' => 'html5,silverlight,flash,html4',
  148. 'file_data_name' => 'async-upload',
  149. //'container' => $field['id'] . '-container',
  150. 'browse_button' => $field['id'] . '-browse-button',
  151. 'drop_element' => $field['id'] . '-dragdrop',
  152. 'multiple_queues' => true,
  153. 'max_file_size' => wp_max_upload_size() . 'b',
  154. 'url' => admin_url( 'admin-ajax.php' ),
  155. 'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ),
  156. 'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
  157. 'multipart' => true,
  158. 'urlstream_upload' => true,
  159. 'filters' => array(
  160. array(
  161. 'title' => _x( 'Allowed Image Files', 'image upload', 'rwmb' ),
  162. 'extensions' => 'jpg,jpeg,gif,png',
  163. ),
  164. ),
  165. 'multipart_params' => array(
  166. 'field_id' => $field['id'],
  167. 'action' => 'rwmb_plupload_image_upload',
  168. )
  169. );
  170. $field = parent::normalize_field( $field );
  171. return $field;
  172. }
  173. }
  174. }