Make WordPress Core


Ignore:
Timestamp:
02/19/2008 06:13:20 AM (19 years ago)
Author:
ryan
Message:

Media Library design updates from Andy. see #5911

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r6808 r6910  
    808808
    809809/**
     810 * wp_count_attachments() - Count number of attachments
     811 *
     812 * {@internal Missing Long Description}}
     813 *
     814 * @package WordPress
     815 * @subpackage Post
     816 * @since 2.5
     817 *
     818 * @param string|array $post_mime_type Array or comma-separated list of MIME patterns
     819 * @return array Number of posts for each post_mime_type
     820 */
     821
     822function wp_count_attachments( $mime_type = '' ) {
     823        global $wpdb;
     824
     825        $and = wp_post_mime_type_where( $mime_type );
     826        $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' $and GROUP BY post_mime_type", ARRAY_A );
     827
     828        $stats = array( );
     829        foreach( (array) $count as $row ) {
     830                $stats[$row['post_mime_type']] = $row['num_posts'];
     831        }
     832
     833        return (object) $stats;
     834}
     835
     836/**
     837 * wp_match_mime_type() - Check a MIME-Type against a list
     838 *
     839 * {@internal Missing Long Description}}
     840 *
     841 * @package WordPress
     842 * @subpackage Post
     843 * @since 2.5
     844 *
     845 * @param string|array $wildcard_mime_types e.g. audio/mpeg or image (same as image/*) or flash (same as *flash*)
     846 * @param string|array $real_mime_types post_mime_type values
     847 * @return array array(wildcard=>array(real types))
     848 */
     849function wp_match_mime_types($wildcard_mime_types, $real_mime_types) {
     850        $matches = array();
     851        if ( is_string($wildcard_mime_types) )
     852                $wildcard_mime_types = array_map('trim', explode(',', $wildcard_mime_types));
     853        if ( is_string($real_mime_types) )
     854                $real_mime_types = array_map('trim', explode(',', $real_mime_types));
     855        $wild = '[-._a-z0-9]*';
     856        foreach ( (array) $wildcard_mime_types as $type ) {
     857                $type = str_replace('*', $wild, $type);
     858                $patternses[1][$type] = $type;
     859                if ( false === strpos($type, '/') ) {
     860                        $patternses[2][$type] = "^$type/$wild$";
     861                        $patternses[3][$type] = "$wild$type$wild";
     862                }
     863        }
     864        asort($patternses);
     865        foreach ( $patternses as $patterns )
     866                foreach ( $patterns as $type => $pattern )
     867                        foreach ( (array) $real_mime_types as $real )
     868                                if ( preg_match("#$pattern#", $real) && ( empty($matches[$type]) || false === array_search($real, $matches[$type]) ) )
     869                                        $matches[$type][] = $real;
     870        return $matches;
     871}
     872
     873/**
     874 * wp_get_post_mime_type_where() - Convert MIME types into SQL
     875 *
     876 * @package WordPress
     877 * @subpackage Post
     878 * @since 2.5
     879 *
     880 * @param string|array $mime_types MIME types
     881 * @return string SQL AND clause
     882 */
     883function wp_post_mime_type_where($post_mime_types) {
     884        $where = '';
     885        $wildcards = array('', '%', '%/%');
     886        if ( is_string($post_mime_types) )
     887                $post_mime_types = array_map('trim', explode(',', $post_mime_types));
     888        foreach ( (array) $post_mime_types as $mime_type ) {
     889                $mime_type = preg_replace('/\s/', '', $mime_type);
     890                $slashpos = strpos($mime_type, '/');
     891                if ( false !== $slashpos ) {
     892                        $mime_group = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, 0, $slashpos));
     893                        $mime_subgroup = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, $slashpos + 1));
     894                        if ( empty($mime_subgroup) )
     895                                $mime_subgroup = '*';
     896                        else
     897                                $mime_subgroup = str_replace('/', '', $mime_subgroup);
     898                        $mime_pattern = "$mime_group/$mime_subgroup";
     899                } else {
     900                        $mime_pattern = preg_replace('/[^-*.a-zA-Z0-9]/', '', $mime_type);
     901                        if ( false === strpos($mime_pattern, '*') )
     902                                $mime_pattern .= '/*';
     903                }
     904
     905                $mime_pattern = preg_replace('/\*+/', '%', $mime_pattern);
     906
     907                if ( in_array( $mime_type, $wildcards ) )
     908                        return '';
     909
     910                if ( false !== strpos($mime_pattern, '%') )
     911                        $wheres[] = "post_mime_type LIKE '$mime_pattern'";
     912                else
     913                        $wheres[] = "post_mime_type = '$mime_pattern'";
     914        }
     915        if ( !empty($wheres) )
     916                $where = ' AND (' . join(' OR ', $wheres) . ') ';
     917        return $where;
     918}
     919
     920/**
    810921 * wp_delete_post() - Deletes a Post
    811922 *
Note: See TracChangeset for help on using the changeset viewer.