<?php
/**
 * Retrieve list of images from a post.
 *
 * The defaults are as follows:
 *     'number' - Default is -1. Total number of images to retrieve.
 *     'order' - Default is 'ASC'. The order to retrieve the attachments.
 *     'orderby' - Default is 'post_date'. How to order the posts.
 *                 Uses 'natural' to retrieve the images in the order they appear in the post.
 *     'output' - Default is OBJECT. Constant for return type, either OBJECT, ARRAY_A, ARRAY_N.
 *     'post_id' - Default is null. Post ID.
 *     'preserve_keys' - Default is false. If true, the list will be indexed by attachments ID.
 *
 * @author Mehdi Kabab <http://pioupioum.fr>
 * @copyright Copyright (C) 2009 Mehdi Kabab
 * @license http://www.gnu.org/licenses/gpl.html  GNU GPL version 3 or later
 * @version 1.0.0
 *
 * @uses $post If no post_id are transmit and in the loop.
 * @link http://pioupioum.fr/outils-astuces/wordpress-recuperation-avancee-images-article.html
 *
 * @param  array $args Optional. Overrides defaults.
 * @return array|bool List of attachments. False on failure.
 **/
function get_post_images($args = array())
{
    global $post;

    $defaults = array(
        'number'        => -1,
        'order'         => 'ASC',
        'orderby'       => 'post_date',
        'output'        => OBJECT,
        'post_id'       => null,
        'preserve_keys' => false
    );
    $args = wp_parse_args($args, $defaults);

    $post_id = (int) $args['post_id'];
    if (0 === $post_id)
    {
        if (!in_the_loop())
        {
            return false;
        }

        $post_id = $post->ID;
    }
    extract($args, EXTR_SKIP);
    $attachments = false;
    $buf         = false;
    $number      = (int) $number;
    $number      = (0 === $number) ? -1 : $number;

    $order = strtoupper(trim($order));
    if ('ASC' !== $order && 'DESC' !== $order)
    {
        $order = 'ASC';
    }

    switch (strtolower(trim($orderby)))
    {
        case 'natural':
            $orderby = 'natural';
            break;
        case 'post_id':
            $orderby = 'ID';
            break;
        case 'post_date':
        default:
            $orderby = 'post_date_gmt';
            break;
    }

    if ('natural' !== $orderby)
    {
        $attachments = get_children(array(
            'order'          => $order,
            'orderby'        => $orderby,
            'numberposts'    => $number,
            'post_mime_type' => 'image',
            'post_parent'    => (int) $post_id,
            'post_status'    => 'inherit',
            'post_type'      => 'attachment'
        ));
    }

    if (!$attachments)
    {
        if ($post_id == $post->ID)
        {
            $post_content = $post->post_content;
        }
        else
        {
            $my_post = get_post($post_id);
            if (!$my_post)
            {
                return false;
            }
            $post_content = $my_post->post_content;
            unset($my_post);
        }

        $regexp = apply_filters('get_images_regexp', '#<\s*?img\s+.*?wp-image-(\d+)[^>]*>#i');
        if (1 === $number)
        {
            preg_match($regexp, $post_content, $matches);
            if (isset($matches[1]))
            {
                $matches = array($matches[1]);
            }
        }
        else
        {
            preg_match_all($regexp, $post_content, $matches);
            if (isset($matches[1]))
            {
                $matches = $matches[1];
            }
        }
        unset($regexp, $post_content);

        if ($matches_count = count($matches))
        {
            $stack = array();
            if (-1 === $number)
            {
                $c = $matches_count;
            }
            else if ($number < $matches_count)
            {
                $c = $number;
            }
            else
            {
                $c = $matches_count;
            }
            unset($matches_count, $number);

            for ($i = 0; $c > $i; ++$i)
            {
                $id = (int) $matches[$i];
                $stack[$id] = get_post($id);
            }

            if ('natural' === $orderby)
            {
                $attachments = $stack;
            }
            else
            {
                $tmp = array();
                foreach ($stack as $id => $attachment)
                {
                    $tmp[$id] = $attachment->$orderby;
                }
                natcasesort($tmp);

                foreach (array_keys($tmp) as $id)
                {
                    $attachments[$id] = $stack[$id];
                }
                unset($tmp, $attachment);

                if ('DESC' === $order)
                {
                    $attachments = array_reverse($attachments, true);
                }
            }
            unset($stack, $id, $c, $i);
        }
    }

    if ($attachments)
    {
        switch (trim(strtoupper($output)))
        {
            case ARRAY_A:
                $buf = array();
                foreach ($attachments as $id => $attachment)
                {
                    $buf[$id] = get_object_vars($attachment);
                }
                break;

            case ARRAY_N:
                $buf = array();
                foreach ($attachments as $id => $attachment)
                {
                    $buf[$id] = array_values(get_object_vars($attachment));
                }
                break;

            case OBJECT:
            default:
                $buf = $attachments;
                break;
        }

        if (!$preserve_keys)
        {
            $buf = array_merge(array(), $buf);
        }
    }

    return apply_filters('get_images', $buf);
}
