From 720a1c31a4b9d85261473aaac2312280906ff681 Mon Sep 17 00:00:00 2001 From: Moris Zen Date: Sun, 24 Jun 2018 12:01:25 +0200 Subject: [PATCH] Init commit --- .gitignore | 3 + .nvmrc | 1 + README.md | 24 +++++ RoboFile.php | 236 ++++++++++++++++++++++++++++++++++++++++++++++++ install.sh | 23 +++++ itermocil.yml | 12 +++ mysql_config.sh | 15 +++ package.json | 8 ++ phpcs.xml | 98 ++++++++++++++++++++ robo.yml | 19 ++++ wp-cli.yml | 3 + yarn.lock | 4 + 12 files changed, 446 insertions(+) create mode 100644 .gitignore create mode 100644 .nvmrc create mode 100644 README.md create mode 100644 RoboFile.php create mode 100755 install.sh create mode 100644 itermocil.yml create mode 100755 mysql_config.sh create mode 100644 package.json create mode 100644 phpcs.xml create mode 100644 robo.yml create mode 100644 wp-cli.yml create mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..05ea3b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +npm-debug.log +yarn-error.log \ No newline at end of file diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..c793025 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +7 \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ae11266 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +## WordPress Backend + +Before you install WordPress, make sure you have all the required software installed for your operating system. + +### Prerequisites + +* **OS X:** You'll need [Homebrew](https://brew.sh/) and [Yarn](https://yarnpkg.com/en/) installed. +* **Windows:** To install under Windows you need to be running the _64-bit version of Windows 10 Anniversary Update or later (build 1607+)_. The [Linux Subsystem for Windows](https://msdn.microsoft.com/en-us/commandline/wsl/install_guide) should be installed and enabled before proceeding. Then, you'll need the prerequisites for Ubuntu Linux, detailed below, set up. +* **Ubuntu Linux:** You'll need the latest version of NodeJS, Yarn and debconf-utils installed first. Follow this [simple guide](https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions) to get the latest version of NodeJS installed. Install the rest of the packages using the `apt-get` package manager. _Note: During the WordPress installation, you may be asked to enter the root password at the prompt due to the use of the `sudo` command_ + +### Install + +The following command will get WordPress running locally on your machine, along with the WordPress plugins you'll need to create and serve custom data via the WP REST API. + +```zsh +> yarn install && yarn start +``` + +When the installation process completes successfully: + +* The WordPress REST API is available at [http://localhost:8080](http://localhost:8080) +* The WordPress GraphQL API is available at [http://localhost:8080/graphql](http://localhost:8080/graphql) +* The WordPress admin is at [http://localhost:8080/wp-admin/](http://localhost:8080/wp-admin/) + diff --git a/RoboFile.php b/RoboFile.php new file mode 100644 index 0000000..3fd9f4e --- /dev/null +++ b/RoboFile.php @@ -0,0 +1,236 @@ + 'saburly4dm1n', + 'wp-pw' => 'U2r6axymsh4be2', + 'wp-theme-dir' => 'saburly-headless', + 'wp-theme-name' => 'Saburly Headless WP', + 'wp-email' => 'info@saburly.com', + 'wp-db-name' => 'wp_saburly', + 'wp-description' => 'We love beautiful code', + 'wp-plugins' => [], + ] + ) { + $confirm = $this->io()->confirm( 'This will replace your current ' . + 'WordPress install. Are you sure you want to do this?', false ); + + if ( ! $confirm ) { + return; + } + + $uname = php_uname(); + $is_darwin = ( strpos( $uname, 'Darwin' ) === 0 ); + $config_db_answer = $this->ask( + "Do you have an existing database you'd like to use and configure yourself? (y/n): " + ); + $db_ip = ''; + $db_pass = ''; + if ( 'y' === $config_db_answer ) { + $db_ip = $this->ask( 'Database IP address (press Enter for default value [0.0.0.0]): ' ); + $db_pass = $this->ask( 'Database root password (press Enter for default value [root]): ' ); + if ( $is_darwin ) { + $this->_exec( 'mysql.server start' ); + } else { + $this->_exec( 'sudo service mysql start' ); + } + } else { + if ( $is_darwin ) { + $this->_exec( 'brew install mysql' ); + $this->_exec( 'mysql.server start' ); + $this->_exec( './mysql_config.sh' ); + } else { + $this->_exec( + "echo 'mysql-server mysql-server/root_password_again password root' | sudo debconf-set-selections" + ); + $this->_exec( + "echo 'mysql-server mysql-server/root_password_again password root' | sudo debconf-set-selections" + ); + $this->_exec( 'sudo apt-get -y install mysql-server' ); + $this->_exec( 'sudo usermod -d /var/lib/mysql/ mysql' ); + $this->_exec( 'sudo service mysql start' ); + } + } + + if ( !$db_pass || strlen( $db_pass ) === 0 ) { + $db_pass = 'root'; + } + + if ( !$db_ip || strlen( $db_ip ) === 0 ) { + $db_ip = '0.0.0.0'; + } + + $this->_exec( + 'mysql -uroot -p' . $db_pass . ' -h ' . $db_ip . " -e 'create user if not exists " + . $opts['wp-db-name'] . "'" + ); + $this->_exec( + 'mysql -uroot -p' . $db_pass . ' -h ' . $db_ip + . " -e 'create database if not exists " . $opts['wp-db-name'] . "'" + ); + $this->_exec( + 'mysql -uroot -p' . $db_pass . ' -h ' . $db_ip . ' -e "grant all privileges on ' . $opts['wp-db-name'] + . '.* to ' . $opts['wp-db-name'] . "@localhost identified by '" . $opts['wp-db-name'] . "'\"" + ); + + $this->_exec( 'mysql -uroot -p' . $db_pass . ' -h ' . $db_ip . " -e 'flush privileges'" ); + + $this->wp( 'core download --version=4.9.5 --locale=en_US --force' ); + $this->wp( + 'core config --dbname=' . $opts['wp-db-name'] . ' --dbuser=' . $opts['wp-db-name'] . ' --dbpass=' + . $opts['wp-db-name'] . ' --dbhost=' . $db_ip + ); + $this->wp( 'db drop --yes' ); + $this->wp( 'db create' ); + + $install_command = implode( ' ', [ + 'core install', + '--url=localhost:8080', + '--title="' . $opts['wp-theme-name'] . '"', + '--admin_user="' . $opts['wp-user'] . '"', + '--admin_password="' . $opts['wp-pw'] . '"', + '--admin_email="' . $opts['wp-email'] . '"', + '--skip-email', + ] ); + + $this->wp( $install_command ); + + $this->wp( 'theme activate ' . $opts['wp-theme-dir'] ); + $this->wp( 'theme delete twentyfourteen' ); + $this->wp( 'theme delete twentyfifteen' ); + $this->wp( 'theme delete twentysixteen' ); + $this->wp( 'theme delete twentyseventeen' ); + + $this->wp( 'plugin delete akismet' ); + $this->wp( 'plugin delete hello' ); + + if ( is_array( $opts['wp-plugins'] ) && count( $opts['wp-plugins'] ) > 0 ) { + $installed_plugin_directories = $opts['wp-plugins']; + } else { + $installed_plugins = array_filter( glob( WP_DIR . '/wp-content/plugins/*' ), 'is_dir' ); + $installed_plugin_directories = array_filter( + str_replace( + WP_DIR . '/wp-content/plugins/', + '', + $installed_plugins + ) + ); + } + + if ( count( $installed_plugin_directories ) > 0 ) { + $plugins_command = 'plugin activate ' . ( implode( ' ', $installed_plugin_directories ) ); + $this->wp( $plugins_command ); + } + + // Sync ACF + $this->wp( 'acf sync' ); + + // Pretty URL structure required for wp-json path to work correctly + $this->wp( 'rewrite structure "/%year%/%monthnum%/%day%/%postname%/"' ); + + // Set the site description + $this->wp( 'option update blogdescription "' . $opts['wp-description'] . '"' ); + + // Update the Hello World post + $this->wp( + 'post update 1 wp-content/themes/saburly-headless/post-content/sample-post.txt '. + '--post_title="Sample Post" --post_name=sample-post' + ); + + // Create homepage content + $this->wp( + 'post create wp-content/themes/saburly-headless/post-content/welcome.txt '. + '--post_type=page --post_status=publish --post_name=welcome '. + '--post_title="Congratulations!"' + ); + + // Update the default 'Uncategorized' category name to make it more menu-friendly + $this->wp( 'term update category 1 --name="Sample Category"' ); + + // Set up example menu + $this->wp( 'menu create "Header Menu"' ); + $this->wp( 'menu item add-post header-menu 1' ); + $this->wp( 'menu item add-post header-menu 2' ); + $this->wp( 'menu item add-term header-menu category 1' ); + $this->wp( 'menu location assign header-menu header-menu' ); + + $this->io()->success( + 'Great. You can now log into WordPress at: http://localhost:8080/wp-admin (' + . $opts['wp-user'] . '/' . $opts['wp-pw'] . ')' + ); + } + + /** + * Start WordPress server. + * + * @return void + */ + public function server() { + $this->wp( 'server' ); + } + + /** + * Import WordPress data. + * + * @param array $opts options + * @return void + */ + public function wordpressImport( $opts = [ + 'migratedb-license' => null, + 'migratedb-from' => null, + ] ) { + if ( isset( $opts['migratedb-license'] ) ) { + $this->wp( 'migratedb setting update license ' . $opts['migratedb-license'] ); + } else { + $this->say( 'WP Migrate DB Pro: no license available. Please set migratedb-license in the robo.yml file.' ); + return; + } + + if ( isset( $opts['migratedb-from'] ) ) { + $command = 'WPMDB_EXCLUDE_RESIZED_MEDIA=1 wp migratedb pull '; + $command .= $opts['migratedb-from']; + $command .= ' --backup=prefix '; + $command .= ' --media=compare '; + $this->io()->success( 'About to run data migration from ' . $opts['migratedb-from'] ); + $this->taskExec( $command )->run(); + // Set siteurl and home + $this->wp( 'option update siteurl http://localhost:8080' ); + $this->wp( 'option update home http://localhost:8080' ); + } else { + $this->say( + 'WP Migrate DB Pro: No source installation specified. Please set migratedb-from in the robo.yml file.' + ); + return; + } + } + + /** + * Run WordPress task. + * + * @param arr $arg Arguments + * @return void + */ + public function wp( $arg ) { + $this->taskExec( 'wp' ) + ->dir( WP_DIR ) + ->rawArg( $arg ) + ->run(); + } +} \ No newline at end of file diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..8c03970 --- /dev/null +++ b/install.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +if [[ "$OSTYPE" == "linux-gnu" ]]; then + # Install PHP and PHP MySQL Plugin + sudo apt-get -y install php php-mysql mysql-client + # Download and install wp-cli + wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar + sudo chmod +x wp-cli.phar + sudo mv wp-cli.phar /usr/local/bin/wp + # Download and install robo + wget http://robo.li/robo.phar + sudo mv robo.phar /usr/bin/robo && sudo chmod +x /usr/bin/robo +elif [[ "$OSTYPE" == "darwin"* ]]; then + # Download and install wp-cli + brew install wp-cli + # Download and install wget + brew install wget + # Download and install robo + wget http://robo.li/robo.phar + sudo mv robo.phar /usr/local/bin/robo && sudo chmod +x /usr/local/bin/robo +else + echo "Sorry, this installation script only works on Mac OS X and Ubuntu Linux. Looks like your operating system is $OSTYPE." +fi diff --git a/itermocil.yml b/itermocil.yml new file mode 100644 index 0000000..680ff06 --- /dev/null +++ b/itermocil.yml @@ -0,0 +1,12 @@ +windows: + - name: WordPress + React + # Create a symlink in your home directory to the actual location of your code + # ln -s ~/path/to/code/for/saburly-wp . + root: ~/saburly-wp/ + layout: main-vertical + panes: + - wp server + - commands: + - cd frontend && yarn install && yarn run dev + name: 'git' + - git status \ No newline at end of file diff --git a/mysql_config.sh b/mysql_config.sh new file mode 100755 index 0000000..bc5b51c --- /dev/null +++ b/mysql_config.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +if [[ "$OSTYPE" == "darwin"* ]]; then + # If there's no password set for MySQL (first-time install), add one and cleanup + if mysql -uroot; then + mysql -uroot <<_EOF_ + UPDATE mysql.user SET authentication_string=PASSWORD('root') WHERE User='root'; + DELETE FROM mysql.user WHERE User=''; + DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1'); + DROP DATABASE IF EXISTS test; + DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'; + FLUSH PRIVILEGES; +_EOF_ + fi +fi diff --git a/package.json b/package.json new file mode 100644 index 0000000..b4b1508 --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "name": "saburly-headless", + "version": "0.1.0", + "repository": "git@gitlab.com:website-saburly/v2-backend.git", + "author": "Saburly", + "license": "UNLICENSED", + "private": true +} \ No newline at end of file diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..162ffa0 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,98 @@ + + + + PHP coding standards configuration. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/robo.yml b/robo.yml new file mode 100644 index 0000000..e97ef21 --- /dev/null +++ b/robo.yml @@ -0,0 +1,19 @@ +command: + wordpress: + setup: + options: + wp-user: saburly4dm1n + wp-pw: U2r6axymsh4be2 + wp-theme-dir: saburly-headless + wp-theme-name: Saburly Headless WP + wp-email: info@saburly.com + wp-db-name: wp_saburly + wp-description: We love beautiful code + # wp-plugins is optional + # If not set, Robo will activate all the plugins that are installed. + # wp-plugins: + import: + options: + migratedb-license: + # Use http vs https to avoid SSL error https://deliciousbrains.com/wp-migrate-db-pro/doc/ssl-errors/ + migratedb-from: \ No newline at end of file diff --git a/wp-cli.yml b/wp-cli.yml new file mode 100644 index 0000000..d5b9f46 --- /dev/null +++ b/wp-cli.yml @@ -0,0 +1,3 @@ +path: ./wordpress +server: + host: 127.0.0.1 \ No newline at end of file diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..fb57ccd --- /dev/null +++ b/yarn.lock @@ -0,0 +1,4 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + +