do_enclose

The timeline below displays how wordpress function do_enclose has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Checks content for video and audio links to add as enclosures.
 *
 * Will not add enclosures that have already been added and will
 * remove enclosures that are no longer in the post. This is called as
 * pingbacks and trackbacks.
 *
 * @since 1.5.0
 * @since 5.3.0 The `$content` parameter was made optional, and the `$post` parameter was
 *              updated to accept a post ID or a WP_Post object.
 * @since 5.6.0 The `$content` parameter is no longer optional, but passing `null` to skip it
 *              is still supported.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string|null $content Post content. If `null`, the `post_content` field from `$post` is used.
 * @param int|WP_Post $post    Post ID or post object.
 * @return void|false Void on success, false if the post is not found.
 */
function do_enclose($content, $post)
{
    global $wpdb;
    // @todo Tidy this code and make the debug code optional.
    require_once ABSPATH . WPINC . '/class-IXR.php';
    $post = get_post($post);
    if (!$post) {
        return false;
    }
    if (null === $content) {
        $content = $post->post_content;
    }
    $post_links = array();
    $pung = get_enclosed($post->ID);
    $post_links_temp = wp_extract_urls($content);
    foreach ($pung as $link_test) {
        // Link is no longer in post.
        if (!in_array($link_test, $post_links_temp, true)) {
            $mids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($link_test) . '%'));
            foreach ($mids as $mid) {
                delete_metadata_by_mid('post', $mid);
            }
        }
    }
    foreach ((array) $post_links_temp as $link_test) {
        // If we haven't pung it already.
        if (!in_array($link_test, $pung, true)) {
            $test = parse_url($link_test);
            if (false === $test) {
                continue;
            }
            if (isset($test['query'])) {
                $post_links[] = $link_test;
            } elseif (isset($test['path']) && '/' !== $test['path'] && '' !== $test['path']) {
                $post_links[] = $link_test;
            }
        }
    }
    /**
     * Filters the list of enclosure links before querying the database.
     *
     * Allows for the addition and/or removal of potential enclosures to save
     * to postmeta before checking the database for existing enclosures.
     *
     * @since 4.4.0
     *
     * @param string[] $post_links An array of enclosure links.
     * @param int      $post_id    Post ID.
     */
    $post_links = apply_filters('enclosure_links', $post_links, $post->ID);
    foreach ((array) $post_links as $url) {
        $url = strip_fragment_from_url($url);
        if ('' !== $url && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($url) . '%'))) {
            $headers = wp_get_http_headers($url);
            if ($headers) {
                $len = isset($headers['Content-Length']) ? (int) $headers['Content-Length'] : 0;
                $type = isset($headers['Content-Type']) ? $headers['Content-Type'] : '';
                $allowed_types = array('video', 'audio');
                // Check to see if we can figure out the mime type from the extension.
                $url_parts = parse_url($url);
                if (false !== $url_parts && !empty($url_parts['path'])) {
                    $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION);
                    if (!empty($extension)) {
                        foreach (wp_get_mime_types() as $exts => $mime) {
                            if (preg_match('!^(' . $exts . ')$!i', $extension)) {
                                $type = $mime;
                                break;
                            }
                        }
                    }
                }
                if (in_array(substr($type, 0, strpos($type, '/')), $allowed_types, true)) {
                    add_post_meta($post->ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n");
                }
            }
        }
    }
}

WordPress Version: 6.2

/**
 * Checks content for video and audio links to add as enclosures.
 *
 * Will not add enclosures that have already been added and will
 * remove enclosures that are no longer in the post. This is called as
 * pingbacks and trackbacks.
 *
 * @since 1.5.0
 * @since 5.3.0 The `$content` parameter was made optional, and the `$post` parameter was
 *              updated to accept a post ID or a WP_Post object.
 * @since 5.6.0 The `$content` parameter is no longer optional, but passing `null` to skip it
 *              is still supported.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string|null $content Post content. If `null`, the `post_content` field from `$post` is used.
 * @param int|WP_Post $post    Post ID or post object.
 * @return void|false Void on success, false if the post is not found.
 */
function do_enclose($content, $post)
{
    global $wpdb;
    // @todo Tidy this code and make the debug code optional.
    include_once ABSPATH . WPINC . '/class-IXR.php';
    $post = get_post($post);
    if (!$post) {
        return false;
    }
    if (null === $content) {
        $content = $post->post_content;
    }
    $post_links = array();
    $pung = get_enclosed($post->ID);
    $post_links_temp = wp_extract_urls($content);
    foreach ($pung as $link_test) {
        // Link is no longer in post.
        if (!in_array($link_test, $post_links_temp, true)) {
            $mids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($link_test) . '%'));
            foreach ($mids as $mid) {
                delete_metadata_by_mid('post', $mid);
            }
        }
    }
    foreach ((array) $post_links_temp as $link_test) {
        // If we haven't pung it already.
        if (!in_array($link_test, $pung, true)) {
            $test = parse_url($link_test);
            if (false === $test) {
                continue;
            }
            if (isset($test['query'])) {
                $post_links[] = $link_test;
            } elseif (isset($test['path']) && '/' !== $test['path'] && '' !== $test['path']) {
                $post_links[] = $link_test;
            }
        }
    }
    /**
     * Filters the list of enclosure links before querying the database.
     *
     * Allows for the addition and/or removal of potential enclosures to save
     * to postmeta before checking the database for existing enclosures.
     *
     * @since 4.4.0
     *
     * @param string[] $post_links An array of enclosure links.
     * @param int      $post_id    Post ID.
     */
    $post_links = apply_filters('enclosure_links', $post_links, $post->ID);
    foreach ((array) $post_links as $url) {
        $url = strip_fragment_from_url($url);
        if ('' !== $url && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($url) . '%'))) {
            $headers = wp_get_http_headers($url);
            if ($headers) {
                $len = isset($headers['Content-Length']) ? (int) $headers['Content-Length'] : 0;
                $type = isset($headers['Content-Type']) ? $headers['Content-Type'] : '';
                $allowed_types = array('video', 'audio');
                // Check to see if we can figure out the mime type from the extension.
                $url_parts = parse_url($url);
                if (false !== $url_parts && !empty($url_parts['path'])) {
                    $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION);
                    if (!empty($extension)) {
                        foreach (wp_get_mime_types() as $exts => $mime) {
                            if (preg_match('!^(' . $exts . ')$!i', $extension)) {
                                $type = $mime;
                                break;
                            }
                        }
                    }
                }
                if (in_array(substr($type, 0, strpos($type, '/')), $allowed_types, true)) {
                    add_post_meta($post->ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n");
                }
            }
        }
    }
}

