Newer
Older
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
<?php
/**
* Access the CiviCRM API v3.
*
* ## EXAMPLES
*
* $ wp civicrm api contact.get id=10
* Output here.
*
* @since 5.69
*/
class CLI_Tools_CiviCRM_Command_API_V3 extends CLI_Tools_CiviCRM_Command {
/**
* Object fields.
*
* @var array
*/
protected $obj_fields = [
'id',
];
/**
* Access the CiviCRM API v3.
*
* ## OPTIONS
*
* <args>...
* : The API query passed as arguments.
*
* [--input=<input>]
* : Specify the input in a particular format.
* ---
* default: args
* options:
* - args
* - json
* ---
*
* [--timezone=<timezone>]
* : The CiviCRM timezone string. Defaults to the WordPress `timezone_string` setting.
*
* [--format=<format>]
* : Render output in a particular format. The "table" format can only be used when retrieving a single item.
* ---
* default: pretty
* options:
* - pretty
* - json
* - table
* ---
*
* ## EXAMPLES
*
* $ wp civicrm api contact.get id=10
* $ wp civicrm api contact.get id=10 --format=json
* $ wp civicrm api group.get id=1 --format=table
* $ echo '{"id":10, "api.Email.get": 1}' | wp cv api contact.get --input=json
*
* @since 5.69
*
* @param array $args The WP-CLI positional arguments.
* @param array $assoc_args The WP-CLI associative arguments.
*/
public function __invoke($args, $assoc_args) {
// Grab associative arguments.
$input_format = (string) \WP_CLI\Utils\get_flag_value($assoc_args, 'input', 'args');
$site_timezone = (string) \WP_CLI\Utils\get_flag_value($assoc_args, 'timezone', $this->site_timezone_get());
$format = (string) \WP_CLI\Utils\get_flag_value($assoc_args, 'format', 'pretty');
// Get the Entity and Action from the first positional argument.
list($entity, $action) = explode('.', $args[0]);
array_shift($args);
// Parse params.
$defaults = ['version' => 3];
switch ($input_format) {
// Input params supplied via args.
case 'args':
$params = $defaults;
foreach ($args as $arg) {
preg_match('/^([^=]+)=(.*)$/', $arg, $matches);
$params[$matches[1]] = $matches[2];
}
break;
// Input params supplied via json.
case 'json':
$json = stream_get_contents(STDIN);
if (empty($json)) {
$params = $defaults;
}
else {
$params = array_merge($defaults, json_decode($json, TRUE));
}
break;
default:
WP_CLI::error(sprintf('Unknown format: %s', $input_format));
break;
}
// Bootstrap CiviCRM.
$this->bootstrap_civicrm();
// CRM-18062: Configure timezone for CiviCRM.
$current_timezone = date_default_timezone_get();
if ($site_timezone) {
date_default_timezone_set($site_timezone);
CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone();
}
// Now call the CiviCRM API.
$result = civicrm_api($entity, $action, $params);
// Restore timezone.
if ($current_timezone) {
date_default_timezone_set($current_timezone);
}
switch ($format) {
// Pretty-print output (default).
case 'pretty':
WP_CLI::log(print_r($result, TRUE));
break;
// Display output as json.
case 'json':
WP_CLI::log(json_encode($result));
break;
// Display output as table.
case 'table':
$assoc_args['format'] = $format;
if (count($result['values']) === 1) {
$item = array_pop($result['values']);
$assoc_args['fields'] = array_keys($item);
$formatter = $this->formatter_get($assoc_args);
$formatter->display_item($item);
}
else {
// Give up and log usual output.
WP_CLI::log(print_r($result, TRUE));
// phpcs:disable
/*
// Testing whether we can do this. It's hard, but kinda works.
$fields_query = civicrm_api3($entity, 'getfields', [
'api_action' => $action,
]);;
$fields = array_keys($fields_query['values']);
//WP_CLI::log(print_r($fields, TRUE));
//WP_CLI::log(print_r($result['values'], TRUE));
$assoc_args['fields'] = $fields;
//WP_CLI::log(print_r($assoc_args['fields'], TRUE));
// Cast items as objects.
array_walk($result['values'], function( &$item ) use ( $fields ) {
foreach ($fields as $field) {
// Make sure the array has all keys.
if (!array_key_exists($field, $item)) {
$item[$field] = '';
}
}
$item = (object) $item;
});
//WP_CLI::log(print_r($result['values'], TRUE));
$formatter = $this->formatter_get($assoc_args);
//WP_CLI::log(print_r($formatter, TRUE));
$formatter->display_items($result['values']);
*/
// phpcs:enable
}
break;
default:
WP_CLI::error(sprintf('Unknown format: %s', $format));
}
}
}