PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.0.2
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.0.2
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / includes / class-convertkit-media-library.php

class-convertkit-media-library.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.0.2, at includes/class-convertkit-media-library.php

89 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Media Library class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Class to import a remote image to the WordPress Media Library.
11 *
12 * @since 2.2.8
13 */
14 class ConvertKit_Media_Library {
15
16 /**
17 * Imports a remote image into the WordPress Media Library
18 *
19 * @since 2.2.8
20 *
21 * @param string $remote_image_url Source URL.
22 * @param int $post_id Post ID to attach the Media Library image to.
23 * @param string $alt_tag Image Alt Tag (optional).
24 * @return WP_Error|int
25 */
26 public function import_remote_image( $remote_image_url, $post_id, $alt_tag = '' ) {
27
28 // Load required functions for image importing.
29 if ( ! function_exists( 'media_handle_upload' ) ) {
30 require_once ABSPATH . 'wp-admin/includes/image.php';
31 require_once ABSPATH . 'wp-admin/includes/file.php';
32 require_once ABSPATH . 'wp-admin/includes/media.php';
33 }
34
35 // Get the remote image.
36 $tmp = download_url( $remote_image_url );
37 if ( is_wp_error( $tmp ) ) {
38 return $tmp;
39 }
40
41 // Get image type.
42 $type = getimagesize( $tmp );
43 if ( ! isset( $type['mime'] ) ) {
44 return new WP_Error( __( 'Could not identify MIME type of imported image.', 'convertkit' ) );
45 }
46 list( $type, $ext ) = explode( '/', $type['mime'] );
47 unset( $type );
48
49 // Define image filename, excluding any parameters.
50 $file_array['name'] = strtok( basename( $remote_image_url ), '?' );
51 $file_array['tmp_name'] = $tmp;
52
53 // Add the extension to the filename, if it doesn't exist.
54 // This happens if we streamed an image URL with no extension specified.
55 // e.g. https://embed.filekitcdn.com/e/pX62TATVeCKK5QzkXWNLw3/iLRneK6yxY4WwQUdkkUMaq.
56 switch ( $ext ) {
57 case 'jpeg':
58 case 'jpg':
59 // If neither .jpeg or .jpg exist, append the extension.
60 if ( strpos( $file_array['name'], '.jpg' ) === false && strpos( $file_array['name'], '.jpeg' ) === false ) {
61 $file_array['name'] .= '.jpg';
62 }
63 break;
64
65 default:
66 if ( strpos( $file_array['name'], '.' . $ext ) === false ) {
67 $file_array['name'] .= '.' . $ext;
68 }
69 break;
70 }
71
72 // Import the image into the Media Library.
73 $image_id = media_handle_sideload( $file_array, $post_id, '' );
74 if ( is_wp_error( $image_id ) ) {
75 return $image_id;
76 }
77
78 // If an alt tag has been specified, set it now.
79 if ( ! empty( $alt_tag ) ) {
80 update_post_meta( $image_id, '_wp_attachment_image_alt', $alt_tag );
81 }
82
83 // Return the image ID.
84 return $image_id;
85
86 }
87
88 }
89