Skip to content

Commit dbb8cd3

Browse files
authored
Moves wp-gae tool into google/cloud-tools (#54)
1 parent 419b0f9 commit dbb8cd3

File tree

12 files changed

+506
-13
lines changed

12 files changed

+506
-13
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"bin": [
2525
"src/Utils/Flex/flex_exec",
26-
"scripts/install_test_deps.sh"
26+
"src/Utils/WordPress/wp-gae"
2727
],
2828
"autoload": {
2929
"psr-4": {

phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@
2929
<file>src/*.php</file>
3030
</whitelist>
3131
</filter>
32+
<php>
33+
<env name="PHPUNIT_TESTS" value="1"/>
34+
</php>
3235
</phpunit>
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
* limitations under the License.
1616
*/
1717

18-
namespace Google\Cloud\Utils;
18+
namespace Google\Cloud\Utils\WordPress;
1919

2020
use Exception;
21+
use Google\Cloud\Utils\Project as BaseProject;
2122
use Symfony\Component\Console\Input\InputInterface;
2223
use Symfony\Component\Console\Output\OutputInterface;
2324
use Symfony\Component\Console\Question\ChoiceQuestion;
@@ -26,7 +27,7 @@
2627
use Symfony\Component\Console\Helper\QuestionHelper;
2728
use Symfony\Component\Filesystem\Filesystem;
2829

29-
class WordPressProject extends Project
30+
class Project extends BaseProject
3031
{
3132
const DEFAULT_DIR = 'my-wordpress-project';
3233
const DEFAULT_DB_REGION = 'us-central1';

src/Utils/WordPress/files/app.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
runtime: php72
2+
3+
# Defaults to "serve index.php" and "serve public/index.php". Can be used to
4+
# serve a custom PHP front controller (e.g. "serve backend/index.php") or to
5+
# run a long-running PHP script as a worker process (e.g. "php worker.php").
6+
entrypoint: serve gae-app.php
7+
8+
handlers:
9+
- url: /(.*\.(htm|html|css|js))
10+
static_files: \1
11+
upload: .*\.(htm|html|css|js)$
12+
13+
- url: /wp-content/(.*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg))
14+
static_files: wp-content/\1
15+
upload: wp-content/.*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg)$
16+
17+
- url: /(.*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg))
18+
static_files: \1
19+
upload: .*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg)$
20+
21+
- url: /wp-includes/images/media/(.*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg))
22+
static_files: wp-includes/images/media/\1
23+
upload: wp-includes/images/media/.*\.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg)$
24+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cron:
2+
- description: "wordpress cron tasks"
3+
url: /wp-cron.php
4+
schedule: every 15 minutes
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/**
4+
* This file handles all routing for a WordPress project running on App Engine.
5+
* It serves up the appropriate PHP file depending on the request URI.
6+
*
7+
* @see https://cloud.google.com/appengine/docs/standard/php7/runtime#application_startup
8+
*/
9+
10+
/**
11+
* Function to return a PHP file to load based on the request URI.
12+
*
13+
* @param string $full_request_uri The request URI derivded from $_SERVER['REQUEST_URI'].
14+
*/
15+
function get_real_file_to_load($full_request_uri)
16+
{
17+
$request_uri = @parse_url($full_request_uri)['path'];
18+
19+
// Redirect /wp-admin to /wp-admin/ (adds a trailing slash)
20+
if ($request_uri === '/wp-admin') {
21+
header('Location: /wp-admin/');
22+
exit;
23+
}
24+
25+
// Serve up "index.php" when /wp-admin/ is requested
26+
if ($request_uri === '/wp-admin/') {
27+
return '/wp-admin/index.php';
28+
}
29+
30+
// Load the file requested if it exists
31+
if (is_file(__DIR__ . $request_uri)) {
32+
return $request_uri;
33+
}
34+
35+
// Send everything else through index.php
36+
return '/index.php';
37+
}
38+
39+
// fixes b/111391534
40+
$_SERVER['HTTPS'] = $_SERVER['HTTP_X_APPENGINE_HTTPS'];
41+
42+
// Loads the expected WordPress framework file
43+
// (e.g index.php, wp-admin/* or wp-login.php)
44+
$file = get_real_file_to_load($_SERVER['REQUEST_URI']);
45+
46+
// Set the environment variables to reflect the script we're loading
47+
// (in order to trick WordPress)
48+
$_SERVER['DOCUMENT_URI'] = $_ENV['DOCUMENT_URI'] = $file;
49+
$_SERVER['PHP_SELF'] = $_ENV['PHP_SELF'] = $file;
50+
$_SERVER['SCRIPT_NAME'] = $_ENV['SCRIPT_NAME'] = $file;
51+
$_SERVER['SCRIPT_FILENAME'] = $_ENV['SCRIPT_FILENAME'] = __DIR__ . $file;
52+
53+
require __DIR__ . $file;

