Esempio n. 1
0
/**
 * Retrieve formatted video metadata.
 *
 * @author Justin Tadlock <*****@*****.**>
 * @author Cherry Team <*****@*****.**>
 * @since  4.0.0
 * @param  int   $post_id  Attachment ID.
 * @param  array $metadata The attachment metadata.
 * @return array           Video metadata.
 */
function cherry_video_meta($post_id, $metadata)
{
    // Get ID3 keys (labels for metadata).
    $id3_keys = wp_get_attachment_id3_keys($post_id);
    $items = array();
    // File size.
    if (!empty($metadata['filesize'])) {
        $items['filesize'] = array(size_format(strip_tags($metadata['filesize']), 2), $id3_keys['filesize']);
    }
    // Mime type.
    if (!empty($metadata['mime_type'])) {
        $items['mime_type'] = array(esc_html($metadata['mime_type']), $id3_keys['mime_type']);
    }
    // Formated length of time the video file runs.
    if (!empty($metadata['length_formatted'])) {
        $items['length_formatted'] = array(esc_html($metadata['length_formatted']), $id3_keys['length_formatted']);
    }
    // Dimensions (width x height in pixels).
    if (!empty($metadata['width']) && !empty($metadata['height'])) {
        // Translators: Media dimensions - 1 is width and 2 is height.
        $items['dimensions'] = array(sprintf(__('%1$s &#215; %2$s', 'cherry'), number_format_i18n(absint($metadata['width'])), number_format_i18n(absint($metadata['height']))), __('Dimensions', 'cherry'));
    }
    /**
     * Filter video metadata.
     *
     * @since 4.0.0
     * @param array $items Metadata.
     */
    return apply_filters('cherry_video_meta', $items);
}
Esempio n. 2
0
/**
 * Ajax handler for updating attachment attributes.
 *
 * @since 3.5.0
 */
function wp_ajax_save_attachment()
{
    if (!isset($_REQUEST['id']) || !isset($_REQUEST['changes'])) {
        wp_send_json_error();
    }
    if (!($id = absint($_REQUEST['id']))) {
        wp_send_json_error();
    }
    check_ajax_referer('update-post_' . $id, 'nonce');
    if (!current_user_can('edit_post', $id)) {
        wp_send_json_error();
    }
    $changes = $_REQUEST['changes'];
    $post = get_post($id, ARRAY_A);
    if ('attachment' != $post['post_type']) {
        wp_send_json_error();
    }
    if (isset($changes['parent'])) {
        $post['post_parent'] = $changes['parent'];
    }
    if (isset($changes['title'])) {
        $post['post_title'] = $changes['title'];
    }
    if (isset($changes['caption'])) {
        $post['post_excerpt'] = $changes['caption'];
    }
    if (isset($changes['description'])) {
        $post['post_content'] = $changes['description'];
    }
    if (MEDIA_TRASH && isset($changes['status'])) {
        $post['post_status'] = $changes['status'];
    }
    if (isset($changes['alt'])) {
        $alt = wp_unslash($changes['alt']);
        if ($alt != get_post_meta($id, '_wp_attachment_image_alt', true)) {
            $alt = wp_strip_all_tags($alt, true);
            update_post_meta($id, '_wp_attachment_image_alt', wp_slash($alt));
        }
    }
    if (wp_attachment_is('audio', $post['ID'])) {
        $changed = false;
        $id3data = wp_get_attachment_metadata($post['ID']);
        if (!is_array($id3data)) {
            $changed = true;
            $id3data = array();
        }
        foreach (wp_get_attachment_id3_keys((object) $post, 'edit') as $key => $label) {
            if (isset($changes[$key])) {
                $changed = true;
                $id3data[$key] = sanitize_text_field(wp_unslash($changes[$key]));
            }
        }
        if ($changed) {
            wp_update_attachment_metadata($id, $id3data);
        }
    }
    if (MEDIA_TRASH && isset($changes['status']) && 'trash' === $changes['status']) {
        wp_delete_post($id);
    } else {
        wp_update_post($post);
    }
    wp_send_json_success();
}
Esempio n. 3
0
/**
 * Update an existing post with values provided in $_POST.
 *
 * @since 1.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param array $post_data Optional.
 * @return int Post ID.
 */
