예제 #1
0
 /**
  * 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);
 }
 /**
  * Capture bulk actions - gf_entries table
  *
  * @uses  GravityView_frontend::get_search_criteria() Convert the $_POST search request into a properly formatted request.
  * @access public
  * @return void|boolean
  */
 public function process_bulk_action()
 {
     if (!class_exists('RGForms')) {
         return;
     }
     if ('bulk' === RGForms::post('action')) {
         check_admin_referer('gforms_entry_list', 'gforms_entry_list');
         // The action is formatted like: approve-16 or disapprove-16, where the first word is the name of the action and the second is the ID of the form. Bulk action 2 is the bottom bulk action select form.
         $bulk_action = !empty($_POST['bulk_action']) ? $_POST['bulk_action'] : $_POST['bulk_action2'];
         /**
          * The extra '-' is to make sure that there are at *least* two items in array.
          * @see https://github.com/katzwebservices/GravityView/issues/370
          */
         $bulk_action .= '-';
         list($approved_status, $form_id) = explode('-', $bulk_action);
         if (empty($form_id)) {
             do_action('gravityview_log_error', '[process_bulk_action] Form ID is empty from parsing bulk action.', $bulk_action);
             return false;
         }
         // All entries are set to be updated, not just the visible ones
         if (!empty($_POST['all_entries'])) {
             // Convert the current entry search into GF-formatted search criteria
             $search = array('search_field' => isset($_POST['f']) ? $_POST['f'][0] : 0, 'search_value' => isset($_POST['v'][0]) ? $_POST['v'][0] : '', 'search_operator' => isset($_POST['o'][0]) ? $_POST['o'][0] : 'contains');
             $search_criteria = GravityView_frontend::get_search_criteria($search, $form_id);
             // Get all the entry IDs for the form
             $entries = gravityview_get_entry_ids($form_id, $search_criteria);
         } else {
             $entries = $_POST['lead'];
         }
         if (empty($entries)) {
             do_action('gravityview_log_error', '[process_bulk_action] Entries are empty');
             return false;
         }
         $entry_count = count($entries) > 1 ? sprintf(__('%d entries', 'gravityview'), count($entries)) : __('1 entry', 'gravityview');
         switch ($approved_status) {
             case 'approve':
                 self::update_bulk($entries, 1, $form_id);
                 $this->bulk_update_message = sprintf(__('%s approved.', 'gravityview'), $entry_count);
                 break;
             case 'unapprove':
                 self::update_bulk($entries, 0, $form_id);
                 $this->bulk_update_message = sprintf(__('%s disapproved.', 'gravityview'), $entry_count);
                 break;
         }
     }
 }
 /**
  * Capture bulk actions - gf_entries table
  *
  * @uses  GravityView_frontend::get_search_criteria() Convert the $_POST search request into a properly formatted request.
  * @access public
  * @return void|boolean
  */
 public function process_bulk_action()
 {
     if (!is_admin() || !class_exists('GFForms') || empty($_POST)) {
         return false;
     }
     // The action is formatted like: gvapprove-16 or gvunapprove-16, where the first word is the name of the action and the second is the ID of the form.
     $bulk_action = $this->get_gv_bulk_action();
     // gforms_entry_list is the nonce that confirms we're on the right page
     // gforms_update_note is sent when bulk editing entry notes. We don't want to process then.
     if ($bulk_action && rgpost('gforms_entry_list') && empty($_POST['gforms_update_note'])) {
         check_admin_referer('gforms_entry_list', 'gforms_entry_list');
         /**
          * The extra '-' is to make sure that there are at *least* two items in array.
          * @see https://github.com/katzwebservices/GravityView/issues/370
          */
         $bulk_action .= '-';
         list($approved_status, $form_id) = explode('-', $bulk_action);
         if (empty($form_id)) {
             do_action('gravityview_log_error', '[process_bulk_action] Form ID is empty from parsing bulk action.', $bulk_action);
             return false;
         }
         // All entries are set to be updated, not just the visible ones
         if (!empty($_POST['all_entries'])) {
             // Convert the current entry search into GF-formatted search criteria
             $search = array('search_field' => isset($_POST['f']) ? $_POST['f'][0] : 0, 'search_value' => isset($_POST['v'][0]) ? $_POST['v'][0] : '', 'search_operator' => isset($_POST['o'][0]) ? $_POST['o'][0] : 'contains');
             $search_criteria = GravityView_frontend::get_search_criteria($search, $form_id);
             // Get all the entry IDs for the form
             $entries = gravityview_get_entry_ids($form_id, $search_criteria);
         } else {
             // Changed from 'lead' to 'entry' in 2.0
             $entries = isset($_POST['lead']) ? $_POST['lead'] : $_POST['entry'];
         }
         if (empty($entries)) {
             do_action('gravityview_log_error', '[process_bulk_action] Entries are empty');
             return false;
         }
         $entry_count = count($entries) > 1 ? sprintf(__('%d entries', 'gravityview'), count($entries)) : __('1 entry', 'gravityview');
         switch ($approved_status) {
             case $this->bulk_action_prefixes['approve']:
                 self::update_bulk($entries, 1, $form_id);
                 $this->bulk_update_message = sprintf(__('%s approved.', 'gravityview'), $entry_count);
                 break;
             case $this->bulk_action_prefixes['unapprove']:
                 self::update_bulk($entries, 0, $form_id);
                 $this->bulk_update_message = sprintf(__('%s disapproved.', 'gravityview'), $entry_count);
                 break;
         }
     }
 }