+
+/**
+ * Apply [embed] AJAX handlers to a string.
+ *
+ * @since 4.0.0
+ *
+ * @global WP_Post $post Global $post.
+ * @global WP_Embed $wp_embed Embed API instance.
+ */
+function wp_ajax_parse_embed() {
+ global $post, $wp_embed;
+
+ if ( ! $post = get_post( (int) $_POST['post_ID'] ) ) {
+ wp_send_json_error();
+ }
+
+ if ( empty( $_POST['shortcode'] ) || ! current_user_can( 'edit_post', $post->ID ) ) {
+ wp_send_json_error();
+ }
+
+ $shortcode = wp_unslash( $_POST['shortcode'] );
+ $url = str_replace( '[embed]', '', str_replace( '[/embed]', '', $shortcode ) );
+ $parsed = false;
+ setup_postdata( $post );
+
+ $wp_embed->return_false_on_fail = true;
+
+ if ( is_ssl() && preg_match( '%^\\[embed[^\\]]*\\]http://%i', $shortcode ) ) {
+ // Admin is ssl and the user pasted non-ssl URL.
+ // Check if the provider supports ssl embeds and use that for the preview.
+ $ssl_shortcode = preg_replace( '%^(\\[embed[^\\]]*\\])http://%i', '$1https://', $shortcode );
+ $parsed = $wp_embed->run_shortcode( $ssl_shortcode );
+
+ if ( ! $parsed ) {
+ $no_ssl_support = true;
+ }
+ }
+
+ if ( ! $parsed ) {
+ $parsed = $wp_embed->run_shortcode( $shortcode );
+ }
+
+ if ( ! $parsed ) {
+ wp_send_json_error( array(
+ 'type' => 'not-embeddable',
+ 'message' => sprintf( __( '%s failed to embed.' ), '<code>' . esc_html( $url ) . '</code>' ),
+ ) );
+ }
+
+ if ( has_shortcode( $parsed, 'audio' ) || has_shortcode( $parsed, 'video' ) ) {
+ $styles = '';
+ $mce_styles = wpview_media_sandbox_styles();
+ foreach ( $mce_styles as $style ) {
+ $styles .= sprintf( '<link rel="stylesheet" href="%s"/>', $style );
+ }
+
+ $html = do_shortcode( $parsed );
+
+ global $wp_scripts;
+ if ( ! empty( $wp_scripts ) ) {
+ $wp_scripts->done = array();
+ }
+ ob_start();
+ wp_print_scripts( 'wp-mediaelement' );
+ $scripts = ob_get_clean();
+
+ $parsed = $styles . $html . $scripts;
+ }
+
+
+ if ( ! empty( $no_ssl_support ) || ( is_ssl() && ( preg_match( '%<(iframe|script|embed) [^>]*src="http://%', $parsed ) ||
+ preg_match( '%<link [^>]*href="http://%', $parsed ) ) ) ) {
+ // Admin is ssl and the embed is not. Iframes, scripts, and other "active content" will be blocked.
+ wp_send_json_error( array(
+ 'type' => 'not-ssl',
+ 'message' => sprintf( __( 'Preview not available. %s cannot be embedded securely.' ), '<code>' . esc_html( $url ) . '</code>' ),
+ ) );
+ }
+
+ wp_send_json_success( array(
+ 'body' => $parsed
+ ) );
+}
+
+function wp_ajax_parse_media_shortcode() {
+ global $post, $wp_scripts;
+
+ if ( ! $post = get_post( (int) $_POST['post_ID'] ) ) {
+ wp_send_json_error();
+ }
+
+ if ( empty( $_POST['shortcode'] ) || ! current_user_can( 'edit_post', $post->ID ) ) {
+ wp_send_json_error();
+ }
+
+ setup_postdata( $post );
+ $shortcode = do_shortcode( wp_unslash( $_POST['shortcode'] ) );
+
+ if ( empty( $shortcode ) ) {
+ wp_send_json_error( array(
+ 'type' => 'no-items',
+ 'message' => __( 'No items found.' ),
+ ) );
+ }
+
+ $head = '';
+ $styles = wpview_media_sandbox_styles();
+
+ foreach ( $styles as $style ) {
+ $head .= '<link type="text/css" rel="stylesheet" href="' . $style . '">';
+ }
+
+ if ( ! empty( $wp_scripts ) ) {
+ $wp_scripts->done = array();
+ }
+
+ ob_start();
+
+ echo $shortcode;
+
+ if ( 'playlist' === $_REQUEST['type'] ) {
+ wp_underscore_playlist_templates();
+
+ wp_print_scripts( 'wp-playlist' );
+ } else {
+ wp_print_scripts( 'wp-mediaelement' );
+ }
+
+ wp_send_json_success( array(
+ 'head' => $head,
+ 'body' => ob_get_clean()
+ ) );
+}