function edit_post($post_data = null)
{
    global $wpdb;
    if (empty($post_data)) {
        $post_data =& $_POST;
    }
    // Clear out any data in internal vars.
    unset($post_data['filter']);
    $post_ID = (int) $post_data['post_ID'];
    $post = get_post($post_ID);
    $post_data['post_type'] = $post->post_type;
    $post_data['post_mime_type'] = $post->post_mime_type;
    if (!empty($post_data['post_status'])) {
        $post_data['post_status'] = sanitize_key($post_data['post_status']);
        if ('inherit' == $post_data['post_status']) {
            unset($post_data['post_status']);
        }
    }
    $ptype = get_post_type_object($post_data['post_type']);
    if (!current_user_can('edit_post', $post_ID)) {
        if ('page' == $post_data['post_type']) {
            wp_die(__('Sorry, you are not allowed to edit this page.'));
        } else {
            wp_die(__('Sorry, you are not allowed to edit this post.'));
        }
    }
    if (post_type_supports($ptype->name, 'revisions')) {
        $revisions = wp_get_post_revisions($post_ID, array('order' => 'ASC', 'posts_per_page' => 1));
        $revision = current($revisions);
        // Check if the revisions have been upgraded
        if ($revisions && _wp_get_post_revision_version($revision) < 1) {
            _wp_upgrade_revisions_of_post($post, wp_get_post_revisions($post_ID));
        }
    }
    if (isset($post_data['visibility'])) {
        switch ($post_data['visibility']) {
            case 'public':
                $post_data['post_password'] = '';
                break;
            case 'password':
                unset($post_data['sticky']);
                break;
            case 'private':
                $post_data['post_status'] = 'private';
                $post_data['post_password'] = '';
                unset($post_data['sticky']);
                break;
        }
    }
    $post_data = _wp_translate_postdata(true, $post_data);
    if (is_wp_error($post_data)) {
        wp_die($post_data->get_error_message());
    }
    // Post Formats
    if (isset($post_data['post_format'])) {
        set_post_format($post_ID, $post_data['post_format']);
    }
    $format_meta_urls = array('url', 'link_url', 'quote_source_url');
    foreach ($format_meta_urls as $format_meta_url) {
        $keyed = '_format_' . $format_meta_url;
        if (isset($post_data[$keyed])) {
            update_post_meta($post_ID, $keyed, wp_slash(esc_url_raw(wp_unslash($post_data[$keyed]))));
        }
    }
    $format_keys = array('quote', 'quote_source_name', 'image', 'gallery', 'audio_embed', 'video_embed');
    foreach ($format_keys as $key) {
        $keyed = '_format_' . $key;
        if (isset($post_data[$keyed])) {
            if (current_user_can('unfiltered_html')) {
                update_post_meta($post_ID, $keyed, $post_data[$keyed]);
            } else {
                update_post_meta($post_ID, $keyed, wp_filter_post_kses($post_data[$keyed]));
            }
        }
    }
    if ('attachment' === $post_data['post_type'] && preg_match('#^(audio|video)/#', $post_data['post_mime_type'])) {
        $id3data = wp_get_attachment_metadata($post_ID);
        if (!is_array($id3data)) {
            $id3data = array();
        }
        foreach (wp_get_attachment_id3_keys($post, 'edit') as $key => $label) {
            if (isset($post_data['id3_' . $key])) {
                $id3data[$key] = sanitize_text_field(wp_unslash($post_data['id3_' . $key]));
            }
        }
        wp_update_attachment_metadata($post_ID, $id3data);
    }
    // Meta Stuff
    if (isset($post_data['meta']) && $post_data['meta']) {
        foreach ($post_data['meta'] as $key => $value) {
            if (!($meta = get_post_meta_by_id($key))) {
                continue;
            }
            if ($meta->post_id != $post_ID) {
                continue;
            }
            if (is_protected_meta($value['key'], 'post') || !current_user_can('edit_post_meta', $post_ID, $value['key'])) {
                continue;
            }
            update_meta($key, $value['key'], $value['value']);
        }
    }
    if (isset($post_data['deletemeta']) && $post_data['deletemeta']) {
        foreach ($post_data['deletemeta'] as $key => $value) {
            if (!($meta = get_post_meta_by_id($key))) {
                continue;
            }
            if ($meta->post_id != $post_ID) {
                continue;
            }
            if (is_protected_meta($meta->meta_key, 'post') || !current_user_can('delete_post_meta', $post_ID, $meta->meta_key)) {
                continue;
            }
            delete_meta($key);
        }
    }
    // Attachment stuff
    if ('attachment' == $post_data['post_type']) {
        if (isset($post_data['_wp_attachment_image_alt'])) {
            $image_alt = wp_unslash($post_data['_wp_attachment_image_alt']);
            if ($image_alt != get_post_meta($post_ID, '_wp_attachment_image_alt', true)) {
                $image_alt = wp_strip_all_tags($image_alt, true);
                // update_meta expects slashed.
                update_post_meta($post_ID, '_wp_attachment_image_alt', wp_slash($image_alt));
            }
        }
        $attachment_data = isset($post_data['attachments'][$post_ID]) ? $post_data['attachments'][$post_ID] : array();
        /** This filter is documented in wp-admin/includes/media.php */
        $post_data = apply_filters('attachment_fields_to_save', $post_data, $attachment_data);
    }
    // Convert taxonomy input to term IDs, to avoid ambiguity.
    if (isset($post_data['tax_input'])) {
        foreach ((array) $post_data['tax_input'] as $taxonomy => $terms) {
            // Hierarchical taxonomy data is already sent as term IDs, so no conversion is necessary.
            if (is_taxonomy_hierarchical($taxonomy)) {
                continue;
            }
            /*
             * Assume that a 'tax_input' string is a comma-separated list of term names.
             * Some languages may use a character other than a comma as a delimiter, so we standardize on
             * commas before parsing the list.
             */
            if (!is_array($terms)) {
                $comma = _x(',', 'tag delimiter');
                if (',' !== $comma) {
                    $terms = str_replace($comma, ',', $terms);
                }
                $terms = explode(',', trim($terms, " \n\t\r\v,"));
            }
            $clean_terms = array();
            foreach ($terms as $term) {
                // Empty terms are invalid input.
                if (empty($term)) {
                    continue;
                }
                $_term = get_terms($taxonomy, array('name' => $term, 'fields' => 'ids', 'hide_empty' => false));
                if (!empty($_term)) {
                    $clean_terms[] = intval($_term[0]);
                } else {
                    // No existing term was found, so pass the string. A new term will be created.
                    $clean_terms[] = $term;
                }
            }
            $post_data['tax_input'][$taxonomy] = $clean_terms;
        }
    }
    add_meta($post_ID);
    update_post_meta($post_ID, '_edit_last', get_current_user_id());
    $success = wp_update_post($post_data);
    // If the save failed, see if we can sanity check the main fields and try again
    if (!$success && is_callable(array($wpdb, 'strip_invalid_text_for_column'))) {
        $fields = array('post_title', 'post_content', 'post_excerpt');
        foreach ($fields as $field) {
            if (isset($post_data[$field])) {
                $post_data[$field] = $wpdb->strip_invalid_text_for_column($wpdb->posts, $field, $post_data[$field]);
            }
        }
        wp_update_post($post_data);
    }
    // Now that we have an ID we can fix any attachment anchor hrefs
    _fix_attachment_links($post_ID);
    wp_set_post_lock($post_ID);
    if (current_user_can($ptype->cap->edit_others_posts) && current_user_can($ptype->cap->publish_posts)) {
        if (!empty($post_data['sticky'])) {
            stick_post($post_ID);
        } else {
            unstick_post($post_ID);
        }
    }
    return $post_ID;
}
Esempio n. 4
0
/**
 * Display fields for ID3 data
 *
 * @since 3.9.0
 *
 * @param WP_Post $post
 */
