Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Runner::register_route_commands( $rest_command,  $route,  $route_data )

Register WP-CLI commands for all endpoints on a route


Parameters Parameters

(string) (Required)

$endpoints

(array) (Required)


Top ↑

Source Source

File: libraries/lifterlms-cli/src/Commands/Restful/Runner.php

	private static function register_route_commands( $rest_command, $route, $route_data ) {

		$resource = str_replace( array( 'llms_', '_' ), array( '', '-' ), $route_data['schema']['title'] );
		$parent   = "llms {$resource}";

		$supported_commands = self::get_supported_commands( $route, $route_data );
		foreach ( $supported_commands as $command => $endpoint_args ) {

			$synopsis = array();
			if ( in_array( $command, array( 'delete', 'get', 'update' ) ) ) {
				$synopsis[] = array(
					'name'        => 'id',
					'type'        => 'positional',
					'description' => 'The id for the resource.',
					'optional'    => false,
				);
			}

			foreach ( $endpoint_args as $name => $args ) {
				$arg_reg = array(
					'name'        => $name,
					'type'        => 'assoc',
					'description' => ! empty( $args['description'] ) ? $args['description'] : '',
					'optional'    => empty( $args['required'] ) ? true : false,
				);
				foreach ( array( 'enum', 'default' ) as $key ) {
					if ( isset( $args[ $key ] ) ) {
						$new_key             = 'enum' === $key ? 'options' : $key;
						$arg_reg[ $new_key ] = $args[ $key ];
					}
				}
				$synopsis[] = $arg_reg;
			}

			if ( in_array( $command, array( 'list', 'get' ) ) ) {
				$synopsis[] = array(
					'name'        => 'fields',
					'type'        => 'assoc',
					'description' => 'Limit response to specific fields. Defaults to all fields.',
					'optional'    => true,
				);
				$synopsis[] = array(
					'name'        => 'field',
					'type'        => 'assoc',
					'description' => 'Get the value of an individual field.',
					'optional'    => true,
				);
				$synopsis[] = array(
					'name'        => 'format',
					'type'        => 'assoc',
					'description' => 'Render response in a particular format.',
					'optional'    => true,
					'default'     => 'table',
					'options'     => array(
						'table',
						'json',
						'csv',
						'ids',
						'yaml',
						'count',
						'headers',
						'body',
						'envelope',
					),
				);
			}

			if ( in_array( $command, array( 'create', 'update', 'delete' ) ) ) {
				$synopsis[] = array(
					'name'        => 'porcelain',
					'type'        => 'flag',
					'description' => 'Output just the id when the operation is successful.',
					'optional'    => true,
				);
			}

			$methods = array(
				'list'   => 'list_items',
				'create' => 'create_item',
				'delete' => 'delete_item',
				'get'    => 'get_item',
				'update' => 'update_item',
			);

			// Add the root command, eg: wp llms course.
			\WP_CLI::add_command(
				"{$parent}",
				$rest_command,
				array(
					'shortdesc' => self::get_command_root_desc( $resource ),
				)
			);

			// Register main subcommands, eg: wp llms course create, wp llms course delete, etc...
			\WP_CLI::add_command(
				"{$parent} {$command}",
				array( $rest_command, $methods[ $command ] ),
				array(
					'shortdesc'     => self::get_command_short_desc( $command, $resource ),
					'synopsis'      => $synopsis,
					'before_invoke' => array( __CLASS__, 'before_invoke_command' ),
				)
			);

			// If listing is supported, add the diff command.
			if ( 'list' === $command ) {
				\WP_CLI::add_command(
					"{$parent} diff",
					array( $rest_command, 'diff_items' ),
					array(
						'shortdesc' => self::get_command_short_desc( 'diff', $resource ),
						'before_invoke' => array( __CLASS__, 'before_invoke_command' ),
					)
				);
			}

			// If creation is supported, add the generate command.
			if ( 'create' === $command ) {
				\WP_CLI::add_command(
					"{$parent} generate",
					array( $rest_command, 'generate_items' ),
					array(
						'shortdesc' => self::get_command_short_desc( 'generate', $resource ),
						'synopsis'  => self::get_generate_command_synopsis( $synopsis ),
						'before_invoke' => array( __CLASS__, 'before_invoke_command' ),
					)
				);
			}


			// If updating and getting is supported, add the edit command.
			if ( 'update' === $command && array_key_exists( 'get', $supported_commands ) ) {
				$synopsis   = array();
				$synopsis[] = array(
					'name'        => 'id',
					'type'        => 'positional',
					'description' => 'The id for the resource.',
					'optional'    => false,
				);
				\WP_CLI::add_command(
					"{$parent} edit",
					array( $rest_command, 'edit_item' ),
					array(
						'shortdesc' => self::get_command_short_desc( 'edit', $resource ),
						'synopsis'  => $synopsis,
						'before_invoke' => array( __CLASS__, 'before_invoke_command' ),
					)
				);
			}
		}
	}

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.