Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
12/11/2021 09:40:29 AM (5 years ago)
Author:
imath
Message:

Introduce two new functions to list files into the BP Attachments API

  • bp_attachments_list_directory_files() returns a list of file objects contained into the directory thanks to its absolute path passed as the function argument.
  • bp_attachments_list_directory_files_recursively() returns a list of file objects contained into the directory and its subdirectories thanks to the directory absolute path passed as the function argument.

Props vapvarun, oztaser

See #8581

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-attachments.php

    r13134 r13174  
    15981598}
    15991599add_action( 'wp_ajax_bp_cover_image_delete', 'bp_attachments_cover_image_ajax_delete' );
     1600
     1601/**
     1602 * List the files of a directory.
     1603 *
     1604 * @since 10.0.0
     1605 *
     1606 * @param string $directory_path Absolute path of a directory.
     1607 * @return array                 The file objects list of the directory.
     1608 */
     1609function bp_attachments_list_directory_files( $directory_path = '' ) {
     1610        if ( ! is_dir( $directory_path ) ) {
     1611                return array();
     1612        }
     1613
     1614        $files    = array();
     1615        $iterator = new FilesystemIterator( $directory_path, FilesystemIterator::SKIP_DOTS );
     1616
     1617        foreach ( $iterator as $file ) {
     1618                $_file = new stdClass();
     1619
     1620                $_file->name               = $file->getfilename();
     1621                $_file->path               = $file->getPathname();
     1622                $_file->size               = $file->getSize();
     1623                $_file->type               = $file->getType();
     1624                $_file->mime_type          = mime_content_type( $_file->path );
     1625                $_file->last_modified      = $file->getMTime();
     1626                $_file->latest_access_date = $file->getATime();
     1627                $_file->id                 = pathinfo( $_file->name, PATHINFO_FILENAME );
     1628                $files[ $_file->id ]       = $_file;
     1629        }
     1630
     1631        return $files;
     1632}
     1633
     1634/**
     1635 * List the files of a directory recursively and eventually find a file using its ID.
     1636 *
     1637 * @since 10.0.0
     1638 *
     1639 * @param string $directory_path Absolute path of a directory.
     1640 * @param string $find           The file ID to find into the directory or its children.
     1641 * @return array                 The file objects list of the directory and subdirectories.
     1642 */
     1643function bp_attachments_list_directory_files_recursively( $directory_path = '', $find = '' ) {
     1644        if ( ! is_dir( $directory_path ) ) {
     1645                return array();
     1646        }
     1647
     1648        $files     = array();
     1649        $directory = new RecursiveDirectoryIterator( $directory_path, FilesystemIterator::SKIP_DOTS );
     1650        $iterator  = new RecursiveIteratorIterator( $directory, RecursiveIteratorIterator::CHILD_FIRST );
     1651        $bp_upload = bp_upload_dir();
     1652        $basedir   = str_replace( '\\', '/', $bp_upload['basedir'] );
     1653
     1654        foreach ( $iterator as $file ) {
     1655                $_file = new stdClass();
     1656
     1657                $_file->name               = $file->getfilename();
     1658                $_file->path               = $file->getPathname();
     1659                $_file->size               = $file->getSize();
     1660                $_file->type               = $file->getType();
     1661                $_file->mime_type          = mime_content_type( $_file->path );
     1662                $_file->last_modified      = $file->getMTime();
     1663                $_file->latest_access_date = $file->getATime();
     1664                $_file->parent_dir_path    = str_replace( '\\', '/', dirname( $_file->path ) );
     1665                $_file->parent_dir_url     = str_replace( $basedir, $bp_upload['baseurl'], $_file->parent_dir_path );
     1666                $_file->id                 = pathinfo( $_file->name, PATHINFO_FILENAME );
     1667
     1668                // Ensure URL is https if SSL is set/forced.
     1669                if ( is_ssl() ) {
     1670                        $_file->parent_dir_url = str_replace( 'http://', 'https://', $_file->parent_dir_url );
     1671                }
     1672
     1673                $file_id = $_file->id;
     1674                if ( $_file->parent_dir_path !== $directory_path ) {
     1675                        $file_id = trailingslashit( str_replace( trailingslashit( $directory_path ), '', $_file->parent_dir_path ) ) . $file_id;
     1676                }
     1677
     1678                $files[ $file_id ] = $_file;
     1679        }
     1680
     1681        if ( $find ) {
     1682                return wp_filter_object_list( $files, array( 'id' => $find ) );
     1683        }
     1684
     1685        return $files;
     1686}
Note: See TracChangeset for help on using the changeset viewer.