AMP
7 years ago
Elementor
6 years ago
Ends
6 years ago
Includes
6 years ago
Plugins
6 years ago
Providers
6 years ago
ThirdParty
7 years ago
AutoLoader.php
7 years ago
Compatibility.php
7 years ago
Core.php
6 years ago
CoreLegacy.php
6 years ago
DisablerLegacy.php
7 years ago
Loader.php
7 years ago
RestAPI.php
6 years ago
Shortcode.php
6 years ago
index.html
7 years ago
Shortcode.php
602 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress; |
| 4 | |
| 5 | use Embera\Embera; |
| 6 | use Embera\Formatter; |
| 7 | |
| 8 | (defined( 'ABSPATH' ) && defined( 'EMBEDPRESS_IS_LOADED' )) or die( "No direct script access allowed." ); |
| 9 | |
| 10 | /** |
| 11 | * Entity responsible to handle the plugin's shortcode events and behaviors. |
| 12 | * |
| 13 | * @package EmbedPress |
| 14 | * @author EmbedPress <help@embedpress.com> |
| 15 | * @copyright Copyright (C) 2018 EmbedPress. All rights reserved. |
| 16 | * @license GPLv2 or later |
| 17 | * @since 1.0.0 |
| 18 | */ |
| 19 | class Shortcode { |
| 20 | /** |
| 21 | * The WP_oEmbed class instance. |
| 22 | * |
| 23 | * @since 1.0.0 |
| 24 | * @access private |
| 25 | * @static |
| 26 | * |
| 27 | * @var string $oEmbedInstance |
| 28 | */ |
| 29 | private static $oEmbedInstance = null; |
| 30 | |
| 31 | /** |
| 32 | * Register the plugin's shortcode into WordPress. |
| 33 | * |
| 34 | * @return void |
| 35 | * @since 1.0.0 |
| 36 | * @static |
| 37 | * |
| 38 | */ |
| 39 | public static function register() { |
| 40 | // Register the new shortcode for embeds. |
| 41 | add_shortcode( EMBEDPRESS_SHORTCODE, [ '\\EmbedPress\\Shortcode', 'do_shortcode' ] ); |
| 42 | add_shortcode( 'embed_oembed_html', [ '\\EmbedPress\\Shortcode', 'do_shortcode' ] ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Method that converts the plugin shortcoded-string into its complex content. |
| 47 | * |
| 48 | * @param array $attributes Array of attributes |
| 49 | * @param string $subject The given string |
| 50 | * |
| 51 | * @return string |
| 52 | * @since 1.0.0 |
| 53 | * @static |
| 54 | * |
| 55 | */ |
| 56 | |
| 57 | public static function do_shortcode( $attributes = [], $subject = null ) { |
| 58 | $plgSettings = Core::getSettings(); |
| 59 | $default = []; |
| 60 | if ( $plgSettings->enableGlobalEmbedResize ) { |
| 61 | $default = [ |
| 62 | 'width' => $plgSettings->enableEmbedResizeWidth, |
| 63 | 'height' => $plgSettings->enableEmbedResizeHeight |
| 64 | ]; |
| 65 | } |
| 66 | $attributes = wp_parse_args( $attributes, $default ); |
| 67 | $embed = self::parseContent( $subject, true, $attributes ); |
| 68 | |
| 69 | return is_object( $embed ) ? $embed->embed : $embed; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Replace a given content with its embeded HTML code. |
| 74 | * |
| 75 | * @param string The raw content that will be replaced. |
| 76 | * @param boolean Optional. If true, new lines at the end of the embeded code are stripped. |
| 77 | * |
| 78 | * @return string |
| 79 | * @since 1.0.0 |
| 80 | * @static |
| 81 | * |
| 82 | */ |
| 83 | public static function parseContent( $subject, $stripNewLine = false, $customAttributes = [] ) { |
| 84 | if ( !empty( $subject ) ) { |
| 85 | if ( empty( $customAttributes ) ) { |
| 86 | $customAttributes = self::parseContentAttributesFromString( $subject ); |
| 87 | } |
| 88 | |
| 89 | $content = preg_replace( '/(\[' . EMBEDPRESS_SHORTCODE . '(?:\]|.+?\])|\[\/' . EMBEDPRESS_SHORTCODE . '\])/i', |
| 90 | "", $subject ); |
| 91 | |
| 92 | // Converts any special HTML entities back to characters. |
| 93 | $content = htmlspecialchars_decode( $content ); |
| 94 | |
| 95 | // Check if the WP_oEmbed class is loaded |
| 96 | if ( !self::$oEmbedInstance ) { |
| 97 | require_once ABSPATH . 'wp-includes/class-wp-oembed.php'; |
| 98 | |
| 99 | self::$oEmbedInstance = _wp_oembed_get_object(); |
| 100 | } |
| 101 | |
| 102 | $emberaInstanceSettings = [ |
| 103 | 'params' => [], |
| 104 | ]; |
| 105 | |
| 106 | $content_uid = md5( $content ); |
| 107 | |
| 108 | $attributes = self::parseContentAttributes( $customAttributes, $content_uid ); |
| 109 | if ( isset( $attributes['width'] ) || isset( $attributes['height'] ) ) { |
| 110 | if ( isset( $attributes['width'] ) ) { |
| 111 | $emberaInstanceSettings['params']['width'] = $attributes['width']; |
| 112 | unset( $attributes['width'] ); |
| 113 | } |
| 114 | |
| 115 | if ( isset( $attributes['height'] ) ) { |
| 116 | $emberaInstanceSettings['params']['height'] = $attributes['height']; |
| 117 | unset( $attributes['height'] ); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Identify what service provider the shortcode's link belongs to |
| 122 | $serviceProvider = self::$oEmbedInstance->get_provider( $content ); |
| 123 | |
| 124 | |
| 125 | // Check if OEmbed was unable to detect the url service provider. |
| 126 | if ( empty( $serviceProvider ) ) { |
| 127 | // Attempt to do the same using Embera. |
| 128 | $emberaInstance = new Embera( $emberaInstanceSettings ); |
| 129 | // Add support to the user's custom service providers |
| 130 | $additionalServiceProviders = Core::getAdditionalServiceProviders(); |
| 131 | if ( !empty( $additionalServiceProviders ) ) { |
| 132 | foreach ( $additionalServiceProviders as $serviceProviderClassName => $serviceProviderUrls ) { |
| 133 | self::addServiceProvider( $serviceProviderClassName, $serviceProviderUrls, $emberaInstance ); |
| 134 | } |
| 135 | |
| 136 | unset( $serviceProviderUrls, $serviceProviderClassName ); |
| 137 | } |
| 138 | |
| 139 | // Attempt to fetch more info about the url-embed. |
| 140 | $urlData = $emberaInstance->getUrlInfo( $content ); |
| 141 | } else { |
| 142 | // Attempt to fetch more info about the url-embed. |
| 143 | $urlData = self::$oEmbedInstance->fetch( $serviceProvider, $content, $attributes ); |
| 144 | } |
| 145 | |
| 146 | // Sanitize the data |
| 147 | $urlData = self::sanitizeUrlData( $urlData ); |
| 148 | |
| 149 | // Stores the original content |
| 150 | if ( is_object( $urlData ) ) { |
| 151 | $urlData->originalContent = $content; |
| 152 | } |
| 153 | |
| 154 | $eventResults = apply_filters( 'embedpress:onBeforeEmbed', $urlData ); |
| 155 | if ( empty( $eventResults ) ) { |
| 156 | // EmbedPress seems unable to embed the url. |
| 157 | return $subject; |
| 158 | } |
| 159 | |
| 160 | // Transform all shortcode attributes into html form. I.e.: {foo: "joe"} -> foo="joe" |
| 161 | $attributesHtml = []; |
| 162 | foreach ( $attributes as $attrName => $attrValue ) { |
| 163 | $attributesHtml[] = $attrName . '="' . $attrValue . '"'; |
| 164 | } |
| 165 | |
| 166 | // Define the EmbedPress html template where the generated embed will be injected in |
| 167 | $embedTemplate = '<div ' . implode( ' ', $attributesHtml ) . '>{html}</div>'; |
| 168 | |
| 169 | // Check if $content is a google shortened url and tries to extract from it which Google service it refers to. |
| 170 | if ( preg_match( '/http[s]?:\/\/goo\.gl\/(?:([a-z]+)\/)?[a-z0-9]+\/?$/i', $content, $matches ) ) { |
| 171 | // Fetch all headers from the short-url so we can know how to handle its original content depending on the service. |
| 172 | $headers = get_headers( $content ); |
| 173 | |
| 174 | $supportedServicesHeadersPatterns = [ |
| 175 | 'maps' => '/^Location:\s+(http[s]?:\/\/.+)$/i', |
| 176 | ]; |
| 177 | |
| 178 | $service = isset( $matches[1] ) ? strtolower( $matches[1] ) : null; |
| 179 | // No specific service was found in the url. |
| 180 | if ( empty( $service ) ) { |
| 181 | // Let's try to guess which service the original url belongs to. |
| 182 | foreach ( $headers as $header ) { |
| 183 | // Check if the short-url reffers to a Google Maps url. |
| 184 | if ( preg_match( $supportedServicesHeadersPatterns['maps'], $header, $matches ) ) { |
| 185 | // Replace the shortened url with its original url. |
| 186 | $content = $matches[1]; |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | unset( $header ); |
| 191 | } else { |
| 192 | // Check if the Google service is supported atm. |
| 193 | if ( isset( $supportedServicesHeadersPatterns[$service] ) ) { |
| 194 | // Tries to extract the url based on its headers. |
| 195 | $originalUrl = self::extractContentFromHeaderAsArray( $supportedServicesHeadersPatterns[$service], |
| 196 | $headers ); |
| 197 | // Replace the shortened url with its original url if the specific header was found. |
| 198 | if ( !empty( $originalUrl ) ) { |
| 199 | $content = $originalUrl; |
| 200 | } |
| 201 | unset( $originalUrl ); |
| 202 | } |
| 203 | } |
| 204 | unset( $service, $supportedServicesHeadersPatterns, $headers, $matches ); |
| 205 | } |
| 206 | |
| 207 | // Facebook is a special case. WordPress will try to embed them using OEmbed, but they always end up embedding the profile page, regardless |
| 208 | // if the url was pointing to a photo, a post, etc. So, since Embera can embed only facebook-media/posts, we'll use it only for that. |
| 209 | if ( isset( $urlData->provider_name ) && in_array( $urlData->provider_name, [ 'Facebook' ] ) ) { |
| 210 | // Check if this is a Facebook profile url. |
| 211 | if ( preg_match( '/facebook\.com\/(?:[^\/]+?)\/?$/', $content, $match ) ) { |
| 212 | // Try to embed the url using WP's OSEmbed. |
| 213 | $parsedContent = self::$oEmbedInstance->get_html( $content, $attributes ); |
| 214 | } else { |
| 215 | // Try to embed the url using EmbedPress' Embera. |
| 216 | $parsedContent = false; |
| 217 | } |
| 218 | } else { |
| 219 | // Try to embed the url using WP's OSEmbed. |
| 220 | $parsedContent = self::$oEmbedInstance->get_html( $content, $attributes ); |
| 221 | } |
| 222 | |
| 223 | if ( !$parsedContent ) { |
| 224 | if ( !isset( $emberaInstance ) ) { |
| 225 | // If the embed couldn't be generated, we'll try to use Embera's API |
| 226 | $emberaInstance = new Embera( $emberaInstanceSettings ); |
| 227 | // Add support to the user's custom service providers |
| 228 | $additionalServiceProviders = Core::getAdditionalServiceProviders(); |
| 229 | if ( !empty( $additionalServiceProviders ) ) { |
| 230 | foreach ( $additionalServiceProviders as $serviceProviderClassName => $serviceProviderUrls ) { |
| 231 | self::addServiceProvider( $serviceProviderClassName, $serviceProviderUrls, $emberaInstance ); |
| 232 | } |
| 233 | |
| 234 | unset( $serviceProviderUrls, $serviceProviderClassName ); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // Register the html template |
| 239 | $emberaFormaterInstance = new Formatter( $emberaInstance, true ); |
| 240 | $emberaFormaterInstance->setTemplate( $embedTemplate ); |
| 241 | |
| 242 | // Try to generate the embed using Embera API |
| 243 | $parsedContent = $emberaFormaterInstance->transform( $content ); |
| 244 | |
| 245 | unset( $emberaFormaterInstance, $additionalServiceProviders, $emberaInstance ); |
| 246 | } else { |
| 247 | // Inject the generated code inside the html template |
| 248 | $parsedContent = str_replace( '{html}', $parsedContent, $embedTemplate ); |
| 249 | |
| 250 | // Replace all single quotes to double quotes. I.e: foo='joe' -> foo="joe" |
| 251 | $parsedContent = str_replace( "'", '"', $parsedContent ); |
| 252 | |
| 253 | // Replace the flag `{provider_alias}` which is used by Embera with the "ose-<serviceProviderAlias>". I.e: YouTube -> "ose-youtube" |
| 254 | $parsedContent = preg_replace( '/((?:ose-)?\{provider_alias\})/i', |
| 255 | "ose-" . strtolower( $urlData->provider_name ), $parsedContent ); |
| 256 | } |
| 257 | |
| 258 | if ( isset( $urlData->provider_name ) || (is_array( $urlData ) && isset( $urlData[$content]['provider_name'] )) ) { |
| 259 | // NFB seems to always return their embed code with all HTML entities into their applicable characters string. |
| 260 | if ( (isset( $urlData->provider_name ) && strtoupper( $urlData->provider_name ) === "NATIONAL FILM BOARD OF CANADA") || (is_array( $urlData ) && isset( $urlData[$content]['provider_name'] ) && strtoupper( $urlData[$content]['provider_name'] ) === "NATIONAL FILM BOARD OF CANADA") ) { |
| 261 | $parsedContent = html_entity_decode( $parsedContent ); |
| 262 | } elseif ( (isset( $urlData->provider_name ) && strtoupper( $urlData->provider_name ) === "FACEBOOK") || (is_array( $urlData ) && isset( $urlData[$content]['provider_name'] ) && strtoupper( $urlData[$content]['provider_name'] ) === "FACEBOOK") ) { |
| 263 | $plgSettings = Core::getSettings(); |
| 264 | |
| 265 | // Check if the user wants to force a certain language into Facebook embeds. |
| 266 | $locale = isset( $plgSettings->fbLanguage ) && !empty( $plgSettings->fbLanguage ) ? $plgSettings->fbLanguage : false; |
| 267 | if ( !!$locale ) { |
| 268 | // Replace the automatically detected language by Facebook's API with the language chosen by the user. |
| 269 | $parsedContent = preg_replace( '/\/[a-z]{2}\_[a-z]{2}\/sdk\.js/i', "/{$locale}/sdk.js", |
| 270 | $parsedContent ); |
| 271 | } |
| 272 | |
| 273 | // 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. |
| 274 | if ( is_admin() ) { |
| 275 | $parsedContent = preg_replace( '~data\-adapt\-container\-width=\"(?:true|1)\"~i', |
| 276 | 'data-adapt-container-width="0"', $parsedContent ); |
| 277 | } |
| 278 | |
| 279 | unset( $locale, $plgSettings ); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | unset( $embedTemplate, $serviceProvider ); |
| 284 | |
| 285 | // This assure that the iframe has the same dimensions the user wants to |
| 286 | if ( isset( $emberaInstanceSettings['params']['width'] ) || isset( $emberaInstanceSettings['params']['height'] ) ) { |
| 287 | if ( isset( $emberaInstanceSettings['params']['width'] ) && isset( $emberaInstanceSettings['params']['height'] ) ) { |
| 288 | $customWidth = (int)$emberaInstanceSettings['params']['width']; |
| 289 | $customHeight = (int)$emberaInstanceSettings['params']['height']; |
| 290 | } else { |
| 291 | if ( preg_match( '~width="(\d+)"|width\s+:\s+(\d+)~i', $parsedContent, $matches ) ) { |
| 292 | $iframeWidth = (int)$matches[1]; |
| 293 | } |
| 294 | |
| 295 | if ( preg_match( '~height="(\d+)"|height\s+:\s+(\d+)~i', $parsedContent, $matches ) ) { |
| 296 | $iframeHeight = (int)$matches[1]; |
| 297 | } |
| 298 | |
| 299 | if ( isset( $iframeWidth ) && isset( $iframeHeight ) && $iframeWidth > 0 && $iframeHeight > 0 ) { |
| 300 | $iframeRatio = ceil( $iframeWidth / $iframeHeight ); |
| 301 | |
| 302 | if ( isset( $emberaInstanceSettings['params']['width'] ) ) { |
| 303 | $customWidth = (int)$emberaInstanceSettings['params']['width']; |
| 304 | $customHeight = ceil( $customWidth / $iframeRatio ); |
| 305 | } else { |
| 306 | $customHeight = (int)$emberaInstanceSettings['params']['height']; |
| 307 | $customWidth = $iframeRatio * $customHeight; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | if ( isset( $customWidth ) && isset( $customHeight ) ) { |
| 313 | if ( preg_match( '~width="(\d+)"~i', $parsedContent ) ) { |
| 314 | $parsedContent = preg_replace( '~width="(\d+)"~i', 'width="' . $customWidth . '"', |
| 315 | $parsedContent ); |
| 316 | } |
| 317 | |
| 318 | if ( preg_match( '~height="(\d+)"~i', $parsedContent ) ) { |
| 319 | $parsedContent = preg_replace( '~height="(\d+)"~i', 'height="' . $customHeight . '"', |
| 320 | $parsedContent ); |
| 321 | } |
| 322 | |
| 323 | if ( preg_match( '~width\s+:\s+(\d+)~i', $parsedContent ) ) { |
| 324 | $parsedContent = preg_replace( '~width\s+:\s+(\d+)~i', 'width: ' . $customWidth, $parsedContent ); |
| 325 | } |
| 326 | |
| 327 | if ( preg_match( '~height\s+:\s+(\d+)~i', $parsedContent ) ) { |
| 328 | $parsedContent = preg_replace( '~height\s+:\s+(\d+)~i', 'height: ' . $customHeight, |
| 329 | $parsedContent ); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | if ( $stripNewLine ) { |
| 335 | $parsedContent = preg_replace( '/\n/', '', $parsedContent ); |
| 336 | } |
| 337 | |
| 338 | $parsedContent = apply_filters( 'pp_embed_parsed_content', $parsedContent, $urlData, $attributes ); |
| 339 | |
| 340 | if ( !empty( $parsedContent ) ) { |
| 341 | $embed = (object)array_merge( (array)$urlData, [ |
| 342 | 'attributes' => (object)$attributes, |
| 343 | 'embed' => $parsedContent, |
| 344 | 'url' => $content, |
| 345 | ] ); |
| 346 | $embed = apply_filters( 'embedpress:onAfterEmbed', $embed ); |
| 347 | return $embed; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | return $subject; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Method that adds support to a given new service provider (SP). |
| 356 | * |
| 357 | * @param string $className The new SP class name. |
| 358 | * @param string $reference The new SP reference name. |
| 359 | * @param \Embera\Embera $emberaInstance The embera's instance where the SP will be registered in. |
| 360 | * |
| 361 | * @return boolean |
| 362 | * @since 1.0.0 |
| 363 | * @static |
| 364 | * |
| 365 | */ |
| 366 | public static function addServiceProvider( $className, $reference, &$emberaInstance ) { |
| 367 | if ( empty( $className ) || empty( $reference ) ) { |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | if ( is_string( $reference ) ) { |
| 372 | $emberaInstance->addProvider( $reference, EMBEDPRESS_NAMESPACE . "\\Providers\\{$className}" ); |
| 373 | } elseif ( is_array( $reference ) ) { |
| 374 | foreach ( $reference as $serviceProviderUrl ) { |
| 375 | self::addServiceProvider( $className, $serviceProviderUrl, $emberaInstance ); |
| 376 | } |
| 377 | } else { |
| 378 | return false; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Method that retrieves all custom parameters from a shortcoded string. |
| 384 | * |
| 385 | * @param string $subject The given shortcoded string. |
| 386 | * |
| 387 | * @return array |
| 388 | * @since 1.0.0 |
| 389 | * @static |
| 390 | * |
| 391 | */ |
| 392 | public static function parseContentAttributesFromString( $subject ) { |
| 393 | $customAttributes = []; |
| 394 | if ( preg_match( '/\[embed\s*(.*?)\]/i', stripslashes( $subject ), $m ) ) { |
| 395 | if ( preg_match_all( '/(\!?\w+-?\w*)(?:="(.+?)")?/i', stripslashes( $m[1] ), $matches ) ) { |
| 396 | $attributes = $matches[1]; |
| 397 | $attrValues = $matches[2]; |
| 398 | |
| 399 | foreach ( $attributes as $attrIndex => $attrName ) { |
| 400 | $customAttributes[$attrName] = $attrValues[$attrIndex]; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | return $customAttributes; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Method that parses and adds the "data-" prefix to the given custom shortcode attributes. |
| 410 | * |
| 411 | * @param array $customAttributes The array containing the embed attributes. |
| 412 | * @param string $content_uid An optional string specifying a unique ID for the embed |
| 413 | * |
| 414 | * @return array |
| 415 | * @since 1.0.0 |
| 416 | * @access private |
| 417 | * @static |
| 418 | * |
| 419 | */ |
| 420 | private static function parseContentAttributes( array $customAttributes, $content_uid = null ) { |
| 421 | $attributes = [ |
| 422 | 'class' => [ "embedpress-wrapper" ], |
| 423 | ]; |
| 424 | |
| 425 | $embedShouldBeResponsive = true; |
| 426 | $embedShouldHaveCustomDimensions = false; |
| 427 | if ( !empty( $customAttributes ) ) { |
| 428 | if ( isset( $customAttributes['class'] ) ) { |
| 429 | if ( !empty( $customAttributes['class'] ) ) { |
| 430 | $customAttributes['class'] = explode( ' ', $customAttributes['class'] ); |
| 431 | |
| 432 | $attributes['class'] = array_merge( $attributes['class'], $customAttributes['class'] ); |
| 433 | } |
| 434 | |
| 435 | unset( $customAttributes['class'] ); |
| 436 | } |
| 437 | |
| 438 | if ( isset( $customAttributes['width'] ) ) { |
| 439 | if ( !empty( $customAttributes['width'] ) ) { |
| 440 | $attributes['width'] = (int)$customAttributes['width']; |
| 441 | $embedShouldHaveCustomDimensions = true; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | if ( isset( $customAttributes['height'] ) ) { |
| 446 | if ( !empty( $customAttributes['height'] ) ) { |
| 447 | $attributes['height'] = (int)$customAttributes['height']; |
| 448 | $embedShouldHaveCustomDimensions = true; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | if ( !empty( $customAttributes ) ) { |
| 453 | $attrNameDefaultPrefix = "data-"; |
| 454 | foreach ( $customAttributes as $attrName => $attrValue ) { |
| 455 | if ( is_numeric( $attrName ) ) { |
| 456 | $attrName = $attrValue; |
| 457 | $attrValue = ""; |
| 458 | } |
| 459 | |
| 460 | $attrName = str_replace( $attrNameDefaultPrefix, "", $attrName ); |
| 461 | |
| 462 | if ( !strlen( $attrValue ) ) { |
| 463 | if ( $attrName[0] === "!" ) { |
| 464 | $attrValue = "false"; |
| 465 | $attrName = substr( $attrName, 1 ); |
| 466 | } else { |
| 467 | $attrValue = "true"; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | $attributes[$attrNameDefaultPrefix . $attrName] = $attrValue; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | // Check if there's any "responsive" parameter |
| 476 | $responsiveAttributes = [ "responsive", "data-responsive" ]; |
| 477 | foreach ( $responsiveAttributes as $responsiveAttr ) { |
| 478 | if ( isset( $attributes[$responsiveAttr] ) ) { |
| 479 | if ( !strlen( $attributes[$responsiveAttr] ) ) { // If the parameter is passed but have no value, it will be true by default |
| 480 | $embedShouldBeResponsive = true; |
| 481 | } else { |
| 482 | $embedShouldBeResponsive = !self::valueIsFalse( $attributes[$responsiveAttr] ); |
| 483 | } |
| 484 | |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | unset( $responsiveAttr, $responsiveAttributes ); |
| 489 | } |
| 490 | |
| 491 | $attributes['class'][] = 'ose-{provider_alias}'; |
| 492 | |
| 493 | if ( !empty( $content_uid ) ) { |
| 494 | $attributes['class'][] = 'ose-uid-' . $content_uid; |
| 495 | } |
| 496 | |
| 497 | if ( $embedShouldBeResponsive && !$embedShouldHaveCustomDimensions ) { |
| 498 | $attributes['class'][] = 'responsive'; |
| 499 | } else { |
| 500 | $attributes['data-responsive'] = "false"; |
| 501 | } |
| 502 | |
| 503 | $attributes['class'] = implode( ' ', array_unique( array_filter( $attributes['class'] ) ) ); |
| 504 | if ( isset( $attributes['width'] ) ) { |
| 505 | $attributes['style'] = "width:{$attributes['width'] }px;height:{$attributes['height'] }px;"; |
| 506 | } |
| 507 | |
| 508 | return $attributes; |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Method that checks if a given value is/can be identified as (bool)false. |
| 513 | * |
| 514 | * @param mixed $subject The value to be checked. |
| 515 | * |
| 516 | * @return boolean |
| 517 | * @since 1.0.0 |
| 518 | * @static |
| 519 | * |
| 520 | */ |
| 521 | public static function valueIsFalse( $subject ) { |
| 522 | $subject = strtolower( trim( (string)$subject ) ); |
| 523 | switch ( $subject ) { |
| 524 | case "0": |
| 525 | case "false": |
| 526 | case "off": |
| 527 | case "no": |
| 528 | case "n": |
| 529 | case "nil": |
| 530 | case "null": |
| 531 | return true; |
| 532 | default: |
| 533 | return false; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Return the value from a header which is in an array resulted from a get_headers() call. |
| 539 | * If the header cannot be found, this method will return null instead. |
| 540 | * |
| 541 | * @param string $headerPattern Regex pattern the header and its value must match. |
| 542 | * @param array $headersList A list of headers resulted from a get_headers() call. |
| 543 | * |
| 544 | * @return mixed |
| 545 | * @since 1.1.0 |
| 546 | * @access private |
| 547 | * @static |
| 548 | * |
| 549 | */ |
| 550 | private static function extractContentFromHeaderAsArray( $headerPattern, $headersList ) { |
| 551 | $headerValue = null; |
| 552 | |
| 553 | foreach ( $headersList as $header ) { |
| 554 | if ( preg_match( $headerPattern, $header, $matches ) ) { |
| 555 | $headerValue = $matches[1]; |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | return $headerValue; |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Sanitize the object returned by the embed source. Sometimes we need to convert |
| 565 | * attributes from "dash" separated to "underline" separated to be able to access |
| 566 | * those attributes from the object, without having to convert it to an array. |
| 567 | * |
| 568 | * @param object $data |
| 569 | * |
| 570 | * @return object |
| 571 | * @since 1.6.1 |
| 572 | * @access private |
| 573 | * @static |
| 574 | * |
| 575 | */ |
| 576 | private static function sanitizeUrlData( $data ) { |
| 577 | if ( is_object( $data ) ) { |
| 578 | $attributes = get_object_vars( $data ); |
| 579 | |
| 580 | foreach ( $attributes as $key => $value ) { |
| 581 | if ( substr_count( $key, '-' ) ) { |
| 582 | unset( $data->$key ); |
| 583 | |
| 584 | $key = str_replace( '-', '_', $key ); |
| 585 | $data->$key = $value; |
| 586 | } |
| 587 | } |
| 588 | } elseif ( is_array( $data ) ) { |
| 589 | foreach ( $data as $key => $value ) { |
| 590 | if ( substr_count( $key, '-' ) ) { |
| 591 | unset( $data[$key] ); |
| 592 | |
| 593 | $key = str_replace( '-', '_', $key ); |
| 594 | $data[$key] = $value; |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | return $data; |
| 600 | } |
| 601 | } |
| 602 |