Main
Manage LifterLMS add-on plugins and themes.
Contents
Source Source
File: libraries/lifterlms-cli/src/Commands/AddOn/Main.php
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 195 196 197 198 199 200 201 202 | class Main extends AbstractCommand { // Include subcommands. use Activate, ChannelSet, Deactivate, Enumerate, Get, Install, Uninstall, Update; /** * Accepts an add-on array and converts it to the format used by the output method * * @since 0.0.1 * * @param array|LLMS_Add_On $item_or_addon Add-on object or add-on item array array from `llms_get_add_ons()`. * @return array Associative array containing all possible fields as used by the output method. */ private function format_item( $item_or_addon ) { $addon = is_array ( $item_or_addon ) ? llms_get_add_on( $item_or_addon ) : $item_or_addon ; $formatted = array ( 'name' => $addon ->get( 'slug' ), 'description' => $addon ->get( 'description' ), 'status' => $addon ->get_status(), 'license' => str_replace ( 'license_' , '' , $addon ->get_license_status() ), 'update' => $addon ->has_available_update() ? 'available' : 'none' , 'version' => $addon ->is_installed() ? $addon ->get_installed_version() : 'N/A' , 'update_version' => $addon ->get( 'version' ), 'title' => $addon ->get( 'title' ), 'channel' => $addon ->get_channel_subscription(), 'type' => $addon ->get( 'type' ), 'file' => $addon ->get( 'update_file' ), 'permalink' => $addon ->get( 'permalink' ), 'changelog' => $addon ->get( 'changelog' ), 'documentation' => $addon ->get( 'documentation' ), ); return $formatted ; } /** * Retrieve an array of available add-on slugs based on the supplied query criteria. * * This function passes data to `wp llms addon list` with specific filters and returns an associative * array of add-on slugs from that list. * * This is used, mostly, to generate a list of available addons for various commands which provide an `--all` flag/option. * * @since 0.0.1 * * @param string $status Add-on status, passed as the `--status` option to `llms addon list`. * @param bool $check_license Whether or not the add-on should be licensed. This is used to determine what is installable / upgradeable. * @param string $type Add-on type. Accepts 'all' (default), 'plugin' or 'theme'. * @return string[] Array of add-on slugs meeting the specified filters. */ private function get_available_addons( $status , $check_license , $type = 'all' ) { $list = \WP_CLI::runcommand( "llms addon list --format=json --status={$status} --fields=name,license,type" , array ( 'return' => true, ) ); $list = array_filter ( json_decode( $list , true ), function ( $item ) use ( $check_license , $type ) { return ( ( $check_license && 'active' === $item [ 'license' ] ) || ! $check_license ) && ( 'all' === $type || $type === $item [ 'type' ] ); } ); return wp_list_pluck( $list , 'name' ); } /** * Retrieves an optionally filtered list of add-ons for use in the `list` command. * * @since 0.0.1 * * @param array $assoc_args Associative array of command options. * @param string $filter_field The optional name of the field to filter results by. * @return array[] Array of add-on items. */ private function get_filtered_items( $assoc_args , $filter_field = '' ) { $addons = llms_get_add_ons(); $list = array_filter ( $addons [ 'items' ], function ( $item ) { return // Skip anything without a slug. ! empty ( $item [ 'slug' ] ) && // Skip the LifterLMS core. 'lifterlms' !== $item [ 'slug' ] && // Skip third party add-ons. ! in_array( 'third-party' , array_keys ( $item [ 'categories' ] ), true ); } ); // Format remaining items. $list = array_map ( array ( $this , 'format_item' ), $list ); // Filter by field value. if ( $filter_field ) { $field_val = $assoc_args [ $filter_field ]; $list = array_filter ( $list , function ( $item ) use ( $filter_field , $field_val ) { return $item [ $filter_field ] === $field_val ; } ); } // Alpha sort the list by slug. usort( $list , function ( $a , $b ) { return strcmp ( $a [ 'name' ], $b [ 'name' ] ); } ); return $list ; } /** * Reusable loop function for handling commands which accept one or more slugs as the commands first argument * * @since 0.0.1 * * @param string[] $slugs Array of add-on slugs, with or without the `lifterlms-` prefix. * @param array $assoc_args Associative array of command options from the original command. * @param string $callback Name of the method to use for handling a single add-on for the given command. * The callback should accept three arguments: * + @type string $slug Add-on slug for the current item. * + @type LLMS_Add_On $addon Add-on object for the current item. * + @type array $assoc_args Array of arguments from the initial command. * The callback should return a truthy to signal success and * a falsy to signal an error. * @return array { * Associative arrays containing details on the errors and successes encountered during the loop. * * @type int $errors Number of errors encountered in the loop. * @type int $successes Number of success encountered in the loop. * } */ private function loop( $slugs , $assoc_args , $callback ) { $successes = 0; $errors = 0; foreach ( $slugs as $slug ) { if ( empty ( $slug ) ) { \WP_CLI::warning( 'Ignoring ambiguous empty slug value.' ); continue ; } $addon = $this ->get_addon( $slug , true, 'warning' ); if ( empty ( $addon ) ) { $errors ++; continue ; } if ( ! $this -> $callback ( $slug , $addon , $assoc_args ) ) { $errors ++; continue ; } $successes ++; } return compact( 'errors' , 'successes' ); } } |
Expand full source code Collapse full source code View on GitHub
Methods Methods
- format_item — Accepts an add-on array and converts it to the format used by the output method
- get_available_addons — Retrieve an array of available add-on slugs based on the supplied query criteria.
- get_filtered_items — Retrieves an optionally filtered list of add-ons for use in the `list` command.
- loop — Reusable loop function for handling commands which accept one or more slugs as the commands first argument
Changelog Changelog
Version | Description |
---|---|
0.0.1 | Introduced. |