WordPress Version: 6.1

/**
 * Checks content for video and audio links to add as enclosures.
 *
 * Will not add enclosures that have already been added and will
 * remove enclosures that are no longer in the post. This is called as
 * pingbacks and trackbacks.
 *
 * @since 1.5.0
 * @since 5.3.0 The `$content` parameter was made optional, and the `$post` parameter was
 *              updated to accept a post ID or a WP_Post object.
 * @since 5.6.0 The `$content` parameter is no longer optional, but passing `null` to skip it
 *              is still supported.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string|null $content Post content. If `null`, the `post_content` field from `$post` is used.
 * @param int|WP_Post $post    Post ID or post object.
 * @return void|false Void on success, false if the post is not found.
 */
function do_enclose($content, $post)
{
    global $wpdb;
    // @todo Tidy this code and make the debug code optional.
    include_once ABSPATH . WPINC . '/class-IXR.php';
    $post = get_post($post);
    if (!$post) {
        return false;
    }
    if (null === $content) {
        $content = $post->post_content;
    }
    $post_links = array();
    $pung = get_enclosed($post->ID);
    $post_links_temp = wp_extract_urls($content);
    foreach ($pung as $link_test) {
        // Link is no longer in post.
        if (!in_array($link_test, $post_links_temp, true)) {
            $mids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($link_test) . '%'));
            foreach ($mids as $mid) {
                delete_metadata_by_mid('post', $mid);
            }
        }
    }
    foreach ((array) $post_links_temp as $link_test) {
        // If we haven't pung it already.
        if (!in_array($link_test, $pung, true)) {
            $test = parse_url($link_test);
            if (false === $test) {
                continue;
            }
            if (isset($test['query'])) {
                $post_links[] = $link_test;
            } elseif (isset($test['path']) && '/' !== $test['path'] && '' !== $test['path']) {
                $post_links[] = $link_test;
            }
        }
    }
    /**
     * Filters the list of enclosure links before querying the database.
     *
     * Allows for the addition and/or removal of potential enclosures to save
     * to postmeta before checking the database for existing enclosures.
     *
     * @since 4.4.0
     *
     * @param string[] $post_links An array of enclosure links.
     * @param int      $post_ID    Post ID.
     */
    $post_links = apply_filters('enclosure_links', $post_links, $post->ID);
    foreach ((array) $post_links as $url) {
        $url = strip_fragment_from_url($url);
        if ('' !== $url && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($url) . '%'))) {
            $headers = wp_get_http_headers($url);
            if ($headers) {
                $len = isset($headers['content-length']) ? (int) $headers['content-length'] : 0;
                $type = isset($headers['content-type']) ? $headers['content-type'] : '';
                $allowed_types = array('video', 'audio');
                // Check to see if we can figure out the mime type from the extension.
                $url_parts = parse_url($url);
                if (false !== $url_parts && !empty($url_parts['path'])) {
                    $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION);
                    if (!empty($extension)) {
                        foreach (wp_get_mime_types() as $exts => $mime) {
                            if (preg_match('!^(' . $exts . ')$!i', $extension)) {
                                $type = $mime;
                                break;
                            }
                        }
                    }
                }
                if (in_array(substr($type, 0, strpos($type, '/')), $allowed_types, true)) {
                    add_post_meta($post->ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n");
                }
            }
        }
    }
}

WordPress Version: 5.7

/**
 * Check content for video and audio links to add as enclosures.
 *
 * Will not add enclosures that have already been added and will
 * remove enclosures that are no longer in the post. This is called as
 * pingbacks and trackbacks.
 *
 * @since 1.5.0
 * @since 5.3.0 The `$content` parameter was made optional, and the `$post` parameter was
 *              updated to accept a post ID or a WP_Post object.
 * @since 5.6.0 The `$content` parameter is no longer optional, but passing `null` to skip it
 *              is still supported.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string|null $content Post content. If `null`, the `post_content` field from `$post` is used.
 * @param int|WP_Post $post    Post ID or post object.
 * @return void|false Void on success, false if the post is not found.
 */
