@@ -37,13 +37,38 @@ public function __construct() {
37
37
38
38
// Register screen options.
39
39
if ( $ this ->on_settings_screen ( $ this ->name ) ) {
40
+ add_filter ( 'convertkit_settings_base_register_notices ' , array ( $ this , 'register_notices ' ) );
41
+ add_action ( 'convertkit_settings_base_render_before ' , array ( $ this , 'maybe_output_notices ' ) );
40
42
add_action ( 'load-settings_page__wp_convertkit_settings ' , array ( $ this , 'add_screen_options ' ) );
43
+ add_action ( 'load-settings_page__wp_convertkit_settings ' , array ( $ this , 'run_bulk_actions ' ) );
44
+ add_filter ( 'convertkit_admin_settings_form_method ' , array ( $ this , 'form_method ' ), 10 , 2 );
45
+ add_filter ( 'convertkit_admin_settings_form_action_url ' , array ( $ this , 'form_action_url ' ), 10 , 2 );
41
46
}
42
47
43
48
parent ::__construct ();
44
49
45
50
}
46
51
52
+ /**
53
+ * Registers success and error notices for the Form Entries screen, to be displayed
54
+ * depending on the action.
55
+ *
56
+ * @since 3.0.0
57
+ *
58
+ * @param array $notices Regsitered success and error notices.
59
+ * @return array
60
+ */
61
+ public function register_notices ( $ notices ) {
62
+
63
+ return array_merge (
64
+ $ notices ,
65
+ array (
66
+ 'form_entries_deleted_success ' => __ ( 'Form Entries deleted successfully. ' , 'convertkit ' ),
67
+ )
68
+ );
69
+
70
+ }
71
+
47
72
/**
48
73
* Register fields for this section
49
74
*
@@ -92,6 +117,13 @@ public function documentation_url() {
92
117
*/
93
118
public function render () {
94
119
120
+ /**
121
+ * Performs actions prior to rendering the settings form.
122
+ *
123
+ * @since 3.0.0
124
+ */
125
+ do_action ( 'convertkit_settings_base_render_before ' );
126
+
95
127
$ form_entries = new ConvertKit_Form_Entries ();
96
128
97
129
// Render opening container.
@@ -103,9 +135,14 @@ public function render() {
103
135
$ this ->print_section_info ();
104
136
105
137
// Setup WP_List_Table.
106
- $ table = new ConvertKit_WP_List_Table ();
138
+ $ table = new ConvertKit_WP_List_Table ( '_wp_convertkit_settings ' , $ this ->name );
139
+
140
+ // Add bulk actions to table.
141
+ $ table ->add_bulk_action ( 'export ' , __ ( 'Export ' , 'convertkit ' ) );
142
+ $ table ->add_bulk_action ( 'delete ' , __ ( 'Delete ' , 'convertkit ' ) );
107
143
108
144
// Add columns to table.
145
+ $ table ->add_column ( 'cb ' , __ ( 'Select ' , 'convertkit ' ), false );
109
146
$ table ->add_column ( 'post_id ' , __ ( 'Post ID ' , 'convertkit ' ), false );
110
147
$ table ->add_column ( 'first_name ' , __ ( 'First Name ' , 'convertkit ' ), false );
111
148
$ table ->add_column ( 'email ' , __ ( 'Email ' , 'convertkit ' ), false );
@@ -128,16 +165,31 @@ public function render() {
128
165
$ table ->add_items ( $ entries );
129
166
130
167
// Set total entries and items per page options key.
131
- $ table ->set_total_items ( $ form_entries ->total () );
168
+ $ table ->set_total_items ( $ form_entries ->total ( $ table -> get_search () ) );
132
169
$ table ->set_items_per_page_screen_options_key ( 'convertkit_form_entries_per_page ' );
133
170
171
+ // Display search term.
172
+ if ( $ table ->is_search () ) {
173
+ ?>
174
+ <span class="subtitle left"><?php esc_html_e ( 'Search results for ' , 'convertkit ' ); ?> "<?php echo esc_html ( $ table ->get_search () ); ?> "</span>
175
+ <?php
176
+ }
177
+
134
178
// Prepare and display WP_List_Table.
135
179
$ table ->prepare_items ();
180
+ $ table ->search_box ( __ ( 'Search ' , 'convertkit ' ), 'convertkit-search ' );
136
181
$ table ->display ();
137
182
138
183
// Render closing container.
139
184
$ this ->render_container_end ();
140
185
186
+ /**
187
+ * Performs actions after rendering of the settings form.
188
+ *
189
+ * @since 3.0.0
190
+ */
191
+ do_action ( 'convertkit_settings_base_render_after ' );
192
+
141
193
}
142
194
143
195
/**
@@ -180,6 +232,101 @@ public function set_screen_options( $screen_option, $option, $value ) { // phpcs
180
232
181
233
}
182
234
235
+ /**
236
+ * Runs the bulk actions for the Form Entries table.
237
+ *
238
+ * @since 3.0.0
239
+ */
240
+ public function run_bulk_actions () {
241
+
242
+ // Bail if nonce is not valid.
243
+ if ( ! isset ( $ _REQUEST ['_wpnonce ' ] ) || ! wp_verify_nonce ( sanitize_key ( wp_unslash ( $ _REQUEST ['_wpnonce ' ] ) ), 'bulk-convertkit-items ' ) ) {
244
+ return ;
245
+ }
246
+
247
+ // Bail if no bulk action is set.
248
+ $ bulk_action = isset ( $ _REQUEST ['action ' ] ) ? sanitize_text_field ( wp_unslash ( $ _REQUEST ['action ' ] ) ) : '' ;
249
+ if ( empty ( $ bulk_action ) ) {
250
+ return ;
251
+ }
252
+
253
+ // Bail if no entries are selected.
254
+ if ( ! isset ( $ _REQUEST ['convertkit-items ' ] ) ) {
255
+ return ;
256
+ }
257
+
258
+ // Initialize Form Entries class.
259
+ $ form_entries = new ConvertKit_Form_Entries ();
260
+
261
+ switch ( $ bulk_action ) {
262
+ case 'export ' :
263
+ // Get entries.
264
+ $ ids = array_unique ( array_map ( 'absint ' , $ _REQUEST ['convertkit-items ' ] ) );
265
+ $ entries = $ form_entries ->get_by_ids ( $ ids );
266
+
267
+ // Convert entries to CSV string.
268
+ $ csv = $ form_entries ->get_csv_string ( $ entries );
269
+
270
+ // Force download with output.
271
+ header ( 'Content-type: application/x-msdownload ' );
272
+ header ( 'Content-Disposition: attachment; filename=kit-form-entries-export.csv ' );
273
+ header ( 'Pragma: no-cache ' );
274
+ header ( 'Expires: 0 ' );
275
+ echo $ csv ; // phpcs:ignore WordPress.Security.EscapeOutput
276
+ exit ();
277
+
278
+ case 'delete ' :
279
+ // Delete entries by IDs.
280
+ $ ids = array_unique ( array_map ( 'absint ' , $ _REQUEST ['convertkit-items ' ] ) );
281
+ $ form_entries ->delete_by_ids ( $ ids );
282
+
283
+ // Redirect with success notice.
284
+ $ this ->redirect_with_success_notice ( 'form_entries_deleted_success ' );
285
+ break ;
286
+ }
287
+
288
+ }
289
+
290
+ /**
291
+ * Defines the settings form's method to 'get', to mirror how
292
+ * WP_List_Table works when performing a search.
293
+ *
294
+ * @since 3.0.0
295
+ *
296
+ * @param string $form_method Form method (post|get).
297
+ * @param string $active_section Active settings section.
298
+ * @return string
299
+ */
300
+ public function form_method ( $ form_method , $ active_section ) {
301
+
302
+ if ( $ active_section !== $ this ->name ) {
303
+ return $ form_method ;
304
+ }
305
+
306
+ return 'get ' ;
307
+
308
+ }
309
+
310
+ /**
311
+ * Defines the settings form's action URL to match the current screen,
312
+ * so the search functionality doesn't load options.php, which doesn't work.
313
+ *
314
+ * @since 3.0.0
315
+ *
316
+ * @param string $form_action_url URL.
317
+ * @param string $active_section Active settings section.
318
+ * @return string
319
+ */
320
+ public function form_action_url ( $ form_action_url , $ active_section ) {
321
+
322
+ if ( $ active_section !== $ this ->name ) {
323
+ return $ form_action_url ;
324
+ }
325
+
326
+ return 'options-general.php ' ;
327
+
328
+ }
329
+
183
330
}
184
331
185
332
// Register Admin Settings section.
0 commit comments