forked from wpbullet/wp-menu-import-export-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.php
141 lines (123 loc) · 3.13 KB
/
command.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
<?php
// Menu import functionality trait.
require 'includes/trait-import.php';
// Menu export functionality trait.
require 'includes/trait-export.php';
/**
* Import a WordPress menus from the exported JSON file
* that generated the "wp menu export" command.
*
* @since 0.1.0
*/
class WPB_Import_Menu_Command extends WP_CLI_Command {
// Menu import functionality trait.
use WPB_Menu_Import;
/**
* Contains all menu locations.
*
* @since 0.1.0
* @access protected
*
* @var array $locations The menu locations.
*/
protected $locations;
/**
* Whether to overwrite the menus.
*
* @since 0.1.0
* @access protected
*
* @var boolean $overwrite_menus Whether to overwrite the menus.
*/
protected $overwrite_menus;
/**
* Start menu import using WP-CLI.
*
* ## OPTIONS
*
* <file>
* : The exported menu JSON file.
*
* [--overwrite]
* : Overwrite the existent menus.
*
* ## EXAMPLES
*
* # Import a menu.
* $ wp menu import my-menu.json
*
* # Import a menu with overriding the existent ones.
* $ wp menu import my-menu.json --overwrite
*/
public function __invoke( $args, $assoc_args ) {
list( $file ) = $args;
if ( ! file_exists( $file ) ) {
WP_CLI::error( 'File to import doesn\'t exist.' );
}
$args = wp_parse_args( $assoc_args );
$this->overwrite_menus = isset( $args['overwrite'] );
$menu_imported = $this->import( $file );
if ( is_wp_error( $menu_imported ) ) {
WP_CLI::error( $menu_imported->get_error_message() );
}
WP_CLI::line();
WP_CLI::success( 'The import was successful.' );
}
}
/**
* Export the WordPress menus into a JSON file that will
* be used later to import.
*
* @since 0.1.0
*/
class WPB_Export_Menu_Command extends WP_CLI_Command {
// Menu import functionality trait.
use WPB_Menu_Export;
/**
* Export a WordPress Menu.
*
* ## OPTIONS
*
* <menu>...
* : The name, slug or term ID for the menu(s).
*
* [--all]
* : Export all WordPress menus.
*
* [--filename[=<value>]]
* : Specify the file name.
*
* ## EXAMPLES
*
* # Export all menus.
* $ wp menu export --all
*
* # Export menus by name.
* $ wp menu export "My Awesome Menu" "Mobile Menu"
*
* # Export menus by term id.
* $ wp menu export 80 81 82
*
* # Export menus by slug.
* $ wp menu export menu-slug-1 menu-slug-2
*
* # Export all menus with a custom file name.
* $ wp menu export --all --filename="custom-filename.json"
*/
public function __invoke( $args, $assoc_args ) {
$menu_exported = $this->export( $args, wp_parse_args( $assoc_args ) );
if ( is_wp_error( $menu_exported ) ) {
WP_CLI::error( $menu_exported->get_error_message() );
}
WP_CLI::line();
WP_CLI::success( 'The menu export was successful.' );
}
}
$export_synopsis = array(
'synopsis' => array(
'name' => 'menu',
'optional' => true,
),
);
WP_CLI::add_command( 'menu import', 'WPB_Import_Menu_Command' );
WP_CLI::add_command( 'menu export', 'WPB_Export_Menu_Command', $export_synopsis );