Revision Limits screen. * * @package Leafcap_Revision_Limits */ namespace Leafcap_Revision_Limits; use WP_Post_Type; defined( 'ABSPATH' ) || exit; /** * Renders the settings screen and the Plugins screen link. */ class Admin { /** * Screen slug. */ const PAGE = 'leafcap-revision-limits'; /** * Capability needed for the screen. */ const CAPABILITY = 'manage_options'; /** * Settings section ID. */ const SECTION = 'leafcap_revision_limits_limits'; /** * Counts for the current request, so the query runs once per page load. * False until loaded. Null means the query failed. * * @var array|null|false */ private $counts = false; /** * Registers hooks. * * @return void */ public function register_hooks() { add_action( 'admin_menu', array( $this, 'add_menu_page' ) ); add_filter( 'plugin_action_links_' . plugin_basename( LEAFCAP_REVISION_LIMITS_FILE ), array( $this, 'add_settings_link' ) ); } /** * Adds the screen under Settings. * * @return void */ public function add_menu_page() { $hook = add_options_page( __( 'Leafcap Revision Limits', 'leafcap-revision-limits' ), __( 'Revision Limits', 'leafcap-revision-limits' ), self::CAPABILITY, self::PAGE, array( $this, 'render_page' ) ); if ( $hook ) { add_action( 'load-' . $hook, array( $this, 'register_fields' ) ); } } /** * Adds a Settings link to the plugin's row on the Plugins screen. * * @param string[] $links Existing links. * @return string[] */ public function add_settings_link( $links ) { if ( ! current_user_can( self::CAPABILITY ) ) { return $links; } $link = sprintf( '%2$s', esc_url( admin_url( 'options-general.php?page=' . self::PAGE ) ), esc_html__( 'Settings', 'leafcap-revision-limits' ) ); array_unshift( $links, $link ); return $links; } /** * Registers one settings row per post type. * * @return void */ public function register_fields() { add_settings_section( self::SECTION, '', '__return_null', self::PAGE ); foreach ( Settings::get_post_types() as $name => $post_type ) { add_settings_field( 'leafcap-revision-limits-' . $name, esc_html( $post_type->label ) . '
' . esc_html( $name ) . '', array( $this, 'render_row' ), self::PAGE, self::SECTION, array( 'post_type' => $post_type ) ); } } /** * Renders the screen. Core prints saved and error notices on Settings * pages, so settings_errors() is not called here. * * @return void */ public function render_page() { if ( ! current_user_can( self::CAPABILITY ) ) { return; } $has_rows = array() !== Settings::get_post_types(); ?>

render_site_default(); ?> render_trimming_notice(); ?>

get_site_default_text() ); ?>

WP_POST_REVISIONS', 'wp-config.php' ), array( 'code' => array() ) ); ?>

name; $limits = Settings::get_limits(); $saved = isset( $limits[ $name ] ) ? $limits[ $name ] : null; $mode = $this->get_mode( $saved ); $number = Settings::MODE_NUMBER === $mode ? (string) $saved : ''; $limit_id = 'leafcap-revision-limits-' . $name . '-limit'; $number_id = 'leafcap-revision-limits-' . $name . '-number'; $field = Settings::OPTION . '[' . $name . ']'; ?>
label ); ?>

get_count_text( $name ) ); ?>

counts ) { $this->counts = $this->query_revision_counts(); } if ( null === $this->counts ) { return __( 'Stored count not available.', 'leafcap-revision-limits' ); } $count = isset( $this->counts[ $name ] ) ? $this->counts[ $name ] : 0; return sprintf( /* translators: %s: Number of stored revisions. */ _n( '%s stored revision', '%s stored revisions', $count, 'leafcap-revision-limits' ), number_format_i18n( $count ) ); } /** * Counts all revision rows, autosaves included, by parent post type. * * Called only while rendering this screen. It is one grouped query, which * reads revisions through the post_type index and each parent by primary * key. The result is not cached, so the counts are current on every visit. * * @return array|null Counts by post type, or null on failure. */ private function query_revision_counts() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- No core API groups revisions by parent type, and counts must be current. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT p.post_type AS post_type, COUNT(*) AS total FROM {$wpdb->posts} AS r INNER JOIN {$wpdb->posts} AS p ON r.post_parent = p.ID WHERE r.post_type = %s GROUP BY p.post_type", 'revision' ) ); // A failed query returns an empty array, so check last_error as well. if ( ! is_array( $rows ) || '' !== $wpdb->last_error ) { return null; } $counts = array(); foreach ( $rows as $row ) { $counts[ $row->post_type ] = (int) $row->total; } return $counts; } }