Passed
Push — master ( 8d6d08...8b273e )
by Jip
02:48
created

Stencil_Abstract_Installable::remove()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 20
rs 9.4286
cc 3
eloc 10
nc 2
nop 0
1
<?php
2
/**
3
 * Abstract Installable
4
 *
5
 * @package Stencil
6
 * @subpackage Upgrader
7
 */
8
9
/**
10
 * Class Stencil_Installable
11
 */
12
abstract class Stencil_Abstract_Installable implements Stencil_Installable_Interface {
13
	/**
14
	 * Slug of this module.
15
	 *
16
	 * @var string
17
	 */
18
	protected $slug;
19
20
	/**
21
	 * Readable name.
22
	 *
23
	 * @var string
24
	 */
25
	protected $name;
26
27
	/**
28
	 * Version of this module.
29
	 *
30
	 * @var string
31
	 */
32
	protected $version = '0.0.0';
33
34
	/**
35
	 * Stencil_Installable constructor.
36
	 *
37
	 * @param string $slug Slug of the module.
38
	 * @param string $name Name of this module.
39
	 */
40
	public function __construct( $slug, $name ) {
41
		$this->slug = $slug;
42
		$this->name = $name;
43
	}
44
45
	/**
46
	 * Get slug name.
47
	 *
48
	 * @return string
49
	 */
50
	public function get_slug() {
51
		return $this->slug;
52
	}
53
54
	/**
55
	 * Is this installable installed.
56
	 *
57
	 * @return bool
58
	 */
59
	public function is_installed() {
60
		return is_dir( $this->get_directory() );
61
	}
62
63
	/**
64
	 * Check if there is an upgrade available
65
	 *
66
	 * @return bool|mixed
67
	 */
68
	public function has_upgrade() {
69
		return false;
70
	}
71
72
	/**
73
	 * Do all requirements pass so it is usable.
74
	 *
75
	 * @return bool|array TRUE if passed, array of errors if failed.
76
	 */
77
	public function passed_requirements() {
78
		return true;
79
	}
80
81
	/**
82
	 * Get the name.
83
	 *
84
	 * @return string
85
	 */
86
	public function __toString() {
87
		return $this->name;
88
	}
89
90
	/**
91
	 * Remove/uninstall
92
	 *
93
	 * @return bool|WP_Error
94
	 */
95
	public function remove() {
96
		global $wp_filesystem;
97
98
		$target_path = $this->get_directory();
99
100
		$upgrader = $this->get_upgrader();
101
		$upgrader->init();
102
103
		// Connect to the Filesystem first.
104
		$res = $upgrader->fs_connect( array( WP_CONTENT_DIR, $target_path ) );
105
106
		// Mainly for non-connected filesystem.
107
		if ( ! $res || is_wp_error( $res ) ) {
108
			return false;
109
		}
110
111
		$deleted = $wp_filesystem->rmdir( $target_path, true );
112
113
		return $deleted;
114
	}
115
116
	/**
117
	 * Install
118
	 *
119
	 * @param bool $upgrading Installing or upgrading.
120
	 *
121
	 * @return bool|WP_Error True on succes, WP_Error on failure
122
	 */
123
	public function install( $upgrading = false ) {
124
		global $wp_filesystem;
125
126
		$download_link = $this->get_download_link();
127
		$target_path   = $this->get_directory();
128
129
		require ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
130
131
		$upgrader = $this->get_upgrader();
132
		$upgrader->init();
133
134
		if ( ! $upgrading ) {
135
			$upgrader->install_strings();
136
		} else {
137
			$upgrader->upgrade_strings();
138
		}
139
140
		$skin = $upgrader->skin;
141
142
		$skin->header();
143
144
		// Connect to the Filesystem first.
145
		$res = $upgrader->fs_connect( array( WP_CONTENT_DIR, $target_path ) );
146
147
		// Mainly for non-connected filesystem.
148
		if ( ! $res ) {
149
			$skin->footer();
150
151
			return false;
152
		}
153
154
		$skin->before();
155
156
		if ( is_wp_error( $res ) ) {
157
			$this->cancel_installer( $skin, $res );
158
159
			return $res;
160
		}
161
162
		/**
163
		 * Download the package (Note, This just returns the filename
164
		 * of the file if the package is a local file)
165
		 */
166
		$download = $upgrader->download_package( $download_link );
167
		if ( is_wp_error( $download ) ) {
168
			$this->cancel_installer( $skin, $download );
169
170
			return $download;
171
		}
172
173
		// Unzips the file into a temporary directory.
174
		$working_dir = $upgrader->unpack_package( $download, true );
175
		if ( is_wp_error( $working_dir ) ) {
176
			$this->cancel_installer( $skin, $working_dir );
177
178
			return $working_dir;
179
		}
180
181
		$temporary_path = $target_path . '_upgrading';
182
183
		if ( $upgrading ) {
184
			$upgrader->maintenance_mode( true );
185
186
			$skin->feedback( 'remove_old' );
187
188
			if ( is_dir( $temporary_path ) ) {
189
				$wp_filesystem->rmdir( $temporary_path, true );
190
			}
191
192
			// Move current install.
193
			$wp_filesystem->move( $target_path, $temporary_path );
194
		}
195
196
		$skin->feedback( 'installing_package' );
197
198
		$installed = $wp_filesystem->move( $working_dir . DIRECTORY_SEPARATOR . $this->get_slug() . '-master', $target_path );
199
		$wp_filesystem->rmdir( $working_dir, true );
200
201
		if ( $upgrading ) {
202
			if ( false === $installed || is_wp_error( $installed ) ) {
203
				// Restore old install.
204
				$wp_filesystem->move( $temporary_path, $target_path );
205
206
				return false;
207
			} else {
208
				// Remove old install.
209
				$wp_filesystem->rmdir( $temporary_path, true );
210
			}
211
		}
212
213
		$upgrader->maintenance_mode( false );
214
215
		$skin->feedback( $installed ? 'process_success' : 'process_failed' );
216
217
		$skin->after();
218
		$skin->footer();
219
220
		// Done.
221
		return true;
222
	}
223
224
	/**
225
	 * Upgrade
226
	 *
227
	 * @return bool
228
	 * @throws Exception When an upgrade is already in progress for this package.
229
	 */
230
	public function upgrade() {
231
		return $this->install( true );
232
	}
233
234
	/**
235
	 * Cancel installer.
236
	 *
237
	 * @param WP_Upgrader_Skin $skin Skin to set message on.
238
	 * @param WP_Error|string $error Error to display.
239
	 */
240
	protected function cancel_installer( $skin, $error ) {
241
		$skin->error( $error );
242
		$skin->after();
243
		$skin->footer();
244
	}
245
}
246