Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: child and related listings UI #58

Merged
merged 18 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 88 additions & 8 deletions includes/class-newspack-listings-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,20 @@ public static function register_routes() {
]
);

// GET listings taxonomy terms by name search term.
// GET listings parent terms.
register_rest_route(
'newspack-listings/v1',
'parents',
[
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ __CLASS__, 'get_parents' ],
'permission_callback' => '__return_true',
],
]
);

// GET listings child posts.
register_rest_route(
'newspack-listings/v1',
'children',
Expand All @@ -93,7 +106,31 @@ public static function register_routes() {
]
);

// Set listings taxonomy terms.
// Set listings parent posts.
register_rest_route(
'newspack-listings/v1',
'parents',
[
[
'methods' => \WP_REST_Server::EDITABLE,
'callback' => [ __CLASS__, 'set_parents' ],
'permission_callback' => [ __CLASS__, 'api_permissions_check' ],
'args' => [
'post_id' => [
'sanitize_callback' => 'absint',
],
'added' => [
'sanitize_callback' => [ __CLASS__, 'sanitize_array' ],
],
'removed' => [
'sanitize_callback' => [ __CLASS__, 'sanitize_array' ],
],
],
],
]
);

// Set listings child posts.
register_rest_route(
'newspack-listings/v1',
'children',
Expand All @@ -103,13 +140,13 @@ public static function register_routes() {
'callback' => [ __CLASS__, 'set_children' ],
'permission_callback' => [ __CLASS__, 'api_permissions_check' ],
'args' => [
'parent' => [
'post_id' => [
'sanitize_callback' => 'absint',
],
'children' => [
'added' => [
'sanitize_callback' => [ __CLASS__, 'sanitize_array' ],
],
'removed' => [
'removed' => [
'sanitize_callback' => [ __CLASS__, 'sanitize_array' ],
],
],
Expand Down Expand Up @@ -397,7 +434,7 @@ function( $post ) use ( $attributes, $fields, $is_amp, $next_page, $query ) {
}

/**
* Look up Listing taxonomy terms by name.
* Get all terms for a specific taxonomy.
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response.
Expand Down Expand Up @@ -429,6 +466,35 @@ function( $term ) {
return new \WP_REST_Response( [] );
}

/**
* Look up parent terms by post ID.
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response.
*/
public static function get_parents( $request ) {
$params = $request->get_params();
$parents = Taxonomies::get_parent_terms( $params );

if ( is_array( $parents ) ) {
return new \WP_REST_Response(
array_map(
function( $term ) {
return [
'value' => $term->term_id,
'label' => $term->name,
'post_type' => $term->taxonomy,
];
},
$parents
),
200
);
}

return new \WP_REST_Response( [] );
}

/**
* Look up child posts by post ID.
*
Expand All @@ -444,8 +510,9 @@ public static function get_children( $request ) {
array_map(
function( $post ) {
return [
'id' => $post->ID,
'name' => $post->post_title,
'value' => $post->ID,
'label' => $post->post_title,
'post_type' => $post->post_type,
];
},
$children
Expand All @@ -457,6 +524,19 @@ function( $post ) {
return new \WP_REST_Response( [] );
}

/**
* Apply shadow term for the parent post to the given post_id,
* and/or remove the shadow term from the given children to remove.
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response.
*/
public static function set_parents( $request ) {
$params = $request->get_params();
$response = Taxonomies::set_parent_posts( $params );
return new \WP_REST_Response( $response );
}

/**
* Apply shadow term for the parent post to the given children,
* and/or remove the shadow term from the given children to remove.
Expand Down
3 changes: 2 additions & 1 deletion includes/class-newspack-listings-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ public static function manage_editor_assets() {
'newspack-listings-editor',
'newspack_listings_data',
[
'post_type_label' => get_post_type_object( $post_type )->labels->singular_name,
'post_type' => $post_type,
'post_type_label' => get_post_type_object( $post_type )->labels->singular_name,
'post_type_slug' => array_search( $post_type, Core::NEWSPACK_LISTINGS_POST_TYPES ),
'post_types' => $post_types,
'taxonomies' => $taxonomies,
'currency' => function_exists( 'get_woocommerce_currency' ) ? get_woocommerce_currency() : __( 'USD', 'newspack-listings' ),
Expand Down
Loading