Events
2 years ago
Purger
2 years ago
AHSC_Check.php
1 year ago
AHSC_Config.php
1 year ago
AHSC_Functions.php
1 year ago
AHSC_Static.php
1 year ago
AHSC_Version.php
2 years ago
AHSC_Warmer.php
2 years ago
index.php
2 years ago
AHSC_Functions.php
298 lines
| 1 | <?php |
| 2 | |
| 3 | use ArubaSPA\HiSpeedCache\Debug\Logger; |
| 4 | |
| 5 | /** |
| 6 | * operation at plugin activation |
| 7 | * @return void |
| 8 | */ |
| 9 | function AHSC_activation( ) { |
| 10 | // Get the option. |
| 11 | $check=AHSC_check(); |
| 12 | if(!$check['is_aruba_server']){ |
| 13 | AHSC_deactivate_me(); |
| 14 | wp_die( esc_html(ahsc_get_check_notice($check)), 'Aruba HiSpeed Cache dependency check', array( 'back_link' => true ) ); |
| 15 | }else{ |
| 16 | $options = AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']; |
| 17 | if ( ! $options ) { |
| 18 | $options = array_map( |
| 19 | function( $opt ) { |
| 20 | return $opt['default']; |
| 21 | }, |
| 22 | AHSC_OPTIONS_LIST_DEFAULT |
| 23 | ); |
| 24 | } |
| 25 | \update_site_option( AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS_NAME'] , $options ); |
| 26 | \update_site_option( 'aruba_hispeed_cache_version', AHSC_CONSTANT['ARUBA_HISPEED_CACHE_VERSION']); |
| 27 | |
| 28 | AHSC_remove_htaccess(); |
| 29 | if(array_key_exists('ahsc_static_cache',AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS'])){ |
| 30 | if(isset(AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_static_cache'])){ |
| 31 | if(AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_static_cache']){ |
| 32 | AHSC_edit_htaccess(); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | /** |
| 39 | * operation at plugin deactivation |
| 40 | * @return void |
| 41 | */ |
| 42 | function AHSC_deactivation( ) { |
| 43 | |
| 44 | \delete_site_option( 'aruba_hispeed_cache_options' ); |
| 45 | \delete_site_option( 'aruba_hispeed_cache_version'); |
| 46 | AHSC_remove_htaccess(); |
| 47 | } |
| 48 | /** |
| 49 | * operation at plugin check requirement |
| 50 | * @return void |
| 51 | */ |
| 52 | function AHSC_deactivate_me() { |
| 53 | if ( \function_exists( 'deactivate_plugins' ) ) { |
| 54 | \deactivate_plugins( AHSC_CONSTANT['ARUBA_HISPEED_CACHE_BASENAME']); |
| 55 | } |
| 56 | } |
| 57 | /** |
| 58 | * create Notice in plugin admin page |
| 59 | * |
| 60 | * @param $handle string notice html id |
| 61 | * @param $html_class string notice general class es error,warning etc.etc. |
| 62 | * @param $content string notice content |
| 63 | * |
| 64 | */ |
| 65 | function AHSC_Notice_Render( $handle, $html_class, $content, $return=false) { |
| 66 | $HTML_CLASS = array( |
| 67 | 'error' => 'notice notice-error', |
| 68 | 'warning' => 'notice notice-warning', |
| 69 | 'success' => 'notice notice-success', |
| 70 | 'info ' => 'notice notice-info', |
| 71 | ); |
| 72 | |
| 73 | if(!$return){ |
| 74 | printf( |
| 75 | '<div id="%1$s" class="%2$s"><p>%3$s</p></div>', |
| 76 | esc_attr( $handle ), |
| 77 | esc_attr( $HTML_CLASS[$html_class] ), |
| 78 | wp_kses_post( $content ) |
| 79 | ); |
| 80 | }else{ |
| 81 | return sprintf( |
| 82 | '<div id="%1$s" class="%2$s"><p>%3$s</p></div>', |
| 83 | esc_attr( $handle ), |
| 84 | esc_attr( $HTML_CLASS[$html_class] ), |
| 85 | wp_kses_post( $content ) |
| 86 | ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * create link in plugin page |
| 92 | * @return void |
| 93 | */ |
| 94 | function AHSC_plugin_action_links($actions){ |
| 95 | |
| 96 | $settings_link = \sprintf( |
| 97 | '<a href="%s">%s</a>', |
| 98 | (( ! is_multisite() ) ? admin_url( 'options-general.php?page=aruba-hispeed-cache') : network_admin_url('settings.php?page=aruba-hispeed-cache')) , |
| 99 | \esc_html(__( 'Settings', 'aruba-hispeed-cache' )) |
| 100 | ); |
| 101 | |
| 102 | $support_link = \sprintf( |
| 103 | '<a href="%s" target="_blank">%s</a>', |
| 104 | AHSC_LOCALIZE_LINK['link_assistance'][strtolower(substr( get_bloginfo ( 'language' ), 0, 2 ))], |
| 105 | \esc_html(__( 'Customer support', 'aruba-hispeed-cache' )) |
| 106 | ); |
| 107 | |
| 108 | \array_unshift( $actions, $settings_link, $support_link ); |
| 109 | |
| 110 | return $actions; |
| 111 | |
| 112 | } |
| 113 | |
| 114 | if ( ! \function_exists( 'ahsc_get_site_home_url' ) ) { |
| 115 | /** |
| 116 | * Return the complete home url of site. |
| 117 | * |
| 118 | * @return string |
| 119 | */ |
| 120 | function ahsc_get_site_home_url() { |
| 121 | $home_uri = \trailingslashit( \home_url() ); |
| 122 | |
| 123 | if ( \function_exists( 'icl_get_home_url' ) ) { |
| 124 | $home_uri = \trailingslashit( \icl_get_home_url() ); |
| 125 | } |
| 126 | |
| 127 | return $home_uri; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | if ( ! \function_exists( 'ahsc_save_options' ) ) { |
| 133 | /** |
| 134 | * Save plugin options |
| 135 | * |
| 136 | * @return void |
| 137 | */ |
| 138 | function ahsc_save_options( ) { |
| 139 | if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) { |
| 140 | if ( isset( $_POST['ahsc_settings_save'] ) && isset( $_POST['ahs-settings-nonce'] ) && \wp_verify_nonce( \sanitize_key( \wp_unslash( $_POST['ahs-settings-nonce'] ) ), 'ahs-save-settings-nonce' ) ) { |
| 141 | $new_options = array(); |
| 142 | |
| 143 | foreach ( array_keys( AHSC_OPTIONS_LIST ) as $opt_key ) { |
| 144 | $new_options[ $opt_key ] = ( isset( $_POST[ $opt_key ] ) ) ? true : false; |
| 145 | } |
| 146 | |
| 147 | if ( \update_site_option( AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS_NAME'], $new_options ) ) { |
| 148 | $content = \esc_html( __('Settings saved.', 'aruba-hispeed-cache') ); |
| 149 | AHSC_Notice_Render('ahs_settings_saved', 'success',$content); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | |
| 157 | if ( ! \function_exists( 'ahsc_has_transient' ) ) { |
| 158 | |
| 159 | /** |
| 160 | * It checks whether a transinet exists if yes, it returns the value otherwise it returns false. |
| 161 | * |
| 162 | * @param string $transient . |
| 163 | * @return mixed |
| 164 | */ |
| 165 | function ahsc_has_transient( $transient ) { |
| 166 | $transient_value = ( \is_multisite() ) ? \get_site_transient( (string) $transient ) : \get_transient( (string) $transient ); |
| 167 | return ( false !== $transient_value ) ? $transient_value : false; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if ( ! \function_exists( 'ahsc_set_transient' ) ) { |
| 172 | |
| 173 | /** |
| 174 | * Undocumented function |
| 175 | * |
| 176 | * @param string $transient . |
| 177 | * @param mixed $value . |
| 178 | * @param integer $expiration . |
| 179 | * @return bool |
| 180 | */ |
| 181 | function ahsc_set_transient( $transient, $value, $expiration = 0 ) { |
| 182 | return ( \is_multisite() ) ? |
| 183 | \set_site_transient( $transient, $value, $expiration ) : |
| 184 | \set_transient( $transient, $value, $expiration ); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if ( ! \function_exists( 'ahsc_delete_transient' ) ) { |
| 189 | |
| 190 | /** |
| 191 | * Undocumented function |
| 192 | * |
| 193 | * @param string $transient . |
| 194 | * @return bool |
| 195 | */ |
| 196 | function ahsc_delete_transient( $transient ) { |
| 197 | if(class_exists('ArubaSPA\HiSpeedCache\Debug\Logger')) { |
| 198 | AHSC_log( 'hook::transient::delete', __NAMESPACE__ . '::' . __FUNCTION__, 'debug' ); |
| 199 | // Logger. |
| 200 | } |
| 201 | return ( \is_multisite() ) ? |
| 202 | \delete_site_transient( $transient ) : |
| 203 | \delete_transient( $transient ); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | if ( ! \function_exists( 'ahsc_has_notice' ) ) { |
| 208 | |
| 209 | /** |
| 210 | * Return bool if transient notice check is set. |
| 211 | * |
| 212 | * @param bool $remove Set to true to clean the transinte if present. |
| 213 | * @return mixed |
| 214 | * |
| 215 | * @SuppressWarnings(PHPMD.StaticAccess) |
| 216 | */ |
| 217 | function ahsc_has_notice( $remove = null ) { |
| 218 | $has_notice = ahsc_has_transient( AHSC_CHECKER['transient_name'] ); |
| 219 | |
| 220 | if ( false !== $has_notice && true === $remove ) { |
| 221 | if ( \is_multisite() ) { |
| 222 | \delete_site_transient(AHSC_CHECKER['transient_name'] ); |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | \delete_transient( AHSC_CHECKER['transient_name']); |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | return $has_notice; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | if ( ! \function_exists( 'ahsc_get_debug_file_content' ) ) { |
| 235 | /** |
| 236 | * Return the contente of log file. |
| 237 | * |
| 238 | * @return string |
| 239 | */ |
| 240 | function ahsc_get_debug_file_content() { |
| 241 | global $wp_filesystem,$logger; |
| 242 | \WP_Filesystem(); |
| 243 | if (class_exists('ArubaSPA\HiSpeedCache\Debug\Logger') ) { |
| 244 | if ( \file_exists( Logger::get_log_file_path_name() ) ) { |
| 245 | return $wp_filesystem->get_contents( Logger::get_log_file_path_name() ); |
| 246 | } |
| 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if ( ! \function_exists('ahsc_current_theme_is_fse_theme' ) ) { |
| 253 | /** |
| 254 | * The function checks if the current theme is a Full Site Editing (FSE) theme in PHP. |
| 255 | * |
| 256 | * @return bool value indicating whether the current theme is a Full Site Editing (FSE) theme. |
| 257 | */ |
| 258 | function ahsc_current_theme_is_fse_theme() { |
| 259 | if ( function_exists( 'wp_is_block_theme' ) ) { |
| 260 | return (bool) wp_is_block_theme(); |
| 261 | } |
| 262 | if ( function_exists( 'gutenberg_is_fse_theme' ) ) { |
| 263 | return (bool) gutenberg_is_fse_theme(); |
| 264 | } |
| 265 | return false; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Write a debug info in to file. |
| 271 | * |
| 272 | * @param string $message the messagge. |
| 273 | * @param string $name the name of message. |
| 274 | * @param string $type the type of log debug|info|warning|error. |
| 275 | * @return void |
| 276 | */ |
| 277 | function AHSC_log( $message, $name = '', $type = 'info' ) { |
| 278 | |
| 279 | if ( class_exists('\ArubaSPA\HiSpeedCache\Debug\Logger') ) { |
| 280 | |
| 281 | //die(var_export(Logger::$logger_ready,true)); |
| 282 | switch ( $type ) { |
| 283 | case 'debug': |
| 284 | Logger::debug( $message, $name ); |
| 285 | break; |
| 286 | case 'info': |
| 287 | Logger::info( $message, $name ); |
| 288 | break; |
| 289 | case 'warning': |
| 290 | Logger::warning( $message, $name ); |
| 291 | break; |
| 292 | case 'error': |
| 293 | Logger::error( $message, $name ); |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 |