Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/15/2022 11:21:26 PM (3 years ago)
Author:
imath
Message:

Attachments: introduce a new function to retrieve a media mime type

bp_attachements_get_mime_type() returns a media mime type if it is supported by WordPress, false if not. This function was mainly introduced to replace the mime_content_type() PHP function which appeared to be problematic for some end users (Some hosts are disabling it, causing an error during the Avatar crop step).

This commit also introduces bp_attachments_get_file_object(), a function to avoid code duplication about getting a BP Attachment object.

Props Nihanthk, maleemuse

See #8646 (trunk)

File:
1 edited

Legend:

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

    r13215 r13263  
    16401640
    16411641/**
     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 */
     1650function 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 */
     1670function 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/**
    16421694 * List the files of a directory.
    16431695 *
     
    16561708
    16571709    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;
    16691717    }
    16701718
     
    16931741
    16941742    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 );
    17071751
    17081752        // Ensure URL is https if SSL is set/forced.
    17091753        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;
    17191763    }
    17201764
Note: See TracChangeset for help on using the changeset viewer.