function do_enclose($content, $post)
{
    global $wpdb;
    // @todo Tidy this code and make the debug code optional.
    include_once ABSPATH . WPINC . '/class-IXR.php';
    $post = get_post($post);
    if (!$post) {
        return false;
    }
    if (null === $content) {
        $content = $post->post_content;
    }
    $post_links = array();
    $pung = get_enclosed($post->ID);
    $post_links_temp = wp_extract_urls($content);
    foreach ($pung as $link_test) {
        // Link is no longer in post.
        if (!in_array($link_test, $post_links_temp, true)) {
            $mids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($link_test) . '%'));
            foreach ($mids as $mid) {
                delete_metadata_by_mid('post', $mid);
            }
        }
    }
    foreach ((array) $post_links_temp as $link_test) {
        // If we haven't pung it already.
        if (!in_array($link_test, $pung, true)) {
            $test = parse_url($link_test);
            if (false === $test) {
                continue;
            }
            if (isset($test['query'])) {
                $post_links[] = $link_test;
            } elseif (isset($test['path']) && '/' !== $test['path'] && '' !== $test['path']) {
                $post_links[] = $link_test;
            }
        }
    }
    /**
     * Filters the list of enclosure links before querying the database.
     *
     * Allows for the addition and/or removal of potential enclosures to save
     * to postmeta before checking the database for existing enclosures.
     *
     * @since 4.4.0
     *
     * @param string[] $post_links An array of enclosure links.
     * @param int      $post_ID    Post ID.
     */
    $post_links = apply_filters('enclosure_links', $post_links, $post->ID);
    foreach ((array) $post_links as $url) {
        $url = strip_fragment_from_url($url);
        if ('' !== $url && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($url) . '%'))) {
            $headers = wp_get_http_headers($url);
            if ($headers) {
                $len = isset($headers['content-length']) ? (int) $headers['content-length'] : 0;
                $type = isset($headers['content-type']) ? $headers['content-type'] : '';
                $allowed_types = array('video', 'audio');
                // Check to see if we can figure out the mime type from the extension.
                $url_parts = parse_url($url);
                if (false !== $url_parts && !empty($url_parts['path'])) {
                    $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION);
                    if (!empty($extension)) {
                        foreach (wp_get_mime_types() as $exts => $mime) {
                            if (preg_match('!^(' . $exts . ')$!i', $extension)) {
                                $type = $mime;
                                break;
                            }
                        }
                    }
                }
                if (in_array(substr($type, 0, strpos($type, '/')), $allowed_types, true)) {
                    add_post_meta($post->ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n");
                }
            }
        }
    }
}

WordPress Version: 5.6

/**
 * Check content for video and audio links to add as enclosures.
 *
 * Will not add enclosures that have already been added and will
 * remove enclosures that are no longer in the post. This is called as
 * pingbacks and trackbacks.
 *
 * @since 1.5.0
 * @since 5.3.0 The `$content` parameter was made optional, and the `$post` parameter was
 *              updated to accept a post ID or a WP_Post object.
 * @since 5.6.0 The `$content` parameter is no longer optional, but passing `null` to skip it
 *              is still supported.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string|null $content Post content. If `null`, the `post_content` field from `$post` is used.
 * @param int|WP_Post $post    Post ID or post object.
 * @return null|bool Returns false if post is not found.
 */
function do_enclose($content, $post)
{
    global $wpdb;
    // @todo Tidy this code and make the debug code optional.
    include_once ABSPATH . WPINC . '/class-IXR.php';
    $post = get_post($post);
    if (!$post) {
        return false;
    }
    if (null === $content) {
        $content = $post->post_content;
    }
    $post_links = array();
    $pung = get_enclosed($post->ID);
    $post_links_temp = wp_extract_urls($content);
    foreach ($pung as $link_test) {
        // Link is no longer in post.
        if (!in_array($link_test, $post_links_temp, true)) {
            $mids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($link_test) . '%'));
            foreach ($mids as $mid) {
                delete_metadata_by_mid('post', $mid);
            }
        }
    }
    foreach ((array) $post_links_temp as $link_test) {
        // If we haven't pung it already.
        if (!in_array($link_test, $pung, true)) {
            $test = parse_url($link_test);
            if (false === $test) {
                continue;
            }
            if (isset($test['query'])) {
                $post_links[] = $link_test;
            } elseif (isset($test['path']) && '/' !== $test['path'] && '' !== $test['path']) {
                $post_links[] = $link_test;
            }
        }
    }
    /**
     * Filters the list of enclosure links before querying the database.
     *
     * Allows for the addition and/or removal of potential enclosures to save
     * to postmeta before checking the database for existing enclosures.
     *
     * @since 4.4.0
     *
     * @param string[] $post_links An array of enclosure links.
     * @param int      $post_ID    Post ID.
     */
    $post_links = apply_filters('enclosure_links', $post_links, $post->ID);
    foreach ((array) $post_links as $url) {
        $url = strip_fragment_from_url($url);
        if ('' !== $url && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($url) . '%'))) {
            $headers = wp_get_http_headers($url);
            if ($headers) {
                $len = isset($headers['content-length']) ? (int) $headers['content-length'] : 0;
                $type = isset($headers['content-type']) ? $headers['content-type'] : '';
                $allowed_types = array('video', 'audio');
                // Check to see if we can figure out the mime type from the extension.
                $url_parts = parse_url($url);
                if (false !== $url_parts && !empty($url_parts['path'])) {
                    $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION);
                    if (!empty($extension)) {
                        foreach (wp_get_mime_types() as $exts => $mime) {
                            if (preg_match('!^(' . $exts . ')$!i', $extension)) {
                                $type = $mime;
                                break;
                            }
                        }
                    }
                }
                if (in_array(substr($type, 0, strpos($type, '/')), $allowed_types, true)) {
                    add_post_meta($post->ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n");
                }
            }
        }
    }
}

