| @@ 80-120 (lines=41) @@ | ||
| 77 | * |
|
| 78 | * @return int|bool Attachment ID on success, false on failure |
|
| 79 | */ |
|
| 80 | function videopress_get_attachment_id_by_url( $url ) { |
|
| 81 | $wp_upload_dir = wp_upload_dir(); |
|
| 82 | // Strip out protocols, so it doesn't fail because searching for http: in https: dir. |
|
| 83 | $dir = set_url_scheme( trailingslashit( $wp_upload_dir['baseurl'] ), 'relative' ); |
|
| 84 | ||
| 85 | // Is URL in uploads directory? |
|
| 86 | if ( false !== strpos( $url, $dir ) ) { |
|
| 87 | ||
| 88 | $file = basename( $url ); |
|
| 89 | ||
| 90 | $query_args = array( |
|
| 91 | 'post_type' => 'attachment', |
|
| 92 | 'post_status' => 'inherit', |
|
| 93 | 'fields' => 'ids', |
|
| 94 | 'meta_query' => array( |
|
| 95 | array( |
|
| 96 | 'key' => '_wp_attachment_metadata', |
|
| 97 | 'compare' => 'LIKE', |
|
| 98 | 'value' => $file, |
|
| 99 | ), |
|
| 100 | ) |
|
| 101 | ); |
|
| 102 | ||
| 103 | $query = new WP_Query( $query_args ); |
|
| 104 | ||
| 105 | if ( $query->have_posts() ) { |
|
| 106 | foreach ( $query->posts as $attachment_id ) { |
|
| 107 | $meta = wp_get_attachment_metadata( $attachment_id ); |
|
| 108 | $original_file = basename( $meta['file'] ); |
|
| 109 | $cropped_files = wp_list_pluck( $meta['sizes'], 'file' ); |
|
| 110 | ||
| 111 | if ( $original_file === $file || in_array( $file, $cropped_files ) ) { |
|
| 112 | return (int) $attachment_id; |
|
| 113 | } |
|
| 114 | } |
|
| 115 | } |
|
| 116 | ||
| 117 | } |
|
| 118 | ||
| 119 | return false; |
|
| 120 | } |
|
| 121 | ||
| 122 | /** |
|
| 123 | * Similar to `media_sideload_image` -- but returns an ID. |
|
| @@ 64-103 (lines=40) @@ | ||
| 61 | * |
|
| 62 | * @return int|bool Attachment ID on success, false on failure |
|
| 63 | */ |
|
| 64 | function videopress_get_attachment_id_by_url( $url ) { |
|
| 65 | $wp_upload_dir = wp_upload_dir(); |
|
| 66 | // Strip out protocols, so it doesn't fail because searching for http: in https: dir. |
|
| 67 | $dir = set_url_scheme( trailingslashit( $wp_upload_dir['baseurl'] ), 'relative' ); |
|
| 68 | ||
| 69 | // Is URL in uploads directory? |
|
| 70 | if ( false !== strpos( $url, $dir ) ) { |
|
| 71 | ||
| 72 | $file = basename( $url ); |
|
| 73 | ||
| 74 | $query_args = array( |
|
| 75 | 'post_type' => 'attachment', |
|
| 76 | 'post_status' => 'inherit', |
|
| 77 | 'fields' => 'ids', |
|
| 78 | 'meta_query' => array( |
|
| 79 | array( |
|
| 80 | 'key' => '_wp_attachment_metadata', |
|
| 81 | 'compare' => 'LIKE', |
|
| 82 | 'value' => $file, |
|
| 83 | ), |
|
| 84 | ) |
|
| 85 | ); |
|
| 86 | ||
| 87 | $query = new WP_Query( $query_args ); |
|
| 88 | ||
| 89 | if ( $query->have_posts() ) { |
|
| 90 | foreach ( $query->posts as $attachment_id ) { |
|
| 91 | $meta = wp_get_attachment_metadata( $attachment_id ); |
|
| 92 | $original_file = basename( $meta['file'] ); |
|
| 93 | $cropped_files = wp_list_pluck( $meta['sizes'], 'file' ); |
|
| 94 | ||
| 95 | if ( $original_file === $file || in_array( $file, $cropped_files ) ) { |
|
| 96 | return (int) $attachment_id; |
|
| 97 | } |
|
| 98 | } |
|
| 99 | } |
|
| 100 | ||
| 101 | } |
|
| 102 | return false; |
|
| 103 | } |
|
| 104 | ||
| 105 | /** |
|
| 106 | * Similar to `media_sideload_image` -- but returns an ID. |
|