public function delete_meta() { global $wpdb; // get a list of meta values bound to this field. $sql = "SELECT post_id, meta_key FROM $wpdb->postmeta WHERE meta_key LIKE '%:field_id' AND meta_value = '".$this->id."'"; $rows = $wpdb->get_results($sql); foreach ($rows as $row) { list($set_name, $field_name, $prop_name) = MPFT::parse_meta_key($row->meta_key); // note that we delete the properties too, but only count the main fields. $sql = "DELETE FROM $wpdb->postmeta WHERE post_id = ".$row->post_id." AND ( meta_key = '".MPFT::meta_key($set_name, $field_name)."' OR meta_key LIKE '".MPFT::meta_key($set_name, $field_name).":%' )"; $wpdb->query($sql); } }
protected function get_user_relations($for = array(), $type = "post", $object) { // build a reverse map of all of the IDs that are related to in related user metadata if (!isset($this->user_relations[$type])) { global $wpdb; $this->user_relations[$type] = array(); $this->user_relations_fields[$type] = array(); // get a list of all meta keys that are related user fields if ($type == "post") { $sql = "SELECT DISTINCT meta_key FROM $wpdb->postmeta WHERE meta_key LIKE '%:field_id' AND meta_value IN (SELECT id FROM ".MPU::table("fields")." WHERE `type` = 'related-user')"; } else if ($type == "user") { $sql = "SELECT DISTINCT meta_key FROM $wpdb->usermeta WHERE meta_key LIKE '%:field_id' AND meta_value IN (SELECT id FROM ".MPU::table("fields")." WHERE `type` = 'related-user')"; } else if ($type == "term") { $sql = "SELECT DISTINCT meta_key FROM {$this->termmeta()} WHERE meta_key LIKE '%:field_id' AND meta_value IN (SELECT id FROM ".MPU::table("fields")." WHERE `type` = 'related-user')"; } $fields = $wpdb->get_col($sql); if (count($fields)) { // run through the meta fields and remove the field id property $meta_keys = array(); foreach ($fields as $field) { list($set_name, $field_name, $prop_name) = MPFT::parse_meta_key($field); $meta_keys[] = "'".MPFT::meta_key($set_name, $field_name)."'"; } // now grab all meta values within these keys if ($type == "post") { $sql = "SELECT * FROM $wpdb->postmeta WHERE meta_key IN (".implode(",", $meta_keys).")"; $col = "post_id"; } else if ($type == "user") { $sql = "SELECT * FROM $wpdb->usermeta WHERE meta_key IN (".implode(",", $meta_keys).")"; $col = "user_id"; } else if ($type == "term") { $col = "term_id"; $sql = "SELECT * FROM {$this->termmeta()} WHERE meta_key IN (".implode(",", $meta_keys).")"; } $rows = $wpdb->get_results($sql); // now run through them all, and store the reverse relationships $pr = array(); $prf = array(); foreach ($rows as $row) { $ids = MPU::db_decode($row->meta_value); if (!is_array($ids) && $ids != "") { $ids = array($ids); } if (is_array($ids)) { foreach ($ids as $id) { if (!isset($pr[$id])) { $pr[(int)$id] = array(); } $pr[(int)$id][] = (int) $row->{$col}; } if (!isset($prf[$row->meta_key])) { $prf[$row->meta_key] = array(); } if (!isset($prf[$row->meta_key][$iid])) { $prf[$row->meta_key][$iid] = array(); } $prf[$row->meta_key][$iid][] = (int) $row->{$col}; } } // remove dupes foreach ($pr as $id => $ids) { $this->user_relations[$id] = array_unique($ids); } // remove dupes from the items stored by field foreach ($prf as $key => $rel) { foreach ($rel as $id => $ids) { if (!isset($this->user_relations_fields[$type][$key])) { $this->user_relations_fields[$type][$key] = array(); } $this->user_relations_fields[$type][$key][$id] = array_unique($ids); } } } } if (count($for)) { // collect the fields $to_merge = array(); foreach ($for as $key) { if (isset($this->user_relations_fields[$type][$key])) { $to_merge = $this->user_relations_fields[$type][$key]; } } return array_merge_recursive($to_merge); } else { return $this->user_relations[$type]; } }
public function reassign_posts($new_post_type_name, $field_sets_action = "keep") { global $wpdb; $post_type_name = $this->name; if ($new_post_type_name) { // get the list of post ids for the existing type, which we'll need for later $post_ids = $wpdb->get_col("SELECT post_id FROM $wpdb->posts WHERE post_type = '$post_type_name'"); // update the posts $wpdb->query("UPDATE $wpdb->posts SET post_type = '$new_post_type_name' WHERE post_type = '$post_type_name'"); if ($field_sets_action == "keep") { // update the link tables for this post type // need to check if there are any clashes for field sets named the same thing $sql = "SELECT id, name, visibility FROM ".MPU::table("field-sets")." WHERE type = 'p' AND ".MPM::visibility_rlike("post_types", $this->name); $post_type_sets = $wpdb->get_results($sql); $sql = "SELECT id, name, visibility FROM ".MPU::table("field-sets")." WHERE type = 'p' AND ".MPM::visibility_rlike("post_types", $this->name); $new_post_type_sets = $wpdb->get_results($sql); $clashes = array(); foreach ($post_type_sets as $post_type_set) { foreach ($new_post_type_sets as $new_post_type_set) { if ($post_type_set->name == $new_post_type_set->name) { // a set with this name already exists, so we'll rename this one to "_2" $sql = "UPDATE ".MPU::table("field-sets")." SET name = CONCAT(name, '_2'), singular_name = CONCAT(singular_name, '_2') WHERE id = ".$post_type_set->id; $wpdb->query($sql); // now we need to update the meta keys for any meta values that were bound to this set // now get the fields inside this clashing set $field_ids = $wpdb->get_col("SELECT id FROM ".MPU::table("fields")." WHERE field_set_id = ".$post_type_set->id); if (count($field_ids)) { // get a list of meta values bound to the fields in this clashing set. $sql = "SELECT post_id, meta_key FROM $wpdb->postmeta WHERE meta_key LIKE '%:field_id' AND meta_value IN (".implode(",", $field_ids).")"; $rows = $wpdb->get_results($sql); // this will need some serious testing! foreach ($rows as $row) { list($set_name, $field_name, $prop_name) = MPFT::parse_meta_key($row->meta_key); $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = REPLACE(meta_key, '".$set_name."', '".$set_name."_2') WHERE post_id = ".$row->post_id." AND ( meta_key = '".MPFT::meta_key($set_name, $field_name)."' OR meta_key LIKE '".MPFT::meta_key($set_name, $field_name).":%' ) "); } } } } } foreach ($post_type_sets as $set) { $visibility = MPU::db_decode($set->visibility); $visibility["post_types"] = $new_post_type_name; $sql = "UPDATE ".MPU::table("field-sets")." SET visibility = '".MPU::db_encode($visibility)."' WHERE id = ".$set->id; $wpdb->query($sql); } } else { $this->delete_field_sets(); } } }
function field($name, $set_index = 1, $object = NULL, $object_type = NULL) { global $wf; if (is_null($object)) { $object = $this->object(); } list($set_name, $field_name, $prop_name) = MPFT::parse_meta_key($name); $set = $this->set($set_name); $set_item = $set[$set_index]; return new MEOW_Field($name, $set_index, $object, $set_item); }
public function delete_meta() { global $wpdb; // get a list of field ids for this set $field_ids = $wpdb->get_col("SELECT id FROM ".MPU::table("fields")." WHERE field_set_id = ".$this->id); if (count($field_ids)) { // get a list of meta values bound to this field. $sql = "SELECT post_id, meta_key FROM $wpdb->postmeta WHERE meta_key LIKE '%:field_id' AND meta_value IN (".implode(",", $field_ids).")"; $rows = $wpdb->get_results($sql); foreach ($rows as $row) { list($set_name, $field_name, $prop_name) = MPFT::parse_meta_key($row->meta_key); $sql = "DELETE FROM $wpdb->postmeta WHERE post_id = ".$row->post_id." AND ( meta_key = '".MPFT::meta_key($set_name, $field_name)."' OR meta_key LIKE '".MPFT::meta_key($set_name, $field_name).":%' )"; $wpdb->query($sql); } } }