| 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' ); ?> "<?php echo esc_html( $table->get_search() ); ?>"</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 no bulk action is set. |
| 259 |
$bulk_action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : ''; |
| 260 |
if ( empty( $bulk_action ) ) { |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
// Bail if no entries are selected. |
| 265 |
if ( ! isset( $_REQUEST['convertkit-items'] ) ) { |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
// Initialize Form Entries class. |
| 270 |
$form_entries = new ConvertKit_Form_Entries(); |
| 271 |
|
| 272 |
switch ( $bulk_action ) { |
| 273 |
case 'export': |
| 274 |
// Get entries. |
| 275 |
$ids = array_unique( array_map( 'absint', $_REQUEST['convertkit-items'] ) ); |
| 276 |
$entries = $form_entries->get_by_ids( $ids ); |
| 277 |
|
| 278 |
// Convert entries to CSV string. |
| 279 |
$csv = $form_entries->get_csv_string( $entries ); |
| 280 |
|
| 281 |
// Force download with output. |
| 282 |
header( 'Content-type: application/x-msdownload' ); |
| 283 |
header( 'Content-Disposition: attachment; filename=kit-form-entries-export.csv' ); |
| 284 |
header( 'Pragma: no-cache' ); |
| 285 |
header( 'Expires: 0' ); |
| 286 |
echo $csv; // phpcs:ignore WordPress.Security.EscapeOutput |
| 287 |
exit(); |
| 288 |
|
| 289 |
case 'delete': |
| 290 |
// Delete entries by IDs. |
| 291 |
$ids = array_unique( array_map( 'absint', $_REQUEST['convertkit-items'] ) ); |
| 292 |
$form_entries->delete_by_ids( $ids ); |
| 293 |
|
| 294 |
// Redirect with success notice. |
| 295 |
$this->redirect_with_success_notice( 'form_entries_deleted_success' ); |
| 296 |
break; |
| 297 |
} |
| 298 |
|
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Defines the settings form's method to 'get', to mirror how |
| 303 |
* WP_List_Table works when performing a search. |
| 304 |
* |
| 305 |
* @since 3.0.0 |
| 306 |
* |
| 307 |
* @param string $form_method Form method (post|get). |
| 308 |
* @param string $active_section Active settings section. |
| 309 |
* @return string |
| 310 |
*/ |
| 311 |
public function form_method( $form_method, $active_section ) { |
| 312 |
|
| 313 |
if ( $active_section !== $this->name ) { |
| 314 |
return $form_method; |
| 315 |
} |
| 316 |
|
| 317 |
return 'get'; |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Defines the settings form's action URL to match the current screen, |
| 323 |
* so the search functionality doesn't load options.php, which doesn't work. |
| 324 |
* |
| 325 |
* @since 3.0.0 |
| 326 |
* |
| 327 |
* @param string $form_action_url URL. |
| 328 |
* @param string $active_section Active settings section. |
| 329 |
* @return string |
| 330 |
*/ |
| 331 |
public function form_action_url( $form_action_url, $active_section ) { |
| 332 |
|
| 333 |
if ( $active_section !== $this->name ) { |
| 334 |
return $form_action_url; |
| 335 |
} |
| 336 |
|
| 337 |
return 'options-general.php'; |
| 338 |
|
| 339 |
} |
| 340 |
|
| 341 |
} |
| 342 |
|
| 343 |
// Register Admin Settings section. |
| 344 |
add_filter( |
| 345 |
'convertkit_admin_settings_register_sections', |
| 346 |
/** |
| 347 |
* Register Form Entries as a section at Settings > Kit. |
| 348 |
* |
| 349 |
* @param array $sections Settings Sections. |
| 350 |
* @return array |
| 351 |
*/ |
| 352 |
function ( $sections ) { |
| 353 |
|
| 354 |
// Register this class as a section at Settings > Kit. |
| 355 |
$sections['form-entries'] = new ConvertKit_Admin_Section_Form_Entries(); |
| 356 |
return $sections; |
| 357 |
|
| 358 |
} |
| 359 |
); |
| 360 |
|
| 361 |
// Add support for WordPress' Screen Options. |
| 362 |
add_action( |
| 363 |
'convertkit_initialize_admin', |
| 364 |
function () { |
| 365 |
|
| 366 |
add_filter( |
| 367 |
'set-screen-option', |
| 368 |
function ( $status, $option, $value ) { |
| 369 |
|
| 370 |
if ( 'convertkit_form_entries_per_page' === $option ) { |
| 371 |
return (int) $value; |
| 372 |
} |
| 373 |
|
| 374 |
return $status; |
| 375 |
|
| 376 |
}, |
| 377 |
10, |
| 378 |
3 |
| 379 |
); |
| 380 |
|
| 381 |
} |
| 382 |
); |
| 383 |
|