Use new code style

This commit is contained in:
David Stone
2025-02-07 19:02:33 -07:00
parent 0181024ae1
commit 8433379d90
672 changed files with 37107 additions and 45249 deletions

View File

@ -39,9 +39,8 @@ trait WP_CLI {
*/
public function get_wp_cli_command_base() {
return (!empty($this->wp_cli_command_base)) ? $this->wp_cli_command_base : $this->slug;
} // end get_wp_cli_command_base;
return (! empty($this->wp_cli_command_base)) ? $this->wp_cli_command_base : $this->slug;
}
/**
* Registers the routes. Should be called by the entity
@ -51,18 +50,15 @@ trait WP_CLI {
*/
public function enable_wp_cli() {
if (!defined('WP_CLI')) {
if ( ! defined('WP_CLI')) {
return;
} // end if;
}
$wp_cli_root = 'wu';
$this->set_wp_cli_enabled_sub_commands();
foreach ($this->wp_cli_enabled_sub_commands as $sub_command => $sub_command_data) {
\WP_CLI::add_command(
"{$wp_cli_root} {$this->get_wp_cli_command_base()} {$sub_command}",
$sub_command_data['callback'],
@ -70,10 +66,8 @@ trait WP_CLI {
'synopsis' => $sub_command_data['synopsis'],
)
);
} // end foreach;
} // end enable_wp_cli;
}
}
/**
* Set wP-CLI Sub-command enabled for this entity.
@ -105,34 +99,32 @@ trait WP_CLI {
/**
* Unset undesired Params.
*/
$params_to_remove = apply_filters('wu_cli_params_to_remove', array(
'id',
'model',
));
$params_to_remove = apply_filters(
'wu_cli_params_to_remove',
array(
'id',
'model',
)
);
$params = array_filter($params, fn($param) => !in_array($param, $params_to_remove, true));
$params = array_filter($params, fn($param) => ! in_array($param, $params_to_remove, true));
foreach ($sub_commands as $sub_command => &$sub_command_data) {
$sub_command_data['synopsis'] = array();
if (in_array($sub_command, array('get', 'update', 'delete'), true)) {
$sub_command_data['synopsis'][] = array(
'name' => 'id',
'type' => 'positional',
'description' => __('The id for the resource.', 'wp-ultimo'),
'optional' => false,
);
} // end if;
}
if (in_array($sub_command, array('list', 'update', 'create'), true)) {
$explanation_list = wu_rest_get_endpoint_schema($this->model_class, 'update');
foreach ($params as $name) {
$explanation = wu_get_isset($explanation_list, $name, array());
$type = wu_get_isset($explanation, 'type', 'assoc');
@ -140,37 +132,30 @@ trait WP_CLI {
$field = array(
'name' => $name,
'description' => wu_get_isset($explanation, 'description', __('No description found.', 'wp-ultimo')),
'optional' => !wu_get_isset($explanation, 'required'),
'optional' => ! wu_get_isset($explanation, 'required'),
'type' => 'assoc',
);
$options = wu_get_isset($explanation, 'options', array());
if ($options) {
$field['options'] = $options;
} // end if;
}
$sub_command_data['synopsis'][] = $field;
} // end foreach;
} // end if;
}
}
if (in_array($sub_command, array('create', 'update'), true)) {
$sub_command_data['synopsis'][] = array(
'name' => 'porcelain',
'type' => 'flag',
'description' => __('Output just the id when the operation is successful.', 'wp-ultimo'),
'optional' => true,
);
} // end if;
}
if (in_array($sub_command, array('list', 'get'), true)) {
$sub_command_data['synopsis'][] = array(
'name' => 'format',
'type' => 'assoc',
@ -194,10 +179,8 @@ trait WP_CLI {
'optional' => true,
'options' => array_merge(array('id'), $params),
);
} // end if;
} // end foreach;
}
}
$this->wp_cli_enabled_sub_commands = $sub_commands;
@ -216,8 +199,7 @@ trait WP_CLI {
$this->get_wp_cli_command_base(),
$this
);
} // end set_wp_cli_enabled_sub_commands;
}
/**
* Allows the additional of additional parameters.
*
@ -228,8 +210,7 @@ trait WP_CLI {
$model = new $this->model_class();
return array_keys($model->to_array());
} // end wp_cli_extra_parameters;
}
/**
* Returns the list of default fields, based on the table schema.
@ -242,8 +223,7 @@ trait WP_CLI {
$schema = $this->model_class::get_schema();
return array_column($schema, 'name');
} // end wp_cli_get_fields;
}
/**
* Returns a specific item.
@ -258,18 +238,15 @@ trait WP_CLI {
$item = $this->model_class::get_by_id($args[0]);
if (empty($item)) {
\WP_CLI::error('Invalid ID.');
}
} // end if;
$fields = (!empty($array_assoc['fields'])) ? $array_assoc['fields'] : $this->wp_cli_get_fields();
$fields = (! empty($array_assoc['fields'])) ? $array_assoc['fields'] : $this->wp_cli_get_fields();
$formatter = new \WP_CLI\Formatter($array_assoc, $fields);
$formatter->display_item($item->to_array());
} // end wp_cli_get_item;
}
/**
* Returns a list of items.
@ -281,7 +258,7 @@ trait WP_CLI {
*/
public function wp_cli_get_items($args, $array_assoc) {
$fields = (!empty($array_assoc['fields'])) ? $array_assoc['fields'] : $this->wp_cli_get_fields();
$fields = (! empty($array_assoc['fields'])) ? $array_assoc['fields'] : $this->wp_cli_get_fields();
unset($array_assoc['fields']);
@ -290,8 +267,7 @@ trait WP_CLI {
$items = array_map(fn($item) => $item->to_array(), $items);
\WP_CLI\Utils\format_items($array_assoc['format'], $items, $fields);
} // end wp_cli_get_items;
}
/**
* Creates an item.
@ -308,28 +284,19 @@ trait WP_CLI {
$success = $item->save();
if ($success === true) {
$item_id = $item->get_id();
if (!empty($array_assoc['porcelain'])) {
if ( ! empty($array_assoc['porcelain'])) {
\WP_CLI::line($item_id);
} else {
$message = sprintf('Item created with ID %d', $item_id);
\WP_CLI::success($message);
} // end if;
}
} else {
\WP_CLI::error($success);
} // end if;
} // end wp_cli_create_item;
}
}
/**
* Updates an item.
@ -344,37 +311,27 @@ trait WP_CLI {
$item = $this->model_class::get_by_id($args[0]);
if (empty($item)) {
\WP_CLI::error('Invalid ID.');
} // end if;
}
$porcelain = false;
if (!empty($array_assoc['porcelain'])) {
if ( ! empty($array_assoc['porcelain'])) {
$porcelain = true;
unset($array_assoc['porcelain']);
} // end if;
}
$params = $array_assoc;
foreach ($params as $param => $value) {
$set_method = "set_{$param}";
if ($param === 'meta') {
$item->update_meta_batch($value);
} elseif (method_exists($item, $set_method)) {
call_user_func(array($item, $set_method), $value);
} else {
$error_message = sprintf(
/* translators: 1. Object class name; 2. Set method name */
__('The %1$s object does not have a %2$s method', 'wp-ultimo'),
@ -383,36 +340,25 @@ trait WP_CLI {
);
\WP_CLI::error($error_message);
} // end if;
} // end foreach;
}
}
$success = $item->save();
if ($success) {
$item_id = $item->get_id();
if ($porcelain) {
\WP_CLI::line($item_id);
} else {
$message = sprintf('Item updated with ID %d', $item_id);
\WP_CLI::success($message);
} // end if;
}
} else {
\WP_CLI::error('Unexpected error. The item was not updated.');
} // end if;
} // end wp_cli_update_item;
}
}
/**
* Deletes an item.
@ -426,23 +372,15 @@ trait WP_CLI {
$item = $this->model_class::get_by_id($args[0]);
if (empty($item)) {
\WP_CLI::error('Invalid ID.');
} // end if;
}
$success = $item->delete();
if (is_wp_error($success) || !$success) {
if (is_wp_error($success) || ! $success) {
\WP_CLI::error('Unexpected error. The item was not deleted.');
} else {
\WP_CLI::success('Item deleted.');
} // end if;
} // end wp_cli_delete_item;
} // end trait WP_CLI;
}
}
}