WordPress Version: 5.5

/**
 * Check content for video and audio links to add as enclosures.
 *
 * Will not add enclosures that have already been added and will
 * remove enclosures that are no longer in the post. This is called as
 * pingbacks and trackbacks.
 *
 * @since 1.5.0
 * @since 5.3.0 The `$content` parameter was made optional, and the `$post` parameter was
 *              updated to accept a post ID or a WP_Post object.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string      $content Post content. If `null`, the `post_content` field from `$post` is used.
 * @param int|WP_Post $post    Post ID or post object.
 * @return null|bool Returns false if post is not found.
 */
function do_enclose($content = null, $post)
{
    global $wpdb;
    // @todo Tidy this code and make the debug code optional.
    include_once ABSPATH . WPINC . '/class-IXR.php';
    $post = get_post($post);
    if (!$post) {
        return false;
    }
    if (null === $content) {
        $content = $post->post_content;
    }
    $post_links = array();
    $pung = get_enclosed($post->ID);
    $post_links_temp = wp_extract_urls($content);
    foreach ($pung as $link_test) {
        // Link is no longer in post.
        if (!in_array($link_test, $post_links_temp, true)) {
            $mids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($link_test) . '%'));
            foreach ($mids as $mid) {
                delete_metadata_by_mid('post', $mid);
            }
        }
    }
    foreach ((array) $post_links_temp as $link_test) {
        // If we haven't pung it already.
        if (!in_array($link_test, $pung, true)) {
            $test = parse_url($link_test);
            if (false === $test) {
                continue;
            }
            if (isset($test['query'])) {
                $post_links[] = $link_test;
            } elseif (isset($test['path']) && '/' !== $test['path'] && '' !== $test['path']) {
                $post_links[] = $link_test;
            }
        }
    }
    /**
     * Filters the list of enclosure links before querying the database.
     *
     * Allows for the addition and/or removal of potential enclosures to save
     * to postmeta before checking the database for existing enclosures.
     *
     * @since 4.4.0
     *
     * @param string[] $post_links An array of enclosure links.
     * @param int      $post_ID    Post ID.
     */
    $post_links = apply_filters('enclosure_links', $post_links, $post->ID);
    foreach ((array) $post_links as $url) {
        if ('' !== $url && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($url) . '%'))) {
            $headers = wp_get_http_headers($url);
            if ($headers) {
                $len = isset($headers['content-length']) ? (int) $headers['content-length'] : 0;
                $type = isset($headers['content-type']) ? $headers['content-type'] : '';
                $allowed_types = array('video', 'audio');
                // Check to see if we can figure out the mime type from the extension.
                $url_parts = parse_url($url);
                if (false !== $url_parts && !empty($url_parts['path'])) {
                    $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION);
                    if (!empty($extension)) {
                        foreach (wp_get_mime_types() as $exts => $mime) {
                            if (preg_match('!^(' . $exts . ')$!i', $extension)) {
                                $type = $mime;
                                break;
                            }
                        }
                    }
                }
                if (in_array(substr($type, 0, strpos($type, '/')), $allowed_types, true)) {
                    add_post_meta($post->ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n");
                }
            }
        }
    }
}

WordPress Version: 5.4

/**
 * Check content for video and audio links to add as enclosures.
 *
 * Will not add enclosures that have already been added and will
 * remove enclosures that are no longer in the post. This is called as
 * pingbacks and trackbacks.
 *
 * @since 1.5.0
 * @since 5.3.0 The `$content` parameter was made optional, and the `$post` parameter was
 *              updated to accept a post ID or a WP_Post object.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string         $content Post content. If `null`, the `post_content` field from `$post` is used.
 * @param int|WP_Post    $post    Post ID or post object.
 * @return null|bool Returns false if post is not found.
 */
function do_enclose($content = null, $post)
{
    global $wpdb;
    // @todo Tidy this code and make the debug code optional.
    include_once ABSPATH . WPINC . '/class-IXR.php';
    $post = get_post($post);
    if (!$post) {
        return false;
    }
    if (null === $content) {
        $content = $post->post_content;
    }
    $post_links = array();
    $pung = get_enclosed($post->ID);
    $post_links_temp = wp_extract_urls($content);
    foreach ($pung as $link_test) {
        // Link is no longer in post.
        if (!in_array($link_test, $post_links_temp, true)) {
            $mids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($link_test) . '%'));
            foreach ($mids as $mid) {
                delete_metadata_by_mid('post', $mid);
            }
        }
    }
    foreach ((array) $post_links_temp as $link_test) {
        // If we haven't pung it already.
        if (!in_array($link_test, $pung, true)) {
            $test = @parse_url($link_test);
            if (false === $test) {
                continue;
            }
            if (isset($test['query'])) {
                $post_links[] = $link_test;
            } elseif (isset($test['path']) && '/' !== $test['path'] && '' !== $test['path']) {
                $post_links[] = $link_test;
            }
        }
    }
    /**
     * Filters the list of enclosure links before querying the database.
     *
     * Allows for the addition and/or removal of potential enclosures to save
     * to postmeta before checking the database for existing enclosures.
     *
     * @since 4.4.0
     *
     * @param string[] $post_links An array of enclosure links.
     * @param int      $post_ID    Post ID.
     */
    $post_links = apply_filters('enclosure_links', $post_links, $post->ID);
    foreach ((array) $post_links as $url) {
        if ('' !== $url && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($url) . '%'))) {
            $headers = wp_get_http_headers($url);
            if ($headers) {
                $len = isset($headers['content-length']) ? (int) $headers['content-length'] : 0;
                $type = isset($headers['content-type']) ? $headers['content-type'] : '';
                $allowed_types = array('video', 'audio');
                // Check to see if we can figure out the mime type from the extension.
                $url_parts = @parse_url($url);
                if (false !== $url_parts) {
                    $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION);
                    if (!empty($extension)) {
                        foreach (wp_get_mime_types() as $exts => $mime) {
                            if (preg_match('!^(' . $exts . ')$!i', $extension)) {
                                $type = $mime;
                                break;
                            }
                        }
                    }
                }
                if (in_array(substr($type, 0, strpos($type, '/')), $allowed_types, true)) {
                    add_post_meta($post->ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n");
                }
            }
        }
    }
}

