PluginProbe
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents / 2.7.3
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents v2.7.3
4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 All 189 releases
← All changes | EmbedPress/Shortcode.php +259 -251 2.7.22.7.3 View file →
@@ -27,8 +27,19 @@
27 27 *
28 28 * @var WP_oEmbed $oEmbedInstance
29 29 */
30 30 private static $oEmbedInstance = null;
31 +
32 + /**
33 + * The Embera class instance.
34 + *
35 + * @since 2.7.4
36 + * @access private
37 + * @static
38 + *
39 + * @var Embera $embera_instance
40 + */
41 + private static $embera_instance = null;
31 42 /**
32 43 * The DefaultProviderCollection class instance.
33 44 *
34 45 * @since 1.0.0
@@ -38,9 +49,12 @@
38 49 * @var DefaultProviderCollection $collection
39 50 */
40 51 private static $collection = null;
41 52
42 - /**
53 + private static $emberaInstanceSettings = [];
54 + private static $ombed_attributes;
55 +
56 + /**
43 57 * Register the plugin's shortcode into WordPress.
44 58 *
45 59 * @return void
46 60 * @since 1.0.0
@@ -94,203 +108,61 @@
94 108 if ( !empty( $subject ) ) {
95 109 if ( empty( $customAttributes ) ) {
96 110 $customAttributes = self::parseContentAttributesFromString( $subject );
97 111 }
98 -
99 - $content = preg_replace( '/(\[' . EMBEDPRESS_SHORTCODE . '(?:\]|.+?\])|\[\/' . EMBEDPRESS_SHORTCODE . '\])/i',
112 + self::set_default_size( $customAttributes);
113 + $url = preg_replace( '/(\[' . EMBEDPRESS_SHORTCODE . '(?:\]|.+?\])|\[\/' . EMBEDPRESS_SHORTCODE . '\])/i',
100 114 "", $subject );
101 115
102 116 // Converts any special HTML entities back to characters.
103 - $content = htmlspecialchars_decode( $content );
104 -
105 - // Check if the WP_oEmbed class is loaded
106 - if ( !self::$oEmbedInstance ) {
107 - global $wp_version;
108 - if ( version_compare( $wp_version, '5.3.0', '>=' ) ) {
109 - require_once ABSPATH . 'wp-includes/class-wp-oembed.php';
110 - } else {
111 - require_once ABSPATH . 'wp-includes/class-oembed.php';
112 - }
113 - self::$oEmbedInstance = _wp_oembed_get_object();
114 - };
115 - if ( !empty( self::get_access_token() ) ) {
116 - self::$oEmbedInstance->providers = array_merge( self::$oEmbedInstance->providers,
117 - self::get_modified_provider( self::get_access_token() ) );
118 - }
119 - $emberaInstanceSettings = [
120 - ];
121 -
122 - $content_uid = md5( $content );
123 -
124 - $attributes = self::parseContentAttributes( $customAttributes, $content_uid );
125 - if ( isset( $attributes[ 'width' ] ) || isset( $attributes[ 'height' ] ) ) {
126 - if ( isset( $attributes[ 'width' ] ) ) {
127 - $emberaInstanceSettings[ 'maxwidth' ] = $attributes[ 'width' ];
128 - unset( $attributes[ 'width' ] );
129 - }
130 -
131 - if ( isset( $attributes[ 'height' ] ) ) {
132 - $emberaInstanceSettings[ 'maxheight' ] = $attributes[ 'height' ];
133 - unset( $attributes[ 'height' ] );
134 - }
135 - }
117 + $url = htmlspecialchars_decode( $url );
118 + $content_uid = md5( $url );
119 + self::$ombed_attributes = self::parseContentAttributes( $customAttributes, $content_uid );
120 + self::set_embera_settings(self::$ombed_attributes);
136 121
137 122 // Identify what service provider the shortcode's link belongs to
138 - $serviceProvider = self::$oEmbedInstance->get_provider( $content );
123 + $serviceProvider = self::get_oembed()->get_provider( $url );
124 + $urlData = self::get_url_data( $url, self::$ombed_attributes, $serviceProvider);
139 125
140 - // Check if OEmbed was unable to detect the url service provider.
141 - if ( empty( $serviceProvider ) ) {
142 - // Attempt to do the same using Embera.
143 - // Add support to the user's custom service providers
144 - $additionalServiceProviders = Core::getAdditionalServiceProviders();
145 - if ( !empty( $additionalServiceProviders ) ) {
146 - foreach ( $additionalServiceProviders as $serviceProviderClassName => $serviceProviderUrls ) {
147 - self::addServiceProvider( $serviceProviderClassName, $serviceProviderUrls );
148 - }
149 - unset( $serviceProviderUrls, $serviceProviderClassName );
150 - }
151 -
152 - // Attempt to fetch more info about the url-embed.
153 - $emberaInstance = new Embera( $emberaInstanceSettings, self::$collection );
154 - $urlData = $emberaInstance->getUrlData( $content );
155 - } else {
156 - // Attempt to fetch more info about the url-embed.
157 - $urlData = self::$oEmbedInstance->fetch( $serviceProvider, $content, $attributes );
158 - }
159 -
160 -
161 126 // Sanitize the data
162 127 $urlData = self::sanitizeUrlData( $urlData );
163 128 // Stores the original content
164 129 if ( is_object( $urlData ) ) {
165 - $urlData->originalContent = $content;
130 + $urlData->originalContent = $url;
166 131 }
167 132
168 133 $eventResults = apply_filters( 'embedpress:onBeforeEmbed', $urlData );
169 134 if ( empty( $eventResults ) ) {
170 - // EmbedPress seems unable to embed the url.
171 135 return $subject;
172 136 }
173 137
174 138 // Transform all shortcode attributes into html form. I.e.: {foo: "joe"} -> foo="joe"
175 - $attributesHtml = [];
176 - foreach ( $attributes as $attrName => $attrValue ) {
177 - $attributesHtml[] = $attrName . '="' . $attrValue . '"';
178 - }
179 -
180 - // Define the EmbedPress html template where the generated embed will be injected in
181 - $embedTemplate = '<div ' . implode( ' ', $attributesHtml ) . '>{html}</div>';
182 -
183 - // Check if $content is a google shortened url and tries to extract from it which Google service it refers to.
184 - if ( preg_match( '/http[s]?:\/\/goo\.gl\/(?:([a-z]+)\/)?[a-z0-9]+\/?$/i', $content, $matches ) ) {
185 - // Fetch all headers from the short-url so we can know how to handle its original content depending on the service.
186 - $headers = get_headers( $content );
187 -
188 - $supportedServicesHeadersPatterns = [
189 - 'maps' => '/^Location:\s+(http[s]?:\/\/.+)$/i',
190 - ];
191 -
192 - $service = isset( $matches[ 1 ] ) ? strtolower( $matches[ 1 ] ) : null;
193 - // No specific service was found in the url.
194 - if ( empty( $service ) ) {
195 - // Let's try to guess which service the original url belongs to.
196 - foreach ( $headers as $header ) {
197 - // Check if the short-url reffers to a Google Maps url.
198 - if ( preg_match( $supportedServicesHeadersPatterns[ 'maps' ], $header, $matches ) ) {
199 - // Replace the shortened url with its original url.
200 - $content = $matches[ 1 ];
201 - break;
202 - }
203 - }
204 - unset( $header );
205 - } else {
206 - // Check if the Google service is supported atm.
207 - if ( isset( $supportedServicesHeadersPatterns[ $service ] ) ) {
208 - // Tries to extract the url based on its headers.
209 - $originalUrl = self::extractContentFromHeaderAsArray( $supportedServicesHeadersPatterns[ $service ],
210 - $headers );
211 - // Replace the shortened url with its original url if the specific header was found.
212 - if ( !empty( $originalUrl ) ) {
213 - $content = $originalUrl;
214 - }
215 - unset( $originalUrl );
216 - }
217 - }
218 - unset( $service, $supportedServicesHeadersPatterns, $headers, $matches );
219 -
220 - }
139 + $attributesHtml = ['class="ose-{provider_alias} ose-uid-' . $content_uid.' ose-embedpress-responsive"'];
140 + //$attributesHtml = [];
141 + //foreach ( self::$ombed_attributes as $attrName => $attrValue ) {
142 + // $attributesHtml[] = $attrName . '="' . $attrValue . '"';
143 + //}
144 + if ( isset( $customAttributes['width'])) {
145 + $attributesHtml[] = "style=\"width:{$customAttributes['width']}px; max-width:100%; height: auto\"";
146 + }
221 147
148 + // Check if $url is a google shortened url and tries to extract from it which Google service it refers to.
149 + self::check_for_google_url($url);
150 + $provider_name = self::get_provider_name($urlData, $url);
151 + $embedTemplate = '<div ' . implode( ' ', $attributesHtml ) . '>{html}</div>';
152 + $parsedContent = self::get_content_from_template($url, $embedTemplate);
153 + // Replace all single quotes to double quotes. I.e: foo='joe' -> foo="joe"
154 + $parsedContent = str_replace( "'", '"', $parsedContent );
155 + $parsedContent = str_replace( "{provider_alias}", $provider_name , $parsedContent );
156 + self::purify_html_content( $parsedContent);
157 + self::modify_content_for_fb_and_canada( $provider_name, $parsedContent);
158 + unset( $embedTemplate, $serviceProvider );
222 159
223 - $parsedContent = self::$oEmbedInstance->get_html( $content, $attributes );
224 -
225 - $provider_name = '';
226 - if (isset( $urlData->provider_name )) {
227 - $provider_name = isset( $urlData->provider_name );
228 - }elseif ( is_array( $urlData ) && isset( $urlData[ $content ][ 'provider_name' ] ) ) {
229 - $provider_name = $urlData[ $content ][ 'provider_name' ];
230 - }
231 -
232 -
233 - if ( !$parsedContent ) {
234 - // If the embed couldn't be generated, we'll try to use Embera's API
235 - if ( !isset( $emberaInstance ) ) {
236 - // Add support to the user's custom service providers
237 - $additionalServiceProviders = Core::getAdditionalServiceProviders();
238 - if ( !empty( $additionalServiceProviders ) ) {
239 - foreach ( $additionalServiceProviders as $serviceProviderClassName => $serviceProviderUrls ) {
240 - self::addServiceProvider( $serviceProviderClassName, $serviceProviderUrls);
241 - }
242 -
243 - unset( $serviceProviderUrls, $serviceProviderClassName );
244 - }
245 - $emberaInstance = new Embera( $emberaInstanceSettings );
246 - }
247 -
248 - // Inject the generated code inside the html template
249 - $parsedContent = str_replace( '{html}', $emberaInstance->autoEmbed($content), $embedTemplate );
250 -
251 - } else {
252 - // Inject the generated code inside the html template
253 - $parsedContent = str_replace( '{html}', $parsedContent, $embedTemplate );
254 - // Replace all single quotes to double quotes. I.e: foo='joe' -> foo="joe"
255 - $parsedContent = str_replace( "'", '"', $parsedContent );
256 - $parsedContent = preg_replace( '/((?:ose-)?\{provider_alias\})/i',
257 - "ose-" . strtolower( $provider_name ), $parsedContent );
258 - }
259 -
260 - if ( !empty($provider_name) ) {
261 - // NFB seems to always return their embed code with all HTML entities into their applicable characters string.
262 - $PROVIDER_NAME_IN_CAP = strtoupper($provider_name);
263 - if ( $PROVIDER_NAME_IN_CAP === "NATIONAL FILM BOARD OF CANADA" ) {
264 - $parsedContent = html_entity_decode( $parsedContent );
265 - } elseif ( $PROVIDER_NAME_IN_CAP === "FACEBOOK" ) {
266 - $plgSettings = Core::getSettings();
267 -
268 - // Check if the user wants to force a certain language into Facebook embeds.
269 - $locale = isset( $plgSettings->fbLanguage ) && !empty( $plgSettings->fbLanguage ) ? $plgSettings->fbLanguage : false;
270 - if ( !!$locale ) {
271 - // Replace the automatically detected language by Facebook's API with the language chosen by the user.
272 - $parsedContent = preg_replace( '/\/[a-z]{2}\_[a-z]{2}\/sdk\.js/i', "/{$locale}/sdk.js",
273 - $parsedContent );
274 - }
275 -
276 - // Make sure `adapt_container_width` parameter is set to false. Setting to true, as it is by default, might cause Facebook to render embeds inside editors (in admin) with only 180px wide.
277 - if ( is_admin() ) {
278 - $parsedContent = preg_replace( '~data\-adapt\-container\-width=\"(?:true|1)\"~i',
279 - 'data-adapt-container-width="0"', $parsedContent );
280 - }
281 -
282 - unset( $locale, $plgSettings );
283 - }
284 - }
285 -
286 - unset( $embedTemplate, $serviceProvider );
287 -
288 160 // This assure that the iframe has the same dimensions the user wants to
289 - if ( isset( $emberaInstanceSettings[ 'width' ] ) || isset( $emberaInstanceSettings[ 'height' ] ) ) {
290 - if ( isset( $emberaInstanceSettings[ 'width' ] ) && isset( $emberaInstanceSettings[ 'height' ] ) ) {
291 - $customWidth = (int)$emberaInstanceSettings[ 'width' ];
292 - $customHeight = (int)$emberaInstanceSettings[ 'height' ];
161 + if ( isset( self::$emberaInstanceSettings[ 'maxwidth' ] ) || isset( self::$emberaInstanceSettings[ 'maxheight' ] ) ) {
162 + if ( isset( self::$emberaInstanceSettings[ 'maxwidth' ] ) && isset( self::$emberaInstanceSettings[ 'maxheight' ] ) ) {
163 + $customWidth = (int)self::$emberaInstanceSettings[ 'maxwidth' ];
164 + $customHeight = (int)self::$emberaInstanceSettings[ 'maxheight' ];
293 165 } else {
294 166 if ( preg_match( '~width="(\d+)"|width\s+:\s+(\d+)~i', $parsedContent, $matches ) ) {
295 167 $iframeWidth = (int)$matches[ 1 ];
296 168 }
@@ -301,29 +173,34 @@
301 173
302 174 if ( isset( $iframeWidth ) && isset( $iframeHeight ) && $iframeWidth > 0 && $iframeHeight > 0 ) {
303 175 $iframeRatio = ceil( $iframeWidth / $iframeHeight );
304 176
305 - if ( isset( $emberaInstanceSettings[ 'width' ] ) ) {
306 - $customWidth = (int)$emberaInstanceSettings[ 'width' ];
177 + if ( isset( self::$emberaInstanceSettings[ 'maxwidth' ] ) ) {
178 + $customWidth = (int)self::$emberaInstanceSettings[ 'maxwidth' ];
307 179 $customHeight = ceil( $customWidth / $iframeRatio );
308 180 } else {
309 - $customHeight = (int)$emberaInstanceSettings[ 'height' ];
181 + $customHeight = (int)self::$emberaInstanceSettings[ 'maxheight' ];
310 182 $customWidth = $iframeRatio * $customHeight;
311 183 }
312 184 }
313 185 }
314 -
186 +
315 187 if ( isset( $customWidth ) && isset( $customHeight ) ) {
316 188 if ( preg_match( '~width="(\d+)"~i', $parsedContent ) ) {
317 189 $parsedContent = preg_replace( '~width="(\d+)"~i', 'width="' . $customWidth . '"',
318 190 $parsedContent );
191 + } elseif ( preg_match( '~width="({.+})"~i', $parsedContent ) ){
192 + // this block was needed for twitch that has width="{width}" in iframe
193 + $parsedContent = preg_replace( '~width="({.+})"~i', 'width="' . $customWidth . '"',
194 + $parsedContent );
319 195 }
320 196
321 197 if ( preg_match( '~height="(\d+)"~i', $parsedContent ) ) {
322 - error_log( 'hit parsed content custom height');
323 - error_log( print_r( $parsedContent,1));
324 198 $parsedContent = preg_replace( '~height="(\d+)"~i', 'height="' . $customHeight . '"',
325 199 $parsedContent );
200 + } elseif ( preg_match( '~height="({.+})"~i', $parsedContent ) ){
201 + $parsedContent = preg_replace( '~height="({.+})"~i', 'height="' . $customHeight . '"',
202 + $parsedContent );
326 203 }
327 204
328 205 if ( preg_match( '~width\s+:\s+(\d+)~i', $parsedContent ) ) {
329 206 $parsedContent = preg_replace( '~width\s+:\s+(\d+)~i', 'width: ' . $customWidth,
@@ -335,20 +212,37 @@
335 212 $parsedContent );
336 213 }
337 214 }
338 215 }
216 +
217 + if ( 'the-new-york-times' === $provider_name && isset( $customAttributes['height']) && isset( $customAttributes['width']) ) {
218 + $styles = <<<KAMAL
219 +<style>
220 +.ose-the-new-york-times iframe{
221 + min-height: auto;
222 + height: {height}px;
223 + width: {width}px;
224 + max-width:100%
225 + max-height: 100%;
226 +}
227 +</style>
228 +KAMAL;
229 + $styles = str_replace( ['{height}', '{width}'], [$customAttributes['height'], $customAttributes['width']], $styles);
230 + $parsedContent = $styles.$parsedContent;
231 + }
339 232
340 233 if ( $stripNewLine ) {
341 234 $parsedContent = preg_replace( '/\n/', '', $parsedContent );
342 235 }
236 +
343 237
344 - $parsedContent = apply_filters( 'pp_embed_parsed_content', $parsedContent, $urlData, $attributes );
238 + $parsedContent = apply_filters( 'pp_embed_parsed_content', $parsedContent, $urlData, self::get_oembed_attributes() );
345 239
346 240 if ( !empty( $parsedContent ) ) {
347 241 $embed = (object)array_merge( (array)$urlData, [
348 - 'attributes' => (object)$attributes,
242 + 'attributes' => (object) self::get_oembed_attributes(),
349 243 'embed' => $parsedContent,
350 - 'url' => $content,
244 + 'url' => $url,
351 245 ] );
352 246 $embed = apply_filters( 'embedpress:onAfterEmbed', $embed );
353 247 return $embed;
354 248 }
@@ -356,8 +250,57 @@
356 250
357 251 return $subject;
358 252 }
359 253
254 + protected static function get_oembed() {
255 + if ( !self::$oEmbedInstance ) {
256 + global $wp_version;
257 + if ( version_compare( $wp_version, '5.3.0', '>=' ) ) {
258 + require_once ABSPATH . 'wp-includes/class-wp-oembed.php';
259 + } else {
260 + require_once ABSPATH . 'wp-includes/class-oembed.php';
261 + }
262 + self::$oEmbedInstance = _wp_oembed_get_object();
263 + }
264 + return self::$oEmbedInstance;
265 + }
266 +
267 + protected static function set_default_size( &$customAttributes ) {
268 + if (empty( $customAttributes['width'])) {
269 + $customAttributes['width'] = 600;
270 + }
271 + if (empty( $customAttributes['height'])) {
272 + $customAttributes['height'] = 550;
273 + }
274 + }
275 +
276 + protected static function get_url_data( $url, $attributes = [], $serviceProvider = '' ) {
277 + if ( !empty( $serviceProvider ) ) {
278 + $urlData = self::get_oembed()->fetch( $serviceProvider, $url, $attributes );
279 + } else {
280 + $urlData = self::get_embera_instance()->getUrlData( $url );
281 + }
282 + return $urlData;
283 + }
284 +
285 + /**
286 + * @return null|Embera
287 + */
288 + public static function get_embera_instance() {
289 + if ( !self::$embera_instance) {
290 + $additionalServiceProviders = Core::getAdditionalServiceProviders();
291 + if ( !empty( $additionalServiceProviders ) ) {
292 + foreach ( $additionalServiceProviders as $serviceProviderClassName => $serviceProviderUrls ) {
293 + self::addServiceProvider( $serviceProviderClassName, $serviceProviderUrls );
294 + }
295 + unset( $serviceProviderUrls, $serviceProviderClassName );
296 + }
297 + self::$embera_instance = new Embera( self::get_embera_settings(), self::get_collection() );
298 + }
299 +
300 + return self::$embera_instance;
301 + }
302 +
360 303 /**
361 304 * Method that adds support to a given new service provider (SP).
362 305 *
363 306 * @param string $className The new SP class name.
@@ -426,9 +369,9 @@
426 369 *
427 370 */
428 371 private static function parseContentAttributes( array $customAttributes, $content_uid = null ) {
429 372 $attributes = [
430 - 'class' => ["embedpress-wrapper"],
373 + 'class' => ["embedpress-wrapper ose-embedpress-responsive"],
431 374 ];
432 375
433 376 $embedShouldBeResponsive = true;
434 377 $embedShouldHaveCustomDimensions = false;
@@ -514,8 +457,29 @@
514 457 }
515 458
516 459 return $attributes;
517 460 }
461 +
462 + protected static function set_embera_settings(&$attributes) {
463 + if ( isset( $attributes[ 'width' ] ) || isset( $attributes[ 'height' ] ) ) {
464 + if ( isset( $attributes[ 'width' ] ) ) {
465 + self::$emberaInstanceSettings[ 'maxwidth' ] = $attributes[ 'width' ];
466 + self::$emberaInstanceSettings[ 'width' ] = $attributes[ 'width' ];
467 + unset( $attributes[ 'width' ] );
468 + }
469 +
470 + if ( isset( $attributes[ 'height' ] ) ) {
471 + self::$emberaInstanceSettings[ 'maxheight' ] = $attributes[ 'height' ];
472 + self::$emberaInstanceSettings[ 'height' ] = $attributes[ 'height' ];
473 + unset( $attributes[ 'height' ] );
474 + }
475 + }
476 +
477 + }
478 +
479 + protected static function get_embera_settings() {
480 + return self::$emberaInstanceSettings;
481 + }
518 482
519 483 /**
520 484 * Method that checks if a given value is/can be identified as (bool)false.
521 485 *
@@ -605,76 +569,120 @@
605 569 }
606 570
607 571 return $data;
608 572 }
609 -
610 - public static function get_modified_provider( $access_token = null ) {
611 - if ( empty( $access_token ) ) {
612 - return [];
613 - }
614 - return [
615 - '#https?://www\.facebook\.com/.*/posts/.*#i' => [
616 - 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token,
617 - true
618 - ],
619 - '#https?://www\.facebook\.com/.*/activity/.*#i' => [
620 - 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token,
621 - true
622 - ],
623 - '#https?://www\.facebook\.com/.*/photos/.*#i' => [
624 - 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token,
625 - true
626 - ],
627 - '#https?://www\.facebook\.com/photo(s/|\.php).*#i' => [
628 - 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token,
629 - true
630 - ],
631 - '#https?://www\.facebook\.com/permalink\.php.*#i' => [
632 - 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token,
633 - true
634 - ],
635 - '#https?://www\.facebook\.com/media/.*#i' => [
636 - 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token,
637 - true
638 - ],
639 - '#https?://www\.facebook\.com/questions/.*#i' => [
640 - 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token,
641 - true
642 - ],
643 - '#https?://www\.facebook\.com/notes/.*#i' => [
644 - 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token,
645 - true
646 - ],
647 - '#https?://www\.facebook\.com/.*/videos/.*#i' => [
648 - 'https://graph.facebook.com/v8.0/oembed_video?access_token=' . $access_token,
649 - true
650 - ],
651 - '#https?://www\.facebook\.com/video\.php.*#i' => [
652 - 'https://graph.facebook.com/v8.0/oembed_video?access_token=' . $access_token,
653 - true
654 - ],
655 - '#https?://www\.facebook\.com/watch/?\?v=\d+#i' => [
656 - 'https://graph.facebook.com/v8.0/oembed_video?access_token=' . $access_token,
657 - true
658 - ],
659 -
660 - '#https?://(www\.)?instagr(\.am|am\.com)/(p|tv)/.*#i' => [
661 - 'https://graph.facebook.com/v8.0/instagram_oembed?access_token=' . $access_token,
662 - true
663 - ]
664 - ];
665 - }
666 -
667 - public static function get_access_token() {
668 - $plgSettings = Core::getSettings();
669 - if ( !empty( $plgSettings->facebook_app_code ) && !empty( $plgSettings->facebook_app_secret ) ) {
670 - return urlencode( $plgSettings->facebook_app_code . '|' . $plgSettings->facebook_app_secret );
671 - } else {
672 - return null;
673 - }
674 - }
675 573
676 574 public static function get_collection()
677 575 {
678 576 return self::$collection;
679 577 }
578 +
579 + protected static function purify_html_content( &$html ) {
580 + if ( !class_exists( '\simple_html_dom') ) {
581 + include_once EMBEDPRESS_PATH_CORE . 'simple_html_dom.php';
582 + }
583 +
584 + $dom = str_get_html($html);
585 + $ifDom = $dom->find( 'iframe', 0);
586 + if (!empty( $ifDom) && is_object( $ifDom)){
587 + $ifDom->removeAttribute( 'sandbox');
588 + }
589 +
590 + ob_start();
591 + echo $dom;
592 + return ob_get_clean();
593 + }
594 +
595 + protected static function get_content_from_template( $url, $template ) {
596 + $html = self::get_oembed()->get_html( $url, self::get_oembed_attributes() );
597 + if ( !$html ) {
598 + $html = str_replace( '{html}', self::get_embera_instance()->autoEmbed($url), $template );
599 + }
600 + return $html = str_replace( '{html}', $html, $template );
601 + }
602 +
603 + protected static function get_oembed_attributes() {
604 + return self::$ombed_attributes;
605 + }
606 + protected static function set_oembed_attributes( $atts) {
607 + self::$ombed_attributes = $atts;
608 + }
609 +
610 + protected static function check_for_google_url(&$url) {
611 + if ( preg_match( '/http[s]?:\/\/goo\.gl\/(?:([a-z]+)\/)?[a-z0-9]+\/?$/i', $url, $matches ) ) {
612 + // Fetch all headers from the short-url so we can know how to handle its original content depending on the service.
613 + $headers = get_headers( $url );
614 +
615 + $supportedServicesHeadersPatterns = [
616 + 'maps' => '/^Location:\s+(http[s]?:\/\/.+)$/i',
617 + ];
618 +
619 + $service = isset( $matches[ 1 ] ) ? strtolower( $matches[ 1 ] ) : null;
620 + // No specific service was found in the url.
621 + if ( empty( $service ) ) {
622 + // Let's try to guess which service the original url belongs to.
623 + foreach ( $headers as $header ) {
624 + // Check if the short-url reffers to a Google Maps url.
625 + if ( preg_match( $supportedServicesHeadersPatterns[ 'maps' ], $header, $matches ) ) {
626 + // Replace the shortened url with its original url.
627 + $url = $matches[ 1 ];
628 + break;
629 + }
630 + }
631 + unset( $header );
632 + } else {
633 + // Check if the Google service is supported atm.
634 + if ( isset( $supportedServicesHeadersPatterns[ $service ] ) ) {
635 + // Tries to extract the url based on its headers.
636 + $originalUrl = self::extractContentFromHeaderAsArray( $supportedServicesHeadersPatterns[ $service ],
637 + $headers );
638 + // Replace the shortened url with its original url if the specific header was found.
639 + if ( !empty( $originalUrl ) ) {
640 + $url = $originalUrl;
641 + }
642 + unset( $originalUrl );
643 + }
644 + }
645 + unset( $service, $supportedServicesHeadersPatterns, $headers, $matches );
646 +
647 + }
648 +
649 + }
650 +
651 + protected static function get_provider_name( $urlData, $url ) {
652 + $provider_name = '';
653 + if (isset( $urlData->provider_name )) {
654 + $provider_name = str_replace( [' ', ','], '-', strtolower( $urlData->provider_name));
655 + }elseif ( is_array( $urlData ) && isset( $urlData[ $url ][ 'provider_name' ] ) ) {
656 + $provider_name = str_replace( [' ', ','], '-', strtolower( $urlData[ $url ][ 'provider_name' ]));
657 + }
658 + return $provider_name;
659 + }
660 +
661 + protected static function modify_content_for_fb_and_canada( $provider_name, &$html ) {
662 + if ( !empty($provider_name) ) {
663 + // NFB seems to always return their embed code with all HTML entities into their applicable characters string.
664 + $PROVIDER_NAME_IN_CAP = strtoupper($provider_name);
665 + if ( $PROVIDER_NAME_IN_CAP === "NATIONAL FILM BOARD OF CANADA" ) {
666 + $html = html_entity_decode( $html );
667 + } elseif ( $PROVIDER_NAME_IN_CAP === "FACEBOOK" ) {
668 + $plgSettings = Core::getSettings();
669 +
670 + // Check if the user wants to force a certain language into Facebook embeds.
671 + $locale = isset( $plgSettings->fbLanguage ) && !empty( $plgSettings->fbLanguage ) ? $plgSettings->fbLanguage : false;
672 + if ( !!$locale ) {
673 + // Replace the automatically detected language by Facebook's API with the language chosen by the user.
674 + $html = preg_replace( '/\/[a-z]{2}\_[a-z]{2}\/sdk\.js/i', "/{$locale}/sdk.js",
675 + $html );
676 + }
677 +
678 + // Make sure `adapt_container_width` parameter is set to false. Setting to true, as it is by default, might cause Facebook to render embeds inside editors (in admin) with only 180px wide.
679 + if ( is_admin() ) {
680 + $html = preg_replace( '~data\-adapt\-container\-width=\"(?:true|1)\"~i',
681 + 'data-adapt-container-width="0"', $html );
682 + }
683 +
684 + unset( $locale, $plgSettings );
685 + }
686 + }
687 + }
680 688 }