function mini_archive_post2post_relationships($relationships, $post_type) { $p2p_relations = P2P_Connection_Type_Factory::get_all_instances(); foreach ($p2p_relations as $p2p) { $add = array(); if ($post_type == "user") { foreach ($p2p->object as $side => $obj) { if ($obj == $post_type) { $add[$side] = true; } } } foreach ($p2p->side as $side => $obj) { if (isset($obj->post_type) && is_array($obj->post_type)) { foreach ($obj->post_type as $pt) { if ($pt == $post_type) { $add[$side] = true; } } } } foreach ($add as $side => $val) { $other_side = "from"; if ($side == "from") { $other_side = "to"; } $relationships[] = (object) array("name" => $p2p->name, "label" => $p2p->title[$side], "type" => "post2post", "direction" => $other_side); } } return $relationships; }
function deleted_post($post_id) { foreach (P2P_Connection_Type_Factory::get_all_instances() as $p2p_type => $ctype) { foreach (array('from', 'to') as $direction) { if ('post' == $ctype->object[$direction]) { p2p_delete_connections($p2p_type, array($direction => $post_id)); } } } }
static function deleted_object($object_id) { $object_type = str_replace('deleted_', '', current_filter()); foreach (P2P_Connection_Type_Factory::get_all_instances() as $p2p_type => $ctype) { foreach (array('from', 'to') as $direction) { if ($object_type == $ctype->object[$direction]) { p2p_delete_connections($p2p_type, array($direction => $object_id)); } } } }
function form($instance) { if (empty($instance)) { $instance = $this->defaults; } $ctypes = array(); foreach (P2P_Connection_Type_Factory::get_all_instances() as $p2p_type => $ctype) { $ctypes[$p2p_type] = $ctype->get_desc(); } echo html('p', $this->input(array('type' => 'text', 'name' => 'title', 'desc' => __('Title:', P2P_TEXTDOMAIN)), $instance)); echo html('p', $this->input(array('type' => 'select', 'name' => 'ctype', 'values' => $ctypes, 'desc' => __('Connection type:', P2P_TEXTDOMAIN), 'extra' => "style='width: 100%'"), $instance)); echo html('p', __('Connection listing:', P2P_TEXTDOMAIN), '<br>', $this->input(array('type' => 'radio', 'name' => 'listing', 'values' => array('connected' => __('connected', P2P_TEXTDOMAIN), 'related' => __('related', P2P_TEXTDOMAIN))), $instance)); }
function form($instance) { if (empty($instance)) { $instance = $this->defaults; } $ctypes = array(); foreach (P2P_Connection_Type_Factory::get_all_instances() as $p2p_type => $ctype) { if (!$ctype instanceof P2P_Connection_Type) { continue; } $ctypes[$p2p_type] = self::ctype_label($ctype); } echo html('p', $this->input(array('type' => 'select', 'name' => 'ctype', 'values' => $ctypes, 'desc' => __('Connection type:', P2P_TEXTDOMAIN)), $instance)); echo html('p', __('Connection listing:', P2P_TEXTDOMAIN), '<br>', $this->input(array('type' => 'radio', 'name' => 'listing', 'values' => array('connected' => __('connected', P2P_TEXTDOMAIN), 'related' => __('related', P2P_TEXTDOMAIN))), $instance)); }
function p2pdc_list_relationships($post_type) { $relationships = array(); $p2p_relations = P2P_Connection_Type_Factory::get_all_instances(); foreach ($p2p_relations as $p2p) { $add = array(); if ($post_type == "user") { foreach ($p2p->object as $side => $obj) { if ($obj == $post_type) { $add[$side] = true; } } } foreach ($p2p->side as $side => $obj) { if (isset($obj->query_vars['post_type']) && is_array($obj->query_vars['post_type'])) { foreach ($obj->query_vars['post_type'] as $pt) { if ($pt == $post_type) { $add[$side] = true; } } } } foreach ($add as $side => $val) { $other_side = "from"; if ($side == "from") { $other_side = "to"; } if (isset($p2p->side[$other_side]->query_vars['post_type'])) { $other_type = $p2p->side[$other_side]->query_vars['post_type']; if (is_array($other_type) && count($other_type) > 0) { $other_type = $other_type[0]; } } else { $other_type = 'user'; } $fields = array(); foreach ($p2p->fields as $key => $field) { $fields[] = $key; } $relationships[] = (object) array("name" => $p2p->name, "direction" => $other_side, "post_type" => $other_type, "fields" => $fields); } } return $relationships; }
/** * Generate the Mappings settings page with support of translations **/ public function plugin_options() { if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.')); } if (!is_plugin_active('posts-to-posts/posts-to-posts.php')) { echo __('In order for this plugin to work you need to install and activate the plugin <a href="https://wordpress.org/plugins/posts-to-posts/">Posts 2 Posts</a>.'); wp_die(); } // Now display the settings editing screen echo '<div class="wrap">'; // header echo "<h1>" . __('LH Relationships Mappings', 'lh-relationships-menu') . "</h1>"; // settings form // See if the user has posted us some information // If they did, this hidden field will be set to 'Y' if (isset($_POST[$this->hidden_field_name]) && $_POST[$this->hidden_field_name] == 'Y') { $options = $this->options; if ($_POST['add_namespace-prefix'] && $_POST['add_namespace-uri'] && $_POST['add_namespace-prefix'] != "" && $_POST['add_namespace-uri'] != "") { $prefix = sanitize_text_field($_POST['add_namespace-prefix']); $options['namespaces'][$prefix] = sanitize_text_field($_POST['add_namespace-uri']); } if ($_POST[$this->opt_name . "-add_p2p_type"] && $_POST[$this->opt_name . "-add_type_uri"] && $_POST[$this->opt_name . "-add_p2p_type"] != "" && $_POST[$this->opt_name . "-add_type_uri"] != "") { $type = sanitize_text_field($_POST[$this->opt_name . "-add_p2p_type"]); $options['type_mapping'][$type] = sanitize_text_field($_POST[$this->opt_name . "-add_type_uri"]); } if ($_POST[$this->opt_name . "-add_postmeta_key"] && $_POST[$this->opt_name . "-add_postmeta_uri"] && $_POST[$this->opt_name . "-add_postmeta_key"] != "" && $_POST[$this->opt_name . "-add_postmeta_uri"] != "") { $key = sanitize_text_field($_POST[$this->opt_name . "-add_postmeta_key"]); $options['postmeta_mapping'][$key] = sanitize_text_field($_POST[$this->opt_name . "-add_postmeta_uri"]); } if ($_POST[$this->opt_name . "-add_usermeta_key"] && $_POST[$this->opt_name . "-add_usermeta_uri"] && $_POST[$this->opt_name . "-add_usermeta_key"] != "" && $_POST[$this->opt_name . "-add_usermeta_uri"] != "") { $key = sanitize_text_field($_POST[$this->opt_name . "-add_usermeta_key"]); $options['usermeta_mapping'][$key] = sanitize_text_field($_POST[$this->opt_name . "-add_usermeta_uri"]); } if (update_site_option($this->opt_name, $options)) { $this->options = get_site_option($this->opt_name); ?> <div class="updated"><p><strong><?php _e('LH Relationships settings saved', 'lh-relationships-menu'); ?> </strong></p></div> <?php } } if (isset($_GET['lh_relationships-action']) && $_GET['lh_relationships-action'] == 'remove_option') { $options = $this->options; unset($options[$_GET['lh_relationships-option']][$_GET['lh_relationships-key']]); if (update_site_option($this->opt_name, $options)) { $this->options = get_site_option($this->opt_name); ?> <div class="updated"><p><strong><?php echo $_GET['lh_relationships-option'] . " " . $_GET['lh_relationships-key'] . " removed"; ?> </strong></p></div> <?php } } echo "<h3>" . __('Namespace Mappings', 'lh-relationships-menu') . "</h3>"; echo "<div><ol>"; foreach ($this->options['namespaces'] as $key => $value) { echo "<li>" . $key . ": " . $value . " <a href=\"" . add_query_arg('lh_relationships-action', 'remove_option', add_query_arg('lh_relationships-option', 'namespaces', add_query_arg('lh_relationships-key', $key))) . "\">remove</a></li>\n"; } echo "</ol></div>"; ?> <form name="lh_relationships-backend_form" method="post" action="<?php echo esc_url(remove_query_arg(array('lh_relationships-action', 'lh_relationships-option', 'lh_relationships-key'))); ?> "> <input type="hidden" name="<?php echo $this->hidden_field_name; ?> " value="Y" /> <strong><?php _e('Add Namespaces', 'lh-relationships-menu'); ?> </strong> <p> <label for="add_namespace-prefix"><?php _e("Prefix:", 'lh-relationships-menu'); ?> </label> <input type="text" name="add_namespace-prefix" id="add_namespace-prefix" size="6" /> <label for="add_namespace-uri"><?php _e("URI:", 'lh-relationships-menu'); ?> </label> <input type="url" name="add_namespace-uri" id="add_namespace-uri" size="40" /> </p> <?php echo "<h3>" . __('Post Relationship Mappings', 'lh-relationships-menu') . "</h3>"; echo "<div><ol>"; foreach ($this->options['type_mapping'] as $key => $value) { echo "<li>" . $key . "--> " . $value . " <a href=\"" . add_query_arg('lh_relationships-action', 'remove_option', add_query_arg('lh_relationships-option', 'type_mapping', add_query_arg('lh_relationships-key', $key))) . "\">" . _e('Remove', 'lh-relationships-menu') . "</a></li>\n"; } echo "</ol></div>"; ?> <strong><?php _e("Add Post Relationship Mappings", 'lh-relationships-menu'); ?> </strong> <p> <?php _e("Connection Type:", 'lh-relationships-menu'); ?> <select name="<?php echo $this->opt_name . "-add_p2p_type"; ?> " id="<?php echo $this->opt_name . "-add_p2p_type"; ?> " > <?php foreach (P2P_Connection_Type_Factory::get_all_instances() as $p2p_type => $ctype) { echo "<option value=\"" . $p2p_type . "\">" . $p2p_type . "</option>"; } echo "</select>"; _e("URI:", 'lh-relationships-menu'); ?> <input type="url" name="<?php echo $this->opt_name . "-add_type_uri"; ?> " id="<?php echo $this->opt_name . "-add_type_uri"; ?> " size="40" /> </p> <?php echo "<h3>" . __('Post Meta Mappings', 'lh-relationships-menu') . "</h3>"; echo "<div><ol>"; foreach ($this->options['postmeta_mapping'] as $key => $value) { echo "<li>" . $key . "--> " . $value . " <a href=\"" . add_query_arg('lh_relationships-action', 'remove_option', add_query_arg('lh_relationships-option', 'postmeta_mapping', add_query_arg('lh_relationships-key', $key))) . "\">" . _e('Remove', 'lh-relationships-menu') . "</a></li>\n"; } echo "</ol></div>"; ?> <strong><?php _e("Add Post Meta Mappings", 'lh-relationships-menu'); ?> </strong> <p> <?php _e("Post Meta Key:", 'lh-relationships-menu'); ?> <input type="text" name="<?php echo $this->opt_name . "-add_postmeta_key"; ?> " id="<?php echo $this->opt_name . "-add_postmeta_key"; ?> " size="10" /> <?php _e("URI:", 'lh-relationships-menu'); ?> <input type="text" name="<?php echo $this->opt_name . "-add_postmeta_uri"; ?> " id="<?php echo $this->opt_name . "-add_postmeta_uri"; ?> " size="40" /> </p> <?php echo "<h3>" . __('User Meta Mappings', 'lh-relationships-menu') . "</h3>"; echo "<div><ol>"; foreach ($this->options['usermeta_mapping'] as $key => $value) { echo "<li>" . $key . "--> " . $value . " <a href=\"" . add_query_arg('lh_relationships-action', 'remove_option', add_query_arg('lh_relationships-option', 'usermeta_mapping', add_query_arg('lh_relationships-key', $key))) . "\">" . _e('Remove', 'lh-relationships-menu') . "</a></li>\n"; } echo "</ol></div>"; ?> <strong><?php _e("Add User Meta Mappings", 'lh-relationships-menu'); ?> </strong> <p> <?php _e("User Meta Key:", 'lh-relationships-menu'); ?> <input type="text" name="<?php echo $this->opt_name . "-add_usermeta_key"; ?> " id="<?php echo $this->opt_name . "-add_usermeta_key"; ?> " size="10" /> <?php _e("URI:", 'lh-relationships-menu'); ?> <input type="text" name="<?php echo $this->opt_name . "-add_usermeta_uri"; ?> " id="<?php echo $this->opt_name . "-add_usermeta_uri"; ?> " size="40" /> </p> <input type="submit" name="submit" id="submit" value="<?php _e('Save', 'lh-relationships-menu'); ?> " /> </form> <?php print_r($this->list_types('user')); echo '</div>'; }
/** * Get a connection type. * * @param string $p2p_type * * @return bool|object False if connection type not found, P2P_Connection_Type instance on success. */ function p2p_type($p2p_type) { return P2P_Connection_Type_Factory::get_instance($p2p_type); }
private function get_dropdown($p2p_type) { $data = array('old_p2p_type' => $p2p_type, 'options' => array_keys(P2P_Connection_Type_Factory::get_all_instances()), 'button_text' => __('Go', P2P_TEXTDOMAIN)); return P2P_Mustache::render('connection-types-form', $data); }
/** * List registered connection types. * * @subcommand connection-types */ function connection_types() { foreach (P2P_Connection_Type_Factory::get_all_instances() as $p2p_type => $ctype) { WP_CLI::line($p2p_type); } }