Changeset 13263
- Timestamp:
- 03/15/2022 11:21:26 PM (3 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-attachments.php
r13215 r13263 1640 1640 1641 1641 /** 1642 * Returns a file's mime type. 1643 * 1644 * @since 10.2.0 1645 * 1646 * @param string $file Absolute path of a file or directory. 1647 * @return false|string False if the mime type is not supported by WordPress. 1648 * The mime type of a file or 'directory' for a directory. 1649 */ 1650 function bp_attachements_get_mime_type( $file = '' ) { 1651 $file_type = wp_check_filetype( $file, wp_get_mime_types() ); 1652 $file_mime = $file_type['type']; 1653 1654 if ( false === $file_mime && is_dir( $file ) ) { 1655 $file_mime = 'directory'; 1656 } 1657 1658 return $file_mime; 1659 } 1660 1661 /** 1662 * Returns a BP Attachments file object. 1663 * 1664 * @since 10.2.0 1665 * 1666 * @param SplFileInfo $file The SplFileInfo file object. 1667 * @return null|object Null if the file is not supported by WordPress. 1668 * A BP Attachments file object otherwise. 1669 */ 1670 function bp_attachments_get_file_object( SplFileInfo $file ) { 1671 $path = $file->getPathname(); 1672 $mime_type = bp_attachements_get_mime_type( $path ); 1673 1674 // Mime type not supported by WordPress. 1675 if ( false === $mime_type ) { 1676 return null; 1677 } 1678 1679 $_file = new stdClass(); 1680 1681 $_file->name = $file->getfilename(); 1682 $_file->path = $path; 1683 $_file->size = $file->getSize(); 1684 $_file->type = $file->getType(); 1685 $_file->mime_type = bp_attachements_get_mime_type( $_file->path ); 1686 $_file->last_modified = $file->getMTime(); 1687 $_file->latest_access_date = $file->getATime(); 1688 $_file->id = pathinfo( $_file->name, PATHINFO_FILENAME ); 1689 1690 return $_file; 1691 } 1692 1693 /** 1642 1694 * List the files of a directory. 1643 1695 * … … 1656 1708 1657 1709 foreach ( $iterator as $file ) { 1658 $_file = new stdClass(); 1659 1660 $_file->name = $file->getfilename(); 1661 $_file->path = $file->getPathname(); 1662 $_file->size = $file->getSize(); 1663 $_file->type = $file->getType(); 1664 $_file->mime_type = mime_content_type( $_file->path ); 1665 $_file->last_modified = $file->getMTime(); 1666 $_file->latest_access_date = $file->getATime(); 1667 $_file->id = pathinfo( $_file->name, PATHINFO_FILENAME ); 1668 $files[ $_file->id ] = $_file; 1710 $supported_file = bp_attachments_get_file_object( $file ); 1711 1712 if ( is_null( $supported_file) ) { 1713 continue; 1714 } 1715 1716 $files[ $supported_file->id ] = $supported_file; 1669 1717 } 1670 1718 … … 1693 1741 1694 1742 foreach ( $iterator as $file ) { 1695 $_file = new stdClass(); 1696 1697 $_file->name = $file->getfilename(); 1698 $_file->path = $file->getPathname(); 1699 $_file->size = $file->getSize(); 1700 $_file->type = $file->getType(); 1701 $_file->mime_type = mime_content_type( $_file->path ); 1702 $_file->last_modified = $file->getMTime(); 1703 $_file->latest_access_date = $file->getATime(); 1704 $_file->parent_dir_path = str_replace( '\\', '/', dirname( $_file->path ) ); 1705 $_file->parent_dir_url = str_replace( $basedir, $bp_upload['baseurl'], $_file->parent_dir_path ); 1706 $_file->id = pathinfo( $_file->name, PATHINFO_FILENAME ); 1743 $supported_file = bp_attachments_get_file_object( $file ); 1744 1745 if ( is_null( $supported_file) ) { 1746 continue; 1747 } 1748 1749 $supported_file->parent_dir_path = str_replace( '\\', '/', dirname( $supported_file->path ) ); 1750 $supported_file->parent_dir_url = str_replace( $basedir, $bp_upload['baseurl'], $supported_file->parent_dir_path ); 1707 1751 1708 1752 // Ensure URL is https if SSL is set/forced. 1709 1753 if ( is_ssl() ) { 1710 $ _file->parent_dir_url = str_replace( 'http://', 'https://', $_file->parent_dir_url );1711 } 1712 1713 $file_id = $ _file->id;1714 if ( $ _file->parent_dir_path !== $directory_path ) {1715 $file_id = trailingslashit( str_replace( trailingslashit( $directory_path ), '', $ _file->parent_dir_path ) ) . $file_id;1716 } 1717 1718 $files[ $file_id ] = $ _file;1754 $supported_file->parent_dir_url = str_replace( 'http://', 'https://', $supported_file->parent_dir_url ); 1755 } 1756 1757 $file_id = $supported_file->id; 1758 if ( $supported_file->parent_dir_path !== $directory_path ) { 1759 $file_id = trailingslashit( str_replace( trailingslashit( $directory_path ), '', $supported_file->parent_dir_path ) ) . $file_id; 1760 } 1761 1762 $files[ $file_id ] = $supported_file; 1719 1763 } 1720 1764 -
trunk/tests/phpunit/testcases/core/attachments.php
r13174 r13263 7 7 class BP_Tests_Core_Attachments extends BP_UnitTestCase { 8 8 /** 9 * @group bp_attachments_list_directory_files 10 */ 11 public function test_bp_attachments_list_directory_files() { 12 $files = bp_attachments_list_directory_files( BP_TESTS_DIR . 'assets' ); 13 14 $expected_keys = array( 'name', 'path', 'size', 'type', 'mime_type', 'last_modified', 'latest_access_date', 'id' ); 15 16 $directories = wp_filter_object_list( $files, array( 'mime_type' => 'directory' ) ); 17 $directory = reset( $directories ); 18 19 $keys = array_keys( get_object_vars( $directory ) ); 20 sort( $keys ); 21 sort( $expected_keys ); 22 23 $this->assertSame( $keys, $expected_keys ); 24 25 $images = wp_filter_object_list( $files, array( 'mime_type' => 'image/jpeg' ) ); 26 $this->assertNotEmpty( $images ); 27 28 $scripts = wp_filter_object_list( $files, array( 'mime_type' => 'text/x-php' ) ); 29 $this->assertEmpty( $scripts ); 30 } 31 32 /** 9 33 * @group bp_attachments_list_directory_files_recursively 10 34 */ 11 35 public function test_bp_attachments_list_directory_files_recursively() { 36 add_filter( 'mime_types', array( $this, 'filter_mime_types' ) ); 12 37 $files = bp_attachments_list_directory_files_recursively( BP_TESTS_DIR . 'assets', 'index' ); 38 remove_filter( 'mime_types', array( $this, 'filter_mime_types' ) ); 13 39 14 40 $this->assertTrue( 1 === count( $files ) ); 15 41 $this->assertTrue( isset( $files['templates/index'] ) ); 16 42 } 43 44 public function filter_mime_types( $mime_types ) { 45 $mime_types['php'] = 'text/x-php'; 46 return $mime_types; 47 } 17 48 }
Note: See TracChangeset
for help on using the changeset viewer.