This repository was archived by the owner on Sep 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTasks.php
More file actions
273 lines (242 loc) · 9.43 KB
/
Tasks.php
File metadata and controls
273 lines (242 loc) · 9.43 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php declare(strict_types=1);
////////////////////////////////////////////////////////////////////////////////
// ___________ __ __ _____
// \_ _____/______ __ __ _____/ |_|__|/ ____\__ __
// | __) \_ __ \ | \_/ ___\ __\ \ __< | |
// | \ | | \/ | /\ \___| | | || | \___ |
// \___ / |__| |____/ \___ >__| |__||__| / ____|
// \/ \/ \/
// -----------------------------------------------------------------------------
// https://github.com/fructify
//
// Designed and Developed by Brad Jones <brad @="bjc.id.au" />
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
namespace Fructify\Robo;
use Gears\String\Str;
use Composer\Semver\Semver;
use GuzzleHttp\Client as Http;
use YaLinqo\Enumerable as Linq;
use Symfony\Component\Finder\Finder;
class Tasks extends \Robo\Tasks
{
/** @var string */
private static $WP_SALTS_URL = 'https://api.wordpress.org/secret-key/1.1/salt/';
/** @var string */
private static $WP_VERSION_URL = 'https://api.wordpress.org/core/version-check/1.7/';
/** @var string */
private static $WP_RELEASES_URL = 'https://wordpress.org/download/release-archive/';
/**
* Installs Wordpress.
*
* This task will download the core wordpress files for you. It is
* automatically run by composer as a post-install-cmd so you really
* shouldn't need to worry about it.
*
* But if you do want to call it, usage would look like:
*
* ```
* ./robo fructify:install v4.*
* ```
*
* @param string $versionContraint A semantic version contraint.
* @return void
*/
public function fructifyInstall(string $versionContraint = '*')
{
// Lets check if wordpress actually exists
if (!file_exists('./wp-includes/version.php'))
{
// Grab the resolved version number
$version = $this->wpResolveVersionNo($versionContraint);
// Download the core wordpress files
$this->_exec('./vendor/bin/wp core download --version='.$version);
// Remove a few things we don't need
@unlink('./license.txt');
@unlink('./readme.html');
@unlink('./wp-config-sample.php');
@unlink('./wp-content/plugins/hello.php');
// Read in the composer file and remove some of the bundled plugins
// and themes unless of course they are actually referenced in the
// composer requirements.
$composer = Str::s(file_get_contents('./composer.json'));
if (!$composer->contains('wpackagist-plugin/akismet'))
{
$this->_deleteDir(['./wp-content/plugins/akismet']);
}
if (!$composer->contains('wpackagist-theme/twentyseventeen'))
{
$this->_deleteDir(['./wp-content/themes/twentyseventeen']);
}
if (!$composer->contains('wpackagist-theme/twentysixteen'))
{
$this->_deleteDir(['./wp-content/themes/twentysixteen']);
}
if (!$composer->contains('wpackagist-theme/twentyfifteen'))
{
$this->_deleteDir(['./wp-content/themes/twentyfifteen']);
}
if (!$composer->contains('wpackagist-theme/twentyfourteen'))
{
$this->_deleteDir(['./wp-content/themes/twentyfourteen']);
}
}
}
/**
* Updates Wordpress.
*
* This task will update the core wordpress files for you. It is
* automatically run by composer as a post-update-cmd so you really
* shouldn't need to worry about it.
*
* But if you do want to call it, usage would look like:
*
* ```
* ./robo fructify:update v4.*
* ```
*
* @param string $versionContraint A semantic version contraint.
* @return void
*/
public function fructifyUpdate(string $versionContraint = '*')
{
// Lets attempt to update wordpress
if (file_exists('./wp-includes/version.php'))
{
// Grab the version of wordpress that is installed
require('./wp-includes/version.php');
$installed_version = $wp_version;
// Get the version we want to update to
$new_version = $this->wpResolveVersionNo($versionContraint);
// Nothing to do, same version.
if ($installed_version == $new_version) return;
// Now lets download the version of wordpress that we already have,
// sounds silly I know but it will make sense soon I promise.
$temp = sys_get_temp_dir().'/'.md5(microtime());
$this->_mkdir($temp);
$this->_exec
(
'./vendor/bin/wp core download'.
' --version='.$installed_version.
' --path='.$temp
);
// Now lets delete all the files that are stock wordpress files
$finder = new Finder();
$finder->files()->in($temp);
foreach ($finder as $file)
{
if (file_exists($file->getRelativePathname()))
{
unlink($file->getRelativePathname());
}
}
// Clean up
$this->_deleteDir(['./wp-admin', './wp-includes', $temp]);
}
// Either we just deleted the old wordpress files or it didn't exist.
// Regardless lets run the install functionality.
$this->fructifyInstall($versionContraint);
}
/**
* Creates New Salts, used for encryption by Wordpress.
*
* This task will create a new set of salts and write them to the
* .salts.php file for you. Again this is tied into composer as a
* post-install-cmd so you really shouldn't need to worry about it.
*
* But if you do want to call it, usage would look like:
*
* ```
* ./robo fructify:salts
* ```
*
* @return void
*/
public function fructifySalts()
{
$this->taskWriteToFile('./.salts.php')
->line('<?php')
->text((new Http)->request('GET', self::$WP_SALTS_URL)->getBody())
->run();
}
/**
* Sets permissions on "special" Wordpress folders.
*
* This task simply loops through some folders and ensures they exist and
* have the correct permissions. It is automatically run by composer as a
* post-install-cmd so you really shouldn't need to worry about it.
*
* But if you do want to call it, usage would look like:
*
* ```
* ./robo fructify:permissions
* ```
*
* @return void
*/
public function fructifyPermissions()
{
// These folders will be given full write permissions
$folders =
[
'./wp-content/uploads'
];
// Loop through each folder
foreach ($folders as $folder)
{
$this->taskFileSystemStack()
->mkdir($folder)
->chmod($folder, 0777)
->run();
}
}
/**
* Resolves a Wordpress Version Contraint.
*
* This is a private helper method. It takes a semantic version contraint,
* parsable by [Composer's Semver](https://github.com/composer/semver) and
* resolves an actual wordpress version number.
*
* We use this page: http://wordpress.org/download/release-archive/
* As an offical list of released versions.
*
* @param string $versionContraint A semantic version contraint.
* @return string A semantic version number.
*/
private function wpResolveVersionNo(string $versionContraint)
{
// Remove a v at the start if it exists
$versionContraint = str_replace('v', '', $versionContraint);
// If the constraint it a single wildcard, lets just
// return the latest stable release of wordpress.
if ($versionContraint == '*')
{
$json = (new Http)->request('GET', self::$WP_VERSION_URL)->getBody();
return json_decode($json, true)['offers'][0]['version'];
}
// Download the releases from the wordpress site.
$html = (new Http)->request('GET', self::$WP_RELEASES_URL)->getBody()->getContents();
// Extract a list of download links, these contain the versions.
preg_match_all("#><a href='https://wordpress\.org/wordpress-[^>]+#",
$html, $matches
);
// Filter the links to obtain a list of just versions
$versions = Linq::from($matches[0])
->select(function(string $v){ return Str::s($v); })
->where(function(Str $v){ return $v->endsWith(".zip'"); })
->where(function(Str $v){ return !$v->contains('IIS'); })
->where(function(Str $v){ return !$v->contains('mu'); })
->select(function(Str $v){ return $v->between('wordpress-', '.zip'); })
->where(function(Str $v)
{
if ($v->contains('-'))
{
return preg_match("#.*-(dev|beta|alpha|rc).*#i", (string)$v) === 1;
}
return true;
})
->toArray();
// Let semver take over and work it's magic
return (string) Semver::satisfiedBy($versions, $versionContraint)[0];
}
}