From 2525dcf688ce2212276c1a433e33071e8991ed3f Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Fri, 13 May 2022 16:05:06 -0400 Subject: [PATCH 1/3] add last updated for blog_public setting --- cssllc-admin-last-updated.php | 179 ++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 cssllc-admin-last-updated.php diff --git a/cssllc-admin-last-updated.php b/cssllc-admin-last-updated.php new file mode 100644 index 0000000..f40239a --- /dev/null +++ b/cssllc-admin-last-updated.php @@ -0,0 +1,179 @@ + + + + + print_json_object() + * + * @return void + */ + public function action__admin_print_scripts() : void { + if ( 'admin_print_scripts' !== current_action() ) { + return; + } + + $this->print_json_object(); + } + + /** + * Action: admin_print_footer_scripts + * + * Print JavaScript to add "Last Updated" text. + * + * @return void + */ + public function action__admin_print_footer_scripts() : void { + if ( 'admin_print_footer_scripts' !== current_action() ) { + return; + } + + if ( 'options-reading' !== get_current_screen()->base ) { + return; + } + ?> + + + + update_option() + * + * @action update_option_{$option_name} + * @action delete_option_{$option_name} + * + * @return void + */ + public function save_option_timestamp( ...$args ) : void { + + // action: delete_option_{$option_name} + $option_name = $args[0]; + + // action: update_option_{$option_name} + if ( 3 === count( $args ) ) { + $old_value = $args[0]; + $new_value = $args[1]; + $option_name = $args[2]; + + if ( $old_value === $new_value ) { + return; + } + } + + $this->update_option( $option_name ); + } + +} + +if ( ! is_admin() ) { + return; +} + +CSSLLC_Admin_Last_Updated::init(); \ No newline at end of file From c6c8f4d716450496bdb1f29ebdc4b1d3188cc92d Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Fri, 13 May 2022 22:19:26 -0400 Subject: [PATCH 2/3] save --- cssllc-admin-last-updated.php | 74 +++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 13 deletions(-) diff --git a/cssllc-admin-last-updated.php b/cssllc-admin-last-updated.php index f40239a..190c624 100644 --- a/cssllc-admin-last-updated.php +++ b/cssllc-admin-last-updated.php @@ -16,17 +16,6 @@ public static function init() : void { new self; } - /** - * Generate option name for timestamp. - * - * @param string $id - * - * @return string - */ - protected static function timestamp_option_name( string $id ) : string { - return sprintf( '_cssllc_admin_last_updated_%s', $id ); - } - /** * Construct. */ @@ -37,6 +26,12 @@ protected function __construct() { add_action( 'update_option_blog_public', array( $this, 'save_option_timestamp' ), 10, 3 ); add_action( 'delete_option_blog_public', array( $this, 'save_option_timestamp' ) ); + + if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { + return; + } + + WP_CLI::add_command( 'admin-last-updated get', array( $this, 'cli__get' ) ); } @@ -138,7 +133,7 @@ public function action__admin_print_footer_scripts() : void { /** * Action: update_option_{$option_name} * - * Save timestamp on option update. + * Save timestamp on option update/delete. * * @param mixed $old_value * @param mixed $new_value @@ -170,9 +165,62 @@ public function save_option_timestamp( ...$args ) : void { $this->update_option( $option_name ); } + /** + * CLI command: admin-last-updated get + * + * @param array $args + * @param array $assoc + * + * @return void + */ + public function cli__get( array $args, array $assoc = array() ) : void { + $id = $args[0]; + $option = get_option( self::OPTION_NAME, array() ); + + if ( + empty( $option ) + || empty( $option[ $id ] ) + ) { + WP_CLI::warning( sprintf( 'No record of last update to ´%s´', $id ) ); + return; + } + + $updated = $option[ $id ]; + + if ( empty( $assoc[ 'seconds' ] ) ) { + $updated = date( 'c', $updated ); + } + + WP_CLI::line( $updated ); + } + + /** + * CLI command: admin-last-updated list + * + * @param array $args + * @param array $assoc + * + * @return void + */ + public function cli__list( array $args, array $assoc = array() ) : void { + + } + + /** + * CLI command: admin-last-updated clear + * + * @param array $args + * @param array $assoc + * + * @return void + */ + public function cli__clear( array $args, array $assoc = array() ) : void { + + } + } -if ( ! is_admin() ) { +if ( ! is_admin() && ( ! defined( 'WP_CLI' ) || ! WP_CLI ) ) { return; } From d0cc836e70d67b7b894ace2f0acd2f6de0930313 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Fri, 13 May 2022 23:29:39 -0400 Subject: [PATCH 3/3] save --- cssllc-admin-last-updated.php | 130 +++++++++++++++++++++++++++------- 1 file changed, 105 insertions(+), 25 deletions(-) diff --git a/cssllc-admin-last-updated.php b/cssllc-admin-last-updated.php index 190c624..dfcac8c 100644 --- a/cssllc-admin-last-updated.php +++ b/cssllc-admin-last-updated.php @@ -4,6 +4,11 @@ class CSSLLC_Admin_Last_Updated { const OPTION_NAME = '_cssllc_admin_last_updated'; + /** + * Initialize. + * + * @return void + */ public static function init() : void { static $once = false; @@ -31,7 +36,9 @@ protected function __construct() { return; } - WP_CLI::add_command( 'admin-last-updated get', array( $this, 'cli__get' ) ); + WP_CLI::add_command( 'admin-last-updated clear', array( $this, 'cli__clear' ) ); + WP_CLI::add_command( 'admin-last-updated get', array( $this, 'cli__get' ) ); + WP_CLI::add_command( 'admin-last-updated list', array( $this, 'cli__list' ) ); } @@ -42,7 +49,7 @@ protected function __construct() { * * @return void */ - protected function update_option( string $id ) : void { + protected function update_timestamp( string $id ) : void { $option = get_option( self::OPTION_NAME, array() ); if ( empty( $option ) ) { @@ -101,7 +108,7 @@ public function action__admin_print_scripts() : void { /** * Action: admin_print_footer_scripts * - * Print JavaScript to add "Last Updated" text. + * Print JavaScript to add "Last updated" text. * * @return void */ @@ -120,10 +127,15 @@ public function action__admin_print_footer_scripts() : void { ( function() { var data = window.cssllc_admin_last_updated; - if ( document.querySelector( 'tr.option-site-visibility th' ) && data.blog_public ) { - document.querySelector( 'tr.option-site-visibility th' ).innerHTML += '
Last updated: ' + data.blog_public + ''; + if ( ! document.querySelector( 'tr.option-site-visibility th' ) ) { + return; } - + + if ( ! data.blog_public ) { + data.blog_public = 'Unknown'; + } + + document.querySelector( 'tr.option-site-visibility th' ).innerHTML += '
Last updated: ' + data.blog_public + ''; } () ); @@ -139,7 +151,7 @@ public function action__admin_print_footer_scripts() : void { * @param mixed $new_value * @param mixed $option_name * - * @uses $this->update_option() + * @uses $this->update_timestamp() * * @action update_option_{$option_name} * @action delete_option_{$option_name} @@ -148,10 +160,10 @@ public function action__admin_print_footer_scripts() : void { */ public function save_option_timestamp( ...$args ) : void { - // action: delete_option_{$option_name} + // Action: delete_option_{$option_name} $option_name = $args[0]; - // action: update_option_{$option_name} + // Action: update_option_{$option_name} if ( 3 === count( $args ) ) { $old_value = $args[0]; $new_value = $args[1]; @@ -162,7 +174,52 @@ public function save_option_timestamp( ...$args ) : void { } } - $this->update_option( $option_name ); + $this->update_timestamp( $option_name ); + } + + /** + * CLI command: admin-last-updated clear + * + * @param array $ids + * @param array $assoc + * + * @return void + * + * @todo test + */ + public function cli__clear( array $ids, array $assoc = array() ) : void { + $option = ( array ) get_option( self::OPTION_NAME, array() ); + $count = count( $ids ) || count( $option ); + $count_format = _n( '%d timestamp', '%d timestamps', $count ); + $question_format = 'Are you sure you want to clear ' . $count_format . '?'; + $question = sprintf( $format, $count ); + + WP_CLI::confirm( $question, $assoc ); + + // Clear all timestamps. + if ( empty( $ids ) ) { + $cleared = delete_option( self::OPTION_NAME ); + + if ( ! $cleared ) { + WP_CLI::error( sprintf( 'Unable to clear ' . $count_format, $count ) ); + } + + WP_CLI::success( sprintf( 'Cleared ' . $count_format, $count ) ); + } + + // Clear specified timestamps. + foreach ( $ids as $id ) { + unset( $option[ $id ] ); + WP_CLI::debug( sprintf( 'Cleared `%s`', $id ) ); + } + + $updated = update_option( self::OPTION_NAME, $option, false ); + + if ( ! $updated ) { + WP_CLI::error( sprintf( 'Unable to clear ' . $count_format, $count ) ); + } + + WP_CLI::success( sprintf( 'Cleared ' . $count_format, $count ) ); } /** @@ -172,22 +229,24 @@ public function save_option_timestamp( ...$args ) : void { * @param array $assoc * * @return void + * + * @todo test */ public function cli__get( array $args, array $assoc = array() ) : void { $id = $args[0]; - $option = get_option( self::OPTION_NAME, array() ); + $option = ( array ) get_option( self::OPTION_NAME, array() ); if ( empty( $option ) || empty( $option[ $id ] ) ) { - WP_CLI::warning( sprintf( 'No record of last update to ´%s´', $id ) ); + WP_CLI::warning( sprintf( 'No record of last update to `%s`', $id ) ); return; } $updated = $option[ $id ]; - if ( empty( $assoc[ 'seconds' ] ) ) { + if ( empty( $assoc['seconds'] ) ) { $updated = date( 'c', $updated ); } @@ -201,26 +260,47 @@ public function cli__get( array $args, array $assoc = array() ) : void { * @param array $assoc * * @return void + * + * @todo test */ public function cli__list( array $args, array $assoc = array() ) : void { + $option = ( array ) get_option( self::OPTION_NAME, array() ); - } - - /** - * CLI command: admin-last-updated clear - * - * @param array $args - * @param array $assoc - * - * @return void - */ - public function cli__clear( array $args, array $assoc = array() ) : void { + if ( empty( $option ) ) { + WP_CLI::warning( sprintf( 'No records', $id ) ); + return; + } + + $items = array(); + + foreach ( $option as $id => $seconds ) { + if ( empty( $seconds ) ) { + continue; + } + + $items[] = array( + 'id' => $id, + 'seconds' => $seconds, + 'timestamp' => date( 'c', $seconds ), + ); + } + WP_CLI\Utils\format_items( + WP_CLI\Utils\get_flag_value( 'format', $assoc, 'table' ), + $items, + array( 'id', 'seconds', 'timestamp' ) + ); } } -if ( ! is_admin() && ( ! defined( 'WP_CLI' ) || ! WP_CLI ) ) { +if ( + ! is_admin() + && ( + ! defined( 'WP_CLI' ) + || ! WP_CLI + ) +) { return; }