Skip to content

Commit

Permalink
Merge pull request #35 from s3rgiosan/feature/add-rest-endpoint-filters
Browse files Browse the repository at this point in the history
Additional filters on search REST endpoint
  • Loading branch information
cmmarslender committed Mar 29, 2019
2 parents d6d44c9 + d164e00 commit 7eb76dc
Show file tree
Hide file tree
Showing 9 changed files with 3,277 additions and 427 deletions.
724 changes: 363 additions & 361 deletions assets/js/content-connect.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion assets/js/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,9 @@
"object_type": this.activeRelationship.object_type,
"post_type": this.activeRelationship.post_type,
"search": this.searchText,
"paged": this.currentPage
"paged": this.currentPage,
"relationship_name": this.activeRelationship.name,
"current_post_id": this.activeRelationship.current_post_id,
} ).then( response => {
// success
var i, result;
Expand Down
2 changes: 1 addition & 1 deletion content-connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: WP Content Connect
* Plugin URI: https://github.com/10up/wp-content-connect
* Description: WordPress library that enables direct relationships for posts to posts and posts to users.
* Version: 1.4.0
* Version: 1.5.0
* Author: Chris Marslender
* Author URI:
* License: MIT
Expand Down
51 changes: 40 additions & 11 deletions includes/API/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function register_endpoint() {

public function localize_endpoints( $data ) {
$data['endpoints']['search'] = get_rest_url( get_current_blog_id(), 'content-connect/v1/search' );
$data['nonces']['search'] = wp_create_nonce( 'content-connect-search' );
$data['nonces']['search'] = wp_create_nonce( 'content-connect-search' );

return $data;
}
Expand Down Expand Up @@ -77,30 +77,47 @@ public function process_search( $request ) {

$search_text = sanitize_text_field( $request->get_param( 'search' ) );

$paged = intval( $request->get_param( 'paged' ) );
$search_args = array(
'paged' => intval( $request->get_param( 'paged' ) ),
'relationship_name' => sanitize_text_field( $request->get_param( 'relationship_name' ) ),
'current_post_id' => intval( $request->get_param( 'current_post_id' ) ),
);

switch( $object_type ) {
case 'user':
$results = $this->search_users( $search_text, array( 'paged' => $paged ) );
$results = $this->search_users( $search_text, $search_args );
break;
case 'post':
$results = $this->search_posts( $search_text, $final_post_types, array( 'paged' => $paged ) );
$results = $this->search_posts( $search_text, $final_post_types, $search_args );
break;
}

return $results;
}

public function search_users( $search_text, $args = array() ) {

$defaults = array(
'paged' => 1,
);

$args = wp_parse_args( $args, $defaults );

$query = new \WP_User_Query( array(
$query_args = array(
'search' => "*{$search_text}*",
'paged' => $args['paged'],
) );
'paged' => $args['paged'],
);

/**
* Filters the search users query args.
*
* @since 1.5.0
* @param array $query_args The \WP_Query args.
* @param array $args Optional. The search users args. Default empty.
* @return array
*/
$query_args = apply_filters( 'tenup_content_connect_search_users_query_args', $query_args, $args );
$query = new \WP_User_Query( $query_args );

// @todo pagination args
$results = array(
Expand All @@ -121,17 +138,29 @@ public function search_users( $search_text, $args = array() ) {
}

public function search_posts( $search_text, $post_types, $args = array() ) {

$defaults = array(
'paged' => 1,
);

$args = wp_parse_args( $args, $defaults );

$query = new \WP_Query( array(
$query_args = array(
'post_type' => $post_types,
's' => $search_text,
'paged' => $args['paged'],
) );
's' => $search_text,
'paged' => $args['paged'],
);

/**
* Filters the search posts query args.
*
* @since 1.5.0
* @param array $query_args The \WP_Query args.
* @param array $args Optional. The search posts args. Default empty.
* @return array
*/
$query_args = apply_filters( 'tenup_content_connect_search_posts_query_args', $query_args, $args );
$query = new \WP_Query( $query_args );

$results = array(
'prev_pages' => ( $args['paged'] > 1 ),
Expand Down
4 changes: 2 additions & 2 deletions includes/UI/MetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public function add_meta_boxes( $post_type, $post ) {
$relationships = apply_filters( 'tenup_content_connect_post_relationship_data', array(), $post );

$relationship_data = array(
'nonces' => array(
'nonces' => array(
'wp_rest' => wp_create_nonce( 'wp_rest' ),
),
'endpoints' => array(),
'endpoints' => array(),
'relationships' => $relationships,
);

Expand Down
17 changes: 9 additions & 8 deletions includes/UI/PostToPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ public function filter_data( $data, $post ) {
$registry = Plugin::instance()->get_registry();

$data[] = array(
'reltype' => 'post-to-post',
'object_type' => 'post', // The object type we'll be querying for in searches on the front end
'post_type' => $other_post_type, // The post type we'll be querying for in searches on the front end (so NOT the current post type, but the matching one in the relationship)
'relid' => $registry->get_relationship_key($this->relationship->from, $this->relationship->to, $this->relationship->name ),
'name' => $this->relationship->name,
'labels' => $this->labels,
'sortable' => $this->sortable,
'selected' => $final_posts,
'reltype' => 'post-to-post',
'object_type' => 'post', // The object type we'll be querying for in searches on the front end.
'post_type' => $other_post_type, // The post type we'll be querying for in searches on the front end (so NOT the current post type, but the matching one in the relationship).
'relid' => $registry->get_relationship_key( $this->relationship->from, $this->relationship->to, $this->relationship->name ),
'name' => $this->relationship->name,
'labels' => $this->labels,
'sortable' => $this->sortable,
'selected' => $final_posts,
'current_post_id' => $post->ID,
);

return $data;
Expand Down
15 changes: 8 additions & 7 deletions includes/UI/PostToUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ public function filter_data( $data, $post ) {
$registry = Plugin::instance()->get_registry();

$data[] = array(
'reltype' => 'post-to-user',
'object_type' => 'user', // The object type we'll be querying for in searches on the front end
'relid' => $registry->get_relationship_key( $this->relationship->post_type, 'user', $this->relationship->name ),
'name' => $this->relationship->name,
'labels' => $this->labels,
'sortable' => $this->sortable,
'selected' => $final_users,
'reltype' => 'post-to-user',
'object_type' => 'user', // The object type we'll be querying for in searches on the front end.
'relid' => $registry->get_relationship_key( $this->relationship->post_type, 'user', $this->relationship->name ),
'name' => $this->relationship->name,
'labels' => $this->labels,
'sortable' => $this->sortable,
'selected' => $final_users,
'current_post_id' => $post->ID,
);

return $data;
Expand Down
Loading

0 comments on commit 7eb76dc

Please sign in to comment.