diff --git a/cssllc-admin-last-updated.php b/cssllc-admin-last-updated.php new file mode 100644 index 0000000..dfcac8c --- /dev/null +++ b/cssllc-admin-last-updated.php @@ -0,0 +1,307 @@ + + + + + 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_timestamp() + * + * @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_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 ) ); + } + + /** + * CLI command: admin-last-updated get + * + * @param array $args + * @param array $assoc + * + * @return void + * + * @todo test + */ + public function cli__get( array $args, array $assoc = array() ) : void { + $id = $args[0]; + $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 ) ); + 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 + * + * @todo test + */ + public function cli__list( array $args, array $assoc = array() ) : void { + $option = ( array ) get_option( self::OPTION_NAME, array() ); + + 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 + ) +) { + return; +} + +CSSLLC_Admin_Last_Updated::init(); \ No newline at end of file