Stencil_Abstract_Installable::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
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
		/**
125
		 * Themes cannot be ugpraded.
126
		 * So we never have to problem that the STENCIL_PATH is being moved.
127
		 *
128
		 * Though if a new theme is installed, should the installed implementations
129
		 * be copied to this new install aswel?
130
		 */
131
132
		global $wp_filesystem;
133
134
		$download_link = $this->get_download_link();
135
		$target_path   = $this->get_directory();
136
137
		require ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
138
139
		$upgrader = $this->get_upgrader();
140
		$upgrader->init();
141
142
		if ( ! $upgrading ) {
143
			$upgrader->install_strings();
144
		} else {
145
			$upgrader->upgrade_strings();
146
		}
147
148
		$skin = $upgrader->skin;
149
150
		$skin->header();
151
152
		// Connect to the Filesystem first.
153
		$res = $upgrader->fs_connect( array( WP_CONTENT_DIR, $target_path ) );
154
155
		// Mainly for non-connected filesystem.
156
		if ( ! $res ) {
157
			$skin->footer();
158
159
			return false;
160
		}
161
162
		$skin->before();
163
164
		if ( is_wp_error( $res ) ) {
165
			$this->cancel_installer( $skin, $res );
166
167
			return $res;
168
		}
169
170
		/**
171
		 * Download the package (Note, This just returns the filename
172
		 * of the file if the package is a local file)
173
		 */
174
		$download = $upgrader->download_package( $download_link );
175
		if ( is_wp_error( $download ) ) {
176
			$this->cancel_installer( $skin, $download );
177
178
			return $download;
179
		}
180
181
		// Unzips the file into a temporary directory.
182
		$working_dir = $upgrader->unpack_package( $download, true );
183
		if ( is_wp_error( $working_dir ) ) {
184
			$this->cancel_installer( $skin, $working_dir );
185
186
			return $working_dir;
187
		}
188
189
		$temporary_path = $target_path . '_upgrading';
190
191
		if ( $upgrading ) {
192
			$upgrader->maintenance_mode( true );
193
194
			$skin->feedback( 'remove_old' );
195
196
			if ( is_dir( $temporary_path ) ) {
197
				$wp_filesystem->rmdir( $temporary_path, true );
198
			}
199
200
			// Move current install.
201
			$wp_filesystem->move( $target_path, $temporary_path );
202
		}
203
204
		$skin->feedback( 'installing_package' );
205
206
		$installed = $wp_filesystem->move( $working_dir . DIRECTORY_SEPARATOR . $this->get_slug() . '-master', $target_path );
207
208
		if ( $upgrading ) {
209
			if ( false === $installed || is_wp_error( $installed ) ) {
210
				// Restore old install.
211
				$wp_filesystem->move( $temporary_path, $target_path );
212
213
				return false;
214
			} else {
215
				// Remove old install.
216
				$wp_filesystem->rmdir( $temporary_path, true );
217
			}
218
		}
219
220
		$upgrader->maintenance_mode( false );
221
222
		$skin->feedback( $installed ? 'process_success' : 'process_failed' );
223
224
		$skin->after();
225
		$skin->footer();
226
227
		// Done.
228
		return true;
229
	}
230
231
	/**
232
	 * Upgrade
233
	 *
234
	 * @return bool
235
	 * @throws Exception When an upgrade is already in progress for this package.
236
	 */
237
	public function upgrade() {
238
		return $this->install( true );
239
	}
240
241
	/**
242
	 * Cancel installer.
243
	 *
244
	 * @param WP_Upgrader_Skin $skin Skin to set message on.
245
	 * @param WP_Error|string  $error Error to display.
246
	 */
247
	protected function cancel_installer( $skin, $error ) {
248
		$skin->error( $error );
249
		$skin->after();
250
		$skin->footer();
251
	}
252
}
253