AMP
6 years ago
Elementor
5 years ago
Ends
5 years ago
Includes
5 years ago
Plugins
6 years ago
Providers
5 years ago
ThirdParty
6 years ago
AutoLoader.php
6 years ago
Compatibility.php
6 years ago
Core.php
5 years ago
CoreLegacy.php
6 years ago
DisablerLegacy.php
6 years ago
Loader.php
6 years ago
RestAPI.php
6 years ago
Shortcode.php
5 years ago
index.html
7 years ago
Shortcode.php
681 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) 2020 WPDeveloper. All rights reserved. |
| 16 | * @license GPLv3 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 | global $wp_version; |
| 98 | if ( version_compare( $wp_version, '5.3.0', '>=' ) ) { |
| 99 | require_once ABSPATH . 'wp-includes/class-wp-oembed.php'; |
| 100 | } else { |
| 101 | require_once ABSPATH . 'wp-includes/class-oembed.php'; |
| 102 | } |
| 103 | self::$oEmbedInstance = _wp_oembed_get_object(); |
| 104 | }; |
| 105 | if ( !empty( self::get_access_token() ) ) { |
| 106 | self::$oEmbedInstance->providers = array_merge( self::$oEmbedInstance->providers, |
| 107 | self::get_modified_provider( self::get_access_token() ) ); |
| 108 | } |
| 109 | $emberaInstanceSettings = [ |
| 110 | 'params' => [], |
| 111 | ]; |
| 112 | |
| 113 | $content_uid = md5( $content ); |
| 114 | |
| 115 | $attributes = self::parseContentAttributes( $customAttributes, $content_uid ); |
| 116 | if ( isset( $attributes[ 'width' ] ) || isset( $attributes[ 'height' ] ) ) { |
| 117 | if ( isset( $attributes[ 'width' ] ) ) { |
| 118 | $emberaInstanceSettings[ 'params' ][ 'width' ] = $attributes[ 'width' ]; |
| 119 | unset( $attributes[ 'width' ] ); |
| 120 | } |
| 121 | |
| 122 | if ( isset( $attributes[ 'height' ] ) ) { |
| 123 | $emberaInstanceSettings[ 'params' ][ 'height' ] = $attributes[ 'height' ]; |
| 124 | unset( $attributes[ 'height' ] ); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Identify what service provider the shortcode's link belongs to |
| 129 | $serviceProvider = self::$oEmbedInstance->get_provider( $content ); |
| 130 | |
| 131 | |
| 132 | // Check if OEmbed was unable to detect the url service provider. |
| 133 | if ( empty( $serviceProvider ) ) { |
| 134 | // Attempt to do the same using Embera. |
| 135 | $emberaInstance = new Embera( $emberaInstanceSettings ); |
| 136 | // Add support to the user's custom service providers |
| 137 | $additionalServiceProviders = Core::getAdditionalServiceProviders(); |
| 138 | if ( !empty( $additionalServiceProviders ) ) { |
| 139 | foreach ( $additionalServiceProviders as $serviceProviderClassName => $serviceProviderUrls ) { |
| 140 | self::addServiceProvider( $serviceProviderClassName, $serviceProviderUrls, $emberaInstance ); |
| 141 | } |
| 142 | |
| 143 | unset( $serviceProviderUrls, $serviceProviderClassName ); |
| 144 | } |
| 145 | |
| 146 | // Attempt to fetch more info about the url-embed. |
| 147 | $urlData = $emberaInstance->getUrlInfo( $content ); |
| 148 | } else { |
| 149 | // Attempt to fetch more info about the url-embed. |
| 150 | $urlData = self::$oEmbedInstance->fetch( $serviceProvider, $content, $attributes ); |
| 151 | } |
| 152 | |
| 153 | // Sanitize the data |
| 154 | $urlData = self::sanitizeUrlData( $urlData ); |
| 155 | // Stores the original content |
| 156 | if ( is_object( $urlData ) ) { |
| 157 | $urlData->originalContent = $content; |
| 158 | } |
| 159 | |
| 160 | $eventResults = apply_filters( 'embedpress:onBeforeEmbed', $urlData ); |
| 161 | if ( empty( $eventResults ) ) { |
| 162 | // EmbedPress seems unable to embed the url. |
| 163 | return $subject; |
| 164 | } |
| 165 | |
| 166 | // Transform all shortcode attributes into html form. I.e.: {foo: "joe"} -> foo="joe" |
| 167 | $attributesHtml = []; |
| 168 | foreach ( $attributes as $attrName => $attrValue ) { |
| 169 | $attributesHtml[] = $attrName . '="' . $attrValue . '"'; |
| 170 | } |
| 171 | |
| 172 | // Define the EmbedPress html template where the generated embed will be injected in |
| 173 | $embedTemplate = '<div ' . implode( ' ', $attributesHtml ) . '>{html}</div>'; |
| 174 | |
| 175 | // Check if $content is a google shortened url and tries to extract from it which Google service it refers to. |
| 176 | if ( preg_match( '/http[s]?:\/\/goo\.gl\/(?:([a-z]+)\/)?[a-z0-9]+\/?$/i', $content, $matches ) ) { |
| 177 | // Fetch all headers from the short-url so we can know how to handle its original content depending on the service. |
| 178 | $headers = get_headers( $content ); |
| 179 | |
| 180 | $supportedServicesHeadersPatterns = [ |
| 181 | 'maps' => '/^Location:\s+(http[s]?:\/\/.+)$/i', |
| 182 | ]; |
| 183 | |
| 184 | $service = isset( $matches[ 1 ] ) ? strtolower( $matches[ 1 ] ) : null; |
| 185 | // No specific service was found in the url. |
| 186 | if ( empty( $service ) ) { |
| 187 | // Let's try to guess which service the original url belongs to. |
| 188 | foreach ( $headers as $header ) { |
| 189 | // Check if the short-url reffers to a Google Maps url. |
| 190 | if ( preg_match( $supportedServicesHeadersPatterns[ 'maps' ], $header, $matches ) ) { |
| 191 | // Replace the shortened url with its original url. |
| 192 | $content = $matches[ 1 ]; |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | unset( $header ); |
| 197 | } else { |
| 198 | // Check if the Google service is supported atm. |
| 199 | if ( isset( $supportedServicesHeadersPatterns[ $service ] ) ) { |
| 200 | // Tries to extract the url based on its headers. |
| 201 | $originalUrl = self::extractContentFromHeaderAsArray( $supportedServicesHeadersPatterns[ $service ], |
| 202 | $headers ); |
| 203 | // Replace the shortened url with its original url if the specific header was found. |
| 204 | if ( !empty( $originalUrl ) ) { |
| 205 | $content = $originalUrl; |
| 206 | } |
| 207 | unset( $originalUrl ); |
| 208 | } |
| 209 | } |
| 210 | unset( $service, $supportedServicesHeadersPatterns, $headers, $matches ); |
| 211 | |
| 212 | } |
| 213 | // Facebook is a special case. WordPress will try to embed them using OEmbed, but they always end up embedding the profile page, regardless |
| 214 | // 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. |
| 215 | // if ( isset( $urlData->provider_name ) && in_array( $urlData->provider_name, ['Facebook'] ) ) { |
| 216 | // // Check if this is a Facebook profile url. |
| 217 | // if ( preg_match( '/facebook\.com\/(?:[^\/]+?)\/?$/', $content, $match ) ) { |
| 218 | // // Try to embed the url using WP's OSEmbed. |
| 219 | // $parsedContent = self::$oEmbedInstance->get_html( $content, $attributes ); |
| 220 | // } else { |
| 221 | // // Try to embed the url using EmbedPress' Embera. |
| 222 | // $parsedContent = false; |
| 223 | // } |
| 224 | // } else { |
| 225 | // // Try to embed the url using WP's OSEmbed. |
| 226 | // |
| 227 | // } |
| 228 | |
| 229 | $parsedContent = self::$oEmbedInstance->get_html( $content, $attributes ); |
| 230 | |
| 231 | |
| 232 | |
| 233 | if ( !$parsedContent ) { |
| 234 | if ( !isset( $emberaInstance ) ) { |
| 235 | |
| 236 | // If the embed couldn't be generated, we'll try to use Embera's API |
| 237 | $emberaInstance = new Embera( $emberaInstanceSettings ); |
| 238 | // Add support to the user's custom service providers |
| 239 | $additionalServiceProviders = Core::getAdditionalServiceProviders(); |
| 240 | if ( !empty( $additionalServiceProviders ) ) { |
| 241 | foreach ( $additionalServiceProviders as $serviceProviderClassName => $serviceProviderUrls ) { |
| 242 | self::addServiceProvider( $serviceProviderClassName, $serviceProviderUrls, |
| 243 | $emberaInstance ); |
| 244 | } |
| 245 | |
| 246 | unset( $serviceProviderUrls, $serviceProviderClassName ); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // Register the html template |
| 251 | $emberaFormaterInstance = new Formatter( $emberaInstance, true ); |
| 252 | $emberaFormaterInstance->setTemplate( $embedTemplate ); |
| 253 | |
| 254 | // Try to generate the embed using Embera API |
| 255 | $parsedContent = $emberaFormaterInstance->transform( $content ); |
| 256 | |
| 257 | unset( $emberaFormaterInstance, $additionalServiceProviders, $emberaInstance ); |
| 258 | } else { |
| 259 | // Inject the generated code inside the html template |
| 260 | $parsedContent = str_replace( '{html}', $parsedContent, $embedTemplate ); |
| 261 | |
| 262 | // Replace all single quotes to double quotes. I.e: foo='joe' -> foo="joe" |
| 263 | $parsedContent = str_replace( "'", '"', $parsedContent ); |
| 264 | |
| 265 | // Replace the flag `{provider_alias}` which is used by Embera with the "ose-<serviceProviderAlias>". I.e: YouTube -> "ose-youtube" |
| 266 | $parsedContent = preg_replace( '/((?:ose-)?\{provider_alias\})/i', |
| 267 | "ose-" . strtolower( $urlData->provider_name ), $parsedContent ); |
| 268 | } |
| 269 | |
| 270 | if ( isset( $urlData->provider_name ) || ( is_array( $urlData ) && isset( $urlData[ $content ][ 'provider_name' ] ) ) ) { |
| 271 | // NFB seems to always return their embed code with all HTML entities into their applicable characters string. |
| 272 | 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" ) ) { |
| 273 | $parsedContent = html_entity_decode( $parsedContent ); |
| 274 | } elseif ( ( isset( $urlData->provider_name ) && strtoupper( $urlData->provider_name ) === "FACEBOOK" ) || ( is_array( $urlData ) && isset( $urlData[ $content ][ 'provider_name' ] ) && strtoupper( $urlData[ $content ][ 'provider_name' ] ) === "FACEBOOK" ) ) { |
| 275 | $plgSettings = Core::getSettings(); |
| 276 | |
| 277 | // Check if the user wants to force a certain language into Facebook embeds. |
| 278 | $locale = isset( $plgSettings->fbLanguage ) && !empty( $plgSettings->fbLanguage ) ? $plgSettings->fbLanguage : false; |
| 279 | if ( !!$locale ) { |
| 280 | // Replace the automatically detected language by Facebook's API with the language chosen by the user. |
| 281 | $parsedContent = preg_replace( '/\/[a-z]{2}\_[a-z]{2}\/sdk\.js/i', "/{$locale}/sdk.js", |
| 282 | $parsedContent ); |
| 283 | } |
| 284 | |
| 285 | // 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. |
| 286 | if ( is_admin() ) { |
| 287 | $parsedContent = preg_replace( '~data\-adapt\-container\-width=\"(?:true|1)\"~i', |
| 288 | 'data-adapt-container-width="0"', $parsedContent ); |
| 289 | } |
| 290 | |
| 291 | unset( $locale, $plgSettings ); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | unset( $embedTemplate, $serviceProvider ); |
| 296 | |
| 297 | // This assure that the iframe has the same dimensions the user wants to |
| 298 | if ( isset( $emberaInstanceSettings[ 'params' ][ 'width' ] ) || isset( $emberaInstanceSettings[ 'params' ][ 'height' ] ) ) { |
| 299 | if ( isset( $emberaInstanceSettings[ 'params' ][ 'width' ] ) && isset( $emberaInstanceSettings[ 'params' ][ 'height' ] ) ) { |
| 300 | $customWidth = (int)$emberaInstanceSettings[ 'params' ][ 'width' ]; |
| 301 | $customHeight = (int)$emberaInstanceSettings[ 'params' ][ 'height' ]; |
| 302 | } else { |
| 303 | if ( preg_match( '~width="(\d+)"|width\s+:\s+(\d+)~i', $parsedContent, $matches ) ) { |
| 304 | $iframeWidth = (int)$matches[ 1 ]; |
| 305 | } |
| 306 | |
| 307 | if ( preg_match( '~height="(\d+)"|height\s+:\s+(\d+)~i', $parsedContent, $matches ) ) { |
| 308 | $iframeHeight = (int)$matches[ 1 ]; |
| 309 | } |
| 310 | |
| 311 | if ( isset( $iframeWidth ) && isset( $iframeHeight ) && $iframeWidth > 0 && $iframeHeight > 0 ) { |
| 312 | $iframeRatio = ceil( $iframeWidth / $iframeHeight ); |
| 313 | |
| 314 | if ( isset( $emberaInstanceSettings[ 'params' ][ 'width' ] ) ) { |
| 315 | $customWidth = (int)$emberaInstanceSettings[ 'params' ][ 'width' ]; |
| 316 | $customHeight = ceil( $customWidth / $iframeRatio ); |
| 317 | } else { |
| 318 | $customHeight = (int)$emberaInstanceSettings[ 'params' ][ 'height' ]; |
| 319 | $customWidth = $iframeRatio * $customHeight; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if ( isset( $customWidth ) && isset( $customHeight ) ) { |
| 325 | if ( preg_match( '~width="(\d+)"~i', $parsedContent ) ) { |
| 326 | $parsedContent = preg_replace( '~width="(\d+)"~i', 'width="' . $customWidth . '"', |
| 327 | $parsedContent ); |
| 328 | } |
| 329 | |
| 330 | if ( preg_match( '~height="(\d+)"~i', $parsedContent ) ) { |
| 331 | $parsedContent = preg_replace( '~height="(\d+)"~i', 'height="' . $customHeight . '"', |
| 332 | $parsedContent ); |
| 333 | } |
| 334 | |
| 335 | if ( preg_match( '~width\s+:\s+(\d+)~i', $parsedContent ) ) { |
| 336 | $parsedContent = preg_replace( '~width\s+:\s+(\d+)~i', 'width: ' . $customWidth, |
| 337 | $parsedContent ); |
| 338 | } |
| 339 | |
| 340 | if ( preg_match( '~height\s+:\s+(\d+)~i', $parsedContent ) ) { |
| 341 | $parsedContent = preg_replace( '~height\s+:\s+(\d+)~i', 'height: ' . $customHeight, |
| 342 | $parsedContent ); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | if ( $stripNewLine ) { |
| 348 | $parsedContent = preg_replace( '/\n/', '', $parsedContent ); |
| 349 | } |
| 350 | |
| 351 | $parsedContent = apply_filters( 'pp_embed_parsed_content', $parsedContent, $urlData, $attributes ); |
| 352 | |
| 353 | if ( !empty( $parsedContent ) ) { |
| 354 | $embed = (object)array_merge( (array)$urlData, [ |
| 355 | 'attributes' => (object)$attributes, |
| 356 | 'embed' => $parsedContent, |
| 357 | 'url' => $content, |
| 358 | ] ); |
| 359 | $embed = apply_filters( 'embedpress:onAfterEmbed', $embed ); |
| 360 | return $embed; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | return $subject; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Method that adds support to a given new service provider (SP). |
| 369 | * |
| 370 | * @param string $className The new SP class name. |
| 371 | * @param string $reference The new SP reference name. |
| 372 | * @param \Embera\Embera $emberaInstance The embera's instance where the SP will be registered in. |
| 373 | * |
| 374 | * @return boolean |
| 375 | * @since 1.0.0 |
| 376 | * @static |
| 377 | * |
| 378 | */ |
| 379 | public static function addServiceProvider( $className, $reference, &$emberaInstance ) { |
| 380 | if ( empty( $className ) || empty( $reference ) ) { |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | if ( is_string( $reference ) ) { |
| 385 | $emberaInstance->addProvider( $reference, EMBEDPRESS_NAMESPACE . "\\Providers\\{$className}" ); |
| 386 | } elseif ( is_array( $reference ) ) { |
| 387 | foreach ( $reference as $serviceProviderUrl ) { |
| 388 | self::addServiceProvider( $className, $serviceProviderUrl, $emberaInstance ); |
| 389 | } |
| 390 | } else { |
| 391 | return false; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Method that retrieves all custom parameters from a shortcoded string. |
| 397 | * |
| 398 | * @param string $subject The given shortcoded string. |
| 399 | * |
| 400 | * @return array |
| 401 | * @since 1.0.0 |
| 402 | * @static |
| 403 | * |
| 404 | */ |
| 405 | public static function parseContentAttributesFromString( $subject ) { |
| 406 | $customAttributes = []; |
| 407 | if ( preg_match( '/\[embed\s*(.*?)\]/i', stripslashes( $subject ), $m ) ) { |
| 408 | if ( preg_match_all( '/(\!?\w+-?\w*)(?:="(.+?)")?/i', stripslashes( $m[ 1 ] ), $matches ) ) { |
| 409 | $attributes = $matches[ 1 ]; |
| 410 | $attrValues = $matches[ 2 ]; |
| 411 | |
| 412 | foreach ( $attributes as $attrIndex => $attrName ) { |
| 413 | $customAttributes[ $attrName ] = $attrValues[ $attrIndex ]; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | return $customAttributes; |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Method that parses and adds the "data-" prefix to the given custom shortcode attributes. |
| 423 | * |
| 424 | * @param array $customAttributes The array containing the embed attributes. |
| 425 | * @param string $content_uid An optional string specifying a unique ID for the embed |
| 426 | * |
| 427 | * @return array |
| 428 | * @since 1.0.0 |
| 429 | * @access private |
| 430 | * @static |
| 431 | * |
| 432 | */ |
| 433 | private static function parseContentAttributes( array $customAttributes, $content_uid = null ) { |
| 434 | $attributes = [ |
| 435 | 'class' => ["embedpress-wrapper"], |
| 436 | ]; |
| 437 | |
| 438 | $embedShouldBeResponsive = true; |
| 439 | $embedShouldHaveCustomDimensions = false; |
| 440 | if ( !empty( $customAttributes ) ) { |
| 441 | if ( isset( $customAttributes[ 'class' ] ) ) { |
| 442 | if ( !empty( $customAttributes[ 'class' ] ) ) { |
| 443 | $customAttributes[ 'class' ] = explode( ' ', $customAttributes[ 'class' ] ); |
| 444 | |
| 445 | $attributes[ 'class' ] = array_merge( $attributes[ 'class' ], $customAttributes[ 'class' ] ); |
| 446 | } |
| 447 | |
| 448 | unset( $customAttributes[ 'class' ] ); |
| 449 | } |
| 450 | |
| 451 | if ( isset( $customAttributes[ 'width' ] ) ) { |
| 452 | if ( !empty( $customAttributes[ 'width' ] ) ) { |
| 453 | $attributes[ 'width' ] = (int)$customAttributes[ 'width' ]; |
| 454 | $embedShouldHaveCustomDimensions = true; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | if ( isset( $customAttributes[ 'height' ] ) ) { |
| 459 | if ( !empty( $customAttributes[ 'height' ] ) ) { |
| 460 | $attributes[ 'height' ] = (int)$customAttributes[ 'height' ]; |
| 461 | $embedShouldHaveCustomDimensions = true; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | if ( !empty( $customAttributes ) ) { |
| 466 | $attrNameDefaultPrefix = "data-"; |
| 467 | foreach ( $customAttributes as $attrName => $attrValue ) { |
| 468 | if ( is_numeric( $attrName ) ) { |
| 469 | $attrName = $attrValue; |
| 470 | $attrValue = ""; |
| 471 | } |
| 472 | |
| 473 | $attrName = str_replace( $attrNameDefaultPrefix, "", $attrName ); |
| 474 | |
| 475 | if ( !strlen( $attrValue ) ) { |
| 476 | if ( $attrName[ 0 ] === "!" ) { |
| 477 | $attrValue = "false"; |
| 478 | $attrName = substr( $attrName, 1 ); |
| 479 | } else { |
| 480 | $attrValue = "true"; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | $attributes[ $attrNameDefaultPrefix . $attrName ] = $attrValue; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // Check if there's any "responsive" parameter |
| 489 | $responsiveAttributes = ["responsive", "data-responsive"]; |
| 490 | foreach ( $responsiveAttributes as $responsiveAttr ) { |
| 491 | if ( isset( $attributes[ $responsiveAttr ] ) ) { |
| 492 | if ( !strlen( $attributes[ $responsiveAttr ] ) ) { // If the parameter is passed but have no value, it will be true by default |
| 493 | $embedShouldBeResponsive = true; |
| 494 | } else { |
| 495 | $embedShouldBeResponsive = !self::valueIsFalse( $attributes[ $responsiveAttr ] ); |
| 496 | } |
| 497 | |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | unset( $responsiveAttr, $responsiveAttributes ); |
| 502 | } |
| 503 | |
| 504 | $attributes[ 'class' ][] = 'ose-{provider_alias}'; |
| 505 | |
| 506 | if ( !empty( $content_uid ) ) { |
| 507 | $attributes[ 'class' ][] = 'ose-uid-' . $content_uid; |
| 508 | } |
| 509 | |
| 510 | if ( $embedShouldBeResponsive && !$embedShouldHaveCustomDimensions ) { |
| 511 | $attributes[ 'class' ][] = 'responsive'; |
| 512 | } else { |
| 513 | $attributes[ 'data-responsive' ] = "false"; |
| 514 | } |
| 515 | |
| 516 | $attributes[ 'class' ] = implode( ' ', array_unique( array_filter( $attributes[ 'class' ] ) ) ); |
| 517 | if ( isset( $attributes[ 'width' ] ) ) { |
| 518 | $attributes[ 'style' ] = "width:{$attributes['width'] }px;height:{$attributes['height'] }px;"; |
| 519 | } |
| 520 | |
| 521 | return $attributes; |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Method that checks if a given value is/can be identified as (bool)false. |
| 526 | * |
| 527 | * @param mixed $subject The value to be checked. |
| 528 | * |
| 529 | * @return boolean |
| 530 | * @since 1.0.0 |
| 531 | * @static |
| 532 | * |
| 533 | */ |
| 534 | public static function valueIsFalse( $subject ) { |
| 535 | $subject = strtolower( trim( (string)$subject ) ); |
| 536 | switch ( $subject ) { |
| 537 | case "0": |
| 538 | case "false": |
| 539 | case "off": |
| 540 | case "no": |
| 541 | case "n": |
| 542 | case "nil": |
| 543 | case "null": |
| 544 | return true; |
| 545 | default: |
| 546 | return false; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Return the value from a header which is in an array resulted from a get_headers() call. |
| 552 | * If the header cannot be found, this method will return null instead. |
| 553 | * |
| 554 | * @param string $headerPattern Regex pattern the header and its value must match. |
| 555 | * @param array $headersList A list of headers resulted from a get_headers() call. |
| 556 | * |
| 557 | * @return mixed |
| 558 | * @since 1.1.0 |
| 559 | * @access private |
| 560 | * @static |
| 561 | * |
| 562 | */ |
| 563 | private static function extractContentFromHeaderAsArray( $headerPattern, $headersList ) { |
| 564 | $headerValue = null; |
| 565 | |
| 566 | foreach ( $headersList as $header ) { |
| 567 | if ( preg_match( $headerPattern, $header, $matches ) ) { |
| 568 | $headerValue = $matches[ 1 ]; |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | return $headerValue; |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * Sanitize the object returned by the embed source. Sometimes we need to convert |
| 578 | * attributes from "dash" separated to "underline" separated to be able to access |
| 579 | * those attributes from the object, without having to convert it to an array. |
| 580 | * |
| 581 | * @param object $data |
| 582 | * |
| 583 | * @return object |
| 584 | * @since 1.6.1 |
| 585 | * @access private |
| 586 | * @static |
| 587 | * |
| 588 | */ |
| 589 | private static function sanitizeUrlData( $data ) { |
| 590 | if ( is_object( $data ) ) { |
| 591 | $attributes = get_object_vars( $data ); |
| 592 | |
| 593 | foreach ( $attributes as $key => $value ) { |
| 594 | if ( substr_count( $key, '-' ) ) { |
| 595 | unset( $data->$key ); |
| 596 | |
| 597 | $key = str_replace( '-', '_', $key ); |
| 598 | $data->$key = $value; |
| 599 | } |
| 600 | } |
| 601 | } elseif ( is_array( $data ) ) { |
| 602 | foreach ( $data as $key => $value ) { |
| 603 | if ( substr_count( $key, '-' ) ) { |
| 604 | unset( $data[ $key ] ); |
| 605 | |
| 606 | $key = str_replace( '-', '_', $key ); |
| 607 | $data[ $key ] = $value; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | return $data; |
| 613 | } |
| 614 | |
| 615 | public static function get_modified_provider( $access_token = null ) { |
| 616 | if ( empty( $access_token ) ) { |
| 617 | return []; |
| 618 | } |
| 619 | return [ |
| 620 | '#https?://www\.facebook\.com/.*/posts/.*#i' => [ |
| 621 | 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token, |
| 622 | true |
| 623 | ], |
| 624 | '#https?://www\.facebook\.com/.*/activity/.*#i' => [ |
| 625 | 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token, |
| 626 | true |
| 627 | ], |
| 628 | '#https?://www\.facebook\.com/.*/photos/.*#i' => [ |
| 629 | 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token, |
| 630 | true |
| 631 | ], |
| 632 | '#https?://www\.facebook\.com/photo(s/|\.php).*#i' => [ |
| 633 | 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token, |
| 634 | true |
| 635 | ], |
| 636 | '#https?://www\.facebook\.com/permalink\.php.*#i' => [ |
| 637 | 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token, |
| 638 | true |
| 639 | ], |
| 640 | '#https?://www\.facebook\.com/media/.*#i' => [ |
| 641 | 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token, |
| 642 | true |
| 643 | ], |
| 644 | '#https?://www\.facebook\.com/questions/.*#i' => [ |
| 645 | 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token, |
| 646 | true |
| 647 | ], |
| 648 | '#https?://www\.facebook\.com/notes/.*#i' => [ |
| 649 | 'https://graph.facebook.com/v8.0/oembed_post?access_token=' . $access_token, |
| 650 | true |
| 651 | ], |
| 652 | '#https?://www\.facebook\.com/.*/videos/.*#i' => [ |
| 653 | 'https://graph.facebook.com/v8.0/oembed_video?access_token=' . $access_token, |
| 654 | true |
| 655 | ], |
| 656 | '#https?://www\.facebook\.com/video\.php.*#i' => [ |
| 657 | 'https://graph.facebook.com/v8.0/oembed_video?access_token=' . $access_token, |
| 658 | true |
| 659 | ], |
| 660 | '#https?://www\.facebook\.com/watch/?\?v=\d+#i' => [ |
| 661 | 'https://graph.facebook.com/v8.0/oembed_video?access_token=' . $access_token, |
| 662 | true |
| 663 | ], |
| 664 | |
| 665 | '#https?://(www\.)?instagr(\.am|am\.com)/(p|tv)/.*#i' => [ |
| 666 | 'https://graph.facebook.com/v8.0/instagram_oembed?access_token=' . $access_token, |
| 667 | true |
| 668 | ] |
| 669 | ]; |
| 670 | } |
| 671 | |
| 672 | public static function get_access_token() { |
| 673 | $plgSettings = Core::getSettings(); |
| 674 | if ( !empty( $plgSettings->facebook_app_code ) && !empty( $plgSettings->facebook_app_secret ) ) { |
| 675 | return urlencode( $plgSettings->facebook_app_code . '|' . $plgSettings->facebook_app_secret ); |
| 676 | } else { |
| 677 | return null; |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 |