prefix . 'astr_posts_history'; $per_page = 10; $paged = astr_safe_get( 'paged', 'int', 1, 'manage_options', false ); $offset = ( $paged - 1 ) * $per_page; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $search = astr_safe_get( 's', 'text', '', 'manage_options', false ); $status = astr_safe_get( 'post_status', 'text', '', 'manage_options', false ); $month = astr_safe_get( 'm', 'text', '', 'manage_options', false ); $cat = astr_safe_get( 'cat', 'int', 0, 'manage_options', false ); $orderby = astr_safe_get( 'orderby', 'key', 'date', 'manage_options', false ); $order_raw = astr_safe_get( 'order', 'key', 'desc', 'manage_options', false ); $order = ( strtolower( $order_raw ) === 'asc' ) ? 'ASC' : 'DESC'; $allowed_orderby = [ 'title' => 'post_title', 'status' => 'status', 'post_type' => 'post_type', 'date' => 'time', 'author' => 'user_name', ]; if ( ! isset( $allowed_orderby[ $orderby ] ) ) { $orderby = 'date'; } $orderby_sql = $allowed_orderby[ $orderby ]; // --- WHERE conditions --- $filters = [ 'post_type' => astr_safe_get( 'post_type_filter', 'key', '' ), 'month' => $month, 'cat' => $cat, 'status' => $status, 'search' => $search, ]; list( $where, $args ) = astr_build_where_clause( $wpdb, $table, $filters ); // --- Prepare final SQL --- $rows = astr_get_latest_rows($wpdb, $table, $where, $args, $orderby_sql, $order, $per_page, $offset); // --- Counts --- // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $total = astr_get_total_count( $wpdb, $table, $where, $args ); $count_all_sql = " SELECT COUNT(*) FROM (SELECT post_id, MAX(time) as max_time FROM $table GROUP BY post_id) latest INNER JOIN $table t ON t.post_id = latest.post_id AND t.time = latest.max_time WHERE EXISTS (SELECT 1 FROM {$wpdb->posts} p WHERE p.post_parent = t.post_id AND p.post_type = 'revision')"; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter $count_all = (int) $wpdb->get_var( $count_all_sql ); // count_published // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $count_published_sql = " SELECT COUNT(*) FROM (SELECT post_id, MAX(time) as max_time FROM $table GROUP BY post_id) latest INNER JOIN $table t ON t.post_id = latest.post_id AND t.time = latest.max_time WHERE EXISTS (SELECT 1 FROM {$wpdb->posts} p WHERE p.post_parent = t.post_id AND p.post_type = 'revision') AND t.status LIKE 'Published%'"; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter $count_published = (int) $wpdb->get_var( $count_published_sql ); $page = astr_safe_get( 'page', 'key', 'astr-posts-history', 'manage_options', false ); $total_pages = max( 1, ceil( $count_all / $per_page ) ); // --- Begin Output --- echo '
'; echo '

Posts History

'; echo '
'; $base_url = esc_url( add_query_arg( [ 's' => $search, 'm' => $month, 'cat' => $cat, 'post_status' => $status, 'post_type_filter' => astr_safe_get( 'post_type_filter', 'key', '' ), 'orderby' => $orderby, 'order' => $order_raw, ], admin_url( 'admin.php?page=astr-posts-history' ) ) ); // === FILTERS + SEARCH === echo '
'; // LEFT: All | Published echo '
'; echo ''; // RIGHT: Search echo '
'; echo '
'; wp_nonce_field( 'astr_filter_action', '_astr_nonce' ); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
'; echo '
'; /*echo '
';*/ echo '
'; echo '
'; // === Top table nav === echo '
'; echo '
'; echo '
'; wp_nonce_field( 'astr_filter_action', '_astr_nonce' ); echo '
'; echo ''; echo ''; // Month filter echo ' '; // select status echo ''; // --- Post type filter --- // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter $post_types_with_revisions = $wpdb->get_col(" SELECT DISTINCT t.post_type FROM $table t INNER JOIN {$wpdb->posts} wp_rev ON wp_rev.post_parent = t.post_id AND wp_rev.post_type = 'revision' WHERE t.post_type IS NOT NULL ORDER BY t.post_type ASC"); if ( ! empty( $post_types_with_revisions ) ) { echo ' '; } // Filter button submit_button( 'Filter', '', '', false ); echo '
'; echo '
'; // === Pagination Top === echo '
'; if ( $total >= $per_page ) { astr_pagination( $total, $total_pages, $paged ); } if ( false ) { astr_pagination( $total, $total_pages, $paged ); } /*echo '
';*/ echo '
'; echo '
'; echo '
'; // === Table ===; echo ''; echo ''; echo wp_kses_post( astr_sortable_th( 'Title', 'title', $orderby, $order ) ); echo wp_kses_post( astr_sortable_th( 'Status', 'status', $orderby, $order ) ); echo wp_kses_post( astr_sortable_th( 'Post Type', 'post_type', $orderby, $order ) ); echo wp_kses_post( astr_sortable_th( 'Date', 'date', $orderby, $order ) ); echo wp_kses_post( astr_sortable_th( 'Author', 'author', $orderby, $order ) ); echo ''; echo ''; if ( ! empty( $rows ) ) { foreach ( $rows as $row ) { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter $revision_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $table WHERE post_id = %d AND revision_id IS NOT NULL", $row->post_id ) ); if ( (int) $revision_count < 1 ) { continue; } $button = astr_get_revision_or_restore_button( $row->post_id, $wpdb ); if ( empty( $button ) ) { continue; } echo ''; echo ''; $status_label = astr_get_status_label( $row->status ); echo ''; $post_type_label = $row->post_type; $post_type_obj = get_post_type_object( $row->post_type ); if ( $post_type_obj && ! empty( $post_type_obj->labels->singular_name ) ) { $post_type_label = $post_type_obj->labels->singular_name; } echo ''; echo ''; echo ''; echo ''; echo ''; } } else { echo ''; } echo '
Revisions
' . esc_html( $row->post_title ) . '' . esc_html( $status_label ) . '' . esc_html( $post_type_label ) . '' . esc_html( mysql2date( 'd-m-Y H:i:s', $row->time ) ) . '' . esc_html( $row->user_name ) . '' . wp_kses_post( $button ) . '
No post history found.
'; // === Bottom pagination === echo '
'; if ( $total >= $per_page ) { astr_pagination( $total, $total_pages, $paged ); } echo '
'; echo ''; }