diff --git a/admin/class-recursivetable.php b/admin/class-recursivetable.php index 6ff30e4..6963e7c 100644 --- a/admin/class-recursivetable.php +++ b/admin/class-recursivetable.php @@ -40,7 +40,7 @@ - + @@ -80,9 +80,22 @@
- GitHub + $github_icon_url, + 'width' => '22', + 'alt' => 'GitHub', + 'loading' => 'lazy', + ) + ); + ?>
-
Document on GitHub
+
Document on GitHub
@@ -158,78 +171,50 @@ $arr Array to be converted. - * @return string HTML table representation of the array. - */ - private static function array_to_html_table_recursive( array $arr ): string { - $str = ''; - foreach ( $arr as $key => $val ) { - $str .= ''; - $str .= ''; - $str .= ''; - } - $str .= '
' . htmlspecialchars( $key ) . ''; - if ( is_array( $val ) ) { - if ( ! empty( $val ) ) { - $str .= self::array_to_html_table_recursive( $val ); - } - } else { - if ( ! is_string( $val ) ) { - return $str; - } - $value = $val; - $str .= '' . nl2br( htmlspecialchars( $value ) ) . ''; - } - $str .= '
'; + // Get sort parameters. + $orderby = filter_input( INPUT_GET, 'orderby', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); + $order = filter_input( INPUT_GET, 'order', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); - return $str; - } - } + // Default sort settings. + $default_orderby = 'date'; + $default_order = 'DESC'; - global $wpdb; - $table = GOOGLE_SS2DB_TABLE_NAME; + // Allowed sort columns. + $allowed_orderby = array( + 'id' => 'id', + 'worksheet_id' => 'worksheet_id', + 'worksheet_name' => 'worksheet_name', + 'sheet_name' => 'sheet_name', + 'title' => 'title', + 'date' => 'date', + ); + + // Sort column validation. + $orderby = isset( $allowed_orderby[ $orderby ] ) ? $orderby : $default_orderby; + $order = in_array( strtoupper( $order ), array( 'ASC', 'DESC' ), true ) ? strtoupper( $order ) : $default_order; $paged = filter_input( INPUT_GET, 'paged', FILTER_VALIDATE_INT ); $nonce = filter_input( INPUT_GET, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); + // Verify pagination nonce. if ( $paged && ! wp_verify_nonce( $nonce, 'google_ss2db_pagination' ) ) { $paged = 1; } - $paged = $paged ? $paged : 1; - $limit = 24; - $offset = ( $paged - 1 ) * $limit; - $countsql = 'SELECT * FROM ' . GOOGLE_SS2DB_TABLE_NAME . ' ORDER BY date DESC'; + $paged = $paged ? $paged : 1; + $limit = 24; + $offset = ( $paged - 1 ) * $limit; + + // SQL with sorting. + $countsql = "SELECT * FROM {$table} ORDER BY {$orderby} {$order}"; $allrows = count( $wpdb->get_results( $countsql ) ); // phpcs:ignore $max_num_pages = ceil( $allrows / $limit ); - $sql = 'SELECT * FROM ' . $table . ' ORDER BY date DESC LIMIT %d OFFSET %d'; - $prepared = $wpdb->prepare( + + $sql = "SELECT * FROM {$table} ORDER BY {$orderby} {$order} LIMIT %d OFFSET %d"; + $prepared = $wpdb->prepare( $sql, // phpcs:ignore $limit, $offset @@ -237,58 +222,114 @@ private static function array_to_html_table_recursive( array $arr ): string { $myrows = $wpdb->get_results( $prepared ); // phpcs:ignore $count = count( $myrows ); + + /** + * Generate sort URLs for table columns. + * + * @param string $column The column to generate sort URL for. + * @return string The generated sort URL. + */ + function get_sort_url( string $column ): string { + $current_page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); + $current_orderby = filter_input( INPUT_GET, 'orderby', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); + $current_orderby = $current_orderby ? $current_orderby : 'date'; + $current_order = filter_input( INPUT_GET, 'order', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); + $current_order = $current_order ? $current_order : 'DESC'; + + // Toggle sort order. + $new_order = 'DESC' === $current_order && $current_orderby === $column ? 'ASC' : 'DESC'; + + $url_params = array( + 'page' => $current_page, + 'orderby' => $column, + 'order' => $new_order, + ); + + return esc_url( add_query_arg( $url_params ) ); + } + if ( 0 < $count ) : ?>

- -
-
- - - id ); ?> - - - worksheet_id ) ) ? $row->worksheet_id : '(no ID)'; - echo esc_html( google_ss2db_truncate_middle( $wordsheet_id ) ); - ?> - - - worksheet_name ); ?> - - - sheet_name ); ?> - - - title ? $row->title : ' (no title)' ); ?> - - -
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+ + ID + + + + + Worksheet ID + + + + + Worksheet Name + + + + + Sheet Name + + + + + Title + + + + + Date + + + Actions
id ); ?>worksheet_id ?? '(no ID)' ) ); ?>worksheet_name ); ?>sheet_name ); ?> + title ? $row->title : '(no title)' ); ?> + date ); - echo esc_html( $date->format( 'Y.m.d H:i:s' ) ); + $date = new DateTime( $row->date ); + $date_format = is_string( get_option( 'date_format' ) ) ? get_option( 'date_format' ) : 'Y-m-d'; + $time_format = is_string( get_option( 'time_format' ) ) ? get_option( 'time_format' ) : 'H:i:s'; + echo esc_html( date_i18n( $date_format . ' ' . $time_format, $date->getTimestamp() ) ); ?> - - - - -
- value; - echo wp_kses_post( RecursiveTable::json_to_debug( $json ) ); - ?> -
- - +
+ + + +
+
+ +