ajax-handlers.php
6 months ago
class-configurate.php
1 year ago
class-helpers.php
1 year ago
class-search-options.php
1 year ago
index.php
4 years ago
class-helpers.php
284 lines
| 1 | <?php |
| 2 | namespace WBCR\Factory_Templates_134; |
| 3 | |
| 4 | // Exit if accessed directly |
| 5 | if( !defined('ABSPATH') ) { |
| 6 | exit; |
| 7 | } |
| 8 | |
| 9 | |
| 10 | /** |
| 11 | * Helpers functions |
| 12 | * |
| 13 | * @author Alex Kovalev <alex.kovalevv@gmail.com>, Github: https://github.com/alexkovalevv |
| 14 | * @since 1.0.0 |
| 15 | * @package clearfy |
| 16 | * @copyright (c) 2018, Webcraftic Ltd |
| 17 | * |
| 18 | */ |
| 19 | class Helpers { |
| 20 | |
| 21 | /** |
| 22 | * Recursive sanitation for an array |
| 23 | * |
| 24 | * @param $array |
| 25 | * |
| 26 | * @return mixed |
| 27 | * @since 2.0.5 |
| 28 | * |
| 29 | */ |
| 30 | public static function recursiveSanitizeArray($array, $function) |
| 31 | { |
| 32 | foreach($array as $key => &$value) { |
| 33 | if( is_array($value) ) { |
| 34 | $value = self::recursiveSanitizeArray($value, $function); |
| 35 | } else { |
| 36 | if( function_exists($function) ) { |
| 37 | $value = $function($value); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return $array; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Is permalink enabled? |
| 47 | * |
| 48 | * @return bool |
| 49 | * @since 1.0.0 |
| 50 | * @global \WP_Rewrite $wp_rewrite |
| 51 | */ |
| 52 | public static function isPermalink() |
| 53 | { |
| 54 | global $wp_rewrite; |
| 55 | |
| 56 | if( !isset($wp_rewrite) || !is_object($wp_rewrite) || !$wp_rewrite->using_permalinks() ) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Display 404 page to bump bots and bad guys |
| 65 | * |
| 66 | * @param bool $simple If true force displaying basic 404 page |
| 67 | */ |
| 68 | public static function setError404() |
| 69 | { |
| 70 | global $wp_query; |
| 71 | |
| 72 | if( function_exists('status_header') ) { |
| 73 | status_header('404'); |
| 74 | nocache_headers(); |
| 75 | } |
| 76 | |
| 77 | if( $wp_query && is_object($wp_query) ) { |
| 78 | $wp_query->set_404(); |
| 79 | get_template_part(404); |
| 80 | } else { |
| 81 | global $pagenow; |
| 82 | |
| 83 | $pagenow = 'index.php'; |
| 84 | |
| 85 | if( !defined('WP_USE_THEMES') ) { |
| 86 | define('WP_USE_THEMES', true); |
| 87 | } |
| 88 | |
| 89 | wp(); |
| 90 | |
| 91 | $_SERVER['REQUEST_URI'] = self::userTrailingslashit('/hmwp_404'); |
| 92 | |
| 93 | require_once(ABSPATH . WPINC . '/template-loader.php'); |
| 94 | } |
| 95 | |
| 96 | exit(); |
| 97 | } |
| 98 | |
| 99 | public static function useTrailingSlashes() |
| 100 | { |
| 101 | return ('/' === substr(get_option('permalink_structure'), -1, 1)); |
| 102 | } |
| 103 | |
| 104 | public static function userTrailingslashit($string) |
| 105 | { |
| 106 | return self::useTrailingSlashes() ? trailingslashit($string) : untrailingslashit($string); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns true if a needle can be found in a haystack |
| 111 | * |
| 112 | * @param string $string |
| 113 | * @param string $find |
| 114 | * @param bool $case_sensitive |
| 115 | * |
| 116 | * @return bool |
| 117 | */ |
| 118 | public static function strContains($string, $find, $case_sensitive = true) |
| 119 | { |
| 120 | if( empty($string) || empty($find) ) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | $pos = $case_sensitive ? strpos($string, $find) : stripos($string, $find); |
| 125 | |
| 126 | return !($pos === false); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Tests if a text starts with an given string. |
| 131 | * |
| 132 | * @param string $string |
| 133 | * @param string $find |
| 134 | * @param bool $case_sensitive |
| 135 | * |
| 136 | * @return bool |
| 137 | */ |
| 138 | public static function strStartsWith($string, $find, $case_sensitive = true) |
| 139 | { |
| 140 | if( $case_sensitive ) { |
| 141 | return strpos($string, $find) === 0; |
| 142 | } |
| 143 | |
| 144 | return stripos($string, $find) === 0; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Tests if a text ends with an given string. |
| 149 | * |
| 150 | * @param $string |
| 151 | * @param $find |
| 152 | * @param bool $case_sensitive |
| 153 | * |
| 154 | * @return bool |
| 155 | */ |
| 156 | public static function strEndsWith($string, $find, $case_sensitive = true) |
| 157 | { |
| 158 | $expected_position = strlen($string) - strlen($find); |
| 159 | |
| 160 | if( $case_sensitive ) { |
| 161 | return strrpos($string, $find, 0) === $expected_position; |
| 162 | } |
| 163 | |
| 164 | return strripos($string, $find, 0) === $expected_position; |
| 165 | } |
| 166 | |
| 167 | public static function arrayMergeInsert(array $arr, array $inserted, $position = 'bottom', $key = null) |
| 168 | { |
| 169 | if( $position == 'top' ) { |
| 170 | return array_merge($inserted, $arr); |
| 171 | } |
| 172 | $key_position = ($key === null) ? false : array_search($key, array_keys($arr)); |
| 173 | if( $key_position === false or ($position != 'before' and $position != 'after') ) { |
| 174 | return array_merge($arr, $inserted); |
| 175 | } |
| 176 | if( $position == 'after' ) { |
| 177 | $key_position++; |
| 178 | } |
| 179 | |
| 180 | return array_merge(array_slice($arr, 0, $key_position, true), $inserted, array_slice($arr, $key_position, null, true)); |
| 181 | } |
| 182 | |
| 183 | public static function maybeGetPostJson($name) |
| 184 | { |
| 185 | if( isset($_POST[$name]) and is_string($_POST[$name]) ) { |
| 186 | $result = json_decode(stripslashes($_POST[$name]), true); |
| 187 | if( !is_array($result) ) { |
| 188 | $result = []; |
| 189 | } |
| 190 | |
| 191 | return $result; |
| 192 | } else { |
| 193 | return []; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | public static function getEscapeJson(array $data) |
| 198 | { |
| 199 | return htmlspecialchars(json_encode($data), ENT_QUOTES, 'UTF-8'); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Replace url for multisite |
| 204 | * |
| 205 | * @param $string |
| 206 | * |
| 207 | * @return string |
| 208 | */ |
| 209 | public static function replaceMsUrl($string) |
| 210 | { |
| 211 | if( is_multisite() && BLOG_ID_CURRENT_SITE != get_current_blog_id() ) { |
| 212 | return str_replace(get_site_url(BLOG_ID_CURRENT_SITE), get_site_url(get_current_blog_id()), $string); |
| 213 | } |
| 214 | |
| 215 | return $string; |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Flushes as many page cache plugin's caches as possible. |
| 220 | * |
| 221 | * @return void |
| 222 | */ |
| 223 | public static function flushPageCache() |
| 224 | { |
| 225 | if( function_exists('wp_cache_clear_cache') ) { |
| 226 | if( is_multisite() ) { |
| 227 | $blog_id = get_current_blog_id(); |
| 228 | wp_cache_clear_cache($blog_id); |
| 229 | } else { |
| 230 | wp_cache_clear_cache(); |
| 231 | } |
| 232 | } else if( has_action('cachify_flush_cache') ) { |
| 233 | do_action('cachify_flush_cache'); |
| 234 | } else if( function_exists('w3tc_pgcache_flush') ) { |
| 235 | w3tc_pgcache_flush(); |
| 236 | } else if( function_exists('wp_fast_cache_bulk_delete_all') ) { |
| 237 | wp_fast_cache_bulk_delete_all(); |
| 238 | } else if( class_exists('\WpFastestCache') ) { |
| 239 | $wpfc = new \WpFastestCache(); |
| 240 | $wpfc->deleteCache(); |
| 241 | } else if( class_exists('c_ws_plugin__qcache_purging_routines') ) { |
| 242 | \c_ws_plugin__qcache_purging_routines::purge_cache_dir(); // quick cache |
| 243 | } else if( class_exists('zencache') ) { |
| 244 | \zencache::clear(); |
| 245 | } else if( class_exists('comet_cache') ) { |
| 246 | \comet_cache::clear(); |
| 247 | } else if( class_exists('\WCL_Cache_Helpers') ) { |
| 248 | \WCL_Cache_Helpers::deleteCache(); |
| 249 | } else if( class_exists('\WpeCommon') ) { |
| 250 | // WPEngine cache purge/flush methods to call by default |
| 251 | $wpe_methods = [ |
| 252 | 'purge_varnish_cache', |
| 253 | ]; |
| 254 | |
| 255 | // More agressive clear/flush/purge behind a filter |
| 256 | if( apply_filters('wbcr/factory/flush_wpengine_aggressive', false) ) { |
| 257 | $wpe_methods = array_merge($wpe_methods, ['purge_memcached', 'clear_maxcdn_cache']); |
| 258 | } |
| 259 | |
| 260 | // Filtering the entire list of WpeCommon methods to be called (for advanced usage + easier testing) |
| 261 | $wpe_methods = apply_filters('wbcr/factory/wpengine_methods', $wpe_methods); |
| 262 | |
| 263 | foreach($wpe_methods as $wpe_method) { |
| 264 | if( method_exists('WpeCommon', $wpe_method) ) { |
| 265 | \WpeCommon::$wpe_method(); |
| 266 | } |
| 267 | } |
| 268 | } else if( function_exists('sg_cachepress_purge_cache') ) { |
| 269 | sg_cachepress_purge_cache(); |
| 270 | } else if( file_exists(WP_CONTENT_DIR . '/wp-cache-config.php') && function_exists('prune_super_cache') ) { |
| 271 | // fallback for WP-Super-Cache |
| 272 | global $cache_path; |
| 273 | if( is_multisite() ) { |
| 274 | $blog_id = get_current_blog_id(); |
| 275 | prune_super_cache(get_supercache_dir($blog_id), true); |
| 276 | prune_super_cache($cache_path . 'blogs/', true); |
| 277 | } else { |
| 278 | prune_super_cache($cache_path . 'supercache/', true); |
| 279 | prune_super_cache($cache_path, true); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 |