public function delete_field($params, $table_operation = true)
{
/**
* @var $wpdb wpdb
*/
global $wpdb;
$tableless_field_types = PodsForm::tableless_field_types();
$simple_tableless_objects = PodsForm::simple_tableless_objects();
$params = (object) pods_sanitize($params);
if (!isset($params->pod)) {
$params->pod = '';
}
if (!isset($params->pod_id)) {
$params->pod_id = 0;
}
$pod = $params->pod;
$save_pod = false;
if (!is_array($pod)) {
$pod = $this->load_pod(array('name' => $pod, 'id' => $params->pod_id, 'table_info' => false));
} else {
$save_pod = true;
}
if (empty($pod)) {
return pods_error(__('Pod not found', 'pods'), $this);
}
$params->pod_id = $pod['id'];
$params->pod = $pod['name'];
if (!isset($params->name)) {
$params->name = '';
}
if (!isset($params->id)) {
$params->id = 0;
}
$field = $this->load_field(array('name' => $params->name, 'id' => $params->id, 'pod' => $params->pod, 'pod_id' => $params->pod_id));
if (false === $field) {
return pods_error(__('Field not found', 'pods'), $this);
}
$params->id = $field['id'];
$params->name = $field['name'];
$simple = 'pick' == $field['type'] && in_array(pods_var('pick_object', $field), $simple_tableless_objects);
$simple = (bool) $this->do_hook('tableless_custom', $simple, $field, $pod, $params);
if ($table_operation && 'table' == $pod['storage'] && (!in_array($field['type'], $tableless_field_types) || $simple)) {
pods_query("ALTER TABLE `@wp_pods_{$params->pod}` DROP COLUMN `{$params->name}`", false);
}
$success = wp_delete_post($params->id);
if (!$success) {
return pods_error(__('Field unable to be deleted', 'pods'), $this);
}
$wpdb->query($wpdb->prepare("DELETE pm FROM {$wpdb->postmeta} AS pm\n LEFT JOIN {$wpdb->posts} AS p\n ON p.post_type = '_pods_field' AND p.ID = pm.post_id\n WHERE p.ID IS NOT NULL AND pm.meta_key = 'sister_id' AND pm.meta_value = %d", $params->id));
if (!pods_tableless() && $table_operation) {
pods_query("DELETE FROM `@wp_podsrel` WHERE (`pod_id` = {$params->pod_id} AND `field_id` = {$params->id}) OR (`related_pod_id` = {$params->pod_id} AND `related_field_id` = {$params->id})", false);
}
// @todo Delete tableless relationship meta
if (true === $save_pod) {
$this->cache_flush_pods($pod);
}
return true;
}