PluginProbe
Embed Privacy / 1.11.0
Embed Privacy v1.11.0
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-vimeo.php

class-vimeo.php in Embed Privacy 1.11.0, at inc/thumbnail/provider/class-vimeo.php

111 lines 2.3 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 * Vimeo thumbnail implementation.
8 *
9 * @author Epiphyt
10 * @license GPL2
11 * @package epiphyt\Embed_Privacy
12 */
13 final class Vimeo 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 'vimeo.com',
19 ];
20
21 /**
22 * @var string Thumbnail provider name
23 */
24 public static $name = 'vimeo';
25
26 /**
27 * {@inheritDoc}
28 */
29 public static function get( $data, $url ) {
30 if ( ! self::is_provider_embed( $url ) ) {
31 return;
32 }
33
34 // the thumbnail URL has usually something like _295x166 in the end
35 // remove this to get the maximum resolution
36 $thumbnail_url = \substr( $data->thumbnail_url, 0, \strrpos( $data->thumbnail_url, '_' ) );
37 $id = self::get_id( $url );
38
39 if ( $id ) {
40 self::save( $id, $url, $thumbnail_url );
41 }
42 }
43
44 /**
45 * {@inheritDoc}
46 */
47 public static function get_id( $content ) {
48 $id = \str_replace(
49 [
50 'https://vimeo.com/',
51 'https://player.vimeo.com/video/',
52 ],
53 '',
54 $content
55 );
56
57 if ( \str_contains( $id, '?' ) ) {
58 $id = \substr( $id, 0, \strpos( $id, '?' ) );
59 }
60
61 return $id;
62 }
63
64 /**
65 * {@inheritDoc}
66 */
67 public static function get_title() {
68 return \_x( 'Vimeo', 'embed provider', 'embed-privacy' );
69 }
70
71 /**
72 * {@inheritDoc}
73 */
74 public static function save( $id, $url, $thumbnail_url = '' ) {
75 $post = \get_post();
76
77 if ( ! $post ) {
78 return;
79 }
80
81 $thumbnail_path = Thumbnail::get_directory()['base_dir'] . '/' . self::$name . '-' . $id . '.jpg';
82
83 if ( ! \file_exists( $thumbnail_path ) ) {
84 require_once \ABSPATH . 'wp-admin/includes/file.php';
85
86 $file = \download_url( $thumbnail_url );
87
88 if ( \is_wp_error( $file ) ) {
89 return;
90 }
91
92 /** @var \WP_Filesystem_Direct $wp_filesystem */
93 global $wp_filesystem;
94
95 // initialize the WP filesystem if not exists
96 if ( empty( $wp_filesystem ) ) {
97 \WP_Filesystem();
98 }
99
100 $wp_filesystem->move( $file, $thumbnail_path );
101 }
102
103 \update_post_meta(
104 $post->ID,
105 Thumbnail::METADATA_PREFIX . '_' . self::$name . '_' . $id,
106 self::$name . '-' . $id . '.jpg'
107 );
108 \update_post_meta( $post->ID, Thumbnail::METADATA_PREFIX . '_' . self::$name . '_' . $id . '_url', $url );
109 }
110 }
111