PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.6
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.6
3.4.6 3.4.5 3.4.4 3.4.3 3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 All 199 releases
convertkit / admin / section / class-convertkit-admin-section-form-entries.php

class-convertkit-admin-section-form-entries.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.4.6, at admin/section/class-convertkit-admin-section-form-entries.php

388 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Form Entries Admin Settings class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Registers Form Entries Settings that can be viewed, deleted and exported at Settings > Kit > Form Entries.
11 *
12 * @package ConvertKit
13 * @author ConvertKit
14 */
15 class ConvertKit_Admin_Section_Form_Entries extends ConvertKit_Admin_Section_Base {
16
17 /**
18 * Constructor
19 *
20 * @since 3.0.0
21 */
22 public function __construct() {
23
24 // Define the programmatic name, Title and Tab Text.
25 $this->name = 'form-entries';
26 $this->title = __( 'Form Entries', 'convertkit' );
27 $this->tab_text = __( 'Form Entries', 'convertkit' );
28
29 // Define settings sections.
30 $this->settings_sections = array(
31 'general' => array(
32 'title' => $this->title,
33 'callback' => array( $this, 'print_section_info' ),
34 'wrap' => false,
35 ),
36 );
37
38 // Register screen options.
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' ) );
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 );
46 }
47
48 parent::__construct();
49
50 }
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
72 /**
73 * Register fields for this section
74 *
75 * @since 3.0.0
76 */
77 public function register_fields() {
78
79 // No fields are registered.
80 // This function is deliberately blank.
81 }
82
83 /**
84 * Prints help info for this section.
85 *
86 * @since 3.0.0
87 */
88 public function print_section_info() {
89
90 ?>
91 <p>
92 <?php
93 esc_html_e( 'Displays a list of form entries from Form Builder blocks that have "store form submissions" enabled. Entries submitted using embedded Kit Forms or Landing Pages are not included.', 'convertkit' );
94 ?>
95 </p>
96 <?php
97
98 }
99
100 /**
101 * Returns the URL for the ConvertKit documentation for this setting section.
102 *
103 * @since 3.0.0
104 *
105 * @return string Documentation URL.
106 */
107 public function documentation_url() {
108
109 return 'https://help.kit.com/en/articles/2502591-how-to-set-up-the-kit-plugin-on-your-wordpress-website';
110
111 }
112
113 /**
114 * Outputs the section as a WP_List_Table of Form Entries.
115 *
116 * @since 3.0.0
117 */
118 public function render() {
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
127 $form_entries = new ConvertKit_Form_Entries();
128
129 // Render opening container.
130 $this->render_container_start();
131
132 ?>
133 <h2><?php esc_html_e( 'Form Entries', 'convertkit' ); ?></h2>
134 <?php
135 $this->print_section_info();
136
137 // Setup 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' ) );
143
144 // Add filters to table.
145 $table->add_filter(
146 'api_result',
147 __( 'All Results', 'convertkit' ),
148 array(
149 'success' => __( 'Success', 'convertkit' ),
150 'error' => __( 'Error', 'convertkit' ),
151 )
152 );
153
154 // Add columns to table.
155 $table->add_column( 'cb', __( 'Select', 'convertkit' ), false );
156 $table->add_column( 'post_id', __( 'Post ID', 'convertkit' ), false );
157 $table->add_column( 'first_name', __( 'First Name', 'convertkit' ), false );
158 $table->add_column( 'email', __( 'Email', 'convertkit' ), false );
159 $table->add_column( 'created_at', __( 'Created', 'convertkit' ), false );
160 $table->add_column( 'updated_at', __( 'Updated', 'convertkit' ), false );
161 $table->add_column( 'api_result', __( 'Result', 'convertkit' ), false );
162 $table->add_column( 'api_error', __( 'Error', 'convertkit' ), false );
163
164 // Get user options.
165 $per_page = (int) ( ! empty( get_user_option( 'convertkit_form_entries_per_page' ) ) ? get_user_option( 'convertkit_form_entries_per_page' ) : 25 );
166
167 // Add form entries to table.
168 $entries = $form_entries->search(
169 $table->get_search(),
170 $table->get_filter( 'api_result' ),
171 $table->get_order_by( 'created_at' ),
172 $table->get_order( 'desc' ),
173 $table->get_pagenum(),
174 $per_page
175 );
176 $table->add_items( $entries );
177
178 // Set total entries and items per page options key.
179 $table->set_total_items( $form_entries->total( $table->get_search(), $table->get_filter( 'api_result' ) ) );
180 $table->set_items_per_page_screen_options_key( 'convertkit_form_entries_per_page' );
181
182 // Display search term.
183 if ( $table->is_search() ) {
184 ?>
185 <span class="subtitle left"><?php esc_html_e( 'Search results for', 'convertkit' ); ?> &quot;<?php echo esc_html( $table->get_search() ); ?>&quot;</span>
186 <?php
187 }
188
189 // Prepare and display WP_List_Table.
190 $table->prepare_items();
191 $table->search_box( __( 'Search', 'convertkit' ), 'convertkit-search' );
192 $table->display();
193
194 // Render closing container.
195 $this->render_container_end();
196
197 /**
198 * Performs actions after rendering of the settings form.
199 *
200 * @since 3.0.0
201 */
202 do_action( 'convertkit_settings_base_render_after' );
203
204 }
205
206 /**
207 * Defines options to display in the Screen Options dropdown on the Logs
208 * WP_List_Table
209 *
210 * @since 3.0.0
211 */
212 public function add_screen_options() {
213
214 add_screen_option(
215 'per_page',
216 array(
217 'label' => __( 'Form Entries per Page', 'convertkit' ),
218 'default' => 25,
219 'option' => 'convertkit_form_entries_per_page',
220 )
221 );
222
223 }
224
225 /**
226 * Sets values for options displayed in the Screen Options dropdown on the
227 * WP_List_Table
228 *
229 * @since 3.0.0
230 *
231 * @param mixed $screen_option The value to save instead of the option value. Default false (to skip saving the current option).
232 * @param string $option The option name.
233 * @param string $value The option value.
234 * @return int|string The option value
235 */
236 public function set_screen_options( $screen_option, $option, $value ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
237
238 if ( 'convertkit_form_entries_per_page' === $option ) {
239 return (int) $value;
240 }
241
242 return $screen_option;
243
244 }
245
246 /**
247 * Runs the bulk actions for the Form Entries table.
248 *
249 * @since 3.0.0
250 */
251 public function run_bulk_actions() {
252
253 // Bail if nonce is not valid.
254 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'bulk-convertkit-items' ) ) {
255 return;
256 }
257
258 // Bail if the user isn't permitted to manage form entries.
259 if ( ! current_user_can( 'manage_options' ) ) {
260 return;
261 }
262
263 // Bail if no bulk action is set.
264 $bulk_action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : '';
265 if ( empty( $bulk_action ) ) {
266 return;
267 }
268
269 // Bail if no entries are selected.
270 if ( ! isset( $_REQUEST['convertkit-items'] ) ) {
271 return;
272 }
273
274 // Initialize Form Entries class.
275 $form_entries = new ConvertKit_Form_Entries();
276
277 switch ( $bulk_action ) {
278 case 'export':
279 // Get entries.
280 $ids = array_unique( array_map( 'absint', $_REQUEST['convertkit-items'] ) );
281 $entries = $form_entries->get_by_ids( $ids );
282
283 // Convert entries to CSV string.
284 $csv = $form_entries->get_csv_string( $entries );
285
286 // Force download with output.
287 header( 'Content-Type: text/csv; charset=utf-8' );
288 header( 'Content-Disposition: attachment; filename=kit-form-entries-export.csv' );
289 header( 'Pragma: no-cache' );
290 header( 'Expires: 0' );
291 echo $csv; // phpcs:ignore WordPress.Security.EscapeOutput
292 exit();
293
294 case 'delete':
295 // Delete entries by IDs.
296 $ids = array_unique( array_map( 'absint', $_REQUEST['convertkit-items'] ) );
297 $form_entries->delete_by_ids( $ids );
298
299 // Redirect with success notice.
300 $this->redirect_with_success_notice( 'form_entries_deleted_success' );
301 break;
302 }
303
304 }
305
306 /**
307 * Defines the settings form's method to 'get', to mirror how
308 * WP_List_Table works when performing a search.
309 *
310 * @since 3.0.0
311 *
312 * @param string $form_method Form method (post|get).
313 * @param string $active_section Active settings section.
314 * @return string
315 */
316 public function form_method( $form_method, $active_section ) {
317
318 if ( $active_section !== $this->name ) {
319 return $form_method;
320 }
321
322 return 'get';
323
324 }
325
326 /**
327 * Defines the settings form's action URL to match the current screen,
328 * so the search functionality doesn't load options.php, which doesn't work.
329 *
330 * @since 3.0.0
331 *
332 * @param string $form_action_url URL.
333 * @param string $active_section Active settings section.
334 * @return string
335 */
336 public function form_action_url( $form_action_url, $active_section ) {
337
338 if ( $active_section !== $this->name ) {
339 return $form_action_url;
340 }
341
342 return 'options-general.php';
343
344 }
345
346 }
347
348 // Register Admin Settings section.
349 add_filter(
350 'convertkit_admin_settings_register_sections',
351 /**
352 * Register Form Entries as a section at Settings > Kit.
353 *
354 * @param array $sections Settings Sections.
355 * @return array
356 */
357 function ( $sections ) {
358
359 // Register this class as a section at Settings > Kit.
360 $sections['form-entries'] = new ConvertKit_Admin_Section_Form_Entries();
361 return $sections;
362
363 }
364 );
365
366 // Add support for WordPress' Screen Options.
367 add_action(
368 'convertkit_initialize_admin',
369 function () {
370
371 add_filter(
372 'set-screen-option',
373 function ( $status, $option, $value ) {
374
375 if ( 'convertkit_form_entries_per_page' === $option ) {
376 return (int) $value;
377 }
378
379 return $status;
380
381 },
382 10,
383 3
384 );
385
386 }
387 );
388