Esempio n. 1
0
/**
 * Get the header images uploaded for the current theme.
 *
 * @since 0.0.1
 *
 * @return array
 */
function get_uploaded_header_images()
{
    $header_images = array();
    // @todo caching
    $headers = get_posts(array('post_type' => 'attachment', 'meta_key' => '_hq_attachment_is_custom_header', 'meta_value' => get_option('stylesheet'), 'orderby' => 'none', 'nopaging' => true));
    if (empty($headers)) {
        return array();
    }
    foreach ((array) $headers as $header) {
        $url = esc_url_raw(hq_get_attachment_url($header->ID));
        $header_data = hq_get_attachment_metadata($header->ID);
        $header_index = basename($url);
        $header_images[$header_index] = array();
        $header_images[$header_index]['attachment_id'] = $header->ID;
        $header_images[$header_index]['url'] = $url;
        $header_images[$header_index]['thumbnail_url'] = $url;
        $header_images[$header_index]['alt_text'] = get_post_meta($header->ID, '_hq_attachment_image_alt', true);
        if (isset($header_data['width'])) {
            $header_images[$header_index]['width'] = $header_data['width'];
        }
        if (isset($header_data['height'])) {
            $header_images[$header_index]['height'] = $header_data['height'];
        }
    }
    return $header_images;
}
Esempio n. 2
0
/**
 * Retrieve thumbnail for an attachment.
 *
 * @since 0.0.1
 *
 * @param int $post_id Optional. Attachment ID. Default 0.
 * @return string|false False on failure. Thumbnail file path on success.
 */
function hq_get_attachment_thumb_file($post_id = 0)
{
    $post_id = (int) $post_id;
    if (!($post = get_post($post_id))) {
        return false;
    }
    if (!is_array($imagedata = hq_get_attachment_metadata($post->ID))) {
        return false;
    }
    $file = get_attached_file($post->ID);
    if (!empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile)) {
        /**
         * Filter the attachment thumbnail file path.
         *
         * @since 0.0.1
         *
         * @param string $thumbfile File path to the attachment thumbnail.
         * @param int    $post_id   Attachment ID.
         */
        return apply_filters('hq_get_attachment_thumb_file', $thumbfile, $post->ID);
    }
    return false;
}
Esempio n. 3
0
/**
 * Maybe attempts to generate attachment metadata, if missing.
 *
 * @since 0.0.1
 *
 * @param HQ_Post $attachment Attachment object.
 */
