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

[WIP] feat: harden post type usage and establish relationships between listings and posts/pages #43

Merged
merged 33 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d44cace
feat: make Business patterns applicable only to places
dkoo Feb 25, 2021
0a7a1cb
feat: create shadow taxonomy for places
dkoo Feb 25, 2021
c1f2c5e
Merge branch 'epic/phase-2' into feat/data-type-usage
dkoo Mar 19, 2021
c05c6c6
feat: add ability to associate Places with regular posts and pages
dkoo Mar 19, 2021
084dad5
Merge branch 'epic/phase-2' into feat/data-type-usage
dkoo Mar 30, 2021
87b3708
fix: remove default map markers from patterns
dkoo Mar 30, 2021
4d4d4b8
feat: new Price block for Marketplace listings
dkoo Mar 31, 2021
082cf5a
feat: add meta field with data synced from Price block
dkoo Mar 31, 2021
1177275
feat: second test pattern
dkoo Mar 31, 2021
54e3fde
fix: incorrect conditional for updating shadow terms (whoops)
dkoo Mar 31, 2021
5256dd2
feat: abstract shadow tax functions to make it easier to add more
dkoo Mar 31, 2021
023e37e
refactor: simplify setting formattedPrice attribute via useEffect
dkoo Mar 31, 2021
9c3f687
chore: fix PHP warning about passing variables
dkoo Mar 31, 2021
5c6bf0c
feat: shadow taxonomies for all CPTs; test automated related listings
dkoo Mar 31, 2021
caa1938
fix: avoid showing post on itself
dkoo Apr 5, 2021
f2d554c
fix: incorrect var assignment
dkoo Apr 5, 2021
621c497
fix: decode HTML entities before rendering currency options
dkoo Apr 5, 2021
a70feef
chore: fix function/class docs
dkoo Apr 5, 2021
9ae4141
fix: formatting an empty string results in NaN
dkoo Apr 5, 2021
d27b19a
fix: wrong post type being returned by util
dkoo Apr 5, 2021
a6f0195
fix: handle all listing post types for shadow taxonomies
dkoo Apr 5, 2021
a50068a
fix: don't show the shadow taxonomy for the post being edited
dkoo Apr 5, 2021
ce6aa70
feat: add new post button for shadow taxonomies
dkoo Apr 5, 2021
819dffe
chore: instantiate var as empty array, just in case
dkoo Apr 5, 2021
4f374be
fix: updating of terms, and delete orphan terms
dkoo Apr 8, 2021
15944a1
Merge branch 'epic/phase-2' into feat/data-type-usage
dkoo Apr 12, 2021
f2a88a2
fix: replace Material icon with WordPress icon
dkoo Apr 12, 2021
a1d033b
feat: custom UI for parent/child listings instead of WP taxonomy UI
dkoo Apr 16, 2021
a3978bd
chore: update function comment
dkoo Apr 22, 2021
df7843f
chore: remove commented-out code
dkoo Apr 22, 2021
527fe4d
fix: posts/pages child search fields; only show child UI if published
dkoo Apr 22, 2021
4b43a7c
fix: logical errors in validation method :facepalm:
dkoo Apr 23, 2021
a136170
chore: update function name and comments for clarity
dkoo Apr 28, 2021
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
107 changes: 107 additions & 0 deletions includes/class-newspack-listings-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use \Newspack_Listings\Newspack_Listings_Core as Core;
use \Newspack_Listings\Newspack_Listings_Blocks as Blocks;
use \Newspack_Listings\Newspack_Listings_Taxonomies as Taxonomies;
use \Newspack_Listings\Utils as Utils;

defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -78,6 +79,71 @@ public static function register_routes() {
],
]
);

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

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

/**
* Permission callback for authenticated requests.
*
* @return boolean if user can edit stuff.
*/
public static function api_permissions_check() {
if ( ! current_user_can( 'manage_options' ) ) {
return new \WP_Error(
'newspack_rest_forbidden',
esc_html__( 'You cannot use this resource.', 'newspack-listings' ),
[
'status' => 403,
]
);
}
return true;
}

/**
* Sanitize an array of text or number values.
*
* @param array $array Array of text or float values to be sanitized.
* @return array Sanitized array.
*/
public static function sanitize_array( $array ) {
return Utils\sanitize_array( $array );
}

