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

186private function import_to_media( $image_url ){
187    if( !$image_url ){
188        Helper::debug_message(
189            sprintf(
190                'Post #%d featured image not set because no image URL was detected.',
191                $this->video_post->get_post()->ID
192            )
193        );
194 
195        return false;
196    }
197 
198    // get the thumbnail
199    $request = wp_remote_get(
200        $image_url,
201        [
202            'user-agent' => Helper::request_user_agent(),
203            'sslverify' => false,
204            /**
205             * Request timeout filter.
206             * Video image import request timeout in seconds.
207             *
208             * @param int $timeout Remote request timeout in seconds.
209             */
210            'timeout' => apply_filters( 'vimeotheque\image_request_timeout', 30 )
211        ]
212    );
213 
214    if( is_wp_error( $request ) || 200 != wp_remote_retrieve_response_code( $request ) ) {
215 
216        $error_message = is_wp_error( $request ) ?
217            sprintf( 'generated error "%s"', $request->get_error_message() ) :
218            sprintf( 'returned response code "%s"', wp_remote_retrieve_response_code( $request ) );
219 
220        Helper::debug_message(
221            sprintf(
222                'Remote request to URL %s for featured image setup on post ID #%d %s.',
223                $image_url,
224                $this->video_post->get_post()->ID,
225                $error_message
226            )
227        );
228 
229        return false;
230    }
231 
232    $image_contents = $request['body'];
233    $image_type = wp_remote_retrieve_header( $request, 'content-type' );
234    // Translate MIME type into an extension
235    if ( $image_type == 'image/jpeg' ){
236        $image_extension = '.jpg';
237    }elseif ( $image_type == 'image/png' ){
238        $image_extension = '.png';
239    }
240 
241    if( !isset( $image_extension ) ){
242        Helper::debug_message(
243            sprintf(
244                'Could not determine extension for image "%s".',
245                $image_url
246            )
247        );
248 
249        return false;
250    }
251 
252    // Construct a file name using post slug and extension
253    $fname = urldecode( basename( get_permalink( $this->video_post->get_post()->ID ) ) ) ;
254    $new_filename = preg_replace( '/[^A-Za-z0-9\-]/', '', $fname ) .
255                    '-vimeo-thumbnail' .
256                    $image_extension;
257 
258    // Save the image bits using the new filename
259    $upload = wp_upload_bits( $new_filename, null, $image_contents );
260    if ( $upload['error'] ) {
261 
262        Helper::debug_message(
263            sprintf(
264                'The following error was encountered during the file upload in WP: "%s".',
265                $upload['error']
266            )
267        );
268 
269        return false;
270    }
271 
272    $_image_url = $upload['url'];
273    $filename = $upload['file'];
274 
275    /**
276     * Action that allows modification of image that will be attached to video post.
277     *
278     * @param string $filename  Complete path to original video image within WP gallery.
279     * @param int $post_id      The post ID that the image will be attached to as featured image.
280     * @param string $video_id  The video ID from Vimeo.
281     */
282    do_action(
283        'vimeotheque\image_file_raw',
284        $filename,
285        $this->video_post->get_post()->ID,
286        $this->video_post->video_id
287    );
288 
289    $wp_filetype = wp_check_filetype( basename( $filename ), null );
290    $attachment = [
291        'post_mime_type'    => $wp_filetype['type'],
292        'post_title'        => get_the_title( $this->video_post->get_post()->ID ).' - Vimeo thumbnail',
293        'post_content'      => '',
294        'post_status'       => 'inherit',
295        'guid'              => $_image_url
296    ];
297    $attach_id = wp_insert_attachment( $attachment, $filename, $this->video_post->get_post()->ID );
298 
299    if( is_wp_error( $attach_id ) ){
300        Helper::debug_message(
301            sprintf(
302                'The following error was encountered when trying to insert the new attachment into the database: "%s".',
303                $attach_id->get_error_message()
304            )
305        );
306        return;
307    }
308 
309    // you must first include the image.php file
310    // for the function wp_generate_attachment_metadata() to work
311    require_once( ABSPATH . 'wp-admin/includes/image.php' );
312    $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
313    wp_update_attachment_metadata( $attach_id, $attach_data );
314 
315    // Add field to mark image as a video thumbnail
316    update_post_meta(
317        $attach_id,
318        'video_thumbnail',
319        $this->video_post->video_id
320    );
321 
322    // set image as featured for current post
323    update_post_meta(
324        $this->video_post->get_post()->ID,
325        '_thumbnail_id',
326        $attach_id
327    );
328 
329    /**
330     * Trigger action on plugin import.
331     *
332     * @param int $attachment_id    ID of attachment create.
333     * @param string $video_id      The video ID from Vimeo being processed.
334     * @param int $post_id          The post ID that has the attachment.
335     */
336    do_action(
337        'vimeotheque\image_imported',
338        $attach_id,
339        $this->video_post->video_id,
340        $this->video_post->get_post()->ID
341    );
342 
343    Helper::debug_message(
344        sprintf(
345            'Image imported successfully from %s into attachment #%d and set as featured image for post #%d.',
346            $image_url,
347            $attach_id,
348            $this->video_post->get_post()->ID
349        )
350    );
351 
352    return [
353        'post_id'       => $this->video_post->get_post()->ID,
354        'attachment_id' => $attach_id
355    ];
356}

Start your video site now!

Manage and coordinate your Vimeo channels, albums or videos with your WordPress website. Perfect fit for membership, portfolio, online courses or any type of video collection.

Get Vimeotheque PRO!