WordPress Version: 5.3

/**
 * Check content for video and audio links to add as enclosures.
 *
 * Will not add enclosures that have already been added and will
 * remove enclosures that are no longer in the post. This is called as
 * pingbacks and trackbacks.
 *
 * @since 1.5.0
 * @since 5.3.0 The `$content` parameter was made optional, and the `$post` parameter was
 *              updated to accept a post ID or a WP_Post object.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string         $content Post content. If `null`, the `post_content` field from `$post` is used.
 * @param int|WP_Post    $post    Post ID or post object.
 * @return null|bool Returns false if post is not found.
 */
function do_enclose($content = null, $post)
{
    global $wpdb;
    // @todo Tidy this code and make the debug code optional.
    include_once ABSPATH . WPINC . '/class-IXR.php';
    $post = get_post($post);
    if (!$post) {
        return false;
    }
    if (null === $content) {
        $content = $post->post_content;
    }
    $post_links = array();
    $pung = get_enclosed($post->ID);
    $post_links_temp = wp_extract_urls($content);
    foreach ($pung as $link_test) {
        // Link is no longer in post.
        if (!in_array($link_test, $post_links_temp, true)) {
            $mids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($link_test) . '%'));
            foreach ($mids as $mid) {
                delete_metadata_by_mid('post', $mid);
            }
        }
    }
    foreach ((array) $post_links_temp as $link_test) {
        // If we haven't pung it already.
        if (!in_array($link_test, $pung, true)) {
            $test = @parse_url($link_test);
            if (false === $test) {
                continue;
            }
            if (isset($test['query'])) {
                $post_links[] = $link_test;
            } elseif (isset($test['path']) && '/' !== $test['path'] && '' !== $test['path']) {
                $post_links[] = $link_test;
            }
        }
    }
    /**
     * Filters the list of enclosure links before querying the database.
     *
     * Allows for the addition and/or removal of potential enclosures to save
     * to postmeta before checking the database for existing enclosures.
     *
     * @since 4.4.0
     *
     * @param array $post_links An array of enclosure links.
     * @param int   $post_ID    Post ID.
     */
    $post_links = apply_filters('enclosure_links', $post_links, $post->ID);
    foreach ((array) $post_links as $url) {
        if ('' !== $url && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like($url) . '%'))) {
            $headers = wp_get_http_headers($url);
            if ($headers) {
                $len = isset($headers['content-length']) ? (int) $headers['content-length'] : 0;
                $type = isset($headers['content-type']) ? $headers['content-type'] : '';
                $allowed_types = array('video', 'audio');
                // Check to see if we can figure out the mime type from
                // the extension
                $url_parts = @parse_url($url);
                if (false !== $url_parts) {
                    $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION);
                    if (!empty($extension)) {
                        foreach (wp_get_mime_types() as $exts => $mime) {
                            if (preg_match('!^(' . $exts . ')$!i', $extension)) {
                                $type = $mime;
                                break;
                            }
                        }
                    }
                }
                if (in_array(substr($type, 0, strpos($type, '/')), $allowed_types, true)) {
                    add_post_meta($post->ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n");
                }
            }
        }
    }
}

WordPress Version: 5.1

/**
 * Check content for video and audio links to add as enclosures.
 *
 * Will not add enclosures that have already been added and will
 * remove enclosures that are no longer in the post. This is called as
 * pingbacks and trackbacks.
 *
 * @since 1.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $content Post Content.
 * @param int    $post_ID Post ID.
 */