src/Utils/WordPress/files/php.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
allow_url_include = "1"
2+
upload_max_filesize = 8M
3+
4+
; for debugging purposes only. Please remove for production instances
5+
display_errors=On
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
/**
3+
* The base configurations of the WordPress.
4+
*
5+
* This file has the following configurations: MySQL settings, Table Prefix,
6+
* Secret Keys, WordPress Language, and ABSPATH. You can find more information
7+
* by visiting {@link http://codex.wordpress.org/Editing_wp-config.php Editing
8+
* wp-config.php} Codex page. You can get the MySQL settings from your web host.
9+
*
10+
* This file is used by the wp-config.php creation script during the
11+
* installation. You don't have to use the web site, you can just copy this file
12+
* to "wp-config.php" and fill in the values.
13+
*
14+
* @package WordPress
15+
*/
16+
17+
// $onGae is true on production.
18+
if (isset($_SERVER['GAE_ENV'])) {
19+
$onGae = true;
20+
} else {
21+
$onGae = false;
22+
}
23+
24+
// Cache settings
25+
// Disable cache for now, as this does not work on App Engine for PHP 7.2
26+
define('WP_CACHE', false);
27+
28+
// Disable pseudo cron behavior
29+
define('DISABLE_WP_CRON', true);
30+
31+
// Determine HTTP or HTTPS, then set WP_SITEURL and WP_HOME
32+
if ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
33+
|| (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)) {
34+
$protocol_to_use = 'https://';
35+
} else {
36+
$protocol_to_use = 'http://';
37+
}
38+
if (isset($_SERVER['HTTP_HOST'])) {
39+
define('HTTP_HOST', $_SERVER['HTTP_HOST']);
40+
} else {
41+
define('HTTP_HOST', 'localhost');
42+
}
43+
define('WP_SITEURL', $protocol_to_use . HTTP_HOST);
44+
define('WP_HOME', $protocol_to_use . HTTP_HOST);
45+
46+
// ** MySQL settings - You can get this info from your web host ** //
47+
if ($onGae) {
48+
/** The name of the Cloud SQL database for WordPress */
49+
define('DB_NAME', 'YOUR_DB_NAME');
50+
/** Production login info */
51+
define('DB_HOST', ':/cloudsql/YOUR_DB_CONNECTION');
52+
define('DB_USER', 'YOUR_DB_USER');
53+
define('DB_PASSWORD', 'YOUR_DB_PASSWORD');
54+
} else {
55+
/** The name of the local database for WordPress */
56+
define('DB_NAME', 'YOUR_DB_NAME');
57+
/** Local environment MySQL login info */
58+
define('DB_HOST', '127.0.0.1');
59+
define('DB_USER', 'YOUR_LOCAL_DB_USER');
60+
define('DB_PASSWORD', 'YOUR_LOCAL_DB_PASSWORD');
61+
}
62+
63+
/** Database Charset to use in creating database tables. */
64+
define('DB_CHARSET', 'utf8');
65+
66+
/** The Database Collate type. Don't change this if in doubt. */
67+
define('DB_COLLATE', '');
68+
69+
/**#@+
70+
* Authentication Unique Keys and Salts.
71+
*
72+
* Change these to different unique phrases!
73+
* You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
74+
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
75+
*
76+
* @since 2.6.0
77+
*/
78+
define('AUTH_KEY', 'YOUR_AUTH_KEY');
79+
define('SECURE_AUTH_KEY', 'YOUR_SECURE_AUTH_KEY');
80+
define('LOGGED_IN_KEY', 'YOUR_LOGGED_IN_KEY');
81+
define('NONCE_KEY', 'YOUR_NONCE_KEY');
82+
define('AUTH_SALT', 'YOUR_AUTH_SALT');
83+
define('SECURE_AUTH_SALT', 'YOUR_SECURE_AUTH_SALT');
84+
define('LOGGED_IN_SALT', 'YOUR_LOGGED_IN_SALT');
85+
define('NONCE_SALT', 'YOUR_NONCE_SALT');
86+
87+
/**#@-*/
88+
/**
89+
* WordPress Database Table prefix.
90+
*
91+
* You can have multiple installations in one database if you give each a unique
92+
* prefix. Only numbers, letters, and underscores please!
93+
*/
94+
$table_prefix = 'wp_';
95+
96+
/**
97+
* WordPress Localized Language, defaults to English.
98+
*
99+
* Change this to localize WordPress. A corresponding MO file for the chosen
100+
* language must be installed to wp-content/languages. For example, install
101+
* de_DE.mo to wp-content/languages and set WPLANG to 'de_DE' to enable German
102+
* language support.
103+
*/
104+
define('WPLANG', '');
105+
106+
/**
107+
* For developers: WordPress debugging mode.
108+
*
109+
* Change this to true to enable the display of notices during development.
110+
* It is strongly recommended that plugin and theme developers use WP_DEBUG
111+
* in their development environments.
112+
*/
113+
define('WP_DEBUG', !$onGae);
114+
115+
/* That's all, stop editing! Happy blogging. */
116+
/** Absolute path to the WordPress directory. */
117+
if (!defined('ABSPATH')) {
118+
define('ABSPATH', dirname(__FILE__) . '/wordpress/');
119+
}
120+
121+
/** Sets up WordPress vars and included files. */
122+
require_once(ABSPATH . 'wp-settings.php');

