PluginProbe
WP Offload SES Lite / 1.3
WP Offload SES Lite v1.3
1.8.2 trunk 0.1.2 0.2.1 0.7.2.1 0.8.2 1.0 1.1 1.2 1.2.1 1.2.2 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
wp-ses / classes / Utils.php

Utils.php in WP Offload SES Lite 1.3, at classes/Utils.php

253 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace DeliciousBrains\WP_Offload_SES;
4
5 use \Datetime;
6
7 class Utils {
8
9 /**
10 * Trailing slash prefix string ensuring no leading slashes.
11 *
12 * @param string $string
13 *
14 * @return string
15 */
16 public static function trailingslash_prefix( $string ) {
17 return ltrim( trailingslashit( $string ), '/\\' );
18 }
19
20 /**
21 * Remove scheme from URL.
22 *
23 * @param string $url
24 *
25 * @return string
26 */
27 public static function remove_scheme( $url ) {
28 return preg_replace( '/^(?:http|https):/', '', $url );
29 }
30
31 /**
32 * Reduce the given URL down to the simplest version of itself.
33 *
34 * Useful for matching against the full version of the URL in a full-text search
35 * or saving as a key for dictionary type lookup.
36 *
37 * @param string $url
38 *
39 * @return string
40 */
41 public static function reduce_url( $url ) {
42 $parts = self::parse_url( $url );
43 $host = isset( $parts['host'] ) ? $parts['host'] : '';
44 $port = isset( $parts['port'] ) ? ":{$parts['port']}" : '';
45 $path = isset( $parts['path'] ) ? $parts['path'] : '';
46
47 return '//' . $host . $port . $path;
48 }
49
50 /**
51 * Parses a URL into its components. Compatible with PHP < 5.4.7.
52 *
53 * @param string $url The URL to parse.
54 * @param int $component PHP_URL_ constant for URL component to return.
55 *
56 * @return mixed An array of the parsed components, mixed for a requested component, or false on error.
57 */
58 public static function parse_url( $url, $component = -1 ) {
59 $url = trim( $url );
60 $no_scheme = 0 === strpos( $url, '//' );
61
62 if ( $no_scheme ) {
63 $url = 'http:' . $url;
64 }
65
66 $parts = parse_url( $url, $component );
67
68 if ( 0 < $component ) {
69 return $parts;
70 }
71
72 if ( $no_scheme && is_array( $parts ) ) {
73 unset( $parts['scheme'] );
74 }
75
76 return $parts;
77 }
78
79 /**
80 * Is the string a URL?
81 *
82 * @param string $string
83 *
84 * @return bool
85 */
86 public static function is_url( $string ) {
87 if ( preg_match( '@^(?:https?:)?//[a-zA-Z0-9\-]+@', $string ) ) {
88 return true;
89 }
90
91 return false;
92 }
93
94 /**
95 * Is the string a relative URL?
96 *
97 * @param string $string
98 *
99 * @return bool
100 */
101 public static function is_relative_url( $string ) {
102 if ( empty( $string ) || ! is_string( $string ) ) {
103 return false;
104 }
105
106 $url = static::parse_url( $string );
107
108 return ( empty( $url['scheme'] ) && empty( $url['host'] ) );
109 }
110
111 /**
112 * Create a site link for given URL.
113 *
114 * @param string $url
115 * @param string $text
116 *
117 * @return string
118 */
119 public static function dbrains_link( $url, $text ) {
120 return sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html( $text ) );
121 }
122
123 /**
124 * Check whether two URLs share the same domain.
125 *
126 * @param string $url_a
127 * @param string $url_b
128 *
129 * @return bool
130 */
131 public static function url_domains_match( $url_a, $url_b ) {
132 if ( ! static::is_url( $url_a ) || ! static::is_url( $url_b ) ) {
133 return false;
134 }
135
136 return static::parse_url( $url_a, PHP_URL_HOST ) === static::parse_url( $url_b, PHP_URL_HOST );
137 }
138
139 /**
140 * Get the current domain.
141 *
142 * @return string|false
143 */
144 public static function current_domain() {
145 return parse_url( home_url(), PHP_URL_HOST );
146 }
147
148 /**
149 * Get the base domain of the current domain.
150 *
151 * @return string
152 */
153 public static function current_base_domain() {
154 $domain = static::current_domain();
155 $parts = explode( '.', $domain, 2 );
156
157 if ( isset( $parts[1] ) && in_array( $parts[0], array( 'www' ) ) ) {
158 return $parts[1];
159 }
160
161 return $domain;
162 }
163
164 /**
165 * Get the first defined constant from the given list of constant names.
166 *
167 * @param array $constants
168 *
169 * @return string|false string constant name if defined, otherwise false if none are defined
170 */
171 public static function get_first_defined_constant( $constants ) {
172 foreach ( (array) $constants as $constant ) {
173 if ( defined( $constant ) ) {
174 return $constant;
175 }
176 }
177
178 return false;
179 }
180
181 /**
182 * Get the default email address used by WordPress. Lifted from wp_mail().
183 *
184 * @param bool $apply_filter Set to false to override the `wp_mail_from` filter.
185 *
186 * @return string
187 */
188 public static function get_wordpress_default_email( $apply_filter = true ) {
189 // Get the site domain and get rid of www.
190 $sitename = wp_parse_url( network_home_url(), PHP_URL_HOST );
191
192 if ( substr( $sitename, 0, 4 ) == 'www.' ) {
193 $sitename = substr( $sitename, 4 );
194 }
195
196 $from_email = 'wordpress@' . $sitename;
197
198 if ( ! $apply_filter ) {
199 return $from_email;
200 }
201
202 return apply_filters( 'wp_mail_from', $from_email );
203 }
204
205 /**
206 * Checks if current screen is a network admin screen,
207 * or if the current AJAX request was sent from a network admin screen.
208 *
209 * @return bool
210 */
211 public static function is_network_admin() {
212 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
213 /**
214 * Workaround - `is_network_admin()` won't work w/ AJAX.
215 * https://core.trac.wordpress.org/ticket/22589
216 */
217 if ( isset( $_SERVER['HTTP_REFERER'] ) && false !== strpos( $_SERVER['HTTP_REFERER'], 'wp-admin/network' ) ) {
218 return true;
219 }
220
221 return false;
222 }
223
224 return is_network_admin();
225 }
226
227 /**
228 * Gets the formatted date & time from a MySQL timestamp.
229 *
230 * @param string $timestamp The timestamp.
231 *
232 * @return array
233 */
234 public static function get_date_and_time( $timestamp ) {
235 $datetime = new Datetime( $timestamp );
236 $date_format = str_replace( 'F', 'M', get_option( 'date_format', 'm/d/Y' ) );
237 $time_format = get_option( 'time_format', 'g:i a' );
238
239 $date = $datetime->format( $date_format );
240 $time = $datetime->format( $time_format );
241
242 if ( ! $date || ! $time ) {
243 return false;
244 }
245
246 return array(
247 'date' => $date,
248 'time' => $time,
249 );
250 }
251
252 }
253