| @@ -2,11 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace EmbedPress; |
| 4 | 4 | |
| 5 | 5 | use Embera\Embera; |
| 6 | -use Embera\ProviderCollection\DefaultProviderCollection; | |
| 7 | -use EmbedPress\Includes\Classes\Helper; | |
| 8 | -use WP_oEmbed; | |
| 6 | +use Embera\Formatter; | |
| 9 | 7 | |
| 10 | 8 | (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed."); |
| 11 | 9 | |
| 12 | 10 | /** |
| @@ -13,13 +11,12 @@ | ||
| 13 | 11 | * Entity responsible to handle the plugin's shortcode events and behaviors. |
| 14 | 12 | * |
| 15 | 13 | * @package EmbedPress |
| 16 | 14 | * @author EmbedPress <help@embedpress.com> |
| 17 | - * @copyright Copyright (C) 2023 WPDeveloper. All rights reserved. | |
| 18 | - * @license GPLv3 or later | |
| 15 | + * @copyright Copyright (C) 2018 EmbedPress. All rights reserved. | |
| 16 | + * @license GPLv2 or later | |
| 19 | 17 | * @since 1.0.0 |
| 20 | 18 | */ |
| 21 | - | |
| 22 | 19 | class Shortcode |
| 23 | 20 | { |
| 24 | 21 | /** |
| 25 | 22 | * The WP_oEmbed class instance. |
| @@ -27,55 +24,19 @@ | ||
| 27 | 24 | * @since 1.0.0 |
| 28 | 25 | * @access private |
| 29 | 26 | * @static |
| 30 | 27 | * |
| 31 | - * @var WP_oEmbed $oEmbedInstance | |
| 28 | + * @var string $oEmbedInstance | |
| 32 | 29 | */ |
| 33 | - | |
| 34 | - | |
| 35 | - public static $y = 0; | |
| 36 | - | |
| 37 | 30 | private static $oEmbedInstance = null; |
| 38 | 31 | |
| 39 | 32 | /** |
| 40 | - * The Embera class instance. | |
| 33 | + * Register the plugin's shortcode into WordPress. | |
| 41 | 34 | * |
| 42 | - * @since 2.7.4 | |
| 43 | - * @access private | |
| 44 | - * @static | |
| 45 | - * | |
| 46 | - * @var Embera $embera_instance | |
| 47 | - */ | |
| 48 | - private static $embera_instance = null; | |
| 49 | - /** | |
| 50 | - * The DefaultProviderCollection class instance. | |
| 51 | - * | |
| 52 | 35 | * @since 1.0.0 |
| 53 | - * @access private | |
| 54 | 36 | * @static |
| 55 | 37 | * |
| 56 | - * @var DefaultProviderCollection $collection | |
| 57 | - */ | |
| 58 | - private static $collection = null; | |
| 59 | - | |
| 60 | - private static $emberaInstanceSettings = []; | |
| 61 | - private static $ombed_attributes; | |
| 62 | - public static $attributes_data; | |
| 63 | - | |
| 64 | - public static function shortcode_scripts() | |
| 65 | - { | |
| 66 | - // Assets are now handled by AssetManager | |
| 67 | - // This function is kept for backward compatibility but does nothing | |
| 68 | - // AssetManager automatically handles shortcode assets when EmbedPress shortcodes are detected | |
| 69 | - } | |
| 70 | - | |
| 71 | - /** | |
| 72 | - * Register the plugin's shortcode into WordPress. | |
| 73 | - * | |
| 74 | 38 | * @return void |
| 75 | - * @since 1.0.0 | |
| 76 | - * @static | |
| 77 | - * | |
| 78 | 39 | */ |
| 79 | 40 | public static function register() |
| 80 | 41 | { |
| 81 | 42 | // Register the new shortcode for embeds. |
| @@ -80,316 +41,25 @@ | ||
| 80 | 41 | { |
| 81 | 42 | // Register the new shortcode for embeds. |
| 82 | 43 | add_shortcode(EMBEDPRESS_SHORTCODE, ['\\EmbedPress\\Shortcode', 'do_shortcode']); |
| 83 | 44 | add_shortcode('embed_oembed_html', ['\\EmbedPress\\Shortcode', 'do_shortcode']); |
| 84 | - add_shortcode('embedpress', ['\\EmbedPress\\Shortcode', 'do_shortcode']); | |
| 85 | - add_shortcode('embedpress_pdf', ['\\EmbedPress\\Shortcode', 'do_shortcode_pdf']); | |
| 86 | - add_shortcode('embedpress_doc', ['\\EmbedPress\\Shortcode', 'do_shortcode_doc']); | |
| 87 | - add_shortcode('embedpress_pdf_gallery', ['\\EmbedPress\\Shortcode', 'do_shortcode_pdf_gallery']); | |
| 88 | - add_shortcode('embedpress_google_reviews', ['\\EmbedPress\\Shortcode', 'do_shortcode_google_reviews']); | |
| 89 | 45 | } |
| 90 | 46 | |
| 91 | 47 | /** |
| 92 | - * Handler for the [embedpress_google_reviews] shortcode. Maps shortcode | |
| 93 | - * attributes to the shared GoogleReviewsRenderer so the block, Elementor | |
| 94 | - * widget, and shortcode emit identical markup. | |
| 95 | - */ | |
| 96 | - public static function do_shortcode_google_reviews($attributes = [], $subject = null) | |
| 97 | - { | |
| 98 | - $atts = shortcode_atts([ | |
| 99 | - // Free | |
| 100 | - 'place_id' => '', | |
| 101 | - 'place_name' => '', | |
| 102 | - 'limit' => 10, | |
| 103 | - 'min_rating' => 0, | |
| 104 | - 'layout' => 'list', | |
| 105 | - 'show_photo' => 'true', | |
| 106 | - 'show_date' => 'true', | |
| 107 | - 'show_stars' => 'true', | |
| 108 | - 'show_link' => 'false', | |
| 109 | - 'show_images' => 'true', | |
| 110 | - // Structural (free): columns / max width (px) / card gap (px) | |
| 111 | - 'columns' => 3, | |
| 112 | - 'max_width' => 0, | |
| 113 | - 'gap' => 20, | |
| 114 | - // Carousel controls (free). (autoplay already declared below.) | |
| 115 | - 'show_arrows' => 'true', | |
| 116 | - 'show_dots' => 'true', | |
| 117 | - 'carousel_loop' => 'true', | |
| 118 | - 'autoplay_speed' => 5, | |
| 119 | - // Header / summary controls (free) | |
| 120 | - 'show_summary' => 'true', | |
| 121 | - 'show_summary_name' => 'true', | |
| 122 | - 'show_summary_rating' => 'true', | |
| 123 | - 'show_summary_stars' => 'true', | |
| 124 | - 'show_summary_count' => 'true', | |
| 125 | - 'show_write_review' => 'true', | |
| 126 | - 'summary_align' => 'left', | |
| 127 | - // Pro (ignored by free renderer; consumed by Pro's render filters) | |
| 128 | - 'sort' => 'newest', | |
| 129 | - 'keyword' => '', | |
| 130 | - 'hide_empty' => 'false', | |
| 131 | - 'fetch_all' => 'false', | |
| 132 | - 'theme' => 'light', | |
| 133 | - 'accent_color' => '', | |
| 134 | - 'autoplay' => 'false', | |
| 135 | - 'schema' => 'false', | |
| 136 | - 'places' => '', // comma-separated extra Place IDs | |
| 137 | - 'cache_ttl' => 0, | |
| 138 | - 'load_more' => 'false', | |
| 139 | - 'per_page' => 0, // 0 = use the "limit" count as the page size | |
| 140 | - ], is_array($attributes) ? $attributes : [], 'embedpress_google_reviews'); | |
| 141 | - | |
| 142 | - $boolish = function ($v) { | |
| 143 | - if (is_bool($v)) return $v; | |
| 144 | - $v = is_string($v) ? strtolower(trim($v)) : $v; | |
| 145 | - return in_array($v, ['true', '1', 1, 'yes', 'on'], true); | |
| 146 | - }; | |
| 147 | - | |
| 148 | - // `places` is a comma-separated list in shortcode form. | |
| 149 | - $places = []; | |
| 150 | - if (is_string($atts['places']) && trim($atts['places']) !== '') { | |
| 151 | - $places = array_values(array_filter(array_map('trim', explode(',', $atts['places'])))); | |
| 152 | - } | |
| 153 | - | |
| 154 | - \EmbedPress\Includes\Classes\GoogleReviewsRenderer::enqueue_assets(); | |
| 155 | - | |
| 156 | - return \EmbedPress\Includes\Classes\GoogleReviewsRenderer::render([ | |
| 157 | - 'place_id' => sanitize_text_field($atts['place_id']), | |
| 158 | - 'place_name' => sanitize_text_field($atts['place_name']), | |
| 159 | - 'limit' => (int) $atts['limit'], | |
| 160 | - 'min_rating' => (int) $atts['min_rating'], | |
| 161 | - 'layout' => sanitize_key($atts['layout']), | |
| 162 | - 'show_photo' => $boolish($atts['show_photo']), | |
| 163 | - 'show_date' => $boolish($atts['show_date']), | |
| 164 | - 'show_stars' => $boolish($atts['show_stars']), | |
| 165 | - 'show_link' => $boolish($atts['show_link']), | |
| 166 | - 'show_images' => $boolish($atts['show_images']), | |
| 167 | - 'show_arrows' => $boolish($atts['show_arrows']), | |
| 168 | - 'show_dots' => $boolish($atts['show_dots']), | |
| 169 | - 'carousel_loop' => $boolish($atts['carousel_loop']), | |
| 170 | - 'autoplay_speed' => (float) $atts['autoplay_speed'], | |
| 171 | - 'show_summary' => $boolish($atts['show_summary']), | |
| 172 | - 'show_summary_name' => $boolish($atts['show_summary_name']), | |
| 173 | - 'show_summary_rating' => $boolish($atts['show_summary_rating']), | |
| 174 | - 'show_summary_stars' => $boolish($atts['show_summary_stars']), | |
| 175 | - 'show_summary_count' => $boolish($atts['show_summary_count']), | |
| 176 | - 'show_write_review' => $boolish($atts['show_write_review']), | |
| 177 | - 'summary_align' => sanitize_key($atts['summary_align']), | |
| 178 | - 'columns' => (int) $atts['columns'], | |
| 179 | - 'max_width' => (int) $atts['max_width'], | |
| 180 | - 'gap' => (int) $atts['gap'], | |
| 181 | - // Pro | |
| 182 | - 'sort' => sanitize_key($atts['sort']), | |
| 183 | - 'keyword' => sanitize_text_field($atts['keyword']), | |
| 184 | - 'hide_empty' => $boolish($atts['hide_empty']), | |
| 185 | - 'fetch_all' => $boolish($atts['fetch_all']), | |
| 186 | - 'theme' => sanitize_key($atts['theme']), | |
| 187 | - 'accent_color' => sanitize_hex_color($atts['accent_color']) ?: '', | |
| 188 | - 'autoplay' => $boolish($atts['autoplay']), | |
| 189 | - 'schema' => $boolish($atts['schema']), | |
| 190 | - 'places' => array_map('sanitize_text_field', $places), | |
| 191 | - 'cache_ttl' => (int) $atts['cache_ttl'], | |
| 192 | - 'load_more' => $boolish($atts['load_more']), | |
| 193 | - 'per_page' => (int) $atts['per_page'], | |
| 194 | - ]); | |
| 195 | - } | |
| 196 | - | |
| 197 | - /** | |
| 198 | 48 | * Method that converts the plugin shortcoded-string into its complex content. |
| 199 | 49 | * |
| 200 | - * @param array $attributes Array of attributes | |
| 201 | - * @param string $subject The given string | |
| 202 | - * | |
| 203 | - * @return string | |
| 204 | 50 | * @since 1.0.0 |
| 205 | 51 | * @static |
| 206 | 52 | * |
| 53 | + * @param array $attributes Array of attributes | |
| 54 | + * @param string $subject The given string | |
| 55 | + * | |
| 56 | + * @return string | |
| 207 | 57 | */ |
| 208 | - | |
| 209 | - | |
| 210 | - | |
| 211 | - public static function content_protection_content($client_id = '', $protection_message = '', $allowed_roles = []) | |
| 212 | - { | |
| 213 | - $default_message = "This content is protected. Please log in or contact the administrator for access."; | |
| 214 | - $protection_message = $protection_message ?: $default_message; | |
| 215 | - | |
| 216 | - $current_user = wp_get_current_user(); | |
| 217 | - $user_roles = $current_user->roles; | |
| 218 | - | |
| 219 | - if (!empty($user_roles) && array_intersect($user_roles, $allowed_roles)) { | |
| 220 | - echo ''; | |
| 221 | - return; | |
| 222 | - } | |
| 223 | - | |
| 224 | - if (strpos($protection_message, '[user_roles]') !== false) { | |
| 225 | - $role_list = implode(', ', $allowed_roles); | |
| 226 | - $protection_message = str_replace('[user_roles]', $role_list, $protection_message); | |
| 227 | - } | |
| 228 | - | |
| 229 | - return sprintf('<div class="protected-message">%s</div>', esc_html($protection_message)); | |
| 230 | - } | |
| 231 | - | |
| 232 | - public static function display_password_form($client_id = '', $embedHtml = '', $pass_hash_key = '', $attributes = []) | |
| 233 | - { | |
| 234 | - $lock_heading = !empty($attributes['lockHeading']) ? sanitize_text_field($attributes['lockHeading']) : 'Content Locked'; | |
| 235 | - $lock_subheading = !empty($attributes['lockSubHeading']) ? sanitize_text_field($attributes['lockSubHeading']) : 'Content is locked and requires password to access it.'; | |
| 236 | - $lock_error_message = !empty($attributes['lockErrorMessage']) ? sanitize_text_field($attributes['lockErrorMessage']) : "Oops, that wasn't the right password. Try again."; | |
| 237 | - $footer_message = !empty($attributes['footerMessage']) ? sanitize_text_field($attributes['footerMessage']) : "In case you don't have the password, kindly reach out to content owner or administrator to request access."; | |
| 238 | - $password_placeholder = !empty($attributes['passwordPlaceholder']) ? sanitize_text_field($attributes['passwordPlaceholder']) : 'Password'; | |
| 239 | - $button_text = !empty($attributes['submitButtonText']) ? sanitize_text_field($attributes['submitButtonText']) : 'Unlock'; | |
| 240 | - $unlocking_text = !empty($attributes['submitUnlockingText']) ? sanitize_text_field($attributes['submitUnlockingText']) : 'Unlocking'; | |
| 241 | - $enable_footer_message = !empty($attributes['enableFooterMessage']); | |
| 242 | - | |
| 243 | - $key = Helper::get_hash(); | |
| 244 | - $salt = wp_salt(32); | |
| 245 | - $wp_hash_key = hash('sha256', $salt . $pass_hash_key); | |
| 246 | - $iv = substr($wp_hash_key, 0, 16); | |
| 247 | - | |
| 248 | - $cipher = openssl_encrypt($embedHtml, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv); | |
| 249 | - $encrypted_data = base64_encode($cipher); | |
| 250 | - | |
| 251 | - update_post_meta(get_the_ID(), 'ep_base_' . $client_id, $encrypted_data); | |
| 252 | - update_post_meta(get_the_ID(), 'hash_key_' . $client_id, $wp_hash_key); | |
| 253 | - | |
| 254 | - $lock_icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><g fill="#6354a5" class="color134563 svgShape"><path d="M46.3 28.7h-3v-6.4C43.3 16.1 38.2 11 32 11c-6.2 0-11.3 5.1-11.3 11.3v6.4h-3v-6.4C17.7 14.4 24.1 8 32 8s14.3 6.4 14.3 14.3v6.4" fill="#6354a5" class="color000000 svgShape"></path><path d="M44.8 55.9H19.2c-2.6 0-4.8-2.2-4.8-4.8V31.9c0-2.6 2.2-4.8 4.8-4.8h25.6c2.6 0 4.8 2.2 4.8 4.8v19.2c0 2.7-2.2 4.8-4.8 4.8zM19.2 30.3c-.9 0-1.6.7-1.6 1.6v19.2c0 .9.7 1.6 1.6 1.6h25.6c.9 0 1.6-.7 1.6-1.6V31.9c0-.9-.7-1.6-1.6-1.6H19.2z" fill="#6354a5" class="color000000 svgShape"></path><path d="M35.2 36.7c0 1.8-1.4 3.2-3.2 3.2s-3.2-1.4-3.2-3.2 1.4-3.2 3.2-3.2 3.2 1.5 3.2 3.2" fill="#6354a5" class="color000000 svgShape"></path><path d="M32.8 36.7h-1.6l-1.6 9.6h4.8l-1.6-9.6" fill="#6354a5" class="color000000 svgShape"></path></g></svg>'; | |
| 255 | - | |
| 256 | - return ' | |
| 257 | - <div id="ep-shortcode-content-' . $client_id . '" class="ep-shortcode-content"> | |
| 258 | - <div class="ep-embed-content-wraper"> | |
| 259 | - <div class="password-form-container"> | |
| 260 | - <h2>' . esc_html($lock_heading) . '</h2> | |
| 261 | - <p>' . esc_html($lock_subheading) . '</p> | |
| 262 | - <form class="password-form" method="post" data-unlocking-text="' . esc_attr($unlocking_text) . '"> | |
| 263 | - <div class="password-field"> | |
| 264 | - <span class="lock-icon">' . $lock_icon . '</span> | |
| 265 | - <input type="password" name="pass_' . esc_attr($client_id) . '" placeholder="' . esc_attr($password_placeholder) . '" required> | |
| 266 | - </div> | |
| 267 | - <input type="hidden" name="ep_client_id" value="' . esc_attr($client_id) . '"> | |
| 268 | - <input type="hidden" name="post_id" value="' . esc_attr(get_the_ID()) . '"> | |
| 269 | - <input type="submit" name="password_submit" value="' . esc_attr($button_text) . '"> | |
| 270 | - <div class="error-message hidden">' . esc_html($lock_error_message) . '</div> | |
| 271 | - </form> | |
| 272 | - ' . ($enable_footer_message ? '<p class="need-access-message">' . esc_html($footer_message) . '</p>' : '') . ' | |
| 273 | - </div> | |
| 274 | - </div> | |
| 275 | - </div>'; | |
| 276 | - } | |
| 277 | - | |
| 278 | - | |
| 279 | 58 | public static function do_shortcode($attributes = [], $subject = null) |
| 280 | 59 | { |
| 281 | - $plgSettings = Core::getSettings(); | |
| 282 | - | |
| 283 | - | |
| 284 | - $default = []; | |
| 285 | - if ($plgSettings->enableGlobalEmbedResize) { | |
| 286 | - $default = [ | |
| 287 | - 'width' => esc_attr($plgSettings->enableEmbedResizeWidth), | |
| 288 | - 'height' => esc_attr($plgSettings->enableEmbedResizeHeight), | |
| 289 | - 'powered_by' => !empty($plgSettings->embedpress_document_powered_by) ? esc_attr($plgSettings->embedpress_document_powered_by) : esc_attr('no'), | |
| 290 | - ]; | |
| 291 | - } | |
| 292 | - | |
| 293 | - if (is_array($attributes)) { | |
| 294 | - $attributes = array_map('esc_attr', $attributes); | |
| 295 | - } | |
| 296 | - | |
| 297 | - $attributes = wp_parse_args($attributes, $default); | |
| 298 | - | |
| 299 | - // ACF dynamic field support: [embedpress]embedpress_my_field[/embedpress] | |
| 300 | - if (!empty($subject)) { | |
| 301 | - $raw = trim(preg_replace( | |
| 302 | - '/(\[' . EMBEDPRESS_SHORTCODE . '(?:\]|.+?\])|\[\/' . EMBEDPRESS_SHORTCODE . '\])/i', | |
| 303 | - "", | |
| 304 | - $subject | |
| 305 | - )); | |
| 306 | - if (!empty($raw) && strpos($raw, 'embedpress_') === 0) { | |
| 307 | - $field_name = substr($raw, strlen('embedpress_')); | |
| 308 | - $resolved = self::resolveAcfFieldUrl($field_name); | |
| 309 | - if (!empty($resolved)) { | |
| 310 | - $subject = $resolved; | |
| 311 | - } | |
| 312 | - } | |
| 313 | - } | |
| 314 | - | |
| 315 | 60 | $embed = self::parseContent($subject, true, $attributes); |
| 316 | 61 | |
| 317 | - | |
| 318 | - $client_id = is_object($embed) ? md5($embed->embed) : ''; | |
| 319 | - | |
| 320 | - $hash_pass = isset($attributes['protection_password']) | |
| 321 | - ? hash('sha256', wp_salt(32) . md5($attributes['protection_password'])) | |
| 322 | - : ''; | |
| 323 | - | |
| 324 | - $pass_hash_key = isset($attributes['protection_password']) | |
| 325 | - ? md5($attributes['protection_password']) | |
| 326 | - : ''; | |
| 327 | - | |
| 328 | - $password_correct = $_COOKIE['password_correct_' . $client_id] ?? ''; | |
| 329 | - | |
| 330 | - $protection_type = $attributes['protection_type'] ?? 'user-role'; | |
| 331 | - $protection_password = $attributes['protection_password'] ?? ''; | |
| 332 | - $protection_content = isset($attributes['protection_content']) ? $attributes['protection_content'] === 'true' : false; | |
| 333 | - $user_role = isset($attributes['user_roles']) ? explode(',', preg_replace('/\s*,\s*/', ',', $attributes['user_roles'])) : ''; | |
| 334 | - $protection_message = $attributes['protection_message'] ?? ''; | |
| 335 | - | |
| 336 | - | |
| 337 | - // Conditions for content protection | |
| 338 | - $password_protected = $protection_type == 'password' && !empty($protection_password); | |
| 339 | - | |
| 340 | - $password_verified = $password_protected && !empty(Helper::is_password_correct($client_id)) && ($hash_pass === $password_correct); | |
| 341 | - $user_role_protected = $protection_type === 'user-role' && Helper::has_allowed_roles($user_role); | |
| 342 | - | |
| 343 | - if ( | |
| 344 | - apply_filters('embedpress/is_allow_rander', false) && ($protection_content == 'true') && (($protection_type == 'password' && !$password_verified) || ($protection_type == 'user-role' && !Helper::has_allowed_roles($user_role))) | |
| 345 | - ) { | |
| 346 | - return $password_protected | |
| 347 | - ? self::display_password_form($client_id, $embed->embed, $pass_hash_key, $attributes) | |
| 348 | - : self::content_protection_content($client_id, $protection_message, $user_role); | |
| 349 | - } | |
| 350 | - | |
| 351 | - | |
| 352 | - if (is_object($embed)) { | |
| 353 | - $array = get_object_vars($embed); | |
| 354 | - if (!empty($array[$embed->url]['provider_name']) && $array[$embed->url]['provider_name'] === 'Instagram Feed') { | |
| 355 | - // shortcode_parse_atts() lowercases keys, so `instaLayout` arrives as `instalayout`. | |
| 356 | - $attr_ci = array_change_key_case((array) $attributes, CASE_LOWER); | |
| 357 | - $layout = isset($attr_ci['instalayout']) ? $attr_ci['instalayout'] : 'insta-grid'; | |
| 358 | - $allowed_layouts = ['insta-grid', 'insta-masonry', 'insta-carousel', 'insta-justify']; | |
| 359 | - if (!in_array($layout, $allowed_layouts, true)) { | |
| 360 | - $layout = 'insta-grid'; | |
| 361 | - } | |
| 362 | - | |
| 363 | - // For carousel layout, the front-end JS (assets/js/instafeed.js) looks | |
| 364 | - // up `data-carouselid` + `data-carousel-options` on the wrapper to | |
| 365 | - // initialize CgCarousel and unhide the nav buttons. Without these the | |
| 366 | - // shortcode-rendered carousel has no arrows and no scroll behavior. | |
| 367 | - $carousel_attrs = ''; | |
| 368 | - if ($layout === 'insta-carousel') { | |
| 369 | - $boolish = function ($v, $default) { | |
| 370 | - if ($v === null) return $default; | |
| 371 | - $v = is_string($v) ? strtolower($v) : $v; | |
| 372 | - return in_array($v, ['true', '1', 1, true], true); | |
| 373 | - }; | |
| 374 | - $options = [ | |
| 375 | - 'layout' => 'insta-carousel', | |
| 376 | - 'slideshow' => isset($attr_ci['slidesshow']) ? intval($attr_ci['slidesshow']) : 3, | |
| 377 | - 'autoplay' => $boolish($attr_ci['carouselautoplay'] ?? null, false), | |
| 378 | - 'autoplayspeed' => isset($attr_ci['autoplayspeed']) ? intval($attr_ci['autoplayspeed']) : 3000, | |
| 379 | - 'transitionspeed' => isset($attr_ci['transitionspeed']) ? intval($attr_ci['transitionspeed']) : 650, | |
| 380 | - 'loop' => $boolish($attr_ci['carouselloop'] ?? null, false), | |
| 381 | - 'arrows' => $boolish($attr_ci['carouselarrows'] ?? null, true), | |
| 382 | - 'spacing' => isset($attr_ci['carouselspacing']) ? intval($attr_ci['carouselspacing']) : 10, | |
| 383 | - ]; | |
| 384 | - $carousel_attrs = ' data-carouselid="' . esc_attr('epsc-' . substr(md5($embed->embed . microtime(true)), 0, 10)) | |
| 385 | - . '" data-carousel-options="' . esc_attr(wp_json_encode($options)) . '"'; | |
| 386 | - } | |
| 387 | - | |
| 388 | - $embed->embed = '<div class="ep-embed-content-wraper ' . esc_attr($layout) . '"' . $carousel_attrs . '>' . $embed->embed . '</div>'; | |
| 389 | - }; | |
| 390 | - } | |
| 391 | - | |
| 392 | 62 | return is_object($embed) ? $embed->embed : $embed; |
| 393 | 63 | } |
| 394 | 64 | |
| 395 | 65 | /** |
| @@ -394,373 +64,283 @@ | ||
| 394 | 64 | |
| 395 | 65 | /** |
| 396 | 66 | * Replace a given content with its embeded HTML code. |
| 397 | 67 | * |
| 398 | - * @param string The raw content that will be replaced. | |
| 399 | - * @param bool $stripNewLine | |
| 400 | - * @param array $customAttributes | |
| 401 | - * @return string|object | |
| 402 | 68 | * @since 1.0.0 |
| 403 | 69 | * @static |
| 70 | + * | |
| 71 | + * @param string The raw content that will be replaced. | |
| 72 | + * @param boolean Optional. If true, new lines at the end of the embeded code are stripped. | |
| 73 | + * | |
| 74 | + * @return string | |
| 404 | 75 | */ |
| 405 | 76 | public static function parseContent($subject, $stripNewLine = false, $customAttributes = []) |
| 406 | 77 | { |
| 407 | - // Dynamic-source shortcodes carry no body — the URL lives in a custom | |
| 408 | - // field, e.g. [embedpress dynamic_source="metabox" dynamic_field="pdf"]. | |
| 409 | - // Resolve it up front so it becomes the $subject and flows through the | |
| 410 | - // normal render path below (the in-body resolution at the bottom only | |
| 411 | - // runs when $subject is already non-empty). Pro-gated for parity with | |
| 412 | - // the block + Elementor widget paths. | |
| 413 | - if ( | |
| 414 | - empty($subject) | |
| 415 | - && !empty($customAttributes['dynamic_source']) | |
| 416 | - && !empty($customAttributes['dynamic_field']) | |
| 417 | - && \EmbedPress\Includes\Classes\Helper::is_pro_features_enabled() | |
| 418 | - ) { | |
| 419 | - $resolved = \EmbedPress\Includes\Classes\DynamicFieldResolver::resolve_field( | |
| 420 | - $customAttributes['dynamic_source'], | |
| 421 | - $customAttributes['dynamic_field'] | |
| 422 | - ); | |
| 423 | - if ($resolved !== '') { | |
| 424 | - $subject = $resolved; | |
| 425 | - } | |
| 426 | - } | |
| 427 | - | |
| 428 | - if (!empty($subject)) { | |
| 78 | + if ( ! empty($subject)) { | |
| 429 | 79 | if (empty($customAttributes)) { |
| 430 | 80 | $customAttributes = self::parseContentAttributesFromString($subject); |
| 431 | 81 | } |
| 432 | - self::set_default_size($customAttributes); | |
| 433 | - $url = preg_replace( | |
| 434 | - '/(\[' . EMBEDPRESS_SHORTCODE . '(?:\]|.+?\])|\[\/' . EMBEDPRESS_SHORTCODE . '\])/i', | |
| 435 | - "", | |
| 436 | - $subject | |
| 437 | - ); | |
| 438 | 82 | |
| 439 | - if (strpos($url, 'youtube.com/embed') !== false) { | |
| 440 | - preg_match("/embed\/([a-zA-Z0-9_-]+)/", $url, $matches); | |
| 83 | + $content = preg_replace('/(\[' . EMBEDPRESS_SHORTCODE . '(?:\]|.+?\])|\[\/' . EMBEDPRESS_SHORTCODE . '\])/i', | |
| 84 | + "", $subject); | |
| 441 | 85 | |
| 442 | - if (isset($matches[1])) { | |
| 443 | - $videoId = $matches[1]; | |
| 444 | - $url = 'https://www.youtube.com/watch?v=' . $videoId; | |
| 445 | - } | |
| 86 | + // Converts any special HTML entities back to characters. | |
| 87 | + $content = htmlspecialchars_decode($content); | |
| 88 | + | |
| 89 | + // Check if the WP_oEmbed class is loaded | |
| 90 | + if ( ! self::$oEmbedInstance) { | |
| 91 | + require_once ABSPATH . 'wp-includes/class-oembed.php'; | |
| 92 | + | |
| 93 | + self::$oEmbedInstance = _wp_oembed_get_object(); | |
| 446 | 94 | } |
| 447 | 95 | |
| 96 | + $emberaInstanceSettings = [ | |
| 97 | + 'params' => [], | |
| 98 | + ]; | |
| 448 | 99 | |
| 449 | - $uniqid = 'ose-uid-' . md5($url); | |
| 450 | - $subject = esc_url($subject); | |
| 100 | + $content_uid = md5($content); | |
| 451 | 101 | |
| 102 | + $attributes = self::parseContentAttributes($customAttributes, $content_uid); | |
| 103 | + if (isset($attributes['width']) || isset($attributes['height'])) { | |
| 104 | + if (isset($attributes['width'])) { | |
| 105 | + $emberaInstanceSettings['params']['width'] = $attributes['width']; | |
| 106 | + unset($attributes['width']); | |
| 107 | + } | |
| 452 | 108 | |
| 453 | - // Converts any special HTML entities back to characters. | |
| 454 | - $url = htmlspecialchars_decode($url); | |
| 455 | - $url = esc_url($url); | |
| 456 | - | |
| 457 | - // Dynamic-source resolution: blocks and shortcodes may declare a | |
| 458 | - // custom-field source (ACF/MetaBox/Pods/Toolset/JetEngine/meta) via | |
| 459 | - // attributes, so the URL is looked up per-post at render time. The | |
| 460 | - // resolved URL replaces the (often-empty) saved one. | |
| 461 | - // Shortcode form: [embedpress dynamic_source="metabox" dynamic_field="pdf_file"] | |
| 462 | - // Pro-gated — see Helper::is_pro_features_enabled() for parity with | |
| 463 | - // the block + Elementor widget paths. | |
| 464 | - if ( | |
| 465 | - !empty($customAttributes['dynamic_source']) | |
| 466 | - && !empty($customAttributes['dynamic_field']) | |
| 467 | - && \EmbedPress\Includes\Classes\Helper::is_pro_features_enabled() | |
| 468 | - ) { | |
| 469 | - $resolved = \EmbedPress\Includes\Classes\DynamicFieldResolver::resolve_field( | |
| 470 | - $customAttributes['dynamic_source'], | |
| 471 | - $customAttributes['dynamic_field'] | |
| 472 | - ); | |
| 473 | - if ($resolved !== '') { | |
| 474 | - $url = esc_url($resolved); | |
| 109 | + if (isset($attributes['height'])) { | |
| 110 | + $emberaInstanceSettings['params']['height'] = $attributes['height']; | |
| 111 | + unset($attributes['height']); | |
| 475 | 112 | } |
| 476 | 113 | } |
| 477 | 114 | |
| 478 | - $url = apply_filters('embedpress/resolve_url', $url, $customAttributes); | |
| 115 | + // Identify what service provider the shortcode's link belongs to | |
| 116 | + $serviceProvider = self::$oEmbedInstance->get_provider($content); | |
| 479 | 117 | |
| 480 | - $content_uid = md5($url); | |
| 481 | 118 | |
| 482 | - self::$ombed_attributes = self::parseContentAttributes($customAttributes, $content_uid); | |
| 119 | + // Check if OEmbed was unable to detect the url service provider. | |
| 120 | + if (empty($serviceProvider)) { | |
| 121 | + // Attempt to do the same using Embera. | |
| 122 | + $emberaInstance = new Embera($emberaInstanceSettings); | |
| 123 | + // Add support to the user's custom service providers | |
| 124 | + $additionalServiceProviders = Core::getAdditionalServiceProviders(); | |
| 125 | + if ( ! empty($additionalServiceProviders)) { | |
| 126 | + foreach ($additionalServiceProviders as $serviceProviderClassName => $serviceProviderUrls) { | |
| 127 | + self::addServiceProvider($serviceProviderClassName, $serviceProviderUrls, $emberaInstance); | |
| 128 | + } | |
| 483 | 129 | |
| 484 | - self::set_embera_settings(self::$ombed_attributes); | |
| 130 | + unset($serviceProviderUrls, $serviceProviderClassName); | |
| 131 | + } | |
| 485 | 132 | |
| 486 | - // Identify what service provider the shortcode's link belongs to | |
| 487 | - $is_embra_provider = apply_filters('embedpress:isEmbra', false, $url, self::get_embera_settings()); | |
| 488 | - | |
| 489 | - if ($is_embra_provider || (strpos($url, 'meetup.com') !== false) || (strpos($url, 'sway.office.com') !== false) || (strpos($url, 'wistia.com') !== false) || self::is_problematic_provider($url) ) { | |
| 490 | - $serviceProvider = ''; | |
| 133 | + // Attempt to fetch more info about the url-embed. | |
| 134 | + $urlData = $emberaInstance->getUrlInfo($content); | |
| 491 | 135 | } else { |
| 492 | - $serviceProvider = self::get_oembed()->get_provider($url); | |
| 136 | + // Attempt to fetch more info about the url-embed. | |
| 137 | + $urlData = self::$oEmbedInstance->fetch($serviceProvider, $content, $attributes); | |
| 493 | 138 | } |
| 494 | 139 | |
| 495 | - // FIX FOR MEETUP as MEETUP API is OFF, use OUR custom embed | |
| 496 | - if ('https://api.meetup.com/oembed' === $serviceProvider) { | |
| 497 | - $serviceProvider = ''; | |
| 498 | - } | |
| 499 | - | |
| 500 | - // Prevent self-referencing loop: if the provider URL points to our own | |
| 501 | - // REST endpoint, skip WP oembed and let Embera handle it directly | |
| 502 | - if (!empty($serviceProvider) && strpos($serviceProvider, 'embedpress/v1/oembed/') !== false) { | |
| 503 | - $serviceProvider = ''; | |
| 504 | - } | |
| 505 | - | |
| 506 | - | |
| 507 | - $urlData = self::get_url_data($url, self::$ombed_attributes, $serviceProvider); | |
| 508 | - | |
| 509 | 140 | // Sanitize the data |
| 510 | - $urlData = self::sanitizeUrlData($urlData, $url); | |
| 141 | + $urlData = self::sanitizeUrlData($urlData); | |
| 511 | 142 | |
| 512 | - | |
| 513 | - | |
| 514 | 143 | // Stores the original content |
| 515 | 144 | if (is_object($urlData)) { |
| 516 | - $urlData->originalContent = $url; | |
| 145 | + $urlData->originalContent = $content; | |
| 517 | 146 | } |
| 518 | 147 | |
| 519 | - $embedResults = apply_filters('embedpress:onBeforeEmbed', $urlData, $subject); | |
| 520 | - | |
| 521 | - if (empty($embedResults)) { | |
| 148 | + $eventResults = apply_filters('embedpress:onBeforeEmbed', $urlData); | |
| 149 | + if (empty($eventResults)) { | |
| 150 | + // EmbedPress seems unable to embed the url. | |
| 522 | 151 | return $subject; |
| 523 | 152 | } |
| 524 | 153 | |
| 525 | 154 | // Transform all shortcode attributes into html form. I.e.: {foo: "joe"} -> foo="joe" |
| 526 | - $attributesHtml = ['class="ose-{provider_alias} ' . $uniqid . ' ose-embedpress-responsive"']; | |
| 527 | - //$attributesHtml = []; | |
| 528 | - //foreach ( self::$ombed_attributes as $attrName => $attrValue ) { | |
| 529 | - // $attributesHtml[] = $attrName . '="' . $attrValue . '"'; | |
| 530 | - //} | |
| 155 | + $attributesHtml = []; | |
| 156 | + foreach ($attributes as $attrName => $attrValue) { | |
| 157 | + $attributesHtml[] = $attrName . '="' . $attrValue . '"'; | |
| 158 | + } | |
| 531 | 159 | |
| 532 | - // Check if $url is a google shortened url and tries to extract from it which Google service it refers to. | |
| 533 | - self::check_for_google_url($url); | |
| 534 | - $provider_name = self::get_provider_name($urlData, $url); | |
| 160 | + // Define the EmbedPress html template where the generated embed will be injected in | |
| 161 | + $embedTemplate = '<div ' . implode(' ', $attributesHtml) . '>{html}</div>'; | |
| 535 | 162 | |
| 536 | - // Get excluded height sources | |
| 537 | - $excludedHeightSources = self::get_excluded_height_sources(); | |
| 538 | - $shouldExcludeHeight = in_array($provider_name, $excludedHeightSources); | |
| 163 | + // Check if $content is a google shortened url and tries to extract from it which Google service it refers to. | |
| 164 | + if (preg_match('/http[s]?:\/\/goo\.gl\/(?:([a-z]+)\/)?[a-z0-9]+\/?$/i', $content, $matches)) { | |
| 165 | + // Fetch all headers from the short-url so we can know how to handle its original content depending on the service. | |
| 166 | + $headers = get_headers($content); | |
| 539 | 167 | |
| 540 | - if (isset($customAttributes['height'])) { | |
| 541 | - $height = esc_attr($customAttributes['height']); | |
| 168 | + $supportedServicesHeadersPatterns = [ | |
| 169 | + 'maps' => '/^Location:\s+(http[s]?:\/\/.+)$/i', | |
| 170 | + ]; | |
| 171 | + | |
| 172 | + $service = isset($matches[1]) ? strtolower($matches[1]) : null; | |
| 173 | + // No specific service was found in the url. | |
| 174 | + if (empty($service)) { | |
| 175 | + // Let's try to guess which service the original url belongs to. | |
| 176 | + foreach ($headers as $header) { | |
| 177 | + // Check if the short-url reffers to a Google Maps url. | |
| 178 | + if (preg_match($supportedServicesHeadersPatterns['maps'], $header, $matches)) { | |
| 179 | + // Replace the shortened url with its original url. | |
| 180 | + $content = $matches[1]; | |
| 181 | + break; | |
| 182 | + } | |
| 183 | + } | |
| 184 | + unset($header); | |
| 185 | + } else { | |
| 186 | + // Check if the Google service is supported atm. | |
| 187 | + if (isset($supportedServicesHeadersPatterns[$service])) { | |
| 188 | + // Tries to extract the url based on its headers. | |
| 189 | + $originalUrl = self::extractContentFromHeaderAsArray($supportedServicesHeadersPatterns[$service], | |
| 190 | + $headers); | |
| 191 | + // Replace the shortened url with its original url if the specific header was found. | |
| 192 | + if ( ! empty($originalUrl)) { | |
| 193 | + $content = $originalUrl; | |
| 194 | + } | |
| 195 | + unset($originalUrl); | |
| 196 | + } | |
| 197 | + } | |
| 198 | + unset($service, $supportedServicesHeadersPatterns, $headers, $matches); | |
| 542 | 199 | } |
| 543 | 200 | |
| 544 | - if (isset($customAttributes['width'])) { | |
| 545 | - $width = esc_attr($customAttributes['width']); | |
| 546 | - // Only apply height to wrapper if provider is not excluded | |
| 547 | - if ($shouldExcludeHeight) { | |
| 548 | - $attributesHtml[] = "style=\"width:{$width}px; max-width:100%; display:inline-block;\""; | |
| 201 | + // Facebook is a special case. WordPress will try to embed them using OEmbed, but they always end up embedding the profile page, regardless | |
| 202 | + // 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. | |
| 203 | + if (isset($urlData->provider_name) && in_array($urlData->provider_name, ['Facebook'])) { | |
| 204 | + // Check if this is a Facebook profile url. | |
| 205 | + if (preg_match('/facebook\.com\/(?:[^\/]+?)\/?$/', $content, $match)) { | |
| 206 | + // Try to embed the url using WP's OSEmbed. | |
| 207 | + $parsedContent = self::$oEmbedInstance->get_html($content, $attributes); | |
| 549 | 208 | } else { |
| 550 | - $attributesHtml[] = "style=\"width:{$width}px; height:{$height}px; max-height:{$height}px; max-width:100%; display:inline-block;\""; | |
| 209 | + // Try to embed the url using EmbedPress' Embera. | |
| 210 | + $parsedContent = false; | |
| 551 | 211 | } |
| 212 | + } else { | |
| 213 | + // Try to embed the url using WP's OSEmbed. | |
| 214 | + $parsedContent = self::$oEmbedInstance->get_html($content, $attributes); | |
| 552 | 215 | } |
| 553 | - $provider_name = sanitize_text_field($provider_name); | |
| 554 | 216 | |
| 555 | - // $html = '{html}'; | |
| 556 | - // if (strpos($url, 'youtube') !== false) { | |
| 557 | - // $html = '<div class="youtube-video">{html}</div>'; | |
| 558 | - // } | |
| 217 | + if ( ! $parsedContent) { | |
| 218 | + if ( ! isset($emberaInstance)) { | |
| 219 | + // If the embed couldn't be generated, we'll try to use Embera's API | |
| 220 | + $emberaInstance = new Embera($emberaInstanceSettings); | |
| 221 | + // Add support to the user's custom service providers | |
| 222 | + $additionalServiceProviders = Core::getAdditionalServiceProviders(); | |
| 223 | + if ( ! empty($additionalServiceProviders)) { | |
| 224 | + foreach ($additionalServiceProviders as $serviceProviderClassName => $serviceProviderUrls) { | |
| 225 | + self::addServiceProvider($serviceProviderClassName, $serviceProviderUrls, $emberaInstance); | |
| 226 | + } | |
| 559 | 227 | |
| 560 | - // Add data-embed-type attribute based on provider | |
| 561 | - $embedType = Helper::get_provider_name($url); | |
| 562 | - $embedTypeAttr = $embedType ? ' data-embed-type="' . esc_attr($embedType) . '"' : ''; | |
| 228 | + unset($serviceProviderUrls, $serviceProviderClassName); | |
| 229 | + } | |
| 230 | + } | |
| 563 | 231 | |
| 564 | - $embedTemplate = '<div ' . implode(' ', $attributesHtml) . $embedTypeAttr . '>{html}</div>'; | |
| 232 | + // Register the html template | |
| 233 | + $emberaFormaterInstance = new Formatter($emberaInstance, true); | |
| 234 | + $emberaFormaterInstance->setTemplate($embedTemplate); | |
| 565 | 235 | |
| 566 | - $parsedContent = self::get_content_from_template($url, $embedTemplate, $serviceProvider); | |
| 567 | - // Embera's ResponsiveEmbeds::addClasses() runs a global | |
| 568 | - // `class="(.+?)"` replace. When an oEmbed payload carries a | |
| 569 | - // wrapper element with a class AND an iframe with an empty | |
| 570 | - // class="" (Sketchfab, Flourish, …), the non-greedy capture | |
| 571 | - // jumps the gap and swallows the iframe's ` src="` opening | |
| 572 | - // quote, leaving `src= <classtokens>"<URL>"`. Restore a proper | |
| 573 | - // quoted src before any later transform can truncate the URL. | |
| 574 | - $parsedContent = self::repair_responsive_class_corruption($parsedContent, $url); | |
| 575 | - // Replace all single quotes to double quotes. I.e: foo='joe' -> foo="joe" | |
| 576 | - $parsedContent = str_replace("'", '"', $parsedContent); | |
| 577 | - $parsedContent = str_replace("{provider_alias}", esc_html($provider_name), $parsedContent); | |
| 236 | + // Try to generate the embed using Embera API | |
| 237 | + $parsedContent = $emberaFormaterInstance->transform($content); | |
| 578 | 238 | |
| 579 | - // YT playlist layouts fill their container, so strip the | |
| 580 | - // hard-coded width/height/inline-block from the outer wrapper | |
| 581 | - // and tag it for CSS targeting. Detect the layout from the | |
| 582 | - // wrapper we just emitted ourselves. | |
| 583 | - $playlist_layout_class = ''; | |
| 584 | - $playlist_layouts_known = ['queue', 'theatre', 'library', 'spotlight', 'cinema', 'magazine']; | |
| 585 | - foreach ($playlist_layouts_known as $pl) { | |
| 586 | - if (strpos($parsedContent, 'ep-player-wrap layout-' . $pl) !== false) { | |
| 587 | - $playlist_layout_class = 'has-layout-' . $pl; | |
| 588 | - break; | |
| 239 | + unset($emberaFormaterInstance, $additionalServiceProviders, $emberaInstance); | |
| 240 | + } else { | |
| 241 | + // Inject the generated code inside the html template | |
| 242 | + $parsedContent = str_replace('{html}', $parsedContent, $embedTemplate); | |
| 243 | + | |
| 244 | + // Replace all single quotes to double quotes. I.e: foo='joe' -> foo="joe" | |
| 245 | + $parsedContent = str_replace("'", '"', $parsedContent); | |
| 246 | + | |
| 247 | + // Replace the flag `{provider_alias}` which is used by Embera with the "ose-<serviceProviderAlias>". I.e: YouTube -> "ose-youtube" | |
| 248 | + $parsedContent = preg_replace('/((?:ose-)?\{provider_alias\})/i', | |
| 249 | + "ose-" . strtolower($urlData->provider_name), $parsedContent); | |
| 250 | + } | |
| 251 | + | |
| 252 | + if (isset($urlData->provider_name) || (is_array($urlData) && isset($urlData[$content]['provider_name']))) { | |
| 253 | + // NFB seems to always return their embed code with all HTML entities into their applicable characters string. | |
| 254 | + 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")) { | |
| 255 | + $parsedContent = html_entity_decode($parsedContent); | |
| 256 | + } elseif ((isset($urlData->provider_name) && strtoupper($urlData->provider_name) === "FACEBOOK") || (is_array($urlData) && isset($urlData[$content]['provider_name']) && strtoupper($urlData[$content]['provider_name']) === "FACEBOOK")) { | |
| 257 | + $plgSettings = Core::getSettings(); | |
| 258 | + | |
| 259 | + // Check if the user wants to force a certain language into Facebook embeds. | |
| 260 | + $locale = isset($plgSettings->fbLanguage) && ! empty($plgSettings->fbLanguage) ? $plgSettings->fbLanguage : false; | |
| 261 | + if ( ! ! $locale) { | |
| 262 | + // Replace the automatically detected language by Facebook's API with the language chosen by the user. | |
| 263 | + $parsedContent = preg_replace('/\/[a-z]{2}\_[a-z]{2}\/sdk\.js/i', "/{$locale}/sdk.js", | |
| 264 | + $parsedContent); | |
| 265 | + } | |
| 266 | + | |
| 267 | + // 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. | |
| 268 | + if (is_admin()) { | |
| 269 | + $parsedContent = preg_replace('~data\-adapt\-container\-width=\"(?:true|1)\"~i', | |
| 270 | + 'data-adapt-container-width="0"', $parsedContent); | |
| 271 | + } | |
| 272 | + | |
| 273 | + unset($locale, $plgSettings); | |
| 589 | 274 | } |
| 590 | 275 | } |
| 591 | - if (!empty($playlist_layout_class)) { | |
| 592 | - $parsedContent = preg_replace( | |
| 593 | - '~<div class="ose-youtube ([^"]+)"\s+style="[^"]*"~', | |
| 594 | - '<div class="ose-youtube $1 ' . $playlist_layout_class . '"', | |
| 595 | - $parsedContent, | |
| 596 | - 1 | |
| 597 | - ); | |
| 598 | - } | |
| 599 | - $parsedContent = str_replace('sandbox="allow-scripts"', 'sandbox="allow-modals allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox"', $parsedContent); | |
| 600 | - $parsedContent = str_replace('<iframe ', '<iframe allowFullScreen="true" ', $parsedContent); | |
| 601 | 276 | |
| 602 | - self::purify_html_content($parsedContent); | |
| 603 | - self::modify_content_for_fb_and_canada($provider_name, $parsedContent); | |
| 604 | 277 | unset($embedTemplate, $serviceProvider); |
| 605 | 278 | |
| 606 | 279 | // This assure that the iframe has the same dimensions the user wants to |
| 607 | - if (isset(self::$emberaInstanceSettings['maxwidth']) || isset(self::$emberaInstanceSettings['maxheight'])) { | |
| 608 | - // Get excluded sources for height attribute | |
| 609 | - $excludedHeightSources = self::get_excluded_height_sources(); | |
| 610 | - $shouldExcludeHeight = in_array($provider_name, $excludedHeightSources); | |
| 611 | - | |
| 612 | - if (isset(self::$emberaInstanceSettings['maxwidth']) && isset(self::$emberaInstanceSettings['maxheight'])) { | |
| 613 | - $customWidth = (int) self::$emberaInstanceSettings['maxwidth']; | |
| 614 | - $customHeight = (int) self::$emberaInstanceSettings['maxheight']; | |
| 280 | + if (isset($emberaInstanceSettings['params']['width']) || isset($emberaInstanceSettings['params']['height'])) { | |
| 281 | + if (isset($emberaInstanceSettings['params']['width']) && isset($emberaInstanceSettings['params']['height'])) { | |
| 282 | + $customWidth = (int)$emberaInstanceSettings['params']['width']; | |
| 283 | + $customHeight = (int)$emberaInstanceSettings['params']['height']; | |
| 615 | 284 | } else { |
| 616 | 285 | if (preg_match('~width="(\d+)"|width\s+:\s+(\d+)~i', $parsedContent, $matches)) { |
| 617 | - $iframeWidth = (int) $matches[1]; | |
| 286 | + $iframeWidth = (int)$matches[1]; | |
| 618 | 287 | } |
| 619 | 288 | |
| 620 | 289 | if (preg_match('~height="(\d+)"|height\s+:\s+(\d+)~i', $parsedContent, $matches)) { |
| 621 | - $iframeHeight = (int) $matches[1]; | |
| 290 | + $iframeHeight = (int)$matches[1]; | |
| 622 | 291 | } |
| 623 | 292 | |
| 624 | 293 | if (isset($iframeWidth) && isset($iframeHeight) && $iframeWidth > 0 && $iframeHeight > 0) { |
| 625 | 294 | $iframeRatio = ceil($iframeWidth / $iframeHeight); |
| 626 | 295 | |
| 627 | - if (isset(self::$emberaInstanceSettings['maxwidth'])) { | |
| 628 | - $customWidth = (int) self::$emberaInstanceSettings['maxwidth']; | |
| 296 | + if (isset($emberaInstanceSettings['params']['width'])) { | |
| 297 | + $customWidth = (int)$emberaInstanceSettings['params']['width']; | |
| 629 | 298 | $customHeight = ceil($customWidth / $iframeRatio); |
| 630 | 299 | } else { |
| 631 | - $customHeight = (int) self::$emberaInstanceSettings['maxheight']; | |
| 632 | - $customWidth = $iframeRatio * $customHeight; | |
| 300 | + $customHeight = (int)$emberaInstanceSettings['params']['height']; | |
| 301 | + $customWidth = $iframeRatio * $customHeight; | |
| 633 | 302 | } |
| 634 | 303 | } |
| 635 | 304 | } |
| 636 | 305 | |
| 637 | 306 | if (isset($customWidth) && isset($customHeight)) { |
| 638 | - // Always apply width changes | |
| 639 | 307 | if (preg_match('~width="(\d+)"~i', $parsedContent)) { |
| 640 | - $parsedContent = preg_replace( | |
| 641 | - '~width="(\d+)"~i', | |
| 642 | - 'width="' . esc_attr($customWidth) . '"', | |
| 643 | - $parsedContent | |
| 644 | - ); | |
| 645 | - } elseif (preg_match('~width="({.+})"~i', $parsedContent)) { | |
| 646 | - // this block was needed for twitch that has width="{width}" in iframe | |
| 647 | - $parsedContent = preg_replace( | |
| 648 | - '~width="({.+})"~i', | |
| 649 | - 'width="' . esc_attr($customWidth) . '"', | |
| 650 | - $parsedContent | |
| 651 | - ); | |
| 308 | + $parsedContent = preg_replace('~width="(\d+)"~i', 'width="' . $customWidth . '"', | |
| 309 | + $parsedContent); | |
| 652 | 310 | } |
| 653 | 311 | |
| 654 | - // Only apply height changes if provider is not excluded | |
| 655 | - if (!$shouldExcludeHeight) { | |
| 656 | - if (preg_match('~height="(\d+)"~i', $parsedContent)) { | |
| 657 | - $parsedContent = preg_replace( | |
| 658 | - '~height="(\d+)"~i', | |
| 659 | - 'height="' . esc_attr($customHeight) . '"', | |
| 660 | - $parsedContent | |
| 661 | - ); | |
| 662 | - } elseif (preg_match('~height="({.+})"~i', $parsedContent)) { | |
| 663 | - $parsedContent = preg_replace( | |
| 664 | - '~height="({.+})"~i', | |
| 665 | - 'height="' . esc_attr($customHeight) . '"', | |
| 666 | - $parsedContent | |
| 667 | - ); | |
| 668 | - } | |
| 312 | + if (preg_match('~height="(\d+)"~i', $parsedContent)) { | |
| 313 | + $parsedContent = preg_replace('~height="(\d+)"~i', 'height="' . $customHeight . '"', | |
| 314 | + $parsedContent); | |
| 669 | 315 | } |
| 670 | 316 | |
| 671 | 317 | if (preg_match('~width\s+:\s+(\d+)~i', $parsedContent)) { |
| 672 | - $parsedContent = preg_replace( | |
| 673 | - '~width\s+:\s+(\d+)~i', | |
| 674 | - 'width: ' . esc_attr($customWidth), | |
| 675 | - $parsedContent | |
| 676 | - ); | |
| 318 | + $parsedContent = preg_replace('~width\s+:\s+(\d+)~i', 'width: ' . $customWidth, $parseContent); | |
| 677 | 319 | } |
| 678 | 320 | |
| 679 | - // Only apply height CSS changes if provider is not excluded | |
| 680 | - if (!$shouldExcludeHeight) { | |
| 681 | - if (preg_match('~height\s+:\s+(\d+)~i', $parsedContent)) { | |
| 682 | - $parsedContent = preg_replace( | |
| 683 | - '~height\s+:\s+(\d+)~i', | |
| 684 | - 'height: ' . esc_attr($customHeight), | |
| 685 | - $parsedContent | |
| 686 | - ); | |
| 687 | - } | |
| 321 | + if (preg_match('~height\s+:\s+(\d+)~i', $parsedContent)) { | |
| 322 | + $parsedContent = preg_replace('~height\s+:\s+(\d+)~i', 'height: ' . $customHeight, | |
| 323 | + $parseContent); | |
| 688 | 324 | } |
| 689 | - | |
| 690 | - if ('gfycat' === $provider_name && preg_match('~height\s*:\s*auto\s*;~i', $parsedContent) && !$shouldExcludeHeight) { | |
| 691 | - $parsedContent = preg_replace( | |
| 692 | - '~height\s*:\s*auto\s*~i', | |
| 693 | - 'height: ' . esc_attr($customHeight) . 'px', | |
| 694 | - $parsedContent | |
| 695 | - ); | |
| 696 | - $parsedContent = preg_replace( | |
| 697 | - '~style="position:relative;padding-bottom(.+?)"~i', | |
| 698 | - '', | |
| 699 | - $parsedContent | |
| 700 | - ); | |
| 701 | - $styles = "<style> | |
| 702 | - .ose-gfycat.ose-embedpress-responsive{ | |
| 703 | - position: relative; | |
| 704 | - } | |
| 705 | - </style>"; | |
| 706 | - $parsedContent = $styles . $parsedContent; | |
| 707 | - } | |
| 708 | 325 | } |
| 709 | 326 | } |
| 710 | 327 | |
| 711 | - | |
| 712 | - | |
| 713 | - | |
| 714 | - | |
| 715 | - if ('the-new-york-times' === $provider_name && isset($customAttributes['height']) && isset($customAttributes['width'])) { | |
| 716 | - $height = $customAttributes['height']; | |
| 717 | - $width = $customAttributes['width']; | |
| 718 | - $styles = <<<KAMAL | |
| 719 | -<style> | |
| 720 | -.ose-the-new-york-times iframe{ | |
| 721 | - min-height: auto; | |
| 722 | - height: {height}px; | |
| 723 | - width: {width}px; | |
| 724 | - max-width:100% | |
| 725 | - max-height: 100%; | |
| 726 | -} | |
| 727 | -</style> | |
| 728 | -KAMAL; | |
| 729 | - $styles = str_replace(['{height}', '{width}'], [esc_attr($height), esc_attr($width)], $styles); | |
| 730 | - $parsedContent = $styles . $parsedContent; | |
| 731 | - } | |
| 732 | - | |
| 733 | 328 | if ($stripNewLine) { |
| 734 | 329 | $parsedContent = preg_replace('/\n/', '', $parsedContent); |
| 735 | 330 | } |
| 736 | 331 | |
| 332 | + $parsedContent = apply_filters('pp_embed_parsed_content', $parsedContent, $urlData, $attributes); | |
| 737 | 333 | |
| 738 | - $parsedContent = apply_filters('pp_embed_parsed_content', $parsedContent, $urlData, self::get_oembed_attributes()); | |
| 739 | - | |
| 740 | - if (!empty($parsedContent)) { | |
| 741 | - // Get provider name using Helper method | |
| 742 | - $provider_name = Helper::get_provider_name($url); | |
| 743 | - | |
| 744 | - $embed = (object) array_merge((array) $urlData, [ | |
| 745 | - 'attributes' => (object) self::get_oembed_attributes(), | |
| 334 | + if ( ! empty($parsedContent)) { | |
| 335 | + $embed = (object)array_merge((array)$urlData, [ | |
| 336 | + 'attributes' => (object)$attributes, | |
| 746 | 337 | 'embed' => $parsedContent, |
| 747 | - 'url' => $url, | |
| 748 | - 'provider_name' => $provider_name, | |
| 338 | + 'url' => $content, | |
| 749 | 339 | ]); |
| 750 | 340 | |
| 751 | - $embed = self::modify_spotify_content($embed); | |
| 752 | 341 | $embed = apply_filters('embedpress:onAfterEmbed', $embed); |
| 753 | 342 | |
| 754 | - // Attributes to remove | |
| 755 | - $attributesToRemove = 'autoplay;'; | |
| 756 | - | |
| 757 | - // New attribute to add | |
| 758 | - $newAttribute = 'encrypted-media;' . 'accelerometer;' . 'autoplay;' . 'clipboard-write;' . 'gyroscope;' . 'picture-in-picture'; | |
| 759 | - | |
| 760 | - // Remove existing attributes | |
| 761 | - $embed->embed = str_replace($attributesToRemove, $newAttribute, $embed->embed); | |
| 762 | - | |
| 763 | 343 | return $embed; |
| 764 | 344 | } |
| 765 | 345 | } |
| 766 | 346 | |
| @@ -766,110 +346,32 @@ | ||
| 766 | 346 | |
| 767 | 347 | return $subject; |
| 768 | 348 | } |
| 769 | 349 | |
| 770 | - protected static function get_oembed() | |
| 771 | - { | |
| 772 | - if (!self::$oEmbedInstance) { | |
| 773 | - global $wp_version; | |
| 774 | - if (version_compare($wp_version, '5.3.0', '>=')) { | |
| 775 | - require_once ABSPATH . 'wp-includes/class-wp-oembed.php'; | |
| 776 | - } else { | |
| 777 | - require_once ABSPATH . 'wp-includes/class-oembed.php'; | |
| 778 | - } | |
| 779 | - self::$oEmbedInstance = _wp_oembed_get_object(); | |
| 780 | - } | |
| 781 | - return self::$oEmbedInstance; | |
| 782 | - } | |
| 783 | - | |
| 784 | - protected static function set_default_size(&$customAttributes) | |
| 785 | - { | |
| 786 | - | |
| 787 | - $plgSettings = Core::getSettings(); | |
| 788 | - | |
| 789 | - | |
| 790 | - if (empty($customAttributes['width'])) { | |
| 791 | - $customAttributes['width'] = !empty($plgSettings->enableEmbedResizeWidth) ? esc_attr($plgSettings->enableEmbedResizeWidth) : 600; | |
| 792 | - } | |
| 793 | - if (empty($customAttributes['height'])) { | |
| 794 | - $customAttributes['height'] = !empty($plgSettings->enableEmbedResizeHeight) ? esc_attr($plgSettings->enableEmbedResizeHeight) : 550; | |
| 795 | - } | |
| 796 | - } | |
| 797 | - | |
| 798 | - protected static function get_url_data($url, $attributes = [], $serviceProvider = '') | |
| 799 | - { | |
| 800 | - if (!empty($serviceProvider)) { | |
| 801 | - $urlData = self::get_oembed()->fetch($serviceProvider, $url, $attributes); | |
| 802 | - } else { | |
| 803 | - if (self::is_problematic_provider($url)) { | |
| 804 | - // Force fake response for problematic providers | |
| 805 | - $urlData = self::get_fake_response_for_url($url); | |
| 806 | - } else { | |
| 807 | - $urlData = self::get_embera_instance()->getUrlData($url); | |
| 808 | - } | |
| 809 | - } | |
| 810 | - | |
| 811 | - return $urlData; | |
| 812 | - } | |
| 813 | - | |
| 814 | - // self::$attributes_data = self::$ombed_attributes; | |
| 815 | - | |
| 816 | - public static function getAttributesData() | |
| 817 | - { | |
| 818 | - self::$attributes_data = self::get_oembed_attributes(); | |
| 819 | - // return self::get_oembed_attributes(); | |
| 820 | - return self::$attributes_data; | |
| 821 | - } | |
| 822 | - | |
| 823 | - | |
| 824 | 350 | /** |
| 825 | - * @return null|Embera | |
| 826 | - */ | |
| 827 | - public static function get_embera_instance() | |
| 828 | - { | |
| 829 | - if (!self::$embera_instance) { | |
| 830 | - $additionalServiceProviders = Core::getAdditionalServiceProviders(); | |
| 831 | - if (!empty($additionalServiceProviders)) { | |
| 832 | - foreach ($additionalServiceProviders as $serviceProviderClassName => $serviceProviderUrls) { | |
| 833 | - self::addServiceProvider($serviceProviderClassName, $serviceProviderUrls); | |
| 834 | - } | |
| 835 | - unset($serviceProviderUrls, $serviceProviderClassName); | |
| 836 | - } | |
| 837 | - self::$embera_instance = new Embera(self::get_embera_settings(), self::get_collection()); | |
| 838 | - } else { | |
| 839 | - // Re-instantiate Embera with new settings since setConfig does not exist | |
| 840 | - self::$embera_instance = new Embera(self::get_embera_settings(), self::get_collection()); | |
| 841 | - } | |
| 842 | - return self::$embera_instance; | |
| 843 | - } | |
| 844 | - | |
| 845 | - /** | |
| 846 | 351 | * Method that adds support to a given new service provider (SP). |
| 847 | 352 | * |
| 848 | - * @param string $className The new SP class name. | |
| 849 | - * @param string $reference The new SP reference name.* | |
| 850 | - * @return DefaultProviderCollection|bool | |
| 851 | 353 | * @since 1.0.0 |
| 852 | 354 | * @static |
| 853 | 355 | * |
| 356 | + * @param string $className The new SP class name. | |
| 357 | + * @param string $reference The new SP reference name. | |
| 358 | + * @param \Embera\Embera $emberaInstance The embera's instance where the SP will be registered in. | |
| 359 | + * | |
| 360 | + * @return boolean | |
| 854 | 361 | */ |
| 855 | - public static function addServiceProvider($className, $reference) | |
| 362 | + public static function addServiceProvider($className, $reference, &$emberaInstance) | |
| 856 | 363 | { |
| 857 | 364 | if (empty($className) || empty($reference)) { |
| 858 | 365 | return false; |
| 859 | 366 | } |
| 860 | 367 | |
| 861 | - if (is_null(self::$collection)) { | |
| 862 | - self::$collection = new DefaultProviderCollection(); | |
| 863 | - } | |
| 864 | 368 | if (is_string($reference)) { |
| 865 | - self::$collection->addProvider($reference, $className); | |
| 866 | - return self::$collection; | |
| 369 | + $emberaInstance->addProvider($reference, EMBEDPRESS_NAMESPACE . "\\Providers\\{$className}"); | |
| 867 | 370 | } elseif (is_array($reference)) { |
| 868 | 371 | foreach ($reference as $serviceProviderUrl) { |
| 869 | - self::addServiceProvider($className, $serviceProviderUrl); | |
| 372 | + self::addServiceProvider($className, $serviceProviderUrl, $emberaInstance); | |
| 870 | 373 | } |
| 871 | - return self::$collection; | |
| 872 | 374 | } else { |
| 873 | 375 | return false; |
| 874 | 376 | } |
| 875 | 377 | } |
| @@ -876,14 +378,14 @@ | ||
| 876 | 378 | |
| 877 | 379 | /** |
| 878 | 380 | * Method that retrieves all custom parameters from a shortcoded string. |
| 879 | 381 | * |
| 880 | - * @param string $subject The given shortcoded string. | |
| 881 | - * | |
| 882 | - * @return array | |
| 883 | 382 | * @since 1.0.0 |
| 884 | 383 | * @static |
| 885 | 384 | * |
| 385 | + * @param string $subject The given shortcoded string. | |
| 386 | + * | |
| 387 | + * @return array | |
| 886 | 388 | */ |
| 887 | 389 | public static function parseContentAttributesFromString($subject) |
| 888 | 390 | { |
| 889 | 391 | $customAttributes = []; |
| @@ -896,8 +398,9 @@ | ||
| 896 | 398 | $customAttributes[$attrName] = $attrValues[$attrIndex]; |
| 897 | 399 | } |
| 898 | 400 | } |
| 899 | 401 | } |
| 402 | + | |
| 900 | 403 | return $customAttributes; |
| 901 | 404 | } |
| 902 | 405 | |
| 903 | 406 | /** |
| @@ -902,30 +405,28 @@ | ||
| 902 | 405 | |
| 903 | 406 | /** |
| 904 | 407 | * Method that parses and adds the "data-" prefix to the given custom shortcode attributes. |
| 905 | 408 | * |
| 906 | - * @param array $customAttributes The array containing the embed attributes. | |
| 907 | - * @param string $content_uid An optional string specifying a unique ID for the embed | |
| 908 | - * | |
| 909 | - * @return array | |
| 910 | 409 | * @since 1.0.0 |
| 911 | 410 | * @access private |
| 912 | 411 | * @static |
| 913 | 412 | * |
| 413 | + * @param array $customAttributes The array containing the embed attributes. | |
| 414 | + * @param string $content_uid An optional string specifying a unique ID for the embed | |
| 415 | + * | |
| 416 | + * @return array | |
| 914 | 417 | */ |
| 915 | - | |
| 916 | 418 | private static function parseContentAttributes(array $customAttributes, $content_uid = null) |
| 917 | 419 | { |
| 918 | - | |
| 919 | 420 | $attributes = [ |
| 920 | - 'class' => ["embedpress-wrapper ose-embedpress-responsive"], | |
| 421 | + 'class' => ["embedpress-wrapper"], | |
| 921 | 422 | ]; |
| 922 | 423 | |
| 923 | - $embedShouldBeResponsive = true; | |
| 424 | + $embedShouldBeResponsive = true; | |
| 924 | 425 | $embedShouldHaveCustomDimensions = false; |
| 925 | - if (!empty($customAttributes)) { | |
| 426 | + if ( ! empty($customAttributes)) { | |
| 926 | 427 | if (isset($customAttributes['class'])) { |
| 927 | - if (!empty($customAttributes['class'])) { | |
| 428 | + if ( ! empty($customAttributes['class'])) { | |
| 928 | 429 | $customAttributes['class'] = explode(' ', $customAttributes['class']); |
| 929 | 430 | |
| 930 | 431 | $attributes['class'] = array_merge($attributes['class'], $customAttributes['class']); |
| 931 | 432 | } |
| @@ -933,46 +434,40 @@ | ||
| 933 | 434 | unset($customAttributes['class']); |
| 934 | 435 | } |
| 935 | 436 | |
| 936 | 437 | if (isset($customAttributes['width'])) { |
| 937 | - if (!empty($customAttributes['width'])) { | |
| 938 | - $attributes['width'] = (int) $customAttributes['width']; | |
| 438 | + if ( ! empty($customAttributes['width'])) { | |
| 439 | + $attributes['width'] = (int)$customAttributes['width']; | |
| 939 | 440 | $embedShouldHaveCustomDimensions = true; |
| 940 | 441 | } |
| 941 | 442 | } |
| 942 | 443 | |
| 943 | 444 | if (isset($customAttributes['height'])) { |
| 944 | - if (!empty($customAttributes['height'])) { | |
| 945 | - $attributes['height'] = (int) $customAttributes['height']; | |
| 445 | + if ( ! empty($customAttributes['height'])) { | |
| 446 | + $attributes['height'] = (int)$customAttributes['height']; | |
| 946 | 447 | $embedShouldHaveCustomDimensions = true; |
| 947 | 448 | } |
| 948 | 449 | } |
| 949 | 450 | |
| 950 | - if (!empty($customAttributes)) { | |
| 451 | + if ( ! empty($customAttributes)) { | |
| 951 | 452 | $attrNameDefaultPrefix = "data-"; |
| 952 | 453 | foreach ($customAttributes as $attrName => $attrValue) { |
| 953 | 454 | if (is_numeric($attrName)) { |
| 954 | - $attrName = $attrValue; | |
| 455 | + $attrName = $attrValue; | |
| 955 | 456 | $attrValue = ""; |
| 956 | 457 | } |
| 957 | 458 | |
| 958 | 459 | $attrName = str_replace($attrNameDefaultPrefix, "", $attrName); |
| 959 | 460 | |
| 960 | - if (is_bool($attrValue)) { | |
| 961 | - $attrValue = $attrValue ? "true" : "false"; | |
| 962 | - } else if (is_numeric($attrValue)) { | |
| 963 | - // Preserve numeric values as strings | |
| 964 | - $attrValue = (string) $attrValue; | |
| 965 | - } else if (!is_string($attrValue) || !strlen($attrValue)) { | |
| 461 | + if ( ! strlen($attrValue)) { | |
| 966 | 462 | if ($attrName[0] === "!") { |
| 967 | 463 | $attrValue = "false"; |
| 968 | - $attrName = substr($attrName, 1); | |
| 464 | + $attrName = substr($attrName, 1); | |
| 969 | 465 | } else { |
| 970 | 466 | $attrValue = "true"; |
| 971 | 467 | } |
| 972 | 468 | } |
| 973 | 469 | |
| 974 | - | |
| 975 | 470 | $attributes[$attrNameDefaultPrefix . $attrName] = $attrValue; |
| 976 | 471 | } |
| 977 | 472 | } |
| 978 | 473 | |
| @@ -979,12 +474,12 @@ | ||
| 979 | 474 | // Check if there's any "responsive" parameter |
| 980 | 475 | $responsiveAttributes = ["responsive", "data-responsive"]; |
| 981 | 476 | foreach ($responsiveAttributes as $responsiveAttr) { |
| 982 | 477 | if (isset($attributes[$responsiveAttr])) { |
| 983 | - if (!strlen($attributes[$responsiveAttr])) { // If the parameter is passed but have no value, it will be true by default | |
| 478 | + if ( ! strlen($attributes[$responsiveAttr])) { // If the parameter is passed but have no value, it will be true by default | |
| 984 | 479 | $embedShouldBeResponsive = true; |
| 985 | 480 | } else { |
| 986 | - $embedShouldBeResponsive = !self::valueIsFalse($attributes[$responsiveAttr]); | |
| 481 | + $embedShouldBeResponsive = ! self::valueIsFalse($attributes[$responsiveAttr]); | |
| 987 | 482 | } |
| 988 | 483 | |
| 989 | 484 | break; |
| 990 | 485 | } |
| @@ -993,13 +488,13 @@ | ||
| 993 | 488 | } |
| 994 | 489 | |
| 995 | 490 | $attributes['class'][] = 'ose-{provider_alias}'; |
| 996 | 491 | |
| 997 | - if (!empty($content_uid)) { | |
| 492 | + if ( ! empty($content_uid)) { | |
| 998 | 493 | $attributes['class'][] = 'ose-uid-' . $content_uid; |
| 999 | 494 | } |
| 1000 | 495 | |
| 1001 | - if ($embedShouldBeResponsive && !$embedShouldHaveCustomDimensions) { | |
| 496 | + if ($embedShouldBeResponsive && ! $embedShouldHaveCustomDimensions) { | |
| 1002 | 497 | $attributes['class'][] = 'responsive'; |
| 1003 | 498 | } else { |
| 1004 | 499 | $attributes['data-responsive'] = "false"; |
| 1005 | 500 | } |
| @@ -1004,77 +499,25 @@ | ||
| 1004 | 499 | $attributes['data-responsive'] = "false"; |
| 1005 | 500 | } |
| 1006 | 501 | |
| 1007 | 502 | $attributes['class'] = implode(' ', array_unique(array_filter($attributes['class']))); |
| 1008 | - if (isset($attributes['width'])) { | |
| 1009 | - $height = esc_attr($attributes['height']); | |
| 1010 | - $width = esc_attr($attributes['width']); | |
| 1011 | 503 | |
| 1012 | - $attributes['style'] = "width:{$width}px;height:{$height}px;"; | |
| 1013 | - } | |
| 1014 | - | |
| 1015 | - | |
| 1016 | - | |
| 1017 | 504 | return $attributes; |
| 1018 | 505 | } |
| 1019 | 506 | |
| 1020 | - protected static function set_embera_settings(&$attributes) | |
| 1021 | - { | |
| 1022 | - | |
| 1023 | - if (isset($attributes['width']) || isset($attributes['height'])) { | |
| 1024 | - if (isset($attributes['width'])) { | |
| 1025 | - self::$emberaInstanceSettings['maxwidth'] = esc_attr($attributes['width']); | |
| 1026 | - self::$emberaInstanceSettings['width'] = esc_attr($attributes['width']); | |
| 1027 | - unset($attributes['width']); | |
| 1028 | - } | |
| 1029 | - | |
| 1030 | - if (isset($attributes['height'])) { | |
| 1031 | - self::$emberaInstanceSettings['maxheight'] = esc_attr($attributes['height']); | |
| 1032 | - self::$emberaInstanceSettings['height'] = esc_attr($attributes['height']); | |
| 1033 | - unset($attributes['height']); | |
| 1034 | - } | |
| 1035 | - } | |
| 1036 | - | |
| 1037 | - // Add Meetup-specific attributes to Embera settings | |
| 1038 | - $meetup_attributes = ['orderby', 'order', 'per_page', 'enable_pagination', 'timezone', 'date_format', 'time_format']; | |
| 1039 | - foreach ($meetup_attributes as $attr) { | |
| 1040 | - if (isset($attributes[$attr])) { | |
| 1041 | - self::$emberaInstanceSettings[$attr] = $attributes[$attr]; | |
| 1042 | - } | |
| 1043 | - } | |
| 1044 | - | |
| 1045 | - foreach ($attributes as $key => $value) { | |
| 1046 | - if (strpos($key, 'data-') === 0) { | |
| 1047 | - $key = str_replace('data-', '', $key); | |
| 1048 | - self::$emberaInstanceSettings[$key] = $value; | |
| 1049 | - } | |
| 1050 | - } | |
| 1051 | - } | |
| 1052 | - | |
| 1053 | - protected static function get_embera_settings() | |
| 1054 | - { | |
| 1055 | - return self::$emberaInstanceSettings; | |
| 1056 | - } | |
| 1057 | - | |
| 1058 | - public static function get_block_controls_data() | |
| 1059 | - { | |
| 1060 | - var_dump(self::$emberaInstanceSettings); | |
| 1061 | - } | |
| 1062 | - | |
| 1063 | 507 | /** |
| 1064 | 508 | * Method that checks if a given value is/can be identified as (bool)false. |
| 1065 | 509 | * |
| 1066 | - * @param mixed $subject The value to be checked. | |
| 1067 | - * | |
| 1068 | - * @return boolean | |
| 1069 | 510 | * @since 1.0.0 |
| 1070 | 511 | * @static |
| 1071 | 512 | * |
| 513 | + * @param mixed $subject The value to be checked. | |
| 514 | + * | |
| 515 | + * @return boolean | |
| 1072 | 516 | */ |
| 1073 | - | |
| 1074 | 517 | public static function valueIsFalse($subject) |
| 1075 | 518 | { |
| 1076 | - $subject = strtolower(trim((string) $subject)); | |
| 519 | + $subject = strtolower(trim((string)$subject)); | |
| 1077 | 520 | switch ($subject) { |
| 1078 | 521 | case "0": |
| 1079 | 522 | case "false": |
| 1080 | 523 | case "off": |
| @@ -1091,16 +534,16 @@ | ||
| 1091 | 534 | /** |
| 1092 | 535 | * Return the value from a header which is in an array resulted from a get_headers() call. |
| 1093 | 536 | * If the header cannot be found, this method will return null instead. |
| 1094 | 537 | * |
| 1095 | - * @param string $headerPattern Regex pattern the header and its value must match. | |
| 1096 | - * @param array $headersList A list of headers resulted from a get_headers() call. | |
| 1097 | - * | |
| 1098 | - * @return mixed | |
| 1099 | 538 | * @since 1.1.0 |
| 1100 | 539 | * @access private |
| 1101 | 540 | * @static |
| 1102 | 541 | * |
| 542 | + * @param string $headerPattern Regex pattern the header and its value must match. | |
| 543 | + * @param array $headersList A list of headers resulted from a get_headers() call. | |
| 544 | + * | |
| 545 | + * @return mixed | |
| 1103 | 546 | */ |
| 1104 | 547 | private static function extractContentFromHeaderAsArray($headerPattern, $headersList) |
| 1105 | 548 | { |
| 1106 | 549 | $headerValue = null; |
| @@ -1119,35 +562,35 @@ | ||
| 1119 | 562 | * Sanitize the object returned by the embed source. Sometimes we need to convert |
| 1120 | 563 | * attributes from "dash" separated to "underline" separated to be able to access |
| 1121 | 564 | * those attributes from the object, without having to convert it to an array. |
| 1122 | 565 | * |
| 1123 | - * @param object $data | |
| 1124 | - * | |
| 1125 | - * @return object | |
| 1126 | 566 | * @since 1.6.1 |
| 1127 | 567 | * @access private |
| 1128 | 568 | * @static |
| 1129 | 569 | * |
| 570 | + * @param object $data | |
| 571 | + * | |
| 572 | + * @return object | |
| 1130 | 573 | */ |
| 1131 | - private static function sanitizeUrlData($data, $url = '') | |
| 574 | + private static function sanitizeUrlData($data) | |
| 1132 | 575 | { |
| 1133 | 576 | if (is_object($data)) { |
| 1134 | 577 | $attributes = get_object_vars($data); |
| 1135 | 578 | |
| 1136 | 579 | foreach ($attributes as $key => $value) { |
| 1137 | - if (substr_count($key, '-') && $key !== $url) { | |
| 580 | + if (substr_count($key, '-')) { | |
| 1138 | 581 | unset($data->$key); |
| 1139 | 582 | |
| 1140 | - $key = str_replace('-', '_', $key); | |
| 583 | + $key = str_replace('-', '_', $key); | |
| 1141 | 584 | $data->$key = $value; |
| 1142 | 585 | } |
| 1143 | 586 | } |
| 1144 | 587 | } elseif (is_array($data)) { |
| 1145 | 588 | foreach ($data as $key => $value) { |
| 1146 | - if (substr_count($key, '-') && $key !== $url) { | |
| 589 | + if (substr_count($key, '-')) { | |
| 1147 | 590 | unset($data[$key]); |
| 1148 | 591 | |
| 1149 | - $key = str_replace('-', '_', $key); | |
| 592 | + $key = str_replace('-', '_', $key); | |
| 1150 | 593 | $data[$key] = $value; |
| 1151 | 594 | } |
| 1152 | 595 | } |
| 1153 | 596 | } |
| @@ -1152,941 +595,6 @@ | ||
| 1152 | 595 | } |
| 1153 | 596 | } |
| 1154 | 597 | |
| 1155 | 598 | return $data; |
| 1156 | - } | |
| 1157 | - | |
| 1158 | - public static function get_collection() | |
| 1159 | - { | |
| 1160 | - return self::$collection; | |
| 1161 | - } | |
| 1162 | - | |
| 1163 | - /** | |
| 1164 | - * Repairs an iframe whose `src` was corrupted by Embera's responsive | |
| 1165 | - * class injection (see ResponsiveEmbeds::addClasses()). | |
| 1166 | - * | |
| 1167 | - * The corruption signature is a `src=` that is NOT immediately followed | |
| 1168 | - * by a quote — instead the responsive-item class tokens leak in: | |
| 1169 | - * src= embera-embed-responsive-item embera-embed-responsive-item-rich"<URL>" | |
| 1170 | - * The real URL is still intact between the stray quote and the next | |
| 1171 | - * quote, so we drop the leaked tokens and restore a proper quoted src. | |
| 1172 | - * | |
| 1173 | - * @param string $html The (possibly corrupted) embed markup. | |
| 1174 | - * @param string $url The original embed URL (kept for signature stability). | |
| 1175 | - * @return string | |
| 1176 | - */ | |
| 1177 | - protected static function repair_responsive_class_corruption($html, $url) | |
| 1178 | - { | |
| 1179 | - // Fast bail: only act on the exact corruption signature so we never | |
| 1180 | - // touch healthy embeds. The injected tokens always land right after | |
| 1181 | - // an unquoted `src=`, i.e. src= embera-embed-responsive-item … | |
| 1182 | - if (strpos($html, 'src= embera-embed-responsive-item') === false) { | |
| 1183 | - return $html; | |
| 1184 | - } | |
| 1185 | - | |
| 1186 | - // The damage is mechanically reversible from the string itself: | |
| 1187 | - // addClasses() consumed the iframe's `src="` opening quote, so the | |
| 1188 | - // markup now reads | |
| 1189 | - // src= embera-embed-responsive-item embera-embed-responsive-item-<type>"<URL>" | |
| 1190 | - // The real URL is still intact between the stray quote and the next | |
| 1191 | - // quote — we just drop the leaked class tokens and restore a proper | |
| 1192 | - // quoted `src`. The iframe keeps its own (empty) class="" attribute, | |
| 1193 | - // and the responsive-item class already lives on the wrapper <div>, | |
| 1194 | - // so nothing else needs re-adding. | |
| 1195 | - return preg_replace( | |
| 1196 | - '~src=\s+embera-embed-responsive-item\s+embera-embed-responsive-item-[a-z0-9_-]+\s*"([^"]+)"~i', | |
| 1197 | - 'src="$1"', | |
| 1198 | - $html, | |
| 1199 | - 1 | |
| 1200 | - ); | |
| 1201 | - } | |
| 1202 | - | |
| 1203 | - protected static function purify_html_content(&$html) | |
| 1204 | - { | |
| 1205 | - if (!class_exists('\simple_html_dom')) { | |
| 1206 | - include_once EMBEDPRESS_PATH_CORE . 'simple_html_dom.php'; | |
| 1207 | - } | |
| 1208 | - | |
| 1209 | - $dom = str_get_html($html); | |
| 1210 | - $ifDom = $dom->find('iframe', 0); | |
| 1211 | - if (!empty($ifDom) && is_object($ifDom)) { | |
| 1212 | - $ifDom->removeAttribute('sandbox'); | |
| 1213 | - } | |
| 1214 | - | |
| 1215 | - ob_start(); | |
| 1216 | - echo $dom; | |
| 1217 | - return ob_get_clean(); | |
| 1218 | - } | |
| 1219 | - | |
| 1220 | - protected static function get_content_from_template($url, $template, $serviceProvider) | |
| 1221 | - { | |
| 1222 | - | |
| 1223 | - $html = apply_filters('embed_apple_podcast', '', $url, $template, $serviceProvider); | |
| 1224 | - | |
| 1225 | - if (!empty($html)) { | |
| 1226 | - return $html; | |
| 1227 | - } | |
| 1228 | - | |
| 1229 | - // YouTube playlist + watch?…&list=… need the Embera path so the | |
| 1230 | - // Youtube provider's getPlaylistGallery() can render the queue UI. | |
| 1231 | - // WP oembed would return a videoseries/single-video iframe and | |
| 1232 | - // short-circuit us. esc_url() encodes & → &, so accept either form. | |
| 1233 | - $is_yt_playlist_url = | |
| 1234 | - preg_match('~^https?://(?:www\.)?youtube\.com/playlist\?(?:[^#]*(?:&|&\#038;))?list=[\w-]+~i', $url) || | |
| 1235 | - preg_match('~^https?://(?:www\.)?youtube\.com/watch\?(?:[^#]*(?:&|&\#038;))?list=[\w-]+~i', $url); | |
| 1236 | - if ($is_yt_playlist_url) { | |
| 1237 | - $serviceProvider = ''; | |
| 1238 | - } | |
| 1239 | - | |
| 1240 | - if (empty($serviceProvider)) { | |
| 1241 | - $html = ''; | |
| 1242 | - } else { | |
| 1243 | - $html = self::get_oembed()->get_html($url, self::get_oembed_attributes()); | |
| 1244 | - } | |
| 1245 | - | |
| 1246 | - if (!$html) { | |
| 1247 | - if (self::is_problematic_provider($url)) { | |
| 1248 | - // Force fake response for problematic providers | |
| 1249 | - $urlData = self::get_fake_response_for_url($url); | |
| 1250 | - if (!empty($urlData[$url]) && !isset($urlData[$url]['error'])) { | |
| 1251 | - $html = $urlData[$url]['html']; | |
| 1252 | - } | |
| 1253 | - } else { | |
| 1254 | - $html = self::get_embera_instance()->autoEmbed($url); | |
| 1255 | - } | |
| 1256 | - | |
| 1257 | - } | |
| 1258 | - | |
| 1259 | - return str_replace('{html}', $html, $template); | |
| 1260 | - } | |
| 1261 | - | |
| 1262 | - /** | |
| 1263 | - * Get list of providers that have known API issues and should use fake responses | |
| 1264 | - * | |
| 1265 | - * @return array | |
| 1266 | - */ | |
| 1267 | - protected static function get_problematic_providers() | |
| 1268 | - { | |
| 1269 | - return apply_filters('embedpress_problematic_providers', [ | |
| 1270 | - 'commaful.com', | |
| 1271 | - 'flourish.studio', | |
| 1272 | - 'dailymotion.com', | |
| 1273 | - 'dai.ly', | |
| 1274 | - ]); | |
| 1275 | - } | |
| 1276 | - | |
| 1277 | - /** | |
| 1278 | - * Check if a URL belongs to a problematic provider | |
| 1279 | - * | |
| 1280 | - * @param string $url | |
| 1281 | - * @return bool | |
| 1282 | - */ | |
| 1283 | - public static function is_problematic_provider($url) | |
| 1284 | - { | |
| 1285 | - $problematic_providers = self::get_problematic_providers(); | |
| 1286 | - | |
| 1287 | - foreach ($problematic_providers as $provider) { | |
| 1288 | - if (strpos($url, $provider) !== false) { | |
| 1289 | - return true; | |
| 1290 | - } | |
| 1291 | - } | |
| 1292 | - | |
| 1293 | - return false; | |
| 1294 | - } | |
| 1295 | - | |
| 1296 | - /** | |
| 1297 | - * Get fake response for problematic providers | |
| 1298 | - * | |
| 1299 | - * @param string $url | |
| 1300 | - * @return array | |
| 1301 | - */ | |
| 1302 | - protected static function get_fake_response_for_url($url) | |
| 1303 | - { | |
| 1304 | - // Initialize Embera instance to ensure collection is available | |
| 1305 | - $embera = self::get_embera_instance(); | |
| 1306 | - $collection = self::get_collection(); | |
| 1307 | - | |
| 1308 | - if ($collection) { | |
| 1309 | - $providers = $collection->findProviders($url); | |
| 1310 | - | |
| 1311 | - if (!empty($providers[$url])) { | |
| 1312 | - $providerClass = get_class($providers[$url]); | |
| 1313 | - // Create new instance with the URL | |
| 1314 | - $provider = new $providerClass($url); | |
| 1315 | - return [$url => $provider->getFakeResponse()]; | |
| 1316 | - } | |
| 1317 | - } | |
| 1318 | - | |
| 1319 | - return [$url => ['error' => 'No provider found for URL']]; | |
| 1320 | - } | |
| 1321 | - | |
| 1322 | - protected static function get_oembed_attributes() | |
| 1323 | - { | |
| 1324 | - | |
| 1325 | - return self::$ombed_attributes; | |
| 1326 | - } | |
| 1327 | - protected static function set_oembed_attributes($atts) | |
| 1328 | - { | |
| 1329 | - self::$ombed_attributes = $atts; | |
| 1330 | - } | |
| 1331 | - | |
| 1332 | - protected static function check_for_google_url(&$url) | |
| 1333 | - { | |
| 1334 | - if (preg_match('/http[s]?:\/\/goo\.gl\/(?:([a-z]+)\/)?[a-z0-9]+\/?$/i', $url, $matches)) { | |
| 1335 | - // Fetch all headers from the short-url so we can know how to handle its original content depending on the service. | |
| 1336 | - $headers = get_headers($url); | |
| 1337 | - | |
| 1338 | - $supportedServicesHeadersPatterns = [ | |
| 1339 | - 'maps' => '/^Location:\s+(http[s]?:\/\/.+)$/i', | |
| 1340 | - ]; | |
| 1341 | - | |
| 1342 | - $service = isset($matches[1]) ? strtolower($matches[1]) : null; | |
| 1343 | - // No specific service was found in the url. | |
| 1344 | - if (empty($service)) { | |
| 1345 | - // Let's try to guess which service the original url belongs to. | |
| 1346 | - foreach ($headers as $header) { | |
| 1347 | - // Check if the short-url reffers to a Google Maps url. | |
| 1348 | - if (preg_match($supportedServicesHeadersPatterns['maps'], $header, $matches)) { | |
| 1349 | - // Replace the shortened url with its original url. | |
| 1350 | - $url = $matches[1]; | |
| 1351 | - break; | |
| 1352 | - } | |
| 1353 | - } | |
| 1354 | - unset($header); | |
| 1355 | - } else { | |
| 1356 | - // Check if the Google service is supported atm. | |
| 1357 | - if (isset($supportedServicesHeadersPatterns[$service])) { | |
| 1358 | - // Tries to extract the url based on its headers. | |
| 1359 | - $originalUrl = self::extractContentFromHeaderAsArray( | |
| 1360 | - $supportedServicesHeadersPatterns[$service], | |
| 1361 | - $headers | |
| 1362 | - ); | |
| 1363 | - // Replace the shortened url with its original url if the specific header was found. | |
| 1364 | - if (!empty($originalUrl)) { | |
| 1365 | - $url = $originalUrl; | |
| 1366 | - } | |
| 1367 | - unset($originalUrl); | |
| 1368 | - } | |
| 1369 | - } | |
| 1370 | - unset($service, $supportedServicesHeadersPatterns, $headers, $matches); | |
| 1371 | - } | |
| 1372 | - } | |
| 1373 | - | |
| 1374 | - protected static function get_provider_name($urlData, $url) | |
| 1375 | - { | |
| 1376 | - $provider_name = ''; | |
| 1377 | - if (isset($urlData->provider_name)) { | |
| 1378 | - $provider_name = str_replace([' ', ','], '-', strtolower($urlData->provider_name)); | |
| 1379 | - } elseif (is_array($urlData) && !empty($urlData)) { | |
| 1380 | - $data = array_shift($urlData); | |
| 1381 | - if (isset($data['provider_name'])) { | |
| 1382 | - $provider_name = str_replace([' ', ','], '-', strtolower($data['provider_name'])); | |
| 1383 | - } | |
| 1384 | - } | |
| 1385 | - return $provider_name; | |
| 1386 | - } | |
| 1387 | - | |
| 1388 | - /** | |
| 1389 | - * Get list of sources that should be excluded from height attribute application | |
| 1390 | - * | |
| 1391 | - * @return array Array of provider names that should not have height applied | |
| 1392 | - * @since 1.0.0 | |
| 1393 | - * @static | |
| 1394 | - */ | |
| 1395 | - protected static function get_excluded_height_sources() | |
| 1396 | - { | |
| 1397 | - // Default excluded sources - you can add more here | |
| 1398 | - $defaultExcluded = [ | |
| 1399 | - 'meetup' // Meetup events should not have fixed height | |
| 1400 | - ]; | |
| 1401 | - | |
| 1402 | - // Allow filtering of excluded sources | |
| 1403 | - $excludedSources = apply_filters('embedpress_excluded_height_sources', $defaultExcluded); | |
| 1404 | - | |
| 1405 | - // Ensure it's always an array | |
| 1406 | - if (!is_array($excludedSources)) { | |
| 1407 | - $excludedSources = []; | |
| 1408 | - } | |
| 1409 | - | |
| 1410 | - return $excludedSources; | |
| 1411 | - } | |
| 1412 | - | |
| 1413 | - protected static function modify_content_for_fb_and_canada($provider_name, &$html) | |
| 1414 | - { | |
| 1415 | - if (!empty($provider_name)) { | |
| 1416 | - // NFB seems to always return their embed code with all HTML entities into their applicable characters string. | |
| 1417 | - $PROVIDER_NAME_IN_CAP = strtoupper($provider_name); | |
| 1418 | - if ($PROVIDER_NAME_IN_CAP === "NATIONAL FILM BOARD OF CANADA") { | |
| 1419 | - $html = html_entity_decode($html); | |
| 1420 | - } elseif ($PROVIDER_NAME_IN_CAP === "FACEBOOK") { | |
| 1421 | - $plgSettings = Core::getSettings(); | |
| 1422 | - | |
| 1423 | - // Check if the user wants to force a certain language into Facebook embeds. | |
| 1424 | - $locale = isset($plgSettings->fbLanguage) && !empty($plgSettings->fbLanguage) ? $plgSettings->fbLanguage : false; | |
| 1425 | - if (!!$locale) { | |
| 1426 | - // Replace the automatically detected language by Facebook's API with the language chosen by the user. | |
| 1427 | - $html = preg_replace( | |
| 1428 | - '/\/[a-z]{2}\_[a-z]{2}\/sdk\.js/i', | |
| 1429 | - "/{$locale}/sdk.js", | |
| 1430 | - $html | |
| 1431 | - ); | |
| 1432 | - } | |
| 1433 | - | |
| 1434 | - // 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. | |
| 1435 | - if (is_admin()) { | |
| 1436 | - $html = preg_replace( | |
| 1437 | - '~data\-adapt\-container\-width=\"(?:true|1)\"~i', | |
| 1438 | - 'data-adapt-container-width="0"', | |
| 1439 | - $html | |
| 1440 | - ); | |
| 1441 | - } | |
| 1442 | - | |
| 1443 | - unset($locale, $plgSettings); | |
| 1444 | - } | |
| 1445 | - } | |
| 1446 | - } | |
| 1447 | - | |
| 1448 | - public static function modify_spotify_content($embed) | |
| 1449 | - { | |
| 1450 | - $should_modify = apply_filters('embedpress_should_modify_spotify', true); | |
| 1451 | - $isSpotify = (isset($embed->provider_name) && strtoupper($embed->provider_name) === 'SPOTIFY') || (isset($embed->url) && isset($embed->{$embed->url}) && isset($embed->{$embed->url}['provider_name']) && strtoupper($embed->{$embed->url}['provider_name']) === 'SPOTIFY'); | |
| 1452 | - if ( | |
| 1453 | - $should_modify && $isSpotify && isset($embed->embed) | |
| 1454 | - && preg_match('/src=\"(.+?)\"/', $embed->embed, $match) | |
| 1455 | - ) { | |
| 1456 | - $options = (array) get_option(EMBEDPRESS_PLG_NAME . ':spotify'); | |
| 1457 | - // Parse the url to retrieve all its info like variables etc. | |
| 1458 | - $url_full = $match[1]; | |
| 1459 | - $modified_url = str_replace('playlist-v2', 'playlist', $url_full); | |
| 1460 | - if (isset($options['theme'])) { | |
| 1461 | - if (strpos($modified_url, '?') !== false) { | |
| 1462 | - $modified_url .= '&theme=' . sanitize_text_field($options['theme']); | |
| 1463 | - } else { | |
| 1464 | - $modified_url .= '?theme=' . sanitize_text_field($options['theme']); | |
| 1465 | - } | |
| 1466 | - } | |
| 1467 | - | |
| 1468 | - // Replaces the old url with the new one. | |
| 1469 | - $embed->embed = str_replace($url_full, $modified_url, $embed->embed); | |
| 1470 | - } | |
| 1471 | - return $embed; | |
| 1472 | - } | |
| 1473 | - | |
| 1474 | - public static function getParamData($attributes) | |
| 1475 | - { | |
| 1476 | - | |
| 1477 | - $urlParamData = array( | |
| 1478 | - 'themeMode' => isset($attributes['theme_mode']) ? esc_attr($attributes['theme_mode']) : 'default', | |
| 1479 | - 'toolbar' => apply_filters('embedpress/is_allow_rander', false) && isset($attributes['toolbar']) ? esc_attr($attributes['toolbar']) : 'true', | |
| 1480 | - 'lazyLoad' => isset($attributes['lazyLoad']) ? esc_attr($attributes['lazyLoad']) : 'false', | |
| 1481 | - 'position' => isset($attributes['toolbar_position']) ? esc_attr($attributes['toolbar_position']) : 'top', | |
| 1482 | - 'presentation' => isset($attributes['presentation']) ? esc_attr($attributes['presentation']) : 'true', | |
| 1483 | - 'download' => apply_filters('embedpress/is_allow_rander', false) && isset($attributes['download']) ? esc_attr($attributes['download']) : 'true', | |
| 1484 | - 'copy_text' => apply_filters('embedpress/is_allow_rander', false) && isset($attributes['copy_text']) ? esc_attr($attributes['copy_text']) : 'true', | |
| 1485 | - 'add_text' => isset($attributes['add_text']) ? esc_attr($attributes['add_text']) : 'true', | |
| 1486 | - 'draw' => apply_filters('embedpress/is_allow_rander', false) && isset($attributes['draw']) ? esc_attr($attributes['draw']) : 'true', | |
| 1487 | - 'doc_rotation' => isset($attributes['doc_rotation']) ? esc_attr($attributes['doc_rotation']) : 'true', | |
| 1488 | - 'add_image' => isset($attributes['add_image']) ? esc_attr($attributes['add_image']) : 'true', | |
| 1489 | - 'doc_details' => isset($attributes['doc_details']) ? esc_attr($attributes['doc_details']) : 'true', | |
| 1490 | - 'selection_tool' => isset($attributes['selection_tool']) ? esc_attr($attributes['selection_tool']) : '0', | |
| 1491 | - 'scrolling' => isset($attributes['scrolling']) ? esc_attr($attributes['scrolling']) : '-1', | |
| 1492 | - 'spreads' => isset($attributes['spreads']) ? esc_attr($attributes['spreads']) : '-1', | |
| 1493 | - 'zoom_in' => isset($attributes['zoom_in']) ? $attributes['zoom_in'] : 'true', | |
| 1494 | - 'zoom_out' => isset($attributes['zoom_out']) ? $attributes['zoom_out'] : 'true', | |
| 1495 | - 'fit_view' => isset($attributes['fit_view']) ? $attributes['fit_view'] : 'true', | |
| 1496 | - 'bookmark' => isset($attributes['bookmark']) ? $attributes['bookmark'] : 'true', | |
| 1497 | - 'sound' => isset($attributes['sound']) ? $attributes['sound'] : 'true', | |
| 1498 | - 'flipbook_toolbar_position' => !empty($attributes['toolbar_position']) ? $attributes['toolbar_position'] : 'bottom', | |
| 1499 | - 'flipbook_rtl' => defined('EMBEDPRESS_SL_ITEM_SLUG') && isset($attributes['flipbook_rtl']) && ($attributes['flipbook_rtl'] === 'true' || $attributes['flipbook_rtl'] === true) ? 'true' : 'false', | |
| 1500 | - 'flipbook_highlight_links' => isset($attributes['flipbook_highlight_links']) && ($attributes['flipbook_highlight_links'] === 'true' || $attributes['flipbook_highlight_links'] === true) ? 'true' : 'false', | |
| 1501 | - 'flipbook_highlight_color' => isset($attributes['flipbook_highlight_color']) ? esc_attr($attributes['flipbook_highlight_color']) : '', | |
| 1502 | - 'pageNumber' => isset($attributes['page_number']) ? absint($attributes['page_number']) : 1, | |
| 1503 | - 'watermark_text' => defined('EMBEDPRESS_SL_ITEM_SLUG') && isset($attributes['watermarkText']) ? esc_attr($attributes['watermarkText']) : '', | |
| 1504 | - 'watermark_font_size' => defined('EMBEDPRESS_SL_ITEM_SLUG') && isset($attributes['watermarkFontSize']) ? esc_attr($attributes['watermarkFontSize']) : '48', | |
| 1505 | - 'watermark_color' => defined('EMBEDPRESS_SL_ITEM_SLUG') && isset($attributes['watermarkColor']) ? esc_attr($attributes['watermarkColor']) : '#000000', | |
| 1506 | - 'watermark_opacity' => defined('EMBEDPRESS_SL_ITEM_SLUG') && isset($attributes['watermarkOpacity']) ? esc_attr($attributes['watermarkOpacity']) : '15', | |
| 1507 | - 'watermark_style' => defined('EMBEDPRESS_SL_ITEM_SLUG') && isset($attributes['watermarkStyle']) ? esc_attr($attributes['watermarkStyle']) : 'center', | |
| 1508 | - ); | |
| 1509 | - | |
| 1510 | - if ($urlParamData['themeMode'] == 'custom') { | |
| 1511 | - $urlParamData['customColor'] = isset($attributes['custom_color']) ? esc_attr($attributes['custom_color']) : '#333333'; | |
| 1512 | - } | |
| 1513 | - | |
| 1514 | - $queryString = http_build_query($urlParamData); | |
| 1515 | - | |
| 1516 | - // Ensure UTF-8 encoding with fallback for when mbstring is not available | |
| 1517 | - if (function_exists('mb_convert_encoding')) { | |
| 1518 | - $queryString = \mb_convert_encoding($queryString, 'UTF-8'); | |
| 1519 | - } | |
| 1520 | - // If neither mbstring nor iconv is available, use the string as-is | |
| 1521 | - // since http_build_query() already produces valid output | |
| 1522 | - | |
| 1523 | - if (isset($attributes['viewer_style']) && $attributes['viewer_style'] == 'flip-book') { | |
| 1524 | - return "&key=" . base64_encode($queryString); | |
| 1525 | - } | |
| 1526 | - | |
| 1527 | - return "#key=" . base64_encode($queryString); | |
| 1528 | - } | |
| 1529 | - | |
| 1530 | - public static function getUnit($value) | |
| 1531 | - { | |
| 1532 | - if (preg_match('/[a-zA-Z%]+$/', $value, $matches)) { | |
| 1533 | - return ''; | |
| 1534 | - } | |
| 1535 | - return 'px'; | |
| 1536 | - } | |
| 1537 | - | |
| 1538 | - /** | |
| 1539 | - * Resolve a URL from an ACF field. | |
| 1540 | - * | |
| 1541 | - * Handles ACF "File URL" return format (string) and | |
| 1542 | - * "File Array" return format (array with 'url' key). | |
| 1543 | - * | |
| 1544 | - * @param string $field_name The ACF field name. | |
| 1545 | - * @param int|null $post_id Optional post ID. Defaults to current post. | |
| 1546 | - * @return string The resolved URL, or empty string. | |
| 1547 | - */ | |
| 1548 | - private static function resolveAcfFieldUrl($field_name, $post_id = null) | |
| 1549 | - { | |
| 1550 | - if (!function_exists('get_field')) { | |
| 1551 | - return ''; | |
| 1552 | - } | |
| 1553 | - | |
| 1554 | - $field_name = sanitize_key($field_name); | |
| 1555 | - if (empty($field_name)) { | |
| 1556 | - return ''; | |
| 1557 | - } | |
| 1558 | - | |
| 1559 | - $post_id = $post_id ?: get_the_ID(); | |
| 1560 | - $field_value = get_field($field_name, $post_id); | |
| 1561 | - | |
| 1562 | - if (empty($field_value)) { | |
| 1563 | - return ''; | |
| 1564 | - } | |
| 1565 | - | |
| 1566 | - // ACF "File URL" return format | |
| 1567 | - if (is_string($field_value)) { | |
| 1568 | - return trim($field_value); | |
| 1569 | - } | |
| 1570 | - | |
| 1571 | - // ACF "File Array" return format | |
| 1572 | - if (is_array($field_value) && !empty($field_value['url'])) { | |
| 1573 | - return trim($field_value['url']); | |
| 1574 | - } | |
| 1575 | - | |
| 1576 | - return ''; | |
| 1577 | - } | |
| 1578 | - | |
| 1579 | - public static function do_shortcode_pdf($attributes = [], $subject = null) | |
| 1580 | - { | |
| 1581 | - $plgSettings = Core::getSettings(); | |
| 1582 | - | |
| 1583 | - $default = [ | |
| 1584 | - 'width' => esc_attr($plgSettings->enableEmbedResizeWidth), | |
| 1585 | - 'height' => esc_attr($plgSettings->enableEmbedResizeHeight), | |
| 1586 | - 'powered_by' => !empty($plgSettings->embedpress_document_powered_by) ? esc_attr($plgSettings->embedpress_document_powered_by) : esc_attr('no'), | |
| 1587 | - 'display_mode' => 'inline', | |
| 1588 | - 'trigger_text' => 'View PDF', | |
| 1589 | - ]; | |
| 1590 | - | |
| 1591 | - if (!empty($plgSettings->pdf_custom_color_settings)) { | |
| 1592 | - $default['theme_mode'] = 'custom'; | |
| 1593 | - } | |
| 1594 | - if (isset($default['theme_mode']) && $default['theme_mode'] == 'custom') { | |
| 1595 | - $default['custom_color'] = esc_attr($plgSettings->custom_color); | |
| 1596 | - } | |
| 1597 | - | |
| 1598 | - $attributes = wp_parse_args($attributes, $default); | |
| 1599 | - | |
| 1600 | - $url = preg_replace( | |
| 1601 | - '/(\[' . EMBEDPRESS_SHORTCODE . '(?:\]|.+?\])|\[\/' . EMBEDPRESS_SHORTCODE . '\])/i', | |
| 1602 | - "", | |
| 1603 | - $subject | |
| 1604 | - ); | |
| 1605 | - | |
| 1606 | - $url = trim($url); | |
| 1607 | - | |
| 1608 | - // ACF dynamic field support: [embedpress_pdf]embedpress_my_field[/embedpress_pdf] | |
| 1609 | - if (!empty($url) && strpos($url, 'embedpress_') === 0) { | |
| 1610 | - $field_name = substr($url, strlen('embedpress_')); | |
| 1611 | - $url = self::resolveAcfFieldUrl($field_name); | |
| 1612 | - } | |
| 1613 | - | |
| 1614 | - $url = esc_url($url); | |
| 1615 | - | |
| 1616 | - ob_start(); | |
| 1617 | - | |
| 1618 | - $id = 'embedpress-pdf-shortcode'; | |
| 1619 | - | |
| 1620 | - $widthUnit = self::getUnit($attributes['width']); | |
| 1621 | - $heightUnit = self::getUnit($attributes['height']); | |
| 1622 | - | |
| 1623 | - $dimension = "width: {$attributes['width']}{$widthUnit}; height: {$attributes['height']}{$heightUnit};"; | |
| 1624 | - | |
| 1625 | - | |
| 1626 | - $client_id = is_object('$embed') ? md5('$embed->embed') : ''; | |
| 1627 | - | |
| 1628 | - $hash_pass = isset($attributes['protection_password']) | |
| 1629 | - ? hash('sha256', wp_salt(32) . md5($attributes['protection_password'])) | |
| 1630 | - : ''; | |
| 1631 | - | |
| 1632 | - $pass_hash_key = isset($attributes['protection_password']) | |
| 1633 | - ? md5($attributes['protection_password']) | |
| 1634 | - : ''; | |
| 1635 | - | |
| 1636 | - $password_correct = $_COOKIE['password_correct_' . $client_id] ?? ''; | |
| 1637 | - | |
| 1638 | - $protection_type = $attributes['protection_type'] ?? 'user-role'; | |
| 1639 | - $protection_password = $attributes['protection_password'] ?? ''; | |
| 1640 | - $protection_content = isset($attributes['protection_content']) ? $attributes['protection_content'] === 'true' : false; | |
| 1641 | - $user_role = isset($attributes['user_roles']) ? explode(',', preg_replace('/\s*,\s*/', ',', $attributes['user_roles'])) : ''; | |
| 1642 | - | |
| 1643 | - $protection_message = $attributes['protection_message'] ?? ''; | |
| 1644 | - | |
| 1645 | - // Conditions for content protection | |
| 1646 | - $password_protected = $protection_type == 'password' && !empty($protection_password); | |
| 1647 | - | |
| 1648 | - $password_verified = $password_protected && !empty(Helper::is_password_correct($client_id)) && ($hash_pass === $password_correct); | |
| 1649 | - $user_role_protected = $protection_type === 'user-role' && Helper::has_allowed_roles($user_role); | |
| 1650 | - | |
| 1651 | - if ( | |
| 1652 | - apply_filters('embedpress/is_allow_rander', false) && ($protection_content == 'true') && (($protection_type == 'user-role' && !Helper::has_allowed_roles($user_role))) | |
| 1653 | - ) { | |
| 1654 | - | |
| 1655 | - return self::content_protection_content($client_id, $protection_message, $user_role); | |
| 1656 | - } | |
| 1657 | - | |
| 1658 | - // Lightbox mode: render thumbnail + lightbox instead of inline viewer | |
| 1659 | - $display_mode = isset($attributes['display_mode']) ? $attributes['display_mode'] : 'inline'; | |
| 1660 | - if (in_array($display_mode, ['button', 'link', 'text']) && self::is_pdf($url) && !self::is_external_url($url)) { | |
| 1661 | - $viewer_style = isset($attributes['viewer_style']) ? $attributes['viewer_style'] : 'modern'; | |
| 1662 | - $param_string = self::getParamData($attributes); | |
| 1663 | - $viewer_params = ''; | |
| 1664 | - if (preg_match('/key=(.+)$/', $param_string, $matches)) { | |
| 1665 | - $viewer_params = $matches[1]; | |
| 1666 | - } | |
| 1667 | - $trigger_text = isset($attributes['trigger_text']) ? $attributes['trigger_text'] : 'View PDF'; | |
| 1668 | - ?> | |
| 1669 | - <div class="embedpress-document-embed ep-doc-<?php echo esc_attr(md5($id)); ?>"> | |
| 1670 | - <div class="ep-pdf-thumbnail-wrap" | |
| 1671 | - data-pdf-url="<?php echo esc_url($url); ?>" | |
| 1672 | - data-viewer-style="<?php echo esc_attr($viewer_style); ?>" | |
| 1673 | - data-viewer-params="<?php echo esc_attr($viewer_params); ?>"> | |
| 1674 | - <span class="ep-pdf-trigger ep-pdf-trigger--<?php echo esc_attr($display_mode); ?>"><?php echo esc_html($trigger_text); ?></span> | |
| 1675 | - </div> | |
| 1676 | - </div> | |
| 1677 | - <?php | |
| 1678 | - return ob_get_clean(); | |
| 1679 | - } | |
| 1680 | - if ($display_mode === 'lightbox' && self::is_pdf($url) && !self::is_external_url($url)) { | |
| 1681 | - $viewer_style = isset($attributes['viewer_style']) ? $attributes['viewer_style'] : 'modern'; | |
| 1682 | - $param_string = self::getParamData($attributes); | |
| 1683 | - $viewer_params = ''; | |
| 1684 | - if (preg_match('/key=(.+)$/', $param_string, $matches)) { | |
| 1685 | - $viewer_params = $matches[1]; | |
| 1686 | - } | |
| 1687 | - $max_width = $attributes['width'] . self::getUnit($attributes['width']); | |
| 1688 | - if (empty(self::getUnit($attributes['width']))) { | |
| 1689 | - $max_width .= 'px'; | |
| 1690 | - } | |
| 1691 | - $pdf_title = Helper::get_file_title($url); | |
| 1692 | - // Server-resolved poster: avoids making every visitor download the | |
| 1693 | - // whole PDF just to render a preview canvas (FB #84167). | |
| 1694 | - $pdf_poster = Helper::get_pdf_poster_url($url); | |
| 1695 | - ?> | |
| 1696 | - <div class="embedpress-document-embed ep-doc-<?php echo esc_attr(md5($id)); ?>" style="max-width: <?php echo esc_attr($max_width); ?>;"> | |
| 1697 | - <div class="ep-pdf-thumbnail-card"> | |
| 1698 | - <div class="ep-pdf-thumbnail-wrap" | |
| 1699 | - data-pdf-url="<?php echo esc_url($url); ?>" | |
| 1700 | - data-viewer-style="<?php echo esc_attr($viewer_style); ?>" | |
| 1701 | - data-viewer-params="<?php echo esc_attr($viewer_params); ?>"> | |
| 1702 | - <div class="ep-pdf-thumbnail-inner"> | |
| 1703 | - <?php if (!empty($pdf_poster)): ?> | |
| 1704 | - <img class="ep-pdf-thumbnail-custom" src="<?php echo esc_url($pdf_poster); ?>" alt="<?php echo esc_attr($pdf_title); ?>" loading="lazy" /> | |
| 1705 | - <?php else: ?> | |
| 1706 | - <canvas class="ep-pdf-thumbnail-canvas" data-pdf-url="<?php echo esc_url($url); ?>" data-loading="true"></canvas> | |
| 1707 | - <?php endif; ?> | |
| 1708 | - <div class="ep-pdf-thumbnail-overlay"> | |
| 1709 | - <div class="ep-pdf-thumbnail-icon-circle"> | |
| 1710 | - <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8 5v14l11-7z"/></svg> | |
| 1711 | - </div> | |
| 1712 | - </div> | |
| 1713 | - </div> | |
| 1714 | - </div> | |
| 1715 | - | |
| 1716 | - </div> | |
| 1717 | - <?php | |
| 1718 | - if (!empty($attributes['powered_by']) && $attributes['powered_by'] === 'yes') { | |
| 1719 | - printf('<p class="embedpress-el-powered">%s</p>', __('Powered By EmbedPress', 'embedpress')); | |
| 1720 | - } | |
| 1721 | - ?> | |
| 1722 | - </div> | |
| 1723 | - <?php | |
| 1724 | - return ob_get_clean(); | |
| 1725 | - } | |
| 1726 | - | |
| 1727 | -?> | |
| 1728 | - <div class="embedpress-document-embed ose-document <?php echo 'ep-doc-' . md5($id); ?>" style="<?php echo esc_attr($dimension); ?>; max-width:100%; display: block"> | |
| 1729 | - <?php if ($url != '') { | |
| 1730 | - if (self::is_pdf($url) && !self::is_external_url($url)) { | |
| 1731 | - $renderer = Helper::get_pdf_renderer(); | |
| 1732 | - | |
| 1733 | - $src = $renderer . ((strpos($renderer, '?') === false) ? '?' : '&') . 'file=' . urlencode($url) . self::getParamData($attributes); | |
| 1734 | - | |
| 1735 | - | |
| 1736 | - if (isset($attributes['viewer_style']) && $attributes['viewer_style'] === 'flip-book') { | |
| 1737 | - $src = urlencode($url) . self::getParamData($attributes); // Assuming generate_pdf_params is meant to be getParamData | |
| 1738 | - $renderer = Helper::get_flipbook_renderer(); | |
| 1739 | - $src_url = $renderer . ((strpos($renderer, '?') === false) ? '?' : '&') . 'file=' . $src; | |
| 1740 | - echo '<iframe title="' . esc_attr(Helper::get_file_title($url)) . '" class="embedpress-embed-document-pdf ' . esc_attr($id) . '" style="' . esc_attr($dimension) . '; max-width:100%; display: inline-block" src="' . esc_url($src_url) . '" frameborder="0" oncontextmenu="return false;"></iframe>'; | |
| 1741 | - } else { | |
| 1742 | - ?> | |
| 1743 | - <iframe title="<?php echo esc_attr(Helper::get_file_title($url)); ?>" allowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true" style="<?php echo esc_attr($dimension); ?>; max-width:100%; display: inline-block" data-emsrc="<?php echo esc_url($url); ?>" data-emid="<?php echo esc_attr($id); ?>" class="embedpress-embed-document-pdf <?php echo esc_attr($id); ?>" src="<?php echo esc_url($src); ?>" frameborder="0"> | |
| 1744 | - </iframe> | |
| 1745 | - <?php | |
| 1746 | - } | |
| 1747 | - } else { | |
| 1748 | - ?> | |
| 1749 | - <div> | |
| 1750 | - <iframe title="<?php echo esc_attr(Helper::get_file_title($url)); ?>" allowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true" style="<?php echo esc_attr($dimension); ?>; max-width:100%;" src="<?php echo esc_url($url); ?>" data-emsrc="<?php echo esc_url($url); ?>" data-emid="<?php echo esc_attr($id); ?>" class="embedpress-embed-document-pdf <?php echo esc_attr($id); ?>"></iframe> | |
| 1751 | - </div> | |
| 1752 | - <?php | |
| 1753 | - | |
| 1754 | - } | |
| 1755 | - | |
| 1756 | - if (!empty($attributes['powered_by']) && $attributes['powered_by'] === 'yes') { | |
| 1757 | - printf('<p class="embedpress-el-powered">%s</p>', __('Powered By EmbedPress', 'embedpress')); | |
| 1758 | - } | |
| 1759 | - } | |
| 1760 | - ?> | |
| 1761 | - </div> | |
| 1762 | - | |
| 1763 | -<?php | |
| 1764 | - | |
| 1765 | - | |
| 1766 | - return ob_get_clean(); | |
| 1767 | - } | |
| 1768 | - | |
| 1769 | - | |
| 1770 | - public static function do_shortcode_doc($attributes = [], $subject = null) | |
| 1771 | - { | |
| 1772 | - $plgSettings = Core::getSettings(); | |
| 1773 | - $url = preg_replace( | |
| 1774 | - '/(\[' . EMBEDPRESS_SHORTCODE . '(?:\]|.+?\])|\[\/' . EMBEDPRESS_SHORTCODE . '\])/i', | |
| 1775 | - "", | |
| 1776 | - $subject | |
| 1777 | - ); | |
| 1778 | - | |
| 1779 | - $url = trim($url); | |
| 1780 | - | |
| 1781 | - // ACF dynamic field support: [embedpress_doc]embedpress_my_field[/embedpress_doc] | |
| 1782 | - if (!empty($url) && strpos($url, 'embedpress_') === 0) { | |
| 1783 | - $field_name = substr($url, strlen('embedpress_')); | |
| 1784 | - $resolved = self::resolveAcfFieldUrl($field_name); | |
| 1785 | - if (!empty($resolved)) { | |
| 1786 | - $url = $resolved; | |
| 1787 | - } | |
| 1788 | - } | |
| 1789 | - | |
| 1790 | - $url = esc_url($url); | |
| 1791 | - | |
| 1792 | - | |
| 1793 | - $default = [ | |
| 1794 | - 'url' => $url, | |
| 1795 | - 'width' => !empty($plgSettings->enableEmbedResizeWidth) ? esc_attr($plgSettings->enableEmbedResizeWidth) : '100%', | |
| 1796 | - 'height' => !empty($plgSettings->enableEmbedResizeHeight) ? esc_attr($plgSettings->enableEmbedResizeHeight) : '500px', | |
| 1797 | - 'viewer' => !empty($plgSettings->embedpress_document_viewer) ? esc_attr($plgSettings->embedpress_document_viewer) : 'custom', | |
| 1798 | - 'powered_by' => (!isset($plgSettings->embedpress_document_powered_by) || $plgSettings->embedpress_document_powered_by === 'yes') ? 'yes' : 'no', | |
| 1799 | - ]; | |
| 1800 | - | |
| 1801 | - | |
| 1802 | - $atts = shortcode_atts($default, $attributes); | |
| 1803 | - | |
| 1804 | - | |
| 1805 | - $url = esc_url($atts['url']); | |
| 1806 | - if (empty($url)) return ''; | |
| 1807 | - | |
| 1808 | - $dimension = "width: {$atts['width']}px; height: {$atts['height']}px"; | |
| 1809 | - | |
| 1810 | - $embed_content = ''; | |
| 1811 | - | |
| 1812 | - // PDF Handling | |
| 1813 | - if (self::is_pdf($url)) { | |
| 1814 | - $embed_content .= '<div class="embedpress-document-embed ose-document embedpress-doc-wrap ep-doc-' . md5($url) . '" style="' . esc_attr($dimension) . '; max-width: 100%; display: block">'; | |
| 1815 | - $embed_content .= '<iframe src="' . esc_url($url) . '" style="' . esc_attr($dimension) . '; max-width: 100%;" frameborder="0" allowfullscreen></iframe>'; | |
| 1816 | - if ($atts['powered_by'] === 'yes') { | |
| 1817 | - $embed_content .= '<p class="embedpress-el-powered" style="text-align: center">Powered By EmbedPress</p>'; | |
| 1818 | - } | |
| 1819 | - $embed_content .= '</div>'; | |
| 1820 | - return $embed_content; | |
| 1821 | - } | |
| 1822 | - | |
| 1823 | - // Office or Google Viewer Handling | |
| 1824 | - if (self::is_file_url($url)) { | |
| 1825 | - $viewer_url = 'https://view.officeapps.live.com/op/embed.aspx?src=' . urlencode($url) . '&embedded=true'; | |
| 1826 | - } else { | |
| 1827 | - $viewer_url = 'https://drive.google.com/viewerng/viewer?url=' . urlencode($url) . '&embedded=true&chrome=false'; | |
| 1828 | - } | |
| 1829 | - | |
| 1830 | - if ($atts['viewer'] === 'google') { | |
| 1831 | - $viewer_url = '//docs.google.com/gview?embedded=true&url=' . urlencode($url); | |
| 1832 | - } elseif ($atts['viewer'] === 'custom') { | |
| 1833 | - $hostname = parse_url($url, PHP_URL_HOST); | |
| 1834 | - $domain = implode('.', array_slice(explode('.', $hostname), -2)); | |
| 1835 | - if ($domain === 'google.com') { | |
| 1836 | - $viewer_url = $url . '?embedded=true'; | |
| 1837 | - if (strpos($viewer_url, '/presentation/')) { | |
| 1838 | - $viewer_url = Helper::get_google_presentation_url($url); | |
| 1839 | - } | |
| 1840 | - } | |
| 1841 | - } | |
| 1842 | - | |
| 1843 | - // Determine embed type from URL | |
| 1844 | - $embed_type = Helper::get_provider_name($url) ?: 'document'; | |
| 1845 | - | |
| 1846 | - $embed_content .= '<div class="embedpress-document-embed ose-document embedpress-doc-wrap ep-doc-' . md5($url) . '" style="' . esc_attr($dimension) . '; max-width: 100%; display: block" data-embed-type="' . esc_attr($embed_type) . '">'; | |
| 1847 | - | |
| 1848 | - $embed_content .= '<iframe src="' . esc_url($viewer_url) . '" style="' . esc_attr($dimension) . '; max-width: 100%;" frameborder="0" allowfullscreen ></iframe>'; | |
| 1849 | - | |
| 1850 | - if ($atts['powered_by'] === 'yes') { | |
| 1851 | - $embed_content .= '<p class="embedpress-el-powered" style="text-align: center">Powered By EmbedPress</p>'; | |
| 1852 | - } | |
| 1853 | - | |
| 1854 | - $embed_content .= '</div>'; | |
| 1855 | - | |
| 1856 | - return $embed_content; | |
| 1857 | - } | |
| 1858 | - | |
| 1859 | - protected static function is_file_url($url) | |
| 1860 | - { | |
| 1861 | - $pattern = '/\.([0-9a-z]+)(?=[?#])|(\.)(?:[\w]+)$/i'; | |
| 1862 | - return preg_match($pattern, $url) === 1; | |
| 1863 | - } | |
| 1864 | - | |
| 1865 | - protected static function is_external_url($url) | |
| 1866 | - { | |
| 1867 | - return strpos($url, get_site_url()) === false; | |
| 1868 | - } | |
| 1869 | - | |
| 1870 | - protected static function is_pdf($url) | |
| 1871 | - { | |
| 1872 | - $arr = explode('.', $url); | |
| 1873 | - return end($arr) === 'pdf'; | |
| 1874 | - } | |
| 1875 | - | |
| 1876 | - /** | |
| 1877 | - * PDF Gallery shortcode handler | |
| 1878 | - * | |
| 1879 | - * Usage: [embedpress_pdf_gallery urls="url1.pdf,url2.pdf" thumbnails="thumb1.jpg,,thumb3.jpg" | |
| 1880 | - * layout="grid" columns="3" gap="20" viewer_style="modern" ...] | |
| 1881 | - */ | |
| 1882 | - public static function do_shortcode_pdf_gallery($attributes = [], $subject = null) | |
| 1883 | - { | |
| 1884 | - $defaults = [ | |
| 1885 | - 'urls' => '', | |
| 1886 | - 'thumbnails' => '', | |
| 1887 | - 'layout' => 'grid', | |
| 1888 | - 'columns' => '3', | |
| 1889 | - 'columns_tablet' => '2', | |
| 1890 | - 'columns_mobile' => '1', | |
| 1891 | - 'gap' => '20', | |
| 1892 | - 'aspect_ratio' => '4:3', | |
| 1893 | - 'border_radius' => '8', | |
| 1894 | - 'viewer_style' => 'modern', | |
| 1895 | - 'theme_mode' => 'default', | |
| 1896 | - 'custom_color' => '#403A81', | |
| 1897 | - 'toolbar' => 'true', | |
| 1898 | - 'toolbar_position' => 'top', | |
| 1899 | - 'flipbook_toolbar_position' => 'bottom', | |
| 1900 | - 'presentation' => 'true', | |
| 1901 | - 'download' => 'true', | |
| 1902 | - 'copy_text' => 'true', | |
| 1903 | - 'draw' => 'true', | |
| 1904 | - 'add_text' => 'true', | |
| 1905 | - 'add_image' => 'true', | |
| 1906 | - 'doc_rotation' => 'true', | |
| 1907 | - 'doc_details' => 'true', | |
| 1908 | - 'zoom_in' => 'true', | |
| 1909 | - 'zoom_out' => 'true', | |
| 1910 | - 'fit_view' => 'true', | |
| 1911 | - 'bookmark' => 'true', | |
| 1912 | - 'flipbook_rtl' => 'false', | |
| 1913 | - 'flipbook_highlight_links' => 'false', | |
| 1914 | - 'flipbook_highlight_color' => '', | |
| 1915 | - 'watermark_text' => '', | |
| 1916 | - 'watermark_font_size' => '48', | |
| 1917 | - 'watermark_color' => '#000000', | |
| 1918 | - 'watermark_opacity' => '15', | |
| 1919 | - 'watermark_style' => 'center', | |
| 1920 | - 'carousel_autoplay' => 'false', | |
| 1921 | - 'carousel_speed' => '3000', | |
| 1922 | - 'carousel_loop' => 'true', | |
| 1923 | - 'carousel_arrows' => 'true', | |
| 1924 | - 'carousel_dots' => 'false', | |
| 1925 | - 'slides_per_view' => '3', | |
| 1926 | - ]; | |
| 1927 | - | |
| 1928 | - $attributes = shortcode_atts($defaults, $attributes, 'embedpress_pdf_gallery'); | |
| 1929 | - | |
| 1930 | - $urls = array_filter(array_map('trim', explode(',', $attributes['urls']))); | |
| 1931 | - if (empty($urls)) { | |
| 1932 | - return ''; | |
| 1933 | - } | |
| 1934 | - | |
| 1935 | - $thumbnails = array_map('trim', explode(',', $attributes['thumbnails'])); | |
| 1936 | - | |
| 1937 | - // Build items array | |
| 1938 | - $items = []; | |
| 1939 | - foreach ($urls as $index => $url) { | |
| 1940 | - $items[] = [ | |
| 1941 | - 'url' => esc_url($url), | |
| 1942 | - 'fileName' => basename(parse_url($url, PHP_URL_PATH)), | |
| 1943 | - 'customThumbnailUrl' => !empty($thumbnails[$index]) ? esc_url($thumbnails[$index]) : '', | |
| 1944 | - ]; | |
| 1945 | - } | |
| 1946 | - | |
| 1947 | - // Build viewer params | |
| 1948 | - $viewer_params = self::getGalleryParamData($attributes); | |
| 1949 | - | |
| 1950 | - $layout = esc_attr($attributes['layout']); | |
| 1951 | - | |
| 1952 | - // Pro gate: bookshelf requires Pro | |
| 1953 | - if ($layout === 'bookshelf' && !defined('EMBEDPRESS_SL_ITEM_SLUG')) { | |
| 1954 | - $layout = 'grid'; | |
| 1955 | - } | |
| 1956 | - | |
| 1957 | - $columns = intval($attributes['columns']); | |
| 1958 | - $columns_tablet = intval($attributes['columns_tablet']); | |
| 1959 | - $columns_mobile = intval($attributes['columns_mobile']); | |
| 1960 | - $gap = intval($attributes['gap']); | |
| 1961 | - $border_radius = intval($attributes['border_radius']); | |
| 1962 | - $aspect_ratio = esc_attr($attributes['aspect_ratio']); | |
| 1963 | - $viewer_style = esc_attr($attributes['viewer_style']); | |
| 1964 | - $gallery_id = 'ep-gallery-' . substr(md5($attributes['urls']), 0, 8); | |
| 1965 | - | |
| 1966 | - $carousel_options = ''; | |
| 1967 | - if ($layout === 'carousel' || $layout === 'bookshelf') { | |
| 1968 | - $carousel_options = wp_json_encode([ | |
| 1969 | - 'autoplay' => $attributes['carousel_autoplay'] === 'true', | |
| 1970 | - 'autoplaySpeed' => intval($attributes['carousel_speed']), | |
| 1971 | - 'loop' => $attributes['carousel_loop'] !== 'false', | |
| 1972 | - 'arrows' => $attributes['carousel_arrows'] !== 'false', | |
| 1973 | - 'dots' => $attributes['carousel_dots'] === 'true', | |
| 1974 | - 'slidesPerView' => intval($attributes['slides_per_view']), | |
| 1975 | - ]); | |
| 1976 | - } | |
| 1977 | - | |
| 1978 | - $style = sprintf( | |
| 1979 | - '--ep-gallery-columns:%d;--ep-gallery-columns-tablet:%d;--ep-gallery-columns-mobile:%d;--ep-gallery-gap:%dpx;--ep-gallery-radius:%dpx;', | |
| 1980 | - $columns, $columns_tablet, $columns_mobile, $gap, $border_radius | |
| 1981 | - ); | |
| 1982 | - | |
| 1983 | - ob_start(); | |
| 1984 | - ?> | |
| 1985 | - <div class="ep-pdf-gallery" | |
| 1986 | - data-layout="<?php echo $layout; ?>" | |
| 1987 | - data-columns="<?php echo $columns; ?>" | |
| 1988 | - data-columns-tablet="<?php echo $columns_tablet; ?>" | |
| 1989 | - data-columns-mobile="<?php echo $columns_mobile; ?>" | |
| 1990 | - data-gap="<?php echo $gap; ?>" | |
| 1991 | - data-border-radius="<?php echo $border_radius; ?>" | |
| 1992 | - data-viewer-style="<?php echo $viewer_style; ?>" | |
| 1993 | - data-viewer-params="<?php echo esc_attr($viewer_params); ?>" | |
| 1994 | - data-gallery-id="<?php echo esc_attr($gallery_id); ?>" | |
| 1995 | - <?php if ($carousel_options): ?>data-carousel-options="<?php echo esc_attr($carousel_options); ?>"<?php endif; ?> | |
| 1996 | - style="<?php echo esc_attr($style); ?>"> | |
| 1997 | - | |
| 1998 | - <?php if ($layout === 'carousel' || $layout === 'bookshelf'): ?> | |
| 1999 | - <div class="ep-pdf-gallery__carousel"> | |
| 2000 | - <div class="ep-pdf-gallery__carousel-track"> | |
| 2001 | - <?php else: ?> | |
| 2002 | - <div class="ep-pdf-gallery__grid"> | |
| 2003 | - <?php endif; ?> | |
| 2004 | - | |
| 2005 | - <?php foreach ($items as $index => $item): ?> | |
| 2006 | - <div class="ep-pdf-gallery__item" | |
| 2007 | - data-pdf-url="<?php echo $item['url']; ?>" | |
| 2008 | - data-pdf-index="<?php echo $index; ?>" | |
| 2009 | - data-pdf-name="<?php echo esc_attr($item['fileName']); ?>"> | |
| 2010 | - <div class="ep-pdf-gallery__thumbnail-wrap" data-ratio="<?php echo $aspect_ratio; ?>"> | |
| 2011 | - <?php | |
| 2012 | - $sc_thumb = !empty($item['customThumbnailUrl']) ? $item['customThumbnailUrl'] : (!empty($item['autoThumbnailUrl']) ? $item['autoThumbnailUrl'] : ''); | |
| 2013 | - ?> | |
| 2014 | - <?php if ($sc_thumb): ?> | |
| 2015 | - <img src="<?php echo esc_url($sc_thumb); ?>" alt="<?php echo esc_attr($item['fileName']); ?>" /> | |
| 2016 | - <?php else: ?> | |
| 2017 | - <canvas class="ep-pdf-gallery__canvas" data-pdf-src="<?php echo esc_url($item['url']); ?>" data-loading="true"></canvas> | |
| 2018 | - <?php endif; ?> | |
| 2019 | - <div class="ep-pdf-gallery__overlay"> | |
| 2020 | - <svg class="ep-pdf-gallery__view-icon" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> | |
| 2021 | - <path d="M15 3l2.3 2.3-2.89 2.87 1.42 1.42L18.7 6.7 21 9V3h-6zM3 9l2.3-2.3 2.87 2.89 1.42-1.42L6.7 5.3 9 3H3v6zm6 12l-2.3-2.3 2.89-2.87-1.42-1.42L5.3 17.3 3 15v6h6zm12-6l-2.3 2.3-2.87-2.89-1.42 1.42 2.89 2.87L15 21h6v-6z"/> | |
| 2022 | - </svg> | |
| 2023 | - </div> | |
| 2024 | - </div> | |
| 2025 | - <div class="ep-pdf-gallery__book-title"><?php echo esc_html($item['fileName']); ?></div> | |
| 2026 | - </div> | |
| 2027 | - <?php endforeach; ?> | |
| 2028 | - | |
| 2029 | - <?php if ($layout === 'carousel' || $layout === 'bookshelf'): ?> | |
| 2030 | - </div> | |
| 2031 | - <button class="ep-pdf-gallery__carousel-prev" aria-label="Previous"> | |
| 2032 | - <svg viewBox="0 0 24 24"><path d="M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z"/></svg> | |
| 2033 | - </button> | |
| 2034 | - <button class="ep-pdf-gallery__carousel-next" aria-label="Next"> | |
| 2035 | - <svg viewBox="0 0 24 24"><path d="M8.59 16.59L10 18l6-6-6-6-1.41 1.41L13.17 12z"/></svg> | |
| 2036 | - </button> | |
| 2037 | - <div class="ep-pdf-gallery__carousel-dots"></div> | |
| 2038 | - </div> | |
| 2039 | - <?php else: ?> | |
| 2040 | - </div> | |
| 2041 | - <?php endif; ?> | |
| 2042 | - </div> | |
| 2043 | - <?php | |
| 2044 | - return ob_get_clean(); | |
| 2045 | - } | |
| 2046 | - | |
| 2047 | - /** | |
| 2048 | - * Generate base64-encoded viewer params for PDF gallery shortcode | |
| 2049 | - */ | |
| 2050 | - private static function getGalleryParamData($attributes) | |
| 2051 | - { | |
| 2052 | - $theme_mode = esc_attr($attributes['theme_mode']); | |
| 2053 | - | |
| 2054 | - $params = [ | |
| 2055 | - 'themeMode' => $theme_mode, | |
| 2056 | - 'toolbar' => apply_filters('embedpress/is_allow_rander', false) && isset($attributes['toolbar']) ? esc_attr($attributes['toolbar']) : 'true', | |
| 2057 | - 'position' => esc_attr($attributes['toolbar_position']), | |
| 2058 | - 'flipbook_toolbar_position' => esc_attr($attributes['flipbook_toolbar_position']), | |
| 2059 | - 'presentation' => esc_attr($attributes['presentation']), | |
| 2060 | - 'download' => apply_filters('embedpress/is_allow_rander', false) && isset($attributes['download']) ? esc_attr($attributes['download']) : 'true', | |
| 2061 | - 'copy_text' => apply_filters('embedpress/is_allow_rander', false) && isset($attributes['copy_text']) ? esc_attr($attributes['copy_text']) : 'true', | |
| 2062 | - 'add_text' => esc_attr($attributes['add_text']), | |
| 2063 | - 'draw' => apply_filters('embedpress/is_allow_rander', false) && isset($attributes['draw']) ? esc_attr($attributes['draw']) : 'true', | |
| 2064 | - 'doc_rotation' => esc_attr($attributes['doc_rotation']), | |
| 2065 | - 'doc_details' => esc_attr($attributes['doc_details']), | |
| 2066 | - 'add_image' => esc_attr($attributes['add_image']), | |
| 2067 | - 'zoom_in' => esc_attr($attributes['zoom_in']), | |
| 2068 | - 'zoom_out' => esc_attr($attributes['zoom_out']), | |
| 2069 | - 'fit_view' => esc_attr($attributes['fit_view']), | |
| 2070 | - 'bookmark' => esc_attr($attributes['bookmark']), | |
| 2071 | - 'flipbook_rtl' => defined('EMBEDPRESS_SL_ITEM_SLUG') && isset($attributes['flipbook_rtl']) && ($attributes['flipbook_rtl'] === 'true' || $attributes['flipbook_rtl'] === true) ? 'true' : 'false', | |
| 2072 | - 'flipbook_highlight_links' => isset($attributes['flipbook_highlight_links']) && ($attributes['flipbook_highlight_links'] === 'true' || $attributes['flipbook_highlight_links'] === true) ? 'true' : 'false', | |
| 2073 | - 'flipbook_highlight_color' => isset($attributes['flipbook_highlight_color']) ? esc_attr($attributes['flipbook_highlight_color']) : '', | |
| 2074 | - 'watermark_text' => defined('EMBEDPRESS_SL_ITEM_SLUG') ? esc_attr($attributes['watermark_text']) : '', | |
| 2075 | - 'watermark_font_size' => defined('EMBEDPRESS_SL_ITEM_SLUG') ? esc_attr($attributes['watermark_font_size']) : '48', | |
| 2076 | - 'watermark_color' => defined('EMBEDPRESS_SL_ITEM_SLUG') ? esc_attr($attributes['watermark_color']) : '#000000', | |
| 2077 | - 'watermark_opacity' => defined('EMBEDPRESS_SL_ITEM_SLUG') ? esc_attr($attributes['watermark_opacity']) : '15', | |
| 2078 | - 'watermark_style' => defined('EMBEDPRESS_SL_ITEM_SLUG') ? esc_attr($attributes['watermark_style']) : 'center', | |
| 2079 | - ]; | |
| 2080 | - | |
| 2081 | - if ($theme_mode === 'custom') { | |
| 2082 | - $params['customColor'] = esc_attr($attributes['custom_color']); | |
| 2083 | - } | |
| 2084 | - | |
| 2085 | - $query_string = http_build_query($params); | |
| 2086 | - if (function_exists('mb_convert_encoding')) { | |
| 2087 | - $query_string = mb_convert_encoding($query_string, 'UTF-8'); | |
| 2088 | - } | |
| 2089 | - | |
| 2090 | - return base64_encode($query_string); | |
| 2091 | 599 | } |
| 2092 | 600 | } |