forked from tinify/wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-tiny-helpers.php
More file actions
174 lines (153 loc) · 4.75 KB
/
class-tiny-helpers.php
File metadata and controls
174 lines (153 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/*
* Tiny Compress Images - WordPress plugin.
* Copyright (C) 2015-2018 Tinify B.V.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
class Tiny_Helpers {
/**
* truncate_text will truncate a string to a given length.
* When text is longer than the given length, the string will be truncated and
* the last characters will be replaced with an ellipsis.
*
* We can use mb_strlen & mb_substr as WordPress provides a compat function for
* it if mbstring php module is not installed.
*
* @param string $text the text
* @param integer $length the maximum length of the string
* @return string the truncated string
*/
public static function truncate_text( $text, $length ) {
if ( mb_strlen( $text ) > $length ) {
return mb_substr( $text, 0, $length - 3 ) . '...';
}
return $text;
}
/**
* Will replace the file extension with the specified mimetype.
*
* @param string $mimetype The format to replace the extension with.
* Currently supports 'image/avif' or 'image/webp'
* @param string $filepath The full path to replace the extension in, ex /home/user/image.png
*
* @return string The full path to the file with the new extension, ex /home/user/image.avif
*/
public static function replace_file_extension( $mimetype, $filepath ) {
$parts = pathinfo( $filepath );
if ( ! isset( $parts['extension'] ) ) {
return $filepath;
}
$extension_new = self::mimetype_to_extension( $mimetype );
if ( null === $extension_new ) {
return $filepath;
}
$dir = $parts['dirname'];
$name = $parts['filename'];
$sep = DIRECTORY_SEPARATOR;
if ( '.' === $dir ) {
return $name . '.' . $extension_new;
}
if ( $dir === $sep ) {
return $sep . $name . '.' . $extension_new;
}
$dir = rtrim( $dir, '/\\' );
return $dir . $sep . $name . '.' . $extension_new;
}
private static function mimetype_to_extension( $mimetype ) {
switch ( $mimetype ) {
case 'image/jpeg':
return 'jpg';
case 'image/png':
return 'png';
case 'image/webp':
return 'webp';
case 'image/avif':
return 'avif';
default:
return null;
}
}
/**
* Will return the mimetype of the file
*
* ref: https://www.php.net/manual/en/class.finfo.php
*
* @param string $input The file contents
* @return string The mimetype of the file
*/
public static function get_mimetype( $input ) {
if ( class_exists( 'finfo' ) ) {
$finfo = new finfo( FILEINFO_MIME_TYPE );
$mime = $finfo->buffer( $input );
return $mime;
} else {
throw new Exception( 'finfo extension is not available.' );
}
}
/**
* Checks wether a user is viewing from a page builder
*
* @since 3.6.5
*/
public static function is_pagebuilder_request() {
$pagebuilder_keys = array(
'fl_builder', // Beaver Builder
'et_fb', // Divi Builder
'bricks', // Bricks Builder
'breakdance', // Breakdance Builder
'breakdance_browser', // Breakdance Builder
'ct_builder', // Oxygen Builder
'fb-edit', // Avada Live Builder
'builder', // Avada Live Builder
'spio_no_cdn', // Site Origin
'tatsu', // Tatsu Builder
'tve', // Thrive Architect
'tcbf', // Thrive Architect
);
foreach ( $pagebuilder_keys as $key ) {
if ( isset( $_GET[ $key ] ) ) {
return true;
}
}
return false;
}
/**
* Gets or initializes the WordPress filesystem instance.
*
* Returns the global WP_Filesystem instance, initializing it if necessary.
* This helper prevents repeated initialization code throughout the plugin.
*
* @since 3.7.0
*
* @return WP_Filesystem_Base The WP_Filesystem instance.
* @throws Exception If the filesystem cannot be initialized.
*/
public static function get_wp_filesystem() {
global $wp_filesystem;
if ( $wp_filesystem instanceof WP_Filesystem_Base ) {
return $wp_filesystem;
}
// Initialize the filesystem only if the function isn't available yet.
if ( ! function_exists( 'WP_Filesystem' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
WP_Filesystem();
if ( ! ( $wp_filesystem instanceof WP_Filesystem_Base ) ) {
throw new Exception( 'Unable to initialize WordPress filesystem.' );
}
return $wp_filesystem;
}
}