/**
Expand Down Expand Up @@ -362,6 +428,47 @@ function( $term ) {

return new \WP_REST_Response( [] );
}

/**
* Look up child posts by post ID.
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response.
*/
public static function get_children( $request ) {
$params = $request->get_params();
$children = Taxonomies::get_child_posts( $params );

if ( is_array( $children ) ) {
return new \WP_REST_Response(
array_map(
function( $post ) {
return [
'id' => $post->ID,
'name' => $post->post_title,
];
},
$children
),
200
);
}

return new \WP_REST_Response( [] );
}

/**
* Apply shadow term for the parent post to the given children,
* 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_children( $request ) {
$params = $request->get_params();
$response = Taxonomies::set_child_posts( $params );
return new \WP_REST_Response( $response );
}
}

Newspack_Listings_Api::instance();
76 changes: 44 additions & 32 deletions includes/class-newspack-listings-block-patterns.php

Large diffs are not rendered by default.

24 changes: 20 additions & 4 deletions includes/class-newspack-listings-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Newspack_Listings;

use \Newspack_Listings\Newspack_Listings_Core as Core;
use \Newspack_Listings\Newspack_Listings_Taxonomies as Taxonomies;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -63,23 +64,38 @@ public static function manage_editor_assets() {
$total_count = 0;
$post_type = get_post_type();
$post_types = [];
$taxonomies = [];

foreach ( Core::NEWSPACK_LISTINGS_POST_TYPES as $label => $name ) {
$post_count = wp_count_posts( $name )->publish;
$total_count = $total_count + $post_count;
$post_types[ $label ] = [
foreach ( Core::NEWSPACK_LISTINGS_POST_TYPES as $slug => $name ) {
$post_count = wp_count_posts( $name )->publish;
$total_count = $total_count + $post_count;
$post_types[ $slug ] = [
'name' => $name,
'label' => get_post_type_object( Core::NEWSPACK_LISTINGS_POST_TYPES[ $slug ] )->labels->singular_name,
'show_in_inserter' => 0 < $post_count,
];
}

$shadow_taxonomy_config = Taxonomies::get_shadow_taxonomy_config();

foreach ( Taxonomies::NEWSPACK_LISTINGS_TAXONOMIES as $slug => $name ) {
$taxonomies[ $slug ] = [
'name' => $name,
'label' => get_post_type_object( Core::NEWSPACK_LISTINGS_POST_TYPES[ $slug ] )->labels->singular_name,
'post_types' => $shadow_taxonomy_config[ $slug ]['post_types'],
];
}

wp_localize_script(
'newspack-listings-editor',
'newspack_listings_data',
[
'post_type_label' => get_post_type_object( $post_type )->labels->singular_name,
'post_type' => $post_type,
'post_types' => $post_types,
'taxonomies' => $taxonomies,
'currency' => function_exists( 'get_woocommerce_currency' ) ? get_woocommerce_currency() : __( 'USD', 'newspack-listings' ),
'currencies' => function_exists( 'get_woocommerce_currencies' ) ? get_woocommerce_currencies() : [ 'USD' => __( 'United States (US) dollar', 'newspack-listings' ) ],

// If we don't have ANY listings that can be added to a list yet, alert the editor so we can show messaging.
'no_listings' => 0 === $total_count,
Expand Down
89 changes: 79 additions & 10 deletions includes/class-newspack-listings-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,19 @@ public static function register_post_types() {
'generic' => [
'labels' => [
'name' => _x( 'Generic Listings', 'post type general name', 'newspack-listings' ),
'singular_name' => _x( 'Listing', 'post type singular name', 'newspack-listings' ),
'singular_name' => _x( 'Generic Listing', 'post type singular name', 'newspack-listings' ),
'menu_name' => _x( 'Generic Listings', 'admin menu', 'newspack-listings' ),
'name_admin_bar' => _x( 'Listing', 'add new on admin bar', 'newspack-listings' ),
'name_admin_bar' => _x( 'Generic Listing', 'add new on admin bar', 'newspack-listings' ),
'add_new' => _x( 'Add New', 'popup', 'newspack-listings' ),
'add_new_item' => __( 'Add New Listing', 'newspack-listings' ),
'new_item' => __( 'New Listing', 'newspack-listings' ),
'edit_item' => __( 'Edit Listing', 'newspack-listings' ),
'view_item' => __( 'View Listing', 'newspack-listings' ),
'add_new_item' => __( 'Add New Generic Listing', 'newspack-listings' ),
'new_item' => __( 'New Generic Listing', 'newspack-listings' ),
'edit_item' => __( 'Edit Generic Listing', 'newspack-listings' ),
'view_item' => __( 'View Generic Listing', 'newspack-listings' ),
'all_items' => __( 'Generic Listings', 'newspack-listings' ),
'search_items' => __( 'Search Listings', 'newspack-listings' ),
'parent_item_colon' => __( 'Parent Listing:', 'newspack-listings' ),
'not_found' => __( 'No listings found.', 'newspack-listings' ),
'not_found_in_trash' => __( 'No listings found in Trash.', 'newspack-listings' ),
'search_items' => __( 'Search Generic Listings', 'newspack-listings' ),
'parent_item_colon' => __( 'Parent Generic Listing:', 'newspack-listings' ),
'not_found' => __( 'No generic listings found.', 'newspack-listings' ),
'not_found_in_trash' => __( 'No generic listings found in Trash.', 'newspack-listings' ),
],
'rewrite' => [ 'slug' => $prefix . '/' . $settings['newspack_listings_generic_slug'] ],
],
Expand Down Expand Up @@ -507,6 +507,29 @@ public static function get_meta_fields( $post_type = null, $field_names_only = f
},
],
],
'newspack_listings_price' => [
'post_types' => [
self::NEWSPACK_LISTINGS_POST_TYPES['marketplace'],
],
'label' => __( 'Price', 'newspack-listings' ),
'source' => [
'blockName' => 'newspack-listings/price',
'attr' => 'price',
'single' => true,
],
'settings' => [
'object_subtype' => $post_type,
'default' => '',
'description' => __( 'Price for this listing.', 'newspack-listings' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'single' => true,
'show_in_rest' => true,
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
},
],
],
'newspack_listings_hide_author' => [
'post_types' => [
self::NEWSPACK_LISTINGS_POST_TYPES['event'],
Expand All @@ -528,6 +551,52 @@ public static function get_meta_fields( $post_type = null, $field_names_only = f
},
],
],
'newspack_listings_hide_parents' => [
'post_types' => [
'page',
'post',
self::NEWSPACK_LISTINGS_POST_TYPES['event'],
self::NEWSPACK_LISTINGS_POST_TYPES['generic'],
self::NEWSPACK_LISTINGS_POST_TYPES['marketplace'],
self::NEWSPACK_LISTINGS_POST_TYPES['place'],
],
'label' => __( 'Hide parent listings', 'newspack-listings' ),
'settings' => [
'object_subtype' => $post_type,
'default' => boolval( Settings::get_settings( 'newspack_listings_hide_parents' ) ), // Configurable in plugin-wide settings.
'description' => __( 'Hide parent listings assigned to this post', 'newspack-listings' ),
'type' => 'boolean',
'sanitize_callback' => 'rest_sanitize_boolean',
'single' => true,
'show_in_rest' => true,
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
},
],
],
'newspack_listings_hide_children' => [
'post_types' => [
'page',
'post',
self::NEWSPACK_LISTINGS_POST_TYPES['event'],
self::NEWSPACK_LISTINGS_POST_TYPES['generic'],
self::NEWSPACK_LISTINGS_POST_TYPES['marketplace'],
self::NEWSPACK_LISTINGS_POST_TYPES['place'],
],
'label' => __( 'Hide child listings', 'newspack-listings' ),
'settings' => [
'object_subtype' => $post_type,
'default' => boolval( Settings::get_settings( 'newspack_listings_hide_children' ) ), // Configurable in plugin-wide settings.
'description' => __( 'Hide child listings assigned to this post', 'newspack-listings' ),
'type' => 'boolean',
'sanitize_callback' => 'rest_sanitize_boolean',
'single' => true,
'show_in_rest' => true,
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
},
],
],
];

// Return only the fields that are associated with the given $post_type.
Expand Down
14 changes: 14 additions & 0 deletions includes/class-newspack-listings-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ public static function get_default_settings() {
'type' => 'checkbox',
'value' => true,
],
[
'description' => __( 'This setting can be overridden per listing, post, or page.', 'newspack-listings' ),
'key' => 'newspack_listings_hide_parents',
'label' => __( 'Hide parent listings by default', 'newpack-listings' ),
'type' => 'checkbox',
'value' => false,
],
[
'description' => __( 'This setting can be overridden per listing, post, or page.', 'newspack-listings' ),
'key' => 'newspack_listings_hide_children',
'label' => __( 'Hide child listings by default', 'newpack-listings' ),
'type' => 'checkbox',
'value' => false,
],
];
}

Expand Down
Loading