/** * @ticket 36578 */ public function test_wp_ajax_send_attachment_to_editor_should_return_an_image() { // Become an administrator $post = $_POST; $user_id = self::factory()->user->create(array('role' => 'administrator', 'user_login' => 'user_36578_administrator', 'user_email' => '*****@*****.**')); wp_set_current_user($user_id); $_POST = array_merge($_POST, $post); $filename = DIR_TESTDATA . '/images/canola.jpg'; $contents = file_get_contents($filename); $upload = wp_upload_bits(basename($filename), null, $contents); $attachment = $this->_make_attachment($upload); // Set up a default request $_POST['nonce'] = wp_create_nonce('media-send-to-editor'); $_POST['html'] = 'Bar Baz'; $_POST['post_id'] = 0; $_POST['attachment'] = array('id' => $attachment, 'align' => 'left', 'image-size' => 'large', 'image_alt' => 'Foo bar', 'url' => 'http://example.com/'); // Make the request try { $this->_handleAjax('send-attachment-to-editor'); } catch (WPAjaxDieContinueException $e) { unset($e); } // Get the response. $response = json_decode($this->_last_response, true); $expected = get_image_send_to_editor($attachment, '', '', 'left', 'http://example.com/', false, 'large', 'Foo bar'); // Ensure everything is correct $this->assertTrue($response['success']); $this->assertEquals($expected, $response['data']); }
function network_shared_media_media_upload() { global $blog_id, $post; if (isset($_POST['send'])) { $nsm_blog_id = (int) $_GET['blog_id']; reset($_POST['send']); $nsm_send_id = (int) key($_POST['send']); } /* copied from wp-admin/inculdes/ajax-actions.php wp_ajax_send_attachment_to_editor() */ if (isset($nsm_blog_id) && isset($nsm_send_id)) { switch_to_blog($nsm_blog_id); if (!current_user_can('upload_files')) { $current_blog_name = get_bloginfo('name'); restore_current_blog(); wp_die(__('You do not have permission to upload files to site: ') . $current_blog_name); } $attachment = wp_unslash($_POST['attachments'][$nsm_send_id]); $id = $nsm_send_id; if (!($post = get_post($id))) { wp_send_json_error(); } if ('attachment' != $post->post_type) { wp_send_json_error(); } $rel = $url = ''; $html = $title = isset($attachment['post_title']) ? $attachment['post_title'] : ''; if (!empty($attachment['url'])) { $url = $attachment['url']; if (strpos($url, 'attachment_id') || get_attachment_link($id) == $url) { $rel = ' rel="attachment wp-att-' . $id . '"'; } $html = '<a href="' . esc_url($url) . '"' . $rel . '>' . $html . '</a>'; } if ('image' === substr($post->post_mime_type, 0, 5)) { $align = isset($attachment['align']) ? $attachment['align'] : 'none'; $size = isset($attachment['image-size']) ? $attachment['image-size'] : 'medium'; $alt = isset($attachment['image_alt']) ? $attachment['image_alt'] : ''; $caption = isset($attachment['post_excerpt']) ? $attachment['post_excerpt'] : ''; $title = ''; // We no longer insert title tags into <img> tags, as they are redundant. $html = get_image_send_to_editor($id, $caption, $title, $align, $url, (bool) $rel, $size, $alt); } elseif ('video' === substr($post->post_mime_type, 0, 5) || 'audio' === substr($post->post_mime_type, 0, 5)) { global $wp_embed; $meta = get_post_meta($id, '_wp_attachment_metadata', true); $html = $wp_embed->shortcode($meta, $url); } /** This filter is documented in wp-admin/includes/media.php */ $html = apply_filters('media_send_to_editor', $html, $id, $attachment); // replace wp-image-<id>, wp-att-<id> and attachment_<id> $html = preg_replace(array('#(caption id="attachment_)(\\d+")#', '#(wp-image-|wp-att-)(\\d+)#'), array(sprintf('${1}nsm_%s_${2}', esc_attr($nsm_blog_id)), sprintf('${1}nsm-%s-${2}', esc_attr($nsm_blog_id))), $html); if (isset($_POST['chromeless']) && $_POST['chromeless']) { // WP3.5+ media browser is identified by the 'chromeless' parameter exit($html); } else { return media_send_to_editor($html); } } }
function mfrh_media_send_to_editor($html, $attachment_id, $attachment) { $post =& get_post($attachment_id); if (substr($post->post_mime_type, 0, 5) == 'image') { $url = wp_get_attachment_url($attachment_id); $align = !empty($attachment['align']) ? $attachment['align'] : 'none'; $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium'; $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : ''; $rel = $url == get_attachment_link($attachment_id); return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt); } return $html; }
/** * Ajax handler for sending an attachment to the editor. * * Generates the HTML to send an attachment to the editor. * Backwards compatible with the media_send_to_editor filter * and the chain of filters that follow. * * @since 3.5.0 */ function wp_ajax_send_attachment_to_editor() { check_ajax_referer('media-send-to-editor', 'nonce'); $attachment = wp_unslash($_POST['attachment']); $id = intval($attachment['id']); if (!($post = get_post($id))) { wp_send_json_error(); } if ('attachment' != $post->post_type) { wp_send_json_error(); } if (current_user_can('edit_post', $id)) { // If this attachment is unattached, attach it. Primarily a back compat thing. if (0 == $post->post_parent && ($insert_into_post_id = intval($_POST['post_id']))) { wp_update_post(array('ID' => $id, 'post_parent' => $insert_into_post_id)); } } $rel = ''; $url = empty($attachment['url']) ? '' : $attachment['url']; if (strpos($url, 'attachment_id') || get_attachment_link($id) == $url) { $rel = ' rel="attachment wp-att-' . $id . '"'; } remove_filter('media_send_to_editor', 'image_media_send_to_editor'); if ('image' === substr($post->post_mime_type, 0, 5)) { $align = isset($attachment['align']) ? $attachment['align'] : 'none'; $size = isset($attachment['image-size']) ? $attachment['image-size'] : 'medium'; $alt = isset($attachment['image_alt']) ? $attachment['image_alt'] : ''; // No whitespace-only captions. $caption = isset($attachment['post_excerpt']) ? $attachment['post_excerpt'] : ''; if ('' === trim($caption)) { $caption = ''; } $title = ''; // We no longer insert title tags into <img> tags, as they are redundant. $html = get_image_send_to_editor($id, $caption, $title, $align, $url, $rel, $size, $alt); } elseif (wp_attachment_is('video', $post) || wp_attachment_is('audio', $post)) { $html = stripslashes_deep($_POST['html']); } else { $html = isset($attachment['post_title']) ? $attachment['post_title'] : ''; if (!empty($url)) { $html = '<a href="' . esc_url($url) . '"' . $rel . '>' . $html . '</a>'; } } /** This filter is documented in wp-admin/includes/media.php */ $html = apply_filters('media_send_to_editor', $html, $id, $attachment); wp_send_json_success($html); }
/** * {@internal Missing Short Description}} * * @since unknown * * @param unknown_type $html * @param unknown_type $attachment_id * @param unknown_type $attachment * @return unknown */ function image_media_send_to_editor($html, $attachment_id, $attachment) { $post =& get_post($attachment_id); if (substr($post->post_mime_type, 0, 5) == 'image') { $url = $attachment['url']; if (isset($attachment['align'])) { $align = $attachment['align']; } else { $align = 'none'; } if (!empty($attachment['image-size'])) { $size = $attachment['image-size']; } else { $size = 'medium'; } $rel = $url == get_attachment_link($attachment_id); return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size); } return $html; }
function handle_sideload($post) { if ( empty( $post['format'] ) ) return; // Nothing to import. switch ( $post['format'] ) { case 'gallery': if ( !empty( $post['gallery'] ) ) { foreach ( $post['gallery'] as $i => $photo ) { $id = $this->handle_sideload_import( $post, (string)$photo['src'], (string)$photo['caption']); if ( is_wp_error($id) ) return $id; } $post['post_content'] = "[gallery]\n" . $post['post_content']; $post = apply_filters( 'tumblr_importer_format_post', $post ); do_action( 'tumblr_importer_metadata', $post ); wp_update_post($post); break; // If we processed a gallery, break, otherwise let it fall through to the Image handler } case 'image': if ( isset( $post['media']['src'] ) ) { $id = $this->handle_sideload_import( $post, (string)$post['media']['src'], (string)$post['post_title']); if ( is_wp_error($id) ) return $id; $link = !empty($post['media']['link']) ? $post['media']['link'] : null; // image_send_to_editor has a filter to wrap in a shortcode. $post_content = $post['post_content']; $post['post_content'] = get_image_send_to_editor($id, (string)$post['post_title'], (string)$post['post_title'], 'none', $link, true, 'full' ); $post['post_content'] .= $post_content; $post['meta']['attribution'] = $link; $post = apply_filters( 'tumblr_importer_format_post', $post ); do_action( 'tumblr_importer_metadata', $post ); //$post['post_content'] .= "\n" . $post['post_content']; // the [caption] shortcode doesn't allow HTML, but this might have some extra markup wp_update_post($post); } break; case 'audio': // Handle Tumblr Hosted Audio if ( isset( $post['media']['audio'] ) ) { $id = $this->handle_sideload_import( $post, (string)$post['media']['audio'], $post['post_title'], (string)$post['media']['filename'] ); if ( is_wp_error($id) ) return $id; $post['post_content'] = wp_get_attachment_link($id) . "\n" . $post['post_content']; } else { // Try to work out a "source" link to display Tumblr-style. preg_match( '/(http[^ "<>\']+)/', $post['post_content'], $matches ); if ( isset( $matches[1] ) ) { $url_parts = parse_url( $matches[1] ); $post['meta']['attribution'] = $url_parts['scheme'] . "://" . $url_parts['host'] . "/"; } } $post = apply_filters( 'tumblr_importer_format_post', $post ); do_action( 'tumblr_importer_metadata', $post ); wp_update_post($post); break; case 'video': // Handle Tumblr hosted video if ( isset( $post['media']['video'] ) ) { $id = $this->handle_sideload_import( $post, (string)$post['media']['video'], $post['post_title'], (string)$post['media']['filename'] ); if ( is_wp_error($id) ) return $id; // @TODO: Check/change this to embed the imported video. $link = wp_get_attachment_link($id) . "\n" . $post['post_content']; $post['post_content'] = $link; $post['meta']['attribution'] = $link; } else { // Try to work out a "source" link to mimic Tumblr's post formatting. preg_match( '/(http[^ "<>\']+)/', $post['post_content'], $matches ); if ( isset( $matches[1] ) ) { $url_parts = parse_url( $matches[1] ); $post['meta']['attribution'] = $url_parts['scheme'] . "://" . $url_parts['host'] . "/"; } } $post = apply_filters( 'tumblr_importer_format_post', $post ); do_action( 'tumblr_importer_metadata', $post ); wp_update_post($post); // Else, Check to see if the url embedded is handled by oEmbed (or not) break; } return true; // all processed }
/** * @ticket 36084 */ function test_get_image_send_to_editor_defaults_no_caption_no_rel() { $id = self::$large_id; $caption = ''; $title = 'A test title value.'; $align = 'left'; $url = get_permalink($id); $rel = ''; $size = 'thumbnail'; $alt = 'An example alt value.'; // Calculate attachment data. $attachment = wp_get_attachment_image_src($id, $size); $html = '<a href="%1$s"><img src="%2$s" alt="%3$s" width="%4$d" height="%5$d" class="align%6$s size-%7$s wp-image-%8$d" /></a>'; $expected = sprintf($html, $url, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id); $this->assertSame($expected, get_image_send_to_editor($id, $caption, $title, $align, $url, $rel, $size, $alt)); }
/** * Generates the HTML to send an attachment to the editor. * Backwards compatible with the media_send_to_editor filter and the chain * of filters that follow. * * @since 3.5.0 */ function wp_ajax_send_attachment_to_editor() { check_ajax_referer('media-send-to-editor', 'nonce'); $attachment = stripslashes_deep($_POST['attachment']); $id = intval($attachment['id']); if (!($post = get_post($id))) { wp_send_json_error(); } if (!current_user_can('edit_post', $id)) { wp_send_json_error(); } if ('attachment' != $post->post_type) { wp_send_json_error(); } // If this attachment is unattached, attach it. Primarily a back compat thing. if (0 == $post->post_parent && ($insert_into_post_id = intval($_POST['post_id']))) { wp_update_post(array('ID' => $id, 'post_parent' => $insert_into_post_id)); } $rel = $url = ''; $html = $title = isset($attachment['post_title']) ? $attachment['post_title'] : ''; if (!empty($attachment['url'])) { $url = $attachment['url']; if (strpos($url, 'attachment_id') || get_attachment_link($id) == $url) { $rel = ' rel="attachment wp-att-' . $id . '"'; } $html = '<a href="' . esc_url($url) . '"' . $rel . '>' . $html . '</a>'; } remove_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3); if ('image' === substr($post->post_mime_type, 0, 5)) { $align = isset($attachment['align']) ? $attachment['align'] : 'none'; $size = isset($attachment['image-size']) ? $attachment['image-size'] : 'medium'; $alt = isset($attachment['image_alt']) ? $attachment['image_alt'] : ''; $caption = isset($attachment['post_excerpt']) ? $attachment['post_excerpt'] : ''; $title = ''; // We no longer insert title tags into <img> tags, as they are redundant. $html = get_image_send_to_editor($id, $caption, $title, $align, $url, (bool) $rel, $size, $alt); } $html = apply_filters('media_send_to_editor', $html, $id, $attachment); wp_send_json_success($html); }
function dwtp_attach_images($post_array) { global $image_settings; $post_id = $post_array['ID']; $post_content = $post_array['post_content']; if (empty($post_id)) { gdocs_log("Post ID was null when trying to insert images", "error"); return false; } $attached_guids = array(); if (!empty($post_id) && ($images = get_posts(array('post_parent' => $post_id, 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC')))) { foreach ($images as $image) { $attached_images[] = get_image_send_to_editor($image->ID, '', $image->post_title, $image_settings['image_alignment'], wp_get_attachment_url($image->ID), FALSE, $image_settings['image_size'], $image->post_content); if (preg_match('/gdocs.{10}_/', $image->guid, $guid_match) > 0) { //match guids with the gdocs id inserted $attached_guids[] = $guid_match[0]; //for identifying gdocs images added since before } else { $attached_guids[] = $image->guid; } } } //MF preg_match_all('/<img(.*?)>/', $post_content, $doc_imgs, PREG_OFFSET_CAPTURE); gdocs_log("Image GUIDs: " . implode($attached_guids), "debug"); $upload_dir = wp_upload_dir(); $path = $upload_dir['path'] . '/' . $image_settings['gdocs_image_folder']; //assumed subdir of upload if (!file_exists($path)) { mkdir($path); } $replace_offset = 0; foreach ($doc_imgs[0] as $doc_img) { //$doc_imgs[0] because we only need first pattern, not the subpattern preg_match('/src="https(.*?)"/', $doc_img[0], $src_match); //Use doc_img[0] as we also match position in index [1] $img_hash = 'gdocs' . substr($src_match[1], -10) . '_'; // Pick file name hash from last 10 of gdocs user content hash (hope it's static!) $new_img_tag = ''; $existing_img = array_search($img_hash, $attached_guids); // see if we already have this img as attachment if ($existing_img === FALSE) { // Gdocs defaults to HTTPSing their images, but we can go with normal http for simplicity $headers = wp_get_http('http' . $src_match[1], $path . $img_hash); $contentdisposition = explode('"', $headers["content-disposition"]); //pattern is: inline;filename="xxx.jpg" if (count($contentdisposition) > 1) { $filename = urldecode($contentdisposition[1]); // filename may include URL characters that mess up later $file_ext = strripos($filename, '.'); // look for extension from end of string $name = $file_ext > 0 ? substr($filename, 0, $file_ext) : $filename; //strip out file type from name } else { $filename = 'unknown.jpg'; $name = "unknown"; } $filename = $img_hash . $filename; // for uniqueness combine with hash $newpath = $path . $filename; rename($path . $img_hash, $newpath); $wp_filetype = wp_check_filetype(basename($newpath), null); $attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => $name, 'post_name' => $name, 'guid' => $upload_dir['baseurl'] . '/gdocs/' . $filename, 'post_content' => '', 'post_status' => 'inherit'); $attach_id = wp_insert_attachment($attachment, $newpath, $post_id); $attach_data = wp_generate_attachment_metadata($attach_id, $newpath); wp_update_attachment_metadata($attach_id, $attach_data); gdocs_log("Inserted attachment " . implode($attachment) . " to post {$post_id} and generated metadata:", "debug"); $new_img_tag = get_image_send_to_editor($attach_id, '', $name, $image_settings['image_alignment'], wp_get_attachment_url($image->ID), FALSE, $image_settings['image_size'], ''); gdocs_log("Downloaded https" . $src_match[1] . " as {$filename} (content-disp: " . $headers["content-disposition"] . ", hash {$img_hash} not found, index {$existing_img})\n", "debug"); } else { $new_img_tag = $attached_images[$existing_img]; gdocs_log("{$img_hash} already downloaded (index in guid: {$existing_img})\n", "debug"); } // as we replace in post_content the originally matched positions will be offsetted, so we compensate $post_content = substr_replace($post_content, $new_img_tag, $doc_img[1] + $replace_offset, strlen($doc_img[0])); $replace_offset = $replace_offset + (strlen($new_img_tag) - strlen($doc_img[0])); } $post_array['post_content'] = $post_content; return $post_array; }
public function testGetPost() { //add media item to test unattached images in content $upload = $this->_upload_file(dirname(dirname(__DIR__)) . '/data/250x250.png'); $att_id = $this->_make_attachment($upload); $imgHtml = get_image_send_to_editor($att_id, 'Test Caption', 'Test Title', 'left'); $content = "Proin nec risus a metus mattis eleifend. Quisque ullamcorper porttitor aliquam. " . "Donec ut vulputate diam. Etiam eu dui pretium, condimentum nisi eu, tincidunt elit. \n\n" . $imgHtml . " \n\n Morbi ipsum dolor, tristique quis lorem sit amet, blandit ornare arcu. " . "Phasellus facilisis varius porttitor. Nam gravida neque eros, id pellentesque nibh aliquam a."; //get_image_send_to_editor $test_data = $this->_insert_post(array('post_content' => $content), array('100x200.png', '100x300.png')); $upload = $this->_upload_file(dirname(dirname(__DIR__)) . '/data/100x500.png'); $att_id = $this->_make_attachment($upload); set_post_thumbnail($test_data['post_id'], $att_id); list($status, $headers, $body) = $this->_getResponse(array('REQUEST_METHOD' => 'GET', 'PATH_INFO' => Voce\Thermal\get_api_base() . 'v1/posts/' . $test_data['post_id'], 'QUERY_STRING' => '')); $data = json_decode($body); $this->assertEquals('200', $status); $this->assertInternalType('object', $data); $post = get_post($test_data['post_id']); $checks = array('id' => array('type' => 'int', 'value' => $post->ID), 'id_str' => array('type' => 'string', 'value' => $post->ID), 'type' => array('type' => 'string', 'value' => 'post'), 'permalink' => array('type' => 'string', 'value' => get_permalink($post->ID)), 'parent' => array('type' => 'int', 'value' => $post->post_parent), 'parent_str' => array('type' => 'string', 'value' => $post->post_parent), 'date' => array('type' => 'string', 'value' => get_post_time('c', true, $post)), 'modified' => array('type' => 'string', 'value' => get_post_modified_time('c', true, $post)), 'status' => array('type' => 'string', 'value' => $post->post_status), 'comment_status' => array('type' => 'string', 'value' => $post->comment_status), 'comment_count' => array('type' => 'int', 'value' => 0), 'menu_order' => array('type' => 'int', 'value' => 0), 'title' => array('type' => 'string', 'value' => $post->post_title), 'name' => array('type' => 'string', 'value' => $post->post_name), 'excerpt' => array('type' => 'string'), 'excerpt_display' => array('type' => 'string'), 'content' => array('type' => 'string'), 'content_display' => array('type' => 'string'), 'author' => array('type' => 'object'), 'mime_type' => array('type' => 'string', 'value' => ''), 'meta' => array('type' => 'object'), 'taxonomies' => array('type' => 'object'), 'media' => array('type' => 'array')); foreach ($checks as $attrib => $check) { $this->assertObjectHasAttribute($attrib, $data); $this->assertInternalType($check['type'], $data->{$attrib}); if (isset($check['value'])) { $this->assertEquals($check['value'], $data->{$attrib}); } } $this->assertEquals(4, count($data->media)); }
$html = $title = isset($attachment['post_title']) ? $attachment['post_title'] : ''; if (!empty($attachment['url'])) { $url = $attachment['url']; if (strpos($url, 'attachment_id') || get_attachment_link($id) == $url) { $rel = ' rel="attachment wp-att-' . $id . '"'; } $html = '<a href="' . esc_url($url) . '"' . $rel . '>' . $html . '</a>'; } if ('image' === substr($post->post_mime_type, 0, 5)) { $align = isset($attachment['align']) ? $attachment['align'] : 'none'; $size = isset($attachment['image-size']) ? $attachment['image-size'] : 'medium'; $alt = isset($attachment['image_alt']) ? $attachment['image_alt'] : ''; $caption = isset($attachment['post_excerpt']) ? $attachment['post_excerpt'] : ''; $title = ''; // We no longer insert title tags into <img> tags, as they are redundant. $html = get_image_send_to_editor($id, $caption, $title, $align, $url, (bool) $rel, $size, $alt); } elseif ('video' === substr($post->post_mime_type, 0, 5) || 'audio' === substr($post->post_mime_type, 0, 5)) { global $wp_embed; $meta = get_post_meta($id, '_wp_attachment_metadata', true); $html = $wp_embed->shortcode($meta, $url); } /** This filter is documented in wp-admin/includes/media.php */ $html = apply_filters('media_send_to_editor', $html, $id, $attachment); // replace wp-image-<id>, wp-att-<id> and attachment_<id> $html = preg_replace(array('#(caption id="attachment_)(\\d+")#', '#(wp-image-|wp-att-)(\\d+)#'), array(sprintf('${1}nsm_%s_${2}', esc_attr($nsm_blog_id)), sprintf('${1}nsm-%s-${2}', esc_attr($nsm_blog_id))), $html); if (isset($_POST['chromeless']) && $_POST['chromeless']) { // WP3.5+ media browser is identified by the 'chromeless' parameter exit($html); } else { return media_send_to_editor($html); }