| 1 |
<?php |
| 2 |
namespace epiphyt\Embed_Privacy\thumbnail\provider; |
| 3 |
|
| 4 |
use epiphyt\Embed_Privacy\thumbnail\Thumbnail; |
| 5 |
|
| 6 |
/** |
| 7 |
* SlideShare thumbnail implementation. |
| 8 |
* |
| 9 |
* @author Epiphyt |
| 10 |
* @license GPL2 |
| 11 |
* @package epiphyt\Embed_Privacy |
| 12 |
*/ |
| 13 |
final class SlideShare 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 |
'slideshare.net', |
| 19 |
]; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var string Thumbnail provider name |
| 23 |
*/ |
| 24 |
public static $name = 'slideshare'; |
| 25 |
|
| 26 |
/** |
| 27 |
* {@inheritDoc} |
| 28 |
*/ |
| 29 |
public static function get( $data, $url ) { |
| 30 |
if ( ! self::is_provider_embed( $url ) ) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
$id = self::get_id( $data->html ); |
| 35 |
|
| 36 |
if ( $id ) { |
| 37 |
$thumbnail_url = \preg_replace( '/\?.*/', '', $data->thumbnail_url ); |
| 38 |
|
| 39 |
self::save( $id, $url, $thumbnail_url ); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* {@inheritDoc} |
| 45 |
*/ |
| 46 |
public static function get_id( $content ) { |
| 47 |
if ( \str_contains( $content, '/embed_code/key/' ) ) { |
| 48 |
$extracted = \preg_replace( '/.*\/embed_code\/key\//', '', $content ); |
| 49 |
$parts = \explode( '"', $extracted ); |
| 50 |
$id = isset( $parts[0] ) ? $parts[0] : ''; |
| 51 |
} |
| 52 |
else { |
| 53 |
// get ID from meta key by embed URL |
| 54 |
$metadata = Thumbnail::get_metadata( self::$name ); |
| 55 |
|
| 56 |
foreach ( $metadata as $meta ) { |
| 57 |
if ( ! \str_contains( $meta['meta_value'], $content ) ) { |
| 58 |
continue; |
| 59 |
} |
| 60 |
|
| 61 |
$id = \str_replace( |
| 62 |
[ |
| 63 |
Thumbnail::METADATA_PREFIX . '_' . self::$name . '_', |
| 64 |
'_url', |
| 65 |
], |
| 66 |
'', |
| 67 |
$meta['meta_key'] |
| 68 |
); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return $id; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* {@inheritDoc} |
| 77 |
*/ |
| 78 |
public static function get_title() { |
| 79 |
return \_x( 'SlideShare', '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 |
$filename = self::$name . '-' . $id . '.jpg'; |
| 93 |
$thumbnail_path = Thumbnail::get_directory()['base_dir'] . '/' . $filename; |
| 94 |
|
| 95 |
if ( ! \file_exists( $thumbnail_path ) ) { |
| 96 |
require_once \ABSPATH . 'wp-admin/includes/file.php'; |
| 97 |
|
| 98 |
$file = \download_url( $thumbnail_url ); |
| 99 |
|
| 100 |
if ( \is_wp_error( $file ) ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
global $wp_filesystem; |
| 105 |
|
| 106 |
// initialize the WP filesystem if not exists |
| 107 |
if ( empty( $wp_filesystem ) ) { |
| 108 |
\WP_Filesystem(); |
| 109 |
} |
| 110 |
|
| 111 |
$wp_filesystem->move( $file, $thumbnail_path ); |
| 112 |
} |
| 113 |
|
| 114 |
\update_post_meta( $post->ID, Thumbnail::METADATA_PREFIX . '_' . self::$name . '_' . $id, $filename ); |
| 115 |
\update_post_meta( $post->ID, Thumbnail::METADATA_PREFIX . '_' . self::$name . '_' . $id . '_url', $url ); |
| 116 |
} |
| 117 |
} |
| 118 |
|