/
class-pods-deploy-ui.php
221 lines (181 loc) · 5.58 KB
/
class-pods-deploy-ui.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
<?php
class Pods_Deploy_UI {
public static $remote_url_key = 'pods_deploy_remote_url';
/**
* Callback for adding Pods Deploy to menus.
*
* Callback is in the activation function below.
*
* @since 0.4.0
*/
function menu ( $admin_menus ) {
$admin_menus[ 'pods-deploy'] = array(
'label' => __( 'Pods Deploy', 'pods-deploy' ),
'function' => array( $this, 'deploy_handler' ),
'access' => 'manage_options'
);
return $admin_menus;
}
/**
* Handles UI output and form processing
*
* @since 0.4.0
*/
function deploy_handler () {
if ( pods_v_sanitized( 'pods-deploy-submit', 'post') ) {
if ( ! pods_deploy_dependency_check() ) {
return;
}
if ( ! ( $nonce = pods_v_sanitized( '_wpnonce', $_REQUEST ) ) || ! wp_verify_nonce( $nonce, 'pods-deploy' ) ) {
pods_error( __( 'Bad nonce.', 'pods-deploy' ) );
}
$remote_url = pods_v_sanitized( 'remote-url', 'post', false, true );
$private_key = pods_v_sanitized( 'private-key', 'post' );
$public_key = pods_v_sanitized( 'public-key', 'post' );
if ( $remote_url && $private_key && $public_key ) {
Pods_Deploy_Auth::save_local_keys( $private_key, $public_key );
update_option( self::$remote_url_key, $remote_url );
$params = array(
'remote_url' => $remote_url,
'private_key' => $private_key,
'public_key' => $public_key,
);
$pod_types = $this->pod_types();
foreach ( $pod_types as $type => $type_names ) {
$type_sanitized = $this->type_sanitize( $type, $type_names );
if ( ! empty( $type_sanitized ) ) {
$params[ 'deploy_types' ][ $type ] = $type_sanitized;
}
}
/**
* Filter which Pods to deploy.
*
* @param array $Pods Name of Pods to be deployed.
* @param array $params Full params array.
*
* @return array $Pods
*
* @since 0.5.0
*/
$params[ 'deploy_types' ] = apply_filters( 'pods_deploy_pods_to_deploy', $params[ 'deploy_types' ], $params );
if ( ! pods_v_sanitized( 'deploy-components', 'post' ) ) {
$params[ 'components' ] = false;
}
else {
$params[ 'components' ] = true;
}
pods_deploy( $params );
}
else{
_e( 'Keys and URL for remote site not set', 'pods-deploy' );
pods_error( var_dump( array($remote_url, $private_key, $public_key ) ) );
}
}
elseif( pods_v_sanitized( 'pods-deploy-key-gen-submit', 'post' ) ) {
$activate = pods_v_sanitized( 'allow-deploy', 'post' );
if ( $activate ) {
Pods_Deploy_Auth::allow_deploy();
Pods_Deploy_Auth::generate_keys();
}
else {
Pods_Deploy_Auth::revoke_keys();
}
$this->include_view();
}
else {
$this->include_view();
}
}
function type_sanitize( $type = null, $valid_list = null ) {
$results = array();
if ( ! empty( $type ) && isset( $_POST[ $type ] ) ) {
if ( is_array( $valid_list ) ) {
foreach ( $valid_list as $id => $name ) {
if ( pods_v_sanitized( $id, $_POST[ $type ] ) ) {
$results[ $id ] = $name;
}
}
}
}
return $results;
}
/**
* Output a list of field names.
*
* @since 0.4.0
*
* @return array|mixed
*/
function pod_types() {
return pods_deploy_types();
}
/**
* Form fields for deploy form
*
* @since 0.4.0
*
* @return array
*/
function form_fields() {
$keys = Pods_Deploy_Auth::get_keys( false );
$public_local = pods_v_sanitized( 'public', $keys, '' );
$private_local = pods_v_sanitized( 'private', $keys, '' );
$remote_url = get_option( self::$remote_url_key, '' );
$form_fields = array(
'remote-url' =>
array(
'label' => __( 'URL To Remote Site API', 'pods-deploy' ),
'help' => __( 'For example "http://example.com/wp-json"', 'pods-deploy' ),
'value' => $remote_url,
'options' => '',
),
'public-key' =>
array(
'label' => __( 'Remote Site Public Key', 'pods-deploy' ),
'help' => __( 'Public key from remote site.', 'pods-deploy' ),
'value' => $public_local,
'options' => '',
),
'private-key' =>
array(
'label' => __( 'Remote Site Private Key', 'pods-deploy' ),
'help' => __( 'Private key from remote site.', 'pods-deploy' ),
'value' => $private_local,
'options' => '',
),
'deploy-components' =>
array(
'label' => __( 'Activate Pods Components', 'pods-deploy' ),
'help' => __( 'If checked, Pods Deploy will activate all components from this site on remote site. If false, it will only activate the Migrate Packages component, which is required.', 'pods-deploy' ),
'value' => true,
'type' => 'boolean',
),
);
return $form_fields;
}
/**
* Include main UI view and add scope data into it.
*
* @since 0.4.0
*
* @return bool|string
*/
function include_view() {
$keys = Pods_Deploy_Auth::get_keys( true );
$public_remote = pods_v_sanitized( 'public', $keys, '' );
$private_remote = pods_v_sanitized( 'private', $keys, '' );
$deploy_active = Pods_Deploy_Auth::deploy_active();
wp_enqueue_style( 'pods-wizard' );
if ( $deploy_active ) {
$key_gen_submit = __( 'Disable Deployments', 'pods-deploy' );
$key_gen_header = __( 'Click to revoke keys and prevent deployments to this site.', 'pods-deploy' );
}
else{
$key_gen_submit = __( 'Allow Deployments', 'pods-deploy' );
$key_gen_header = __( 'Click to generate new keys and allow deployments to this site', 'pods-deploy' );
}
$form_fields = $this->form_fields();
$data = compact( array( 'keys', 'public_local', 'private_local', 'public_remote', 'private_remote', 'deploy_active', 'key_gen_submit', 'key_gen_header', 'form_fields', ) );
return pods_view( PODS_DEPLOY_DIR . 'ui/main.php', $data );
}
}