PluginProbe
Embed Privacy / trunk
Embed Privacy vtrunk
1.14.0 1.13.0 trunk 0.1 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.10.0 1.10.1 1.10.10 1.10.2 1.10.3 1.10.4 1.10.5 1.10.6 1.10.7 1.10.8 1.10.9 1.11.0 1.11.1 1.11.2 All 68 releases
embed-privacy / inc / integration / class-enfold.php

class-enfold.php in Embed Privacy trunk, at inc/integration/class-enfold.php

124 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace epiphyt\Embed_Privacy\integration;
3
4 use epiphyt\Embed_Privacy\data\Providers;
5 use epiphyt\Embed_Privacy\embed\Provider;
6 use epiphyt\Embed_Privacy\embed\Template;
7
8 /**
9 * Enfold integration for Embed Privacy.
10 *
11 * @author Epiphyt
12 * @license GPL2
13 * @package epiphyt\Embed_Privacy
14 * @since 1.13.0
15 */
16 final class Enfold {
17 /**
18 * @var int Depth of [av_video] shortcode
19 */
20 public static $av_video_depth = 0;
21
22 /**
23 * Initialize functionality.
24 */
25 public static function init() {
26 \add_filter( 'do_shortcode_tag', [ self::class, 'decrement_depth' ], 10, 2 );
27 \add_filter( 'embed_privacy_custom_embed_replacement', [ self::class, 'maybe_set_original_content' ], 10, 2 );
28 \add_filter( 'embed_privacy_custom_oembed_replacement', [ self::class, 'maybe_set_original_content' ], 10, 2 );
29 \add_filter( 'embed_privacy_ignored_shortcodes', [ self::class, 'set_ignored_shortcode' ] );
30 \add_filter( 'pre_do_shortcode_tag', [ self::class, 'increment_depth' ], 10, 2 );
31 \add_filter( 'avf_sc_video_output', [ self::class, 'set_video_output' ], 10, 2 );
32 }
33
34 /**
35 * Decrement [av_video] shortcode depth
36 *
37 * @param string $output Shortcode output
38 * @param string $tag Shortcode tag name
39 * @return string Shortcode output
40 */
41 public static function decrement_depth( $output, $tag ) {
42 if ( $tag === 'av_video' && self::$av_video_depth > 0 ) {
43 --self::$av_video_depth;
44 }
45
46 return $output;
47 }
48
49 /**
50 * Increment [av_video] shortcode depth
51 *
52 * @param string $output Shortcode output
53 * @param string $tag Shortcode tag name
54 * @return string Shortcode output
55 */
56 public static function increment_depth( $output, $tag ) {
57 if ( $tag === 'av_video' ) {
58 ++self::$av_video_depth;
59 }
60
61 return $output;
62 }
63
64 /**
65 * Maybe set original content for this embed.
66 * If we're in context of the [av_video] shortcode, return the original.
67 * Otherwise the custom one.
68 *
69 * @param string $new_content The new content
70 * @param string $original_content The original content
71 * @return string Either the new or original content
72 */
73 static function maybe_set_original_content( $new_content, $original_content ) {
74 if ( self::$av_video_depth ) {
75 return $original_content;
76 }
77
78 return $new_content;
79 }
80
81 /**
82 * Ignore the Avia video shortcode.
83 *
84 * @param string[] $ignored_shortcodes List of shortcodes
85 * @return string[] Updated list of shortcodes
86 */
87 public static function set_ignored_shortcode( array $ignored_shortcodes ) {
88 $ignored_shortcodes[] = 'av_video';
89
90 return $ignored_shortcodes;
91 }
92
93 /**
94 * Set video shortcode output.
95 *
96 * @param string $output Current shortcode output
97 * @param array $atts Shortcode attributes
98 * @return string Updated shortcode output
99 */
100 static function set_video_output( $output, array $atts ) {
101 if ( empty( $atts['src'] ) ) {
102 return $output;
103 }
104
105 $current_provider = null;
106
107 foreach ( Providers::get_instance()->get_list() as $provider ) {
108 if ( empty( $provider->get_pattern() ) ) {
109 continue;
110 }
111
112 if ( \preg_match( $provider->get_pattern(), $atts['src'] ) ) {
113 $current_provider = $provider;
114 }
115 }
116
117 if ( ! $current_provider instanceof Provider ) {
118 return $output;
119 }
120
121 return Template::get( $current_provider, $output, $atts );
122 }
123 }
124