Changeset 13891 for trunk/src/bp-core/bp-core-attachments.php
- Timestamp:
- 06/02/2024 01:12:39 AM (14 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-attachments.php
r13890 r13891 74 74 * @since 3.0.0 75 75 * 76 * @return array See wp_upload_dir(). 76 * @param array $args { 77 * @type int $object_id The ID of the object (Required). 78 * @type string $object_directory The object directory (eg: members, groups) (Required). 79 * } 80 * @return array 77 81 */ 78 82 function bp_attachments_cover_image_upload_dir( $args = array() ) { … … 102 106 103 107 // Set the subdir. 104 $subdir 108 $subdir = '/' . $r['object_directory'] . '/' . $r['object_id'] . '/cover-image'; 105 109 106 110 $upload_dir = bp_attachments_uploads_dir_get(); … … 111 115 * @since 2.4.0 112 116 * 113 * @param array $ valueArray containing the path, URL, and other helpful settings.114 * @param array $upload_dir The original Uploads dir.117 * @param array $image_upload_data Array containing the path, URL, and other helpful settings. 118 * @param array $upload_dir The original Uploads dir. 115 119 */ 116 return apply_filters( 'bp_attachments_cover_image_upload_dir', array( 117 'path' => $upload_dir['basedir'] . $subdir, 118 'url' => set_url_scheme( $upload_dir['baseurl'] ) . $subdir, 119 'subdir' => $subdir, 120 'basedir' => $upload_dir['basedir'], 121 'baseurl' => set_url_scheme( $upload_dir['baseurl'] ), 122 'error' => false, 123 ), $upload_dir ); 120 return apply_filters( 121 'bp_attachments_cover_image_upload_dir', 122 array( 123 'path' => $upload_dir['basedir'] . $subdir, 124 'url' => set_url_scheme( $upload_dir['baseurl'] ) . $subdir, 125 'subdir' => $subdir, 126 'basedir' => $upload_dir['basedir'], 127 'baseurl' => set_url_scheme( $upload_dir['baseurl'] ), 128 'error' => false, 129 ), 130 $upload_dir 131 ); 124 132 } 125 133 … … 197 205 // Loop to explode keys using '|'. 198 206 foreach ( $allowed_types as $allowed_type ) { 199 $t = explode( '|', $allowed_type );207 $t = explode( '|', $allowed_type ); 200 208 $exts = array_merge( $exts, (array) $t ); 201 209 } … … 417 425 'crop_h' => $r['crop_h'], 418 426 'crop_x' => $r['crop_x'], 419 'crop_y' => $r['crop_y'] 427 'crop_y' => $r['crop_y'], 420 428 ) 421 429 ); 422 430 423 // It's a cover image we need to fit it to feature's dimensions.431 // It's a cover image we need to fit it to feature's dimensions. 424 432 } elseif ( 'cover_image' === $type ) { 425 433 $cover_image = bp_attachments_cover_image_generate_file( … … 427 435 'file' => $image_file_path, 428 436 'component' => $r['component'], 429 'cover_image_dir' => $attachment_data['path'] 437 'cover_image_dir' => $attachment_data['path'], 430 438 ) 431 439 ); … … 520 528 $attachment_data = trailingslashit( $type_dir ) . $r['file']; 521 529 } 522 523 530 } else { 524 531 $file = false; 525 532 526 533 // Open the directory and get the first file. 527 if ( $att_dir = opendir( $type_dir ) ) { 534 $att_dir = opendir( $type_dir ); 535 if ( $att_dir ) { 528 536 529 537 while ( false !== ( $attachment_file = readdir( $att_dir ) ) ) { … … 604 612 'file_data_name' => 'file', 605 613 'multipart_params' => array( 606 'action' 607 '_wpnonce' 614 'action' => 'bp_upload_attachment', 615 '_wpnonce' => wp_create_nonce( 'bp-uploader' ), 608 616 ), 609 617 'url' => admin_url( 'admin-ajax.php', 'relative' ), 610 618 'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ), 611 619 'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ), 612 'filters' => array(613 'max_file_size' 620 'filters' => array( 621 'max_file_size' => $max_upload_size . 'b', 614 622 ), 615 623 'multipart' => true, … … 625 633 626 634 $settings = array( 627 'defaults' => $defaults,628 'browser' => array(635 'defaults' => $defaults, 636 'browser' => array( 629 637 'mobile' => wp_is_mobile(), 630 638 'supported' => _device_can_upload(), … … 692 700 /* translators: %s: File name. */ 693 701 'error_uploading' => __( '“%s” has failed to upload.', 'buddypress' ), 694 'has_avatar_warning' => __( 'If you'd like to delete the existing profile photo but not upload a new one, please use the delete tab.', 'buddypress' ) 702 'has_avatar_warning' => __( 'If you'd like to delete the existing profile photo but not upload a new one, please use the delete tab.', 'buddypress' ), 695 703 ) 696 704 ); … … 705 713 * to set specific script data. 706 714 * 707 * @param string $class Name of the class extending BP_Attachment (eg: BP_Attachment_Avatar).715 * @param string $class_name Name of the class extending BP_Attachment (eg: BP_Attachment_Avatar). 708 716 * @return null|WP_Error 709 717 */ 710 function bp_attachments_enqueue_scripts( $class = '' ) { 718 function bp_attachments_enqueue_scripts( $class_name = '' ) { 719 $class = $class_name; 720 711 721 // Enqueue me just once per page, please. 712 722 if ( did_action( 'bp_attachments_enqueue_scripts' ) ) { … … 719 729 720 730 // Get an instance of the class and get the script data. 721 $attachment = new $class ;731 $attachment = new $class(); 722 732 $script_data = $attachment->script_data(); 723 733 … … 766 776 767 777 // Merge other arguments. 768 $ui_args = array_intersect_key( $args, array( 769 'file_data_name' => true, 770 'browse_button' => true, 771 'container' => true, 772 'drop_element' => true, 773 ) ); 778 $ui_args = array_intersect_key( 779 $args, 780 array( 781 'file_data_name' => true, 782 'browse_button' => true, 783 'container' => true, 784 'drop_element' => true, 785 ) 786 ); 774 787 775 788 $defaults = array_merge( $defaults, $ui_args ); … … 780 793 781 794 if ( isset( $args['mime_types'] ) && $args['mime_types'] ) { 782 $defaults['filters']['mime_types'] = 795 $defaults['filters']['mime_types'] = array( array( 'extensions' => $args['mime_types'] ) ); 783 796 } 784 797 … … 793 806 // Include the cropping informations for avatars. 794 807 $settings['crop'] = array( 795 'full_h' 796 'full_w' 808 'full_h' => bp_core_avatar_full_height(), 809 'full_w' => bp_core_avatar_full_width(), 797 810 ); 798 811 … … 811 824 // Init the Avatar nav. 812 825 $avatar_nav = array( 813 'upload' 826 'upload' => array( 814 827 'id' => 'upload', 815 828 'caption' => __( 'Upload', 'buddypress' ), 816 'order' => 0 829 'order' => 0, 817 830 ), 818 'delete' 831 'delete' => array( 819 832 'id' => 'delete', 820 833 'caption' => __( 'Delete', 'buddypress' ), 821 834 'order' => 100, 822 'hide' => (int) ! $has_avatar 835 'hide' => (int) ! $has_avatar, 823 836 ), 824 837 ); … … 851 864 'id' => 'camera', 852 865 'caption' => __( 'Take Photo', 'buddypress' ), 853 'order' => 10 866 'order' => 10, 854 867 ); 855 868 856 869 // Set warning messages. 857 870 $strings['camera_warnings'] = array( 858 'requesting' => __( 'Please allow us to access to your camera.', 'buddypress'),859 'loading' 860 'loaded' 861 'noaccess' 862 'errormsg' 863 'videoerror' 864 'ready' 865 'nocapture' 871 'requesting' => __( 'Please allow us to access to your camera.', 'buddypress' ), 872 'loading' => __( 'Please wait as we access your camera.', 'buddypress' ), 873 'loaded' => __( 'Camera loaded. Click on the "Capture" button to take your photo.', 'buddypress' ), 874 'noaccess' => __( 'It looks like you do not have a webcam or we were unable to get permission to use your webcam. Please upload a photo instead.', 'buddypress' ), 875 'errormsg' => __( 'Your browser is not supported. Please upload a photo instead.', 'buddypress' ), 876 'videoerror' => __( 'Video error. Please upload a photo instead.', 'buddypress' ), 877 'ready' => __( 'Your profile photo is ready. Click on the "Save" button to use this photo.', 'buddypress' ), 878 'nocapture' => __( 'No photo was captured. Click on the "Capture" button to take your photo.', 'buddypress' ), 866 879 ); 867 880 } … … 886 899 $settings['nav'] = bp_sort_by_key( apply_filters( 'bp_attachments_avatar_nav', $avatar_nav, $object ), 'order', 'num' ); 887 900 888 // Specific to BuddyPress cover images.901 // Specific to BuddyPress cover images. 889 902 } elseif ( 'bp_cover_image_upload' === $defaults['multipart_params']['action'] ) { 890 903 … … 908 921 909 922 // Set warning messages. 910 $strings['cover_image_warnings'] = apply_filters( 'bp_attachments_cover_image_ui_warnings', array( 911 'dimensions' => sprintf( 912 /* translators: 1: the advised width size in pixels. 2: the advised height size in pixels. */ 913 __( 'For better results, make sure to upload an image that is larger than %1$spx wide, and %2$spx tall.', 'buddypress' ), 914 (int) $cover_dimensions['width'], 915 (int) $cover_dimensions['height'] 916 ), 917 ) ); 923 $strings['cover_image_warnings'] = apply_filters( 924 'bp_attachments_cover_image_ui_warnings', 925 array( 926 'dimensions' => sprintf( 927 /* translators: 1: the advised width size in pixels. 2: the advised height size in pixels. */ 928 __( 'For better results, make sure to upload an image that is larger than %1$spx wide, and %2$spx tall.', 'buddypress' ), 929 (int) $cover_dimensions['width'], 930 (int) $cover_dimensions['height'] 931 ), 932 ) 933 ); 918 934 } 919 935 … … 936 952 } 937 953 938 wp_enqueue_script ( 'bp-plupload' ); 939 wp_localize_script( 'bp-plupload', 'BP_Uploader', array( 'strings' => $strings, 'settings' => $settings ) ); 954 wp_enqueue_script( 'bp-plupload' ); 955 wp_localize_script( 956 'bp-plupload', 957 'BP_Uploader', 958 array( 959 'strings' => $strings, 960 'settings' => $settings, 961 ) 962 ); 940 963 941 964 /** … … 987 1010 $can = (bool) groups_is_user_admin( bp_loggedin_user_id(), $args['item_id'] ) || bp_current_user_can( 'bp_moderate' ); 988 1011 } 989 // User profile photo.1012 // User profile photo. 990 1013 } elseif ( bp_is_active( 'members' ) && 'user' === $args['object'] ) { 991 1014 $can = bp_loggedin_user_id() === (int) $args['item_id'] || bp_current_user_can( 'bp_moderate' ); 992 1015 } 993 /** 994 * No avatar arguments, fallback to bp_user_can_create_groups() 995 * or bp_is_item_admin() 996 */ 1016 /** 1017 * No avatar arguments, fallback to bp_user_can_create_groups() 1018 * or bp_is_item_admin() 1019 */ 1020 } elseif ( bp_is_group_create() ) { 1021 $can = bp_user_can_create_groups(); 997 1022 } else { 998 if ( bp_is_group_create() ) { 999 $can = bp_user_can_create_groups(); 1000 } else { 1001 $can = bp_is_item_admin(); 1002 } 1023 $can = bp_is_item_admin(); 1003 1024 } 1004 1025 } … … 1027 1048 wp_send_json( $response ); 1028 1049 1029 /**1030 * Send specific json response1031 * the html4 Plupload handler requires a text/html content-type for older IE.1032 * See https://core.trac.wordpress.org/ticket/310371033 */1050 /** 1051 * Send specific json response 1052 * the html4 Plupload handler requires a text/html content-type for older IE. 1053 * See https://core.trac.wordpress.org/ticket/31037 1054 */ 1034 1055 } else { 1035 1056 echo wp_json_encode( $response ); … … 1045 1066 * 1046 1067 * @param string $slug Template part slug. eg 'uploader' for 'uploader.php'. 1047 * @return bool1048 1068 */ 1049 1069 function bp_attachments_get_template_part( $slug ) { … … 1137 1157 1138 1158 // Current component is not supported. 1139 if ( ! in_array( $component, $settings['components'] ) ) {1159 if ( ! in_array( $component, $settings['components'], true ) ) { 1140 1160 return false; 1141 1161 } … … 1155 1175 function bp_attachments_get_cover_image_dimensions( $component = 'members' ) { 1156 1176 // Let's prevent notices when setting the warning strings. 1157 $default = array( 'width' => 0, 'height' => 0 ); 1177 $default = array( 1178 'width' => 0, 1179 'height' => 0, 1180 ); 1158 1181 1159 1182 $settings = bp_attachments_get_cover_image_settings( $component ); … … 1201 1224 } 1202 1225 1203 if ( ( bp_is_group_admin_page() && 'group-cover-image' == bp_get_group_current_admin_tab() )1226 if ( ( bp_is_group_admin_page() && 'group-cover-image' === bp_get_group_current_admin_tab() ) 1204 1227 || ( bp_is_group_create() && bp_is_group_creation_step( 'group-cover-image' ) ) ) { 1205 1228 $retval = ! bp_disable_group_cover_image_uploads(); … … 1222 1245 } 1223 1246 1224 $cover_src = bp_attachments_get_attachment( 'url', array( 1225 'item_id' => $user_id, 1226 ) ); 1247 $cover_src = bp_attachments_get_attachment( 1248 'url', 1249 array( 1250 'item_id' => $user_id, 1251 ) 1252 ); 1227 1253 1228 1254 return (bool) apply_filters( 'bp_attachments_get_user_has_cover_image', $cover_src, $user_id ); … … 1243 1269 } 1244 1270 1245 $cover_src = bp_attachments_get_attachment( 'url', array( 1246 'object_dir' => 'groups', 1247 'item_id' => $group_id, 1248 ) ); 1271 $cover_src = bp_attachments_get_attachment( 1272 'url', 1273 array( 1274 'object_dir' => 'groups', 1275 'item_id' => $group_id, 1276 ) 1277 ); 1249 1278 1250 1279 return (bool) apply_filters( 'bp_attachments_get_group_has_cover_image', $cover_src, $group_id ); … … 1330 1359 'cover_file' => $cover_file, 1331 1360 'cover_basename' => $cover_basename, 1332 'is_too_small' => $is_too_small 1361 'is_too_small' => $is_too_small, 1333 1362 ); 1334 1363 } … … 1338 1367 * 1339 1368 * @since 2.4.0 1340 *1341 * @return string|null A json object containing success data if the upload succeeded,1342 * error message otherwise.1343 1369 */ 1344 1370 function bp_attachments_cover_image_ajax_upload() { 1371 1345 1372 if ( ! bp_is_post_request() ) { 1346 1373 wp_die(); … … 1350 1377 1351 1378 // Sending the json response will be different if the current Plupload runtime is html4. 1352 $is_html4 = ! empty( $_POST['html4' 1379 $is_html4 = ! empty( $_POST['html4'] ); 1353 1380 1354 1381 if ( empty( $_POST['bp_params'] ) ) { … … 1383 1410 // Member's cover image. 1384 1411 if ( 'user' === $bp_params['object'] ) { 1385 $object_data = array( 'dir' => 'members', 'component' => 'members' ); 1412 $object_data = array( 1413 'dir' => 'members', 1414 'component' => 'members', 1415 ); 1386 1416 1387 1417 if ( ! bp_displayed_user_id() && ! empty( $bp_params['item_id'] ) ) { 1388 $needs_reset = array( 'key' => 'displayed_user', 'value' => $bp->displayed_user ); 1418 $needs_reset = array( 1419 'key' => 'displayed_user', 1420 'value' => $bp->displayed_user, 1421 ); 1389 1422 $bp->displayed_user->id = $bp_params['item_id']; 1390 1423 } 1391 1424 1392 // Group's cover image.1425 // Group's cover image. 1393 1426 } elseif ( 'group' === $bp_params['object'] ) { 1394 $object_data = array( 'dir' => 'groups', 'component' => 'groups' ); 1427 $object_data = array( 1428 'dir' => 'groups', 1429 'component' => 'groups', 1430 ); 1395 1431 1396 1432 if ( ! bp_get_current_group_id() && ! empty( $bp_params['item_id'] ) ) { 1397 $needs_reset = array( 'component' => 'groups', 'key' => 'current_group', 'value' => $bp->groups->current_group ); 1433 $needs_reset = array( 1434 'component' => 'groups', 1435 'key' => 'current_group', 1436 'value' => $bp->groups->current_group, 1437 ); 1398 1438 $bp->groups->current_group = groups_get_group( $bp_params['item_id'] ); 1399 1439 } 1400 1440 1401 // Other object's cover image.1441 // Other object's cover image. 1402 1442 } else { 1403 1443 $object_data = apply_filters( 'bp_attachments_cover_image_object_dir', array(), $bp_params['object'] ); … … 1427 1467 1428 1468 $cover_image_attachment = new BP_Attachment_Cover_Image(); 1429 $uploaded = $cover_image_attachment->upload( $_FILES );1469 $uploaded = $cover_image_attachment->upload( $_FILES ); 1430 1470 1431 1471 // Reset objects. … … 1440 1480 if ( ! empty( $uploaded['error'] ) ) { 1441 1481 // Upload error response. 1442 bp_attachments_json_response( false, $is_html4, array( 1443 'type' => 'upload_error', 1444 'message' => sprintf( 1445 /* translators: %s: the upload error message */ 1446 __( 'Upload Failed! Error was: %s', 'buddypress' ), 1447 $uploaded['error'] 1448 ), 1449 ) ); 1482 bp_attachments_json_response( 1483 false, 1484 $is_html4, 1485 array( 1486 'type' => 'upload_error', 1487 'message' => sprintf( 1488 /* translators: %s: the upload error message */ 1489 __( 'Upload Failed! Error was: %s', 'buddypress' ), 1490 $uploaded['error'] 1491 ), 1492 ) 1493 ); 1450 1494 } 1451 1495 … … 1456 1500 // The BP Attachments Uploads Dir is not set, stop. 1457 1501 if ( ! $bp_attachments_uploads_dir ) { 1458 bp_attachments_json_response( false, $is_html4, array( 1459 'type' => 'upload_error', 1460 'message' => $error_message, 1461 ) ); 1502 bp_attachments_json_response( 1503 false, 1504 $is_html4, 1505 array( 1506 'type' => 'upload_error', 1507 'message' => $error_message, 1508 ) 1509 ); 1462 1510 } 1463 1511 … … 1467 1515 if ( 1 === validate_file( $cover_dir ) || ! is_dir( $cover_dir ) ) { 1468 1516 // Upload error response. 1469 bp_attachments_json_response( false, $is_html4, array( 1470 'type' => 'upload_error', 1471 'message' => $error_message, 1472 ) ); 1517 bp_attachments_json_response( 1518 false, 1519 $is_html4, 1520 array( 1521 'type' => 'upload_error', 1522 'message' => $error_message, 1523 ) 1524 ); 1473 1525 } 1474 1526 … … 1480 1532 * class, let's use it. 1481 1533 */ 1482 $cover = bp_attachments_cover_image_generate_file( array( 1483 'file' => $uploaded['file'], 1484 'component' => $object_data['component'], 1485 'cover_image_dir' => $cover_dir 1486 ), $cover_image_attachment ); 1534 $cover = bp_attachments_cover_image_generate_file( 1535 array( 1536 'file' => $uploaded['file'], 1537 'component' => $object_data['component'], 1538 'cover_image_dir' => $cover_dir, 1539 ), 1540 $cover_image_attachment 1541 ); 1487 1542 1488 1543 if ( ! $cover ) { 1489 bp_attachments_json_response( false, $is_html4, array( 1490 'type' => 'upload_error', 1491 'message' => $error_message, 1492 ) ); 1544 bp_attachments_json_response( 1545 false, 1546 $is_html4, 1547 array( 1548 'type' => 'upload_error', 1549 'message' => $error_message, 1550 ) 1551 ); 1493 1552 } 1494 1553 … … 1553 1612 1554 1613 // Finally return the cover image url to the UI. 1555 bp_attachments_json_response( true, $is_html4, array( 1556 'name' => $name, 1557 'url' => $cover_url, 1558 'feedback_code' => $feedback_code, 1559 ) ); 1614 bp_attachments_json_response( 1615 true, 1616 $is_html4, 1617 array( 1618 'name' => $name, 1619 'url' => $cover_url, 1620 'feedback_code' => $feedback_code, 1621 ) 1622 ); 1560 1623 } 1561 1624 add_action( 'wp_ajax_bp_cover_image_upload', 'bp_attachments_cover_image_ajax_upload' ); … … 1565 1628 * 1566 1629 * @since 2.4.0 1567 *1568 * @return string|null A json object containing success data if the cover image was deleted1569 * error message otherwise.1570 1630 */ 1571 1631 function bp_attachments_cover_image_ajax_delete() { 1632 1572 1633 if ( ! bp_is_post_request() ) { 1573 1634 wp_send_json_error(); … … 1594 1655 $dir = 'members'; 1595 1656 1596 // Set it for any other cases.1657 // Set it for any other cases. 1597 1658 } else { 1598 1659 $component = $args['object'] . 's'; … … 1601 1662 1602 1663 // Handle delete. 1603 if ( bp_attachments_delete_file( array( 'item_id' => $args['item_id'], 'object_dir' => $dir, 'type' => 'cover-image' ) ) ) { 1664 if ( bp_attachments_delete_file( 1665 array( 1666 'item_id' => $args['item_id'], 1667 'object_dir' => $dir, 1668 'type' => 'cover-image', 1669 ) 1670 ) ) { 1604 1671 $item_id = (int) $args['item_id']; 1605 1672 … … 1640 1707 1641 1708 } else { 1642 wp_send_json_error( array( 1643 'feedback_code' => 2, 1644 ) ); 1709 wp_send_json_error( 1710 array( 1711 'feedback_code' => 2, 1712 ) 1713 ); 1645 1714 } 1646 1715 } … … 1729 1798 $supported_file = bp_attachments_get_file_object( $file ); 1730 1799 1731 if ( is_null( $supported_file ) ) {1800 if ( is_null( $supported_file ) ) { 1732 1801 continue; 1733 1802 } … … 1762 1831 $supported_file = bp_attachments_get_file_object( $file ); 1763 1832 1764 if ( is_null( $supported_file ) ) {1833 if ( is_null( $supported_file ) ) { 1765 1834 continue; 1766 1835 }
Note: See TracChangeset
for help on using the changeset viewer.