/** * Retrieve entries given search, sort, paging criteria * * @see GFAPI::get_entries() * @see GFFormsModel::get_field_filters_where() * @access public * @param int|array $form_ids The ID of the form or an array IDs of the Forms. Zero for all forms. * @param mixed $passed_criteria (default: null) * @param mixed &$total Optional. An output parameter containing the total number of entries. Pass a non-null value to generate the total count. (default: null) * @return mixed False: Error fetching entries. Array: Multi-dimensional array of Gravity Forms entry arrays */ public static function get_entries($form_ids = null, $passed_criteria = null, &$total = null) { // Filter the criteria before query (includes Adv Filter) $criteria = self::calculate_get_entries_criteria($passed_criteria, $form_ids); do_action('gravityview_log_debug', '[gravityview_get_entries] Final Parameters', $criteria); // Return value $return = null; if (!empty($criteria['cache'])) { $Cache = new GravityView_Cache($form_ids, $criteria); if ($entries = $Cache->get()) { // Still update the total count when using cached results if (!is_null($total)) { $total = GFAPI::count_entries($form_ids, $criteria['search_criteria']); } $return = $entries; } } if (is_null($return) && class_exists('GFAPI') && (is_numeric($form_ids) || is_array($form_ids))) { $entries = GFAPI::get_entries($form_ids, $criteria['search_criteria'], $criteria['sorting'], $criteria['paging'], $total); if (is_wp_error($entries)) { do_action('gravityview_log_error', $entries->get_error_message(), $entries); return false; } if (!empty($criteria['cache']) && isset($Cache)) { // Cache results $Cache->set($entries, 'entries'); } $return = $entries; } /** * @filter `gravityview_entries` Modify the array of entries returned to GravityView after it has been fetched from the cache or from `GFAPI::get_entries()`. * @param array|null $entries Array of entries as returned by the cache or by `GFAPI::get_entries()` * @param array $criteria The final search criteria used to generate the request to `GFAPI::get_entries()` * @param array $passed_criteria The original search criteria passed to `GVCommon::get_entries()` * @param int|null $total Optional. An output parameter containing the total number of entries. Pass a non-null value to generate */ $return = apply_filters('gravityview_entries', $return, $criteria, $passed_criteria, $total); return $return; }
/** * main AJAX logic to retrieve DataTables data */ function get_datatables_data() { global $gravityview_view; if (empty($_POST)) { return; } // Prevent error output ob_start(); // Send correct headers $this->do_ajax_headers('application/javascript'); $this->check_ajax_nonce(); if (empty($_POST['view_id'])) { do_action('gravityview_log_debug', '[DataTables] AJAX request - View ID check failed'); exit(false); } // Prevent emails from being encrypted add_filter('gravityview_email_prevent_encrypt', '__return_true'); do_action('gravityview_log_debug', '[DataTables] AJAX Request ($_POST)', $_POST); // include some frontend logic if (class_exists('GravityView_Plugin') && !class_exists('GravityView_View')) { GravityView_Plugin::getInstance()->frontend_actions(); } // Pass $_GET variables to the View functions, since they're relied on heavily // for searching and filtering, for example the A-Z widget $_GET = json_decode(stripslashes($_POST['getData']), true); $view_id = intval($_POST['view_id']); // create the view object based on the post_id $GravityView_View_Data = GravityView_View_Data::getInstance((int) $_POST['post_id']); // get the view data $view_data = $GravityView_View_Data->get_view($view_id); $view_data['atts']['id'] = $view_id; $atts = $view_data['atts']; // check for order/sorting if (isset($_POST['order'][0]['column'])) { $order_index = $_POST['order'][0]['column']; if (!empty($_POST['columns'][$order_index]['name'])) { // remove prefix 'gv_' $atts['sort_field'] = substr($_POST['columns'][$order_index]['name'], 3); $atts['sort_direction'] = !empty($_POST['order'][0]['dir']) ? strtoupper($_POST['order'][0]['dir']) : 'ASC'; } } // check for search if (!empty($_POST['search']['value'])) { $atts['search_value'] = esc_attr(stripslashes_deep($_POST['search']['value'])); } // Paging/offset $atts['page_size'] = isset($_POST['length']) ? intval($_POST['length']) : ''; $atts['offset'] = isset($_POST['start']) ? intval($_POST['start']) : 0; // prepare to get entries $atts = wp_parse_args($atts, GravityView_View_Data::get_default_args()); // check if someone requested the full filtered data (eg. TableTools print button) if ($atts['page_size'] == '-1') { $mode = 'all'; $atts['page_size'] = PHP_INT_MAX; } else { // regular mode - get view entries $mode = 'page'; } $view_data['atts'] = $atts; $gravityview_view = new GravityView_View($view_data); if (class_exists('GravityView_Cache')) { // We need to fetch the search criteria and pass it to the Cache so that the search is used when generating the cache transient key. $search_criteria = GravityView_frontend::get_search_criteria($atts, $view_data['form_id']); // make sure to allow late filter ( used on Advanced Filter extension ) $criteria = apply_filters('gravityview_search_criteria', array('search_criteria' => $search_criteria), $view_data['form_id'], $_POST['view_id']); $atts['search_criteria'] = $criteria['search_criteria']; // Cache key should also depend on the View assigned fields $atts['directory_table-columns'] = !empty($view_data['fields']['directory_table-columns']) ? $view_data['fields']['directory_table-columns'] : array(); // cache depends on user session $atts['user_session'] = $this->get_user_session(); $Cache = new GravityView_Cache($view_data['form_id'], $atts); if ($output = $Cache->get()) { do_action('gravityview_log_debug', '[DataTables] Cached output found; using cache with key ' . $Cache->get_key()); // update DRAW (mr DataTables is very sensitive!) $temp = json_decode($output, true); $temp['draw'] = intval($_POST['draw']); $output = json_encode($temp); exit($output); } } $view_entries = GravityView_frontend::get_view_entries($atts, $view_data['form_id']); $data = $this->get_output_data($view_entries, $view_data); // wrap all $output = array('draw' => intval($_POST['draw']), 'recordsTotal' => intval($view_entries['count']), 'recordsFiltered' => intval($view_entries['count']), 'data' => $data); do_action('gravityview_log_debug', '[DataTables] Ajax request answer', $output); $json = json_encode($output); if (class_exists('GravityView_Cache')) { do_action('gravityview_log_debug', '[DataTables] Setting cache'); // Cache results $Cache->set($json, 'datatables_output'); } // End prevent error output ob_end_clean(); exit($json); }