@@ -22,9 +22,14 @@ private function __construct() {
2222 // init custom post type
2323 add_action ( 'init ' , array ( $ this , 'register_cpt ' ) );
2424
25+ // post.php view
26+ add_action ( 'add_meta_boxes ' , array ( $ this , 'add_meta_boxes_cpt ' ) );
27+
2528 // edit.php view
2629 add_filter ( 'manage_edit-wplf-submission_columns ' , array ( $ this , 'custom_columns_cpt ' ), 100 , 1 );
2730 add_action ( 'manage_posts_custom_column ' , array ( $ this , 'custom_columns_display_cpt ' ), 10 , 2 );
31+ add_action ( 'restrict_manage_posts ' , array ( $ this , 'form_filter_dropdown ' ) );
32+ add_filter ( 'pre_get_posts ' , array ( $ this , 'filter_by_form ' ) );
2833 }
2934
3035 public static function register_cpt () {
@@ -95,6 +100,53 @@ function custom_columns_cpt( $columns ) {
95100 return $ new_columns ;
96101 }
97102
103+ /**
104+ * Show a form filter in the edit.php view
105+ */
106+ function form_filter_dropdown () {
107+ global $ pagenow ;
108+
109+ if ( 'edit.php ' != $ pagenow ) {
110+ return ;
111+ }
112+
113+ // TODO: put this in a transient
114+ $ forms = get_posts ( array (
115+ 'post_per_page ' => '-1 ' ,
116+ 'post_type ' => 'wplf-form ' ,
117+ ) );
118+ ?>
119+ <label for="filter-by-form" class="screen-reader-text">Filter by form</label>
120+ <select name="form" id="filter-by-form">
121+ <option value="0"><?php _e ('All Forms ' ); ?> </option>
122+ <?php foreach ( $ forms as $ form ) : ?>
123+ <option value="<?php echo $ form ->ID ; ?> " <?php echo isset ( $ _REQUEST ['form ' ] ) && $ _REQUEST ['form ' ] == $ form ->ID ? 'selected ' : '' ; ?> ><?php echo $ form ->post_title ; ?> </option>
124+ <?php endforeach ; ?>
125+ </select>
126+ <?php
127+ }
128+
129+ /**
130+ * Filter by form in the edit.php view
131+ */
132+ function filter_by_form ( $ query ) {
133+ global $ pagenow ;
134+
135+ if ( 'edit.php ' != $ pagenow ) {
136+ return $ query ;
137+ }
138+
139+ if ( $ query ->get ( 'post_type ' ) != 'wplf-submission ' ) {
140+ return $ query ;
141+ }
142+
143+ if ( isset ( $ _REQUEST ['form ' ] ) && ! empty ( $ _REQUEST ['form ' ] ) ) {
144+ $ query ->set ( 'meta_key ' , '_form_id ' );
145+ $ query ->set ( 'meta_value ' , intval ( $ _REQUEST ['form ' ] ) );
146+ }
147+
148+ return $ query ;
149+ }
98150}
99151
100152endif ;
0 commit comments