This repository has been archived by the owner on Mar 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-shortcode-cssg-filesystem.php
284 lines (235 loc) · 11 KB
/
class-shortcode-cssg-filesystem.php
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
274
275
276
277
278
279
280
281
282
283
284
<?php
/**
*
* The Shortcode_CSSG_Filesystem is a slightly modified verstion of
* the Redux_Filesystem class developed by the Redux Framework.
*
* The Redux Framework site: https://reduxframework.com
*
* @since 1.0.0
*
* @package Shortcode Boilerplate
* @subpackage Shortcode Boilerplate/includes
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Shortcode_CSSG_Filesystem' ) ) {
class Shortcode_CSSG_Filesystem {
/**
* Instance of this class.
*
* @since 1.0.0
* @var object
*/
protected static $instance = null;
protected static $direct = null;
private $creds = array();
public $fs_object = null;
public $parent = null;
public function __construct() {
$this->parent->admin_notices[] = array(
'type' => 'error',
'msg' => '<strong>' . __( 'File Permission Issues', 'shortcode-cssg' ) . '</strong><br/>' . sprintf( __( 'We were unable to modify required files. Please check your permissions, or modify your wp-config.php file to contain your FTP login credentials as <a href="%s" target="_blank">outlined here</a>.', 'shortcode-cssg' ), 'https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants' ),
'id' => 'redux-wp-login',
'dismiss' => false,
);
}
/**
* Return an instance of this class.
*
* @since 1.0.0
* @return object A single instance of this class.
*/
public static function get_instance( $parent = null ) {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
if ( $parent !== null ) {
self::$instance->parent = $parent;
}
return self::$instance;
}
public function ftp_form() {
if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
echo '<div class="wrap"><div class="error"><p>';
echo '<strong>' . __( 'File Permission Issues', 'shortcode-cssg' ) . '</strong><br/>' . sprintf( __( 'We were unable to modify required files. Please ensure that <code>%1s</code> has the proper read-write permissions, or modify your wp-config.php file to contain your FTP login credentials as <a href="%2s" target="_blank">outlined here</a>.', 'shortcode-cssg' ), trailingslashit( WP_CONTENT_DIR ) . '/uploads/', 'https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants' );
echo '</p></div><h2></h2>' . '</div>';
}
}
function filesystem_init( $form_url, $method = '', $context = false, $fields = null ) {
global $wp_filesystem;
if ( ! empty( $this->creds ) ) {
return true;
}
ob_start();
/* first attempt to get credentials */
if ( false === ( $this->creds = request_filesystem_credentials( $form_url, $method, false, $context ) ) ) {
$this->creds = array();
$this->parent->ftp_form = ob_get_contents();
ob_end_clean();
/**
* if we comes here - we don't have credentials
* so the request for them is displaying
* no need for further processing
**/
return false;
}
/* now we got some credentials - try to use them*/
if ( ! WP_Filesystem( $this->creds ) ) {
$this->creds = array();
/* incorrect connection data - ask for credentials again, now with error message */
request_filesystem_credentials( $form_url, '', true, $context );
$this->parent->ftp_form = ob_get_contents();
ob_end_clean();
return false;
}
return true;
}
public static function load_direct() {
if ( self::$direct === null ) {
require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php';
require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php';
self::$direct = new WP_Filesystem_Direct( array() );
}
}
public function execute( $action, $file = '', $params = '' ) {
if ( ! empty ( $params ) ) {
extract( $params );
}
// Setup the filesystem with creds
require_once ABSPATH . '/wp-admin/includes/template.php';
require_once ABSPATH . '/wp-includes/pluggable.php';
require_once ABSPATH . '/wp-admin/includes/file.php';
$base = 'post.php?post=' . get_the_ID();
$url = wp_nonce_url( $base );
$this->filesystem_init( $url, 'direct', dirname( $file ) );
return $this->do_action( $action, $file, $params );
}
public function do_action( $action, $file = '', $params = '' ) {
if ( ! empty ( $params ) ) {
extract( $params );
}
global $wp_filesystem;
if ( ! isset( $params['chmod'] ) || ( isset( $params['chmod'] ) && empty( $params['chmod'] ) ) ) {
if ( defined( 'FS_CHMOD_FILE' ) ) {
$chmod = FS_CHMOD_FILE;
} else {
$chmod = 0644;
}
}
$res = false;
if ( ! isset( $recursive ) ) {
$recursive = false;
}
//$target_dir = $wp_filesystem->find_folder( dirname( $file ) );
// Do unique stuff
if ( $action == 'mkdir' ) {
if ( defined( 'FS_CHMOD_DIR' ) ) {
$chmod = FS_CHMOD_DIR;
} else {
$chmod = 0755;
}
$res = $wp_filesystem->mkdir( $file );
if ( ! $res ) {
wp_mkdir_p( $file );
$res = file_exists( $file );
if ( ! $res ) {
mkdir( $file, $chmod, true );
$res = file_exists( $file );
}
}
} elseif ( $action == 'rmdir' ) {
$res = $wp_filesystem->rmdir( $file, $recursive );
} elseif ( $action == 'copy' && ! isset( $this->filesystem->killswitch ) ) {
if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
$res = copy( $file, $destination );
if ( $res ) {
chmod( $destination, $chmod );
}
} else {
$res = $wp_filesystem->copy( $file, $destination, $overwrite, $chmod );
}
} elseif ( $action == 'move' && ! isset( $this->filesystem->killswitch ) ) {
$res = $wp_filesystem->copy( $file, $destination, $overwrite );
} elseif ( $action == 'delete' ) {
$res = $wp_filesystem->delete( $file, $recursive );
} elseif ( $action == 'rmdir' ) {
$res = $wp_filesystem->rmdir( $file, $recursive );
} elseif ( $action == 'dirlist' ) {
if ( ! isset( $include_hidden ) ) {
$include_hidden = true;
}
$res = $wp_filesystem->dirlist( $file, $include_hidden, $recursive );
} elseif ( $action == 'put_contents' && ! isset( $this->filesystem->killswitch ) ) {
// Write a string to a file
if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
self::load_direct();
$res = self::$direct->put_contents( $file, $content, $chmod );
} else {
$res = $wp_filesystem->put_contents( $file, $content, $chmod );
}
} elseif ( $action == 'chown' ) {
// Changes file owner
if ( isset( $owner ) && ! empty( $owner ) ) {
$res = $wp_filesystem->chmod( $file, $chmod, $recursive );
}
} elseif ( $action == 'owner' ) {
// Gets file owner
$res = $wp_filesystem->owner( $file );
} elseif ( $action == 'chmod' ) {
if ( ! isset( $params['chmod'] ) || ( isset( $params['chmod'] ) && empty( $params['chmod'] ) ) ) {
$chmod = false;
}
$res = $wp_filesystem->chmod( $file, $chmod, $recursive );
} elseif ( $action == 'get_contents' ) {
// Reads entire file into a string
if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
self::load_direct();
$res = self::$direct->get_contents( $file );
} else {
$res = $wp_filesystem->get_contents( $file );
}
} elseif ( $action == 'get_contents_array' ) {
// Reads entire file into an array
$res = $wp_filesystem->get_contents_array( $file );
} elseif ( $action == 'object' ) {
$res = $wp_filesystem;
} elseif ( $action == 'unzip' ) {
$unzipfile = unzip_file( $file, $destination );
if ( $unzipfile ) {
$res = true;
}
}
if ( ! $res ) {
if ($action == 'dirlist') {
if (empty($res) || $res == false || $res == '' ) {
return;
}
if (is_array($res) && empty($res)) {
return;
}
if (!is_array($res)) {
if (count(glob("$file*")) == 0) {
return;
}
}
}
$this->killswitch = true;
$this->parent->admin_notices[] = array(
'type' => 'error',
'msg' => '<strong>' . __( 'File Permission Issues', 'shortcode-cssg' ) . '</strong><br/>' . sprintf( __( 'We were unable to modify required files. Please ensure that <code>%1s</code> has the proper read-write permissions, or modify your wp-config.php file to contain your FTP login credentials as <a href="%2s" target="_blank">outlined here</a>.', 'shortcode-cssg' ), trailingslashit( WP_CONTENT_DIR ) . '/uploads/', 'https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants' ),
'id' => 'redux-wp-login',
'dismiss' => false,
);
//add_action( "redux/page/{$this->parent->args['opt_name']}/form/before", array(
// $this,
// 'ftp_form'
//) );
}
return $res;
}
}
Shortcode_CSSG_Filesystem::get_instance();
}