PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.0
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.0
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / CDN / CDN.php

CDN.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.0, at classes/CDN/CDN.php

125 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare(strict_types=1);
3
4 namespace Imagify\CDN;
5
6 use Imagify\EventManagement\SubscriberInterface;
7
8 /**
9 * CDN subscriber
10 */
11 class CDN implements SubscriberInterface {
12 /**
13 * Array of events this subscriber listens to
14 *
15 * @return array
16 */
17 public static function get_subscribed_events() {
18 return [
19 // @filter
20 'imagify_cdn_source_url' => 'get_cdn_source',
21 ];
22 }
23
24 /**
25 * Get the CDN "source".
26 *
27 * @since 1.9.3
28 *
29 * @param string $option_url An URL to use instead of the one stored in the option. It is used only if no constant/filter.
30 *
31 * @return array {
32 * @type string $source Where does it come from? Possible values are 'constant', 'filter', or 'option'.
33 * @type string $name Who? Can be a constant name, a plugin name, or an empty string.
34 * @type string $url The CDN URL, with a trailing slash. An empty string if no URL is set.
35 * }
36 */
37 public function get_cdn_source( $option_url = '' ) {
38 if ( defined( 'IMAGIFY_CDN_URL' ) && IMAGIFY_CDN_URL && is_string( IMAGIFY_CDN_URL ) ) {
39 // Use a constant.
40 $source = [
41 'source' => 'constant',
42 'name' => 'IMAGIFY_CDN_URL',
43 'url' => IMAGIFY_CDN_URL,
44 ];
45 } else {
46 // Maybe use a filter.
47 $filter_source = [
48 'name' => null,
49 'url' => null,
50 ];
51
52 /**
53 * Provide a custom CDN source.
54 *
55 * @since 1.9.3
56 *
57 * @param array $filter_source {
58 * @type $name string The name of which provides the URL (plugin name, etc).
59 * @type $url string The CDN URL.
60 * }
61 */
62 $filter_source = wpm_apply_filters_typed( 'array', 'imagify_cdn_source', $filter_source );
63
64 if ( ! empty( $filter_source['url'] ) ) {
65 $source = [
66 'source' => 'filter',
67 'name' => ! empty( $filter_source['name'] ) ? $filter_source['name'] : '',
68 'url' => $filter_source['url'],
69 ];
70 }
71 }
72
73 if ( empty( $source['url'] ) ) {
74 // No constant, no filter: use the option.
75 $source = [
76 'source' => 'option',
77 'name' => '',
78 'url' => $option_url && is_string( $option_url ) ? $option_url : get_imagify_option( 'cdn_url' ),
79 ];
80 }
81
82 if ( empty( $source['url'] ) ) {
83 // Nothing set.
84 return [
85 'source' => 'option',
86 'name' => '',
87 'url' => '',
88 ];
89 }
90
91 $source['url'] = $this->sanitize_cdn_url( $source['url'] );
92
93 if ( empty( $source['url'] ) ) {
94 // Not an URL.
95 return [
96 'source' => 'option',
97 'name' => '',
98 'url' => '',
99 ];
100 }
101
102 return $source;
103 }
104
105 /**
106 * Sanitize the CDN URL value.
107 *
108 * @since 1.9.3
109 *
110 * @param string $url The URL to sanitize.
111 *
112 * @return string
113 */
114 public function sanitize_cdn_url( $url ) {
115 $url = sanitize_text_field( $url );
116
117 if ( ! $url || ! preg_match( '@^https?://.+\.[^.]+@i', $url ) ) {
118 // Not an URL.
119 return '';
120 }
121
122 return trailingslashit( $url );
123 }
124 }
125