/** * */ public function install($_blog_id = null) { /** * @var $wpdb WPDB */ global $wpdb; // Switch DB table prefixes if (null !== $_blog_id && $_blog_id != $wpdb->blogid) { switch_to_blog(pods_absint($_blog_id)); } else { $_blog_id = null; } $pods_version = get_option('pods_version'); do_action('pods_install', PODS_VERSION, $pods_version, $_blog_id); if (!pods_tableless() && false !== apply_filters('pods_install_run', null, PODS_VERSION, $pods_version, $_blog_id) && !isset($_GET['pods_bypass_install'])) { $sql = file_get_contents(PODS_DIR . 'sql/dump.sql'); $sql = apply_filters('pods_install_sql', $sql, PODS_VERSION, $pods_version, $_blog_id); $charset_collate = 'DEFAULT CHARSET utf8'; if (!empty($wpdb->charset)) { $charset_collate = "DEFAULT CHARSET {$wpdb->charset}"; } if (!empty($wpdb->collate)) { $charset_collate .= " COLLATE {$wpdb->collate}"; } if ('DEFAULT CHARSET utf8' != $charset_collate) { $sql = str_replace('DEFAULT CHARSET utf8', $charset_collate, $sql); } $sql = explode(";\n", str_replace(array("\r", 'wp_'), array("\n", $wpdb->prefix), $sql)); for ($i = 0, $z = count($sql); $i < $z; $i++) { $query = trim($sql[$i]); if (empty($query)) { continue; } pods_query($query, 'Cannot setup SQL tables'); } } do_action('pods_install_post', PODS_VERSION, $pods_version, $_blog_id); }
/** * Import data from an array or a CSV file. * * @param mixed $import_data PHP associative array or CSV input * @param bool $numeric_mode Use IDs instead of the name field when matching * @param string $format Format of import data, options are php or csv * * @return array IDs of imported items * * @since 1.7.1 * @todo This needs some love and use of table_info etc for relationships */ public function import($import_data, $numeric_mode = false, $format = null) { /** * @var $wpdb wpdb */ global $wpdb; if (null === $format && null !== $this->format) { $format = $this->format; } if ('csv' == $format && !is_array($import_data)) { $data = pods_migrate('sv', ',')->parse($import_data); $import_data = $data['items']; } pods_query("SET NAMES utf8"); pods_query("SET CHARACTER SET utf8"); // Loop through the array of items $ids = array(); // Test to see if it's an array of arrays if (!is_array(@current($import_data))) { $import_data = array($import_data); } $pod = $this->load_pod(array('name' => $this->pod)); if (false === $pod) { return pods_error(__('Pod not found', 'pods'), $this); } $fields = array_merge($pod['fields'], $pod['object_fields']); $simple_tableless_objects = PodsForm::simple_tableless_objects(); foreach ($import_data as $key => $data_row) { $data = array(); // Loop through each field (use $fields so only valid fields get parsed) foreach ($fields as $field_name => $field_data) { if (!isset($data_row[$field_name]) && !isset($data_row[$field_data['label']])) { continue; } $field_id = $field_data['id']; $type = $field_data['type']; $pick_object = isset($field_data['pick_object']) ? $field_data['pick_object'] : ''; $pick_val = isset($field_data['pick_val']) ? $field_data['pick_val'] : ''; if (isset($data_row[$field_name])) { $field_value = $data_row[$field_name]; } else { $field_value = $data_row[$field_data['label']]; } if (null !== $field_value && false !== $field_value && '' !== $field_value) { if ('pick' == $type || in_array($type, PodsForm::file_field_types())) { $field_values = is_array($field_value) ? $field_value : array($field_value); $pick_values = array(); foreach ($field_values as $pick_value) { if (in_array($type, PodsForm::file_field_types()) || 'media' == $pick_object) { $where = "`guid` = '" . pods_sanitize($pick_value) . "'"; if (0 < pods_absint($pick_value) && false !== $numeric_mode) { $where = "`ID` = " . pods_absint($pick_value); } $result = pods_query("SELECT `ID` AS `id` FROM `{$wpdb->posts}` WHERE `post_type` = 'attachment' AND {$where} ORDER BY `ID`", $this); if (!empty($result)) { $pick_values[] = $result[0]->id; } } elseif ('pick' == $type) { $related_pod = false; if ('pod' == $pick_object) { $related_pod = $this->load_pod(array('name' => $pick_val, 'table_info' => true), false); } if (empty($related_pod)) { $related_pod = array('id' => 0, 'type' => $pick_object); } if (in_array('taxonomy', array($pick_object, $related_pod['type']))) { $where = "`t`.`name` = '" . pods_sanitize($pick_value) . "'"; if (0 < pods_absint($pick_value) && false !== $numeric_mode) { $where = "`tt`.`term_id` = " . pods_absint($pick_value); } $result = pods_query("SELECT `t`.`term_id` AS `id` FROM `{$wpdb->term_taxonomy}` AS `tt` LEFT JOIN `{$wpdb->terms}` AS `t` ON `t`.`term_id` = `tt`.`term_id` WHERE `taxonomy` = '{$pick_val}' AND {$where} ORDER BY `t`.`term_id`", $this); if (!empty($result)) { $pick_values[] = $result[0]->id; } } elseif (in_array('post_type', array($pick_object, $related_pod['type'])) || in_array('media', array($pick_object, $related_pod['type']))) { $where = "`post_title` = '" . pods_sanitize($pick_value) . "'"; if (0 < pods_absint($pick_value) && false !== $numeric_mode) { $where = "`ID` = " . pods_absint($pick_value); } $result = pods_query("SELECT `ID` AS `id` FROM `{$wpdb->posts}` WHERE `post_type` = '{$pick_val}' AND {$where} ORDER BY `ID`", $this); if (!empty($result)) { $pick_values[] = $result[0]->id; } } elseif (in_array('user', array($pick_object, $related_pod['type']))) { $where = "`user_login` = '" . pods_sanitize($pick_value) . "'"; if (0 < pods_absint($pick_value) && false !== $numeric_mode) { $where = "`ID` = " . pods_absint($pick_value); } $result = pods_query("SELECT `ID` AS `id` FROM `{$wpdb->users}` WHERE {$where} ORDER BY `ID`", $this); if (!empty($result)) { $pick_values[] = $result[0]->id; } } elseif (in_array('comment', array($pick_object, $related_pod['type']))) { $where = "`comment_ID` = " . pods_absint($pick_value); $result = pods_query("SELECT `comment_ID` AS `id` FROM `{$wpdb->comments}` WHERE {$where} ORDER BY `ID`", $this); if (!empty($result)) { $pick_values[] = $result[0]->id; } } elseif (in_array($pick_object, $simple_tableless_objects)) { $pick_values[] = $pick_value; } elseif (!empty($related_pod['id'])) { $where = "`" . $related_pod['field_index'] . "` = '" . pods_sanitize($pick_value) . "'"; if (0 < pods_absint($pick_value) && false !== $numeric_mode) { $where = "`" . $related_pod['field_id'] . "` = " . pods_absint($pick_value); } $result = pods_query("SELECT `" . $related_pod['field_id'] . "` AS `id` FROM `" . $related_pod['table'] . "` WHERE {$where} ORDER BY `" . $related_pod['field_id'] . "`", $this); if (!empty($result)) { $pick_values[] = $result[0]->id; } } } } $field_value = implode(',', $pick_values); } $data[$field_name] = $field_value; } } if (!empty($data)) { $params = array('pod' => $this->pod, 'data' => $data); $ids[] = $this->save_pod_item($params); } } return $ids; }
/** * @param null $id * * @return bool|mixed */ public function delete_bulk() { $this->do_hook('pre_delete_bulk'); if (1 != pods_var('deleted_bulk', 'get', 0)) { $ids = $this->bulk; $success = false; if (!empty($ids)) { $ids = (array) $ids; foreach ($ids as $id) { $id = pods_absint($id); if (empty($id)) { continue; } if ($callback = $this->callback('delete', $id)) { $check = $callback; } elseif (is_object($this->pod)) { $check = $this->pod->delete($id); } else { $check = $this->pods_data->delete($this->sql['table'], array($this->sql['field_id'] => $id)); } if ($check) { $success = true; } } } if ($success) { pods_redirect(pods_query_arg(array('action_bulk' => 'delete', 'deleted_bulk' => 1), array('page', 'lang', 'action', 'id'))); } else { $this->error(sprintf(__("<strong>Error:</strong> %s has not been deleted.", 'pods'), $this->item)); } } else { $this->message(sprintf(__("<strong>Deleted:</strong> %s have been deleted.", 'pods'), $this->items)); unset($_GET['deleted_bulk']); } $this->action_bulk = false; unset($_GET['action_bulk']); $this->do_hook('post_delete_bulk'); $this->manage(); }
/** * Display HTML for all datatype fields * * @deprecated deprecated since 2.0 */ public function showform($id = null, $public_fields = null, $label = 'Save changes') { pods_deprecated('Pods::showform', '2.0'); $public_columns =& $public_fields; $pod = $this->obj->pod; $pod_id = $this->obj->pod_id; $this->obj->type_counter = array(); if (!empty($public_fields)) { $attributes = array(); foreach ($public_fields as $key => $value) { if (is_array($public_fields[$key])) { $attributes[$key] = $value; } else { $attributes[$value] = array(); } } } $fields = $this->obj->fields; // Re-order the fields if a public form if (!empty($attributes)) { $fields = array(); foreach ($attributes as $key => $value) { if (isset($this->obj->fields[$key])) { $fields[$key] = $this->obj->fields[$key]; } } } do_action('pods_showform_pre', $pod_id, $public_fields, $label, $this); foreach ($fields as $key => $field) { if (!is_array($field) || in_array($key, array('created', 'modified'))) { continue; } // Pass options so they can be manipulated via form $field = array_merge($field['options'], $field); // Replace field attributes with public form attributes if (!empty($attributes) && is_array($attributes[$key])) { $field = array_merge($field, $attributes[$key]); } // Replace the input helper name with the helper code if (!empty($field['input_helper'])) { $helper = $this->obj->api->load_helper(array('name' => $field['input_helper'])); $field['input_helper'] = ''; if (!empty($helper)) { $field['input_helper'] = $helper['code']; } } if (empty($field['label'])) { $field['label'] = ucwords($key); } if (1 == $field['required']) { $field['label'] .= ' <span class="red">*</span>'; } if (!empty($field['pick_val'])) { $selected_ids = array(); $pick_object = $field['pick_object']; $pick_val = $field['pick_val']; if ('pod' == $pick_object) { $pick_pod = $this->obj->api->load_pod(array('name' => $pick_val)); $pick_object = $pick_pod['type']; $pick_val = $pick_pod['name']; } $pick_table = $pick_join = $pick_where = ''; $pick_field_id = 'id'; $pick_field_name = 'name'; switch ($pick_object) { case 'pod': $pick_table = "@wp_pods_{$pick_val}"; $pick_field_id = 'id'; $pick_field_name = 'name'; break; case 'post_type': $pick_table = '@wp_posts'; $pick_field_id = 'ID'; $pick_field_name = 'post_title'; $pick_where = "t.`post_type` = '{$pick_val}'"; break; case 'taxonomy': $pick_table = '@wp_terms'; $pick_field_id = 'term_id'; $pick_field_name = 'name'; $pick_join = "`@wp_term_taxonomy` AS tx ON tx.`term_id` = t.`term_id"; $pick_where = "tx.`taxonomy` = '{$pick_val}' AND tx.`taxonomy` IS NOT NULL"; break; case 'user': $pick_table = '@wp_users'; $pick_field_id = 'ID'; $pick_field_name = 'user_login'; break; case 'comment': $pick_table = '@wp_comments'; $pick_field_id = 'comment_ID'; $pick_field_name = 'comment_date'; $pick_where = "t.`comment_type` = '{$pick_val}'"; break; case 'table': $pick_table = "{$pick_val}"; $pick_field_id = 'id'; $pick_field_name = 'name'; break; } $sql = "SELECT `related_item_id` FROM `@wp_podsrel` WHERE `item_id` = %d AND `field_id` = %d"; $sql = array($sql, array($id, $field['id'])); $result = pods_query($sql, $this); foreach ($result as $row) { $selected_ids[] = $row->related_item_id; } // Use default values for public forms if (empty($selected_ids) && !empty($field['default'])) { $default_ids = $field['default']; if (!is_array($field['default'])) { $default_ids = explode(',', $default_ids); } foreach ($default_ids as $default_id) { $default_id = pods_absint($default_id); if (0 < $default_id) { $selected_ids[] = $default_id; } } } // If the PICK field is unique, get values already chosen $exclude = false; if (1 == $field['unique']) { $unique_where = empty($id) ? '' : " AND `item_id` != %d"; $sql = "SELECT `related_item_id` FROM `@wp_podsrel` WHERE `field_id` = %d {$unique_where}"; $sql = array($sql, array($field['id'])); if (!empty($id)) { $sql[1][] = $id; } $result = pods_query($sql, $this); if (!empty($result)) { $exclude = array(); foreach ($result as $row) { $exclude[] = (int) $row->related_item_id; } $exclude = implode(',', $exclude); } } if (!empty($field['options']['pick_filter'])) { $pick_where .= ' AND ' . $field['options']['pick_filter']; } $params = array('exclude' => $exclude, 'selected_ids' => $selected_ids, 'table' => $pick_table, 'field_id' => $pick_field_id, 'field_name' => $pick_field_name, 'join' => $pick_join, 'orderby' => $field['options']['pick_orderby'], 'where' => $pick_where); $this->obj->row[$key] = $this->get_dropdown_values($params); } else { // Set a default value if no value is entered if (!isset($this->obj->row[$key]) || (null === $this->obj->row[$key] || false === $this->obj->row[$key])) { if (!empty($field['default'])) { $this->obj->row[$key] = $field['default']; } else { $this->obj->row[$key] = null; } } } $this->obj->build_field_html($field); } $uri_hash = wp_hash($_SERVER['REQUEST_URI']); $save_button_atts = array('type' => 'button', 'class' => 'button btn_save', 'value' => $label, 'onclick' => "saveForm(1)"); $save_button_atts = apply_filters('pods_showform_save_button_atts', $save_button_atts, $this); $atts = ''; foreach ($save_button_atts as $att => $value) { $atts .= ' ' . esc_attr($att) . '="' . esc_attr($value) . '"'; } $save_button = '<input ' . $atts . '/>'; ?> <div> <input type="hidden" class="form num id" value="<?php echo $id; ?> " /> <input type="hidden" class="form txt pod" value="<?php echo $pod; ?> " /> <input type="hidden" class="form txt pod_id" value="<?php echo $pod_id; ?> " /> <input type="hidden" class="form txt form_count" value="1" /> <input type="hidden" class="form txt token" value="<?php echo pods_generate_key($pod, $uri_hash, $public_fields, 1); ?> " /> <input type="hidden" class="form txt uri_hash" value="<?php echo $uri_hash; ?> " /> <?php echo apply_filters('pods_showform_save_button', $save_button, $save_button_atts, $this); ?> </div> <?php do_action('pods_showform_post', $pod_id, $public_fields, $label, $this); }
/** * Reset the current data * * @param int $row Row number to reset to * * @return mixed * * @since 2.0 */ public function reset($row = null) { $row = pods_absint($row); $this->row = false; if (isset($this->data[$row])) { $this->row = get_object_vars($this->data[$row]); } if (empty($row)) { $this->row_number = -1; } else { $this->row_number = $row - 1; } return $this->row; }
/** * @param null $_blog_id */ public function reset($_blog_id = null) { global $wpdb; // Switch DB table prefixes if (null !== $_blog_id && $_blog_id != $wpdb->blogid) { switch_to_blog(pods_absint($_blog_id)); } else { $_blog_id = null; } $api = pods_api(); $pods = $api->load_pods(array('names_ids' => true)); foreach ($pods as $pod_id => $pod_label) { $api->delete_pod(array('id' => $pod_id)); } $templates = $api->load_templates(); foreach ($templates as $template) { $api->delete_template(array('id' => $template['id'])); } $pages = $api->load_pages(); foreach ($pages as $page) { $api->delete_page(array('id' => $page['id'])); } $helpers = $api->load_helpers(); foreach ($helpers as $helper) { $api->delete_helper(array('id' => $helper['id'])); } $tables = $wpdb->get_results("SHOW TABLES LIKE '{$wpdb->prefix}pods%'", ARRAY_N); if (!empty($tables)) { foreach ($tables as $table) { $table = $table[0]; pods_query("DROP TABLE `{$table}`", false); } } // Remove any orphans $wpdb->query("\n DELETE `p`, `pm`\n FROM `{$wpdb->posts}` AS `p`\n LEFT JOIN `{$wpdb->postmeta}` AS `pm`\n ON `pm`.`post_id` = `p`.`ID`\n WHERE\n `p`.`post_type` LIKE '_pods_%'\n "); delete_option('pods_framework_version'); delete_option('pods_framework_db_version'); delete_option('pods_framework_upgrade_2_0'); delete_option('pods_framework_upgraded_1_x'); // @todo Make sure all entries are being cleaned and do something about the pods_framework_upgrade_{version} dynamic entries created by PodsUpgrade delete_option('pods_framework_upgrade_2_0_0'); delete_option('pods_framework_upgrade_2_0_sister_ids'); delete_option('pods_framework_version_last'); delete_option('pods_component_settings'); $api->cache_flush_pods(); pods_transient_clear('pods_flush_rewrites'); self::$version = ''; // Restore DB table prefix (if switched) if (null !== $_blog_id) { restore_current_blog(); } }
/** * Constructor - Pods Framework core * * @param string $pod The pod name * @param mixed $id (optional) The ID or slug, to load a single record; Provide array of $params to run 'find' * * @return \Pods * * @license http://www.gnu.org/licenses/gpl-2.0.html * @since 1.0.0 * @link http://pods.io/docs/pods/ */ public function __construct($pod = null, $id = null) { if (null === $pod) { $queried_object = get_queried_object(); if ($queried_object) { $id_lookup = true; // Post Type Singular if (isset($queried_object->post_type)) { $pod = $queried_object->post_type; } elseif (isset($queried_object->taxonomy)) { $pod = $queried_object->taxonomy; } elseif (isset($queried_object->user_login)) { $pod = 'user'; } elseif (isset($queried_object->public) && isset($queried_object->name)) { $pod = $queried_object->name; $id_lookup = false; } if (null === $id && $id_lookup) { $id = get_queried_object_id(); } } } $this->api = pods_api($pod); $this->api->display_errors =& $this->display_errors; $this->data = pods_data($this->api, $id, false); PodsData::$display_errors =& $this->display_errors; // Set up page variable if (pods_strict(false)) { $this->page = 1; $this->pagination = false; $this->search = false; } else { // Get the page variable $this->page = pods_var($this->page_var, 'get'); $this->page = empty($this->page) ? 1 : max(pods_absint($this->page), 1); } // Set default pagination handling to on/off if (defined('PODS_GLOBAL_POD_PAGINATION')) { if (!PODS_GLOBAL_POD_PAGINATION) { $this->page = 1; $this->pagination = false; } else { $this->pagination = true; } } // Set default search to on/off if (defined('PODS_GLOBAL_POD_SEARCH')) { if (PODS_GLOBAL_POD_SEARCH) { $this->search = true; } else { $this->search = false; } } // Set default search mode $allowed_search_modes = array('int', 'text', 'text_like'); if (defined('PODS_GLOBAL_POD_SEARCH_MODE') && in_array(PODS_GLOBAL_POD_SEARCH_MODE, $allowed_search_modes)) { $this->search_mode = PODS_GLOBAL_POD_SEARCH_MODE; } // Sync Settings $this->data->page =& $this->page; $this->data->limit =& $this->limit; $this->data->pagination =& $this->pagination; $this->data->search =& $this->search; $this->data->search_mode =& $this->search_mode; // Sync Pod Data $this->api->pod_data =& $this->data->pod_data; $this->pod_data =& $this->api->pod_data; $this->api->pod_id =& $this->data->pod_id; $this->pod_id =& $this->api->pod_id; $this->datatype_id =& $this->pod_id; $this->api->pod =& $this->data->pod; $this->pod =& $this->api->pod; $this->datatype =& $this->pod; $this->api->fields =& $this->data->fields; $this->fields =& $this->api->fields; $this->detail_page =& $this->data->detail_page; $this->id =& $this->data->id; $this->row =& $this->data->row; $this->rows =& $this->data->data; $this->row_number =& $this->data->row_number; $this->sql =& $this->data->sql; if (is_array($id) || is_object($id)) { $this->find($id); } }
/** * @param null $id * * @return bool|mixed */ public function delete_bulk() { $this->do_hook('pre_delete_bulk'); if (1 != pods_var('deleted_bulk', 'get', 0)) { $ids = $this->bulk; $success = false; if (!empty($ids)) { $ids = (array) $ids; foreach ($ids as $id) { $id = pods_absint($id); if (empty($id)) { continue; } if (isset($this->actions_custom['delete']) && is_callable($this->actions_custom['delete'])) { $check = call_user_func_array($this->actions_custom['delete'], array($id, &$this)); if (false !== $check) { $check = true; } } elseif (is_object($this->pod)) { $check = $this->pod->delete($id); } else { $check = $this->pods_data->delete($this->table, array($this->data->field_id => $id)); } if ($check) { $success = true; } } } if ($success) { pods_redirect(pods_var_update(array('action_bulk' => 'delete', 'deleted_bulk' => 1), array('page', 'lang', 'action', 'id'))); } else { $this->error(__("<strong>Error:</strong> {$this->item} has not been deleted.", 'pods')); } } else { $this->message(__("<strong>Deleted:</strong> {$this->items} have been deleted.", 'pods')); unset($_GET['deleted_bulk']); } $this->action_bulk = false; unset($_GET['action_bulk']); $this->do_hook('post_delete_bulk'); $this->manage(); }