-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
executable file
·318 lines (258 loc) · 8.7 KB
/
plugin.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/**
Plugin Name: Ghost Inspector Test Runner
Version: 0.3.3
*/
use \calderawp\ghost\Container as Container;
define( 'CGR_VER', '0.4.0' );
add_action( 'init', function(){
include_once __DIR__ . '/vendor/autoload.php';
if( defined( 'CFCORE_VER' ) ){
calderaGhostRunner();
}else{
}
});
/**
* Get main instance of container
*
* @return Container
*/
function calderaGhostRunner(){
static $calderaGhostInspector;
if( ! is_object( $calderaGhostInspector ) ){
include_once __DIR__ . '/vendor/autoload.php';
$calderaGhostInspector = new Container();
/**
* Runs when main instance of container is initialized
*
* @param Container $calderaGhostInspector
*/
do_action( 'calderaGhostRunner.init', $calderaGhostInspector );
}
return $calderaGhostInspector;
}
/**
* WP CLI Command for import of forms
*/
if ( class_exists( 'WP_CLI' ) ) {
$importCommand = function() {
\calderawp\ghost\Factories::import();
WP_CLI::success( "The test forms may or may not have imported" );
};
WP_CLI::add_command( 'cgr import', $importCommand );
}
/**
* WP CLI Command for running tests
*/
if (class_exists('WP_CLI')) {
$runCommand = function () {
WP_CLI::line('Making requests' );
$runner = new \calderawp\ghost\Run();
$results = $runner->makeRequests();
if( ! empty( $results[ 'fails' ] ) ){
return WP_CLI::error( 'Some tests may not run.', true );
}else{
return WP_CLI::success( 'All of the requests to <em>Start</em> the tests worked.' );
}
};
WP_CLI::add_command('cgr run', $runCommand);
}
/**
* Set API key from ENV var or constant CGRKEY
*/
add_action( 'calderaGhostRunner.init',
function( Container $container ){
$apiKey = calderaGhostRunnerEnv( 'CGRKEY', null );
$container->setApiKey( $apiKey );
},
0
);
/**
* Set tests from the spreadsheet
*/
add_action( 'calderaGhostRunner.init',
function( Container $container ){
$id = calderaGhostRunnerEnv( 'CGRGDID' );
\calderawp\ghost\Factories::testsFromGoogleSheet( $id, $container );
},
2
);
/**
* Make some REST API endpoints
*/
add_action( 'calderaGhostRunner.init',
function( Container $container ){
add_action( 'rest_api_init',
function () use( $container )
{
$permissions = function ( \WP_REST_Request $r ) use ( $container ) {
return hash_equals( $r->get_param( 'key' ), $container->getLocalApiKey() );
};
register_rest_route( 'ghost-runner/v1', 'tests/', array(
'methods' => 'GET',
'permission_callback' => $permissions,
'callback' => function ( \WP_REST_Request $r ) use ( $container ) {
$runner = new \calderawp\ghost\Run( $r->get_param( 'notify' ) );
if( $r->get_param( 'run' ) ){
$runner
->createRequests()
->makeRequests();
}
return rest_ensure_response($runner->getUrls());
},
'args' => [
'key' => [
'type' => 'string',
'required' => true,
],
'notify' => [
'type' => 'string',
'default' => ''
],
'run' => [
'type' => 'boolean',
'default' => false
]
]
) );
register_rest_route( 'ghost-runner/v1', 'import/', array(
'methods' => 'GET',
'permission_callback' => $permissions,
'callback' => function ( \WP_REST_Request $r ) use ( $container ) {
header( 'application/type=text/html' );
\calderawp\ghost\Factories::import();
exit;
},
'args' => [
'key' => [
'type' => 'string',
'required' => true,
],
]
) );
}
);
},
4
);
/**
* Make a hacky admin that works, but like, LOL get rid of this.
*/
add_action( 'calderaGhostRunner.init',
function( Container $container ){
add_action( 'admin_menu', function() use ( $container ) {
add_menu_page(
'Ghost Runner',
'Ghost Runner',
'manage_options',
$container::SLUG,
function() use( $container ) {
$apiUrl = $action = add_query_arg(
array(
'key' => $container->getLocalApiKey()
),
rest_url('ghost-runner/v1/tests/all')
);
$importAction = 'importTests';
$allRunAction = 'allRun';
$action = $container->adminUrl(array(), $allRunAction);
$testRunAction = \calderawp\ghost\Test::ACTION;
/**
* Check if nonce is passed and valid by action.
*
* This is actually the route essentially -- each part has an "action" that action is used as a GET var whose value is nonce. This function checks if that "action" is set and the nonce is valid.
*
* @param string $action Nonce actio
*
* @return bool
*/
$testNonce = function ($action) {
return isset($_GET[$action]) && wp_verify_nonce($_GET[$action], $action);
};
$importUrl = $container->adminUrl(array('hi' => 'Roy'), $importAction);
if( $testNonce($importAction) ){
if ( $testNonce( $importAction ) ) {
\calderawp\ghost\Factories::import();
echo '<div>IMPORTED:)</div>';
printf( '<a href="%s">Import Forms Again</a>', esc_url( $importUrl ) );
}
}else{
echo '<h3>Import Tests</h3>';
echo '<strong>This will delete all pages and all forms</strong>';
printf('<a href="%s">Import Forms</a>', esc_url($importUrl));
}
echo '<h3>Forms</h3>';
$query = new WP_Query(
array(
'post_type' => 'page',
'posts_per_page' => '999',
'meta_query' => array(
'key' => 'GCR',
'value' => 'yes',
'compare' => '='
)
)
);
$linkPattern = '<div class="ghost-runner-test">%s - <a href="%s">Form</a> - <a href="%s">Page</a> - <a href="%s">Git Issue</a>';
if( $query->have_posts() ){
foreach ( $query->posts as $post ){
$editLink = add_query_arg(
'edit',
get_post_meta( $post->ID, 'CGR_formId', true ),
admin_url( 'admin.php?page=caldera-forms' )
);
$gitLink = 'https://github.com/calderawp/caldera-forms/' . get_post_meta( $post->ID, 'CGR_gitIssue', true );
printf(
$linkPattern,
esc_html( $post->post_title ),
esc_url( $editLink ),
esc_url( get_permalink( $post ) ),
esc_url( $gitLink )
);
}
}
}
);
});
}
);
/**
* Get the URL for the form editor
*
* @param string $formId Form ID
*
* @return string
*/
function calderaGhostRunnerFormUrl( $formId ){
$admin = \Caldera_Forms_Admin::get_instance();
if( method_exists( $admin, 'form_edit_link' ) ) {
return \Caldera_Forms_Admin::form_edit_link( $formId );
}else{
$args = array(
'edit' => $formId,
'page' => \Caldera_Forms::PLUGIN_SLUG
);
return add_query_arg( $args, admin_url( 'admin.php' ) );
}
}
/**
* Get value form ENV var, constant or default in that order.
*
* @param string $var Name of variable
* @param null|mixed $default Optional. Default value if not set in either location. Default is null.
*
* @return mixed|null
*/
function calderaGhostRunnerEnv( $var, $default = null ){
$value = getenv( $var );
if( is_null( $value ) && defined( strtoupper( $var ) ) ){
$value = constant( strtoupper( $var ) );
}
if( is_null( $value ) ){
$value = $default;
}
/**
* Change value of env var
*/
return apply_filters( 'calderaGhostRunner.env.'. $var, $value );
}