function hq_maybe_generate_attachment_metadata($attachment)
{
    if (empty($attachment) || (empty($attachment->ID) || !($attachment_id = (int) $attachment->ID))) {
        return;
    }
    $file = get_attached_file($attachment_id);
    $meta = hq_get_attachment_metadata($attachment_id);
    if (empty($meta) && file_exists($file)) {
        $_meta = get_post_meta($attachment_id);
        $regeneration_lock = 'hq_generating_att_' . $attachment_id;
        if (!array_key_exists('_hq_attachment_metadata', $_meta) && !get_transient($regeneration_lock)) {
            set_transient($regeneration_lock, $file);
            hq_update_attachment_metadata($attachment_id, hq_generate_attachment_metadata($attachment_id, $file));
            delete_transient($regeneration_lock);
        }
    }
}
Esempio n. 4
0
    /**
     * Display second step of custom header image page.
     *
     * @since 0.0.1
     */
    public function step_2()
    {
        check_admin_referer('custom-header-upload', '_hqnonce-custom-header-upload');
        if (!current_theme_supports('custom-header', 'uploads')) {
            hq_die(__('Cheatin’ uh?'), 403);
        }
        if (empty($_POST) && isset($_GET['file'])) {
            $attachment_id = absint($_GET['file']);
            $file = get_attached_file($attachment_id, true);
            $url = hq_get_attachment_image_src($attachment_id, 'full');
            $url = $url[0];
        } elseif (isset($_POST)) {
            $data = $this->step_2_manage_upload();
            $attachment_id = $data['attachment_id'];
            $file = $data['file'];
            $url = $data['url'];
        }
        if (file_exists($file)) {
            list($width, $height, $type, $attr) = getimagesize($file);
        } else {
            $data = hq_get_attachment_metadata($attachment_id);
            $height = isset($data['height']) ? $data['height'] : 0;
            $width = isset($data['width']) ? $data['width'] : 0;
            unset($data);
        }
        $max_width = 0;
        // For flex, limit size of image displayed to 1500px unless theme says otherwise
        if (current_theme_supports('custom-header', 'flex-width')) {
            $max_width = 1500;
        }
        if (current_theme_supports('custom-header', 'max-width')) {
            $max_width = max($max_width, get_theme_support('custom-header', 'max-width'));
        }
        $max_width = max($max_width, get_theme_support('custom-header', 'width'));
        // If flexible height isn't supported and the image is the exact right size
        if (!current_theme_supports('custom-header', 'flex-height') && !current_theme_supports('custom-header', 'flex-width') && $width == get_theme_support('custom-header', 'width') && $height == get_theme_support('custom-header', 'height')) {
            // Add the meta-data
            if (file_exists($file)) {
                hq_update_attachment_metadata($attachment_id, hq_generate_attachment_metadata($attachment_id, $file));
            }
            $this->set_header_image(compact('url', 'attachment_id', 'width', 'height'));
            /**
             * Fires after the header image is set or an error is returned.
             *
             * @since 0.0.1
             *
             * @param string $file          Path to the file.
             * @param int    $attachment_id Attachment ID.
             */
            do_action('hq_create_file_in_uploads', $file, $attachment_id);
            // For replication
            return $this->finished();
        } elseif ($width > $max_width) {
            $oitar = $width / $max_width;
            $image = hq_crop_image($attachment_id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace(basename($file), 'midsize-' . basename($file), $file));
            if (!$image || is_hq_error($image)) {
                hq_die(__('Image could not be processed. Please go back and try again.'), __('Image Processing Error'));
            }
            /** This filter is documented in hq-admin/custom-header.php */
            $image = apply_filters('hq_create_file_in_uploads', $image, $attachment_id);
            // For replication
            $url = str_replace(basename($url), basename($image), $url);
            $width = $width / $oitar;
            $height = $height / $oitar;
        } else {
            $oitar = 1;
        }
        ?>

<div class="wrap">
<h1><?php 
        _e('Crop Header Image');
        ?>
</h1>

<form method="post" action="<?php 
        echo esc_url(add_query_arg('step', 3));
        ?>
">
	<p class="hide-if-no-js"><?php 
        _e('Choose the part of the image you want to use as your header.');
        ?>
</p>
	<p class="hide-if-js"><strong><?php 
        _e('You need JavaScript to choose a part of the image.');
        ?>
</strong></p>

	<div id="crop_image" style="position: relative">
		<img src="<?php 
        echo esc_url($url);
        ?>
" id="upload" width="<?php 
        echo $width;
        ?>
" height="<?php 
        echo $height;
        ?>
" />
	</div>

	<input type="hidden" name="x1" id="x1" value="0"/>
	<input type="hidden" name="y1" id="y1" value="0"/>
	<input type="hidden" name="width" id="width" value="<?php 
        echo esc_attr($width);
        ?>
"/>
	<input type="hidden" name="height" id="height" value="<?php 
        echo esc_attr($height);
        ?>
"/>
	<input type="hidden" name="attachment_id" id="attachment_id" value="<?php 
        echo esc_attr($attachment_id);
        ?>
" />
	<input type="hidden" name="oitar" id="oitar" value="<?php 
        echo esc_attr($oitar);
        ?>
" />
	<?php 
        if (empty($_POST) && isset($_GET['file'])) {
            ?>
	<input type="hidden" name="create-new-attachment" value="true" />
	<?php 
        }
        ?>
	<?php 
        hq_nonce_field('custom-header-crop-image');
        ?>

	<p class="submit">
	<?php 
        submit_button(__('Crop and Publish'), 'primary', 'submit', false);
        ?>
	<?php 
        if (isset($oitar) && 1 == $oitar && (current_theme_supports('custom-header', 'flex-height') || current_theme_supports('custom-header', 'flex-width'))) {
            submit_button(__('Skip Cropping, Publish Image as Is'), 'secondary', 'skip-cropping', false);
        }
        ?>
	</p>
</form>
</div>
		<?php 
    }