Ticket #21665: 21665-v1.diff

File 21665-v1.diff, 4.7 KB (added by bootsz, 9 months ago)

Adds filtering for System pages to the Pages table, and adds support for classifying & storing system pages in the database via wp_options.

Line 
1Index: wp-admin/includes/class-wp-posts-list-table.php
2===================================================================
3--- wp-admin/includes/class-wp-posts-list-table.php     (revision 21596)
4+++ wp-admin/includes/class-wp-posts-list-table.php     (working copy)
5@@ -45,6 +45,15 @@
6         */
7        var $sticky_posts_count = 0;
8 
9+       /**
10+        * Holds the number of pages designated as System Pages.
11+        *
12+        * @since 3.5.0
13+        * @var int
14+        * @access private
15+        */
16+       var $system_pages_count = 0;
17+
18        function __construct() {
19                global $post_type_object, $wpdb;
20 
21@@ -67,6 +76,11 @@
22                        $this->sticky_posts_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( 1 ) FROM $wpdb->posts WHERE post_type = %s AND post_status != 'trash' AND ID IN ($sticky_posts)", $post_type ) );
23                }
24 
25+               if ( 'page' == $post_type && $system_pages = get_option( 'system_pages' ) ) {
26+                       $system_pages = implode( ', ', array_map( 'absint', (array) $system_pages ) );
27+                       $this->system_pages_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( 1 ) FROM $wpdb->posts WHERE post_type = %s AND post_status != 'trash' AND ID IN ($system_pages)", $post_type ) );
28+               }
29+
30                parent::__construct( array(
31                        'plural' => 'posts',
32                ) );
33@@ -178,6 +192,15 @@
34                        $status_links = array_merge( array_slice( $status_links, 0, $split ), $sticky_link, array_slice( $status_links, $split ) );
35                }
36 
37+               if ( ! empty( $this->system_pages_count ) ) {
38+                       $class = ! empty( $_REQUEST['show_system'] ) ? ' class="current"' : '';
39+
40+                       $system_link = array( 'system' => "<a href='edit.php?post_type=$post_type&amp;show_system=1'$class>" . sprintf( _nx( 'System <span class="count">(%s)</span>', 'System <span class="count">(%s)</span>', $this->system_pages_count, 'posts' ), number_format_i18n( $this->system_pages_count ) ) . '</a>' );
41+
42+                       $split = 1 + array_search( ( isset( $status_links['publish'] ) ? 'publish' : 'all' ), array_keys( $status_links ) );
43+                       $status_links = array_merge( array_slice( $status_links, 0, $split ), $system_link, array_slice( $status_links, $split ) );
44+               }
45+
46                return $status_links;
47        }
48 
49Index: wp-admin/includes/post.php
50===================================================================
51--- wp-admin/includes/post.php  (revision 21596)
52+++ wp-admin/includes/post.php  (working copy)
53@@ -887,6 +887,9 @@
54        if ( ! empty( $q['show_sticky'] ) )
55                $query['post__in'] = (array) get_option( 'sticky_posts' );
56 
57+       if ( ! empty( $q['show_system'] ) )
58+               $query['post__in'] = (array) get_option( 'system_pages' );
59+
60        wp( $query );
61 
62        return $avail_post_stati;
63Index: wp-admin/options-reading.php
64===================================================================
65--- wp-admin/options-reading.php        (revision 21596)
66+++ wp-admin/options-reading.php        (working copy)
67@@ -90,6 +90,25 @@
68        if ( 'page' == get_option( 'show_on_front' ) && ! get_option( 'page_on_front' ) && ! get_option( 'page_for_posts' ) )
69                update_option( 'show_on_front', 'posts' );
70 ?>
71+
72+<?php
73+       if ( get_option('page_for_posts') != '0' ) :
74+
75+               make_system_page( get_option('page_for_posts') );
76+
77+               if ( get_option('page_for_posts') != get_option('page_for_posts_last') ) :
78+                       undo_system_page( get_option('page_for_posts_last') );
79+               endif;
80+               
81+               update_option('page_for_posts_last', get_option('page_for_posts') );
82+
83+       else:
84+               undo_system_page( get_option('page_for_posts_last') );
85+               update_option('page_for_posts_last', '0');
86+
87+       endif;
88+?>
89+
90 <table class="form-table">
91 <tr valign="top">
92 <th scope="row"><?php _e( 'Front page displays' ); ?></th>
93Index: wp-includes/post.php
94===================================================================
95--- wp-includes/post.php        (revision 21596)
96+++ wp-includes/post.php        (working copy)
97@@ -1946,6 +1946,52 @@
98 }
99 
100 /**
101+ * Classify a page as a System Page.
102+ *
103+ * @since 3.5.0
104+ *
105+ * @param int $post_id Post ID.
106+ */
107+function make_system_page($post_id) {
108+       $system_pages = get_option('system_pages');
109+       $post_id = (int) $post_id;
110+
111+       if ( !is_array($system_pages) )
112+               $system_pages = array($post_id);
113+
114+       if ( ! in_array($post_id, $system_pages) )
115+               $system_pages[] = $post_id;
116+
117+       update_option('system_pages', $system_pages);
118+}
119+
120+/**
121+ * De-Classify a page as a System Page.
122+ *
123+ * @since 3.5.0
124+ *
125+ * @param int $post_id Post ID.
126+ */
127+function undo_system_page($post_id) {
128+       $system_pages = get_option('system_pages');
129+       $post_id = (int) $post_id;
130+
131+       if ( !is_array($system_pages) )
132+               return;
133+
134+       if ( ! in_array($post_id, $system_pages) )
135+               return;
136+
137+       $offset = array_search($post_id, $system_pages);
138+       if ( false === $offset )
139+               return;
140+
141+       array_splice($system_pages, $offset, 1);
142+
143+       update_option('system_pages', $system_pages);
144+}
145+
146+/**
147  * Count number of posts of a post type and is user has permissions to view.
148  *
149  * This function provides an efficient method of finding the amount of post's