-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-sync-deploy.tasks.example.php
66 lines (56 loc) · 1.87 KB
/
wp-sync-deploy.tasks.example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* This file can be used as a starting point for adding custom code
* that should be executed on the target server after each sync or deploy.
*
* @see https://github.com/hirasso/wp-sync-deploy#run-automated-tasks-after-each-deploy--sync-
*
* Debug this file for sync tasks:
* `wp eval-file wp-sync-deploy.tasks.php sync`
*
* Debug this file for deploy tasks:
* `wp eval-file wp-sync-deploy.tasks.php deploy`
*/
/** Feel free to change the namespace to whatever */
namespace WPSyncDeploy\Tasks;
/** Exit if accessed directly */
!defined('ABSPATH') && exit;
/** The web host of this install */
$host = wp_parse_url(home_url())['host'];
/** The current task, either 'sync' or 'deploy' */
$task = $args[0] ?? '';
!in_array($task, ['sync', 'deploy']) && \WP_CLI::error("\$task must either be 'sync' or 'deploy'");
/**
* Ask a question in WP_CLI
* @see https://www.ibenic.com/useful-interactive-prompts-wp-cli-commands/
* @see https://make.wordpress.org/cli/handbook/references/internal-api/wp-cli-colorize/#notes
*/
function ask(string $question, string $options = 'y/n')
{
fwrite(STDOUT, \WP_CLI::colorize("🙋 $question [$options] "));
return strtolower(trim(fgets(STDIN)));
}
/**
* Activate all plugins on the target environment
*/
if ($task === 'deploy') {
\WP_CLI::runcommand('plugin activate --all');
}
/**
* Clear the cache on this install
*/
if (ask("Do you want to clear the cache on '$host'?") === 'y') {
// delete all transients
\WP_CLI::runcommand('transient delete --all');
// delete the Super Cache files if the function exists
if (function_exists('wp_cache_clear_cache')) {
wp_cache_clear_cache();
\WP_CLI::success('Cleared the cache');
}
}
/**
* Flush the rewrite rules
*/
if ($task === 'deploy' && ask("Do you want to flush the rewrite rules on '$host'?") === 'y') {
\WP_CLI::runcommand('rewrite flush');
}