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
|
<?php /** * Fake FTP file system. * - Hook into WordPress with `new Loco_test_DummyFtpConnect` * - Use to write a file with `$file->getWriteContext()->connect( new WP_Filesystem_Debug($creds) )` */ class Loco_test_DummyFtpConnect extends Loco_hooks_Hookable { public function filter_filesystem_method(){ return 'debug'; } }
/** * Dummy FTP file system. * WARNING: this actually modifies files - it just does it while simulating a remote connection * - All operations performed "direct" when authorized, else they fail. */ class WP_Filesystem_Debug extends WP_Filesystem_Base { private $authed;
/** * @var WP_Error */ public $errors;
public function __construct( array $opt ) { $this->options = $opt; $this->method = 'ftp'; }
/** * Dummy FTP connect: requires username=foo password=xxx */ public function connect() { $this->authed = false; $this->errors = new WP_Error; // @codeCoverageIgnoreStart if( empty($this->options['hostname']) ){ $this->errors->add( 'bad_hostname', 'Debug: empty hostname'); return false; } if( empty($this->options['username']) ){ $this->errors->add( 'bad_username', 'Debug: empty username'); return false; } if( $this->options['username'] !== 'foo' ) { $this->errors->add( 'bad_username', 'Debug: username expected to be "foo"'); return false; } if( empty($this->options['password']) ){ $this->errors->add( 'bad_username', 'Debug: empty password'); return false; } if( $this->options['password'] !== 'xxx' ) { $this->errors->add( 'bad_password', 'Debug: password expected to be "xxx"' ); return false; } // @codeCoverageIgnoreEnd $this->authed = true; return true; }
/** * @return WP_Filesystem_Debug */ public function disconnect(){ $this->authed = false; $this->options = []; return $this; }
/** * {@inheritdoc} * Dummy function allows exact path to be returned, subject to debugging filters */ public function find_folder( $path ){ if( WP_CONTENT_DIR === $path ){ return loco_constant('WP_CONTENT_DIR'); } return false; } /** * @internal * Proxies supposed remote call to *real* direct call, as long as instance is authorized. * Deliberately not extending WP_Filesystem_Direct for safety. */ private function _call( $method, array $args ){ if( $this->authed ){ $real = Loco_api_WordPressFileSystem::direct(); return call_user_func_array( [$real,$method], $args ); } return false; }
/** * {@inheritdoc} */ public function is_writable( $file ){ return $this->_call( __FUNCTION__, func_get_args() ); }
/** * {@inheritdoc} */ public function chmod( $file, $mode = false, $recursive = false ){ return $this->_call( __FUNCTION__, func_get_args() ); }
/** * {@inheritdoc} */ public function copy( $source, $destination, $overwrite = false, $mode = false ){ return $this->_call( __FUNCTION__, func_get_args() ); } /** * {@inheritdoc} */ public function put_contents( $path, $data, $mode = false ){ return $this->_call( __FUNCTION__, func_get_args() ); } /** * {@inheritdoc} */ public function delete( $file, $recursive = false, $type = false ){ return $this->_call( __FUNCTION__, func_get_args() ); } /** * {@inheritdoc} */ public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ){ return $this->_call( __FUNCTION__, func_get_args() ); } }
|