-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.php
More file actions
214 lines (164 loc) · 4.2 KB
/
build.php
File metadata and controls
214 lines (164 loc) · 4.2 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
<?php
/**
* A singleton class to run some build functions such as creating dummy tables and enable plugins on the fly.
*
* @author David Ogilo
* @version 1.0
*/
class Build {
private $db;
private $tables;
private $options;
private static $_instance;
private function __construct()
{
global $wpdb;
$this->db = $wpdb;
$this->setArguments();
$this->init();
}
/**
* Initiates the build script process.
*/
private function init()
{
if ( $this->getTables() )
{
//Suppress all errors from showing - errors related to foreign keys constraints on tables.
$this->db->suppress_errors( true );
$this->dropTables();
$this->db->suppress_errors( false );
}
$this->install();
if ( array_key_exists( 'plugins', $this->options ) && $this->options['plugins'] )
$this->activatePlugins( $this->options['plugins'] );
exit( 0 );
}
/**
* Checks the arguments passed to the script and runs functions depending on the arguments passed.
*
* @return null
*/
private function setArguments()
{
$longOptions = array( 'plugins::' );
$this->options = getopt( '', $longOptions );
return null;
}
/**
* Drops dummy PHPUNIT tables;
*
* @return boolean
*/
private function dropTables()
{
static $loop;
$count = 0;
if ( !$tables = $this->getTables() ) return false;
if ( !isset( $loop ) )
print '=== DELETING TABLES ===' . PHP_EOL . PHP_EOL;
foreach( $tables as $index => $table )
{
$name = $table['Tables_in_' . DB_NAME];
$type = $table['Table_type'];
switch( $type )
{
case 'VIEW':
$sql = "DROP VIEW $name";
break;
default:
$sql = "DROP TABLE $name";
break;
}
if ( !$this->db->query( $sql ) ) continue;
unset( $this->tables[$index] );
$count++;
print $name . PHP_EOL;
}
if ( count( $tables ) > $count )
{
$loop = true;
return $this->dropTables();
}
print PHP_EOL . 'Complete.' . PHP_EOL . PHP_EOL;
return true;
}
/**
* Returns an array of PHPUNIT tables
*
* @return array
*/
private function getTables()
{
if ( empty( $this->tables ) )
{
$sql = "SHOW FULL TABLES IN " . DB_NAME . " WHERE Tables_in_" . DB_NAME . " LIKE '" . PHPUNIT_DB_PREFIX . "%'";
$this->tables = $this->db->get_results( $sql, ARRAY_A );
}
return $this->tables;
}
/**
* Installs WordPress
*
* @return boolean
*/
private function install()
{
print '=== INSTALLING PHPUNIT WORDPRESS INSTANCE ===' . PHP_EOL . PHP_EOL;
if ( is_array( wp_install( 'PHPUNIT', 'phpunit', 'phpunit@example.com', true, null, 'phpunit' ) ) )
print 'Complete.' . PHP_EOL . PHP_EOL;
else
{
print 'Failed.' . PHP_EOL . PHP_EOL;
exit( 1 );
}
return true;
}
/**
* Enables plugins on the fly. If plugins are already activated, then nothing happens.
*
* @param string $plugins - a ',' delimited string.
* @return boolean
*/
private function activatePlugins( $plugins )
{
$plugins = explode( ',', $plugins );
print "=== ACTIVATING PLUGINS ===" . PHP_EOL . PHP_EOL;
foreach( $plugins as &$plugin )
{
if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin ) && !is_file( WP_PLUGIN_DIR . '/' . $plugin ) )
$plugin .= '/' . $plugin . '.php';
print $plugin . PHP_EOL;
}
unset( $plugin );
if ( activate_plugins( $plugins ) !== true )
{
print PHP_EOL . 'Failed.' . PHP_EOL . PHP_EOL;
exit( 1 );
}
print PHP_EOL . 'Complete.' . PHP_EOL . PHP_EOL;
return true;
}
/**
* Returns the build instance
*
* @return Build
*/
public static function getInstance()
{
if ( empty( self::$_instance ) )
self::$_instance = new Build();
return self::$_instance;
}
}
( PHP_SAPI === 'cli' ) || die( 'Access Denied' );
//Required so that we can install a dummy instance of wordpress
define( 'WP_SITEURL', 'http://example.com' );
//Initiaties the install process by bypassing redirect to install.php
define( 'WP_INSTALLING', true );
//Override default function to prevent sending an email on install
function wp_new_blog_notification( $blogTitle, $blogURL, $userID, $password ){}
require_once( 'bootstrap.php' );
require_once( ABSPATH . 'wp-admin/includes/upgrade.php');
Build::getInstance();
?>