| 1 |
<?php |
| 2 |
|
| 3 |
namespace EmbedPress; |
| 4 |
|
| 5 |
use Embera\Embera; |
| 6 |
use Embera\ProviderCollection\DefaultProviderCollection; |
| 7 |
use WP_oEmbed; |
| 8 |
|
| 9 |
( defined( 'ABSPATH' ) && defined( 'EMBEDPRESS_IS_LOADED' ) ) or die( "No direct script access allowed." ); |
| 10 |
|
| 11 |
/** |
| 12 |
* Entity responsible to handle the plugin's shortcode events and behaviors. |
| 13 |
* |
| 14 |
* @package EmbedPress |
| 15 |
* @author EmbedPress <help@embedpress.com> |
| 16 |
* @copyright Copyright (C) 2020 WPDeveloper. All rights reserved. |
| 17 |
* @license GPLv3 or later |
| 18 |
* @since 1.0.0 |
| 19 |
*/ |
| 20 |
class Shortcode { |
| 21 |
/** |
| 22 |
* The WP_oEmbed class instance. |
| 23 |
* |
| 24 |
* @since 1.0.0 |
| 25 |
* @access private |
| 26 |
* @static |
| 27 |
* |
| 28 |
* @var WP_oEmbed $oEmbedInstance |
| 29 |
*/ |
| 30 |
private static $oEmbedInstance = null; |
| 31 |
/** |
| 32 |
* The DefaultProviderCollection class instance. |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
* @access private |
| 36 |
* @static |
| 37 |
* |
| 38 |
* @var DefaultProviderCollection $collection |
| 39 |
*/ |
| 40 |
private static $collection = null; |
| 41 |
|
| 42 |
/** |
| 43 |
* Register the plugin's shortcode into WordPress. |
| 44 |
* |
| 45 |
* @return void |
| 46 |
* @since 1.0.0 |
| 47 |
* @static |
| 48 |
* |
| 49 |
*/ |
| 50 |
public static function register() { |
| 51 |
// Register the new shortcode for embeds. |
| 52 |
add_shortcode( EMBEDPRESS_SHORTCODE, ['\\EmbedPress\\Shortcode', 'do_shortcode'] ); |
| 53 |
add_shortcode( 'embed_oembed_html', ['\\EmbedPress\\Shortcode', 'do_shortcode'] ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Method that converts the plugin shortcoded-string into its complex content. |
| 58 |
* |
| 59 |
* @param array $attributes Array of attributes |
| 60 |
* @param string $subject The given string |
| 61 |
* |
| 62 |
* @return string |
| 63 |
* @since 1.0.0 |
| 64 |
* @static |
| 65 |
* |
| 66 |
*/ |
| 67 |
|
| 68 |
public static function do_shortcode( $attributes = [], $subject = null ) { |
| 69 |
$plgSettings = Core::getSettings(); |
| 70 |
$default = []; |
| 71 |
if ( $plgSettings->enableGlobalEmbedResize ) { |
| 72 |
$default = [ |
| 73 |
'width' => $plgSettings->enableEmbedResizeWidth, |
| 74 |
'height' => $plgSettings->enableEmbedResizeHeight |
| 75 |
]; |
| 76 |
} |
| 77 |
$attributes = wp_parse_args( $attributes, $default ); |
| 78 |
$embed = self::parseContent( $subject, true, $attributes ); |
| 79 |
|
| 80 |
return is_object( $embed ) ? $embed->embed : $embed; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Replace a given content with its embeded HTML code. |
| 85 |
* |
| 86 |
* @param string The raw content that will be replaced. |
| 87 |
* @param bool $stripNewLine |
| 88 |
* @param array $customAttributes |
| 89 |
* @return string |
| 90 |
* @since 1.0.0 |
| 91 |
* @static |
| 92 |
*/ |
| 93 |
public static function parseContent( $subject, $stripNewLine = false, $customAttributes = [] ) { |
| 94 |
if ( !empty( $subject ) ) { |
| 95 |
if ( empty( $customAttributes ) ) { |
| 96 |
$customAttributes = self::parseContentAttributesFromString( $subject ); |
| 97 |
} |
| 98 |
|
| 99 |
$content = preg_replace( '/(\[' . EMBEDPRESS_SHORTCODE . '(?:\]|.+?\])|\[\/' . EMBEDPRESS_SHORTCODE . '\])/i', |
| 100 |
"", $subject ); |
| 101 |
|
| 102 |
// 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 |
} |
| 136 |
|
| 137 |
// Identify what service provider the shortcode's link belongs to |
| 138 |
$serviceProvider = self::$oEmbedInstance->get_provider( $content ); |
| 139 |
|
| 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 |
// Sanitize the data |
| 162 |
$urlData = self::sanitizeUrlData( $urlData ); |
| 163 |
// Stores the original content |
| 164 |
if ( is_object( $urlData ) ) { |
| 165 |
$urlData->originalContent = $content; |
| 166 |
} |
| 167 |
|
| 168 |
$eventResults = apply_filters( 'embedpress:onBeforeEmbed', $urlData ); |
| 169 |
if ( empty( $eventResults ) ) { |
| 170 |
// EmbedPress seems unable to embed the url. |
| 171 |
return $subject; |
| 172 |
} |
| 173 |
|
| 174 |
// 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 |
} |
| 221 |
|
| 222 |
|
| 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 |
// 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' ]; |
| 293 |
} else { |
| 294 |
if ( preg_match( '~width="(\d+)"|width\s+:\s+(\d+)~i', $parsedContent, $matches ) ) { |
| 295 |
$iframeWidth = (int)$matches[ 1 ]; |
| 296 |
} |
| 297 |
|
| 298 |
if ( preg_match( '~height="(\d+)"|height\s+:\s+(\d+)~i', $parsedContent, $matches ) ) { |
| 299 |
$iframeHeight = (int)$matches[ 1 ]; |
| 300 |
} |
| 301 |
|
| 302 |
if ( isset( $iframeWidth ) && isset( $iframeHeight ) && $iframeWidth > 0 && $iframeHeight > 0 ) { |
| 303 |
$iframeRatio = ceil( $iframeWidth / $iframeHeight ); |
| 304 |
|
| 305 |
if ( isset( $emberaInstanceSettings[ 'width' ] ) ) { |
| 306 |
$customWidth = (int)$emberaInstanceSettings[ 'width' ]; |
| 307 |
$customHeight = ceil( $customWidth / $iframeRatio ); |
| 308 |
} else { |
| 309 |
$customHeight = (int)$emberaInstanceSettings[ 'height' ]; |
| 310 |
$customWidth = $iframeRatio * $customHeight; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
if ( isset( $customWidth ) && isset( $customHeight ) ) { |
| 316 |
if ( preg_match( '~width="(\d+)"~i', $parsedContent ) ) { |
| 317 |
$parsedContent = preg_replace( '~width="(\d+)"~i', 'width="' . $customWidth . '"', |
| 318 |
$parsedContent ); |
| 319 |
} |
| 320 |
|
| 321 |
if ( preg_match( '~height="(\d+)"~i', $parsedContent ) ) { |
| 322 |
error_log( 'hit parsed content custom height'); |
| 323 |
error_log( print_r( $parsedContent,1)); |
| 324 |
$parsedContent = preg_replace( '~height="(\d+)"~i', 'height="' . $customHeight . '"', |
| 325 |
$parsedContent ); |
| 326 |
} |
| 327 |
|
| 328 |
if ( preg_match( '~width\s+:\s+(\d+)~i', $parsedContent ) ) { |
| 329 |
$parsedContent = preg_replace( '~width\s+:\s+(\d+)~i', 'width: ' . $customWidth, |
| 330 |
$parsedContent ); |
| 331 |
} |
| 332 |
|
| 333 |
if ( preg_match( '~height\s+:\s+(\d+)~i', $parsedContent ) ) { |
| 334 |
$parsedContent = preg_replace( '~height\s+:\s+(\d+)~i', 'height: ' . $customHeight, |
| 335 |
$parsedContent ); |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
if ( $stripNewLine ) { |
| 341 |
$parsedContent = preg_replace( '/\n/', '', $parsedContent ); |
| 342 |
} |
| 343 |
|
| 344 |
$parsedContent = apply_filters( 'pp_embed_parsed_content', $parsedContent, $urlData, $attributes ); |
| 345 |
|
| 346 |
if ( !empty( $parsedContent ) ) { |
| 347 |
$embed = (object)array_merge( (array)$urlData, [ |
| 348 |
'attributes' => (object)$attributes, |
| 349 |
'embed' => $parsedContent, |
| 350 |
'url' => $content, |
| 351 |
] ); |
| 352 |
$embed = apply_filters( 'embedpress:onAfterEmbed', $embed ); |
| 353 |
return $embed; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
return $subject; |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Method that adds support to a given new service provider (SP). |
| 362 |
* |
| 363 |
* @param string $className The new SP class name. |
| 364 |
* @param string $reference The new SP reference name.* |
| 365 |
* @return DefaultProviderCollection|bool |
| 366 |
* @since 1.0.0 |
| 367 |
* @static |
| 368 |
* |
| 369 |
*/ |
| 370 |
public static function addServiceProvider( $className, $reference ) { |
| 371 |
if ( empty( $className ) || empty( $reference ) ) { |
| 372 |
return false; |
| 373 |
} |
| 374 |
|
| 375 |
if (is_null(self::$collection)) { |
| 376 |
self::$collection = new DefaultProviderCollection(); |
| 377 |
} |
| 378 |
if ( is_string( $reference ) ) { |
| 379 |
self::$collection->addProvider( $reference, EMBEDPRESS_NAMESPACE . "\\Providers\\{$className}" ); |
| 380 |
return self::$collection; |
| 381 |
} elseif ( is_array( $reference ) ) { |
| 382 |
foreach ( $reference as $serviceProviderUrl ) { |
| 383 |
self::addServiceProvider( $className, $serviceProviderUrl); |
| 384 |
} |
| 385 |
return self::$collection; |
| 386 |
} else { |
| 387 |
return false; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Method that retrieves all custom parameters from a shortcoded string. |
| 393 |
* |
| 394 |
* @param string $subject The given shortcoded string. |
| 395 |
* |
| 396 |
* @return array |
| 397 |
* @since 1.0.0 |
| 398 |
* @static |
| 399 |
* |
| 400 |
*/ |
| 401 |
public static function parseContentAttributesFromString( $subject ) { |
| 402 |
$customAttributes = []; |
| 403 |
if ( preg_match( '/\[embed\s*(.*?)\]/i', stripslashes( $subject ), $m ) ) { |
| 404 |
if ( preg_match_all( '/(\!?\w+-?\w*)(?:="(.+?)")?/i', stripslashes( $m[ 1 ] ), $matches ) ) { |
| 405 |
$attributes = $matches[ 1 ]; |
| 406 |
$attrValues = $matches[ 2 ]; |
| 407 |
|
| 408 |
foreach ( $attributes as $attrIndex => $attrName ) { |
| 409 |
$customAttributes[ $attrName ] = $attrValues[ $attrIndex ]; |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
return $customAttributes; |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Method that parses and adds the "data-" prefix to the given custom shortcode attributes. |
| 418 |
* |
| 419 |
* @param array $customAttributes The array containing the embed attributes. |
| 420 |
* @param string $content_uid An optional string specifying a unique ID for the embed |
| 421 |
* |
| 422 |
* @return array |
| 423 |
* @since 1.0.0 |
| 424 |
* @access private |
| 425 |
* @static |
| 426 |
* |
| 427 |
*/ |
| 428 |
private static function parseContentAttributes( array $customAttributes, $content_uid = null ) { |
| 429 |
$attributes = [ |
| 430 |
'class' => ["embedpress-wrapper"], |
| 431 |
]; |
| 432 |
|
| 433 |
$embedShouldBeResponsive = true; |
| 434 |
$embedShouldHaveCustomDimensions = false; |
| 435 |
if ( !empty( $customAttributes ) ) { |
| 436 |
if ( isset( $customAttributes[ 'class' ] ) ) { |
| 437 |
if ( !empty( $customAttributes[ 'class' ] ) ) { |
| 438 |
$customAttributes[ 'class' ] = explode( ' ', $customAttributes[ 'class' ] ); |
| 439 |
|
| 440 |
$attributes[ 'class' ] = array_merge( $attributes[ 'class' ], $customAttributes[ 'class' ] ); |
| 441 |
} |
| 442 |
|
| 443 |
unset( $customAttributes[ 'class' ] ); |
| 444 |
} |
| 445 |
|
| 446 |
if ( isset( $customAttributes[ 'width' ] ) ) { |
| 447 |
if ( !empty( $customAttributes[ 'width' ] ) ) { |
| 448 |
$attributes[ 'width' ] = (int)$customAttributes[ 'width' ]; |
| 449 |
$embedShouldHaveCustomDimensions = true; |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
if ( isset( $customAttributes[ 'height' ] ) ) { |
| 454 |
if ( !empty( $customAttributes[ 'height' ] ) ) { |
| 455 |
$attributes[ 'height' ] = (int)$customAttributes[ 'height' ]; |
| 456 |
$embedShouldHaveCustomDimensions = true; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
if ( !empty( $customAttributes ) ) { |
| 461 |
$attrNameDefaultPrefix = "data-"; |
| 462 |
foreach ( $customAttributes as $attrName => $attrValue ) { |
| 463 |
if ( is_numeric( $attrName ) ) { |
| 464 |
$attrName = $attrValue; |
| 465 |
$attrValue = ""; |
| 466 |
} |
| 467 |
|
| 468 |
$attrName = str_replace( $attrNameDefaultPrefix, "", $attrName ); |
| 469 |
|
| 470 |
if ( !strlen( $attrValue ) ) { |
| 471 |
if ( $attrName[ 0 ] === "!" ) { |
| 472 |
$attrValue = "false"; |
| 473 |
$attrName = substr( $attrName, 1 ); |
| 474 |
} else { |
| 475 |
$attrValue = "true"; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
$attributes[ $attrNameDefaultPrefix . $attrName ] = $attrValue; |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
// Check if there's any "responsive" parameter |
| 484 |
$responsiveAttributes = ["responsive", "data-responsive"]; |
| 485 |
foreach ( $responsiveAttributes as $responsiveAttr ) { |
| 486 |
if ( isset( $attributes[ $responsiveAttr ] ) ) { |
| 487 |
if ( !strlen( $attributes[ $responsiveAttr ] ) ) { // If the parameter is passed but have no value, it will be true by default |
| 488 |
$embedShouldBeResponsive = true; |
| 489 |
} else { |
| 490 |
$embedShouldBeResponsive = !self::valueIsFalse( $attributes[ $responsiveAttr ] ); |
| 491 |
} |
| 492 |
|
| 493 |
break; |
| 494 |
} |
| 495 |
} |
| 496 |
unset( $responsiveAttr, $responsiveAttributes ); |
| 497 |
} |
| 498 |
|
| 499 |
$attributes[ 'class' ][] = 'ose-{provider_alias}'; |
| 500 |
|
| 501 |
if ( !empty( $content_uid ) ) { |
| 502 |
$attributes[ 'class' ][] = 'ose-uid-' . $content_uid; |
| 503 |
} |
| 504 |
|
| 505 |
if ( $embedShouldBeResponsive && !$embedShouldHaveCustomDimensions ) { |
| 506 |
$attributes[ 'class' ][] = 'responsive'; |
| 507 |
} else { |
| 508 |
$attributes[ 'data-responsive' ] = "false"; |
| 509 |
} |
| 510 |
|
| 511 |
$attributes[ 'class' ] = implode( ' ', array_unique( array_filter( $attributes[ 'class' ] ) ) ); |
| 512 |
if ( isset( $attributes[ 'width' ] ) ) { |
| 513 |
$attributes[ 'style' ] = "width:{$attributes['width'] }px;height:{$attributes['height'] }px;"; |
| 514 |
} |
| 515 |
|
| 516 |
return $attributes; |
| 517 |
} |
| 518 |
|
| 519 |
/** |
| 520 |
* Method that checks if a given value is/can be identified as (bool)false. |
| 521 |
* |
| 522 |
* @param mixed $subject The value to be checked. |
| 523 |
* |
| 524 |
* @return boolean |
| 525 |
* @since 1.0.0 |
| 526 |
* @static |
| 527 |
* |
| 528 |
*/ |
| 529 |
public static function valueIsFalse( $subject ) { |
| 530 |
$subject = strtolower( trim( (string)$subject ) ); |
| 531 |
switch ( $subject ) { |
| 532 |
case "0": |
| 533 |
case "false": |
| 534 |
case "off": |
| 535 |
case "no": |
| 536 |
case "n": |
| 537 |
case "nil": |
| 538 |
case "null": |
| 539 |
return true; |
| 540 |
default: |
| 541 |
return false; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
/** |
| 546 |
* Return the value from a header which is in an array resulted from a get_headers() call. |
| 547 |
* If the header cannot be found, this method will return null instead. |
| 548 |
* |
| 549 |
* @param string $headerPattern Regex pattern the header and its value must match. |
| 550 |
* @param array $headersList A list of headers resulted from a get_headers() call. |
| 551 |
* |
| 552 |
* @return mixed |
| 553 |
* @since 1.1.0 |
| 554 |
* @access private |
| 555 |
* @static |
| 556 |
* |
| 557 |
*/ |
| 558 |
private static function extractContentFromHeaderAsArray( $headerPattern, $headersList ) { |
| 559 |
$headerValue = null; |
| 560 |
|
| 561 |
foreach ( $headersList as $header ) { |
| 562 |
if ( preg_match( $headerPattern, $header, $matches ) ) { |
| 563 |
$headerValue = $matches[ 1 ]; |
| 564 |
break; |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
return $headerValue; |
| 569 |
} |
| 570 |
|
| 571 |
/** |
| 572 |
* Sanitize the object returned by the embed source. Sometimes we need to convert |
| 573 |
* attributes from "dash" separated to "underline" separated to be able to access |
| 574 |
* those attributes from the object, without having to convert it to an array. |
| 575 |
* |
| 576 |
* @param object $data |
| 577 |
* |
| 578 |
* @return object |
| 579 |
* @since 1.6.1 |
| 580 |
* @access private |
| 581 |
* @static |
| 582 |
* |
| 583 |
*/ |
| 584 |
private static function sanitizeUrlData( $data ) { |
| 585 |
if ( is_object( $data ) ) { |
| 586 |
$attributes = get_object_vars( $data ); |
| 587 |
|
| 588 |
foreach ( $attributes as $key => $value ) { |
| 589 |
if ( substr_count( $key, '-' ) ) { |
| 590 |
unset( $data->$key ); |
| 591 |
|
| 592 |
$key = str_replace( '-', '_', $key ); |
| 593 |
$data->$key = $value; |
| 594 |
} |
| 595 |
} |
| 596 |
} elseif ( is_array( $data ) ) { |
| 597 |
foreach ( $data as $key => $value ) { |
| 598 |
if ( substr_count( $key, '-' ) ) { |
| 599 |
unset( $data[ $key ] ); |
| 600 |
|
| 601 |
$key = str_replace( '-', '_', $key ); |
| 602 |
$data[ $key ] = $value; |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
return $data; |
| 608 |
} |
| 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 |
|
| 676 |
public static function get_collection() |
| 677 |
{ |
| 678 |
return self::$collection; |
| 679 |
} |
| 680 |
} |
| 681 |
|