PluginProbe
Embed Privacy / 1.10.4
Embed Privacy v1.10.4
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 / data / class-providers.php

class-providers.php in Embed Privacy 1.10.4, at inc/data/class-providers.php

265 lines 7.1 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\data;
3
4 use epiphyt\Embed_Privacy\embed\Provider;
5 use epiphyt\Embed_Privacy\Embed_Privacy;
6 use WP_Post;
7
8 /**
9 * Embed provider related functionality.
10 *
11 * @author Epiphyt
12 * @license GPL2
13 * @package epiphyt\Embed_Privacy
14 * @since 1.10.0
15 */
16 final class Providers {
17 /**
18 * @var \epiphyt\Embed_Privacy\data\Providers
19 */
20 public static $instance;
21
22 /**
23 * @var \epiphyt\Embed_privacy\embed\Provider[][] List of embed providers
24 */
25 private $list = [];
26
27 /**
28 * Initialize functionality.
29 */
30 public static function init() {
31 \add_filter( 'embed_privacy_provider_name', [ self::class, 'sanitize_name' ] );
32 }
33
34 /**
35 * Get an embed provider by its name.
36 *
37 * @param string $name The name to search for
38 * @return \epiphyt\Embed_privacy\embed\Provider The embed provider
39 */
40 public function get_by_name( $name ) {
41 $provider = new Provider();
42
43 if ( empty( $name ) ) {
44 return $provider;
45 }
46
47 $embed_providers = $this->get_list();
48 $name = self::sanitize_name( $name );
49
50 foreach ( $embed_providers as $embed_provider ) {
51 if ( $embed_provider->get_name() !== $name ) {
52 continue;
53 }
54
55 $provider = $embed_provider;
56 break;
57 }
58
59 return $provider;
60 }
61
62 /**
63 * Get a provider by its post object.
64 *
65 * @param \WP_Post $post Post object
66 * @return \epiphyt\Embed_privacy\embed\Provider Embed provider instance
67 */
68 public static function get_by_post( $post ) {
69 return new Provider( $post );
70 }
71
72 /**
73 * Get a list of providers by their post objects.
74 *
75 * @param \WP_Post[] $posts List of post objects
76 * @return \epiphyt\Embed_privacy\embed\Provider[] List of embed provider instances
77 */
78 public static function get_by_posts( $posts ) {
79 return \array_map( [ self::class, 'get_by_post' ], $posts );
80 }
81
82 /**
83 * Get a unique instance of the class.
84 *
85 * @return \epiphyt\Embed_Privacy\data\Providers The single instance of this class
86 */
87 public static function get_instance() {
88 if ( self::$instance === null ) {
89 self::$instance = new self();
90 }
91
92 return self::$instance;
93 }
94
95 /**
96 * Get a specific type of embeds.
97 *
98 * For more information on the accepted arguments in $args, see the
99 * {@link https://developer.wordpress.org/reference/classes/wp_query/
100 * WP_Query} documentation in the Developer Handbook.
101 *
102 * @param string $type The embed type
103 * @param array $args Additional arguments
104 * @return \epiphyt\Embed_Privacy\embed\Provider[] A list of providers
105 */
106 public function get_list( $type = 'all', $args = [] ) {
107 if ( ! empty( $this->list ) && isset( $this->list[ $type ] ) ) {
108 return $this->list[ $type ];
109 }
110
111 if ( $type === 'all' && isset( $this->list['custom'] ) && isset( $this->list['oembed'] ) ) {
112 $this->list[ $type ] = \array_merge( $this->list['custom'], $this->list['oembed'] );
113
114 return $this->list[ $type ];
115 }
116
117 if ( ! empty( $args ) ) {
118 $hash = \hash( 'md5', \wp_json_encode( $args ) );
119 }
120
121 // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query
122 switch ( $type ) {
123 case 'custom':
124 $custom_providers = \get_posts( \array_merge( [
125 'meta_query' => [ // phpcs:ignore SlevomatCodingStandard.Arrays.DisallowPartiallyKeyed.DisallowedPartiallyKeyed
126 'relation' => 'OR',
127 [ // phpcs:ignore Universal.Arrays.MixedKeyedUnkeyedArray.Found, Universal.Arrays.MixedArrayKeyTypes.ImplicitNumericKey
128 'compare' => 'NOT EXISTS',
129 'key' => 'is_system',
130 'value' => 'yes',
131 ],
132 [ // phpcs:ignore Universal.Arrays.MixedKeyedUnkeyedArray.Found
133 'compare' => '!=',
134 'key' => 'is_system',
135 'value' => 'yes',
136 ],
137 ],
138 'no_found_rows' => true,
139 'numberposts' => -1,
140 'order' => 'ASC',
141 'orderby' => 'post_title',
142 'post_type' => 'epi_embed',
143 'update_post_term_cache' => false,
144 ], $args ) );
145 $google_provider = \get_posts( \array_merge( [
146 'meta_key' => 'is_system',
147 'meta_value' => 'yes',
148 'name' => 'google-maps',
149 'no_found_rows' => true,
150 'post_type' => 'epi_embed',
151 'update_post_term_cache' => false,
152 ], $args ) );
153
154 if ( ! empty( $hash ) ) {
155 $this->list[ $hash ] = self::get_by_posts( \array_merge( $custom_providers, $google_provider ) );
156 }
157 else {
158 $this->list[ $type ] = self::get_by_posts( \array_merge( $custom_providers, $google_provider ) );
159 }
160 break;
161 case 'oembed':
162 $embed_providers = \get_posts( \array_merge( [
163 'meta_key' => 'is_system',
164 'meta_value' => 'yes',
165 'no_found_rows' => true,
166 'numberposts' => -1,
167 'order' => 'ASC',
168 'orderby' => 'post_title',
169 'post_type' => 'epi_embed',
170 'update_post_term_cache' => false,
171 ], $args ) );
172
173 if ( ! empty( $hash ) ) {
174 $this->list[ $hash ] = self::get_by_posts( $embed_providers );
175 }
176 else {
177 $this->list[ $type ] = self::get_by_posts( $embed_providers );
178 }
179 break;
180 case 'all':
181 default:
182 $embed_providers = \get_posts( \array_merge( [
183 'no_found_rows' => true,
184 'numberposts' => -1,
185 'order' => 'ASC',
186 'orderby' => 'post_title',
187 'post_type' => 'epi_embed',
188 'update_post_term_cache' => false,
189 ], $args ) );
190
191 if ( ! empty( $hash ) ) {
192 $this->list[ $hash ] = self::get_by_posts( $embed_providers );
193 }
194 else {
195 $this->list['all'] = self::get_by_posts( $embed_providers );
196 }
197 break;
198 }
199 // phpcs:enable
200
201 $identifier = ! empty( $hash ) && ! empty( $this->list[ $hash ] ) ? $hash : $type;
202
203 /**
204 * Filter the list of providers of a specific identifier.
205 *
206 * @since 1.10.0
207 *
208 * @param array $provider_list Current provider list
209 * @param string $identifier Current identifier
210 * @param array $global_list List with all providers of all identifiers
211 */
212 $this->list[ $identifier ] = (array) \apply_filters( 'embed_privacy_provider_list', $this->list[ $identifier ], $identifier, $this->list );
213
214 return $this->list[ $identifier ];
215 }
216
217 /**
218 * Check if a provider is always active.
219 *
220 * @param string $provider The embed provider in lowercase
221 * @return bool True if provider is always active, false otherwise
222 */
223 public static function is_always_active( $provider ) {
224 $javascript_detection = \get_option( 'embed_privacy_javascript_detection' );
225 $provider = self::sanitize_name( $provider );
226
227 if ( $javascript_detection ) {
228 return false;
229 }
230
231 $cookie = Embed_Privacy::get_instance()->get_cookie();
232
233 return isset( $cookie->{$provider} ) && $cookie->{$provider} === true;
234 }
235
236 /**
237 * Whether the current provider is disabled.
238 *
239 * @param \WP_Post|null $post Optional post object
240 * @return bool Whether the current provider is disabled
241 */
242 public static function is_disabled( $post = null ) {
243 $post_id = null;
244
245 if ( ! $post instanceof WP_Post ) {
246 $post_id = \get_the_ID();
247 }
248 else {
249 $post_id = $post->ID;
250 }
251
252 return \get_post_meta( $post_id, 'is_disabled', true ) === 'yes';
253 }
254
255 /**
256 * Sanitize the embed provider name.
257 *
258 * @param string $name Current provider name
259 * @return string Sanitized provider name
260 */
261 public static function sanitize_name( $name ) {
262 return \preg_replace( '/-\d+$/', '', \sanitize_title( \strtolower( $name ) ) );
263 }
264 }
265