function do_enclose($content, $post_ID)
{
    global $wpdb;
    //TODO: Tidy this ghetto code up and make the debug code optional
    include_once ABSPATH . WPINC . '/class-IXR.php';
    $post_links = array();
    $pung = get_enclosed($post_ID);
    $post_links_temp = wp_extract_urls($content);
    foreach ($pung as $link_test) {
        if (!in_array($link_test, $post_links_temp)) {
            // link no longer in post
            $mids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post_ID, $wpdb->esc_like($link_test) . '%'));
            foreach ($mids as $mid) {
                delete_metadata_by_mid('post', $mid);
            }
        }
    }
    foreach ((array) $post_links_temp as $link_test) {
        if (!in_array($link_test, $pung)) {
            // If we haven't pung it already
            $test = @parse_url($link_test);
            if (false === $test) {
                continue;
            }
            if (isset($test['query'])) {
                $post_links[] = $link_test;
            } elseif (isset($test['path']) && $test['path'] != '/' && $test['path'] != '') {
                $post_links[] = $link_test;
            }
        }
    }
    /**
     * Filters the list of enclosure links before querying the database.
     *
     * Allows for the addition and/or removal of potential enclosures to save
     * to postmeta before checking the database for existing enclosures.
     *
     * @since 4.4.0
     *
     * @param array $post_links An array of enclosure links.
     * @param int   $post_ID    Post ID.
     */
    $post_links = apply_filters('enclosure_links', $post_links, $post_ID);
    foreach ((array) $post_links as $url) {
        if ($url != '' && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post_ID, $wpdb->esc_like($url) . '%'))) {
            if ($headers = wp_get_http_headers($url)) {
                $len = isset($headers['content-length']) ? (int) $headers['content-length'] : 0;
                $type = isset($headers['content-type']) ? $headers['content-type'] : '';
                $allowed_types = array('video', 'audio');
                // Check to see if we can figure out the mime type from
                // the extension
                $url_parts = @parse_url($url);
                if (false !== $url_parts) {
                    $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION);
                    if (!empty($extension)) {
                        foreach (wp_get_mime_types() as $exts => $mime) {
                            if (preg_match('!^(' . $exts . ')$!i', $extension)) {
                                $type = $mime;
                                break;
                            }
                        }
                    }
                }
                if (in_array(substr($type, 0, strpos($type, '/')), $allowed_types)) {
                    add_post_meta($post_ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n");
                }
            }
        }
    }
}

WordPress Version: 4.6

/**
 * Check content for video and audio links to add as enclosures.
 *
 * Will not add enclosures that have already been added and will
 * remove enclosures that are no longer in the post. This is called as
 * pingbacks and trackbacks.
 *
 * @since 1.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $content Post Content.
 * @param int    $post_ID Post ID.
 */
function do_enclose($content, $post_ID)
{
    global $wpdb;
    //TODO: Tidy this ghetto code up and make the debug code optional
    include_once ABSPATH . WPINC . '/class-IXR.php';
    $post_links = array();
    $pung = get_enclosed($post_ID);
    $post_links_temp = wp_extract_urls($content);
    foreach ($pung as $link_test) {
        if (!in_array($link_test, $post_links_temp)) {
            // link no longer in post
            $mids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post_ID, $wpdb->esc_like($link_test) . '%'));
            foreach ($mids as $mid) {
                delete_metadata_by_mid('post', $mid);
            }
        }
    }
    foreach ((array) $post_links_temp as $link_test) {
        if (!in_array($link_test, $pung)) {
            // If we haven't pung it already
            $test = @parse_url($link_test);
            if (false === $test) {
                continue;
            }
            if (isset($test['query'])) {
                $post_links[] = $link_test;
            } elseif (isset($test['path']) && $test['path'] != '/' && $test['path'] != '') {
                $post_links[] = $link_test;
            }
        }
    }
    /**
     * Filters the list of enclosure links before querying the database.
     *
     * Allows for the addition and/or removal of potential enclosures to save
     * to postmeta before checking the database for existing enclosures.
     *
     * @since 4.4.0
     *
     * @param array $post_links An array of enclosure links.
     * @param int   $post_ID    Post ID.
     */
    $post_links = apply_filters('enclosure_links', $post_links, $post_ID);
    foreach ((array) $post_links as $url) {
        if ($url != '' && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post_ID, $wpdb->esc_like($url) . '%'))) {
            if ($headers = wp_get_http_headers($url)) {
                $len = isset($headers['content-length']) ? (int) $headers['content-length'] : 0;
                $type = isset($headers['content-type']) ? $headers['content-type'] : '';
                $allowed_types = array('video', 'audio');
                // Check to see if we can figure out the mime type from
                // the extension
                $url_parts = @parse_url($url);
                if (false !== $url_parts) {
                    $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION);
                    if (!empty($extension)) {
                        foreach (wp_get_mime_types() as $exts => $mime) {
                            if (preg_match('!^(' . $exts . ')$!i', $extension)) {
                                $type = $mime;
                                break;
                            }
                        }
                    }
                }
                if (in_array(substr($type, 0, strpos($type, "/")), $allowed_types)) {
                    add_post_meta($post_ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n");
                }
            }
        }
    }
}

WordPress Version: 4.4

/**
 * Check content for video and audio links to add as enclosures.
 *
 * Will not add enclosures that have already been added and will
 * remove enclosures that are no longer in the post. This is called as
 * pingbacks and trackbacks.
 *
 * @since 1.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $content Post Content.
 * @param int    $post_ID Post ID.
 */
