PluginProbe
Embed Privacy / 1.10.0
Embed Privacy v1.10.0
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.0, at inc/data/class-providers.php

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