PluginProbe
Embed Privacy / 1.10.1
Embed Privacy v1.10.1
1.14.0 1.13.0 trunk 0.1 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.10.0 1.10.1 1.10.10 1.10.2 1.10.3 1.10.4 1.10.5 1.10.6 1.10.7 1.10.8 1.10.9 1.11.0 1.11.1 1.11.2 All 68 releases
embed-privacy / inc / thumbnail / provider / class-youtube.php

class-youtube.php in Embed Privacy 1.10.1, at inc/thumbnail/provider/class-youtube.php

135 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace epiphyt\Embed_Privacy\thumbnail\provider;
3
4 use epiphyt\Embed_Privacy\thumbnail\Thumbnail;
5
6 /**
7 * YouTube thumbnail implementation.
8 *
9 * @author Epiphyt
10 * @license GPL2
11 * @package epiphyt\Embed_Privacy
12 */
13 final class YouTube extends Thumbnail_Provider implements Thumbnail_Provider_Interface {
14 /**
15 * @var string[] List of valid domains for the thumbnail provider
16 */
17 public static $domains = [
18 'youtu.be',
19 'youtube.com',
20 ];
21
22 /**
23 * @var string Thumbnail provider name
24 */
25 public static $name = 'youtube';
26
27 /**
28 * {@inheritDoc}
29 */
30 public static function get( $data, $url ) {
31 if ( ! self::is_provider_embed( $url ) ) {
32 return;
33 }
34
35 $id = self::get_id_by_thumbnail_url( $data->thumbnail_url );
36
37 if ( $id ) {
38 self::save( $id, $url );
39 }
40 }
41
42 /**
43 * {@inheritDoc}
44 */
45 public static function get_id( $content ) {
46 $id = \str_replace(
47 [
48 'https://www.youtube.com/watch?v=',
49 'https://www.youtube.com/embed/',
50 'https://youtu.be/',
51 ],
52 '',
53 $content
54 );
55 $id = \str_contains( $id, '?' ) ? \substr( $id, 0, \strpos( $id, '?' ) ) : $id;
56
57 return $id;
58 }
59
60 /**
61 * Get the thumbnail ID from a thumbnail URL.
62 *
63 * @param string $url Thumbnail URL to get the thumbnail ID from
64 * @return string Thumbnail ID
65 */
66 public static function get_id_by_thumbnail_url( $url ) {
67 // format: <id>/<thumbnail-name>.jpg
68 $extracted = \str_replace( 'https://i.ytimg.com/vi/', '', $url );
69 // first part is the ID
70 $parts = \explode( '/', $extracted );
71
72 return isset( $parts[0] ) ? $parts[0] : '';
73 }
74
75 /**
76 * {@inheritDoc}
77 */
78 public static function get_title() {
79 return \_x( 'YouTube', 'embed provider', 'embed-privacy' );
80 }
81
82 /**
83 * {@inheritDoc}
84 */
85 public static function save( $id, $url, $thumbnail_url = '' ) {
86 $post = \get_post();
87
88 if ( ! $post ) {
89 return;
90 }
91
92 require_once \ABSPATH . 'wp-admin/includes/file.php';
93
94 $directory = Thumbnail::get_directory();
95 // list of images we try to retrieve
96 // see: https://stackoverflow.com/a/2068371
97 $images = [
98 'maxresdefault',
99 'hqdefault',
100 '0',
101 ];
102 $thumbnail_url = 'https://img.youtube.com/vi/%1$s/%2$s.jpg';
103
104 foreach ( $images as $image ) {
105 $thumbnail_path = $directory['base_dir'] . '/' . self::$name . '-' . $id . '-' . $image . '.jpg';
106
107 if ( ! \file_exists( $thumbnail_path ) ) {
108 $file = \download_url( \sprintf( $thumbnail_url, $id, $image ) );
109
110 if ( \is_wp_error( $file ) ) {
111 continue;
112 }
113
114 /** @var \WP_Filesystem_Direct $wp_filesystem */
115 global $wp_filesystem;
116
117 // initialize the WP filesystem if not exists
118 if ( empty( $wp_filesystem ) ) {
119 \WP_Filesystem();
120 }
121
122 $wp_filesystem->move( $file, $thumbnail_path );
123 }
124
125 \update_post_meta(
126 $post->ID,
127 Thumbnail::METADATA_PREFIX . '_' . self::$name . '_' . $id,
128 self::$name . '-' . $id . '-' . $image . '.jpg'
129 );
130 \update_post_meta( $post->ID, Thumbnail::METADATA_PREFIX . '_' . self::$name . '_' . $id . '_url', $url );
131 break;
132 }
133 }
134 }
135