function do_enclose($content, $post_ID)
{
    global $wpdb;
    //TODO: Tidy this ghetto code up and make the debug code optional
    include_once ABSPATH . WPINC . '/class-IXR.php';
    $post_links = array();
    $pung = get_enclosed($post_ID);
    $post_links_temp = wp_extract_urls($content);
    foreach ($pung as $link_test) {
        if (!in_array($link_test, $post_links_temp)) {
            // link no longer in post
            $mids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post_ID, $wpdb->esc_like($link_test) . '%'));
            foreach ($mids as $mid) {
                delete_metadata_by_mid('post', $mid);
            }
        }
    }
    foreach ((array) $post_links_temp as $link_test) {
        if (!in_array($link_test, $pung)) {
            // If we haven't pung it already
            $test = @parse_url($link_test);
            if (false === $test) {
                continue;
            }
            if (isset($test['query'])) {
                $post_links[] = $link_test;
            } elseif (isset($test['path']) && $test['path'] != '/' && $test['path'] != '') {
                $post_links[] = $link_test;
            }
        }
    }
    /**
     * Filter the list of enclosure links before querying the database.
     *
     * Allows for the addition and/or removal of potential enclosures to save
     * to postmeta before checking the database for existing enclosures.
     *
     * @since 4.4.0
     *
     * @param array $post_links An array of enclosure links.
     * @param int   $post_ID    Post ID.
     */
    $post_links = apply_filters('enclosure_links', $post_links, $post_ID);
    foreach ((array) $post_links as $url) {
        if ($url != '' && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post_ID, $wpdb->esc_like($url) . '%'))) {
            if ($headers = wp_get_http_headers($url)) {
                $len = isset($headers['content-length']) ? (int) $headers['content-length'] : 0;
                $type = isset($headers['content-type']) ? $headers['content-type'] : '';
                $allowed_types = array('video', 'audio');
                // Check to see if we can figure out the mime type from
                // the extension
                $url_parts = @parse_url($url);
                if (false !== $url_parts) {
                    $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION);
                    if (!empty($extension)) {
                        foreach (wp_get_mime_types() as $exts => $mime) {
                            if (preg_match('!^(' . $exts . ')$!i', $extension)) {
                                $type = $mime;
                                break;
                            }
                        }
                    }
                }
                if (in_array(substr($type, 0, strpos($type, "/")), $allowed_types)) {
                    add_post_meta($post_ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n");
                }
            }
        }
    }
}

WordPress Version: 4.3

/**
 * Check content for video and audio links to add as enclosures.
 *
 * Will not add enclosures that have already been added and will
 * remove enclosures that are no longer in the post. This is called as
 * pingbacks and trackbacks.
 *
 * @since 1.5.0
 *
 * @global wpdb $wpdb
 *
 * @param string $content Post Content.
 * @param int    $post_ID Post ID.
 */
function do_enclose($content, $post_ID)
{
    global $wpdb;
    //TODO: Tidy this ghetto code up and make the debug code optional
    include_once ABSPATH . WPINC . '/class-IXR.php';
    $post_links = array();
    $pung = get_enclosed($post_ID);
    $post_links_temp = wp_extract_urls($content);
    foreach ($pung as $link_test) {
        if (!in_array($link_test, $post_links_temp)) {
            // link no longer in post
            $mids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post_ID, $wpdb->esc_like($link_test) . '%'));
            foreach ($mids as $mid) {
                delete_metadata_by_mid('post', $mid);
            }
        }
    }
    foreach ((array) $post_links_temp as $link_test) {
        if (!in_array($link_test, $pung)) {
            // If we haven't pung it already
            $test = @parse_url($link_test);
            if (false === $test) {
                continue;
            }
            if (isset($test['query'])) {
                $post_links[] = $link_test;
            } elseif (isset($test['path']) && $test['path'] != '/' && $test['path'] != '') {
                $post_links[] = $link_test;
            }
        }
    }
    foreach ((array) $post_links as $url) {
        if ($url != '' && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post_ID, $wpdb->esc_like($url) . '%'))) {
            if ($headers = wp_get_http_headers($url)) {
                $len = isset($headers['content-length']) ? (int) $headers['content-length'] : 0;
                $type = isset($headers['content-type']) ? $headers['content-type'] : '';
                $allowed_types = array('video', 'audio');
                // Check to see if we can figure out the mime type from
                // the extension
                $url_parts = @parse_url($url);
                if (false !== $url_parts) {
                    $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION);
                    if (!empty($extension)) {
                        foreach (wp_get_mime_types() as $exts => $mime) {
                            if (preg_match('!^(' . $exts . ')$!i', $extension)) {
                                $type = $mime;
                                break;
                            }
                        }
                    }
                }
                if (in_array(substr($type, 0, strpos($type, "/")), $allowed_types)) {
                    add_post_meta($post_ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n");
                }
            }
        }
    }
}

WordPress Version: 4.0

/**
 * Check content for video and audio links to add as enclosures.
 *
 * Will not add enclosures that have already been added and will
 * remove enclosures that are no longer in the post. This is called as
 * pingbacks and trackbacks.
 *
 * @since 1.5.0
 *
 * @see $wpdb
 *
 * @param string $content Post Content.
 * @param int $post_ID Post ID.
 */
function do_enclose($content, $post_ID)
{
    global $wpdb;
    //TODO: Tidy this ghetto code up and make the debug code optional
    include_once ABSPATH . WPINC . '/class-IXR.php';
    $post_links = array();
    $pung = get_enclosed($post_ID);
    $post_links_temp = wp_extract_urls($content);
    foreach ($pung as $link_test) {
        if (!in_array($link_test, $post_links_temp)) {
            // link no longer in post
            $mids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post_ID, $wpdb->esc_like($link_test) . '%'));
            foreach ($mids as $mid) {
                delete_metadata_by_mid('post', $mid);
            }
        }
    }
    foreach ((array) $post_links_temp as $link_test) {
        if (!in_array($link_test, $pung)) {
            // If we haven't pung it already
            $test = @parse_url($link_test);
            if (false === $test) {
                continue;
            }
            if (isset($test['query'])) {
                $post_links[] = $link_test;
            } elseif (isset($test['path']) && $test['path'] != '/' && $test['path'] != '') {
                $post_links[] = $link_test;
            }
        }
    }
    foreach ((array) $post_links as $url) {
        if ($url != '' && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post_ID, $wpdb->esc_like($url) . '%'))) {
            if ($headers = wp_get_http_headers($url)) {
                $len = isset($headers['content-length']) ? (int) $headers['content-length'] : 0;
                $type = isset($headers['content-type']) ? $headers['content-type'] : '';
                $allowed_types = array('video', 'audio');
                // Check to see if we can figure out the mime type from
                // the extension
                $url_parts = @parse_url($url);
                if (false !== $url_parts) {
                    $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION);
                    if (!empty($extension)) {
                        foreach (wp_get_mime_types() as $exts => $mime) {
                            if (preg_match('!^(' . $exts . ')$!i', $extension)) {
                                $type = $mime;
                                break;
                            }
                        }
                    }
                }
                if (in_array(substr($type, 0, strpos($type, "/")), $allowed_types)) {
                    add_post_meta($post_ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n");
                }
            }
        }
    }
}

