Skip to content

✨️ Add cache update feature #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions src/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,18 @@ private function enable_object_cache() {
$this->docker_compose_exec( $enable_redis_cache, 'Unable to enable object cache' );
}

/**
* Disable object cache.
*/
private function disable_object_cache() {

$redis_plugin_constant = 'docker-compose exec --user=\'www-data\' php wp config delete --type=variable redis_server';
$uninstall_wp_redis_plugin = "docker-compose exec --user='www-data' php wp plugin uninstall wp-redis --deactivate";

$this->docker_compose_exec( $uninstall_wp_redis_plugin, 'Unable to uninstall wp-redis plugin.' );
$this->docker_compose_exec( $redis_plugin_constant, 'Unable to remove redis_server variable from wp-config.' );
}

/**
* Enable page cache.
*/
Expand Down Expand Up @@ -504,6 +516,34 @@ private function enable_page_cache() {
$this->docker_compose_exec( $add_redis_maxttl, $nginx_helper_fail_msg );
}

/**
* Disable page cache.
*/
private function disable_page_cache() {
$uninstall_nginx_helper = 'docker-compose exec --user=\'www-data\' php wp plugin uninstall nginx-helper --deactivate';
$nginx_helper_fail_msg = 'Unable to uninstall nginx-helper plugin properly.';

$wp_cli_params = ( 'wp' === $this->site_data['app_sub_type'] ) ? 'option delete' : 'network meta delete 1';

$delete_hostname_constant = "docker-compose exec --user='www-data' php wp config delete RT_WP_NGINX_HELPER_REDIS_HOSTNAME --type=constant";
$delete_port_constant = "docker-compose exec --user='www-data' php wp config delete RT_WP_NGINX_HELPER_REDIS_PORT --type=constant";
$delete_prefix_constant = "docker-compose exec --user='www-data' php wp config delete RT_WP_NGINX_HELPER_REDIS_PREFIX --type=constant";
$delete_cache_key_salt = "docker-compose exec --user='www-data' php wp config delete WP_CACHE_KEY_SALT --type=constant";
$delete_redis_maxttl = "docker-compose exec --user='www-data' php wp config delete WP_REDIS_MAXTTL --type=constant";
$delete_plugin_data = "docker-compose exec --user='www-data' php wp $wp_cli_params rt_wp_nginx_helper_options";

$constant_remove_error = 'Unable to remove %s constant from wp-config.yml';
$delete_plugin_data_error = 'Unable to remove rt_wp_nginx_helper_options from options/network meta';

$this->docker_compose_exec( $uninstall_nginx_helper, $nginx_helper_fail_msg );
$this->docker_compose_exec( $delete_hostname_constant, sprintf( $constant_remove_error, 'RT_WP_NGINX_HELPER_REDIS_HOSTNAME' ) );
$this->docker_compose_exec( $delete_port_constant, sprintf( $constant_remove_error, 'RT_WP_NGINX_HELPER_REDIS_PORT' ) );
$this->docker_compose_exec( $delete_prefix_constant, sprintf( $constant_remove_error, 'RT_WP_NGINX_HELPER_REDIS_PREFIX' ) );
$this->docker_compose_exec( $delete_cache_key_salt, sprintf( $constant_remove_error, 'WP_CACHE_KEY_SALT' ) );
$this->docker_compose_exec( $delete_plugin_data, sprintf( $constant_remove_error, 'WP_REDIS_MAXTTL' ) );
$this->docker_compose_exec( $delete_redis_maxttl, $delete_plugin_data_error );
}

/**
* Execute command with fail msg.
*
Expand Down Expand Up @@ -1447,6 +1487,66 @@ protected function update_ssl( $assoc_args ) {
}
}

/**
* Update cache of a site
*
* @param array|bool $assoc_args Associate arguments passed to update command
*/
protected function update_cache( $assoc_args )
{
parent::update_cache( $assoc_args );

$cache = get_flag_value( $assoc_args, 'cache', false );
$local_cache = get_flag_value( $assoc_args, 'with-local-redis' );

if ( $cache === 'on' ) {
$cache = true;
} elseif( $cache === 'off') {
$cache = false;
}

$this->site_data->cache_host = $cache ? $local_cache ? 'redis' : 'global-redis' : false;
$this->site_data->cache_nginx_browser = $cache;
$this->site_data->cache_nginx_fullpage = $cache;
$this->site_data->cache_mysql_query = $cache;
$this->site_data->cache_app_object = $cache;
$this->cache_type = $cache;

$site_conf_dir = $this->site_data->site_fs_path . '/config';
$site_nginx_default_conf = $site_conf_dir . '/nginx/conf.d/main.conf';
$server_name = implode( ' ', explode( ',', $this->site_data->alias_domains ) );
$default_conf_content = $this->generate_default_conf( $this->site_data->site_type, $this->cache_type, $server_name );
$this->fs->dumpFile( $site_nginx_default_conf, $default_conf_content );

$this->dump_docker_compose_yml( [ 'nohttps' => ! $this->site_data->site_ssl ] );

$containers = ['nginx', 'php'];

if ( $local_cache ) {
$containers[] = 'redis';
}

chdir( $this->site_data->site_fs_path );

if ( $cache ) {
$this->enable_object_cache();
$this->enable_page_cache();
} else {
$this->disable_object_cache();
$this->disable_page_cache();
}

\EE\Site\Utils\stop_site_containers( $this->site_data->site_fs_path, [] );
\EE\Site\Utils\start_site_containers( $this->site_data->site_fs_path, $containers );

$this->site_data->save();

$action = $cache ? 'Enabled' : 'Disabled';
$local_cache_message = $local_cache ? ' with local cache' : '';

EE::success( $action . ' cache for ' . $this->site_data->site_url . $local_cache_message );
}

/**
* Restarts containers associated with site.
* When no service(--nginx etc.) is specified, all site containers will be restarted.
Expand Down