Definition:
function do_enclose( $content, $post_ID ) {}
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.
Parameters
- string $content: Post Content
- int $post_ID: Post ID
Defined actions
- delete_postmeta
do_action( 'delete_postmeta', $mid );
- deleted_postmeta
do_action( 'deleted_postmeta', $mid );
- added_postmeta
do_action( 'added_postmeta', $wpdb->insert_id, $post_ID, 'enclosure', $meta_value );
Source code
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' ); $log = debug_fopen( ABSPATH . 'enclosures.log', 'a' ); $post_links = array(); debug_fwrite( $log, 'BEGIN ' . date( 'YmdHis', time() ) . "\n" ); $pung = get_enclosed( $post_ID ); $ltrs = '\w'; $gunk = '/#~:.?+=&%@!\-'; $punc = '.:?\-'; $any = $ltrs . $gunk . $punc; preg_match_all( "{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp ); debug_fwrite( $log, 'Post contents:' ); debug_fwrite( $log, $content . "\n" ); foreach ( $pung as $link_test ) { if ( !in_array( $link_test, $post_links_temp[0] ) ) { // link no longer in post $mid = $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 ) . '%') ); do_action( 'delete_postmeta', $mid ); $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id IN(%s)", implode( ',', $mid ) ) ); do_action( 'deleted_postmeta', $mid ); } } foreach ( (array) $post_links_temp[0] 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 = (int) $headers['content-length']; $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 ( get_allowed_mime_types( ) as $exts => $mime ) { if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) { $type = $mime; break; } } } } if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) { $meta_value = "$url\n$len\n$type\n"; $wpdb->insert($wpdb->postmeta, array('post_id' => $post_ID, 'meta_key' => 'enclosure', 'meta_value' => $meta_value) ); do_action( 'added_postmeta', $wpdb->insert_id, $post_ID, 'enclosure', $meta_value ); } } } } }
954
No comments yet... Be the first to leave a reply!