| @@ -1,0 +1,185 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Attachments Functions | |
| 4 | + * | |
| 5 | + * @package GamiPress\Attachments_Functions | |
| 6 | + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.7.1 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Attempt to find the attachment ID from the file URL or, if comes from external URL, attempt to download and insert it as attachment | |
| 14 | + * | |
| 15 | + * @since 1.7.0 | |
| 16 | + * | |
| 17 | + * @param string $url Attachment URL | |
| 18 | + * | |
| 19 | + * @return int|WP_Error Attachment ID on success, WP_Error otherwise | |
| 20 | + */ | |
| 21 | +function gamipress_import_attachment( $url ) { | |
| 22 | + | |
| 23 | + $site_url = site_url(); | |
| 24 | + | |
| 25 | + // If the URL is absolute, but does not contain address, then upload it assuming base_site_url | |
| 26 | + if ( preg_match( '|^/[\w\W]+$|', $url ) ) | |
| 27 | + $url = rtrim( $site_url, '/' ) . $url; | |
| 28 | + | |
| 29 | + $thumbnail_id = gamipress_get_attachment_id_from_url( $url ); | |
| 30 | + | |
| 31 | + if( $thumbnail_id ) { | |
| 32 | + return $thumbnail_id; | |
| 33 | + } else { | |
| 34 | + // Remove protocol for following checks | |
| 35 | + $site_url = str_replace( array( 'https://', 'http://' ), array( '', '' ), $site_url ); | |
| 36 | + | |
| 37 | + if( strpos( $url, $site_url ) === false ) | |
| 38 | + return gamipress_insert_external_attachment( $url ); | |
| 39 | + } | |
| 40 | + | |
| 41 | + return new WP_Error( 'attachment_import_error', __('Attachment not found', 'gamipress') ); | |
| 42 | + | |
| 43 | +} | |
| 44 | + | |
| 45 | +/** | |
| 46 | + * Retrieves the attachment ID from the file URL | |
| 47 | + * | |
| 48 | + * @since 1.7.0 | |
| 49 | + * | |
| 50 | + * @param string $url Attachment URL | |
| 51 | + * | |
| 52 | + * @return int|false Attachment ID on success, false otherwise | |
| 53 | + */ | |
| 54 | +function gamipress_get_attachment_id_from_url( $url ) { | |
| 55 | + | |
| 56 | + global $wpdb; | |
| 57 | + | |
| 58 | + $attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid='%s';", $url ) ); | |
| 59 | + | |
| 60 | + return isset( $attachment[0] ) ? $attachment[0] : false; | |
| 61 | + | |
| 62 | +} | |
| 63 | + | |
| 64 | +/** | |
| 65 | + * Attempt to create a new attachment from an external URL | |
| 66 | + * | |
| 67 | + * Taken from WordPress Importer tool | |
| 68 | + * | |
| 69 | + * @since 1.7.0 | |
| 70 | + * | |
| 71 | + * @param string $url URL to fetch attachment from | |
| 72 | + * @param array $post Attachment post details | |
| 73 | + * | |
| 74 | + * @return int|WP_Error Post ID on success, WP_Error otherwise | |
| 75 | + */ | |
| 76 | +function gamipress_insert_external_attachment( $url, $post = array() ) { | |
| 77 | + | |
| 78 | + $upload = gamipress_fetch_remote_file( $url ); | |
| 79 | + | |
| 80 | + if ( is_wp_error( $upload ) ) | |
| 81 | + return $upload; | |
| 82 | + | |
| 83 | + if ( $info = wp_check_filetype( $upload['file'] ) ) | |
| 84 | + $post['post_mime_type'] = $info['type']; | |
| 85 | + else | |
| 86 | + return new WP_Error( 'attachment_processing_error', __('Invalid file type', 'gamipress') ); | |
| 87 | + | |
| 88 | + $post['guid'] = $upload['url']; | |
| 89 | + | |
| 90 | + // as per wp-admin/includes/upload.php | |
| 91 | + $post_id = wp_insert_attachment( $post, $upload['file'] ); | |
| 92 | + wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) ); | |
| 93 | + | |
| 94 | + return $post_id; | |
| 95 | + | |
| 96 | +} | |
| 97 | + | |
| 98 | +/** | |
| 99 | + * Attempt to download a remote file attachment | |
| 100 | + * | |
| 101 | + * Taken from WordPress Importer tool | |
| 102 | + * | |
| 103 | + * @since 1.7.0 | |
| 104 | + * | |
| 105 | + * @param string $url URL of item to fetch | |
| 106 | + * | |
| 107 | + * @return array|WP_Error Local file location details on success, WP_Error otherwise | |
| 108 | + */ | |
| 109 | +function gamipress_fetch_remote_file( $url ) { | |
| 110 | + | |
| 111 | + if( ! filter_var( $url, FILTER_VALIDATE_URL ) ) { | |
| 112 | + return new WP_Error( 'import_url_error', __('Invalid URL', 'gamipress') ); | |
| 113 | + } | |
| 114 | + | |
| 115 | + // extract the file name and extension from the url | |
| 116 | + $file_name = basename( $url ); | |
| 117 | + | |
| 118 | + // get placeholder file in the upload dir with a unique, sanitized filename | |
| 119 | + $upload = wp_upload_bits( $file_name, 0, '', null ); | |
| 120 | + | |
| 121 | + if ( $upload['error'] ) | |
| 122 | + return new WP_Error( 'upload_dir_error', $upload['error'] ); | |
| 123 | + | |
| 124 | + // fetch the remote url and write it to the placeholder file | |
| 125 | + $remote_response = wp_safe_remote_get( $url, array( | |
| 126 | + 'timeout' => 300, | |
| 127 | + 'stream' => true, | |
| 128 | + 'filename' => $upload['file'], | |
| 129 | + ) ); | |
| 130 | + | |
| 131 | + $headers = wp_remote_retrieve_headers( $remote_response ); | |
| 132 | + | |
| 133 | + // request failed | |
| 134 | + if ( ! $headers ) { | |
| 135 | + @unlink( $upload['file'] ); | |
| 136 | + return new WP_Error( 'import_file_error', __('Remote server did not respond', 'gamipress') ); | |
| 137 | + } | |
| 138 | + | |
| 139 | + $remote_response_code = wp_remote_retrieve_response_code( $remote_response ); | |
| 140 | + | |
| 141 | + // make sure the fetch was successful | |
| 142 | + if ( $remote_response_code != '200' ) { | |
| 143 | + @unlink( $upload['file'] ); | |
| 144 | + return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'gamipress'), esc_html( $remote_response_code ), get_status_header_desc($remote_response_code) ) ); | |
| 145 | + } | |
| 146 | + | |
| 147 | + $filesize = filesize( $upload['file'] ); | |
| 148 | + | |
| 149 | + if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) { | |
| 150 | + @unlink( $upload['file'] ); | |
| 151 | + return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'gamipress') ); | |
| 152 | + } | |
| 153 | + | |
| 154 | + if ( 0 == $filesize ) { | |
| 155 | + @unlink( $upload['file'] ); | |
| 156 | + return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'gamipress') ); | |
| 157 | + } | |
| 158 | + | |
| 159 | + // To verify the file type | |
| 160 | + $file_verify = wp_check_filetype_and_ext( $upload['file'], $file_name ); | |
| 161 | + | |
| 162 | + // To check extension | |
| 163 | + if ( $file_verify['proper_filename_change_key'] ) { | |
| 164 | + @unlink( $upload['file'] ); | |
| 165 | + return new WP_Error( 'security_error', __('The contents of the file do not match its extension.', 'gamipress') ); | |
| 166 | + } | |
| 167 | + | |
| 168 | + // Multimedia MIMES allowed | |
| 169 | + $allowed_mimes = array( | |
| 170 | + 'image/jpeg', | |
| 171 | + 'image/png', | |
| 172 | + 'image/gif', | |
| 173 | + 'image/webp', | |
| 174 | + 'video/mp4', | |
| 175 | + 'audio/mpeg' | |
| 176 | + ); | |
| 177 | + | |
| 178 | + if ( ! in_array( $file_verify['type'], $allowed_mimes ) ) { | |
| 179 | + @unlink( $upload['file'] ); | |
| 180 | + return new WP_Error( 'security_error', __('File type not allowed. Only multimedia files are accepted.', 'gamipress') ); | |
| 181 | + } | |
| 182 | + | |
| 183 | + return $upload; | |
| 184 | + | |
| 185 | +} | |