From 103fbd2783c6c353a352df211947140e409e4473 Mon Sep 17 00:00:00 2001 From: Abhijit Bhatnagar Date: Fri, 28 Jun 2024 00:06:02 +0530 Subject: [PATCH 1/3] intial commit for wp cli support --- bootstrap.php | 6 +++ includes/NFD_CLI.php | 90 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 includes/NFD_CLI.php diff --git a/bootstrap.php b/bootstrap.php index 26549ed..bdec421 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -33,6 +33,12 @@ function () { 'isHidden' => true, ) ); + + // Register the custom command with WP_CLI + if ( defined( '\\WP_CLI' ) && \WP_CLI ) { + require_once __DIR__ . '/includes/NFD_CLI.php'; + WP_CLI::add_command( 'newfold', 'NFD_CLI' ); + } } ); diff --git a/includes/NFD_CLI.php b/includes/NFD_CLI.php new file mode 100644 index 0000000..71dcd0c --- /dev/null +++ b/includes/NFD_CLI.php @@ -0,0 +1,90 @@ + true, + "hasAISiteGen" => true, + ]; + } + );'; + + // create a php file that overrides the AI capabilities + if ( file_put_contents( self::capabilities_file_path(), $enable_ai_filter ) !== false ) { + WP_CLI::success( "AI Capabilities Enabled." ); + } else { + WP_CLI::error( "Could not enable AI capabilities." ); + } + + // Update the hiive token in the option, it gets automatically encrypted while saving and decrypted while reading + if ( update_option( 'nfd_data_token', $token) ) { + WP_CLI::success( "Hiive token encrypted and saved." ); + } + } + + /** + * Disable AI and AI Sitegen capability. + * + * @return null + */ + private static function disable() { + + // delete the php file that overrides the AI capabilities + if ( unlink( self::capabilities_file_path() ) !== false ) { + WP_CLI::success( "AI Capabilities Disabled." ); + } else { + WP_CLI::error( "Could not disable AI capabilities." ); + } + + // delete the Hiive token + if ( delete_option( 'nfd_data_token' ) ) { + WP_CLI::success( "Hiive token deleted." ); + } + + } +} + From 04492e6bd990fe670711585869e305ac894ccd9b Mon Sep 17 00:00:00 2001 From: Abhijit Bhatnagar Date: Fri, 28 Jun 2024 00:30:24 +0530 Subject: [PATCH 2/3] adding some command documentation --- includes/NFD_CLI.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/includes/NFD_CLI.php b/includes/NFD_CLI.php index 71dcd0c..88875e6 100644 --- a/includes/NFD_CLI.php +++ b/includes/NFD_CLI.php @@ -13,7 +13,12 @@ private static function capabilities_file_path() { /** * Enables and disables AI and AI Sitegen Capabilities. - * + * + * ## EXAMPLES + * + * wp newfold ai enable + * wp newfold ai disable🌞 + * * @when after_wp_load */ public function ai( $args ) { @@ -26,6 +31,9 @@ public function ai( $args ) { self::disable(); break; default: + WP_CLI::warning( "No action provided" ); + WP_CLI::log( "Usage: wp newfold ai enable " ); + WP_CLI::log( " wp newfold ai disable" ); break; } } From ddd229828552707d16e546535b874fbd476734d3 Mon Sep 17 00:00:00 2001 From: Abhijit Bhatnagar Date: Fri, 28 Jun 2024 01:55:27 +0530 Subject: [PATCH 3/3] fixed linting issues and use WP file functions --- bootstrap.php | 2 +- includes/NFD_CLI.php | 75 ++++++++++++++++++++++++++++++-------------- 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/bootstrap.php b/bootstrap.php index bdec421..2188cd3 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -35,7 +35,7 @@ function () { ); // Register the custom command with WP_CLI - if ( defined( '\\WP_CLI' ) && \WP_CLI ) { + if ( defined( '\\WP_CLI' ) && \WP_CLI ) { require_once __DIR__ . '/includes/NFD_CLI.php'; WP_CLI::add_command( 'newfold', 'NFD_CLI' ); } diff --git a/includes/NFD_CLI.php b/includes/NFD_CLI.php index 88875e6..860b3b8 100644 --- a/includes/NFD_CLI.php +++ b/includes/NFD_CLI.php @@ -1,5 +1,8 @@ * wp newfold ai disable🌞 - * + * * @when after_wp_load + * + * @param string $args arguments passed to this from the command line. */ public function ai( $args ) { - switch( $args[0] ) { + switch ( $args[0] ) { case 'enable': self::enable( $args[1] ); break; @@ -31,9 +36,9 @@ public function ai( $args ) { self::disable(); break; default: - WP_CLI::warning( "No action provided" ); - WP_CLI::log( "Usage: wp newfold ai enable " ); - WP_CLI::log( " wp newfold ai disable" ); + \WP_CLI::warning( 'No action provided' ); + \WP_CLI::log( 'Usage: wp newfold ai enable ' ); + \WP_CLI::log( ' wp newfold ai disable' ); break; } } @@ -41,12 +46,13 @@ public function ai( $args ) { /** * Enable AI and AI Sitegen capability. * + * @param string $token Hiive token to be saved as an option. * @return null */ private static function enable( $token ) { - if( empty( $token ) ) { - WP_CLI::error( "Hiive token not provided. Cannot enable AI capabilities." ); + if ( empty( $token ) ) { + \WP_CLI::error( 'Hiive token not provided. Cannot enable AI capabilities.' ); return; } @@ -61,38 +67,59 @@ function () { } );'; + // Initialize the WP Filesystem + if ( ! function_exists( 'WP_Filesystem' ) ) { + require_once ABSPATH . 'wp-admin/includes/file.php'; + } + WP_Filesystem(); + global $wp_filesystem; + + if ( ! $wp_filesystem ) { + WP_CLI::error( 'Failed to initialize the WP Filesystem.' ); + } + // create a php file that overrides the AI capabilities - if ( file_put_contents( self::capabilities_file_path(), $enable_ai_filter ) !== false ) { - WP_CLI::success( "AI Capabilities Enabled." ); - } else { - WP_CLI::error( "Could not enable AI capabilities." ); + if ( $wp_filesystem->put_contents( self::capabilities_file_path(), $enable_ai_filter ) === false ) { + \WP_CLI::error( 'Could not enable AI capabilities.' ); } + \WP_CLI::success( 'AI Capabilities Enabled.' ); // Update the hiive token in the option, it gets automatically encrypted while saving and decrypted while reading - if ( update_option( 'nfd_data_token', $token) ) { - WP_CLI::success( "Hiive token encrypted and saved." ); + if ( update_option( 'nfd_data_token', $token ) ) { + \WP_CLI::success( 'Hiive token encrypted and saved.' ); } } /** * Disable AI and AI Sitegen capability. - * - * @return null */ private static function disable() { + // Initialize the WP Filesystem + if ( ! function_exists( 'WP_Filesystem' ) ) { + require_once ABSPATH . 'wp-admin/includes/file.php'; + } + WP_Filesystem(); + global $wp_filesystem; + + if ( ! $wp_filesystem ) { + WP_CLI::error( 'Failed to initialize the WP Filesystem.' ); + } + + // Check if the file exists + if ( ! $wp_filesystem->exists( self::capabilities_file_path() ) ) { + WP_CLI::error( 'Could not disable AI capabilities.' ); + } + // delete the php file that overrides the AI capabilities - if ( unlink( self::capabilities_file_path() ) !== false ) { - WP_CLI::success( "AI Capabilities Disabled." ); - } else { - WP_CLI::error( "Could not disable AI capabilities." ); + if ( ! $wp_filesystem->delete( self::capabilities_file_path() ) ) { + \WP_CLI::error( 'Could not disable AI capabilities.' ); } + \WP_CLI::success( 'AI Capabilities Disabled.' ); // delete the Hiive token if ( delete_option( 'nfd_data_token' ) ) { - WP_CLI::success( "Hiive token deleted." ); + \WP_CLI::success( 'Hiive token deleted.' ); } - } } -