/** * @return array */ public function get_row(&$counter = 0, $method = null) { if (!empty($this->row) && 0 < (int) $this->id && 'table' != $method) { return $this->row; } if (is_object($this->pod) && ('Pods' == get_class($this->pod) || 'Pod' == get_class($this->pod))) { $this->row = $this->pod->fetch(); } else { $this->row = false; if (!empty($this->data)) { if (empty($this->data_keys) || count($this->data) != count($this->data_keys)) { $this->data_keys = array_keys($this->data); } if (count($this->data) == $this->total && isset($this->data_keys[$counter]) && isset($this->data[$this->data_keys[$counter]])) { $this->row = $this->data[$this->data_keys[$counter]]; $counter++; } } if (false === $this->row && 0 < (int) $this->id && !empty($this->sql['table'])) { $this->pods_data->select(array('table' => $this->sql['table'], 'where' => '`' . $this->sql['field_id'] . '` = ' . (int) $this->id, 'limit' => 1)); $this->row = $this->pods_data->fetch(); } } return $this->row; }
/** * @return string */ public function migrate_relationships() { if (true === $this->check_progress(__FUNCTION__)) { return '1'; } $migration_limit = (int) apply_filters('pods_upgrade_item_limit', 1500); $migration_limit = max($migration_limit, 100); $last_id = (int) $this->check_progress(__FUNCTION__); $sql = "\n SELECT `r`.*, `p`.`tbl_row_id` AS `real_id`, `p`.`datatype`\n FROM `@wp_pod_rel` AS `r`\n LEFT JOIN `@wp_pod` AS `p` ON `p`.`id` = `r`.`pod_id`\n WHERE {$last_id} < `r`.`id`\n AND `r`.`pod_id` IS NOT NULL\n AND `r`.`field_id` IS NOT NULL\n AND `p`.`id` IS NOT NULL\n ORDER BY `r`.`id`\n LIMIT 0, {$migration_limit}\n "; $rel = pods_query($sql); $last_id = true; $pod_types = pods_query("SELECT `id`, `name` FROM `@wp_pod_types` ORDER BY `id`"); $types = array(); $x = 0; if (!empty($rel) && !empty($pod_types)) { foreach ($pod_types as $type) { $type->name = pods_clean_name($type->name); $types[$type->id] = $this->api->load_pod(array('name' => $type->name), false); if (empty($types[$type->id])) { return pods_error(sprintf(__('Pod <strong>%s</strong> not found, relationships cannot be migrated', 'pods'), $type->name)); } $pod_fields = pods_query("SELECT `id`, `name` FROM `@wp_pod_fields` WHERE `datatype` = {$type->id} ORDER BY `id`"); $types[$type->id]['old_fields'] = array(); foreach ($pod_fields as $field) { // Handle name changes if (in_array($field->name, array('created', 'modified', 'author'))) { $field->name .= '2'; } $types[$type->id]['old_fields'][$field->id] = $field->name; } } foreach ($rel as $r) { $r->pod_id = (int) $r->pod_id; if (!isset($types[$r->datatype]) || !isset($types[$r->datatype]['old_fields'][$r->field_id])) { continue; } if (!isset($types[$r->datatype]['fields'][$types[$r->datatype]['old_fields'][$r->field_id]])) { continue; } $field = $types[$r->datatype]['fields'][$types[$r->datatype]['old_fields'][$r->field_id]]; if (!in_array($field['type'], array('pick', 'file'))) { continue; } $pod_id = $types[$r->datatype]['id']; $field_id = $field['id']; $item_id = $r->real_id; $related_pod_id = 0; $related_field_id = 0; $related_item_id = $r->tbl_row_id; if ('pick' == $field['type']) { $old_sister_id = (int) pods_var('_pods_1x_sister_id', $field['options'], 0); if (0 < $old_sister_id) { $sql = "\n SELECT `f`.`id`, `f`.`name`, `t`.`name` AS `pod`\n FROM `@wp_pod_fields` AS `f`\n LEFT JOIN `@wp_pod_types` AS `t` ON `t`.`id` = `f`.`datatype`\n WHERE `f`.`id` = " . $old_sister_id . " AND `t`.`id` IS NOT NULL\n ORDER BY `f`.`id`\n LIMIT 1\n "; $old_field = pods_query($sql); if (empty($old_field)) { continue; } $old_field = $old_field[0]; $related_field = $this->api->load_field(array('name' => $old_field->name, 'pod' => $old_field->pod)); if (empty($related_field)) { continue; } $related_pod_id = $related_field['pod_id']; $related_field_id = $related_field['id']; } elseif ('pod' == $field['pick_object'] && 0 < strlen($field['pick_val'])) { $related_pod = $this->api->load_pod(array('name' => $field['pick_val']), false); if (empty($related_pod)) { continue; } $related_pod_id = $related_pod['id']; } } $r->id = (int) $r->id; $pod_id = (int) $pod_id; $field_id = (int) $field_id; $item_id = (int) $item_id; $related_pod_id = (int) $related_pod_id; $related_field_id = (int) $related_field_id; $related_item_id = (int) $related_item_id; $r->weight = (int) $r->weight; $table_data = array('id' => $r->id, 'pod_id' => $pod_id, 'field_id' => $field_id, 'item_id' => $item_id, 'related_pod_id' => $related_pod_id, 'related_field_id' => $related_field_id, 'related_item_id' => $related_item_id, 'weight' => $r->weight); $table_formats = array_fill(0, count($table_data), '%d'); $sql = PodsData::insert_on_duplicate("@wp_podsrel", $table_data, $table_formats); pods_query($sql); $last_id = $r->id; $x++; if (10 < $x) { $this->update_progress(__FUNCTION__, $last_id); $x = 0; } } } $this->update_progress(__FUNCTION__, $last_id); if ($migration_limit == count($rel)) { return '-2'; } else { return '1'; } }
/** * Fetch the current position in the loop (starting at 1) * * @see PodsData::position * * @return int Current row number (+1) * @since 2.3 */ public function position() { $this->do_hook('position'); return $this->data->position(); }
/** * @static * * Do a query on the database * * @param string|array $sql The SQL to execute * @param string $error Error to throw on problems * @param null $results_error (optional) * @param null $no_results_error (optional) * * @return array|bool|mixed|null|void Result of the query * * @since 2.0 */ public static function query($sql, $error = 'Database Error', $results_error = null, $no_results_error = null) { /** * @var $wpdb wpdb */ global $wpdb; if ($wpdb->show_errors) { self::$display_errors = true; } $display_errors = self::$display_errors; if (is_object($error)) { if (isset($error->display_errors) && false === $error->display_errors) { $display_errors = false; } $error = 'Database Error'; } elseif (is_bool($error)) { $display_errors = $error; if (false !== $error) { $error = 'Database Error'; } } $params = (object) array('sql' => $sql, 'error' => $error, 'results_error' => $results_error, 'no_results_error' => $no_results_error, 'display_errors' => $display_errors); // Handle Preparations of Values (sprintf format) if (is_array($sql)) { if (isset($sql[0]) && 1 < count($sql)) { if (2 == count($sql)) { if (!is_array($sql[1])) { $sql[1] = array($sql[1]); } $params->sql = self::prepare($sql[0], $sql[1]); } elseif (3 == count($sql)) { $params->sql = self::prepare($sql[0], array($sql[1], $sql[2])); } else { $params->sql = self::prepare($sql[0], array($sql[1], $sql[2], $sql[3])); } } else { $params = array_merge($params, $sql); } if (1 == pods_var('pods_debug_sql_all', 'get', 0) && pods_is_admin(array('pods'))) { echo '<textarea cols="100" rows="24">' . str_replace(array('@wp_users', '@wp_'), array($wpdb->users, $wpdb->prefix), $params->sql) . '</textarea>'; } } $params->sql = trim($params->sql); // Run Query $params->sql = self::do_hook('query', $params->sql, $params); $result = $wpdb->query($params->sql); $result = self::do_hook('query_result', $result, $params); if (false === $result && !empty($params->error) && !empty($wpdb->last_error)) { return pods_error("{$params->error}; SQL: {$params->sql}; Response: {$wpdb->last_error}", $params->display_errors); } if ('INSERT' == strtoupper(substr($params->sql, 0, 6)) || 'REPLACE' == strtoupper(substr($params->sql, 0, 7))) { $result = $wpdb->insert_id; } elseif (preg_match('/^[\\s\\r\\n\\(]*SELECT/', strtoupper($params->sql))) { $result = (array) $wpdb->last_result; if (!empty($result) && !empty($params->results_error)) { return pods_error($params->results_error, $params->display_errors); } elseif (empty($result) && !empty($params->no_results_error)) { return pods_error($params->no_results_error, $params->display_errors); } } return $result; }
/** * Include and Init the PodsData class * * @see PodsData * * @param string|\Pod $pod The pod object to load * @param int $id (optional) Id of the pod to fetch * @param bool $strict (optional) If true throw an error if the pod does not exist * @param bool $unique (optional) If true always return a unique class * * @return PodsData * * @since 2.0 */ function pods_data($pod = null, $id = null, $strict = true, $unique = true) { require_once PODS_DIR . 'classes/PodsData.php'; if ($unique && false !== $pod) { return new PodsData($pod, $id, $strict); } return PodsData::init($pod, $id, $strict); }