marcoszf
12/27/2018 - 4:16 PM

wp-automating-local-site.php

<?php
//Scripts(em WP-CLI) para automação de criar ou deletar rapidamente sites em Wordpress em localhost.

//Crie um arquivo php:

//Confere se WP-CLI está disponível e se sim adiciona 'installer' aos comandos do qual há 2 subcomandos :install e uninstall
if ( defined( 'WP_CLI' ) && WP_CLI ) {
    WP_CLI::add_command( 'installer', ‘WP_CLI_Installer’, array( ‘when’ => ‘before_wp_load’ );
}
/**
 * Quickly manage WordPress installations.
 *
 * Usages:
 *
 * wp installer install
 * wp installer uninstall
 *
 **/
class WP_CLI_Installer {
    
    public function install( $args, $assoc_args ) {
      $base_path = isset( $assoc_args['base_path'] ) ? $assoc_args['base_path'] : getcwd();
      $site_path = $base_path . '/' . $args[0];
      $dbuser    = $assoc_args['dbuser'];
      $dbpass    = $assoc_args['dbpass'];
      $dbhost    = $assoc_args['dbhost'];

      // Download WordPress
      $download = "wp core download --path=%s";
      WP_CLI::log( 'Downloading WordPress...' );
      WP_CLI::launch( \WP_CLI\Utils\esc_cmd( $download, $site_path ) );

      // Create the wp-config file
      $config = "wp --path=%s core config --dbname=%s --dbuser=%s --dbpass=%s --dbhost=%s";
      WP_CLI::log( 'Creating wp-config.php...' );
      WP_CLI::launch( \WP_CLI\Utils\esc_cmd( $config, $site_path, $args[0], $dbuser, $dbpass, $dbhost ) );

      // Create the database
      $db_create = "wp --path=%s db create";
      WP_CLI::log( 'Creating the database...' );
      WP_CLI::launch( \WP_CLI\Utils\esc_cmd( $db_create, $site_path ) );

      // Install WordPress core.
      $admin_user  = $assoc_args['admin_user'];
      $admin_pass  = $assoc_args['admin_password'];
      $admin_email = $assoc_args['admin_email'];
      $subcommand  = 'install';
      $base_url    = $assoc_args['base_url'];

      if ( isset( $assoc_args['multisite'] ) ) {
          $subcommand = 'multisite-install';
      }

      $core_install = "wp --path=%s core %s --url=%s --title=%s --admin_user=%s --admin_password=%s --admin_email=%s";
      WP_CLI::log( 'Installing WordPress...' );
      WP_CLI::launch( \WP_CLI\Utils\esc_cmd( $core_install, $site_path, $subcommand, $base_url . $args[0], $args[0], $admin_user, $admin_pass, $admin_email ) );

      WP_CLI::success( "WordPress installed at $site_path" );
    }
    /**
    * O comando WP_CLI::launch() inicia diversos comandos WP-CLI existentes que são responsáveis por Baixar o Wordpress, 
    * criar o wp-config, e criar/instalar o banco de dados
    **/
    
      //Criar o subcomando uninstall é ainda mais fácil
    public function uninstall( $args, $assoc_args ) {
      $base_path = isset( $assoc_args['base_path'] ) ? $assoc_args['base_path'] : getcwd();
      $site_path = $base_path . '/' . $args[0];

      // Let's make sure we really want to do this
      if ( ! isset( $assoc_args['yes'] ) ) {
          WP_CLI::confirm( 'Are you sure you want to proceed? Data WILL be lost!', $assoc_args );
      }

      // Drop the database
      $db_drop = "wp --path=%s db drop --yes";
      WP_CLI::log( 'Dropping database...' );
      WP_CLI::launch( \WP_CLI\Utils\esc_cmd( $db_drop, $site_path ) );

      // Remove the files
      $remove_files = "rm -rf %s";
      WP_CLI::log( 'Removing files...' );
      WP_CLI::launch( \WP_CLI\Utils\esc_cmd( $remove_files, $site_path ) );

      WP_CLI::success( "Uninstalled WordPress from $site_path" );
    }

}

?>