PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 1.7.1
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v1.7.1
4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.10 4.4.11 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5.0 4.5.1
embedpress / freemius / includes / fs-essential-functions.php
embedpress / freemius / includes Last commit date
debug 9 years ago entities 9 years ago managers 9 years ago sdk 9 years ago supplements 9 years ago class-freemius-abstract.php 9 years ago class-freemius.php 9 years ago class-fs-api.php 9 years ago class-fs-logger.php 9 years ago class-fs-plugin-updater.php 9 years ago class-fs-security.php 9 years ago fs-core-functions.php 9 years ago fs-essential-functions.php 9 years ago fs-plugin-info-dialog.php 9 years ago i18n.php 9 years ago index.php 9 years ago l10n.php 9 years ago
fs-essential-functions.php
445 lines
1 <?php
2 /**
3 * @package Freemius
4 * @copyright Copyright (c) 2015, Freemius, Inc.
5 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6 * @since 1.1.5
7 */
8
9 if ( ! function_exists( 'fs_normalize_path' ) ) {
10 if ( function_exists( 'wp_normalize_path' ) ) {
11 /**
12 * Normalize a filesystem path.
13 *
14 * Replaces backslashes with forward slashes for Windows systems, and ensures
15 * no duplicate slashes exist.
16 *
17 * @param string $path Path to normalize.
18 *
19 * @return string Normalized path.
20 */
21 function fs_normalize_path( $path ) {
22 return wp_normalize_path( $path );
23 }
24 } else {
25 function fs_normalize_path( $path ) {
26 $path = str_replace( '\\', '/', $path );
27 $path = preg_replace( '|/+|', '/', $path );
28
29 return $path;
30 }
31 }
32 }
33
34 #region Core Redirect (copied from BuddyPress) -----------------------------------------
35
36 if ( ! function_exists( 'fs_redirect' ) ) {
37 /**
38 * Redirects to another page, with a workaround for the IIS Set-Cookie bug.
39 *
40 * @link http://support.microsoft.com/kb/q176113/
41 * @since 1.5.1
42 * @uses apply_filters() Calls 'wp_redirect' hook on $location and $status.
43 *
44 * @param string $location The path to redirect to.
45 * @param bool $exit If true, exit after redirect (Since 1.2.1.5).
46 * @param int $status Status code to use.
47 *
48 * @return bool False if $location is not set
49 */
50 function fs_redirect( $location, $exit = true, $status = 302 ) {
51 global $is_IIS;
52
53 $file = '';
54 $line = '';
55 if ( headers_sent($file, $line) ) {
56 if ( WP_FS__DEBUG_SDK && class_exists( 'FS_Admin_Notice_Manager' ) ) {
57 $notices = FS_Admin_Notice_Manager::instance( 'global' );
58
59 $notices->add( "Freemius failed to redirect the page because the headers have been already sent from line <b><code>{$line}</code></b> in file <b><code>{$file}</code></b>. If it's unexpected, it usually happens due to invalid space and/or EOL character(s).", 'Oops...', 'error' );
60 }
61
62 return false;
63 }
64
65 if ( defined( 'DOING_AJAX' ) ) {
66 // Don't redirect on AJAX calls.
67 return false;
68 }
69
70 if ( ! $location ) // allows the wp_redirect filter to cancel a redirect
71 {
72 return false;
73 }
74
75 $location = fs_sanitize_redirect( $location );
76
77 if ( $is_IIS ) {
78 header( "Refresh: 0;url=$location" );
79 } else {
80 if ( php_sapi_name() != 'cgi-fcgi' ) {
81 status_header( $status );
82 } // This causes problems on IIS and some FastCGI setups
83 header( "Location: $location" );
84 }
85
86 if ( $exit ) {
87 exit();
88 }
89
90 return true;
91 }
92
93 if ( ! function_exists( 'fs_sanitize_redirect' ) ) {
94 /**
95 * Sanitizes a URL for use in a redirect.
96 *
97 * @since 2.3
98 *
99 * @param string $location
100 *
101 * @return string redirect-sanitized URL
102 */
103 function fs_sanitize_redirect( $location ) {
104 $location = preg_replace( '|[^a-z0-9-~+_.?#=&;,/:%!]|i', '', $location );
105 $location = fs_kses_no_null( $location );
106
107 // remove %0d and %0a from location
108 $strip = array( '%0d', '%0a' );
109 $found = true;
110 while ( $found ) {
111 $found = false;
112 foreach ( (array) $strip as $val ) {
113 while ( strpos( $location, $val ) !== false ) {
114 $found = true;
115 $location = str_replace( $val, '', $location );
116 }
117 }
118 }
119
120 return $location;
121 }
122 }
123
124 if ( ! function_exists( 'fs_kses_no_null' ) ) {
125 /**
126 * Removes any NULL characters in $string.
127 *
128 * @since 1.0.0
129 *
130 * @param string $string
131 *
132 * @return string
133 */
134 function fs_kses_no_null( $string ) {
135 $string = preg_replace( '/\0+/', '', $string );
136 $string = preg_replace( '/(\\\\0)+/', '', $string );
137
138 return $string;
139 }
140 }
141 }
142
143 #endregion Core Redirect (copied from BuddyPress) -----------------------------------------
144
145 if ( ! function_exists( '__fs' ) ) {
146 global $fs_text_overrides;
147
148 if ( ! isset( $fs_text_overrides ) ) {
149 $fs_text_overrides = array();
150 }
151
152 /**
153 * Retrieve a translated text by key.
154 *
155 * @author Vova Feldman (@svovaf)
156 * @since 1.1.4
157 *
158 * @param string $key
159 * @param string $slug
160 *
161 * @return string
162 *
163 * @global $fs_text , $fs_text_overrides
164 */
165 function __fs( $key, $slug = 'freemius' ) {
166 global $fs_text,
167 $fs_module_info_text,
168 $fs_text_overrides;
169
170 if ( isset( $fs_text_overrides[ $slug ] ) ) {
171 if ( isset( $fs_text_overrides[ $slug ][ $key ] ) ) {
172 return $fs_text_overrides[ $slug ][ $key ];
173 }
174
175 $lower_key = strtolower( $key );
176 if ( isset( $fs_text_overrides[ $slug ][ $lower_key ] ) ) {
177 return $fs_text_overrides[ $slug ][ $lower_key ];
178 }
179 }
180
181 if ( ! isset( $fs_text ) ) {
182 $dir = defined( 'WP_FS__DIR_INCLUDES' ) ?
183 WP_FS__DIR_INCLUDES :
184 dirname( __FILE__ );
185
186 require_once $dir . '/i18n.php';
187 }
188
189 if ( isset( $fs_text[ $key ] ) ) {
190 return $fs_text[ $key ];
191 }
192
193 if ( isset( $fs_module_info_text[ $key ] ) ) {
194 return $fs_module_info_text[ $key ];
195 }
196
197 return $key;
198 }
199
200 /**
201 * Display a translated text by key.
202 *
203 * @author Vova Feldman (@svovaf)
204 * @since 1.1.4
205 *
206 * @param string $key
207 * @param string $slug
208 */
209 function _efs( $key, $slug = 'freemius' ) {
210 echo __fs( $key, $slug );
211 }
212
213 /**
214 * Override default i18n text phrases.
215 *
216 * @author Vova Feldman (@svovaf)
217 * @since 1.1.6
218 *
219 * @param string[] $key_value
220 * @param string $slug
221 *
222 * @global $fs_text_overrides
223 */
224 function fs_override_i18n( array $key_value, $slug = 'freemius' ) {
225 global $fs_text_overrides;
226
227 if ( ! isset( $fs_text_overrides[ $slug ] ) ) {
228 $fs_text_overrides[ $slug ] = array();
229 }
230
231 foreach ( $key_value as $key => $value ) {
232 $fs_text_overrides[ $slug ][ $key ] = $value;
233 }
234 }
235 }
236
237 if ( ! function_exists( 'fs_get_ip' ) ) {
238 /**
239 * Get client IP.
240 *
241 * @author Vova Feldman (@svovaf)
242 * @since 1.1.2
243 *
244 * @return string|null
245 */
246 function fs_get_ip() {
247 $fields = array(
248 'HTTP_CF_CONNECTING_IP',
249 'HTTP_CLIENT_IP',
250 'HTTP_X_FORWARDED_FOR',
251 'HTTP_X_FORWARDED',
252 'HTTP_FORWARDED_FOR',
253 'HTTP_FORWARDED',
254 'REMOTE_ADDR',
255 );
256
257 foreach ( $fields as $ip_field ) {
258 if ( ! empty( $_SERVER[ $ip_field ] ) ) {
259 return $_SERVER[ $ip_field ];
260 }
261 }
262
263 return null;
264 }
265 }
266
267 /**
268 * Leverage backtrace to find caller plugin main file path.
269 *
270 * @author Vova Feldman (@svovaf)
271 * @since 1.0.6
272 *
273 * @return string
274 */
275 function fs_find_caller_plugin_file() {
276 /**
277 * All the code below will be executed once on activation.
278 * If the user changes the main plugin's file name, the file_exists()
279 * will catch it.
280 */
281 if ( ! function_exists( 'get_plugins' ) ) {
282 require_once ABSPATH . 'wp-admin/includes/plugin.php';
283 }
284
285 $all_plugins = get_plugins();
286 $all_plugins_paths = array();
287
288 // Get active plugin's main files real full names (might be symlinks).
289 foreach ( $all_plugins as $relative_path => &$data ) {
290 $all_plugins_paths[] = fs_normalize_path( realpath( WP_PLUGIN_DIR . '/' . $relative_path ) );
291 }
292
293 $plugin_file = null;
294 for ( $i = 1, $bt = debug_backtrace(), $len = count( $bt ); $i < $len; $i ++ ) {
295 if ( in_array( fs_normalize_path( $bt[ $i ]['file'] ), $all_plugins_paths ) ) {
296 $plugin_file = $bt[ $i ]['file'];
297 break;
298 }
299 }
300
301 if ( is_null( $plugin_file ) ) {
302 // Throw an error to the developer in case of some edge case dev environment.
303 wp_die( __fs( 'failed-finding-main-path' ), __fs( 'error' ), array( 'back_link' => true ) );
304 }
305
306 return $plugin_file;
307 }
308
309 require_once dirname( __FILE__ ) . '/supplements/fs-essential-functions-1.1.7.1.php';
310
311 /**
312 * Update SDK newest version reference.
313 *
314 * @author Vova Feldman (@svovaf)
315 * @since 1.1.6
316 *
317 * @param string $sdk_relative_path
318 * @param string|bool $plugin_file
319 *
320 * @global $fs_active_plugins
321 */
322 function fs_update_sdk_newest_version( $sdk_relative_path, $plugin_file = false ) {
323 global $fs_active_plugins;
324
325 if ( ! is_string( $plugin_file ) ) {
326 $plugin_file = plugin_basename( fs_find_caller_plugin_file() );
327 }
328
329 $fs_active_plugins->newest = (object) array(
330 'plugin_path' => $plugin_file,
331 'sdk_path' => $sdk_relative_path,
332 'version' => $fs_active_plugins->plugins[ $sdk_relative_path ]->version,
333 'in_activation' => ! is_plugin_active( $plugin_file ),
334 'timestamp' => time(),
335 );
336
337 // Update DB with latest SDK version and path.
338 update_option( 'fs_active_plugins', $fs_active_plugins );
339 }
340
341 /**
342 * Reorder the plugins load order so the plugin with the newest Freemius SDK is loaded first.
343 *
344 * @author Vova Feldman (@svovaf)
345 * @since 1.1.6
346 *
347 * @return bool Was plugin order changed. Return false if plugin was loaded first anyways.
348 *
349 * @global $fs_active_plugins
350 */
351 function fs_newest_sdk_plugin_first() {
352 global $fs_active_plugins;
353
354 /**
355 * @todo Multi-site network activated plugin are always loaded prior to site plugins so if there's a a plugin activated in the network mode that has an older version of the SDK of another plugin which is site activated that has new SDK version, the fs-essential-functions.php will be loaded from the older SDK. Same thing about MU plugins (loaded even before network activated plugins).
356 *
357 * @link https://github.com/Freemius/wordpress-sdk/issues/26
358 */
359 // $active_sitewide_plugins = get_site_option( 'active_sitewide_plugins' );
360
361 $active_plugins = get_option( 'active_plugins' );
362 $newest_sdk_plugin_key = array_search( $fs_active_plugins->newest->plugin_path, $active_plugins );
363 if ( 0 == $newest_sdk_plugin_key ) {
364 // if it's 0 it's the first plugin already, no need to continue
365 return false;
366 }
367
368 array_splice( $active_plugins, $newest_sdk_plugin_key, 1 );
369 array_unshift( $active_plugins, $fs_active_plugins->newest->plugin_path );
370 update_option( 'active_plugins', $active_plugins );
371
372 return true;
373 }
374
375 /**
376 * Go over all Freemius SDKs in the system and find and "remember"
377 * the newest SDK which is associated with an active plugin.
378 *
379 * @author Vova Feldman (@svovaf)
380 * @since 1.1.6
381 *
382 * @global $fs_active_plugins
383 */
384 function fs_fallback_to_newest_active_sdk() {
385 global $fs_active_plugins;
386
387 /**
388 * @var object $newest_sdk_data
389 */
390 $newest_sdk_data = null;
391 $newest_sdk_path = null;
392
393 foreach ( $fs_active_plugins->plugins as $sdk_relative_path => $data ) {
394 if ( is_null( $newest_sdk_data ) || version_compare( $data->version, $newest_sdk_data->version, '>' )
395 ) {
396 // If plugin inactive or SDK starter file doesn't exist, remove SDK reference.
397 if ( ! is_plugin_active( $data->plugin_path ) ||
398 ! file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $sdk_relative_path . '/start.php' ) )
399 ) {
400 unset( $fs_active_plugins->plugins[ $sdk_relative_path ] );
401
402 // No need to store the data since it will be stored in fs_update_sdk_newest_version()
403 // or explicitly with update_option().
404 } else {
405 $newest_sdk_data = $data;
406 $newest_sdk_path = $sdk_relative_path;
407 }
408 }
409 }
410
411 if ( is_null( $newest_sdk_data ) ) {
412 // Couldn't find any SDK reference.
413 $fs_active_plugins = new stdClass();
414 update_option( 'fs_active_plugins', $fs_active_plugins );
415 } else {
416 fs_update_sdk_newest_version( $newest_sdk_path, $newest_sdk_data->plugin_path );
417 }
418 }
419
420 #region Actions / Filters -----------------------------------------
421
422 /**
423 * Apply filter for specific plugin.
424 *
425 * @author Vova Feldman (@svovaf)
426 * @since 1.0.9
427 *
428 * @param string $slug Plugin slug
429 * @param string $tag The name of the filter hook.
430 * @param mixed $value The value on which the filters hooked to `$tag` are applied on.
431 *
432 * @return mixed The filtered value after all hooked functions are applied to it.
433 *
434 * @uses apply_filters()
435 */
436 function fs_apply_filter( $slug, $tag, $value ) {
437 $args = func_get_args();
438
439 return call_user_func_array( 'apply_filters', array_merge(
440 array( "fs_{$tag}_{$slug}" ),
441 array_slice( $args, 2 ) )
442 );
443 }
444
445 #endregion Actions / Filters -----------------------------------------