PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 3.6.13
TinyPNG – JPEG, PNG & WebP image compression v3.6.13
3.7.0 3.6.14 trunk 1.0.0 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.4.0 1.5.0 1.6.0 1.7.0 1.7.1 1.7.2 2.0.0 2.0.1 2.0.2 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.0.0 3.0.1 3.1.0 3.2.0 3.2.1 3.3 3.4 3.4.1 3.4.2 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.6.0 3.6.1 3.6.10 3.6.11 3.6.12 3.6.13 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9
tiny-compress-images / src / class-tiny-helpers.php
tiny-compress-images / src Last commit date
compatibility 5 months ago config 5 months ago css 5 months ago data 3 years ago images 3 years ago js 5 months ago vendor 4 months ago views 2 months ago class-tiny-apache-rewrite.php 2 months ago class-tiny-bulk-optimization.php 5 months ago class-tiny-cli.php 5 months ago class-tiny-compress-client.php 5 months ago class-tiny-compress-fopen.php 5 months ago class-tiny-compress.php 5 months ago class-tiny-conversion.php 2 months ago class-tiny-diagnostics.php 5 months ago class-tiny-exception.php 5 months ago class-tiny-helpers.php 5 months ago class-tiny-image-size.php 5 months ago class-tiny-image.php 5 months ago class-tiny-logger.php 2 months ago class-tiny-notices.php 2 months ago class-tiny-php.php 5 months ago class-tiny-picture.php 4 months ago class-tiny-plugin.php 2 months ago class-tiny-settings.php 2 months ago class-tiny-source-base.php 2 months ago class-tiny-source-image.php 5 months ago class-tiny-source-picture.php 5 months ago class-tiny-wp-base.php 5 months ago
class-tiny-helpers.php
175 lines
1 <?php
2 /*
3 * Tiny Compress Images - WordPress plugin.
4 * Copyright (C) 2015-2018 Tinify B.V.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 class Tiny_Helpers {
22
23
24 /**
25 * truncate_text will truncate a string to a given length.
26 * When text is longer than the given length, the string will be truncated and
27 * the last characters will be replaced with an ellipsis.
28 *
29 * We can use mb_strlen & mb_substr as WordPress provides a compat function for
30 * it if mbstring php module is not installed.
31 *
32 * @param string $text the text
33 * @param integer $length the maximum length of the string
34 * @return string the truncated string
35 */
36 public static function truncate_text( $text, $length ) {
37 if ( mb_strlen( $text ) > $length ) {
38 return mb_substr( $text, 0, $length - 3 ) . '...';
39 }
40 return $text;
41 }
42
43 /**
44 * Will replace the file extension with the specified mimetype.
45 *
46 * @param string $mimetype The format to replace the extension with.
47 * Currently supports 'image/avif' or 'image/webp'
48 * @param string $filepath The full path to replace the extension in, ex /home/user/image.png
49 *
50 * @return string The full path to the file with the new extension, ex /home/user/image.avif
51 */
52 public static function replace_file_extension( $mimetype, $filepath ) {
53 $parts = pathinfo( $filepath );
54
55 if ( ! isset( $parts['extension'] ) ) {
56 return $filepath;
57 }
58
59 $extension_new = self::mimetype_to_extension( $mimetype );
60 if ( null === $extension_new ) {
61 return $filepath;
62 }
63
64 $dir = $parts['dirname'];
65 $name = $parts['filename'];
66 $sep = DIRECTORY_SEPARATOR;
67
68 if ( '.' === $dir ) {
69 return $name . '.' . $extension_new;
70 }
71
72 if ( $dir === $sep ) {
73 return $sep . $name . '.' . $extension_new;
74 }
75
76 $dir = rtrim( $dir, '/\\' );
77 return $dir . $sep . $name . '.' . $extension_new;
78 }
79
80 private static function mimetype_to_extension( $mimetype ) {
81 switch ( $mimetype ) {
82 case 'image/jpeg':
83 return 'jpg';
84 case 'image/png':
85 return 'png';
86 case 'image/webp':
87 return 'webp';
88 case 'image/avif':
89 return 'avif';
90 default:
91 return null;
92 }
93 }
94
95 /**
96 * Will return the mimetype of the file
97 *
98 * ref: https://www.php.net/manual/en/class.finfo.php
99 *
100 * @param string $input The file contents
101 * @return string The mimetype of the file
102 */
103 public static function get_mimetype( $input ) {
104 if ( class_exists( 'finfo' ) ) {
105 $finfo = new finfo( FILEINFO_MIME_TYPE );
106 $mime = $finfo->buffer( $input );
107 return $mime;
108 } else {
109 throw new Exception( 'finfo extension is not available.' );
110 }
111 }
112
113
114 /**
115 * Checks wether a user is viewing from a page builder
116 *
117 * @since 3.6.5
118 */
119 public static function is_pagebuilder_request() {
120 $pagebuilder_keys = array(
121 'fl_builder', // Beaver Builder
122 'et_fb', // Divi Builder
123 'bricks', // Bricks Builder
124 'breakdance', // Breakdance Builder
125 'breakdance_browser', // Breakdance Builder
126 'ct_builder', // Oxygen Builder
127 'fb-edit', // Avada Live Builder
128 'builder', // Avada Live Builder
129 'spio_no_cdn', // Site Origin
130 'tatsu', // Tatsu Builder
131 'tve', // Thrive Architect
132 'tcbf', // Thrive Architect
133 );
134
135 foreach ( $pagebuilder_keys as $key ) {
136 if ( isset( $_GET[ $key ] ) ) {
137 return true;
138 }
139 }
140
141 return false;
142 }
143
144 /**
145 * Gets or initializes the WordPress filesystem instance.
146 *
147 * Returns the global WP_Filesystem instance, initializing it if necessary.
148 * This helper prevents repeated initialization code throughout the plugin.
149 *
150 * @since 3.7.0
151 *
152 * @return WP_Filesystem_Base The WP_Filesystem instance.
153 * @throws Exception If the filesystem cannot be initialized.
154 */
155 public static function get_wp_filesystem() {
156 global $wp_filesystem;
157
158 if ( $wp_filesystem instanceof WP_Filesystem_Base ) {
159 return $wp_filesystem;
160 }
161
162 // Initialize the filesystem only if the function isn't available yet.
163 if ( ! function_exists( 'WP_Filesystem' ) ) {
164 require_once ABSPATH . 'wp-admin/includes/file.php';
165 }
166 WP_Filesystem();
167
168 if ( ! ( $wp_filesystem instanceof WP_Filesystem_Base ) ) {
169 throw new Exception( 'Unable to initialize WordPress filesystem.' );
170 }
171
172 return $wp_filesystem;
173 }
174 }
175