/** * Ajax handler to retrieve content from Resource space and add as attachment. */ function ajax_get_image() { $resource_id = intval($_POST['resource_id']); $parent_post_id = isset($_POST['post']) ? intval($_POST['post']) : 0; if (empty($resource_id)) { wp_send_json_error(esc_html__('Empty resource id', 'resourcespace')); } $url = PJ_RESOURCE_SPACE_DOMAIN . '/plugins/api_search/'; $key = PJ_RESOURCE_SPACE_KEY; $url = add_query_arg(array('key' => $key, 'search' => $resource_id, 'prettyfieldnames' => 1, 'previewsize' => 'pre', 'original' => true), $url); $request_args = array('headers' => array()); // Pass basic auth header if available. if (defined('PJ_RESOURCE_SPACE_AUTHL') && defined('PJ_RESOURCE_SPACE_AUTHP')) { $request_args['headers']['Authorization'] = 'Basic ' . base64_encode(PJ_RESOURCE_SPACE_AUTHL . ':' . PJ_RESOURCE_SPACE_AUTHP); } $response = wp_remote_get($url, $request_args); if (200 == wp_remote_retrieve_response_code($response)) { $data = json_decode(wp_remote_retrieve_body($response)); } else { wp_send_json_error(esc_html__('Unable to query API', 'resourcespace')); } if (count($data) < 1) { wp_send_json_error(esc_html__('Resource not found', 'resourcespace')); } // Request original URL. // $attachment_id = $this->sideload_image( $data[0]->original ); // Request preview size. $attachment_id = $this->sideload_image($data[0]->preview); if (is_wp_error($attachment_id)) { wp_send_json_error($attachment_id->get_error_message()); } else { wp_send_json_success(wp_prepare_attachment_for_js($attachment_id)); } exit; }
function get_image($image_id) { $attachment_data = wp_prepare_attachment_for_js($image_id); $image = wp_array_slice_assoc($attachment_data, array('sizes', 'caption', 'description')); if (empty($image)) { return false; } foreach ($this->sizes as $size) { $size_name = $size; if (!isset($image['sizes'][$size])) { $size = 'full'; $image['error'] = "wrong-size"; } // Format Data $image[$size_name]['img'] = $image['sizes'][$size]['url']; $image[$size_name]['width'] = $image['sizes'][$size]['width']; $image[$size_name]['height'] = $image['sizes'][$size]['height']; } $image['id'] = $image_id; $image['desc'] = $image['description']; unset($image['sizes'], $image['description']); if (!$this->has_descriptions && !empty($image['description'])) { $this->has_descriptions = true; } $image['desc'] = htmlspecialchars(str_replace('"', '"', $image['desc']), ENT_QUOTES); $image['caption'] = htmlspecialchars(str_replace('"', '"', $image['caption']), ENT_QUOTES); return $image; }
/** * Refresh the parameters passed to the JavaScript via JSON. * * @see WP_Fields_API_Control::to_json() */ public function json() { $json = parent::json(); $json['mime_type'] = $this->mime_type; $json['button_labels'] = $this->button_labels; $json['canUpload'] = current_user_can('upload_files'); $value = $this->value(); if (is_object($this->field)) { if ($this->field->default) { // Fake an attachment model - needs all fields used by template. // Note that the default value must be a URL, NOT an attachment ID. $type = 'document'; if (in_array(substr($this->field->default, -3), array('jpg', 'png', 'gif', 'bmp'))) { $type = 'image'; } $default_attachment = array('id' => 1, 'url' => $this->field->default, 'type' => $type, 'icon' => wp_mime_type_icon($type), 'title' => basename($this->field->default)); if ('image' === $type) { $default_attachment['sizes'] = array('full' => array('url' => $this->field->default)); } $json['defaultAttachment'] = $default_attachment; } if ($value && $this->field->default && $value === $this->field->default) { // Set the default as the attachment. $json['attachment'] = $json['defaultAttachment']; } elseif ($value) { $json['attachment'] = wp_prepare_attachment_for_js($value); } } return $json; }
/** * Refresh the parameters passed to the JavaScript via JSON. * * @see WP_Fields_API_Control::to_json() */ public function to_json() { parent::to_json(); $this->json['label'] = html_entity_decode($this->label, ENT_QUOTES, get_bloginfo('charset')); $this->json['mime_type'] = $this->mime_type; $this->json['button_labels'] = $this->button_labels; $this->json['canUpload'] = current_user_can('upload_files'); $value = $this->value(); if (is_object($this->setting)) { if ($this->setting->default) { // Fake an attachment model - needs all fields used by template. // Note that the default value must be a URL, NOT an attachment ID. $type = in_array(substr($this->setting->default, -3), array('jpg', 'png', 'gif', 'bmp')) ? 'image' : 'document'; $default_attachment = array('id' => 1, 'url' => $this->setting->default, 'type' => $type, 'icon' => wp_mime_type_icon($type), 'title' => basename($this->setting->default)); if ('image' === $type) { $default_attachment['sizes'] = array('full' => array('url' => $this->setting->default)); } $this->json['defaultAttachment'] = $default_attachment; } if ($value && $this->setting->default && $value === $this->setting->default) { // Set the default as the attachment. $this->json['attachment'] = $this->json['defaultAttachment']; } elseif ($value) { $this->json['attachment'] = wp_prepare_attachment_for_js($value); } } }
function blocks_get_gallery($post_id) { $ret = array('tags' => array(), 'images' => array()); $gallery = get_post_gallery($post_id, false); $gallery_ids = $gallery ? explode(',', $gallery['ids']) : array(); foreach ($gallery_ids as $id) { $thumb = wp_get_attachment_image_src($id, 'medium'); $image = wp_get_attachment_image_src($id, 'large'); if ($thumb && $image) { $image_data = array('tags' => array(), 'tags_string' => ''); $image_data['thumbnail'] = $thumb; $image_data['image'] = $image; $image_data['tags'] = wp_get_post_tags($id); foreach ($image_data['tags'] as $tag) { $ret['tags'][$tag->slug] = $tag->name; $image_data['tags_string'] .= ' ' . $tag->slug; } $data = wp_prepare_attachment_for_js($id); $image_data['title'] = $data['title']; $image_data['caption'] = $data['caption']; $image_data['alt'] = $data['alt']; $image_data['description'] = $data['description']; $image_data['link'] = get_post_meta($id, "_blocks_link", true); $ret['images'][] = $image_data; } } asort($ret['tags']); return $ret; }
function thumbnail($sizeW = 300, $sizeH = 300, $params = array(), $postid = null, $thumbnailid = null, $echo = true) { if ($postid == null) { $postid = get_the_ID(); } $post = get_post($postid); $thumbnailid = $thumbnailid == null ? get_post_thumbnail_id($post->ID) : $thumbnailid; $datas = wp_prepare_attachment_for_js($thumbnailid); // extract intersting data $params['alt'] = isset($params['alt']) ? $params['alt'] : $datas['alt']; $image_path = the_thumbnail($sizeW, $sizeH, $postid, $thumbnailid); foreach ($params as $k => $param) { if ($param == 'ORIGINAL-SRC') { $params[$k] = $datas['url']; } if ($param == 'SRC') { $params[$k] = $image_path; } } $params['src'] = isset($params['src']) ? $params['src'] : $image_path; $s = parse_to_html($params); if ($echo == true) { echo '<img' . $s . '/>'; } else { return '<img' . $s . '/>'; } }
public function getAttachment($attachmentId, $width = null, $height = null, $crop = false, $bookId = false) { $width = (int) $width; $height = (int) $height; $cachedUrl = $this->getAttachUrlCached($attachmentId, $width, $height, $crop, $bookId); if ($cachedUrl) { return $cachedUrl; } $attachment = wp_prepare_attachment_for_js($attachmentId); $filePath = $this->getFilePath($attachment['url']); // First try: // Trying to find image in wordpress sizes. if (!empty($attachment) && $attachment['sizes']) { foreach ($attachment['sizes'] as $size) { if ($width && $width === $size['width'] && ($height && $height === $size['height'])) { $this->saveAttachUrlCached($attachmentId, $width, $height, $crop, $bookId, $size['url']); return $size['url']; } } } // Second try // Trying to find cropped images. $filename = pathinfo($filePath, PATHINFO_FILENAME); $filename = $filename . '-' . $width . 'x' . $height . '.' . pathinfo($filePath, PATHINFO_EXTENSION); if (is_file($file = dirname($filePath) . '/' . $filename)) { $imgUrl = str_replace(ABSPATH, get_bloginfo('wpurl') . '/', $file); $this->saveAttachUrlCached($attachmentId, $width, $height, $crop, $bookId, $imgUrl); return str_replace(ABSPATH, get_bloginfo('wpurl') . '/', $file); } // Third and last try $editor = wp_get_image_editor($filePath); if (!has_filter('image_resize_dimensions', 'image_crop_dimensions')) { function image_crop_dimensions($default, $orig_w, $orig_h, $new_w, $new_h, $crop) { if (!$crop || !$new_w || !$new_h) { return null; } $size_ratio = max($new_w / $orig_w, $new_h / $orig_h); $crop_w = round($new_w / $size_ratio); $crop_h = round($new_h / $size_ratio); $s_x = floor(($orig_w - $crop_w) / 2); $s_y = floor(($orig_h - $crop_h) / 2); return array(0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h); } add_filter('image_resize_dimensions', 'image_crop_dimensions', 10, 6); } if (is_wp_error($editor) || is_wp_error($editor->resize($width, $height, (bool) $crop))) { $imgUrl = isset($attachment['sizes'], $attachment['sizes']['full'], $attachment['sizes']['full']['url']) ? $attachment['sizes']['full']['url'] : $attachment['url']; $this->saveAttachUrlCached($attachmentId, $width, $height, $crop, $bookId, $imgUrl); return $imgUrl; } if (is_wp_error($data = $editor->save())) { return $attachment['sizes']['full']['url']; } $editor = null; unset($editor); $imgUrl = str_replace(ABSPATH, get_bloginfo('wpurl') . '/', $data['path']); $this->saveAttachUrlCached($attachmentId, $width, $height, $crop, $bookId, $imgUrl); return str_replace(ABSPATH, get_bloginfo('wpurl') . '/', $data['path']); }
function get_gallery_post() { if (isset($_POST['id']) && !empty($_POST['id'])) { $projectsCats = array('cocheras', 'terrazas', 'habitaciones', 'antes-despues'); $id = $_POST['id']; $post = get_post($id); $gallery = get_post_gallery($post->ID, false); $category = get_the_category($post->ID); $cat_elements = array(); $cat_info = array(); foreach ($projectsCats as $cat) { $catdata = get_category_by_slug($cat); $args = array('category_name' => $cat, 'orderby' => 'meta_value_num', 'meta_key' => 'order', 'order' => 'ASC', 'posts_per_page' => 3); $projects = new WP_Query($args); array_push($cat_info, $catdata); array_push($cat_elements, $projects); } if ($gallery !== false) { $gallerydata = array(); $images = explode(',', $gallery['ids']); foreach ($images as $key => $image) { $imagedata = wp_prepare_attachment_for_js($image); array_push($gallerydata, $imagedata); } $post_info = array('post' => $post, 'category' => $category, 'categories' => $cat_elements, 'cat_info' => $cat_info, 'gallery' => $gallerydata); echo json_encode($post_info); } die; } else { echo 'error'; die; } die; }
/** * Returns an object with data for an attachment using * wp_prepare_attachment_for_js based on the provided id. * * @since 1.0 * @param string $id The attachment id. * @return object */ public static function get_attachment_data($id) { $data = wp_prepare_attachment_for_js($id); if (gettype($data) == 'array') { return json_decode(json_encode($data)); } return $data; }
/** * Return image markup * * carawebs_class_autoloader('Data'); * StudentStudio\Fetch\Data::image( 2301, 'thumbnail' ); * * @param [type] $image_ID [description] * @param string $image_size [description] * @return [type] [description] */ public static function get_image($image_ID, $image_size = 'full') { $image_object = wp_prepare_attachment_for_js($image_ID); $src = $image_object['sizes'][$image_size]['url']; $title = $image_object['title']; $height = $image_object['sizes'][$image_size]['height']; $width = $image_object['sizes'][$image_size]['width']; $alt = $image_object['alt']; $image = "<img src='{$src}' width='{$width}' height='{$height}' title='{$title}' class='img-responsive'/>"; return $image; }
/** * Returns an array of the featured image details * * @param int $postID The post ID * * @return array Array of info about the featured image */ public function get_featured_images($postID) { if (empty($postID)) { return FALSE; } $imageID = get_post_thumbnail_id($postID); if (empty($imageID)) { return FALSE; } return wp_prepare_attachment_for_js($imageID); }
/** * Refresh the parameters passed to the JavaScript via JSON. * * @since 3.4.0 * * @uses WP_Customize_Media_Control::to_json() */ public function to_json() { parent::to_json(); $value = $this->value(); if ($value) { // Get the attachment model for the existing file. $attachment_id = attachment_url_to_postid($value); if ($attachment_id) { $this->json['attachment'] = wp_prepare_attachment_for_js($attachment_id); } } }
function view() { $args = $this->definition; $args['default'] = $args['size']['default']; $args['attachment'] = null; unset($args['size']['default']); usort($args['size'], array(&$this, 'orderSizes')); if (!empty($this->value) && !empty($this->value->id)) { $args['attachment'] = json_encode(wp_prepare_attachment_for_js($this->value->id)); } $view = $this->propertyHelpers->getView($this->id, $this->groupId, $this->index, $args, json_encode($this->value)); return $view; }
/** * Generate HTML code from shortcode content. * * @param array $atts Shortcode attributes. * @param string $content Current content. * * @return string */ public function element_shortcode_full($atts = null, $content = null) { $arr_params = shortcode_atts($this->config['params'], $atts); extract($arr_params); $html_elemments = $script = ''; $image_styles = array(); if ($image_margin_top) { $image_styles[] = "margin-top:{$image_margin_top}px"; } if ($image_margin_bottom) { $image_styles[] = "margin-bottom:{$image_margin_bottom}px"; } if ($image_margin_right) { $image_styles[] = "margin-right:{$image_margin_right}px"; } if ($image_margin_left) { $image_styles[] = "margin-left:{$image_margin_left}px"; } $styles = count($image_styles) ? ' style="' . implode(';', $image_styles) . '"' : ''; if ($image_file) { $html_elemments .= "<div class='contact-img-wrapper {$image_container_style}'>"; $image_id = IG_Pb_Helper_Functions::get_image_id($image_file); $attachment = wp_prepare_attachment_for_js($image_id); $image_file = !empty($attachment['sizes'][$image_size]['url']) ? $attachment['sizes'][$image_size]['url'] : $image_file; $html_elemments .= "<img src='{$image_file}'{$alt_text}{$styles}{$class_img} />"; $script = ''; $target = ''; $sub_shortcode = IG_Pb_Helper_Shortcode::remove_autop($content); $items = explode('<!--seperate-->', $sub_shortcode); $items = array_filter($items); if ($items) { $buttons = "" . implode('', $items) . ''; $html_elemments .= "<div class='btns-wrapper'><div class='contact-btns'>" . $buttons . "</div><div class='vertical-helper'></div></div>"; } $html_elemments .= '</div>'; if (strtolower($image_alignment) != 'inherit') { if (strtolower($image_alignment) == 'left') { $cls_alignment = 'pull-left'; } if (strtolower($image_alignment) == 'right') { $cls_alignment = 'pull-right'; } if (strtolower($image_alignment) == 'center') { $cls_alignment = 'text-center'; } $html_elemments = "<div class='{$cls_alignment}'>" . $html_elemments . '</div>'; } } return $this->element_wrapper($html_elemments . $script, $arr_params); }
/** * Ajax handler to retrieve content from Resource space and add as attachment. */ function ajax_get_image() { $resource_id = intval($_POST['resource_id']); $parent_post_id = isset($_POST['post']) ? intval($_POST['post']) : 0; if (empty($resource_id)) { wp_send_json_error(esc_html__('Empty resource id', 'resourcespace')); add_filter('http_request_host_is_external', '__return_true'); } $args = array_map('rawurlencode', array('key' => PJ_RESOURCE_SPACE_KEY, 'search' => '!list' . $resource_id, 'prettyfieldnames' => false, 'original' => true, 'previewsize' => 'pre', 'metadata' => true)); $url = add_query_arg($args, PJ_RESOURCE_SPACE_DOMAIN . '/plugins/api_search/'); $request_args = array('headers' => array()); // Pass basic auth header if available. if (defined('PJ_RESOURCE_SPACE_AUTHL') && defined('PJ_RESOURCE_SPACE_AUTHP')) { $request_args['headers']['Authorization'] = 'Basic ' . base64_encode(PJ_RESOURCE_SPACE_AUTHL . ':' . PJ_RESOURCE_SPACE_AUTHP); } $response = wp_remote_get($url, $request_args); if (200 == wp_remote_retrieve_response_code($response)) { $data = json_decode(wp_remote_retrieve_body($response)); } else { wp_send_json_error(esc_html__('Unable to query API', 'resourcespace')); } if (count($data) < 1) { wp_send_json_error(esc_html__('Resource not found', 'resourcespace')); } // Request original URL. $attachment_id = wpcom_vip_download_image($data[0]->original_link); // Update the title to the actual title and content, not the filename $postarr = array('ID' => $attachment_id, 'post_title' => $data[0]->field8, 'post_content' => $data[0]->field18); wp_update_post($postarr); // Update post to show proper values in wp attachment views $post = array('ID' => $attachment_id, 'post_title' => isset($data[0]->field8) ? $data[0]->field8 : '', 'post_excerpt' => isset($data[0]->field18) ? $data[0]->field18 : ''); wp_update_post($post); // Update Metadata. update_post_meta($attachment_id, 'resource_space', 1); // Metadata for connecting resource between Wp and RS update_post_meta($attachment_id, 'resource_external_id', $data[0]->ref); // Allow plugins to hook in here. do_action('resourcespace_import_complete', $attachment_id, $data[0]); if (is_wp_error($attachment_id)) { wp_send_json_error($attachment_id->get_error_message()); } else { wp_send_json_success(wp_prepare_attachment_for_js($attachment_id)); } exit; }
function __construct($plugin_file_path, $aws) { parent::__construct($plugin_file_path); $this->aws = $aws; add_action('aws_admin_menu', array($this, 'admin_menu')); $this->plugin_title = __('Amazon S3 Uploader', 'wp_s3'); $this->plugin_menu_title = __('S3 Uploader', 'wp_s3'); add_action('wp_ajax_wp_s3-create-bucket', array($this, 'ajax_create_bucket')); add_filter('wp_get_attachment_url', array($this, 'wp_get_attachment_url'), 9, 2); //add_filter('wp_generate_attachment_metadata', array($this, 'wp_generate_attachment_metadata'), 20, 2); //add_filter('delete_attachment', array($this, 'delete_attachment'), 20); if (is_admin() && !empty($_FILES) && (!empty($_POST['html-upload']) || $_SERVER['REQUEST_URI'] == '/wp-admin/async-upload.php')) { $file = $_FILES['async-upload']; if (!is_array($file_info = $this->file_info($file))) { die('That file is not allowed: ' . $file_info . '. Hit back to try again.'); } $upload = $this->file_upload($file_info, intval($_POST['post_id'])); if ($_POST['action'] == 'upload-attachment') { $data = wp_prepare_attachment_for_js($upload['id']); if (substr($data['url'], 0, 5) == '/tmp/') { $data['url'] = $upload['file']; } if (substr($data['url'], 0, 2) == '//') { $data['url'] = 'http:' . $data['url']; } foreach ($upload['sizes'] as $key => $info) { $data['sizes'][$key]['url'] = $upload['prefix'] . $info['file']; } $data['sizes']['full']['url'] = $data['url']; die(json_encode(array('success' => true, 'data' => $data))); } if (empty($_POST['html-upload'])) { die('' . $data['id']); } header("Location: upload.php"); die(''); } }
/** * Refresh the parameters passed to the JavaScript via JSON. * * * highly based on WP_Customize_Media_Control merged with WP_Customize_Upload_Control ::to_json() */ public function to_json() { $this->json['mime_type'] = $this->mime_type; $this->json['button_labels'] = $this->button_labels; $this->json['bg_repeat_options'] = $this->bg_repeat_options; $this->json['bg_attachment_options'] = $this->bg_attachment_options; $this->json['bg_position_options'] = $this->bg_position_options; $this->json['canUpload'] = current_user_can('upload_files'); $this->json['default_model'] = $this->default_model; $value = $this->value(); if (isset($this->setting) && is_object($this->setting)) { $_defaults = isset($this->setting->default) ? $this->setting->default : null; $default_bg_img = isset($_defaults['background-image']) ? $_defaults['background-image'] : null; } $default_bg_im = isset($default_bg_img) ? $default_bg_img : null; if ($default_bg_img) { // Fake an attachment model - needs all fields used by template. // Note that the default value must be a URL, NOT an attachment ID. $type = in_array(substr($default_bg_img, -3), array('jpg', 'png', 'gif', 'bmp', 'svg')) ? 'image' : 'document'; $default_attachment = array('id' => 1, 'url' => $default_bg_img, 'type' => $type, 'icon' => wp_mime_type_icon($type), 'title' => basename($default_bg_img)); $default_attachment['sizes'] = array('full' => array('url' => $default_bg_img)); $this->json['defaultAttachment'] = $default_attachment; } $background_image = isset($value['background-image']) ? $value['background-image'] : null; if ($background_image && $default_bg_img && $background_image === $default_bg_img) { // Set the default as the attachment. $this->json['attachment'] = $this->json['defaultAttachment']; } elseif ($background_image) { $attachment_id = attachment_url_to_postid($background_image); if ($attachment_id) { $this->json['attachment'] = wp_prepare_attachment_for_js($attachment_id); } else { //already an id $this->json['attachment'] = wp_prepare_attachment_for_js($background_image); } } parent::to_json(); }
/** * Refresh the parameters passed to the JavaScript via JSON. * * * @Override * @see WP_Customize_Control::to_json() */ public function to_json() { parent::to_json(); $this->json['title'] = !empty($this->title) ? esc_html($this->title) : ''; $this->json['notice'] = !empty($this->notice) ? $this->notice : ''; $this->json['dst_width'] = isset($this->dst_width) ? $this->dst_width : $this->width; $this->json['dst_height'] = isset($this->dst_height) ? $this->dst_height : $this->height; //overload WP_Customize_Upload_Control //we need to re-build the absolute url of the logo src set in old Customizr $value = $this->value(); if ($value) { //re-build the absolute url if the value isn't an attachment id before retrieving the id if ((int) esc_attr($value) < 1) { $upload_dir = wp_upload_dir(); $value = false !== strpos($value, '/wp-content/') ? $value : $upload_dir['baseurl'] . $value; } // Get the attachment model for the existing file. $attachment_id = attachment_url_to_postid($value); if ($attachment_id) { $this->json['attachment'] = wp_prepare_attachment_for_js($attachment_id); } } //end overload }
/** * AJAX handler for cropping an image. * * @since 4.3.0 * * @global WP_Site_Icon $wp_site_icon */ function wp_ajax_crop_image() { $attachment_id = absint($_POST['id']); check_ajax_referer('image_editor-' . $attachment_id, 'nonce'); if (!current_user_can('customize')) { wp_send_json_error(); } $context = str_replace('_', '-', $_POST['context']); $data = array_map('absint', $_POST['cropDetails']); $cropped = wp_crop_image($attachment_id, $data['x1'], $data['y1'], $data['width'], $data['height'], $data['dst_width'], $data['dst_height']); if (!$cropped || is_wp_error($cropped)) { wp_send_json_error(array('message' => __('Image could not be processed.'))); } switch ($context) { case 'site-icon': require_once ABSPATH . '/wp-admin/includes/class-wp-site-icon.php'; global $wp_site_icon; // Skip creating a new attachment if the attachment is a Site Icon. if (get_post_meta($attachment_id, '_wp_attachment_context', true) == $context) { // Delete the temporary cropped file, we don't need it. wp_delete_file($cropped); // Additional sizes in wp_prepare_attachment_for_js(). add_filter('image_size_names_choose', array($wp_site_icon, 'additional_sizes')); break; } /** This filter is documented in wp-admin/custom-header.php */ $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $attachment_id); // For replication. $object = $wp_site_icon->create_attachment_object($cropped, $attachment_id); unset($object['ID']); // Update the attachment. add_filter('intermediate_image_sizes_advanced', array($wp_site_icon, 'additional_sizes')); $attachment_id = $wp_site_icon->insert_attachment($object, $cropped); remove_filter('intermediate_image_sizes_advanced', array($wp_site_icon, 'additional_sizes')); // Additional sizes in wp_prepare_attachment_for_js(). add_filter('image_size_names_choose', array($wp_site_icon, 'additional_sizes')); break; default: /** * Fires before a cropped image is saved. * * Allows to add filters to modify the way a cropped image is saved. * * @since 4.3.0 * * @param string $context The Customizer control requesting the cropped image. * @param int $attachment_id The attachment ID of the original image. * @param string $cropped Path to the cropped image file. */ do_action('wp_ajax_crop_image_pre_save', $context, $attachment_id, $cropped); /** This filter is documented in wp-admin/custom-header.php */ $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $attachment_id); // For replication. $parent_url = wp_get_attachment_url($attachment_id); $url = str_replace(basename($parent_url), basename($cropped), $parent_url); $size = @getimagesize($cropped); $image_type = $size ? $size['mime'] : 'image/jpeg'; $object = array('post_title' => basename($cropped), 'post_content' => $url, 'post_mime_type' => $image_type, 'guid' => $url, 'context' => $context); $attachment_id = wp_insert_attachment($object, $cropped); $metadata = wp_generate_attachment_metadata($attachment_id, $cropped); /** * Filter the cropped image attachment metadata. * * @since 4.3.0 * * @see wp_generate_attachment_metadata() * * @param array $metadata Attachment metadata. */ $metadata = apply_filters('wp_ajax_cropped_attachment_metadata', $metadata); wp_update_attachment_metadata($attachment_id, $metadata); /** * Filter the attachment ID for a cropped image. * * @since 4.3.0 * * @param int $attachment_id The attachment ID of the cropped image. * @param string $context The Customizer control requesting the cropped image. */ $attachment_id = apply_filters('wp_ajax_cropped_attachment_id', $attachment_id, $context); } wp_send_json_success(wp_prepare_attachment_for_js($attachment_id)); }
public function getAttachment($attachment_id) { return wp_prepare_attachment_for_js($attachment_id); }
/** * Save backwards compatible attachment attributes. * * @since 3.5.0 */ function wp_ajax_save_attachment_compat() { if (!isset($_REQUEST['id'])) { wp_send_json_error(); } if (!($id = absint($_REQUEST['id']))) { wp_send_json_error(); } if (empty($_REQUEST['attachments']) || empty($_REQUEST['attachments'][$id])) { wp_send_json_error(); } $attachment_data = $_REQUEST['attachments'][$id]; check_ajax_referer('update-post_' . $id, 'nonce'); if (!current_user_can('edit_post', $id)) { wp_send_json_error(); } $post = get_post($id, ARRAY_A); if ('attachment' != $post['post_type']) { wp_send_json_error(); } /** This filter is documented in wp-admin/includes/media.php */ $post = apply_filters('attachment_fields_to_save', $post, $attachment_data); if (isset($post['errors'])) { $errors = $post['errors']; // @todo return me and display me! unset($post['errors']); } wp_update_post($post); foreach (get_attachment_taxonomies($post) as $taxonomy) { if (isset($attachment_data[$taxonomy])) { wp_set_object_terms($id, array_map('trim', preg_split('/,+/', $attachment_data[$taxonomy])), $taxonomy, false); } } if (!($attachment = wp_prepare_attachment_for_js($id))) { wp_send_json_error(); } wp_send_json_success($attachment); }
function folioslider_func($atts, $content = null) { extract(shortcode_atts(array('gallery' => ''), $atts)); ob_start(); ?> <div class="project"> <ul class="project-slides"> <?php $img_ids = explode(",", $gallery); foreach ($img_ids as $img_id) { $meta = wp_prepare_attachment_for_js($img_id); $caption = $meta['caption']; $title = $meta['title']; $description = $meta['description']; $image_src = wp_get_attachment_image_src($img_id, ''); ?> <li class="slide"><img src="<?php echo esc_url($image_src[0]); ?> " alt=""></li> <?php } ?> </ul> </div> <?php return ob_get_clean(); }
function render_attachment($id = 0, $field) { // vars $attachment = wp_prepare_attachment_for_js($id); $thumb = ''; $prefix = "attachments[{$id}]"; $compat = get_compat_media_markup($id); $dimentions = ''; // thumb if (isset($attachment['thumb']['src'])) { // video $thumb = $attachment['thumb']['src']; } elseif (isset($attachment['sizes']['thumbnail']['url'])) { // image $thumb = $attachment['sizes']['thumbnail']['url']; } elseif ($attachment['type'] === 'image') { // svg $thumb = $attachment['url']; } else { // fallback (perhaps attachment does not exist) $thumb = $attachment['icon']; } // dimentions if ($attachment['type'] === 'audio') { $dimentions = __('Length', 'acf') . ': ' . $attachment['fileLength']; } elseif (!empty($attachment['width'])) { $dimentions = $attachment['width'] . ' x ' . $attachment['height']; } if ($attachment['filesizeHumanReadable']) { $dimentions .= ' (' . $attachment['filesizeHumanReadable'] . ')'; } ?> <div class="acf-gallery-side-info acf-cf"> <img src="<?php echo $thumb; ?> " alt="<?php echo $attachment['alt']; ?> " /> <p class="filename"><strong><?php echo $attachment['filename']; ?> </strong></p> <p class="uploaded"><?php echo $attachment['dateFormatted']; ?> </p> <p class="dimensions"><?php echo $dimentions; ?> </p> <p class="actions"><a href="#" class="edit-attachment" data-id="<?php echo $id; ?> "><?php _e('Edit', 'acf'); ?> </a> <a href="#" class="remove-attachment" data-id="<?php echo $id; ?> "><?php _e('Remove', 'acf'); ?> </a></p> </div> <table class="form-table"> <tbody> <?php acf_render_field_wrap(array('name' => 'title', 'prefix' => $prefix, 'type' => 'text', 'label' => 'Title', 'value' => $attachment['title']), 'tr'); acf_render_field_wrap(array('name' => 'caption', 'prefix' => $prefix, 'type' => 'textarea', 'label' => 'Caption', 'value' => $attachment['caption']), 'tr'); acf_render_field_wrap(array('name' => 'alt', 'prefix' => $prefix, 'type' => 'text', 'label' => 'Alt Text', 'value' => $attachment['alt']), 'tr'); acf_render_field_wrap(array('name' => 'description', 'prefix' => $prefix, 'type' => 'textarea', 'label' => 'Description', 'value' => $attachment['description']), 'tr'); ?> </tbody> </table> <?php echo $compat['item']; ?> <?php }
function test_wp_prepare_attachment_for_js() { // Attachment without media $id = wp_insert_attachment(array( 'post_status' => 'publish', 'post_title' => 'Prepare', 'post_content_filtered' => 'Prepare', 'post_type' => 'post' )); $post = get_post( $id ); $prepped = wp_prepare_attachment_for_js( $post ); $this->assertInternalType( 'array', $prepped ); $this->assertEquals( 0, $prepped['uploadedTo'] ); $this->assertEquals( '', $prepped['mime'] ); $this->assertEquals( '', $prepped['type'] ); $this->assertEquals( '', $prepped['subtype'] ); // #21963, there will be a guid always, so there will be a URL $this->assertNotEquals( '', $prepped['url'] ); $this->assertEquals( site_url( 'wp-includes/images/media/default.png' ), $prepped['icon'] ); // Fake a mime $post->post_mime_type = 'image/jpeg'; $prepped = wp_prepare_attachment_for_js( $post ); $this->assertEquals( 'image/jpeg', $prepped['mime'] ); $this->assertEquals( 'image', $prepped['type'] ); $this->assertEquals( 'jpeg', $prepped['subtype'] ); // Fake a mime without a slash. See #WP22532 $post->post_mime_type = 'image'; $prepped = wp_prepare_attachment_for_js( $post ); $this->assertEquals( 'image', $prepped['mime'] ); $this->assertEquals( 'image', $prepped['type'] ); $this->assertEquals( '', $prepped['subtype'] ); }
public function wa_create_image() { if (wp_verify_nonce($_POST['wa_fronted_save_nonce'], 'wa_fronted_save_nonce') && isset($_POST['file_data']) && isset($_POST['file_name']) && isset($_POST['file_type'])) { global $post; $file_data = rawurldecode($_POST['file_data']); $base64str = preg_replace('/(data:image\\/(jpeg|png|gif);base64,)/i', '', $file_data); $upload_dir = wp_upload_dir(); $upload_path = str_replace('/', DIRECTORY_SEPARATOR, $upload_dir['path']) . DIRECTORY_SEPARATOR; $decoded = base64_decode($base64str); $filename = $_POST['file_name']; $hashed_filename = md5($filename . microtime()) . '_' . $filename; $image_upload = file_put_contents($upload_path . $hashed_filename, $decoded); $post_id = isset($_POST['post_id']) ? $_POST['post_id'] : $post->ID; //HANDLE UPLOADED FILE if (!function_exists('wp_handle_sideload')) { require_once ABSPATH . 'wp-admin/includes/file.php'; } // Without that I'm getting a debug error!? if (!function_exists('wp_get_current_user')) { require_once ABSPATH . 'wp-includes/pluggable.php'; } $file = array(); $file['error'] = ''; $file['tmp_name'] = $upload_path . $hashed_filename; $file['name'] = $hashed_filename; $file['type'] = $_POST['file_type']; $file['size'] = filesize($upload_path . $hashed_filename); // upload file to server $attachment_id = media_handle_sideload($file, $post_id); wp_send_json(array('attachment_id' => $attachment_id, 'attachment_obj' => wp_prepare_attachment_for_js($attachment_id))); } else { return false; } }
"> <div class="item <?php echo $full_width_class; ?> "> <?php global $post; ?> <a class="zoom" data-rel="prettyPhoto[gal]" href="<?php echo ci_get_image_src($post->ID, 'large'); ?> "> <figure class="item-thumb"> <?php $attachment = wp_prepare_attachment_for_js($post->ID); ?> <img src="<?php echo ci_get_image_src($post->ID, $thumb_size); ?> " alt="<?php echo esc_attr($attachment['alt']); ?> "> </figure> </a> <?php if ($caption) { ?> <div class="item-info"> <p class="item-byline"><?php
/** * Get Attachment Meta * * Returns the attachment meta field. * Use to get title, caption, description * Or anything else listed here: * https://codex.wordpress.org/Function_Reference/wp_prepare_attachment_for_js * * @param int $post_id * @param int $field * @uses wp_prepare_attachment_for_js(); * @return string field (id, caption, title, description, etc) * @since 2.0.4 */ function sell_media_get_attachment_meta($post_id = null, $field = 'id') { if (sell_media_has_multiple_attachments($post_id)) { $attachment_id = get_query_var('id'); } else { $attachments = sell_media_get_attachments($post_id); $attachment_id = $attachments[0]; } $attachment_meta = wp_prepare_attachment_for_js($attachment_id); return $attachment_meta[$field]; }
function optimizer_image_alt($attachment) { $imgid = optimizer_attachment_id_by_url($attachment); if ($imgid) { $imgaltraw = wp_prepare_attachment_for_js($imgid); $imgalt = $imgaltraw['alt']; if (!empty($imgalt)) { $imgalt = 'alt="' . $imgaltraw['alt'] . '"'; } } else { $imgalt = ''; } return $imgalt; }
/** * Add custom parameters to pass to the JS via JSON. * * @since 1.0.0 * @access public * @return void */ public function to_json() { parent::to_json(); $background_choices = $this->background_choices; $field_labels = $this->field_labels; // Loop through each of the settings and set up the data for it. foreach ($this->settings as $setting_key => $setting_id) { $this->json[$setting_key] = array('link' => $this->get_link($setting_key), 'value' => $this->value($setting_key), 'label' => isset($field_labels[$setting_key]) ? $field_labels[$setting_key] : ''); if ('image_url' === $setting_key) { if ($this->value($setting_key)) { // Get the attachment model for the existing file. $attachment_id = attachment_url_to_postid($this->value($setting_key)); if ($attachment_id) { $this->json['attachment'] = wp_prepare_attachment_for_js($attachment_id); } } } elseif ('repeat' === $setting_key) { $this->json[$setting_key]['choices'] = $background_choices['repeat']; } elseif ('size' === $setting_key) { $this->json[$setting_key]['choices'] = $background_choices['size']; } elseif ('position' === $setting_key) { $this->json[$setting_key]['choices'] = $background_choices['position']; } elseif ('attach' === $setting_key) { $this->json[$setting_key]['choices'] = $background_choices['attach']; } } }
public static function getExtendedData($id) { if ($id != '' && is_numeric($id)) { return wp_prepare_attachment_for_js($id); } }