WordPress Version: 3.9

/**
 * Check content for video and audio links to add as enclosures.
 *
 * Will not add enclosures that have already been added and will
 * remove enclosures that are no longer in the post. This is called as
 * pingbacks and trackbacks.
 *
 * @since 1.5.0
 *
 * @uses $wpdb
 *
 * @param string $content Post Content
 * @param int $post_ID Post ID
 */
function do_enclose($content, $post_ID)
{
    global $wpdb;
    //TODO: Tidy this ghetto code up and make the debug code optional
    include_once ABSPATH . WPINC . '/class-IXR.php';
    $post_links = array();
    $pung = get_enclosed($post_ID);
    $post_links_temp = wp_extract_urls($content);
    foreach ($pung as $link_test) {
        if (!in_array($link_test, $post_links_temp)) {
            // link no longer in post
            $mids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, like_escape($link_test) . '%'));
            foreach ($mids as $mid) {
                delete_metadata_by_mid('post', $mid);
            }
        }
    }
    foreach ((array) $post_links_temp as $link_test) {
        if (!in_array($link_test, $pung)) {
            // If we haven't pung it already
            $test = @parse_url($link_test);
            if (false === $test) {
                continue;
            }
            if (isset($test['query'])) {
                $post_links[] = $link_test;
            } elseif (isset($test['path']) && $test['path'] != '/' && $test['path'] != '') {
                $post_links[] = $link_test;
            }
        }
    }
    foreach ((array) $post_links as $url) {
        if ($url != '' && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, like_escape($url) . '%'))) {
            if ($headers = wp_get_http_headers($url)) {
                $len = isset($headers['content-length']) ? (int) $headers['content-length'] : 0;
                $type = isset($headers['content-type']) ? $headers['content-type'] : '';
                $allowed_types = array('video', 'audio');
                // Check to see if we can figure out the mime type from
                // the extension
                $url_parts = @parse_url($url);
                if (false !== $url_parts) {
                    $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION);
                    if (!empty($extension)) {
                        foreach (wp_get_mime_types() as $exts => $mime) {
                            if (preg_match('!^(' . $exts . ')$!i', $extension)) {
                                $type = $mime;
                                break;
                            }
                        }
                    }
                }
                if (in_array(substr($type, 0, strpos($type, "/")), $allowed_types)) {
                    add_post_meta($post_ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n");
                }
            }
        }
    }
}

WordPress Version: 3.7

/**
 * Check content for video and audio links to add as enclosures.
 *
 * Will not add enclosures that have already been added and will
 * remove enclosures that are no longer in the post. This is called as
 * pingbacks and trackbacks.
 *
 * @package WordPress
 * @since 1.5.0
 *
 * @uses $wpdb
 *
 * @param string $content Post Content
 * @param int $post_ID Post ID
 */
function do_enclose($content, $post_ID)
{
    global $wpdb;
    //TODO: Tidy this ghetto code up and make the debug code optional
    include_once ABSPATH . WPINC . '/class-IXR.php';
    $post_links = array();
    $pung = get_enclosed($post_ID);
    $post_links_temp = wp_extract_urls($content);
    foreach ($pung as $link_test) {
        if (!in_array($link_test, $post_links_temp)) {
            // link no longer in post
            $mids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, like_escape($link_test) . '%'));
            foreach ($mids as $mid) {
                delete_metadata_by_mid('post', $mid);
            }
        }
    }
    foreach ((array) $post_links_temp as $link_test) {
        if (!in_array($link_test, $pung)) {
            // If we haven't pung it already
            $test = @parse_url($link_test);
            if (false === $test) {
                continue;
            }
            if (isset($test['query'])) {
                $post_links[] = $link_test;
            } elseif (isset($test['path']) && $test['path'] != '/' && $test['path'] != '') {
                $post_links[] = $link_test;
            }
        }
    }
    foreach ((array) $post_links as $url) {
        if ($url != '' && !$wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, like_escape($url) . '%'))) {
            if ($headers = wp_get_http_headers($url)) {
                $len = isset($headers['content-length']) ? (int) $headers['content-length'] : 0;
                $type = isset($headers['content-type']) ? $headers['content-type'] : '';
                $allowed_types = array('video', 'audio');
                // Check to see if we can figure out the mime type from
                // the extension
                $url_parts = @parse_url($url);
                if (false !== $url_parts) {
                    $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION);
                    if (!empty($extension)) {
                        foreach (wp_get_mime_types() as $exts => $mime) {
                            if (preg_match('!^(' . $exts . ')$!i', $extension)) {
                                $type = $mime;
                                break;
                            }
                        }
                    }
                }
                if (in_array(substr($type, 0, strpos($type, "/")), $allowed_types)) {
                    add_post_meta($post_ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n");
                }
            }
        }
    }
}