Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Validate user-supplied guid values against expected inputs |
||
| 5 | * |
||
| 6 | * @since 1.1 |
||
| 7 | * @param string $guid video identifier |
||
| 8 | * @return bool true if passes validation test |
||
| 9 | */ |
||
| 10 | function videopress_is_valid_guid( $guid ) { |
||
| 11 | if ( ! empty( $guid ) && strlen( $guid ) === 8 && ctype_alnum( $guid ) ) { |
||
| 12 | return true; |
||
| 13 | } |
||
| 14 | return false; |
||
| 15 | } |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Get details about a specific video by GUID: |
||
| 19 | * |
||
| 20 | * @param $guid string |
||
| 21 | * @return object |
||
| 22 | */ |
||
| 23 | function videopress_get_video_details( $guid ) { |
||
| 24 | if ( ! videopress_is_valid_guid( $guid ) ) { |
||
| 25 | return new WP_Error( 'bad-guid-format', __( 'Invalid Video GUID!', 'jetpack' ) ); |
||
| 26 | } |
||
| 27 | |||
| 28 | $version = '1.1'; |
||
| 29 | $endpoint = sprintf( '/videos/%1$s', $guid ); |
||
| 30 | $response = wp_remote_get( sprintf( 'https://public-api.wordpress.com/rest/v%1$s%2$s', $version, $endpoint ) ); |
||
| 31 | $data = json_decode( wp_remote_retrieve_body( $response ) ); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Allow functions to modify fetched video details. |
||
| 35 | * |
||
| 36 | * This filter allows third-party code to modify the return data |
||
| 37 | * about a given video. It may involve swapping some data out or |
||
| 38 | * adding new parameters. |
||
| 39 | * |
||
| 40 | * @since 3.10 |
||
| 41 | * |
||
| 42 | * @param object $data The data returned by the WPCOM API. See: https://developer.wordpress.com/docs/api/1.1/get/videos/%24guid/ |
||
| 43 | * @param string $guid The GUID of the VideoPress video in question. |
||
| 44 | */ |
||
| 45 | return apply_filters( 'videopress_get_video_details', $data, $guid ); |
||
| 46 | } |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * Get an attachment ID given a URL. |
||
| 51 | * |
||
| 52 | * Modified from http://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
||
| 53 | * |
||
| 54 | * @todo: Add some caching in here. |
||
|
0 ignored issues
–
show
|
|||
| 55 | * |
||
| 56 | * @param string $url |
||
| 57 | * |
||
| 58 | * @return int|bool Attachment ID on success, false on failure |
||
| 59 | */ |
||
| 60 | function videopress_get_attachment_id_by_url( $url ) { |
||
| 61 | $wp_upload_dir = wp_upload_dir(); |
||
| 62 | // Strip out protocols, so it doesn't fail because searching for http: in https: dir. |
||
| 63 | $dir = set_url_scheme( trailingslashit( $wp_upload_dir['baseurl'] ), 'relative' ); |
||
| 64 | |||
| 65 | // Is URL in uploads directory? |
||
| 66 | if ( false !== strpos( $url, $dir ) ) { |
||
| 67 | |||
| 68 | $file = basename( $url ); |
||
| 69 | |||
| 70 | $query_args = array( |
||
| 71 | 'post_type' => 'attachment', |
||
| 72 | 'post_status' => 'inherit', |
||
| 73 | 'fields' => 'ids', |
||
| 74 | 'meta_query' => array( |
||
| 75 | array( |
||
| 76 | 'key' => '_wp_attachment_metadata', |
||
| 77 | 'compare' => 'LIKE', |
||
| 78 | 'value' => $file, |
||
| 79 | ), |
||
| 80 | ) |
||
| 81 | ); |
||
| 82 | |||
| 83 | $query = new WP_Query( $query_args ); |
||
| 84 | |||
| 85 | if ( $query->have_posts() ) { |
||
| 86 | foreach ( $query->posts as $attachment_id ) { |
||
| 87 | $meta = wp_get_attachment_metadata( $attachment_id ); |
||
| 88 | $original_file = basename( $meta['file'] ); |
||
| 89 | $cropped_files = wp_list_pluck( $meta['sizes'], 'file' ); |
||
| 90 | |||
| 91 | if ( $original_file === $file || in_array( $file, $cropped_files ) ) { |
||
| 92 | return (int) $attachment_id; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | } |
||
| 98 | return false; |
||
| 99 | } |
||
| 100 |
This check looks
TODOcomments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.