← All changes
|
libraries/ct-ajax-list-table/includes/ajax-functions.php
+92
-0
5.8.4
→
6.0.4
View file →
| @@ -1,0 +1,92 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Ajax Functions | |
| 4 | + * | |
| 5 | + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> | |
| 6 | + * | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +defined( 'ABSPATH' ) || exit; | |
| 11 | + | |
| 12 | +function ct_ajax_list_table_handle_request() { | |
| 13 | + | |
| 14 | + global $ct_table, $ct_query, $ct_list_table, $ct_ajax_list_items_per_page; | |
| 15 | + | |
| 16 | + // Security check, forces to die if not security passed | |
| 17 | + check_ajax_referer( 'ct_ajax_list_table', 'nonce' ); | |
| 18 | + | |
| 19 | + if( ! isset( $_GET['object'] ) ) { | |
| 20 | + wp_send_json_error(); | |
| 21 | + } | |
| 22 | + | |
| 23 | + // Setup the CT Table | |
| 24 | + $ct_table = ct_setup_table( sanitize_text_field( wp_unslash( $_GET['object'] ) ) ); | |
| 25 | + | |
| 26 | + if( ! is_object( $ct_table ) ) { | |
| 27 | + wp_send_json_error(); | |
| 28 | + } | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * Filter capability to check | |
| 32 | + * | |
| 33 | + * @param string $capability By default, "manage_options" | |
| 34 | + * | |
| 35 | + * @return string | |
| 36 | + */ | |
| 37 | + $capability = apply_filters( 'ct_ajax_list_table_' . $ct_table->name . '_capability', 'manage_options' ); | |
| 38 | + | |
| 39 | + if( ! current_user_can( $capability ) ) { | |
| 40 | + wp_send_json_error(); | |
| 41 | + } | |
| 42 | + | |
| 43 | + // Setup this constant to allow from CT_List_Table meet that this render comes from this plugin | |
| 44 | + @define( 'IS_CT_AJAX_LIST_TABLE', true ); | |
| 45 | + | |
| 46 | + if( isset( $_GET['query_args'] ) && is_array( $_GET['query_args'] ) ) { | |
| 47 | + $query_args = map_deep( wp_unslash( $_GET['query_args'] ), 'sanitize_text_field' ); | |
| 48 | + } else { | |
| 49 | + $query_args = json_decode( wp_unslash( $_GET['query_args'] ), true ); | |
| 50 | + // Sanitize | |
| 51 | + $query_args = map_deep( $query_args, 'sanitize_text_field' ); | |
| 52 | + } | |
| 53 | + | |
| 54 | + if( isset( $_GET['paged'] ) ) { | |
| 55 | + $query_args['paged'] = absint( $_GET['paged'] ); | |
| 56 | + } | |
| 57 | + | |
| 58 | + $query_args = wp_parse_args( $query_args, array( | |
| 59 | + 'paged' => 1, | |
| 60 | + 'items_per_page' => 20, | |
| 61 | + ) ); | |
| 62 | + | |
| 63 | + if( isset( $query_args['paged'] ) ) { | |
| 64 | + $query_args['paged'] = absint( $query_args['paged'] ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Filter query vars | |
| 69 | + * | |
| 70 | + * @param array $query_args | |
| 71 | + * | |
| 72 | + * @return array | |
| 73 | + */ | |
| 74 | + $query_args = apply_filters( 'ct_ajax_list_table_' . $ct_table->name . '_query_args', $query_args ); | |
| 75 | + | |
| 76 | + $ct_ajax_list_items_per_page = $query_args['items_per_page']; | |
| 77 | + add_filter( 'edit_' . $ct_table->name . '_per_page', 'ct_ajax_list_override_items_per_page' ); | |
| 78 | + | |
| 79 | + // Set up vars | |
| 80 | + $ct_query = new CT_Query( $query_args ); | |
| 81 | + $ct_list_table = new CT_List_Table(); | |
| 82 | + | |
| 83 | + $ct_list_table->prepare_items(); | |
| 84 | + | |
| 85 | + ob_start(); | |
| 86 | + $ct_list_table->display(); | |
| 87 | + $output = ob_get_clean(); | |
| 88 | + | |
| 89 | + wp_send_json_success( $output ); | |
| 90 | + | |
| 91 | +} | |
| 92 | +add_action( 'wp_ajax_ct_ajax_list_table_request', 'ct_ajax_list_table_handle_request' ); | |