Skip to content
This repository has been archived by the owner on Feb 22, 2022. It is now read-only.

WP CLI autoupdate list and autoupdate enable/disable #8

Open
jeffpaul opened this issue Mar 25, 2020 · 3 comments
Open

WP CLI autoupdate list and autoupdate enable/disable #8

jeffpaul opened this issue Mar 25, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@jeffpaul
Copy link
Member

Migrated from: audrasjb/wp-autoupdates#30
Previously opened by: @NicolasKulka
Original description:

Command "wp autoupdate pluginlist" => get list plugins with autoupdate enable or disable

Command "wp autoupdate set --type=plugin --slug=wps-hide-login --action=enable" => Set plugin enable or disable autoupdate

<?php
if ( defined( 'WP_CLI' ) && WP_CLI ) {

	class AutoUpdateWPCLI {

		public function __construct() {

			// example constructor called when plugin loads

		}

		public function pluginlist() {
			$wp_auto_update_plugins = get_site_option( 'wp_auto_update_plugins', array() );

			$plugins = get_plugins();
			$items   = array();
			foreach ( $plugins as $plugin_path => $plugin ) {
				if ( in_array( $plugin_path, $wp_auto_update_plugins ) ) {
					$items[] = array(
						'name'       => $plugin_path,
						'autoupdate' => 'enable'
					);
					continue;
				}

				$items[] = array(
					'name'       => $plugin_path,
					'autoupdate' => 'disable'
				);
			}

			$fields = array( 'name', 'autoupdate' );
			WP_CLI\Utils\format_items( 'table', $items, $fields );
		}

		/**
		 * @param $args
		 * @param $assoc_args
		 *
		 * @throws \WP_CLI\ExitException
		 *
		 * Example : wp autoupdate set --type=plugin --slug=wps-hide-login --action=enable
		 */
		public function set( $args, $assoc_args ) {

			$wp_auto_update_plugins = get_site_option( 'wp_auto_update_plugins', array() );

			// process arguments
			$type   = WP_CLI\Utils\get_flag_value( $assoc_args, 'type' );
			$slug   = WP_CLI\Utils\get_flag_value( $assoc_args, 'slug' );
			$action = WP_CLI\Utils\get_flag_value( $assoc_args, 'action' );

			if ( 'plugin' === $type ) {

				if ( $action !== 'disable' && $action !== 'enable' ) {
					WP_CLI::error( $action . ' does not exist. Only action enable or disable exist.' );
				}

				$plugins = get_plugins( '/' . $slug );
				$file    = array_key_first( $plugins );

				$plugin = array( $slug . '/' . $file );

				if ( $action === 'disable' ) {
					$new_autoupdated_plugins = array_diff( $wp_auto_update_plugins, $plugin );
				} else {
					$new_autoupdated_plugins = array_merge( $wp_auto_update_plugins, $plugin );
				}

				$new_autoupdated_plugins = array_unique( $new_autoupdated_plugins );

				update_site_option( 'wp_auto_update_plugins', $new_autoupdated_plugins );

				WP_CLI::success( $slug . ' autoupdate is ' . $action  );
			} elseif ( 'theme' === $type ) {

			} else {
				WP_CLI::error( $type . ' does not exist. Only plugin or theme exist.' );
			}

		}

	}

	//if ( wp_autoupdates_is_plugins_auto_update_enabled() ) {
	WP_CLI::add_command( 'autoupdate', 'AutoUpdateWPCLI' );
	//}

}
@jeffpaul
Copy link
Member Author

Previously commented by: @NicolasKulka
Original comment:

Proposed by @wpserveur

@jeffpaul
Copy link
Member Author

Previously commented by: @pbiron
Original comment:

Yes, having WP_CLI commands for this would be very helpful.

When this gets merged into core, I suggest you do a PR against https://github.com/wp-cli/extension-command/.

As for the syntax of the commands:

  • I think that an "auto-update" column should just be added to wp plugin list and wp theme list (rather than adding a new command to list that value). That would allow things like wp plugin list --auto-update=enabled, etc.
  • add a new --auto-update assoc_arg, so that wp plugin wps-hide-login --auto-update would enable auto-updates and wp plugin wps-hide-login --auto-update=disable would disable them, etc.

p.s. I love WPS Hide Login and use it whenever I can :-)

@jeffpaul
Copy link
Member Author

Previously commented by: @NicolasKulka
Original comment:

Thank you for your return, yes I fully agree with you.

PS: thank you :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant