/** * Upgrades orders using the old item structure * @return void */ function appthemes_upgrade_item_addons() { // Get All Orders $posts = new WP_Query(array('post_type' => APPTHEMES_ORDER_PTYPE, 'nopaging' => true)); foreach ($posts->posts as $order) { $connected = new WP_Query(array('connected_type' => APPTHEMES_ORDER_CONNECTION, 'connected_from' => $order->ID)); // Get all items foreach ($connected->posts as $post) { $post_id = $post->ID; // Get all addons $meta = p2p_get_meta($post->p2p_id); // Don't upgrade new items if (isset($meta['type'])) { continue; } if (isset($meta['addon'])) { foreach ($meta['addon'] as $addon) { // Add an item for each addon $p2p_id = p2p_type(APPTHEMES_ORDER_CONNECTION)->connect($order->ID, $post_id); // Add meta data p2p_add_meta($p2p_id, 'type', $addon); p2p_add_meta($p2p_id, 'price', $meta[$addon][0]); } } // Add an item for the regular item $p2p_id = p2p_type(APPTHEMES_ORDER_CONNECTION)->connect($order->ID, $post_id); p2p_add_meta($p2p_id, 'type', 'regular'); // value of VA_ITEM_REGULAR, since upgrade is only Vantage-applicable p2p_add_meta($p2p_id, 'price', $meta['price'][0]); // Delete the old item p2p_delete_connection($post->p2p_id); } } }
/** * Create a connection. * * @param int $p2p_type A valid connection type. * @param array $args Connection information. * * @return bool|int False on failure, p2p_id on success. */ function p2p_create_connection($p2p_type, $args) { global $wpdb; extract(wp_parse_args($args, array('from' => false, 'to' => false, 'meta' => array())), EXTR_SKIP); $from = absint($from); $to = absint($to); if (!$from || !$to) { return false; } $wpdb->insert($wpdb->p2p, array('p2p_type' => $p2p_type, 'p2p_from' => $from, 'p2p_to' => $to)); $p2p_id = $wpdb->insert_id; foreach ($meta as $key => $value) { p2p_add_meta($p2p_id, $key, $value); } return $p2p_id; }
/** * Create a connection. * * @param int $p2p_type A valid connection type. * @param array $args Connection information. * * @return bool|int False on failure, p2p_id on success. */ function p2p_create_connection($p2p_type, $args) { global $wpdb; extract(wp_parse_args($args, array('direction' => 'from', 'from' => false, 'to' => false, 'meta' => array())), EXTR_SKIP); list($from) = _p2p_normalize($from); list($to) = _p2p_normalize($to); if (!$from || !$to) { return false; } $args = array($from, $to); if ('to' == $direction) { $args = array_reverse($args); } $wpdb->insert($wpdb->p2p, array('p2p_type' => $p2p_type, 'p2p_from' => $args[0], 'p2p_to' => $args[1])); $p2p_id = $wpdb->insert_id; foreach ($meta as $key => $value) { p2p_add_meta($p2p_id, $key, $value); } do_action('p2p_created_connection', $p2p_id); return $p2p_id; }
/** * Connect two items. * * @param mixed The first end of the connection. * @param mixed The second end of the connection. * @param array Additional information about the connection. * * @return int|object p2p_id or WP_Error on failure */ public function connect($from_arg, $to_arg, $meta = array()) { $from = $this->recognize($from_arg, 'current'); if (!$from) { return new WP_Error('first_parameter', 'Invalid first parameter.'); } $to = $this->recognize($to_arg, 'opposite'); if (!$to) { return new WP_Error('second_parameter', 'Invalid second parameter.'); } if (!$this->self_connections && $from->get_id() == $to->get_id()) { return new WP_Error('self_connection', 'Connection between an element and itself is not allowed.'); } if (!$this->duplicate_connections && $this->get_p2p_id($from, $to)) { return new WP_Error('duplicate_connection', 'Duplicate connections are not allowed.'); } if ('one' == $this->get('opposite', 'cardinality')) { if ($this->has_connections($from)) { return new WP_Error('cardinality_opposite', 'Cardinality problem (opposite).'); } } if ('one' == $this->get('current', 'cardinality')) { if ($this->flip_direction()->has_connections($to)) { return new WP_Error('cardinality_current', 'Cardinality problem (current).'); } } $p2p_id = $this->create_connection(array('from' => $from, 'to' => $to, 'meta' => array_merge($meta, $this->data))); // Store additional default values // Don't add default p2p meta if meta was passed to ->connect() method $new_meta = array_merge($meta, $this->data); foreach ($this->fields as $key => $args) { // (array) null == array() if (!isset($new_meta[$key])) { foreach ((array) $this->get_default($args, $p2p_id) as $default_value) { p2p_add_meta($p2p_id, $key, $default_value); } } } return $p2p_id; }
private function add_addon($connection_id, $addon_id, $price) { p2p_add_meta($connection_id, 'addon', $addon_id); p2p_add_meta($connection_id, $addon_id, $addon_price); return array('addon_id' => $addon_id, 'price' => $price); }
/** * Adds an item to the order. * * @param string $type A string representing the type of item being added * @param int $price The price of the item * @param int $post_id (optional) The post that this item affects * @param bool $unique (optional) Is the item unique per order * * @return bool A boolean True if the item has been added, False otherwise */ public function add_item($type, $price, $post_id = 0, $unique = false) { if (empty($post_id)) { $post_id = $this->get_id(); } if (!is_numeric($post_id)) { trigger_error('Post ID must be an integer', E_USER_WARNING); return false; } if (!is_numeric($price)) { trigger_error('Price must be numeric', E_USER_WARNING); return false; } if (!is_string($type) && !is_int($type)) { trigger_error('Item Type must be a string or integer', E_USER_WARNING); return false; } if ($unique) { $this->remove_item($type); } $p2p_id = p2p_type(APPTHEMES_ORDER_CONNECTION)->connect($this->id, $post_id); if (!is_int($p2p_id)) { return false; } $this->items[] = array('type' => $type, 'price' => (double) $price, 'post_id' => $post_id, 'post' => get_post($post_id), 'unique_id' => $p2p_id); p2p_add_meta($p2p_id, 'type', $type); p2p_add_meta($p2p_id, 'price', (double) $price); $this->refresh_total(); return true; }
/** * Collect metadata from all boxes. */ static function save_post($post_id, $post) { if ('revision' == $post->post_type || defined('DOING_AJAX')) { return; } // Custom fields if (isset($_POST['p2p_types'])) { foreach ($_POST['p2p_types'] as $p2p_type) { $ctype = p2p_type($p2p_type); if (!$ctype) { continue; } foreach ($ctype->get_connections($post_id) as $p2p_id => $item_id) { $data = scbForms::get_value(array('p2p_meta', $p2p_id), $_POST, array()); foreach (self::$box_args[$p2p_type]->fields as $key => $field_args) { if ('checkbox' == $field_args['type']) { $new_values = scbForms::get_value($key, $data, array()); $old_values = p2p_get_meta($p2p_id, $key); foreach (array_diff($new_values, $old_values) as $value) { p2p_add_meta($p2p_id, $key, $value); } foreach (array_diff($old_values, $new_values) as $value) { p2p_delete_meta($p2p_id, $key, $value); } } else { p2p_update_meta($p2p_id, $key, $data[$key]); } } } } } // Ordering if (isset($_POST['p2p_order'])) { foreach ($_POST['p2p_order'] as $key => $list) { foreach ($list as $i => $p2p_id) { p2p_update_meta($p2p_id, $key, $i); } } } }
/** * AJAX Handler for adding a new step * * @since 1.0.0 * @return void */ function badgeos_add_step_ajax_handler() { // Create a new Step post and grab it's ID $step_id = wp_insert_post(array('post_type' => 'step', 'post_status' => 'publish')); // Output the edit step html to insert into the Steps metabox badgeos_steps_ui_html($step_id, $_POST['achievement_id']); // Grab the post object for our Badge $achievement = get_post($_POST['achievement_id']); // Create the P2P connection from the step to the badge $p2p_id = p2p_create_connection('step-to-' . $achievement->post_type, array('from' => $step_id, 'to' => $_POST['achievement_id'], 'meta' => array('date' => current_time('mysql')))); // Add relevant meta to our P2P connection p2p_add_meta($p2p_id, 'order', '0'); // Die here, because it's AJAX die; }
function attach_domain_by_url($domain_url) { //get the domain $domain = new foolic_domain($domain_url); if ($domain->ID > 0) { $p2p_id = p2p_type(foolic_post_relationships::LICENSEKEY_TO_DOMAINS)->get_p2p_id($this->ID, $domain->ID); if (!$p2p_id) { $p2p_response = p2p_create_connection(foolic_post_relationships::LICENSEKEY_TO_DOMAINS, array('from' => $this->ID, 'to' => $domain->ID, 'meta' => array('date_connected' => current_time('mysql'), 'attached' => '1'))); if ($p2p_response !== false) { //make sure domains are not cached $this->_domains = false; $this->_attached_domains = false; $this->process_domains(); } } else { //we have an existing connection p2p_add_meta($p2p_id, 'attached', '1', true); //make sure domains are not cached $this->_domains = false; $this->_attached_domains = false; $this->process_domains(); } return $domain; } return false; }