Changeset 10356 for trunk/src/bp-core/bp-core-avatars.php
- Timestamp:
- 11/15/2015 07:57:03 PM (10 years ago)
- File:
-
- 1 edited
-
trunk/src/bp-core/bp-core-avatars.php (modified) (60 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-avatars.php
r10287 r10356 54 54 $bp->avatar->full = new stdClass; 55 55 56 // Dimensions 56 // Dimensions. 57 57 $bp->avatar->thumb->width = BP_AVATAR_THUMB_WIDTH; 58 58 $bp->avatar->thumb->height = BP_AVATAR_THUMB_HEIGHT; … … 60 60 $bp->avatar->full->height = BP_AVATAR_FULL_HEIGHT; 61 61 62 // Upload maximums 62 // Upload maximums. 63 63 $bp->avatar->original_max_width = BP_AVATAR_ORIGINAL_MAX_WIDTH; 64 64 $bp->avatar->original_max_filesize = BP_AVATAR_ORIGINAL_MAX_FILESIZE; 65 65 66 // Defaults 66 // Defaults. 67 67 $bp->avatar->thumb->default = bp_core_avatar_default_thumb(); 68 68 $bp->avatar->full->default = bp_core_avatar_default(); 69 69 70 // These have to be set on page load in order to avoid infinite filter loops at runtime 70 // These have to be set on page load in order to avoid infinite filter loops at runtime. 71 71 $bp->avatar->upload_path = bp_core_avatar_upload_path(); 72 72 $bp->avatar->url = bp_core_avatar_url(); 73 73 74 74 // Cache the root blog's show_avatars setting, to avoid unnecessary 75 // calls to switch_to_blog() 75 // calls to switch_to_blog(). 76 76 $bp->avatar->show_avatars = (bool) BP_SHOW_AVATARS; 77 77 78 // Backpat for pre-1.5 78 // Backpat for pre-1.5. 79 79 if ( ! defined( 'BP_AVATAR_UPLOAD_PATH' ) ) 80 80 define( 'BP_AVATAR_UPLOAD_PATH', $bp->avatar->upload_path ); 81 81 82 // Backpat for pre-1.5 82 // Backpat for pre-1.5. 83 83 if ( ! defined( 'BP_AVATAR_URL' ) ) 84 84 define( 'BP_AVATAR_URL', $bp->avatar->url ); … … 184 184 * image regardless if the Gravatar exists. Default: false. 185 185 * } 186 *187 186 * @return string Formatted HTML <img> element, or raw avatar URL based on $html arg. 188 187 */ … … 190 189 $bp = buddypress(); 191 190 192 // If avatars are disabled for the root site, obey that request and bail 191 // If avatars are disabled for the root site, obey that request and bail. 193 192 if ( ! $bp->avatar->show_avatars ) { 194 193 return; … … 218 217 ) ); 219 218 220 /* *Set item_id ***********************************************************/219 /* Set item_id ***********************************************************/ 221 220 222 221 if ( empty( $params['item_id'] ) ) { … … 259 258 } 260 259 261 /* *Set avatar_dir ********************************************************/260 /* Set avatar_dir ********************************************************/ 262 261 263 262 if ( empty( $params['avatar_dir'] ) ) { … … 300 299 } 301 300 302 /* *<img> alt *************************************************************/301 /* <img> alt *************************************************************/ 303 302 304 303 if ( false !== strpos( $params['alt'], '%s' ) || false !== strpos( $params['alt'], '%1$s' ) ) { … … 334 333 } 335 334 336 /* *Sanity Checks *********************************************************/335 /* Sanity Checks *********************************************************/ 337 336 338 337 // Get a fallback for the 'alt' parameter, create html output. … … 361 360 } 362 361 363 // Extra attributes 362 // Extra attributes. 364 363 $extra_attr = ! empty( $args['extra_attr'] ) ? ' ' . $args['extra_attr'] : ''; 365 364 … … 383 382 } 384 383 385 // Set image width 384 // Set image width. 386 385 if ( false !== $params['width'] ) { 387 386 // Width has been specified. No modification necessary. … … 393 392 $html_width = ' width="' . $params['width'] . '"'; 394 393 395 // Set image height 394 // Set image height. 396 395 if ( false !== $params['height'] ) { 397 396 // Height has been specified. No modification necessary. … … 415 414 $params['class'] = apply_filters( 'bp_core_avatar_class', $params['class'], $params['item_id'], $params['object'], $params ); 416 415 417 // Use an alias to leave the param unchanged 416 // Use an alias to leave the param unchanged. 418 417 $avatar_classes = $params['class']; 419 418 if ( ! is_array( $avatar_classes ) ) { … … 421 420 } 422 421 423 // merge classes422 // Merge classes. 424 423 $avatar_classes = array_merge( $avatar_classes, array( 425 424 $params['object'] . '-' . $params['item_id'] . '-avatar', … … 427 426 ) ); 428 427 429 // Sanitize each class 428 // Sanitize each class. 430 429 $avatar_classes = array_map( 'sanitize_html_class', $avatar_classes ); 431 430 432 // populate the class attribute431 // Populate the class attribute. 433 432 $html_class = ' class="' . join( ' ', $avatar_classes ) . ' photo"'; 434 433 435 // Set img URL and DIR based on prepopulated constants 434 // Set img URL and DIR based on prepopulated constants. 436 435 $avatar_loc = new stdClass(); 437 436 $avatar_loc->path = trailingslashit( bp_core_avatar_upload_path() ); … … 473 472 $legacy_group_avatar_name = ( 'full' == $params['type'] ) ? '-groupavatar-full' : '-groupavatar-thumb'; 474 473 475 // Check for directory 474 // Check for directory. 476 475 if ( file_exists( $avatar_folder_dir ) ) { 477 476 478 // Open directory 477 // Open directory. 479 478 if ( $av_dir = opendir( $avatar_folder_dir ) ) { 480 479 481 // Stash files in an array once to check for one that matches 480 // Stash files in an array once to check for one that matches. 482 481 $avatar_files = array(); 483 482 while ( false !== ( $avatar_file = readdir( $av_dir ) ) ) { 484 // Only add files to the array (skip directories) 483 // Only add files to the array (skip directories). 485 484 if ( 2 < strlen( $avatar_file ) ) { 486 485 $avatar_files[] = $avatar_file; … … 488 487 } 489 488 490 // Check for array 489 // Check for array. 491 490 if ( 0 < count( $avatar_files ) ) { 492 491 493 // Check for current avatar 492 // Check for current avatar. 494 493 foreach( $avatar_files as $key => $value ) { 495 494 if ( strpos ( $value, $avatar_size )!== false ) { … … 498 497 } 499 498 500 // Legacy avatar check 499 // Legacy avatar check. 501 500 if ( !isset( $avatar_url ) ) { 502 501 foreach( $avatar_files as $key => $value ) { … … 506 505 } 507 506 508 // Legacy group avatar check 507 // Legacy group avatar check. 509 508 if ( !isset( $avatar_url ) ) { 510 509 foreach( $avatar_files as $key => $value ) { … … 518 517 } 519 518 520 // Close the avatar directory 519 // Close the avatar directory. 521 520 closedir( $av_dir ); 522 521 523 // If we found a locally uploaded avatar 522 // If we found a locally uploaded avatar. 524 523 if ( isset( $avatar_url ) ) { 525 // Support custom scheme 524 // Support custom scheme. 526 525 $avatar_url = set_url_scheme( $avatar_url, $params['scheme'] ); 527 526 528 // Return it wrapped in an <img> element 527 // Return it wrapped in an <img> element. 529 528 if ( true === $params['html'] ) { 530 529 … … 572 571 if ( ! apply_filters( 'bp_core_fetch_avatar_no_grav', $params['no_grav'], $params ) ) { 573 572 574 // Set gravatar type 573 // Set gravatar type. 575 574 if ( empty( $bp->grav_default->{$params['object']} ) ) { 576 575 $default_grav = 'wavatar'; … … 590 589 } 591 590 592 // Set gravatar object 591 // Set gravatar object. 593 592 if ( empty( $params['email'] ) ) { 594 593 if ( 'user' == $params['object'] ) { … … 619 618 $gravatar = apply_filters( 'bp_gravatar_url', '//www.gravatar.com/avatar/' ); 620 619 621 // Append email hash to Gravatar 620 // Append email hash to Gravatar. 622 621 $gravatar .= md5( strtolower( $params['email'] ) ); 623 622 624 // Main Gravatar URL args 623 // Main Gravatar URL args. 625 624 $url_args = array( 626 625 'd' => $default_grav, … … 628 627 ); 629 628 630 // Custom Gravatar URL args 629 // Custom Gravatar URL args. 631 630 if ( ! empty( $params['force_default'] ) ) { 632 631 $url_args['f'] = 'y'; … … 636 635 } 637 636 638 // Set up the Gravatar URL 637 // Set up the Gravatar URL. 639 638 $gravatar = esc_url( add_query_arg( 640 639 rawurlencode_deep( array_filter( $url_args ) ), … … 689 688 $defaults = array( 690 689 'item_id' => false, 691 'object' => 'user', // user OR group OR blog OR custom type (if you use filters)690 'object' => 'user', // User OR group OR blog OR custom type (if you use filters). 692 691 'avatar_dir' => false 693 692 ); … … 777 776 } 778 777 779 // Check the nonce 778 // Check the nonce. 780 779 check_admin_referer( $nonce, 'nonce' ); 781 780 782 // Capability check 781 // Capability check. 783 782 if ( ! bp_attachments_current_user_can( 'edit_avatar', $avatar_data ) ) { 784 783 wp_send_json_error(); 785 784 } 786 785 787 // Handle delete 786 // Handle delete. 788 787 if ( bp_core_delete_existing_avatar( array( 'item_id' => $avatar_data['item_id'], 'object' => $avatar_data['object'] ) ) ) { 789 788 $return = array( … … 821 820 * @param array $file The appropriate entry the from $_FILES superglobal. 822 821 * @param string $upload_dir_filter A filter to be applied to 'upload_dir'. 823 *824 822 * @return bool True on success, false on failure. 825 823 */ … … 855 853 } 856 854 857 // The Avatar UI available width 855 // The Avatar UI available width. 858 856 $ui_available_width = 0; 859 857 860 // Try to set the ui_available_width using the avatar_admin global 858 // Try to set the ui_available_width using the avatar_admin global. 861 859 if ( isset( $bp->avatar_admin->ui_available_width ) ) { 862 860 $ui_available_width = $bp->avatar_admin->ui_available_width; … … 903 901 */ 904 902 function bp_avatar_ajax_upload() { 905 // Bail if not a POST action 903 // Bail if not a POST action. 906 904 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { 907 905 wp_die(); … … 917 915 } 918 916 919 // Check the nonce 917 // Check the nonce. 920 918 check_admin_referer( 'bp-uploader' ); 921 919 922 // Init the BuddyPress parameters 920 // Init the BuddyPress parameters. 923 921 $bp_params = array(); 924 922 925 // We need it to carry on 923 // We need it to carry on. 926 924 if ( ! empty( $_POST['bp_params' ] ) ) { 927 925 $bp_params = $_POST['bp_params' ]; … … 930 928 } 931 929 932 // We need the object to set the uploads dir filter 930 // We need the object to set the uploads dir filter. 933 931 if ( empty( $bp_params['object'] ) ) { 934 932 bp_attachments_json_response( false, $is_html4 ); 935 933 } 936 934 937 // Capability check 935 // Capability check. 938 936 if ( ! bp_attachments_current_user_can( 'edit_avatar', $bp_params ) ) { 939 937 bp_attachments_json_response( false, $is_html4 ); … … 984 982 } 985 983 986 // Upload the avatar 984 // Upload the avatar. 987 985 $avatar = bp_core_avatar_handle_upload( $_FILES, $bp_params['upload_dir_filter'] ); 988 986 989 // Reset objects 987 // Reset objects. 990 988 if ( ! empty( $needs_reset ) ) { 991 989 if ( ! empty( $needs_reset['component'] ) ) { … … 996 994 } 997 995 998 // Init the feedback message 996 // Init the feedback message. 999 997 $feedback_message = false; 1000 998 … … 1010 1008 1011 1009 if ( empty( $avatar ) ) { 1012 // Default upload error 1010 // Default upload error. 1013 1011 $message = __( 'Upload failed.', 'buddypress' ); 1014 1012 1015 // Use the template message if set 1013 // Use the template message if set. 1016 1014 if ( ! empty( $feedback_message ) ) { 1017 1015 $message = $feedback_message; 1018 1016 } 1019 1017 1020 // Upload error reply 1018 // Upload error reply. 1021 1019 bp_attachments_json_response( false, $is_html4, array( 1022 1020 'type' => 'upload_error', … … 1031 1029 $uploaded_image = @getimagesize( $bp->avatar_admin->image->file ); 1032 1030 1033 // Set the name of the file 1031 // Set the name of the file. 1034 1032 $name = $_FILES['file']['name']; 1035 1033 $name_parts = pathinfo( $name ); 1036 1034 $name = trim( substr( $name, 0, - ( 1 + strlen( $name_parts['extension'] ) ) ) ); 1037 1035 1038 // Finally return the avatar to the editor 1036 // Finally return the avatar to the editor. 1039 1037 bp_attachments_json_response( true, $is_html4, array( 1040 1038 'name' => $name, … … 1054 1052 * @param string $data Base64 encoded image. 1055 1053 * @param int $item_id Item to associate. 1056 *1057 1054 * @return bool True on success, false on failure. 1058 1055 */ … … 1095 1092 $avatar_to_crop = str_replace( bp_core_avatar_upload_path(), '', $original_file ); 1096 1093 1097 // Crop to default values 1094 // Crop to default values. 1098 1095 $crop_args = array( 'item_id' => $item_id, 'original_file' => $avatar_to_crop, 'crop_x' => 0, 'crop_y' => 0 ); 1099 1096 … … 1134 1131 * @type int $crop_y The vertical starting point of the crop. Default: 0. 1135 1132 * } 1136 *1137 1133 * @return bool True on success, false on failure. 1138 1134 */ … … 1164 1160 } 1165 1161 1166 // Crop the file 1162 // Crop the file. 1167 1163 $avatar_attachment = new BP_Attachment_Avatar(); 1168 1164 $cropped = $avatar_attachment->crop( $r ); 1169 1165 1170 // Check for errors 1166 // Check for errors. 1171 1167 if ( empty( $cropped['full'] ) || empty( $cropped['thumb'] ) || is_wp_error( $cropped['full'] ) || is_wp_error( $cropped['thumb'] ) ) { 1172 1168 return false; … … 1185 1181 */ 1186 1182 function bp_avatar_ajax_set() { 1187 // Bail if not a POST action 1183 // Bail if not a POST action. 1188 1184 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { 1189 1185 wp_send_json_error(); 1190 1186 } 1191 1187 1192 // Check the nonce 1188 // Check the nonce. 1193 1189 check_admin_referer( 'bp_avatar_cropstore', 'nonce' ); 1194 1190 … … 1204 1200 } 1205 1201 1206 // Capability check 1202 // Capability check. 1207 1203 if ( ! bp_attachments_current_user_can( 'edit_avatar', $avatar_data ) ) { 1208 1204 wp_send_json_error(); … … 1255 1251 $original_file = str_replace( bp_core_avatar_url(), '', $avatar_data['original_file'] ); 1256 1252 1257 // Set avatars dir & feedback part 1253 // Set avatars dir & feedback part. 1258 1254 if ( 'user' === $avatar_data['object'] ) { 1259 1255 $avatar_dir = 'avatars'; 1260 1256 1261 // Defaults to object-avatars dir 1257 // Defaults to object-avatars dir. 1262 1258 } else { 1263 1259 $avatar_dir = sanitize_key( $avatar_data['object'] ) . '-avatars'; 1264 1260 } 1265 1261 1266 // Crop args 1262 // Crop args. 1267 1263 $r = array( 1268 1264 'item_id' => $avatar_data['item_id'], … … 1276 1272 ); 1277 1273 1278 // Handle crop 1274 // Handle crop. 1279 1275 if ( bp_core_avatar_handle_crop( $r ) ) { 1280 1276 $return = array( … … 1326 1322 * @param string $alt Alternate text to use in image tag. Default: ''. 1327 1323 * @param array $args Arguments passed to get_avatar_data(), after processing. 1328 *1329 1324 * @return string BP avatar path, if found; else the original avatar path. 1330 1325 */ … … 1332 1327 global $pagenow; 1333 1328 1334 // Do not filter if inside WordPress options page 1329 // Do not filter if inside WordPress options page. 1335 1330 if ( 'options-discussion.php' == $pagenow ) 1336 1331 return $avatar; 1337 1332 1338 // If passed an object, assume $user->user_id 1333 // If passed an object, assume $user->user_id. 1339 1334 if ( is_object( $user ) ) { 1340 1335 if ( isset( $user->user_id ) ) { … … 1344 1339 } 1345 1340 1346 // If passed a number, assume it was a $user_id 1341 // If passed a number, assume it was a $user_id. 1347 1342 } elseif ( is_numeric( $user ) ) { 1348 1343 $id = $user; 1349 1344 1350 // If passed a string and that string returns a user, get the $id 1345 // If passed a string and that string returns a user, get the $id. 1351 1346 } elseif ( is_string( $user ) && ( $user_by_email = get_user_by( 'email', $user ) ) ) { 1352 1347 $id = $user_by_email->ID; 1353 1348 } 1354 1349 1355 // If somehow $id hasn't been assigned, return the result of get_avatar 1350 // If somehow $id hasn't been assigned, return the result of get_avatar. 1356 1351 if ( empty( $id ) ) { 1357 1352 return !empty( $avatar ) ? $avatar : $default; 1358 1353 } 1359 1354 1360 // Image alt tag 1355 // Image alt tag. 1361 1356 if ( empty( $alt ) ) { 1362 1357 $alt = sprintf( __( 'Profile photo of %s', 'buddypress' ), bp_core_get_user_displayname( $id ) ); … … 1378 1373 ); 1379 1374 1380 // Support new arguments as of WordPress 4.2.0 1375 // Support new arguments as of WordPress 4.2.0. 1381 1376 if ( ! empty( $args['width'] ) ) { 1382 1377 $avatar_args['width'] = $args['width']; … … 1404 1399 } 1405 1400 1406 // Let BuddyPress handle the fetching of the avatar 1401 // Let BuddyPress handle the fetching of the avatar. 1407 1402 $bp_avatar = bp_core_fetch_avatar( $avatar_args ); 1408 1403 1409 // If BuddyPress found an avatar, use it. If not, use the result of get_avatar 1404 // If BuddyPress found an avatar, use it. If not, use the result of get_avatar. 1410 1405 return ( !$bp_avatar ) ? $avatar : $bp_avatar; 1411 1406 } … … 1416 1411 * 1417 1412 * @param array $file The $_FILES array. 1418 *1419 1413 * @return bool True if no errors are found. False if there are errors. 1420 1414 */ … … 1430 1424 * 1431 1425 * @param array $file The $_FILES array. 1432 *1433 1426 * @return bool True if the avatar is under the size limit, otherwise false. 1434 1427 */ … … 1483 1476 * 1484 1477 * @param array $file The $_FILES array. 1485 *1486 1478 * @return bool True if the file extension is permitted, otherwise false. 1487 1479 */ … … 1497 1489 * @param string $type The variable we want to return from the $bp->avatars object. 1498 1490 * Only 'upload_path' and 'url' are supported. Default: 'upload_path'. 1499 *1500 1491 * @return string The avatar upload directory path. 1501 1492 */ … … 1612 1603 * 1613 1604 * @param int $user_id ID of the user whose avatar is being checked. 1614 *1615 1605 * @return bool True if the user has uploaded a local avatar. Otherwise false. 1616 1606 */ … … 1644 1634 * @param string $h_or_w Which dimension is being fetched. 'height' or 'width'. 1645 1635 * Default: 'height'. 1646 *1647 1636 * @return int|bool $dim The dimension. 1648 1637 */ … … 1785 1774 * of the mystery-person, 'gravatar' if the fallback should be 1786 1775 * Gravatar's version. Default: 'gravatar'. 1787 *1788 1776 * @return string The URL of the default avatar. 1789 1777 */ 1790 1778 function bp_core_avatar_default( $type = 'gravatar' ) { 1791 // Local override 1779 // Local override. 1792 1780 if ( defined( 'BP_AVATAR_DEFAULT' ) ) { 1793 1781 $avatar = BP_AVATAR_DEFAULT; 1794 1782 1795 // Use the local default image 1783 // Use the local default image. 1796 1784 } elseif ( 'local' === $type ) { 1797 1785 $avatar = buddypress()->plugin_url . 'bp-core/images/mystery-man.jpg'; 1798 1786 1799 // Use Gravatar's mystery person as fallback 1787 // Use Gravatar's mystery person as fallback. 1800 1788 } else { 1801 1789 $avatar = '//www.gravatar.com/avatar/00000000000000000000000000000000?d=mm&s=' . bp_core_avatar_full_width(); … … 1823 1811 * of the mystery-person, 'gravatar' if the fallback should be 1824 1812 * Gravatar's version. Default: 'gravatar'. 1825 *1826 1813 * @return string The URL of the default avatar thumb. 1827 1814 */ 1828 1815 function bp_core_avatar_default_thumb( $type = 'gravatar' ) { 1829 // Local override 1816 // Local override. 1830 1817 if ( defined( 'BP_AVATAR_DEFAULT_THUMB' ) ) { 1831 1818 $avatar = BP_AVATAR_DEFAULT_THUMB; 1832 1819 1833 // Use the local default image 1820 // Use the local default image. 1834 1821 } elseif ( 'local' === $type ) { 1835 1822 $avatar = buddypress()->plugin_url . 'bp-core/images/mystery-man-50.jpg'; 1836 1823 1837 // Use Gravatar's mystery person as fallback 1824 // Use Gravatar's mystery person as fallback. 1838 1825 } else { 1839 1826 $avatar = '//www.gravatar.com/avatar/00000000000000000000000000000000?d=mm&s=' . bp_core_avatar_thumb_width(); … … 1870 1857 $reset_w = false; 1871 1858 1872 // Group's avatar edit screen 1859 // Group's avatar edit screen. 1873 1860 if ( bp_is_group_admin_page() ) { 1874 1861 $reset_w = bp_is_group_admin_screen( 'group-avatar' ); 1875 1862 1876 // Group's avatar create screen 1863 // Group's avatar create screen. 1877 1864 } elseif ( bp_is_group_create() ) { 1878 1865 /** 1879 * we can't use bp_get_groups_current_create_step()1866 * We can't use bp_get_groups_current_create_step(). 1880 1867 * as it's not set yet 1881 1868 */ 1882 1869 $reset_w = 'group-avatar' === bp_action_variable( 1 ); 1883 1870 1884 // User's change avatar screen 1871 // User's change avatar screen. 1885 1872 } else { 1886 1873 $reset_w = bp_is_user_change_avatar(); 1887 1874 } 1888 1875 1889 // A user or a group is cropping an avatar 1876 // A user or a group is cropping an avatar. 1890 1877 if ( true === $reset_w && isset( $_POST['avatar-crop-submit'] ) ) { 1891 1878 $posts_query->set( 'w', 0 ); … … 1914 1901 1915 1902 if ( bp_is_active( 'groups' ) ) { 1916 // Group creation 1903 // Group creation. 1917 1904 if ( bp_is_group_create() && bp_is_group_creation_step( 'group-avatar' ) && 'crop-image' !== bp_get_avatar_admin_step() ) { 1918 1905 $retval = ! bp_disable_group_avatar_uploads(); 1919 1906 1920 // Group Manage 1907 // Group Manage. 1921 1908 } elseif ( bp_is_group_admin_page() && bp_is_group_admin_screen( 'group-avatar' ) && 'crop-image' !== bp_get_avatar_admin_step() ) { 1922 1909 $retval = ! bp_disable_group_avatar_uploads(); … … 1931 1918 * @since 2.3.0 1932 1919 * 1933 * @param bool whetherto load the Avatar UI.1920 * @param bool $retval Whether or not to load the Avatar UI. 1934 1921 */ 1935 1922 return apply_filters( 'bp_avatar_is_front_edit', $retval ); … … 1971 1958 * @since 2.3.0 1972 1959 * 1973 * @param bool whetherto load Webcam Avatar UI part.1960 * @param bool $value Whether or not to load Webcam Avatar UI part. 1974 1961 */ 1975 1962 return apply_filters( 'bp_avatar_use_webcam', true );
Note: See TracChangeset
for help on using the changeset viewer.