/** * Test get_oembed_response_data with an author. */ function test_get_oembed_response_data_author() { $user_id = self::factory()->user->create(array('display_name' => 'John Doe')); $post = self::factory()->post->create_and_get(array('post_title' => 'Some Post', 'post_author' => $user_id)); $data = get_oembed_response_data($post, 400); $this->assertEqualSets(array('version' => '1.0', 'provider_name' => get_bloginfo('name'), 'provider_url' => get_home_url('/'), 'author_name' => 'John Doe', 'author_url' => get_author_posts_url($user_id), 'title' => 'Some Post', 'type' => 'rich', 'width' => 400, 'height' => 225, 'html' => get_post_embed_html(400, 225, $post)), $data); }
/** * Callback for our API endpoint. * * Returns the JSON object for the post. * * @param WP_REST_Request $request Full details about the request. * * @return WP_Error|WP_REST_Response */ public function get_oembed_response(WP_REST_Request $request) { $post_id = url_to_postid($request['url']); /** * Filter the determined post id. * * @param int $post_id The post ID. * @param string $url The requestd URL. */ $post_id = apply_filters('rest_oembed_request_post_id', $post_id, $request['url']); if (0 === $post_id) { return new WP_Error('rest_oembed_invalid_url', __('Invalid URL.', 'oembed-api'), array('status' => 404)); } // Todo: Perhaps just default to json if something invalid is provided. if (!in_array($request['format'], array('json', 'xml'))) { return new WP_Error('rest_oembed_invalid_format', __('Invalid format.', 'oembed-api'), array('status' => 501)); } /** * Current post object. * * @var WP_Post $post */ $post = get_post($post_id); /** * User object for the post author. * * @var WP_User $author */ $author = get_userdata($post->post_author); /** * Filter the allowed minimum width for the oEmbed response. * * @param int $width The minimum width. Defaults to 200. */ $minwidth = apply_filters('rest_oembed_minwidth', 200); /** * Filter the allowed maximum width for the oEmbed response. * * @param int $width The maximum width. Defaults to 600. */ $maxwidth = apply_filters('rest_oembed_maxwidth', 600); $width = $request['maxwidth']; if ($width < $minwidth) { $width = $minwidth; } else { if ($width > $maxwidth) { $width = $maxwidth; } } // Todo: this shouldn't be hardcoded. $height = ceil($width / 16 * 9); /** * Filters the oEmbed response data. * * @param array $data The response data. */ $data = apply_filters('rest_oembed_response_data', array('version' => '1.0', 'provider_name' => get_bloginfo('name'), 'provider_url' => get_home_url(), 'author_name' => $author->display_name, 'author_url' => get_author_posts_url($author->ID, $author->user_nicename), 'title' => $post->post_title, 'type' => 'rich', 'width' => $width, 'height' => $height, 'html' => get_post_embed_html($post, $width, $height))); return $data; }
/** * Filters the oEmbed response data to return an iframe embed code. * * @since 4.4.0 * * @param array $data The response data. * @param WP_Post $post The post object. * @param int $width The requested width. * @param int $height The calculated height. * @return array The modified response data. */ function get_oembed_response_data_rich($data, $post, $width, $height) { $data['width'] = absint($width); $data['height'] = absint($height); $data['type'] = 'rich'; $data['html'] = get_post_embed_html($width, $height, $post); // Add post thumbnail to response if available. $thumbnail_id = false; if (has_post_thumbnail($post->ID)) { $thumbnail_id = get_post_thumbnail_id($post->ID); } if ('attachment' === get_post_type($post)) { if (wp_attachment_is_image($post)) { $thumbnail_id = $post->ID; } else { if (wp_attachment_is('video', $post)) { $thumbnail_id = get_post_thumbnail_id($post); $data['type'] = 'video'; } } } if ($thumbnail_id) { list($thumbnail_url, $thumbnail_width, $thumbnail_height) = wp_get_attachment_image_src($thumbnail_id, array($width, 99999)); $data['thumbnail_url'] = $thumbnail_url; $data['thumbnail_width'] = $thumbnail_width; $data['thumbnail_height'] = $thumbnail_height; } return $data; }
</ul> <div id="wp-embed-share-tab-wordpress" class="wp-embed-share-tab" role="tabpanel" aria-labelledby="wp-embed-share-tab-button-wordpress" aria-hidden="false"> <input type="text" value="<?php the_permalink(); ?> " class="wp-embed-share-input" tabindex="0" readonly/> <p class="wp-embed-share-description"> <?php _e('Copy and paste this URL into your WordPress site to embed'); ?> </p> </div> <div id="wp-embed-share-tab-html" class="wp-embed-share-tab" role="tabpanel" aria-labelledby="wp-embed-share-tab-button-html" aria-hidden="true"> <textarea class="wp-embed-share-input" tabindex="0" readonly><?php echo esc_textarea(get_post_embed_html(null, 600, 400)); ?> </textarea> <p class="wp-embed-share-description"> <?php _e('Copy and paste this code into your site to embed'); ?> </p> </div> </div> <button type="button" class="wp-embed-share-dialog-close" aria-label="<?php esc_attr_e('Close sharing dialog'); ?> ">
/** * Prints the necessary markup for the embed sharing dialog. * * @since 4.4.0 */ function print_embed_sharing_dialog() { if (is_404()) { return; } ?> <div class="wp-embed-share-dialog hidden" role="dialog" aria-label="<?php esc_attr_e('Sharing options'); ?> "> <div class="wp-embed-share-dialog-content"> <div class="wp-embed-share-dialog-text"> <ul class="wp-embed-share-tabs" role="tablist"> <li class="wp-embed-share-tab-button wp-embed-share-tab-button-wordpress" role="presentation"> <button type="button" role="tab" aria-controls="wp-embed-share-tab-wordpress" aria-selected="true" tabindex="0"><?php esc_html_e('WordPress Embed'); ?> </button> </li> <li class="wp-embed-share-tab-button wp-embed-share-tab-button-html" role="presentation"> <button type="button" role="tab" aria-controls="wp-embed-share-tab-html" aria-selected="false" tabindex="-1"><?php esc_html_e('HTML Embed'); ?> </button> </li> </ul> <div id="wp-embed-share-tab-wordpress" class="wp-embed-share-tab" role="tabpanel" aria-hidden="false"> <input type="text" value="<?php the_permalink(); ?> " class="wp-embed-share-input" aria-describedby="wp-embed-share-description-wordpress" tabindex="0" readonly/> <p class="wp-embed-share-description" id="wp-embed-share-description-wordpress"> <?php _e('Copy and paste this URL into your WordPress site to embed'); ?> </p> </div> <div id="wp-embed-share-tab-html" class="wp-embed-share-tab" role="tabpanel" aria-hidden="true"> <textarea class="wp-embed-share-input" aria-describedby="wp-embed-share-description-html" tabindex="0" readonly><?php echo esc_textarea(get_post_embed_html(600, 400)); ?> </textarea> <p class="wp-embed-share-description" id="wp-embed-share-description-html"> <?php _e('Copy and paste this code into your site to embed'); ?> </p> </div> </div> <button type="button" class="wp-embed-share-dialog-close" aria-label="<?php esc_attr_e('Close sharing dialog'); ?> "> <span class="dashicons dashicons-no"></span> </button> </div> </div> <?php }
/** * Test the get_post_embed_html function. */ function test_get_post_embed_html() { $post_id = $this->factory->post->create(); $expected = '<iframe sandbox="allow-scripts" security="restricted" src="' . esc_url(get_post_embed_url($post_id)) . '" width="200" height="200" title="Embedded WordPress Post" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>'; $this->assertEquals($expected, get_post_embed_html($post_id, 200, 200)); }
function test_get_post_embed_html() { $post_id = self::factory()->post->create(); $title = esc_attr(sprintf(__('“%1$s” — %2$s'), get_the_title($post_id), get_bloginfo('name'))); $expected = '<iframe sandbox="allow-scripts" security="restricted" src="' . esc_url(get_post_embed_url($post_id)) . '" width="200" height="200" title="' . $title . '" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" class="wp-embedded-content"></iframe>'; $this->assertStringEndsWith($expected, get_post_embed_html(200, 200, $post_id)); }
"> <span class="dashicons dashicons-share"></span> </button> </div> </div> </div> <div class="wp-embed-share-dialog hidden"> <div class="wp-embed-share-dialog-content"> <div class="wp-embed-share-dialog-text"> <p class="wp-embed-share-title"> <?php _e('Copy and paste the HTML code below into your site to embed:', 'oembed-api'); ?> </p> <input type="text" value="<?php echo esc_attr(get_post_embed_html(null, 600, 400)); ?> " class="wp-embed-share-input"/> <p class="wp-embed-share-title"> <?php _e('If you use WordPress, copy and paste this URL instead:', 'oembed-api'); ?> </p> <input type="text" value="<?php the_permalink(); ?> " class="wp-embed-share-input"/> </div> <button type="button" class="wp-embed-share-dialog-close" aria-label="<?php
/** * Get the oEmbed data for a given post. * * @param WP_Post|int $post Optional. Post object or ID. Defaults to the current post. * @param int $width The requested width. * @return array|false Response data on success, false if post doesn't exist. */ function get_oembed_response_data($post = null, $width) { /** * Current post object. * * @var WP_Post $post */ $post = get_post($post); if (!$post) { return false; } /** * User object for the post author. * * @var WP_User $author */ $author = get_userdata($post->post_author); // If a post doesn't have an author, fall back to the site's name. $author_name = get_bloginfo('name'); $author_url = get_home_url(); if ($author) { $author_name = $author->display_name; $author_url = get_author_posts_url($author->ID, $author->user_nicename); } /** * Filter the allowed minimum width for the oEmbed response. * * @param int $width The minimum width. Defaults to 200. */ $minwidth = apply_filters('oembed_minwidth', 200); /** * Filter the allowed maximum width for the oEmbed response. * * @param int $width The maximum width. Defaults to 600. */ $maxwidth = apply_filters('oembed_maxwidth', 600); if ($width < $minwidth) { $width = $minwidth; } else { if ($width > $maxwidth) { $width = $maxwidth; } } $height = ceil($width / 16 * 9); $data = array('version' => '1.0', 'provider_name' => get_bloginfo('name'), 'provider_url' => get_home_url(), 'author_name' => $author_name, 'author_url' => $author_url, 'title' => $post->post_title, 'type' => 'rich', 'width' => $width, 'height' => $height, 'html' => get_post_embed_html($post, $width, $height)); // Add post thumbnail to response if available. $thumbnail_id = false; if (has_post_thumbnail($post->ID)) { $thumbnail_id = get_post_thumbnail_id($post->ID); } if ('attachment' === get_post_type($post) && wp_attachment_is_image($post->ID)) { $thumbnail_id = $post->ID; } if ($thumbnail_id) { list($thumbnail_url, $thumbnail_width, $thumbnail_height) = wp_get_attachment_image_src($thumbnail_id, array($width, 99999)); $data['thumbnail_url'] = $thumbnail_url; $data['thumbnail_width'] = $thumbnail_width; $data['thumbnail_height'] = $thumbnail_height; } /** * Filters the oEmbed response data. * * @param array $data The response data. * @param WP_Post $post The post object. */ return apply_filters('oembed_response_data', $data, $post); }
/** * Fetch our oEmbed response data to return. * * A simplified version of {@link get_oembed_response_data()}. * * @since 2.6.0 * * @link http://oembed.com/ View the 'Response parameters' section for more details. * * @param array $item Custom oEmbed response data. * @param int $width The requested width. * @return array */ protected function get_oembed_response_data($item, $width) { $data = wp_parse_args($item, array('version' => '1.0', 'provider_name' => get_bloginfo('name'), 'provider_url' => get_home_url(), 'author_name' => get_bloginfo('name'), 'author_url' => get_home_url(), 'title' => ucfirst($this->slug_endpoint), 'type' => 'rich')); /** This filter is documented in /wp-includes/embed.php */ $min_max_width = apply_filters('oembed_min_max_width', array('min' => 200, 'max' => 600)); $width = min(max($min_max_width['min'], $width), $min_max_width['max']); $height = max(ceil($width / 16 * 9), 200); $data['width'] = absint($width); $data['height'] = absint($height); // Set 'html' parameter. if ('video' === $data['type'] || 'rich' === $data['type']) { // Fake a WP post so we can use get_post_embed_html(). $post = new stdClass(); $post->post_content = $data['content']; $post->post_title = $data['title']; $data['html'] = get_post_embed_html($data['width'], $data['height'], $post); } // Remove temporary parameters. unset($data['content']); return $data; }
function test_get_post_embed_html() { $post_id = self::factory()->post->create(); $expected = '<iframe sandbox="allow-scripts" security="restricted" src="' . esc_url(get_post_embed_url($post_id)) . '" width="200" height="200" title="Embedded WordPress Post" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" class="wp-embedded-content"></iframe>'; $this->assertStringEndsWith($expected, get_post_embed_html(200, 200, $post_id)); }
public function update_embed_code() { if (!isset($_POST['post_id']) || !$_POST['post_id']) { return; } $post_id = (int) $_POST['post_id']; $width = (int) $_POST['width']; $height = (int) $_POST['height']; echo get_post_embed_html($width, $height, $post_id); exit; }
/** * Get the oEmbed data for a given post. * * @param WP_Post|int $post Optional. Post object or ID. Defaults to the current post. * @param int $width The requested width. * @return array|false Response data on success, false if post doesn't exist. */ function get_oembed_response_data($post = null, $width) { $post = get_post($post); if (!$post) { return false; } /** * Filter the allowed minimum width for the oEmbed response. * * @param int $width The minimum width. Defaults to 200. */ $minwidth = apply_filters('oembed_minwidth', 200); /** * Filter the allowed maximum width for the oEmbed response. * * @param int $width The maximum width. Defaults to 600. */ $maxwidth = apply_filters('oembed_maxwidth', 600); if ($width < $minwidth) { $width = $minwidth; } else { if ($width > $maxwidth) { $width = $maxwidth; } } $height = ceil($width / 16 * 9); if (200 > $height) { $height = 200; } $data = array('version' => '1.0', 'provider_name' => get_bloginfo('name'), 'provider_url' => get_home_url(), 'author_name' => get_bloginfo('name'), 'author_url' => get_home_url(), 'title' => $post->post_title, 'type' => 'rich', 'width' => $width, 'height' => $height, 'html' => get_post_embed_html($post, $width, $height)); /** * Filter the oEmbed response data. * * @param array $data The response data. * @param WP_Post $post The post object. */ return apply_filters('oembed_response_data', $data, $post); }
/** * Update the epiaode embed code via ajax * @return void */ public function update_episode_embed_code() { // Make sure we have a valid post ID if (!isset($_POST['post_id']) || !$_POST['post_id']) { return; } // Get info for embed code $post_id = (int) $_POST['post_id']; $width = (int) $_POST['width']; $height = (int) $_POST['height']; // Generate embed code echo get_post_embed_html($width, $height, $post_id); // Exit after ajax request exit; }