src/Utils/WordPress/wp-gae

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
/**
3+
* Copyright 2019 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
if (file_exists(__DIR__ . '/../../../vendor/autoload.php')) {
19+
// Run the command from the git clone.
20+
require_once __DIR__ . '/../../../vendor/autoload.php';
21+
} else if (file_exists(__DIR__ . '/../../../../../../vendor/autoload.php')) {
22+
// Run the command from the vendor dir installation.
23+
require_once __DIR__ . '/../../../../../../vendor/autoload.php';
24+
}
25+
26+
use Symfony\Component\Console\Application;
27+
use Symfony\Component\Console\Command\Command;
28+
use Symfony\Component\Console\Input\InputDefinition;
29+
use Symfony\Component\Console\Input\InputInterface;
30+
use Symfony\Component\Console\Input\InputArgument;
31+
use Symfony\Component\Console\Input\InputOption;
32+
use Symfony\Component\Console\Output\OutputInterface;
33+
use Google\Cloud\Utils\WordPress\Project;
34+
35+
$wordPressOptions = new InputDefinition([
36+
new InputOption('project_id', null, InputOption::VALUE_REQUIRED, 'Google Cloud project id'),
37+
new InputOption('db_region', null, InputOption::VALUE_REQUIRED, 'Cloud SQL region'),
38+
new InputOption('db_instance', null, InputOption::VALUE_REQUIRED, 'Cloud SQL instance id'),
39+
new InputOption('db_name', null, InputOption::VALUE_REQUIRED, 'Cloud SQL database name'),
40+
new InputOption('db_user', null, InputOption::VALUE_REQUIRED, 'Cloud SQL database username'),
41+
new InputOption('db_password', null, InputOption::VALUE_REQUIRED, 'Cloud SQL database password'),
42+
new InputOption('local_db_user', null, InputOption::VALUE_REQUIRED, 'Local SQL database username'),
43+
new InputOption('local_db_password', null, InputOption::VALUE_REQUIRED, 'Local SQL database password'),
44+
]);
45+
46+
$application = new Application('Wordpress Helper');
47+
$application->add(new Command('create'))
48+
->setDescription('Create a new WordPress site for Google Cloud')
49+
->setDefinition(clone $wordPressOptions)
50+
->addOption('dir', null, InputOption::VALUE_REQUIRED, 'Directory for the new project', Project::DEFAULT_DIR)
51+
->addOption('wordpress_url', null, InputOption::VALUE_REQUIRED, 'URL of the WordPress archive', Project::LATEST_WP)
52+
->setCode(function (InputInterface $input, OutputInterface $output) {
53+
$wordpress = new Project($input, $output);
54+
// Run the wizard to prompt user for project and database parameters.
55+
$dir = $wordpress->promptForProjectDir();
56+
57+
// download wordpress
58+
$wordpress->downloadWordpress($dir);
59+
60+
// initialize the project and download the plugins
61+
$wordpress->initializeProject($dir);
62+
$wordpress->downloadGcsPlugin();
63+
64+
$dbParams = $wordpress->initializeDatabase();
65+
66+
// populate random key params
67+
$params = $dbParams + $wordpress->generateRandomValueParams();
68+
69+
// copy all the sample files into the project dir and replace parameters
70+
$wordpress->copyFiles(__DIR__ . '/files', [
71+
//'.gcloudignore' => '/',
72+
'app.yaml' => '/',
73+
'cron.yaml' => '/',
74+
'php.ini' => '/',
75+
'gae-app.php' => '/',
76+
'wp-config.php' => '/',
77+
], $params);
78+
79+
$output->writeln("<info>Your WordPress project is ready at $dir</info>");
80+
});
81+
82+
$application->add(new Command('update'))
83+
->setDescription('Update an existing WordPress site for Google Cloud')
84+
->setDefinition(clone $wordPressOptions)
85+
->addArgument('dir', InputArgument::REQUIRED, 'Directory for the existing project')
86+
->setCode(function (InputInterface $input, OutputInterface $output) {
87+
// use the supplied dir for the wordpress project
88+
$dir = $input->getArgument('dir');
89+
90+
$wordpress = new Project($input, $output);
91+
$wordpress->initializeProject($dir);
92+
93+
// Download the plugins if they don't exist
94+
if (!file_exists($dir . '/wp-content/plugins/gcs')) {
95+
$wordpress->downloadGcsPlugin();
96+
}
97+
98+
$dbParams = $wordpress->initializeDatabase();
99+
100+
// populate random key params
101+
$params = $dbParams + $wordpress->generateRandomValueParams();
102+
103+
// copy all the sample files into the project dir and replace parameters
104+
$wordpress->copyFiles(__DIR__ . '/files', [
105+
//'.gcloudignore' => '/',
106+
'app.yaml' => '/',
107+
'cron.yaml' => '/',
108+
'php.ini' => '/',
109+
'gae-app.php' => '/',
110+
'wp-config.php' => '/',
111+
], $params);
112+
113+
$output->writeln("<info>Your WordPress project has been updated at $dir</info>");
114+
});
115+
116+
if (getenv('PHPUNIT_TESTS') === '1') {
117+
return $application;
118+
}
119+
120+
$application->run();

0 commit comments

Comments
 (0)