function attachment_id3_data_meta_box($post)
{
    $meta = array();
    if (!empty($post->ID)) {
        $meta = wp_get_attachment_metadata($post->ID);
    }
    foreach (wp_get_attachment_id3_keys($post, 'edit') as $key => $label) {
        ?>
	<p>
		<label for="title"><?php 
        echo $label;
        ?>
</label><br />
		<input type="text" name="id3_<?php 
        echo esc_attr($key);
        ?>
" id="id3_<?php 
        echo esc_attr($key);
        ?>
" class="large-text" value="<?php 
        if (!empty($meta[$key])) {
            echo esc_attr($meta[$key]);
        }
        ?>
" />
	</p>
	<?php 
    }
}
Esempio n. 5
0
/**
 * Prepares an attachment post object for JS, where it is expected
 * to be JSON-encoded and fit into an Attachment model.
 *
 * @since 3.5.0
 *
 * @param mixed $attachment Attachment ID or object.
 * @return array|void Array of attachment details.
 */
function wp_prepare_attachment_for_js($attachment)
{
    if (!($attachment = get_post($attachment))) {
        return;
    }
    if ('attachment' != $attachment->post_type) {
        return;
    }
    $meta = wp_get_attachment_metadata($attachment->ID);
    if (false !== strpos($attachment->post_mime_type, '/')) {
        list($type, $subtype) = explode('/', $attachment->post_mime_type);
    } else {
        list($type, $subtype) = array($attachment->post_mime_type, '');
    }
    $attachment_url = wp_get_attachment_url($attachment->ID);
    $response = array('id' => $attachment->ID, 'title' => $attachment->post_title, 'filename' => wp_basename(get_attached_file($attachment->ID)), 'url' => $attachment_url, 'link' => get_attachment_link($attachment->ID), 'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true), 'author' => $attachment->post_author, 'description' => $attachment->post_content, 'caption' => $attachment->post_excerpt, 'name' => $attachment->post_name, 'status' => $attachment->post_status, 'uploadedTo' => $attachment->post_parent, 'date' => strtotime($attachment->post_date_gmt) * 1000, 'modified' => strtotime($attachment->post_modified_gmt) * 1000, 'menuOrder' => $attachment->menu_order, 'mime' => $attachment->post_mime_type, 'type' => $type, 'subtype' => $subtype, 'icon' => wp_mime_type_icon($attachment->ID), 'dateFormatted' => mysql2date(get_option('date_format'), $attachment->post_date), 'nonces' => array('update' => false, 'delete' => false, 'edit' => false), 'editLink' => false, 'meta' => false);
    $author = new WP_User($attachment->post_author);
    $response['authorName'] = $author->display_name;
    if ($attachment->post_parent) {
        $post_parent = get_post($attachment->post_parent);
    } else {
        $post_parent = false;
    }
    if ($post_parent) {
        $parent_type = get_post_type_object($post_parent->post_type);
        if ($parent_type && $parent_type->show_ui && current_user_can('edit_post', $attachment->post_parent)) {
            $response['uploadedToLink'] = get_edit_post_link($attachment->post_parent, 'raw');
        }
        $response['uploadedToTitle'] = $post_parent->post_title ? $post_parent->post_title : __('(no title)');
    }
    $attached_file = get_attached_file($attachment->ID);
    if (isset($meta['filesize'])) {
        $bytes = $meta['filesize'];
    } elseif (file_exists($attached_file)) {
        $bytes = filesize($attached_file);
    } else {
        $bytes = '';
    }
    if ($bytes) {
        $response['filesizeInBytes'] = $bytes;
        $response['filesizeHumanReadable'] = size_format($bytes);
    }
    if (current_user_can('edit_post', $attachment->ID)) {
        $response['nonces']['update'] = wp_create_nonce('update-post_' . $attachment->ID);
        $response['nonces']['edit'] = wp_create_nonce('image_editor-' . $attachment->ID);
        $response['editLink'] = get_edit_post_link($attachment->ID, 'raw');
    }
    if (current_user_can('delete_post', $attachment->ID)) {
        $response['nonces']['delete'] = wp_create_nonce('delete-post_' . $attachment->ID);
    }
    if ($meta && 'image' === $type) {
        $sizes = array();
        /** This filter is documented in wp-admin/includes/media.php */
        $possible_sizes = apply_filters('image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')));
        unset($possible_sizes['full']);
        // Loop through all potential sizes that may be chosen. Try to do this with some efficiency.
        // First: run the image_downsize filter. If it returns something, we can use its data.
        // If the filter does not return something, then image_downsize() is just an expensive
        // way to check the image metadata, which we do second.
        foreach ($possible_sizes as $size => $label) {
            /** This filter is documented in wp-includes/media.php */
            if ($downsize = apply_filters('image_downsize', false, $attachment->ID, $size)) {
                if (!$downsize[3]) {
                    continue;
                }
                $sizes[$size] = array('height' => $downsize[2], 'width' => $downsize[1], 'url' => $downsize[0], 'orientation' => $downsize[2] > $downsize[1] ? 'portrait' : 'landscape');
            } elseif (isset($meta['sizes'][$size])) {
                if (!isset($base_url)) {
                    $base_url = str_replace(wp_basename($attachment_url), '', $attachment_url);
                }
                // Nothing from the filter, so consult image metadata if we have it.
                $size_meta = $meta['sizes'][$size];
                // We have the actual image size, but might need to further constrain it if content_width is narrower.
                // Thumbnail, medium, and full sizes are also checked against the site's height/width options.
                list($width, $height) = image_constrain_size_for_editor($size_meta['width'], $size_meta['height'], $size, 'edit');
                $sizes[$size] = array('height' => $height, 'width' => $width, 'url' => $base_url . $size_meta['file'], 'orientation' => $height > $width ? 'portrait' : 'landscape');
            }
        }
        $sizes['full'] = array('url' => $attachment_url);
        if (isset($meta['height'], $meta['width'])) {
            $sizes['full']['height'] = $meta['height'];
            $sizes['full']['width'] = $meta['width'];
            $sizes['full']['orientation'] = $meta['height'] > $meta['width'] ? 'portrait' : 'landscape';
        }
        $response = array_merge($response, array('sizes' => $sizes), $sizes['full']);
    } elseif ($meta && 'video' === $type) {
        if (isset($meta['width'])) {
            $response['width'] = (int) $meta['width'];
        }
        if (isset($meta['height'])) {
            $response['height'] = (int) $meta['height'];
        }
    }
    if ($meta && ('audio' === $type || 'video' === $type)) {
        if (isset($meta['length_formatted'])) {
            $response['fileLength'] = $meta['length_formatted'];
        }
        $response['meta'] = array();
        foreach (wp_get_attachment_id3_keys($attachment, 'js') as $key => $label) {
            $response['meta'][$key] = false;
            if (!empty($meta[$key])) {
                $response['meta'][$key] = $meta[$key];
            }
        }
        $id = get_post_thumbnail_id($attachment->ID);
        if (!empty($id)) {
            list($src, $width, $height) = wp_get_attachment_image_src($id, 'full');
            $response['image'] = compact('src', 'width', 'height');
            list($src, $width, $height) = wp_get_attachment_image_src($id, 'thumbnail');
            $response['thumb'] = compact('src', 'width', 'height');
        } else {
            $src = wp_mime_type_icon($attachment->ID);
            $width = 48;
            $height = 64;
            $response['image'] = compact('src', 'width', 'height');
            $response['thumb'] = compact('src', 'width', 'height');
        }
    }
    if (function_exists('get_compat_media_markup')) {
        $response['compat'] = get_compat_media_markup($attachment->ID, array('in_modal' => true));
    }
    /**
     * Filter the attachment data prepared for JavaScript.
     *
     * @since 3.5.0
     *
     * @param array      $response   Array of prepared attachment data.
     * @param int|object $attachment Attachment ID or object.
     * @param array      $meta       Array of attachment meta data.
     */
    return apply_filters('wp_prepare_attachment_for_js', $response, $attachment, $meta);
}
Esempio n. 6
0
/**
 * Update an existing post with values provided in $_POST.
 *
 * @since 1.5.0
 *
 * @param array $post_data Optional.
 * @return int Post ID.
 */
