tools-sections
3 weeks ago
about.php
3 weeks ago
listings.php
3 weeks ago
post-types.php
3 weeks ago
support.php
3 weeks ago
taxonomies.php
3 weeks ago
tools.php
3 weeks ago
utility.php
3 weeks ago
wp-cli.php
3 weeks ago
wp-cli.php
125 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Custom Post Type UI WP-CLI. |
| 4 | * |
| 5 | * @package CPTUI |
| 6 | * @subpackage WP-CLI |
| 7 | * @author WebDevStudios |
| 8 | * @since 1.6.0 |
| 9 | * @license GPL-2.0+ |
| 10 | */ |
| 11 | |
| 12 | // phpcs:disable WebDevStudios.All.RequireAuthor |
| 13 | |
| 14 | /** |
| 15 | * Imports and exports Custom Post Type UI setting data. |
| 16 | */ |
| 17 | class CPTUI_Import_JSON extends WP_CLI_Command { |
| 18 | |
| 19 | public $args; |
| 20 | |
| 21 | public $assoc_args; |
| 22 | |
| 23 | public $type; |
| 24 | |
| 25 | public $data = []; |
| 26 | |
| 27 | /** |
| 28 | * Imports and parses JSON into CPTUI settings. |
| 29 | * |
| 30 | * ## Options |
| 31 | * |
| 32 | * [--type=<type>] |
| 33 | * : What type of import this is. Available options are `post_type` and `taxonomy`. |
| 34 | * |
| 35 | * [--data-path=<path>] |
| 36 | * : The server path to the file holding JSON data to import. Relative to PWD. |
| 37 | */ |
| 38 | public function import( $args, $assoc_args ) { |
| 39 | $this->args = $args; |
| 40 | $this->assoc_args = $assoc_args; |
| 41 | |
| 42 | if ( ! isset( $this->assoc_args['type'] ) ) { |
| 43 | WP_CLI::error( esc_html__( 'Please provide whether you are importing post types or taxonomies', 'custom-post-type-ui' ) ); |
| 44 | } |
| 45 | |
| 46 | if ( ! isset( $this->assoc_args['data-path'] ) ) { |
| 47 | WP_CLI::error( esc_html__( 'Please provide a path to the file holding your CPTUI JSON data.', 'custom-post-type-ui' ) ); |
| 48 | } |
| 49 | |
| 50 | $this->type = $assoc_args['type']; |
| 51 | |
| 52 | $json = file_get_contents( $this->assoc_args['data-path'] ); |
| 53 | |
| 54 | if ( empty( $json ) ) { |
| 55 | WP_CLI::error( esc_html__( 'No JSON data found', 'custom-post-type-ui' ) ); |
| 56 | } |
| 57 | |
| 58 | if ( 'post_type' === $this->type ) { |
| 59 | $this->data['cptui_post_import'] = json_decode( stripslashes_deep( trim( $json ) ), true ); |
| 60 | } |
| 61 | |
| 62 | if ( 'taxonomy' === $this->type ) { |
| 63 | $this->data['cptui_tax_import'] = json_decode( stripslashes_deep( trim( $json ) ), true ); |
| 64 | } |
| 65 | |
| 66 | $result = cptui_import_types_taxes_settings( $this->data ); |
| 67 | |
| 68 | if ( false === $result || 'import_fail' === $result ) { |
| 69 | WP_CLI::error( sprintf( esc_html__( 'An error on import occurred', 'custom-post-type-ui' ) ) ); |
| 70 | } else { |
| 71 | WP_CLI::success( |
| 72 | sprintf( |
| 73 | /* translators: Placeholders are just for HTML markup that doesn't need translated */ |
| 74 | esc_html__( 'Imported %s successfully', 'custom-post-type-ui' ), |
| 75 | $this->type |
| 76 | ) |
| 77 | ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Export CPTUI settings to file. |
| 83 | * |
| 84 | * ## Options |
| 85 | * |
| 86 | * [--type=<type>] |
| 87 | * : Which settings to export. Available options are `post_type` and `taxonomy`. |
| 88 | * |
| 89 | * [--dest-path=<path>] |
| 90 | * : The path and file to export to. Relative to PWD. |
| 91 | */ |
| 92 | public function export( $args, $assoc_args ) { |
| 93 | $this->args = $args; |
| 94 | $this->assoc_args = $assoc_args; |
| 95 | |
| 96 | if ( ! isset( $this->assoc_args['type'] ) ) { |
| 97 | WP_CLI::error( esc_html__( 'Please provide whether you are exporting your post types or taxonomies', 'custom-post-type-ui' ) ); |
| 98 | } |
| 99 | |
| 100 | if ( ! isset( $this->assoc_args['dest-path'] ) ) { |
| 101 | WP_CLI::error( esc_html__( 'Please provide a path to export your data to.', 'custom-post-type-ui' ) ); |
| 102 | } |
| 103 | |
| 104 | $this->type = $assoc_args['type']; |
| 105 | |
| 106 | if ( 'post_type' === $this->type ) { |
| 107 | $content = cptui_get_post_type_data(); |
| 108 | } |
| 109 | |
| 110 | if ( 'taxonomy' === $this->type ) { |
| 111 | $content = cptui_get_taxonomy_data(); |
| 112 | } |
| 113 | |
| 114 | $content = wp_json_encode( $content ); |
| 115 | $result = file_put_contents( $this->assoc_args['dest-path'], $content ); |
| 116 | |
| 117 | if ( false === $result ) { |
| 118 | WP_CLI::error( esc_html__( 'Error saving data.', 'custom-post-type-ui' ) ); |
| 119 | } |
| 120 | |
| 121 | WP_CLI::success( esc_html__( 'Successfully saved data to file.', 'custom-post-type-ui' ) ); |
| 122 | } |
| 123 | } |
| 124 | WP_CLI::add_command( 'cptui', 'CPTUI_Import_JSON' ); |
| 125 |