/** * This method allow to link a custom post type by passing his ID, the second param allow to put one or lot's of objects ID... * * @param integer $custom_id * @param array $object_ids * @param array $post_types * @param boolean $append (Optionnal) * @return void * @author Amaury Balmer */ function rpt_set_object_relation($custom_id = 0, $object_ids = array(), $post_types = array(), $append = true) { global $wpdb; // Object ID is valid ? $custom_id = (int) $custom_id; if ($custom_id == 0) { return false; } // No append ? replace ! delete before ! if ($append == false) { rpt_delete_object_relation($custom_id, $post_types); } // Always an array ? if (!is_array($object_ids)) { $object_ids = array((int) $object_ids); } // Cast values and make unique $object_ids = array_map('intval', $object_ids); $object_ids = array_unique($object_ids); // No valid ID ? if (empty($object_ids)) { return false; } // Loop for insert on DB ! foreach ((array) $object_ids as $object_id) { if ($object_id == 0 || $object_id == $custom_id) { continue; // No zero, no master/master ! } $wpdb->insert($wpdb->posts_relations, array('object_id_1' => $custom_id, 'object_id_2' => $object_id)); } return true; }
/** * Save relations when save object. * * @param string $post_ID * @param object $post * @return boolean * @author Amaury Balmer */ public static function save_post($post_ID = 0, $post = null) { // Don't do anything when autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return false; } // Save only when relation post type is called before. if (!isset($_POST['post-relation-post-types']) || $_POST['post-relation-post-types'] != 1) { return false; } // Get current post object if (!isset($post) || is_null($post)) { $post = get_post($post_ID); } // Take post type from arg ! $post_type = $post->post_type; // Prepare admin type for each relations box ! $current_options = get_option(RPT_OPTION); // All tag-style post taxonomies foreach ((array) $current_options as $current_post_type => $_post_types) { foreach ((array) $_post_types as $_post_type) { if ($_post_type != $post_type) { continue; } if (isset($_POST['action']) && !isset($_POST['relations'][$current_post_type])) { rpt_delete_object_relation($post_ID, array($current_post_type)); } elseif (isset($_POST['relations'][$current_post_type])) { // Secure datas if (is_array($_POST['relations'][$current_post_type])) { $_POST['relations'][$current_post_type] = array_map('intval', $_POST['relations'][$current_post_type]); $_POST['relations'][$current_post_type] = array_unique($_POST['relations'][$current_post_type]); } else { $_POST['relations'][$current_post_type] = (int) $_POST['relations'][$current_post_type]; } rpt_set_object_relation($post_ID, $_POST['relations'][$current_post_type], $current_post_type, false); } } } return true; }
/** * Hook call for delete relation of deleted post * * @param integer $post_id * @return void * @author Amaury Balmer */ public static function delete_post($post_id = 0) { $type = get_post_type($post_id); rpt_delete_object_relation((int) $post_id, array($type)); }
function save($values, $args) { if (isset($values['name'])) { $values = $values['name']; } if (empty($values)) { rpt_delete_object_relation($args['post_id'], array($this->post_type)); } // Secure datas if (is_array($values)) { $values = array_map('intval', $values); } else { $values = (int) $values; } rpt_set_object_relation($args['post_id'], $values, $this->post_type, false); return 1; }