public function get_results($form_id) { $this->log_debug(__METHOD__ . '(): Running.'); $capability = apply_filters('gform_web_api_capability_get_results', 'gravityforms_view_entries'); $this->authorize($capability); $s = rgget('s'); // search criteria $search_criteria = false === empty($s) && is_array($s) ? $s : array(); $form = GFAPI::get_form($form_id); if (!$form) { self::die_not_found(); } // for the Web API return all fields $fields = rgar($form, 'fields'); $form_id = $form['id']; $key = $this->get_results_cache_key($form_id, $fields, $search_criteria); $key_tmp = 'tmp' . $key; $data = get_option($key, array()); $cache_meta = $this->get_results_cache_meta($form_id); // add the cache meta early so form editor updates can test for valid field hash if (empty($cache_meta)) { $this->update_results_cache_meta($form_id, $fields, 0); } $cache_expiry = rgar($cache_meta, 'timestamp'); $cache_timestamp = isset($data['timestamp']) ? $data['timestamp'] : 0; $cache_expired = $cache_expiry ? $cache_expiry > $cache_timestamp : false; // check for valid cached results first if (!empty($data) && 'complete' == rgar($data, 'status') && !$cache_expired) { $results = $data; $status = 200; if (isset($results['progress'])) { unset($results['progress']); } } else { $state = get_option($key_tmp); if (empty($state) || 'complete' == rgar($data, 'status') && $cache_expired) { if (!class_exists('GFResults')) { require_once GFCommon::get_base_path() . '/includes/addon/class-gf-results.php'; } $gf_results = new GFResults($this->_slug, array()); $max_execution_time = 5; $results = $gf_results->get_results_data($form, $fields, $search_criteria, $state, $max_execution_time); if ('complete' == rgar($data, 'status')) { $status = 200; if (false == empty($state)) { delete_option($key_tmp); } } else { if (false === empty($data) && 'complete' == rgar($data, 'status') && $cache_expired) { $data['status'] = 'expired'; $data['progress'] = $results['progress']; $this->update_results_cache($key, $data); } $this->update_results_cache($key_tmp, $results); $this->schedule_results_cron($form, $fields, $search_criteria); if ($data) { $results = $data; } $status = 202; } } else { // The cron task is recursive, not periodic, so system restarts, script timeouts and memory issues can prevent the cron from restarting. // Check timestamp and kick off the cron again if it appears to have stopped $state_timestamp = rgar($state, 'timestamp'); $state_age = time() - $state_timestamp; if ($state_age > 180 && !$this->results_cron_is_scheduled($form, $fields, $search_criteria)) { $this->schedule_results_cron($form, $fields, $search_criteria); } if (false === empty($data) && 'expired' == rgar($data, 'status')) { $results = $data; } else { $results = $state; } $status = 202; } } $fields = rgar($results, 'field_data'); if (!empty($fields)) { // add choice labels to the results so the client doesn't need to cross-reference with the form object $results['field_data'] = $this->results_data_add_labels($form, $fields); } $this->end($status, $results); }
public function get_results($form_id) { $this->authorize("gravityforms_view_entries"); $s = rgget("s"); // search criteria $search_criteria = false === empty($s) && is_array($s) ? $s : array(); $form = GFAPI::get_form($form_id); if (!$form) { self::die_not_found(); } // for the Web API return all fields $fields = rgar($form, "fields"); $form_id = $form["id"]; $key = $this->get_results_cache_key($form_id, $fields, $search_criteria); $key_tmp = "tmp" . $key; $data = get_option($key, array()); $cache_meta = $this->get_results_cache_meta($form_id); // add the cache meta early so form editor updates can test for valid field hash if (empty($cache_meta)) { $this->update_results_cache_meta($form_id, $fields, 0); } $cache_expiry = rgar($cache_meta, "timestamp"); $cache_timestamp = isset($data["timestamp"]) ? $data["timestamp"] : 0; $cache_expired = $cache_expiry ? $cache_expiry > $cache_timestamp : false; // check for valid cached results first if (!empty($data) && "complete" == rgar($data, "status") && !$cache_expired) { $results = $data; $status = 200; if (isset($results["progress"])) { unset($results["progress"]); } } else { $state = get_option($key_tmp); if (empty($state) || "complete" == rgar($data, "status") && $cache_expired) { if (!class_exists("GFResults")) { require_once GFCommon::get_base_path() . "/includes/addon/class-gf-results.php"; } $gf_results = new GFResults($this->_slug, array()); $max_execution_time = 5; $results = $gf_results->get_results_data($form, $fields, $search_criteria, $state, $max_execution_time); if ("complete" == $results["status"]) { $status = 200; if (false == empty($state)) { delete_option($key_tmp); } } else { if (false === empty($data) && "complete" == rgar($data, "status") && $cache_expired) { $data["status"] = "expired"; $data["progress"] = $results["progress"]; $this->update_results_cache($key, $data); } $this->update_results_cache($key_tmp, $results); $this->schedule_results_cron($form, $fields, $search_criteria); if ($data) { $results = $data; } $status = 202; } } else { // The cron task is recursive, not periodic, so system restarts, script timeouts and memory issues can prevent the cron from restarting. // Check timestamp and kick off the cron again if it appears to have stopped $state_timestamp = rgar($state, "timestamp"); $state_age = time() - $state_timestamp; if ($state_age > 180 && !$this->results_cron_is_scheduled($form, $fields, $search_criteria)) { $this->schedule_results_cron($form, $fields, $search_criteria); } if (false === empty($data) && "expired" == rgar($data, "status")) { $results = $data; } else { $results = $state; } $status = 202; } } $fields = $results["field_data"]; // add choice labels to the results so the client doesn't need to cross-reference with the form object $results["field_data"] = $this->results_data_add_labels($form, $fields); $this->end($status, $results); }
private static function get_addon_global_data($form, $options, $fields) { /* if the results class isn't loaded, load it */ if (!class_exists("GFResults")) { require_once GFCommon::get_base_path() . "/includes/addon/class-gf-results.php"; } $form_id = $form['id']; /* add form filter to keep in line with GF standard */ $form = apply_filters("gform_form_pre_results_{$form_id}", apply_filters('gform_form_pre_results', $form)); /* initiat the results class */ $gf_results = new GFResults('', $options); /* ensure that only active leads are queried */ $search = array('field_filters' => array('mode' => ''), 'status' => 'active'); /* get the results */ $data = $gf_results->get_results_data($form, $fields, $search); /* unset some array keys we don't need */ unset($data['status']); unset($data['timestamp']); return $data; }