function prepare_items() { global $wpdb, $per_page, $frm_settings; $paged = $this->get_pagenum(); $default_orderby = 'name'; $default_order = 'ASC'; $orderby = isset($_REQUEST['orderby']) ? $_REQUEST['orderby'] : $default_orderby; $order = isset($_REQUEST['order']) ? $_REQUEST['order'] : $default_order; $page = $this->get_pagenum(); $default_count = empty($this->page_name) ? 20 : 10; $per_page = $this->get_items_per_page('formidable_page_formidable' . str_replace('-', '_', $this->page_name) . '_per_page', $default_count); $start = isset($_REQUEST['start']) ? $_REQUEST['start'] : ($page - 1) * $per_page; $s = isset($_REQUEST['s']) ? stripslashes($_REQUEST['s']) : ''; $fid = isset($_REQUEST['fid']) ? $_REQUEST['fid'] : ''; if ($s != '') { preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches); $search_terms = array_map('trim', $matches[0]); } $s_query = " (status is NULL OR status = '' OR status = 'published') AND default_template=0 AND is_template = " . (int) $this->params['template']; if ($s != '') { foreach ((array) $search_terms as $term) { if (!empty($s_query)) { $s_query .= " AND"; } $term = FrmAppHelper::esc_like($term); $s_query .= $wpdb->prepare(" (name like %s OR description like %s OR created_at like %s)", '%' . $term . '%', '%' . $term . '%', '%' . $term . '%'); unset($term); } } $frm_form = new FrmForm(); $this->items = $frm_form->getAll($s_query, " ORDER BY {$orderby} {$order}", " LIMIT {$start}, {$per_page}", true, false); $total_items = FrmAppHelper::getRecordCount($s_query, $this->table_name); $this->set_pagination_args(array('total_items' => $total_items, 'per_page' => $per_page)); }
/** * @param string $key * @param string $where */ private static function interpret_array_to_sql($key, $value, &$where, &$values) { $key = trim($key); if (strpos($key, 'created_at') !== false || strpos($key, 'updated_at') !== false) { $k = explode(' ', $key); $where .= ' DATE_FORMAT(' . reset($k) . ', %s) ' . str_replace(reset($k), '', $key); $values[] = '%Y-%m-%d %H:%i:%s'; } else { $where .= ' ' . $key; } $lowercase_key = explode(' ', strtolower($key)); $lowercase_key = end($lowercase_key); if (is_array($value)) { // translate array of values to "in" if (strpos($lowercase_key, 'like') !== false) { $where = preg_replace('/' . $key . '$/', '', $where); $where .= '('; $start = true; foreach ($value as $v) { if (!$start) { $where .= ' OR '; } $start = false; $where .= $key . ' %s'; $values[] = '%' . FrmAppHelper::esc_like($v) . '%'; } $where .= ')'; } else { if (!empty($value)) { $where .= ' in (' . FrmAppHelper::prepare_array_values($value, '%s') . ')'; $values = array_merge($values, $value); } } } else { if (strpos($lowercase_key, 'like') !== false) { /** * Allow string to start or end with the value * If the key is like% then skip the first % for starts with * If the key is %like then skip the last % for ends with */ $start = $end = '%'; if ($lowercase_key == 'like%') { $start = ''; $where = rtrim($where, '%'); } else { if ($lowercase_key == '%like') { $end = ''; $where = rtrim(rtrim($where, '%like'), '%LIKE'); $where .= 'like'; } } $where .= ' %s'; $values[] = $start . FrmAppHelper::esc_like($value) . $end; } else { if ($value === null) { $where .= ' IS NULL'; } else { // allow a - to prevent = from being added if (substr($key, -1) == '-') { $where = rtrim($where, '-'); } else { $where .= '='; } $where .= is_numeric($value) ? strpos($value, '.') !== false ? '%f' : '%d' : '%s'; $values[] = $value; } } } }
public static function filter_where($entry_ids, $args) { global $wpdb, $frmdb, $frm_entry_meta, $frm_field; $defaults = array('where_opt' => false, 'where_is' => '=', 'where_val' => '', 'form_id' => false, 'form_posts' => array(), 'after_where' => false, 'display' => false, 'drafts' => 0); extract(wp_parse_args($args, $defaults)); $form_id = (int) $form_id; if (!$form_id or !$where_opt or !is_numeric($where_opt)) { return $entry_ids; } $where_field = $frm_field->getOne($where_opt); if (!$where_field) { return $entry_ids; } if ($where_val == 'NOW') { $where_val = date_i18n('Y-m-d', strtotime(current_time('mysql'))); } if ($where_field->type == 'date' and !empty($where_val)) { $where_val = date('Y-m-d', strtotime($where_val)); } else { if ($where_is == '=' and $where_val != '' and ($where_field->type == 'checkbox' or $where_field->type == 'select' and isset($where_field->field_options['multiple']) and $where_field->field_options['multiple'] or $where_field->type == 'data' and $where_field->field_options['data_type'] == 'checkbox' and is_numeric($where_val))) { $where_is = 'LIKE'; } } if ($where_field->form_id != $form_id) { //TODO: get linked entry IDs and get entries where data field value(s) in linked entry IDs } $temp_where_is = str_replace(array('!', 'not '), '', $where_is); //get values that aren't blank and then remove them from entry list if ($where_val == '' and $temp_where_is == '=') { $temp_where_is = '!='; } $orig_where_val = $where_val; if ($where_is == 'LIKE' or $where_is == 'not LIKE') { //add extra slashes to match values that are escaped in the database $where_val_esc = "'%" . esc_sql(FrmAppHelper::esc_like(addslashes($where_val))) . "%'"; $where_val = "'%" . esc_sql(FrmAppHelper::esc_like($where_val)) . "%'"; } else { if (!strpos($where_is, 'in')) { $where_val_esc = "'" . str_replace('\\', '\\\\\\', esc_sql($where_val)) . "'"; $where_val = "'" . esc_sql($where_val) . "'"; } } $where_val = apply_filters('frm_filter_where_val', $where_val, $args); $field_options = maybe_unserialize($where_field->field_options); //Filter by DFE text if ($where_field->type == 'data' && !is_numeric($where_val) && $orig_where_val != '' && (!isset($field_options['post_field']) || $field_options['post_field'] != 'post_category')) { //Get entry IDs by DFE text if ($where_is == 'LIKE' or $where_is == 'not LIKE') { $linked_id = $frm_entry_meta->search_entry_metas($orig_where_val, $where_field->field_options['form_select'], $temp_where_is); } else { $linked_id = $wpdb->get_col($wpdb->prepare("SELECT item_id FROM {$frmdb->entry_metas} WHERE field_id=%d AND meta_value {$temp_where_is} %s", $where_field->field_options['form_select'], $orig_where_val)); } //If text doesn't return any entry IDs, get entry IDs from entry key if (!$linked_id) { $linked_field = $frm_field->getOne($where_field->field_options['form_select']); $linked_id = $wpdb->get_col("SELECT id FROM {$frmdb->entries} WHERE form_id={$linked_field->form_id} AND item_key {$temp_where_is} {$where_val}"); } //Change $where_val to linked entry IDs if ($linked_id) { $linked_id = (array) $linked_id; if ($where_field->field_options['data_type'] == 'checkbox' || $where_field->field_options['data_type'] == 'select' && isset($where_field->field_options['multiple']) && $where_field->field_options['multiple'] == 1) { $where_val = "'%" . implode("%' OR meta_value LIKE '%", $linked_id) . "%'"; if ($where_is == '!=' or $where_is == 'not LIKE') { $temp_where_is = 'LIKE'; } else { if ($where_is == '=' or $where_is == 'LIKE') { $where_is = $temp_where_is = 'LIKE'; } } } else { $where_is = $temp_where_is = (strpos($where_is, '!') === false and strpos($where_is, 'not') === false) ? ' in ' : ' not in '; $where_val = '(' . implode(',', $linked_id) . ')'; } unset($where_val_esc); $where_val = apply_filters('frm_filter_dfe_where_val', $where_val, $args); } unset($linked_id); } $where_statement = "(meta_value " . (in_array($where_field->type, array('number', 'scale')) ? ' +0 ' : '') . $temp_where_is . " " . $where_val . " "; if (isset($where_val_esc) and $where_val_esc != $where_val) { $where_statement .= " OR meta_value " . (in_array($where_field->type, array('number', 'scale')) ? ' +0 ' : '') . $temp_where_is . " " . $where_val_esc; } $where_statement .= ") and fi.id=" . (int) $where_opt; $where_statement = apply_filters('frm_where_filter', $where_statement, $args); $new_ids = $frm_entry_meta->getEntryIds($where_statement, '', '', true, $drafts); if ($where_is != $temp_where_is) { $new_ids = array_diff($entry_ids, $new_ids); } unset($temp_where_is); if (!empty($form_posts)) { //if there are posts linked to entries for this form if (isset($field_options['post_field']) and in_array($field_options['post_field'], array('post_category', 'post_custom', 'post_status', 'post_content', 'post_excerpt', 'post_title', 'post_name', 'post_date'))) { $post_ids = array(); foreach ($form_posts as $form_post) { $post_ids[$form_post->post_id] = $form_post->id; if (!in_array($form_post->id, $new_ids)) { $new_ids[] = $form_post->id; } } if (!empty($post_ids)) { if ($field_options['post_field'] == 'post_category') { $add_posts = $remove_posts = false; //check categories $temp_where_is = str_replace(array('!', 'not '), '', $where_is); $join_with = ' OR '; $t_where = "t.term_id {$temp_where_is} {$where_val}"; $t_where .= " {$join_with} t.slug {$temp_where_is} {$where_val}"; $t_where .= " {$join_with} t.name {$temp_where_is} {$where_val}"; unset($temp_where_is); $query = "SELECT tr.object_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON tt.term_id = t.term_id INNER JOIN {$wpdb->term_relationships} AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = '{$field_options['taxonomy']}' AND ({$t_where}) AND tr.object_id IN (" . implode(',', array_keys($post_ids)) . ")"; $add_posts = $wpdb->get_col($query); if ($where_is == '!=' or $where_is == 'not LIKE') { $remove_posts = $add_posts; $add_posts = false; } else { if (!$add_posts) { return array(); } } } else { if ($field_options['post_field'] == 'post_custom' and $field_options['custom_field'] != '') { //check custom fields $add_posts = $wpdb->get_col("SELECT post_id FROM {$wpdb->postmeta} WHERE post_id in (" . implode(',', array_keys($post_ids)) . ") AND meta_key='" . $field_options['custom_field'] . "' AND meta_value " . (in_array($where_field->type, array('number', 'scale')) ? ' +0 ' : '') . $where_is . " " . $where_val); } else { //if field is post field $add_posts = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE ID in (" . implode(',', array_keys($post_ids)) . ") AND " . $field_options['post_field'] . (in_array($where_field->type, array('number', 'scale')) ? ' +0 ' : ' ') . $where_is . " " . $where_val); } } if ($add_posts and !empty($add_posts)) { $new_ids = array(); foreach ($add_posts as $add_post) { if (!in_array($post_ids[$add_post], $new_ids)) { $new_ids[] = $post_ids[$add_post]; } } } if (isset($remove_posts)) { if (!empty($remove_posts)) { foreach ($remove_posts as $remove_post) { $key = array_search($post_ids[$remove_post], $new_ids); if ($key and $new_ids[$key] == $post_ids[$remove_post]) { unset($new_ids[$key]); } unset($key); } } unset($remove_posts); } else { if (!$add_posts) { $new_ids = array(); } } } } } if ($after_where) { $entry_ids = array_intersect($new_ids, $entry_ids); } else { $entry_ids = $new_ids; } return $entry_ids; }
public static function get_field_stats($id, $type = 'total', $user_id = false, $value = false, $round = 100, $limit = '', $atts = array(), $drafts = false) { global $frm_entry_meta, $wpdb, $frmdb, $frm_post_ids, $frm_field; $field = $frm_field->getOne($id); if (!$field) { return 0; } $id = $field->id; if (isset($atts['thousands_sep']) && $atts['thousands_sep']) { $thousands_sep = $atts['thousands_sep']; unset($atts['thousands_sep']); $round = $round == 100 ? 2 : $round; } $where_value = ''; if ($value) { $slash_val = strpos($value, '\\') === false ? addslashes($value) : $value; if ($field->type == 'checkbox' || $field->type == 'select' && isset($field->field_options['multiple']) && $field->field_options['multiple']) { $where_value = $wpdb->prepare(" AND (meta_value LIKE %s OR meta_value LIKE %s )", '%' . FrmAppHelper::esc_like($value) . '%', '%' . FrmAppHelper::esc_like($slash_val) . '%'); //add extra slashes to match values that are escaped in the database } else { $where_value = $wpdb->prepare(" AND (meta_value = %s OR meta_value = %s )", FrmAppHelper::esc_like($value), addcslashes($slash_val, '_%')); } unset($slash_val); } //if(!$frm_post_ids) $frm_post_ids = array(); $post_ids = array(); if (isset($frm_post_ids[$id])) { $form_posts = $frm_post_ids[$id]; } else { $where_post = array('form_id' => $field->form_id, 'post_id >' => 1); if ($drafts != 'both') { $where_post['is_draft'] = $drafts; } if ($user_id) { $where_post['user_id'] = $user_id; } $form_posts = $frmdb->get_records($frmdb->entries, $where_post, '', '', 'id,post_id'); $frm_post_ids[$id] = $form_posts; } if ($form_posts) { foreach ($form_posts as $form_post) { $post_ids[$form_post->id] = $form_post->post_id; } } if (!empty($limit)) { $limit = " LIMIT " . $limit; } if ($value) { $atts[$id] = $value; } if (!empty($atts)) { $entry_ids = array(); if (isset($atts['entry_id']) and $atts['entry_id'] and is_numeric($atts['entry_id'])) { $entry_ids[] = $atts['entry_id']; } $after_where = false; foreach ($atts as $orig_f => $val) { if (strpos($val, '"') === 0 and substr($val, -1) != '"' or strpos($val, "'") === 0 and substr($val, -1) != "'") { //parse atts back together if they were broken at spaces $next_val = array('char' => substr($val, 0, 1), 'val' => $val); continue; } else { if (!isset($next_val)) { $temp = FrmAppHelper::replace_quotes($val); foreach (array('"', "'") as $q) { if (substr($temp, -1) != $q and (strpos($temp, '<' . $q) or strpos($temp, '>' . $q))) { $next_val = array('char' => $q, 'val' => $val); $cont = true; } unset($q); } unset($temp); if (isset($cont)) { unset($cont); continue; } } } if (isset($next_val)) { if (substr(FrmAppHelper::replace_quotes($val), -1) == $next_val['char']) { $val = $next_val['val'] . ' ' . $val; unset($next_val); } else { $next_val['val'] .= ' ' . $val; continue; } } $entry_ids = self::get_field_matches(compact('entry_ids', 'orig_f', 'val', 'id', 'atts', 'field', 'form_posts', 'after_where', 'drafts')); $after_where = true; } if (empty($entry_ids)) { if ($type == 'star') { $stat = ''; ob_start(); include FrmAppHelper::plugin_path() . '/pro/classes/views/frmpro-fields/star_disabled.php'; $contents = ob_get_contents(); ob_end_clean(); return $contents; } else { return 0; } } foreach ($post_ids as $entry_id => $post_id) { if (!in_array($entry_id, $entry_ids)) { unset($post_ids[$entry_id]); } } $where_value .= " AND it.item_id in (" . implode(',', $entry_ids) . ")"; } $join = ''; if (is_numeric($id)) { $where = $wpdb->prepare("field_id=%d", $id); } else { $join .= " LEFT OUTER JOIN {$frmdb->fields} fi ON it.field_id=fi.id"; $where = $wpdb->prepare("fi.field_key=%s", $id); } $where .= $where_value; if ($user_id) { $where .= $wpdb->prepare(" AND en.user_id=%d", $user_id); } $join .= " LEFT OUTER JOIN {$frmdb->entries} en ON en.id=it.item_id"; if ($drafts != 'both') { $where .= $wpdb->prepare(' AND en.is_draft=%d', $drafts); } $field_metas = $wpdb->get_col("SELECT meta_value FROM {$frmdb->entry_metas} it {$join} WHERE {$where} ORDER BY it.created_at DESC" . $limit); if (!empty($post_ids)) { if (isset($field->field_options['post_field']) and $field->field_options['post_field']) { if ($field->field_options['post_field'] == 'post_custom') { //get custom post field value $post_values = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key= %s AND post_id in (" . implode(',', $post_ids) . ")", $field->field_options['custom_field'])); } else { if ($field->field_options['post_field'] == 'post_category') { $post_query = "SELECT tr.object_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON tt.term_id = t.term_id INNER JOIN {$wpdb->term_relationships} AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = %d AND tr.object_id in (" . implode(',', $post_ids) . ")"; $post_query_vars = array($field->field_options['taxonomy']); if ($value) { $post_query .= ' AND (t.term_id = %s OR t.slug = %s OR t.name = %s)'; $post_query_vars[] = $value; $post_query_vars[] = $value; $post_query_vars[] = $value; } $post_values = $wpdb->get_col($wpdb->prepare($post_query, $post_query_vars)); $post_values = array_unique($post_values); } else { $post_values = $wpdb->get_col("SELECT {$field->field_options['post_field']} FROM {$wpdb->posts} WHERE ID in (" . implode(',', $post_ids) . ")"); } } $field_metas = array_merge($post_values, $field_metas); } } if ($type != 'star') { unset($field); } if (empty($field_metas)) { if ($type == 'star') { $stat = ''; ob_start(); include FrmAppHelper::plugin_path() . '/pro/classes/views/frmpro-fields/star_disabled.php'; $contents = ob_get_contents(); ob_end_clean(); return $contents; } else { return 0; } } $count = count($field_metas); $total = array_sum($field_metas); switch ($type) { case 'average': case 'mean': case 'star': $stat = $total / $count; break; case 'median': rsort($field_metas); $n = ceil($count / 2); // Middle of the array if ($count % 2) { $stat = $field_metas[$n - 1]; // If number is odd } else { $n2 = floor($count / 2); // Other middle of the array $stat = ($field_metas[$n - 1] + $field_metas[$n2 - 1]) / 2; } $stat = maybe_unserialize($stat); if (is_array($stat)) { $stat = 0; } break; case 'deviation': $mean = $total / $count; $stat = 0.0; foreach ($field_metas as $i) { $stat += pow($i - $mean, 2); } if ($count > 1) { $stat /= $count - 1; $stat = sqrt($stat); } else { $stat = 0; } break; case 'minimum': $stat = min($field_metas); break; case 'maximum': $stat = max($field_metas); break; case 'count': $stat = $count; break; case 'unique': $stat = array_unique($field_metas); $stat = count($stat); break; case 'total': default: $stat = $total; } $stat = round($stat, $round); if ($type == 'star') { ob_start(); include FrmAppHelper::plugin_path() . '/pro/classes/views/frmpro-fields/star_disabled.php'; $contents = ob_get_contents(); ob_end_clean(); return $contents; } if ($round && $round < 5 || isset($thousands_sep)) { $thousands_sep = isset($thousands_sep) ? $thousands_sep : ','; $stat = number_format($stat, $round, '.', $thousands_sep); } return $stat; }
public static function get_search_str($where_clause = '', $search_str, $form_id = false, $fid = false) { global $frm_entry_meta, $wpdb; $where_item = ''; $join = ' ('; if (!is_array($search_str)) { $search_str = explode(" ", $search_str); } foreach ($search_str as $search_param) { $unescaped_search_param = $search_param; $search_param = FrmAppHelper::esc_like($search_param); if (!is_numeric($fid)) { $where_item .= empty($where_item) ? ' (' : ' OR'; if (in_array($fid, array('created_at', 'user_id', 'updated_at', 'id'))) { if ($fid == 'user_id' && !is_numeric($search_param)) { $search_param = FrmProAppHelper::get_user_id_param($unescaped_search_param); } $where_item .= $wpdb->prepare(" it.{$fid} like %s", '%' . $search_param . '%'); } else { $where_item .= $wpdb->prepare(' it.name like %s OR it.item_key like %s OR it.description like %s OR it.created_at like %s', '%' . $search_param . '%', '%' . $search_param . '%', '%' . $search_param . '%', '%' . $search_param . '%'); } } if (empty($fid) || is_numeric($fid)) { $where_entries = $wpdb->prepare('(meta_value LIKE %s', '%' . $search_param . '%'); if ($data_fields = FrmProFormsHelper::has_field('data', $form_id, false)) { $df_form_ids = array(); //search the joined entry too foreach ((array) $data_fields as $df) { //don't check if a different field is selected if (is_numeric($fid) && (int) $fid != $df->id) { continue; } $df->field_options = maybe_unserialize($df->field_options); if (isset($df->field_options['form_select']) && is_numeric($df->field_options['form_select'])) { $df_form_ids[] = $df->field_options['form_select']; } unset($df); } unset($data_fields); if (!empty($df_form_ids)) { $data_form_ids = $wpdb->get_col("SELECT form_id FROM {$wpdb->prefix}frm_fields WHERE id in (" . implode(',', array_filter($df_form_ids, 'is_numeric')) . ")"); if ($data_form_ids) { $data_entry_ids = $frm_entry_meta->getEntryIds("fi.form_id in (" . implode(',', $data_form_ids) . ") " . $wpdb->prepare("and meta_value LIKE %s", '%' . $search_param . '%')); if (!empty($data_entry_ids)) { $where_entries .= " OR meta_value in (" . implode(',', $data_entry_ids) . ")"; } } unset($data_form_ids); } unset($df_form_ids); } $where_entries .= ")"; if (is_numeric($fid)) { $where_entries .= $wpdb->prepare(' AND field_id=%d', $fid); } if (is_admin() && isset($_GET) && isset($_GET['page']) && $_GET['page'] == 'formidable-entries') { $include_drafts = true; } else { $include_drafts = false; } $meta_ids = $frm_entry_meta->getEntryIds($where_entries, '', '', true, $include_drafts); if (!empty($where_clause)) { $where_clause .= " AND" . $join; if (!empty($join)) { $join = ''; } } if (!empty($meta_ids)) { $where_clause .= " it.id in (" . implode(',', $meta_ids) . ")"; } else { $where_clause .= " it.id=0"; } } } if (!empty($where_item)) { $where_item .= ')'; if (!empty($where_clause)) { $where_clause .= empty($fid) ? ' OR' : ' AND'; } $where_clause .= $where_item; } if (empty($join)) { $where_clause .= ')'; } return $where_clause; }
public static function get_search_ids($s, $form_id) { global $wpdb, $frm_entry_meta; if (empty($s)) { return false; } preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches); $search_terms = array_map('trim', $matches[0]); $n = '%'; //!empty($q['exact']) ? '' : '%'; $p_search = $search = ''; $search_or = ''; $e_ids = array(); $data_field = FrmProFormsHelper::has_field('data', $form_id, false); foreach ((array) $search_terms as $term) { $term = FrmAppHelper::esc_like($term); $p_search .= $wpdb->prepare(" AND (({$wpdb->posts}.post_title LIKE %s) OR ({$wpdb->posts}.post_content LIKE %s))", $n . $term . $n, $n . $term . $n); $search .= $wpdb->prepare($search_or . 'meta_value LIKE %s', $n . $term . $n); $search_or = ' OR '; if (is_numeric($term)) { $e_ids[] = (int) $term; } if ($data_field) { $df_form_ids = array(); //search the joined entry too foreach ((array) $data_field as $df) { if (is_numeric($df->field_options['form_select'])) { $df_form_ids[] = (int) $df->field_options['form_select']; } unset($df); } $data_form_ids = $wpdb->get_col("SELECT form_id FROM {$wpdb->prefix}frm_fields WHERE id in (" . implode(',', $df_form_ids) . ")"); unset($df_form_ids); if ($data_form_ids) { $data_entry_ids = $frm_entry_meta->getEntryIds("fi.form_id in (" . implode(',', $data_form_ids) . ")" . $wpdb->prepare(' AND meta_value LIKE %s', '%' . $term . '%')); if ($data_entry_ids) { $search .= "{$search_or}meta_value in (" . implode(',', $data_entry_ids) . ")"; } } unset($data_form_ids); } } $p_ids = ''; $matching_posts = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE 1=1 {$p_search}"); if ($matching_posts) { $p_ids = $wpdb->get_col("SELECT id FROM {$wpdb->prefix}frm_items WHERE post_id in (" . implode(',', $matching_posts) . ") AND form_id=" . (int) $form_id); $p_ids = $p_ids ? " OR item_id in (" . implode(',', $p_ids) . ")" : ''; } if (!empty($e_ids)) { $p_ids .= " OR item_id in (" . implode(',', $e_ids) . ")"; } return $frm_entry_meta->getEntryIds("(({$search}){$p_ids}) and fi.form_id=" . (int) $form_id); }
public static function get_display_data($display, $content = '', $entry_id = false, $extra_atts = array()) { global $frmpro_display, $frm_entry, $frmpro_settings, $frm_entry_meta, $frm_vars, $post; $frm_vars['forms_loaded'][] = true; if (!isset($display->frm_form_id)) { $display = FrmProDisplaysHelper::setup_edit_vars($display, false); } if (!isset($display->frm_form_id) or empty($display->frm_form_id)) { return $content; } // check if entry needs to be deleted before loading entries if (FrmAppHelper::get_param('frm_action') == 'destroy' && isset($_GET['entry'])) { $deleted = FrmProEntriesController::ajax_destroy($display->frm_form_id, false, false); if (!empty($deleted)) { $message = '<div class="with_frm_style"><div class="frm_message">' . $deleted . '</div></div>'; } unset($_GET['entry']); } //for backwards compatability $display->id = $display->frm_old_id; $display->display_key = $display->post_name; $defaults = array('filter' => false, 'user_id' => '', 'limit' => '', 'page_size' => '', 'order_by' => '', 'order' => '', 'drafts' => false, 'auto_id' => ''); extract(wp_parse_args($extra_atts, $defaults)); //if (FrmProAppHelper::rewriting_on() && $frmpro_settings->permalinks ) // self::parse_pretty_entry_url(); if ($display->frm_show_count == 'one' and is_numeric($display->frm_entry_id) and $display->frm_entry_id > 0 and !$entry_id) { $entry_id = $display->frm_entry_id; } $entry = false; $show = 'all'; global $wpdb, $frmpro_entry; $where = $wpdb->prepare('it.form_id=%d', $display->frm_form_id); if (in_array($display->frm_show_count, array('dynamic', 'calendar', 'one'))) { $one_param = isset($_GET['entry']) ? $_GET['entry'] : $auto_id; $get_param = isset($_GET[$display->frm_param]) ? $_GET[$display->frm_param] : ($display->frm_show_count == 'one' ? $one_param : $auto_id); unset($one_param); if ($get_param) { if (($display->frm_type == 'id' or $display->frm_show_count == 'one') and is_numeric($get_param)) { $where .= $wpdb->prepare(' AND it.id=%d', $get_param); } else { $where .= $wpdb->prepare(' AND it.item_key=%s', $get_param); } $entry = $frm_entry->getAll($where, '', 1, 0); if ($entry) { $entry = reset($entry); } if ($entry and $entry->post_id) { //redirect to single post page if this entry is a post if (in_the_loop() and $display->frm_show_count != 'one' and !is_single($entry->post_id) and $post->ID != $entry->post_id) { $this_post = get_post($entry->post_id); if (in_array($this_post->post_status, array('publish', 'private'))) { die(FrmAppHelper::js_redirect(get_permalink($entry->post_id))); } } } } unset($get_param); } if ($entry and in_array($display->frm_show_count, array('dynamic', 'calendar'))) { $new_content = $display->frm_dyncontent; $show = 'one'; } else { $new_content = $display->post_content; } $show = $display->frm_show_count == 'one' ? 'one' : $show; $shortcodes = FrmProDisplaysHelper::get_shortcodes($new_content, $display->frm_form_id); //don't let page size and limit override single entry displays if ($display->frm_show_count == 'one') { $display->frm_page_size = $display->frm_limit = ''; } //don't keep current content if post type is frm_display if ($post and $post->post_type == 'frm_display') { $display->frm_insert_loc = ''; } $pagination = ''; $is_draft = !empty($drafts) ? 1 : 0; $form_query = $wpdb->prepare("SELECT id, post_id FROM {$wpdb->prefix}frm_items WHERE form_id=%d and post_id>%d", $display->frm_form_id, 1); if ($drafts != 'both') { $form_query .= $wpdb->prepare(' AND is_draft=%d', $is_draft); } if ($entry and $entry->form_id == $display->frm_form_id) { $form_query .= $wpdb->prepare(' AND id=%d', $entry->id); $form_posts = $wpdb->get_results($form_query); $entry_ids = array($entry->id); } else { $form_posts = $wpdb->get_results($form_query); //Only get $entry_ids if filters are set or if frm_search parameter is set if (isset($display->frm_where) && !empty($display->frm_where) && (!$entry || !$post || empty($auto_id)) || isset($_GET['frm_search'])) { $entry_query = $wpdb->prepare("SELECT id FROM {$wpdb->prefix}frm_items WHERE form_id=%d", $display->frm_form_id); if ($drafts != 'both') { $entry_query .= $wpdb->prepare(" AND is_draft=%d", $is_draft); } $entry_ids = $wpdb->get_col($entry_query); unset($entry_query); } } unset($form_query); $empty_msg = (isset($display->frm_empty_msg) and !empty($display->frm_empty_msg)) ? '<div class="frm_no_entries">' . FrmProFieldsHelper::get_default_value($display->frm_empty_msg, false, true, true) . '</div>' : ''; if (isset($message)) { // if an entry was deleted above, show a message $empty_msg = $message . $empty_msg; } $after_where = false; if ($user_id and !empty($user_id)) { $user_id = FrmProAppHelper::get_user_id_param($user_id); $uid_used = false; } if (isset($display->frm_where) && !empty($display->frm_where) && (!$entry || !$post || empty($auto_id))) { $display->frm_where = apply_filters('frm_custom_where_opt', $display->frm_where, array('display' => $display, 'entry' => $entry)); $continue = false; foreach ($display->frm_where as $where_key => $where_opt) { $where_val = isset($display->frm_where_val[$where_key]) ? $display->frm_where_val[$where_key] : ''; if (preg_match("/\\[(get|get-(.?))\\b(.*?)(?:(\\/))?\\]/s", $where_val)) { $where_val = FrmProFieldsHelper::get_default_value($where_val, false, true, true); //if this param doesn't exist, then don't include it if ($where_val == '') { if (!$after_where) { $continue = true; } continue; } } else { $where_val = FrmProFieldsHelper::get_default_value($where_val, false, true, true); } $continue = false; if ($where_val == 'current_user') { if ($user_id and is_numeric($user_id)) { $where_val = $user_id; $uid_used = true; } else { $where_val = get_current_user_id(); } } $where_val = do_shortcode($where_val); if (in_array($where_opt, array('id', 'item_key', 'post_id')) && !is_array($where_val) && strpos($where_val, ',')) { $where_val = explode(',', $where_val); } if (is_array($where_val) and !empty($where_val)) { $new_where = '('; if (strpos($display->frm_where_is[$where_key], 'LIKE') !== false) { foreach ($where_val as $w) { if ($new_where != '(') { $new_where .= ','; } $new_where .= $wpdb->prepare('%s', '%' . FrmAppHelper::esc_like($w) . '%'); unset($w); } } else { foreach ($where_val as $w) { if ($new_where != '(') { $new_where .= ','; } $new_where .= $wpdb->prepare('%s', $w); unset($w); } } $new_where .= ')'; $where_val = $new_where; unset($new_where); if (strpos($display->frm_where_is[$where_key], '!') === false && strpos($display->frm_where_is[$where_key], 'not') === false) { $display->frm_where_is[$where_key] = ' in '; } else { $display->frm_where_is[$where_key] = ' not in '; } } if (is_numeric($where_opt)) { $filter_opts = apply_filters('frm_display_filter_opt', array('where_opt' => $where_opt, 'where_is' => $display->frm_where_is[$where_key], 'where_val' => $where_val, 'form_id' => $display->frm_form_id, 'form_posts' => $form_posts, 'after_where' => $after_where, 'display' => $display, 'drafts' => $is_draft)); $entry_ids = FrmProAppHelper::filter_where($entry_ids, $filter_opts); unset($filter_opts); $after_where = true; $continue = false; if (empty($entry_ids)) { break; } } else { if ($where_opt == 'created_at' or $where_opt == 'updated_at') { if ($where_val == 'NOW') { $where_val = current_time('mysql', 1); } if (strpos($display->frm_where_is[$where_key], 'LIKE') === false) { $where_val = date('Y-m-d H:i:s', strtotime($where_val)); } $where .= $wpdb->prepare(" and it.{$where_opt} " . $display->frm_where_is[$where_key] . "%s", ''); if (strpos($display->frm_where_is[$where_key], 'in')) { $where .= " {$where_val}"; } else { if (strpos($display->frm_where_is[$where_key], 'LIKE') !== false) { $where .= $wpdb->prepare(" %s", '%' . FrmAppHelper::esc_like($where_val) . '%'); } else { $where .= $wpdb->prepare(" %s", $where_val); } } $continue = true; } else { if (in_array($where_opt, array('id', 'item_key', 'post_id'))) { $where .= " and it.{$where_opt} " . $display->frm_where_is[$where_key]; if (strpos($display->frm_where_is[$where_key], 'in')) { $where .= " {$where_val}"; } else { $where .= $wpdb->prepare(" %s", $where_val); } $continue = true; } } } } if (!$continue and empty($entry_ids)) { if ($display->frm_insert_loc == 'after') { $content .= $empty_msg; } else { if ($display->frm_insert_loc == 'before') { $content = $empty_msg . $content; } else { if ($filter) { $empty_msg = apply_filters('the_content', $empty_msg); } if ($post->post_type == 'frm_display' and in_the_loop()) { $content = ''; } $content .= $empty_msg; } } return $content; } } if ($user_id && is_numeric($user_id) && !$uid_used) { $where .= $wpdb->prepare(" AND it.user_id=%d", $user_id); } $s = FrmAppHelper::get_param('frm_search', false); if ($s) { $new_ids = FrmProEntriesHelper::get_search_ids($s, $display->frm_form_id); if ($after_where and isset($entry_ids) and !empty($entry_ids)) { $entry_ids = array_intersect($new_ids, $entry_ids); } else { $entry_ids = $new_ids; } if (empty($entry_ids)) { if ($post->post_type == 'frm_display' and in_the_loop()) { $content = ''; } return $content . ' ' . $empty_msg; } } if (isset($entry_ids) && !empty($entry_ids)) { $where .= ' and it.id in (' . implode(',', array_filter($entry_ids, 'is_numeric')) . ')'; } if ($entry_id) { $entry_id_array = explode(',', $entry_id); //Get IDs (if there are any) $numeric_entry_ids = array_filter($entry_id_array, 'is_numeric'); //If there are entry keys, use esc_sql if (empty($numeric_entry_ids)) { $entry_id_array = array_filter($entry_id_array, 'esc_sql'); } $where .= !empty($numeric_entry_ids) ? " and it.id in ('" . implode("','", $numeric_entry_ids) . "')" : " and it.item_key in ('" . implode("','", $entry_id_array) . "')"; } if ($drafts != 'both') { $where .= $wpdb->prepare(' AND is_draft=%d', $is_draft); } unset($is_draft); if ($show == 'one') { $limit = ' LIMIT 1'; } else { if (isset($_GET['frm_cat']) and isset($_GET['frm_cat_id'])) { //Get fields with specified field value 'frm_cat' = field key/id, 'frm_cat_id' = order position of selected option global $frm_field; if ($cat_field = $frm_field->getOne($_GET['frm_cat'])) { $categories = maybe_unserialize($cat_field->options); if (isset($categories[$_GET['frm_cat_id']])) { $cat_entry_ids = $frm_entry_meta->getEntryIds(array('meta_value' => $categories[$_GET['frm_cat_id']], 'fi.field_key' => $_GET['frm_cat'])); if ($cat_entry_ids) { $where .= " and it.id in (" . implode(',', $cat_entry_ids) . ")"; } else { $where .= " and it.id=0"; } } } } } if (!empty($limit) and is_numeric($limit)) { $display->frm_limit = (int) $limit; } if (is_numeric($display->frm_limit)) { $num_limit = (int) $display->frm_limit; $limit = ' LIMIT ' . $display->frm_limit; } if (!empty($order_by)) { $display->frm_order_by = explode(',', $order_by); $order_by = ''; } if (!empty($order)) { $display->frm_order = explode(',', $order); } unset($order); if (!empty($page_size) && is_numeric($page_size)) { $display->frm_page_size = (int) $page_size; } // if limit is lower than page size, ignore the page size if (isset($num_limit) && $display->frm_page_size > $num_limit) { $display->frm_page_size = ''; } if (isset($display->frm_page_size) and is_numeric($display->frm_page_size)) { $page_param = ($_GET and isset($_GET['frm-page-' . $display->ID])) ? 'frm-page-' . $display->ID : 'frm-page'; $current_page = (int) FrmAppHelper::get_param($page_param, 1); $record_where = $where == $wpdb->prepare('it.form_id=%d', $display->frm_form_id) ? $display->frm_form_id : $where; $record_count = $frm_entry->getRecordCount($record_where); if (isset($num_limit) and $record_count > (int) $num_limit) { $record_count = (int) $num_limit; } $page_count = $frm_entry->getPageCount($display->frm_page_size, $record_count); //Get a page of entries $entries = $frmpro_entry->get_view_page($current_page, $display->frm_page_size, $where, array('order_by_array' => $display->frm_order_by, 'order_array' => $display->frm_order, 'posts' => $form_posts)); $page_last_record = FrmAppHelper::getLastRecordNum($record_count, $current_page, $display->frm_page_size); $page_first_record = FrmAppHelper::getFirstRecordNum($record_count, $current_page, $display->frm_page_size); if ($page_count > 1) { $page_param = 'frm-page-' . $display->ID; $pagination = FrmProDisplaysController::get_pagination_file(FrmAppHelper::plugin_path() . '/pro/classes/views/displays/pagination.php', compact('current_page', 'record_count', 'page_count', 'page_last_record', 'page_first_record', 'page_param')); } } else { //Get all entries $entries = $frmpro_entry->get_view_results($where, array('order_by_array' => $display->frm_order_by, 'order_array' => $display->frm_order, 'limit' => $limit, 'posts' => $form_posts)); } $total_count = count($entries); $sc_atts = array(); if (isset($record_count)) { $sc_atts['record_count'] = $record_count; } else { $sc_atts['record_count'] = $total_count; } $display_content = ''; if (isset($message)) { // if an entry was deleted above, show a message $display_content .= $message; } if ($show == 'all') { $display_content .= isset($display->frm_before_content) ? $display->frm_before_content : ''; } if (!isset($entry_ids) || empty($entry_ids)) { $entry_ids = array_keys($entries); } $display_content = apply_filters('frm_before_display_content', $display_content, $display, $show, array('total_count' => $total_count, 'record_count' => $sc_atts['record_count'], 'entry_ids' => $entry_ids)); $filtered_content = apply_filters('frm_display_entries_content', $new_content, $entries, $shortcodes, $display, $show, $sc_atts); if ($filtered_content != $new_content) { $display_content .= $filtered_content; } else { $odd = 'odd'; $count = 0; if (!empty($entries)) { foreach ($entries as $entry) { $count++; //TODO: use the count with conditionals $display_content .= apply_filters('frm_display_entry_content', $new_content, $entry, $shortcodes, $display, $show, $odd, array('count' => $count, 'total_count' => $total_count, 'record_count' => $sc_atts['record_count'], 'pagination' => $pagination, 'entry_ids' => $entry_ids)); $odd = $odd == 'odd' ? 'even' : 'odd'; unset($entry); } unset($count); } else { if ($post->post_type == 'frm_display' and in_the_loop()) { $display_content = ''; } if (!isset($message) || FrmAppHelper::get_param('frm_action') != 'destroy') { $display_content .= $empty_msg; } } } if (isset($message)) { unset($message); } if ($show == 'all') { $display_content .= isset($display->frm_after_content) ? apply_filters('frm_after_content', $display->frm_after_content, $display, $show, array('total_count' => $total_count, 'record_count' => $sc_atts['record_count'], 'entry_ids' => $entry_ids)) : ''; } if (!isset($sc_atts)) { $sc_atts = array('record_count' => 0); } if (!isset($total_count)) { $total_count = 0; } $display_content .= apply_filters('frm_after_display_content', $pagination, $display, $show, array('total_count' => $total_count, 'record_count' => $sc_atts['record_count'], 'entry_ids' => $entry_ids)); unset($sc_atts); $display_content = FrmProFieldsHelper::get_default_value($display_content, false, true, true); if ($display->frm_insert_loc == 'after') { $content .= $display_content; } else { if ($display->frm_insert_loc == 'before') { $content = $display_content . $content; } else { if ($filter) { $display_content = apply_filters('the_content', $display_content); } $content = $display_content; } } return $content; }