protected function assertConnectionDoesntExist($ctype, $args) { $cb = function ($args) use($ctype) { return !p2p_connection_exists($ctype, $args); }; $desc = sprintf("'%s' connection doesn't exist", $ctype); $constraint = new P2P_Constraint($desc, $cb); self::assertThat($args, $constraint); }
/** * Get a sensible title for the current item (usually the arguments array for a column) * * @param array $item An array of arguments * @return string The item title */ protected function get_item_title(array $item) { if (isset($item['taxonomy'])) { if ($tax = get_taxonomy($item['taxonomy'])) { if (!empty($tax->exclusive)) { return $tax->labels->singular_name; } else { return $tax->labels->name; } } else { return $item['taxonomy']; } } else { if (isset($item['post_field'])) { return ucwords(trim(str_replace(array('post_', '_'), ' ', $item['post_field']))); } else { if (isset($item['meta_key'])) { return ucwords(trim(str_replace(array('_', '-'), ' ', $item['meta_key']))); } else { if (isset($item['connection']) and isset($item['value'])) { return ucwords(trim(str_replace(array('_', '-'), ' ', $item['value']))); } else { if (isset($item['connection'])) { if (function_exists('p2p_type') and p2p_connection_exists($item['connection'])) { $ctype = p2p_type($item['connection']); $other = 'from' == $ctype->direction_from_types('post', $this->cpt->post_type) ? 'to' : 'from'; return $ctype->side[$other]->get_title(); } else { return $item['connection']; } } } } } } }
/** * @param $post_id */ function save_meta_values($post_id, $post) { $meta_fields = $this->get_meta_fields(); if (isset($_POST['post_ID']) && !empty($_REQUEST['rtbiz_is_staff_member']) && 'yes' == $_REQUEST['rtbiz_is_staff_member']) { $users = rtbiz_get_wp_user_for_contact($_POST['post_ID']); if (empty($users[0])) { $primary_email = rtbiz_get_entity_meta($_POST['post_ID'], self::$primary_email_key, true); $userid = email_exists($primary_email); if (rtbiz_is_primary_email_unique($primary_email, $_POST['post_ID']) && !is_wp_error($userid) && !p2p_connection_exists($this->post_type . '_to_user', array('to' => $userid))) { rtbiz_connect_contact_to_user($_POST['post_ID'], $userid); } else { update_user_meta(get_current_user_id(), Rtbiz_Entity::$meta_key_prefix . 'wp_user_not_connected_' . $_POST['post_ID'], true); } } } if (isset($_POST['contact_meta'][self::$primary_email_key]) && empty($_POST['contact_meta'][self::$primary_email_key])) { update_user_meta(get_current_user_id(), Rtbiz_Entity::$meta_key_prefix . 'empty_primary_email_' . $_POST['post_ID'], true); } else { delete_user_meta(get_current_user_id(), Rtbiz_Entity::$meta_key_prefix . 'empty_primary_email_' . $_POST['post_ID']); } foreach ($meta_fields as $field) { if (isset($_POST['contact_meta'][$field['key']]) && !empty($_POST['contact_meta'][$field['key']])) { if ($field['key'] == self::$primary_email_key) { if (!rtbiz_is_primary_email_unique($_POST['contact_meta'][$field['key']], $_POST['post_ID'])) { update_user_meta(get_current_user_id(), Rtbiz_Entity::$meta_key_prefix . 'unique_primary_email_' . $_POST['post_ID'], true); continue; } else { delete_user_meta(get_current_user_id(), Rtbiz_Entity::$meta_key_prefix . 'unique_primary_email_' . $_POST['post_ID']); } } $contact_meta[$field['key']] = $_POST['contact_meta'][$field['key']]; if (isset($field['is_multiple']) && $field['is_multiple']) { $oldmeta = self::get_meta($post_id, $field['key']); foreach ($oldmeta as $ometa) { self::delete_meta($post_id, $field['key'], $ometa); } foreach ($contact_meta[$field['key']] as $nmeta) { if ('' == $nmeta) { continue; } self::add_meta($post_id, $field['key'], $nmeta); } } else { self::update_meta($post_id, $field['key'], $_POST['contact_meta'][$field['key']]); } } else { $oldmeta = self::get_meta($post_id, $field['key']); foreach ($oldmeta as $ometa) { self::delete_meta($post_id, $field['key'], $ometa); } } } Rtbiz_Contact_Profile_Access::save($post_id, null); parent::save_meta_values($post_id, $post); }
function rtbiz_is_our_employee($value, $module) { global $rtbiz_contact; if (is_numeric($value)) { $value = get_user_by('id', $value); } elseif (is_string($value)) { $value = get_user_by('email', $value); } elseif (!is_object($value)) { return false; } $isEmployee = p2p_connection_exists($rtbiz_contact->post_type . '_to_user', array('to' => $value->ID)); return $isEmployee && !empty($value) && user_can($value, rtbiz_get_access_role_cap($module, 'author')) ? true : false; }
/** * Check if a certain Posts 2 Posts connection exists. * * This is just a caching wrapper for `p2p_connection_exists()`, which performs a * database query on every call. * * @param string $connection A connection type. * @return bool Whether the connection exists. */ protected function p2p_connection_exists($connection) { if (!isset($this->connection_exists[$connection])) { $this->connection_exists[$connection] = p2p_connection_exists($connection); } return $this->connection_exists[$connection]; }
public function connect_post_to_entity($from_post_type, $to_post_type, $from = '', $to = '') { if (!p2p_connection_exists($from_post_type . '_to_' . $to_post_type, array('from' => $from, 'to' => $to))) { p2p_create_connection($from_post_type . '_to_' . $to_post_type, array('from' => $from, 'to' => $to, 'meta' => array('date' => current_time('mysql')))); } }
/** * Connect two items. * * @param int The first end of the connection. * @param int The second end of the connection. * @param array Additional information about the connection. * * @return int p2p_id */ public function connect($from, $to, $meta = array()) { if (!$this->get_current('side')->item_exists($from)) { return false; } if (!$this->get_opposite('side')->item_exists($to)) { return false; } $args = array($from, $to); if ('to' == $this->direction) { $args = array_reverse($args); } if ($this->accepts_single_connection()) { $to_check = 'any'; } elseif ($this->prevent_duplicates) { $to_check = $to; } else { $to_check = false; } if ($to_check && p2p_connection_exists($this->name, array('direction' => $this->direction, 'from' => $args[0], 'to' => $to_check))) { return false; } return p2p_create_connection($this->name, array('from' => $args[0], 'to' => $args[1], 'meta' => array_merge($meta, $this->data))); }