do_enclose

Check content for video and audio links to add as enclosures.

Description

do_enclose( (string) $content, (int) $post_ID ); 

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.

Parameters (2)

0. $content (string)
Post Content.
1. $post_id (int)
The post id.

Usage

  1. if ( !function_exists( 'do_enclose' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // Post Content. 
  5. $content = ''; 
  6.  
  7. // The post id. 
  8. $post_id = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = do_enclose($content, $post_id); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function do_enclose( $content, $post_ID ) { 
  2. global $wpdb; 
  3.  
  4. //TODO: Tidy this ghetto code up and make the debug code optional 
  5. include_once( ABSPATH . WPINC . '/class-IXR.php' ); 
  6.  
  7. $post_links = array(); 
  8.  
  9. $pung = get_enclosed( $post_ID ); 
  10.  
  11. $post_links_temp = wp_extract_urls( $content ); 
  12.  
  13. foreach ( $pung as $link_test ) { 
  14. if ( ! in_array( $link_test, $post_links_temp ) ) { // link no longer in post 
  15. $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 ) . '%') ); 
  16. foreach ( $mids as $mid ) 
  17. delete_metadata_by_mid( 'post', $mid ); 
  18.  
  19. foreach ( (array) $post_links_temp as $link_test ) { 
  20. if ( !in_array( $link_test, $pung ) ) { // If we haven't pung it already 
  21. $test = @parse_url( $link_test ); 
  22. if ( false === $test ) 
  23. continue; 
  24. if ( isset( $test['query'] ) ) 
  25. $post_links[] = $link_test; 
  26. elseif ( isset($test['path']) && ( $test['path'] != '/' ) && ($test['path'] != '' ) ) 
  27. $post_links[] = $link_test; 
  28.  
  29. /** 
  30. * Filters the list of enclosure links before querying the database. 
  31. * Allows for the addition and/or removal of potential enclosures to save 
  32. * to postmeta before checking the database for existing enclosures. 
  33. * @since 4.4.0 
  34. * @param array $post_links An array of enclosure links. 
  35. * @param int $post_ID Post ID. 
  36. */ 
  37. $post_links = apply_filters( 'enclosure_links', $post_links, $post_ID ); 
  38.  
  39. foreach ( (array) $post_links as $url ) { 
  40. 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 ) . '%' ) ) ) { 
  41.  
  42. if ( $headers = wp_get_http_headers( $url) ) { 
  43. $len = isset( $headers['content-length'] ) ? (int) $headers['content-length'] : 0; 
  44. $type = isset( $headers['content-type'] ) ? $headers['content-type'] : ''; 
  45. $allowed_types = array( 'video', 'audio' ); 
  46.  
  47. // Check to see if we can figure out the mime type from 
  48. // the extension 
  49. $url_parts = @parse_url( $url ); 
  50. if ( false !== $url_parts ) { 
  51. $extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION ); 
  52. if ( !empty( $extension ) ) { 
  53. foreach ( wp_get_mime_types() as $exts => $mime ) { 
  54. if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) { 
  55. $type = $mime; 
  56. break; 
  57.  
  58. if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) { 
  59. add_post_meta( $post_ID, 'enclosure', "$url\n$len\n$mime\n" );