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