TemplateLayouts
1 year ago
AirTable.php
1 year ago
Boomplay.php
1 year ago
Calendly.php
2 years ago
Giphy.php
2 years ago
GitHub.php
3 years ago
GoogleDocs.php
2 years ago
GoogleDrive.php
2 years ago
GoogleMaps.php
2 years ago
GooglePhotos.php
1 year ago
Gumroad.php
2 years ago
InstagramFeed.php
1 year ago
LinkedIn.php
2 years ago
NRKRadio.php
2 years ago
OpenSea.php
2 years ago
SelfHosted.php
2 years ago
Spreaker.php
1 year ago
TikTok.php
2 years ago
Twitch.php
2 years ago
Wrapper.php
3 years ago
X.php
2 years ago
Youtube.php
1 year ago
index.html
7 years ago
GooglePhotos.php
272 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress\Providers; |
| 4 | |
| 5 | use EmbedPress\Includes\Classes\Helper; |
| 6 | use Embera\Provider\ProviderAdapter; |
| 7 | use Embera\Provider\ProviderInterface; |
| 8 | use Embera\Url; |
| 9 | |
| 10 | (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed."); |
| 11 | |
| 12 | class GooglePhotos extends ProviderAdapter implements ProviderInterface |
| 13 | { |
| 14 | protected static $hosts = ["photos.app.goo.gl", "photos.google.com"]; |
| 15 | private $player_js = "https://cdn.jsdelivr.net/npm/publicalbum@latest/embed-ui.min.js"; |
| 16 | private $min_expiration = 60; |
| 17 | private $allowed_url_patttern = "/^https:\/\/photos\.app\.goo\.gl\/|^https:\/\/photos\.google\.com\/share\//"; |
| 18 | static public $name = "google-photos-album"; |
| 19 | |
| 20 | /** @var array Array with allowed params for the current Provider */ |
| 21 | protected $allowedParams = [ |
| 22 | 'mode', |
| 23 | 'maxwidth', |
| 24 | 'google_photos_width', |
| 25 | 'google_photos_height', |
| 26 | 'maxheight', |
| 27 | 'imageWidth', |
| 28 | 'imageHeight', |
| 29 | 'playerAutoplay', |
| 30 | 'delay', |
| 31 | 'repeat', |
| 32 | 'mediaitemsAspectRatio', |
| 33 | 'mediaitemsEnlarge', |
| 34 | 'mediaitemsStretch', |
| 35 | 'mediaitemsCover', |
| 36 | 'backgroundColor', |
| 37 | 'expiration', |
| 38 | ]; |
| 39 | |
| 40 | |
| 41 | /** inline {@inheritdoc} */ |
| 42 | protected $httpsSupport = true; |
| 43 | |
| 44 | public function getAllowedParams() |
| 45 | { |
| 46 | return $this->allowedParams; |
| 47 | } |
| 48 | |
| 49 | public function __construct($url, array $config = []) |
| 50 | { |
| 51 | parent::__construct($url, $config); |
| 52 | } |
| 53 | |
| 54 | public function validateUrl(Url $url) |
| 55 | { |
| 56 | return preg_match('~^https:\/\/(photos\.app\.goo\.gl|photos\.google\.com)\/.*$~i', (string) $url); |
| 57 | } |
| 58 | |
| 59 | public function get_embeded_content($link, $width = 0, $height = 480, $imageWidth = 1920, $imageHeight = 1080, $expiration = 60, $mode = 'gallery-player', $playerAutoplay = false, $delay = 5, $repeat = true, $aspectRatio = true, $enlarge = true, $stretch = true, $cover = false, $backgroundColor = '#000000') |
| 60 | { |
| 61 | if (is_object($link)) { |
| 62 | return $this->get_html($link, $expiration); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | |
| 67 | $props = $this->create_default_attr(); |
| 68 | $props->link = $link; |
| 69 | $props->width = $width; |
| 70 | $props->height = $height; |
| 71 | $props->imageWidth = $imageWidth; |
| 72 | $props->imageHeight = $imageHeight; |
| 73 | $props->mode = $mode; |
| 74 | $props->slideshowAutoplay = $playerAutoplay; |
| 75 | $props->slideshowDelay = $delay; |
| 76 | $props->repeat = $repeat; |
| 77 | $props->mediaitemsAspectRatio = $aspectRatio; |
| 78 | $props->mediaitemsEnlarge = $enlarge; |
| 79 | $props->mediaitemsStretch = $stretch; |
| 80 | $props->mediaitemsCover = $cover; |
| 81 | $props->backgroundColor = $backgroundColor; |
| 82 | |
| 83 | return $this->get_html($props, $expiration); |
| 84 | } |
| 85 | |
| 86 | private function get_html($props, $expiration = 0) |
| 87 | { |
| 88 | |
| 89 | |
| 90 | // Generate a hash from the $props object for uniqueness |
| 91 | $props_hash = md5(serialize($props)); |
| 92 | $url_hash = md5($props->link); |
| 93 | $transient = sprintf('%s-%s-%s', self::$name, $url_hash, $props_hash); |
| 94 | |
| 95 | $html = get_transient($transient); |
| 96 | if ($html) { |
| 97 | return $html; |
| 98 | } |
| 99 | |
| 100 | $html = $this->get_embed_google_photos_html($props); |
| 101 | if ($html) { |
| 102 | $expiration = $expiration > 0 ? $expiration : $this->min_expiration; |
| 103 | set_transient($transient, $html, $expiration * 60); |
| 104 | return $html; |
| 105 | } |
| 106 | |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | private function get_remote_contents($url) |
| 112 | { |
| 113 | if (preg_match($this->allowed_url_patttern, $url)) { |
| 114 | $response = wp_remote_get($url); |
| 115 | if (!is_wp_error($response)) { |
| 116 | return wp_remote_retrieve_body($response); |
| 117 | } |
| 118 | } |
| 119 | return null; |
| 120 | } |
| 121 | |
| 122 | private function parse_ogtags($contents) |
| 123 | { |
| 124 | preg_match_all('~<\s*meta\s+property="(og:[^"]+)"\s+content="([^"]*)~i', $contents, $m); |
| 125 | $ogtags = []; |
| 126 | for ($i = 0; $i < count($m[1]); $i++) { |
| 127 | $ogtags[$m[1][$i]] = $m[2][$i]; |
| 128 | } |
| 129 | return $ogtags; |
| 130 | } |
| 131 | |
| 132 | private function parse_photos($contents) |
| 133 | { |
| 134 | preg_match_all('~\"(http[^"]+)\"\,[0-9^,]+\,[0-9^,]+~i', $contents, $m); |
| 135 | return array_unique($m[1]); |
| 136 | } |
| 137 | |
| 138 | private function get_embed_google_photos_html($props) |
| 139 | { |
| 140 | |
| 141 | if ($contents = $this->get_remote_contents($props->link)) { |
| 142 | $og = $this->parse_ogtags($contents); |
| 143 | $title = $og['og:title'] ?? null; |
| 144 | $photos = $this->parse_photos($contents); |
| 145 | |
| 146 | $style = sprintf( |
| 147 | 'display: none; width: %s; height: %s; max-width: 100%%;', |
| 148 | $props->width === 0 ? '100%' : ($props->width . 'px'), |
| 149 | $props->height === 0 ? '100%' : ($props->height . 'px') |
| 150 | ); |
| 151 | |
| 152 | |
| 153 | $items_code = ''; |
| 154 | foreach ($photos as $photo) { |
| 155 | $src = sprintf('%s=w%d-h%d', $photo, $props->imageWidth, $props->imageHeight); |
| 156 | $items_code .= sprintf('<object data="%s"></object>', esc_url($src)); |
| 157 | } |
| 158 | |
| 159 | $attributes = [ |
| 160 | 'data-link' => $props->link, |
| 161 | 'data-found' => count($photos), |
| 162 | 'data-title' => $title, |
| 163 | 'data-mediaitems-aspect-ratio' => $props->mediaitemsAspectRatio, |
| 164 | 'data-mediaitems-enlarge' => $props->mediaitemsEnlarge, |
| 165 | 'data-mediaitems-stretch' => $props->mediaitemsStretch, |
| 166 | 'data-mediaitems-cover' => $props->mediaitemsCover, |
| 167 | 'data-background-color' => $props->backgroundColor, |
| 168 | ]; |
| 169 | |
| 170 | |
| 171 | // Apply filter to allow modification of attributes |
| 172 | $attributes = apply_filters('embedpress_google_photos_attributes', $attributes, $props); |
| 173 | |
| 174 | $attributes_string = ''; |
| 175 | foreach ($attributes as $key => $value) { |
| 176 | if ($value !== null) { |
| 177 | $attributes_string .= sprintf(' %s="%s"', $key, $value); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return sprintf( |
| 182 | "<div class=\"pa-%s-widget\" style=\"%s\"%s>%s</div>\n", |
| 183 | $props->mode, |
| 184 | $style, |
| 185 | $attributes_string, |
| 186 | $items_code |
| 187 | ); |
| 188 | } |
| 189 | return null; |
| 190 | } |
| 191 | |
| 192 | private function create_default_attr() |
| 193 | { |
| 194 | $props = new \stdClass(); |
| 195 | $props->mode = 'gallery-player'; |
| 196 | $props->width = 600; |
| 197 | $props->height = 450; |
| 198 | $props->imageWidth = 1920; |
| 199 | $props->imageHeight = 1080; |
| 200 | $props->slideshowAutoplay = false; |
| 201 | $props->slideshowDelay = 5; |
| 202 | $props->repeat = true; |
| 203 | $props->mediaitemsAspectRatio = true; |
| 204 | $props->mediaitemsEnlarge = true; |
| 205 | $props->mediaitemsStretch = true; |
| 206 | $props->mediaitemsCover = false; |
| 207 | $props->backgroundColor = '#000000'; |
| 208 | return $props; |
| 209 | } |
| 210 | |
| 211 | public function fakeResponse() |
| 212 | { |
| 213 | $src_url = urldecode($this->url); |
| 214 | |
| 215 | // Fetch parameters |
| 216 | $params = $this->getParams(); |
| 217 | |
| 218 | // Extract configuration or set defaults |
| 219 | $width = isset($this->config['maxwidth']) ? $this->config['maxwidth'] : 600; |
| 220 | $height = isset($this->config['maxheight']) ? $this->config['maxheight'] : 450; |
| 221 | |
| 222 | if(isset($params['google_photos_width'])){ |
| 223 | $this->config['maxwidth'] = $params['google_photos_width']; |
| 224 | $width = $params['google_photos_width']; |
| 225 | } |
| 226 | if(isset($params['google_photos_height'])){ |
| 227 | $this->config['maxwidth'] = $params['google_photos_height']; |
| 228 | $height = $params['google_photos_height']; |
| 229 | } |
| 230 | |
| 231 | $expiration = $params['expiration'] ?? 60; |
| 232 | $mode = $params['mode'] ?? 'carousel'; |
| 233 | $playerAutoplay = isset($params['playerAutoplay']) ? Helper::getBooleanParam($params['playerAutoplay']) : false; |
| 234 | $delay = $params['delay'] ?? 5; |
| 235 | $repeat = isset($params['repeat']) ? Helper::getBooleanParam($params['repeat']) : false; |
| 236 | $aspectRatio = isset($params['mediaitemsAspectRatio']) ? Helper::getBooleanParam($params['mediaitemsAspectRatio']) : false; |
| 237 | $enlarge = isset($params['mediaitemsEnlarge']) ? Helper::getBooleanParam($params['mediaitemsEnlarge']) : false; |
| 238 | $stretch = isset($params['mediaitemsStretch']) ? Helper::getBooleanParam($params['mediaitemsStretch']) : false; |
| 239 | $cover = isset($params['mediaitemsCover']) ? Helper::getBooleanParam($params['mediaitemsCover']) : false; |
| 240 | $backgroundColor = $params['backgroundColor'] ?? '#000000'; |
| 241 | |
| 242 | return [ |
| 243 | 'type' => 'rich', |
| 244 | 'provider_name' => 'Google Photos', |
| 245 | 'provider_url' => 'https://photos.app.goo.gl', |
| 246 | 'title' => $params['title'] ?? 'Unknown title', |
| 247 | 'html' => $this->get_embeded_content( |
| 248 | $src_url, |
| 249 | $width, |
| 250 | $height, |
| 251 | $width, |
| 252 | $height, |
| 253 | $expiration, |
| 254 | $mode, |
| 255 | $playerAutoplay, |
| 256 | $delay, |
| 257 | $repeat, |
| 258 | $aspectRatio, |
| 259 | $enlarge, |
| 260 | $stretch, |
| 261 | $cover, |
| 262 | $backgroundColor |
| 263 | ) . '<script src="' . $this->player_js . '"></script>', |
| 264 | ]; |
| 265 | } |
| 266 | |
| 267 | public function modifyResponse(array $response = []) |
| 268 | { |
| 269 | return $this->fakeResponse(); |
| 270 | } |
| 271 | } |
| 272 |