to ); $this->columns = array_merge( array( 'delete' => $this->clear_connections_link() ), array( 'title' => $ptype_obj->labels->singular_name ), $this->fields ); } function connect() { $from = absint( $_POST['from'] ); $to = absint( $_POST['to'] ); if ( !$from || !$to ) die(-1); $args = array( $from, $to ); if ( $this->reversed ) $args = array_reverse( $args ); $p2p_id = false; if ( $this->prevent_duplicates ) { $p2p_ids = P2P_Connections::get( $args[0], $args[1] ); if ( !empty( $p2p_ids ) ) $p2p_id = $p2p_ids[0]; } if ( !$p2p_id ) $p2p_id = P2P_Connections::connect( $args[0], $args[1] ); $this->connection_row( $p2p_id, $to ); } function disconnect() { $p2p_id = absint( $_POST['p2p_id'] ); p2p_delete_connection( $p2p_id ); die(1); } function clear_connections() { $post_id = absint( $_POST['post_id'] ); p2p_disconnect( $post_id, $this->direction ); die(1); } function box( $post_id ) { $connected_ids = $this->get_connected_ids( $post_id ); $data_attr = array(); foreach ( array( 'box_id', 'direction', 'prevent_duplicates' ) as $key ) $data_attr[] = "data-$key='" . $this->$key . "'"; $data_attr = implode( ' ', $data_attr ); ?>
> columns as $key => $title ) { echo html( 'th', array( 'class' => "p2p-col-$key" ), $title ); } ?> $post_b ) { $this->connection_row( $p2p_id, $post_b ); } ?>
>

'; foreach ( array_keys( $this->columns ) as $key ) { switch ( $key ) { case 'title': $value = $this->column_title( $post_id ); break; case 'delete': $value = $this->column_delete( $p2p_id ); break; default: $value = html( 'input', array( 'type' => 'text', 'name' => "p2p_meta[$p2p_id][$key]", 'value' => p2p_get_meta( $p2p_id, $key, true ) ) ); } echo html( 'td', array( 'class' => "p2p-col-$key" ), $value ); } echo ''; } public function results_row( $post ) { echo ''; foreach ( array( 'add', 'title' ) as $key ) { $method = "column_$key"; echo html( 'td', array( 'class' => "p2p-col-$key" ), $this->$method( $post->ID ) ); } echo ''; } protected function column_title( $post_id ) { return html( 'a', array( 'href' => str_replace( '&', '&', get_edit_post_link( $post_id ) ), 'title' => get_post_type_object( get_post_type( $post_id ) )->labels->edit_item, ), get_post_field( 'post_title', $post_id ) ); } protected function column_add( $post_id ) { return html( 'a', array( 'data-post_id' => $post_id, 'href' => '#', 'title' => __( 'Create connection', 'posts-to-posts' ) ), __( 'Create connection', 'posts-to-posts' ) ); } protected function column_delete( $p2p_id ) { return html( 'a', array( 'data-p2p_id' => $p2p_id, 'href' => '#', 'title' => __( 'Delete connection', 'posts-to-posts' ) ), __( 'Delete connection', 'posts-to-posts' ) ); } protected function clear_connections_link() { return html( 'a', array( 'href' => '#', 'title' => __( 'Delete all connections', 'posts-to-posts' ) ), __( 'Delete all connections', 'posts-to-posts' ) ); } protected function get_connected_ids( $post_id ) { $connected_posts = p2p_get_connected( $post_id, $this->direction ); if ( empty( $connected_posts ) ) return array(); $args = array( 'post__in' => $connected_posts, 'post_type'=> $this->to, 'post_status' => 'any', 'nopaging' => true, 'suppress_filters' => false, ); $post_ids = scbUtil::array_pluck( get_posts($args), 'ID' ); return array_intersect( $connected_posts, $post_ids ); // to preserve p2p_id keys } function get_search_args( $args, $post_id ) { $args = array_merge( $args, array( 'post_type' => $this->to, 'post_status' => 'any', 'posts_per_page' => 5, 'suppress_filters' => false, 'update_post_term_cache' => false, 'update_post_meta_cache' => false ) ); if ( $this->prevent_duplicates ) $args['post__not_in'] = p2p_get_connected( $post_id, $this->direction ); return $args; } function get_recent_args( $post_id ) { $args = array( 'numberposts' => 10, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => $this->to, 'post_status' => 'publish', 'suppress_filters' => false //true ); if ( $this->prevent_duplicates ) $args['post__not_in'] = p2p_get_connected( $post_id, $this->direction ); return $args; } }