本文整理汇总了PHP中wp_prepare_attachment_for_js函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_prepare_attachment_for_js函数的具体用法?PHP wp_prepare_attachment_for_js怎么用?PHP wp_prepare_attachment_for_js使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_prepare_attachment_for_js函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: ajax_get_image
/**
* 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;
}
示例2: get_image
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;
}
示例3: json
/**
* 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;
}
示例4: to_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);
}
}
}
示例5: blocks_get_gallery
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;
}
示例6: thumbnail
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 . '/>';
}
}
示例7: getAttachment
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']);
}
示例8: get_gallery_post
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;
}
示例9: get_attachment_data
/**
* 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;
}
示例10: get_image
/**
* 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;
}
示例11: get_featured_images
/**
* 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);
}
示例12: to_json
/**
* 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);
}
}
}
示例13: view
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;
}
示例14: element_shortcode_full
/**
* 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);
}
示例15: ajax_get_image
/**
* 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;
}