function edit_post($post_data = null)
{
    global $wpdb;
    if (empty($post_data)) {
        $post_data =& $_POST;
    }
    // Clear out any data in internal vars.
    unset($post_data['filter']);
    $post_ID = (int) $post_data['post_ID'];
    $post = get_post($post_ID);
    $post_data['post_type'] = $post->post_type;
    $post_data['post_mime_type'] = $post->post_mime_type;
    if (!empty($post_data['post_status'])) {
        $post_data['post_status'] = sanitize_key($post_data['post_status']);
        if ('inherit' == $post_data['post_status']) {
            unset($post_data['post_status']);
        }
    }
    $ptype = get_post_type_object($post_data['post_type']);
    if (!current_user_can('edit_post', $post_ID)) {
        if ('page' == $post_data['post_type']) {
            wp_die(__('You are not allowed to edit this page.'));
        } else {
            wp_die(__('You are not allowed to edit this post.'));
        }
    }
    if (post_type_supports($ptype->name, 'revisions')) {
        $revisions = wp_get_post_revisions($post_ID, array('order' => 'ASC', 'posts_per_page' => 1));
        $revision = current($revisions);
        // Check if the revisions have been upgraded
        if ($revisions && _wp_get_post_revision_version($revision) < 1) {
            _wp_upgrade_revisions_of_post($post, wp_get_post_revisions($post_ID));
        }
    }
    if (isset($post_data['visibility'])) {
        switch ($post_data['visibility']) {
            case 'public':
                $post_data['post_password'] = '';
                break;
            case 'password':
                unset($post_data['sticky']);
                break;
            case 'private':
                $post_data['post_status'] = 'private';
                $post_data['post_password'] = '';
                unset($post_data['sticky']);
                break;
        }
    }
    $post_data = _wp_translate_postdata(true, $post_data);
    if (is_wp_error($post_data)) {
        wp_die($post_data->get_error_message());
    }
    // Post Formats
    if (isset($post_data['post_format'])) {
        set_post_format($post_ID, $post_data['post_format']);
    }
    $format_meta_urls = array('url', 'link_url', 'quote_source_url');
    foreach ($format_meta_urls as $format_meta_url) {
        $keyed = '_format_' . $format_meta_url;
        if (isset($post_data[$keyed])) {
            update_post_meta($post_ID, $keyed, wp_slash(esc_url_raw(wp_unslash($post_data[$keyed]))));
        }
    }
    $format_keys = array('quote', 'quote_source_name', 'image', 'gallery', 'audio_embed', 'video_embed');
    foreach ($format_keys as $key) {
        $keyed = '_format_' . $key;
        if (isset($post_data[$keyed])) {
            if (current_user_can('unfiltered_html')) {
                update_post_meta($post_ID, $keyed, $post_data[$keyed]);
            } else {
                update_post_meta($post_ID, $keyed, wp_filter_post_kses($post_data[$keyed]));
            }
        }
    }
    if ('attachment' === $post_data['post_type'] && preg_match('#^(audio|video)/#', $post_data['post_mime_type'])) {
        $id3data = wp_get_attachment_metadata($post_ID);
        if (!is_array($id3data)) {
            $id3data = array();
        }
        foreach (wp_get_attachment_id3_keys($post, 'edit') as $key => $label) {
            if (isset($post_data['id3_' . $key])) {
                $id3data[$key] = sanitize_text_field(wp_unslash($post_data['id3_' . $key]));
            }
        }
        wp_update_attachment_metadata($post_ID, $id3data);
    }
    // Meta Stuff
    if (isset($post_data['meta']) && $post_data['meta']) {
        foreach ($post_data['meta'] as $key => $value) {
            if (!($meta = get_post_meta_by_id($key))) {
                continue;
            }
            if ($meta->post_id != $post_ID) {
                continue;
            }
            if (is_protected_meta($value['key'], 'post') || !current_user_can('edit_post_meta', $post_ID, $value['key'])) {
                continue;
            }
            update_meta($key, $value['key'], $value['value']);
        }
    }
    if (isset($post_data['deletemeta']) && $post_data['deletemeta']) {
        foreach ($post_data['deletemeta'] as $key => $value) {
            if (!($meta = get_post_meta_by_id($key))) {
                continue;
            }
            if ($meta->post_id != $post_ID) {
                continue;
            }
            if (is_protected_meta($meta->meta_key, 'post') || !current_user_can('delete_post_meta', $post_ID, $meta->meta_key)) {
                continue;
            }
            delete_meta($key);
        }
    }
    // Attachment stuff
    if ('attachment' == $post_data['post_type']) {
        if (isset($post_data['_wp_attachment_image_alt'])) {
            $image_alt = wp_unslash($post_data['_wp_attachment_image_alt']);
            if ($image_alt != get_post_meta($post_ID, '_wp_attachment_image_alt', true)) {
                $image_alt = wp_strip_all_tags($image_alt, true);
                // update_meta expects slashed.
                update_post_meta($post_ID, '_wp_attachment_image_alt', wp_slash($image_alt));
            }
        }
        $attachment_data = isset($post_data['attachments'][$post_ID]) ? $post_data['attachments'][$post_ID] : array();
        /** This filter is documented in wp-admin/includes/media.php */
        $post_data = apply_filters('attachment_fields_to_save', $post_data, $attachment_data);
    }
    add_meta($post_ID);
    update_post_meta($post_ID, '_edit_last', get_current_user_id());
    $success = wp_update_post($post_data);
    // If the save failed, see if we can sanity check the main fields and try again
    if (!$success && is_callable(array($wpdb, 'strip_invalid_text_for_column'))) {
        $fields = array('post_title', 'post_content', 'post_excerpt');
        foreach ($fields as $field) {
            if (isset($post_data[$field])) {
                $post_data[$field] = $wpdb->strip_invalid_text_for_column($wpdb->posts, $field, $post_data[$field]);
            }
        }
        wp_update_post($post_data);
    }
    // Now that we have an ID we can fix any attachment anchor hrefs
    _fix_attachment_links($post_ID);
    wp_set_post_lock($post_ID);
    if (current_user_can($ptype->cap->edit_others_posts)) {
        if (!empty($post_data['sticky'])) {
            stick_post($post_ID);
        } else {
            unstick_post($post_ID);
        }
    }
    return $post_ID;
}
Esempio n. 7
0
    function wp_playlist_shortcode($output, $attr)
    {
        global $content_width;
        $post = get_post();
        static $instance = 0;
        $instance++;
        if (isset($attr['orderby'])) {
            $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
            if (!$attr['orderby']) {
                unset($attr['orderby']);
            }
        }
        extract(shortcode_atts(array('type' => 'audio', 'order' => 'ASC', 'orderby' => 'menu_order ID', 'id' => $post ? $post->ID : 0, 'include' => '', 'exclude' => '', 'style' => 'light', 'tracklist' => true, 'tracknumbers' => true, 'images' => true, 'artists' => true), $attr, 'playlist'));
        $id = intval($id);
        if ('RAND' == $order) {
            $orderby = 'none';
        }
        $args = array('post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => $type, 'order' => $order, 'orderby' => $orderby);
        if (!empty($include)) {
            $args['include'] = $include;
            $_attachments = get_posts($args);
            $attachments = array();
            foreach ($_attachments as $key => $val) {
                $attachments[$val->ID] = $_attachments[$key];
            }
        } elseif (!empty($exclude)) {
            $args['post_parent'] = $id;
            $args['exclude'] = $exclude;
            $attachments = get_children($args);
        } else {
            $args['post_parent'] = $id;
            $attachments = get_children($args);
        }
        if (empty($attachments)) {
            return '';
        }
        if (is_feed()) {
            $output = "\n";
            foreach ($attachments as $att_id => $attachment) {
                $output .= wp_get_attachment_link($att_id) . "\n";
            }
            return $output;
        }
        $outer = 22;
        $default_width = 640;
        $default_height = 360;
        $theme_width = empty($content_width) ? $default_width : $content_width - $outer;
        $theme_height = empty($content_width) ? $default_height : round($default_height * $theme_width / $default_width);
        $data = compact('type');
        foreach (array('tracklist', 'tracknumbers', 'images', 'artists') as $key) {
            $data[$key] = filter_var(${$key}, FILTER_VALIDATE_BOOLEAN);
        }
        $tracks = array();
        foreach ($attachments as $attachment) {
            $url = wp_get_attachment_url($attachment->ID);
            $ftype = wp_check_filetype($url, wp_get_mime_types());
            $track = array('src' => $url, 'type' => $ftype['type'], 'title' => $attachment->post_title, 'caption' => $attachment->post_excerpt, 'description' => $attachment->post_content);
            $track['meta'] = array();
            $meta = wp_get_attachment_metadata($attachment->ID);
            if (!empty($meta)) {
                foreach (wp_get_attachment_id3_keys($attachment) as $key => $label) {
                    if (!empty($meta[$key])) {
                        $track['meta'][$key] = $meta[$key];
                    }
                }
                if ('video' === $type) {
                    if (!empty($meta['width']) && !empty($meta['height'])) {
                        $width = $meta['width'];
                        $height = $meta['height'];
                        $theme_height = round($height * $theme_width / $width);
                    } else {
                        $width = $default_width;
                        $height = $default_height;
                    }
                    $track['dimensions'] = array('original' => compact('width', 'height'), 'resized' => array('width' => $theme_width, 'height' => $theme_height));
                }
            }
            if ($images) {
                $id = get_post_thumbnail_id($attachment->ID);
                if (!empty($id)) {
                    list($src, $width, $height) = wp_get_attachment_image_src($id, 'full');
                    $track['image'] = compact('src', 'width', 'height');
                    list($src, $width, $height) = wp_get_attachment_image_src($id, 'thumbnail');
                    $track['thumb'] = compact('src', 'width', 'height');
                } else {
                    $src = wp_mime_type_icon($attachment->ID);
                    $width = 48;
                    $height = 64;
                    $track['image'] = compact('src', 'width', 'height');
                    $track['thumb'] = compact('src', 'width', 'height');
                }
            }
            $tracks[] = $track;
        }
        $data['tracks'] = $tracks;
        $safe_type = esc_attr($type);
        $safe_style = esc_attr($style);
        $playlist_meta = 'true' == $this->options('playlist_meta') ? "" : " hide-playlist-meta-pro ";
        ob_start();
        if (1 === $instance) {
            do_action('wp_playlist_scripts', $type, $style);
        }
        ?>
	<div class="wp-playlist wp-<?php 
        echo $safe_type;
        ?>
-playlist wp-playlist-<?php 
        echo $safe_style;
        ?>
 <?php 
        echo $playlist_meta;
        ?>
">
		<?php 
        if ('audio' === $type) {
            ?>
		<div class="wp-playlist-current-item"></div>
		<?php 
        }
        ?>
		<<?php 
        echo $safe_type;
        ?>
 controls="controls" preload="none" width="<?php 
        echo (int) $theme_width;
        ?>
"<?php 
        if ('video' === $safe_type) {
            echo ' height="', (int) $theme_height, '"';
        } else {
            echo ' style="visibility: hidden"';
        }
        ?>
></<?php 
        echo $safe_type;
        ?>
>
		<div class="wp-playlist-next"></div>
		<div class="wp-playlist-prev"></div>
		<noscript>
		<ol><?php 
        foreach ($attachments as $att_id => $attachment) {
            printf('<li>%s</li>', wp_get_attachment_link($att_id));
        }
        ?>
</ol>
		</noscript>
		<script type="application/json"><?php 
        echo json_encode($data);
        ?>
</script>
	</div>
		<?php 
        return apply_filters('wp_playlist_shortcode', ob_get_clean());
    }
Esempio n. 8
0
 /**
  * Adds and formats video meta data for the items array.
  *
  * @since  2.0.0
  * @access public
  * @return void
  */
 public function video_meta()
 {
     /* Get ID3 keys (labels for metadata). */
     $id3_keys = wp_get_attachment_id3_keys(get_post($this->args['post_id']));
     /* Formated length of time the video file runs. */
     if (!empty($this->meta['length_formatted'])) {
         $this->items['length_formatted'] = array($this->meta['length_formatted'], $id3_keys['length_formatted']);
     }
     /* Dimensions (width x height in pixels). */
     if (!empty($this->meta['width']) && !empty($this->meta['height'])) {
         /* Translators: Media dimensions - 1 is width and 2 is height. */
         $this->items['dimensions'] = array(sprintf(__('%1$s &#215; %2$s', 'hybrid-core'), number_format_i18n(absint($this->meta['width'])), number_format_i18n(absint($this->meta['height']))), __('Dimensions', 'hybrid-core'));
     }
     /* File name.  We're linking this to the actual file URL. */
     $this->items['file_name'] = array('<a href="' . esc_url(wp_get_attachment_url($this->args['post_id'])) . '">' . basename(get_attached_file($this->args['post_id'])) . '</a>', __('File Name', 'hybrid-core'));
     /* File size. */
     if (!empty($this->meta['filesize'])) {
         $this->items['filesize'] = array(size_format($this->meta['filesize'], 2), $id3_keys['filesize']);
     }
     /* File type (the metadata for this can be incorrect, so we're just looking at the actual file). */
     if (preg_match('/^.*?\\.(\\w+)$/', get_attached_file($this->args['post_id']), $matches)) {
         $this->items['file_type'] = array(esc_html(strtoupper($matches[1])), __('File Type', 'hybrid-core'));
     }
     /* Mime type. */
     if (!empty($this->meta['mime_type'])) {
         $this->items['mime_type'] = array($this->meta['mime_type'], $id3_keys['mime_type']);
     }
 }
Esempio n. 9
0
/**
 * Prepare Media for JSON
 *  this is a copy from send json for attachment, we will improve it in our 1.1 release
 * @todo refactor
 * @param type $attachment
 * @return type
 */
function mpp_media_to_json($attachment)
{
    if (!($attachment = get_post($attachment))) {
        return;
    }
    if ('attachment' != $attachment->post_type) {
        return;
    }
    //the attachment can be either a media or a cover
    //in case of media, if it is non photo, we need the thumb.url to point to the cover(or generated cover)
    //in case of cover, we don't care
    $media = mpp_get_media($attachment->ID);
    $meta = wp_get_attachment_metadata($attachment->ID);
    if (false !== strpos($attachment->post_mime_type, '/')) {
        list($type, $subtype) = explode('/', $attachment->post_mime_type);
    } else {
        list($type, $subtype) = array($attachment->post_mime_type, '');
    }
    $attachment_url = wp_get_attachment_url($attachment->ID);
    $response = array('id' => $media->id, 'title' => $media->title, 'filename' => wp_basename($attachment->guid), 'url' => $attachment_url, 'link' => mpp_get_media_permalink($media), 'alt' => $media->title, 'author' => $media->user_id, 'description' => $media->description, 'caption' => $media->excerpt, 'name' => $media->slug, 'status' => $media->status, 'parent_id' => $media->gallery_id, 'date' => strtotime($attachment->post_date_gmt) * 1000, 'modified' => strtotime($attachment->post_modified_gmt) * 1000, 'menuOrder' => $attachment->menu_order, 'mime' => $attachment->post_mime_type, 'type' => $media->type, 'subtype' => $subtype, 'dateFormatted' => mysql2date(get_option('date_format'), $attachment->post_date), 'meta' => false);
    if ($attachment->post_parent) {
        $post_parent = get_post($attachment->post_parent);
        $parent_type = get_post_type_object($post_parent->post_type);
        if ($parent_type && $parent_type->show_ui && current_user_can('edit_post', $attachment->post_parent)) {
            $response['uploadedToLink'] = get_edit_post_link($attachment->post_parent, 'raw');
        }
        $response['uploadedToTitle'] = $post_parent->post_title ? $post_parent->post_title : __('(no title)');
    }
    $attached_file = get_attached_file($attachment->ID);
    if (file_exists($attached_file)) {
        $bytes = filesize($attached_file);
        $response['filesizeInBytes'] = $bytes;
        $response['filesizeHumanReadable'] = size_format($bytes);
    }
    if ($meta && 'image' === $type) {
        $sizes = array();
        /** This filter is documented in wp-admin/includes/media.php */
        $possible_sizes = apply_filters('image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')));
        unset($possible_sizes['full']);
        // Loop through all potential sizes that may be chosen. Try to do this with some efficiency.
        // First: run the image_downsize filter. If it returns something, we can use its data.
        // If the filter does not return something, then image_downsize() is just an expensive
        // way to check the image metadata, which we do second.
        foreach ($possible_sizes as $size => $label) {
            /** This filter is documented in wp-includes/media.php */
            if ($downsize = apply_filters('image_downsize', false, $attachment->ID, $size)) {
                if (!$downsize[3]) {
                    continue;
                }
                $sizes[$size] = array('height' => $downsize[2], 'width' => $downsize[1], 'url' => $downsize[0], 'orientation' => $downsize[2] > $downsize[1] ? 'portrait' : 'landscape');
            } elseif (isset($meta['sizes'][$size])) {
                if (!isset($base_url)) {
                    $base_url = str_replace(wp_basename($attachment_url), '', $attachment_url);
                }
                // Nothing from the filter, so consult image metadata if we have it.
                $size_meta = $meta['sizes'][$size];
                // We have the actual image size, but might need to further constrain it if content_width is narrower.
                // Thumbnail, medium, and full sizes are also checked against the site's height/width options.
                list($width, $height) = image_constrain_size_for_editor($size_meta['width'], $size_meta['height'], $size, 'edit');
                $sizes[$size] = array('height' => $height, 'width' => $width, 'url' => $base_url . $size_meta['file'], 'orientation' => $height > $width ? 'portrait' : 'landscape');
            }
        }
        $sizes['full'] = array('url' => $attachment_url);
        if (isset($meta['height'], $meta['width'])) {
            $sizes['full']['height'] = $meta['height'];
            $sizes['full']['width'] = $meta['width'];
            $sizes['full']['orientation'] = $meta['height'] > $meta['width'] ? 'portrait' : 'landscape';
        }
        $response = array_merge($response, array('sizes' => $sizes), $sizes['full']);
    } elseif ($meta && 'video' === $type) {
        if (isset($meta['width'])) {
            $response['width'] = (int) $meta['width'];
        }
        if (isset($meta['height'])) {
            $response['height'] = (int) $meta['height'];
        }
    }
    if ($meta && ('audio' === $type || 'video' === $type)) {
        if (isset($meta['length_formatted'])) {
            $response['fileLength'] = $meta['length_formatted'];
        }
        $response['meta'] = array();
        foreach (wp_get_attachment_id3_keys($attachment, 'js') as $key => $label) {
            $response['meta'][$key] = false;
            if (!empty($meta[$key])) {
                $response['meta'][$key] = $meta[$key];
            }
        }
        $id = mpp_get_media_cover_id($attachment->ID);
        if (!empty($id)) {
            list($url, $width, $height) = wp_get_attachment_image_src($id, 'full');
            $response['image'] = compact('url', 'width', 'height');
            list($url, $width, $height) = wp_get_attachment_image_src($id, 'thumbnail');
            $response['thumb'] = compact('url', 'width', 'height');
        } else {
            $url = mpp_get_media_cover_src('thumbnail', $media->id);
            $width = 48;
            $height = 64;
            $response['image'] = compact('url', 'width', 'height');
            $response['thumb'] = compact('url', 'width', 'height');
        }
    }
    if (!in_array($type, array('image', 'audio', 'video'))) {
        //inject thumbnail
        $url = mpp_get_media_cover_src('thumbnail', $media->id);
        $width = 48;
        $height = 64;
        $response['image'] = compact('url', 'width', 'height');
        $response['thumb'] = compact('url', 'width', 'height');
    }
    return apply_filters('mpp_prepare_media_for_js', $response, $attachment, $meta);
}
Esempio n. 10
0
 /**
  * Builds the Playlist shortcode output.
  *
  * This implements the functionality of the playlist shortcode for
  * displaying a collection of WordPress audio or video files in a
  * post.
  *
  * This method is based on the WordPress core function
  * `wp_playlist_shortcode` but has been modified to add VTT data.
  *
  * @param array $attr {
  *     Array of default playlist attributes.
  *
  *     @type string  $type         Type of playlist to display. Accepts 'audio' or 'video'. Default 'audio'.
  *     @type string  $order        Designates ascending or descending order of items in the playlist.
  *                                 Accepts 'ASC', 'DESC'. Default 'ASC'.
  *     @type string  $orderby      Any column, or columns, to sort the playlist. If $ids are
  *                                 passed, this defaults to the order of the $ids array ('post__in').
  *                                 Otherwise default is 'menu_order ID'.
  *     @type int     $id           If an explicit $ids array is not present, this parameter
  *                                 will determine which attachments are used for the playlist.
  *                                 Default is the current post ID.
  *     @type array   $ids          Create a playlist out of these explicit attachment IDs. If empty,
  *                                 a playlist will be created from all $type attachments of $id.
  *                                 Default empty.
  *     @type array   $exclude      List of specific attachment IDs to exclude from the playlist. Default empty.
  *     @type string  $style        Playlist style to use. Accepts 'light' or 'dark'. Default 'light'.
  *     @type bool    $tracklist    Whether to show or hide the playlist. Default true.
  *     @type bool    $tracknumbers Whether to show or hide the numbers next to entries in the playlist. Default true.
  *     @type bool    $images       Show or hide the video or audio thumbnail (Featured Image/post
  *                                 thumbnail). Default true.
  *     @type bool    $artists      Whether to show or hide artist name in the playlist. Default true.
  * }
  * @return string Playlist output. Empty string if the passed type is unsupported.
  * @since 1.3.0
  * @global int $content_width
  * @staticvar int $instance
  * @see wp_playlist_shortcode
  */
 function wp_playlist_shortcode($attr)
 {
     static $instance = 0;
     $instance++;
     if (!empty($attr['ids'])) {
         $attr['include'] = $attr['ids'];
         // 'ids' is explicitly ordered, unless you specify otherwise.
         if (empty($attr['orderby'])) {
             $attr['orderby'] = 'post__in';
         }
     }
     $output = apply_filters('post_playlist', '', $attr, $instance);
     if ('' !== $output) {
         return $output;
     }
     unset($output);
     $atts = shortcode_atts(array('type' => 'audio', 'order' => 'ASC', 'orderby' => 'menu_order ID', 'id' => get_the_ID() ?: 0, 'include' => '', 'exclude' => '', 'style' => 'light', 'tracklist' => true, 'tracknumbers' => true, 'images' => true, 'artists' => true), $attr, 'playlist');
     if ('audio' !== $atts['type']) {
         $atts['type'] = 'video';
     }
     $args = array('post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => $atts['type'], 'order' => $atts['order'], 'orderby' => $atts['orderby']);
     if ('' !== $atts['include']) {
         $args['include'] = $atts['include'];
     } else {
         $args['post_parent'] = intval($atts['id']);
         if ('' !== $atts['exclude']) {
             $args['exclude'] = $atts['exclude'];
         }
     }
     if (!($attachments = get_posts($args))) {
         return '';
     }
     if (is_feed()) {
         ob_start();
         echo "\n";
         foreach ($attachments as $a) {
             echo wp_get_attachment_link($a->ID), "\n";
         }
         return ob_get_clean();
     }
     $outer = 22;
     // default padding and border of wrapper
     global $content_width;
     $default_width = 640;
     $default_height = 360;
     $theme_width = empty($content_width) ? $default_width : $content_width - $outer;
     $theme_height = empty($content_width) ? $default_height : round($default_height * $theme_width / $default_width);
     ob_start();
     if (1 === $instance) {
         do_action('wp_playlist_scripts', $atts['type'], $atts['style']);
     }
     echo '<div class="wp-playlist wp-', $atts['type'], '-playlist wp-playlist-', esc_attr($atts['style']), '">';
     if ('audio' === $atts['type']) {
         echo '<div class="wp-playlist-current-item"></div><audio';
     } else {
         echo '<video height="', $theme_height, '"';
     }
     echo ' controls="controls" preload="none" width="', $theme_width, '">', '</', $atts['type'], '>';
     echo '<div class="wp-playlist-next"></div>', '<div class="wp-playlist-prev"></div>', '<noscript>', '<ol>';
     $atts['images'] = wp_validate_boolean($atts['images']);
     $tracks = array();
     foreach ($attachments as $a) {
         // For <noscript>.
         echo '<li>', wp_get_attachment_link($a->ID), '</li>';
         // Remaining is for JSON data.
         $track = array('src' => wp_get_attachment_url($a->ID), 'title' => $a->post_title);
         if ($a->post_excerpt) {
             $track['caption'] = $a->post_excerpt;
         }
         if ($a->post_content) {
             $track['description'] = $a->post_content;
         }
         if ($meta = $this->get_tracks_json_data($track['src'])) {
             $track['webvtt'] = $meta;
         }
         if ($meta = wp_get_attachment_metadata($a->ID)) {
             foreach (wp_get_attachment_id3_keys($a) as $key => $label) {
                 if (!empty($meta[$key])) {
                     $track['meta'][$key] = $meta[$key];
                 }
             }
             if ('video' === $atts['type']) {
                 if (!empty($meta['width']) && !empty($meta['height'])) {
                     $width = $meta['width'];
                     $height = $meta['height'];
                     $theme_height = round($height * $theme_width / $width);
                 } else {
                     $width = $default_width;
                     $height = $default_height;
                 }
                 $track['dimensions'] = array('original' => compact('width', 'height'), 'resized' => array('width' => $theme_width, 'height' => $theme_height));
             }
         }
         if (true === $atts['images'] && ($thumb_id = get_post_thumbnail_id($a->ID))) {
             $track['image'] = wp_get_attachment_image_src($thumb_id, 'full');
             $track['thumb'] = wp_get_attachment_image_src($thumb_id);
         }
         $tracks[] = $track;
     }
     echo '</ol>', '</noscript>', '<script type="application/json" class="wp-playlist-script">', wp_json_encode(array('type' => $atts['type'], 'tracklist' => wp_validate_boolean($atts['tracklist']), 'tracknumbers' => wp_validate_boolean($atts['tracknumbers']), 'images' => $atts['images'], 'artists' => wp_validate_boolean($atts['artists']), 'tracks' => $tracks)), '</script>', '</div>';
     return ob_get_clean();
 }