-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-wpcli-command.php
More file actions
83 lines (72 loc) · 2.33 KB
/
class-wpcli-command.php
File metadata and controls
83 lines (72 loc) · 2.33 KB
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
<?php
/**
* Generate Posts
*/
class Parser_JSON_Generate {
/**
* Generate JSON files
*
* ## OPTIONS
*
* [--post_type=<post_type>]
* : Comma seperated post types to generate JSON files for.
*
* [--posts_per_page=<number>]
* : Posts per JSON file. Default -1
*
* ## EXAMPLES
*
* wp parser-json generate --post_type=post,page
*/
public function __invoke( $args, $assoc_args ) {
$post_types = isset( $assoc_args['post_type'] ) ? $assoc_args['post_type'] : '';
$post_types = preg_split( '/[,]+/', trim( $post_types ), -1, PREG_SPLIT_NO_EMPTY );
$post_types = array_map( 'trim', array_unique( $post_types ) );
if ( $post_types ) {
$post_types = array_fill_keys( array_values( $post_types ), '' );
$post_types = wppj_get_json_post_types( $post_types );
} else {
// Back compatibility
if ( wppj_phpdoc_parser_post_types_exists() ) {
$hook_types = wppj_get_phpdoc_parser_hook_types();
$post_types = wppj_get_phpdoc_parser_post_types();
$post_types = array_merge( $post_types, array_fill_keys( $hook_types, 'wp-parser-hook' ) );
unset( $post_types['methods'] );
}
}
if ( ! isset( $assoc_args['posts_per_page'] ) ) {
$assoc_args['posts_per_page'] = -1;
} else {
$assoc_args['posts_per_page'] = absint( $assoc_args['posts_per_page'] );
if ( ! $assoc_args['posts_per_page'] ) {
WP_CLI::error( 'Please provide a valid number for --posts_per_page' );
return;
}
}
WP_CLI::log( 'Generating JSON files...' );
$files = new WP_Parser_JSON_File();
if ( true === $files->generate_files( $post_types, $assoc_args ) ) {
WP_CLI::error( 'Could not access the WP_Filesystem API' );
return;
}
$errors = get_settings_errors( 'wp-parser-json' );
foreach ( $errors as $error ) {
if ( 'updated' === $error['type'] ) {
WP_CLI::success( 'JSON files generated' );
} else {
$error_msg = mb_convert_encoding( $error['message'], 'UTF-8', 'HTML-ENTITIES' );
if ( 'no_post_type' === $error['code'] ) {
$warnings = explode( '<br/>', $error_msg );
$warnings = array_filter( $warnings );
foreach ( $warnings as $value ) {
WP_CLI::warning( $value );
}
WP_CLI::error( 'Not all JSON files are created' );
continue;
}
WP_CLI::error( $error_msg );
}
}
}
}
WP_CLI::add_command( 'parser-json generate', 'Parser_JSON_Generate' );