PluginProbe
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress / 4.1.1
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress v4.1.1
4.1.2 4.1.1 4.1.0 4.0.1 4.0.0 3.3.1 3.3.0 trunk 1.0.0 2.0.0 2.1.0 3.0.0 3.1.0 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8
quickwebp / includes / global-functions.php

global-functions.php in QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress 4.1.1, at includes/global-functions.php

248 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4 /**
5 * The global functions for this plugin
6 *
7 * @since 1.0.0
8 */
9
10
11 /**
12 * Sanitize a string
13 */
14 function quickwebp_sanitize_name( $name ) {
15
16 $extension = pathinfo( $name, PATHINFO_EXTENSION );
17 $name = pathinfo( $name, PATHINFO_FILENAME );
18 $name = mb_convert_encoding( $name, "UTF-8" );
19 $char_not_clean = array('/\?/','/\’/','/\'/','/À/','/Á/','/Â/','/Ã/','/Ä/','/�
20 /','/Ç/','/È/','/É/','/Ê/','/Ë/','/Ì/','/Í/','/Î/','/Ï/','/Ò/','/Ó/','/Ô/','/Õ/','/Ö/','/Ù/','/Ú/','/Û/','/Ü/','/Ý/','/à/','/á/','/â/','/ã/','/ä/','/å/','/ç/','/è/','/é/','/ê/','/ë/','/ì/','/í/','/î/','/ï/','/ð/','/ò/','/ó/','/ô/','/õ/','/ö/','/ù/','/ú/','/û/','/ü/','/ý/','/ÿ/', '/©/');
21 $clean = array('','-','-','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','o','o','o','o','o','u','u','u','u','y','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','o','o','o','o','o','o','u','u','u','u','y','y','copy');
22 $friendly_name = preg_replace($char_not_clean, $clean, $name);
23 $friendly_name = sanitize_title($friendly_name);
24
25 return $friendly_name . '.' . $extension;
26 }
27
28 /**
29 * Default values for the settings
30 */
31 function quickwebp_settings_default( $id ) {
32
33 $settings_arr = array(
34 'quickwebp_settings_conversion' => '1',
35 'quickwebp_settings_conversion_quality' => 'high',
36 'quickwebp_settings_conversion_ignore_webp' => array('checked'),
37 'quickwebp_settings_conversion_save_original' => array(),
38 'quickwebp_settings_conversion_display_webp_mode' => 'disabled',
39 'quickwebp_settings_resize' => '1',
40 'quickwebp_settings_resize_value' => 2000,
41 'quickwebp_settings_completion' => '1',
42 'quickwebp_settings_completion_options' => array(
43 'title',
44 'caption',
45 'alt',
46 'description'
47 ),
48 'quickwebp_settings_cleanup' => '1',
49 'quickwebp_settings_paste_image' => '0',
50 'quickwebp_settings_debug_mode' => '0',
51 );
52
53 return isset($settings_arr[$id]) ? $settings_arr[$id] : '';
54 }
55
56 /**
57 * Get the QuickWebP debug log path.
58 *
59 * @return string
60 */
61 function quickwebp_get_debug_log_path() {
62 $upload_dir = wp_upload_dir();
63 $base_dir = trailingslashit( $upload_dir['basedir'] ) . 'quickwebp';
64
65 if ( ! is_dir( $base_dir ) ) {
66 wp_mkdir_p( $base_dir );
67 }
68
69 return trailingslashit( $base_dir ) . 'quickwebp-debug.log';
70 }
71
72 /**
73 * Check whether QuickWebP debug mode is enabled.
74 *
75 * @return bool
76 */
77 function quickwebp_is_debug_mode_enabled() {
78 return '1' === (string) get_option( 'quickwebp_settings_debug_mode', quickwebp_settings_default( 'quickwebp_settings_debug_mode' ) );
79 }
80
81 /**
82 * Write a debug message to the QuickWebP custom log file.
83 *
84 * @param string $message Debug message.
85 * @param array $context Optional contextual data.
86 *
87 * @return void
88 */
89 function quickwebp_debug_log( $message, $context = array() ) {
90 if ( ! quickwebp_is_debug_mode_enabled() ) {
91 return;
92 }
93
94 $formatted_message = '[' . gmdate( 'Y-m-d H:i:s' ) . '] ' . sanitize_text_field( $message );
95
96 if ( ! empty( $context ) ) {
97 $formatted_message .= ' ' . wp_json_encode( $context );
98 }
99
100 error_log( $formatted_message . PHP_EOL, 3, quickwebp_get_debug_log_path() );
101 }
102
103 /**
104 * Allowed tags for SVG files
105 */
106 function quickwebp_allowed_tags_for_svg_files() {
107 $allowedtags = array(
108 'svg' => array(
109 'class' => true,
110 'xmlns' => true,
111 'width' => true,
112 'height' => true,
113 'viewbox' => true,
114 'preserveaspectratio' => true,
115 'fill' => true,
116 'aria-hidden' => true,
117 'focusable' => true,
118 'role' => true,
119 ),
120 'path' => array(
121 'fill' => true,
122 'fill-rule' => true,
123 'd' => true,
124 'transform' => true,
125 'stroke' => true,
126 'stroke-linecap' => true,
127 'stroke-linejoin' => true,
128 ),
129 'polygon' => array(
130 'fill' => true,
131 'fill-rule' => true,
132 'points' => true,
133 'transform' => true,
134 'focusable' => true,
135 ),
136 'rect' => array(
137 'fill' => true,
138 'fill-rule' => true,
139 'height' => true,
140 'width' => true,
141 'x' => true,
142 'y' => true,
143 ),
144 'line' => array(
145 'fill' => true,
146 'fill-rule' => true,
147 'x1' => true,
148 'x2' => true,
149 'y1' => true,
150 'y2' => true,
151 'stroke' => true,
152 'stroke-width' => true,
153 'transform' => true,
154 ),
155 'defs' => array(
156 'id' => true,
157 ),
158 'clipPath' => array(
159 'id' => true,
160 ),
161 'g' => array(
162 'clip-path' => true,
163 'mask' => true,
164 ),
165 'circle' => array(
166 'cx' => true,
167 'cy' => true,
168 'r' => true,
169 'fill' => true,
170 'stroke' => true,
171 'stroke-width' => true,
172 'stroke-dasharray' => true,
173 'stroke-linecap' => true,
174 ),
175 'mask' => array(
176 'id' => true,
177 'fill' => true,
178 'style' => true,
179 'maskUnits' => true,
180 'x' => true,
181 'y' => true,
182 'width' => true,
183 'height' => true,
184 ),
185 'image' => array(
186 'id' => true,
187 'href' => true,
188 'x' => true,
189 'y' => true,
190 'width' => true,
191 'height' => true,
192 'clip' => true,
193 'mask' => true,
194 'opacity' => true,
195 'xlink:href' => true,
196 ),
197 'defs' => array(
198 'id' => true,
199 ),
200 'pattern' => array(
201 'id' => true,
202 'x' => true,
203 'y' => true,
204 'width' => true,
205 'height' => true,
206 'patternUnits' => true,
207 'patternContentUnits' => true,
208 ),
209 'use' => array(
210 'id' => true,
211 'x' => true,
212 'y' => true,
213 'xlink:href' => true,
214 'transform' => true,
215 ),
216 );
217 return $allowedtags;
218 }
219
220 /**
221 * Build a QuickWebP Pro URL with tracking parameters.
222 *
223 * @param string $context Stable location identifier for the CTA.
224 * @param array $additional_args Optional additional query args.
225 *
226 * @return string
227 */
228 function quickwebp_get_pro_url( $context, $additional_args = array() ) {
229 $base_url = 'https://solutions.leyoweb.com/products/quickwebp/';
230
231 if ( class_exists( 'Quickwebp_Settings' ) && defined( 'Quickwebp_Settings::QUICKWEBP_GET_LICENSE_URL' ) ) {
232 $base_url = Quickwebp_Settings::QUICKWEBP_GET_LICENSE_URL;
233 }
234
235 return add_query_arg(
236 array_merge(
237 $additional_args,
238 array(
239 'utm_source' => 'quickwebp-plugin',
240 'utm_medium' => 'plugin',
241 'utm_campaign' => 'upgrade-pro',
242 'utm_content' => sanitize_key( $context ),
243 )
244 ),
245 $base_url
246 );
247 }
248