This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.
Image_Import::import_to_media( $image_url )
Parameters
- $image_url
-
(Required)
Return
(array|bool)
Source
File: includes/libs/image-import.class.php
private function import_to_media( $image_url ){ if( !$image_url ){ Helper::debug_message( sprintf( 'Post #%d featured image not set because no image URL was detected.', $this->video_post->get_post()->ID ) ); return false; } // get the thumbnail $request = wp_remote_get( $image_url, [ 'user-agent' => Helper::request_user_agent(), 'sslverify' => false, /** * Request timeout filter. * Video image import request timeout in seconds. * * @param int $timeout Remote request timeout in seconds. */ 'timeout' => apply_filters( 'vimeotheque\image_request_timeout', 30 ) ] ); if( is_wp_error( $request ) || 200 != wp_remote_retrieve_response_code( $request ) ) { $error_message = is_wp_error( $request ) ? sprintf( 'generated error "%s"', $request->get_error_message() ) : sprintf( 'returned response code "%s"', wp_remote_retrieve_response_code( $request ) ); Helper::debug_message( sprintf( 'Remote request to URL %s for featured image setup on post ID #%d %s.', $image_url, $this->video_post->get_post()->ID, $error_message ) ); return false; } $image_contents = $request['body']; $image_type = wp_remote_retrieve_header( $request, 'content-type' ); // Translate MIME type into an extension if ( $image_type == 'image/jpeg' ){ $image_extension = '.jpg'; }elseif ( $image_type == 'image/png' ){ $image_extension = '.png'; } if( !isset( $image_extension ) ){ Helper::debug_message( sprintf( 'Could not determine extension for image "%s".', $image_url ) ); return false; } // Construct a file name using post slug and extension $fname = urldecode( basename( get_permalink( $this->video_post->get_post()->ID ) ) ) ; $new_filename = preg_replace( '/[^A-Za-z0-9\-]/', '', $fname ) . '-vimeo-thumbnail' . $image_extension; // Save the image bits using the new filename $upload = wp_upload_bits( $new_filename, null, $image_contents ); if ( $upload['error'] ) { Helper::debug_message( sprintf( 'The following error was encountered during the file upload in WP: "%s".', $upload['error'] ) ); return false; } $_image_url = $upload['url']; $filename = $upload['file']; /** * Action that allows modification of image that will be attached to video post. * * @param string $filename Complete path to original video image within WP gallery. * @param int $post_id The post ID that the image will be attached to as featured image. * @param string $video_id The video ID from Vimeo. */ do_action( 'vimeotheque\image_file_raw', $filename, $this->video_post->get_post()->ID, $this->video_post->video_id ); $wp_filetype = wp_check_filetype( basename( $filename ), null ); $attachment = [ 'post_mime_type' => $wp_filetype['type'], 'post_title' => get_the_title( $this->video_post->get_post()->ID ).' - Vimeo thumbnail', 'post_content' => '', 'post_status' => 'inherit', 'guid' => $_image_url ]; $attach_id = wp_insert_attachment( $attachment, $filename, $this->video_post->get_post()->ID ); if( is_wp_error( $attach_id ) ){ Helper::debug_message( sprintf( 'The following error was encountered when trying to insert the new attachment into the database: "%s".', $attach_id->get_error_message() ) ); return; } // you must first include the image.php file // for the function wp_generate_attachment_metadata() to work require_once( ABSPATH . 'wp-admin/includes/image.php' ); $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); wp_update_attachment_metadata( $attach_id, $attach_data ); // Add field to mark image as a video thumbnail update_post_meta( $attach_id, 'video_thumbnail', $this->video_post->video_id ); // set image as featured for current post update_post_meta( $this->video_post->get_post()->ID, '_thumbnail_id', $attach_id ); /** * Trigger action on plugin import. * * @param int $attachment_id ID of attachment create. * @param string $video_id The video ID from Vimeo being processed. * @param int $post_id The post ID that has the attachment. */ do_action( 'vimeotheque\image_imported', $attach_id, $this->video_post->video_id, $this->video_post->get_post()->ID ); Helper::debug_message( sprintf( 'Image imported successfully from %s into attachment #%d and set as featured image for post #%d.', $image_url, $attach_id, $this->video_post->get_post()->ID ) ); return [ 'post_id' => $this->video_post->get_post()->ID, 'attachment_id' => $attach_id ]; }