| @@ -1,0 +1,115 @@ | ||
| 1 | +<?php | |
| 2 | +/* | |
| 3 | + * License: GPLv3 | |
| 4 | + * License URI: https://www.gnu.org/licenses/gpl.txt | |
| 5 | + * Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/) | |
| 6 | + */ | |
| 7 | + | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + | |
| 10 | + die( 'These aren\'t the droids you\'re looking for.' ); | |
| 11 | +} | |
| 12 | + | |
| 13 | +if ( ! class_exists( 'WpssoIntegUtilElementor' ) ) { | |
| 14 | + | |
| 15 | + class WpssoIntegUtilElementor { | |
| 16 | + | |
| 17 | + private $p; // Wpsso class object. | |
| 18 | + | |
| 19 | + public function __construct( &$plugin ) { | |
| 20 | + | |
| 21 | + $this->p =& $plugin; | |
| 22 | + | |
| 23 | + if ( $this->p->debug->enabled ) { | |
| 24 | + | |
| 25 | + $this->p->debug->mark(); | |
| 26 | + } | |
| 27 | + | |
| 28 | + add_filter( 'sucom_get_post_types', array( $this, 'exclude_post_types' ), 10, 3 ); | |
| 29 | + | |
| 30 | + add_action( 'elementor/editor/after_save', array( $this, 'refresh_post_cache' ), 1000, 2 ); | |
| 31 | + | |
| 32 | + $this->p->util->add_plugin_filters( $this, array( | |
| 33 | + 'content_videos' => 2, | |
| 34 | + ), $prio = 110 ); | |
| 35 | + } | |
| 36 | + | |
| 37 | + /* | |
| 38 | + * Elementor incorrectly registers the 'elementor_library' post type as 'public' = true and 'show_ui' = true, so we | |
| 39 | + * need to remove it from the list of public post types. | |
| 40 | + */ | |
| 41 | + public function exclude_post_types( $post_types, $output, $args ) { | |
| 42 | + | |
| 43 | + if ( 'objects' === $output ) { | |
| 44 | + | |
| 45 | + foreach ( $post_types as $num => $obj ) { | |
| 46 | + | |
| 47 | + if ( is_object( $obj ) ) { // Just in case. | |
| 48 | + | |
| 49 | + if ( 'elementor_library' === $obj->name ) { | |
| 50 | + | |
| 51 | + unset( $post_types[ $num ] ); | |
| 52 | + } | |
| 53 | + } | |
| 54 | + } | |
| 55 | + | |
| 56 | + } else unset( $post_types[ 'elementor_library' ] ); | |
| 57 | + | |
| 58 | + return $post_types; | |
| 59 | + } | |
| 60 | + | |
| 61 | + public function refresh_post_cache( $post_id, $editor_data ) { | |
| 62 | + | |
| 63 | + $this->p->post->refresh_cache( $post_id ); // Refresh the cache for a single post ID. | |
| 64 | + } | |
| 65 | + | |
| 66 | + public function filter_content_videos( $videos, $content ) { | |
| 67 | + | |
| 68 | + if ( $this->p->debug->enabled ) { | |
| 69 | + | |
| 70 | + $this->p->debug->mark(); | |
| 71 | + } | |
| 72 | + | |
| 73 | + /* | |
| 74 | + * Elementor widget for videos. | |
| 75 | + * | |
| 76 | + * Example: | |
| 77 | + * | |
| 78 | + * <div class="elementor-element elementor-element-5c62c7a elementor-aspect-ratio-169 elementor-widget elementor-widget-video" data-id="5c62c7a" data-element_type="widget" data-settings="{"youtube_url":"https:\/\/www.youtube.com\/watch?v=vfeYTg4POxw","modestbranding":"yes","yt_privacy":"yes","video_type":"youtube","controls":"yes","aspect_ratio":"169"}" data-widget_type="video.default"> | |
| 79 | + */ | |
| 80 | + if ( preg_match_all( '/<(div)[^<>]*? class=[\'"][^\'"]*(elementor-widget-video)[^\'"]*[\'"][^<>]* data-settings=[\'"]([^ ]+)[\'"][^<>]*>/i', | |
| 81 | + $content, $all_matches, PREG_SET_ORDER ) ) { | |
| 82 | + | |
| 83 | + if ( $this->p->debug->enabled ) { | |
| 84 | + | |
| 85 | + $this->p->debug->log( count( $all_matches ) . ' <div/> elementor widget video tag(s) found' ); | |
| 86 | + } | |
| 87 | + | |
| 88 | + foreach ( $all_matches as $match_num => $media ) { | |
| 89 | + | |
| 90 | + $media[ 3 ] = html_entity_decode( $media[ 3 ] ); // Just in case. | |
| 91 | + | |
| 92 | + $json_decoded = json_decode( $media[ 3 ], $assoc = true ); | |
| 93 | + | |
| 94 | + if ( ! empty( $json_decoded[ 'video_type' ] ) ) { // Example: 'youtube'. | |
| 95 | + | |
| 96 | + $video_type = $json_decoded[ 'video_type' ]; | |
| 97 | + | |
| 98 | + if ( ! empty( $json_decoded[ $video_type . '_url' ] ) ) { // Example: 'youtube_url'. | |
| 99 | + | |
| 100 | + $videos[] = array( | |
| 101 | + 'url' => $json_decoded[ $video_type . '_url' ], | |
| 102 | + ); | |
| 103 | + } | |
| 104 | + } | |
| 105 | + } | |
| 106 | + | |
| 107 | + } elseif ( $this->p->debug->enabled ) { | |
| 108 | + | |
| 109 | + $this->p->debug->log( 'no <div/> elementor widget video tag(s) found' ); | |
| 110 | + } | |
| 111 | + | |
| 112 | + return $videos; | |
| 113 | + } | |
| 114 | + } | |
| 115 | +} | |