Skip to content

Commit ad2dc73

Browse files
author
Antti Kuosmanen
committed
Filter submissions by Form in edit.php
- Show number of submissions in Form list with a link to the submissions
1 parent 3c06d6d commit ad2dc73

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

classes/class-cpt-wplf-form.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ function custom_columns_cpt( $columns ) {
173173
'cb' => $columns['cb'],
174174
'title' => $columns['title'],
175175
'shortcode' => __( 'Shortcode', 'wp-libre-form' ),
176+
'submissions' => __( 'Submissions', 'wp-libre-form' ),
176177
'date' => $columns['date'],
177178
);
178179
return $new_columns;
@@ -186,6 +187,18 @@ function custom_columns_display_cpt( $column, $post_id ) {
186187
if( 'shortcode' === $column ) {
187188
echo '<code>[libre-form id="' . $post_id . '"]</code>';
188189
}
190+
if( 'submissions' === $column ) {
191+
// count number of submissions
192+
$submissions = get_posts( array(
193+
'post_type' => 'wplf-submission',
194+
'posts_per_page' => -1,
195+
'meta_key' => '_form_id',
196+
'meta_value' => $post_id,
197+
) );
198+
?>
199+
<a href="<?php echo admin_url( 'edit.php?post_type=wplf-submission&form=' . $post_id ); ?>"><?php echo count( $submissions ); ?></a>
200+
<?php
201+
}
189202
}
190203

191204

classes/class-cpt-wplf-submission.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

100152
endif;

0 commit comments

Comments
 (0)