ppet */ public static function get_code_snippet( $module, $server_type = '', $expiry_times = array() ) { $module = Utils::get_module( $module ); if ( ! $module ) { return ''; } if ( ! $server_type ) { $server_type = self::get_server_type(); } return apply_filters( 'wphb_code_snippet', $module->get_server_code_snippet( $server_type, $expiry_times ), $server_type, $module ); } /** * Check if .htaccess is writable. * * @return bool */ public static function is_htaccess_writable() { if ( ! function_exists( 'get_home_path' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $home_path = get_home_path(); return ( ! file_exists( $home_path . '.htaccess' ) && is_writable( $home_path ) ) || is_writable( $home_path . '.htaccess' ); } /** * Check if .htaccess has Hummingbird caching or gzip rules in place. * * @param string $module Module slug. * * @return bool */ public static function is_htaccess_written( $module = '' ) { if ( ! function_exists( 'get_home_path' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } if ( ! function_exists( 'extract_from_markers' ) ) { require_once ABSPATH . 'wp-admin/includes/misc.php'; } $existing_rules = array_filter( extract_from_markers( get_home_path() . '.htaccess', 'WP-HUMMINGBIRD-' . strtoupper( $module ) ) ); return ! empty( $existing_rules ); } /** * Add rules .htaccess file. * * @param string $module Gzip or caching module. * * @return bool */ public static function save_htaccess( $module ) { if ( self::is_htaccess_written( $module ) ) { return false; } $htaccess_file = get_home_path() . '.htaccess'; if ( self::is_htaccess_writable() ) { $code = self::get_code_snippet( $module, 'apache' ); $code = explode( "\n", $code ); return insert_with_markers( $htaccess_file, 'WP-HUMMINGBIRD-' . strtoupper( $module ), $code ); } return false; } /** * Remove rules from .htaccess file. * * @param string $module Module. * * @return bool */ public static function unsave_htaccess( $module ) { if ( ! self::is_htaccess_written( $module ) ) { return false; } $htaccess_file = get_home_path() . '.htaccess'; if ( self::is_htaccess_writable() ) { return insert_with_markers( $htaccess_file, 'WP-HUMMINGBIRD-' . strtoupper( $module ), '' ); } return false; } }