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: new Curated List block, block pattern, and map functionality #3

Merged
merged 29 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bdb56d9
feat: initial post type and block setup
dkoo Sep 11, 2020
8eb2322
feat: improvements to block structure and meta
dkoo Sep 15, 2020
305a3fa
fix: error after fetching post
dkoo Sep 15, 2020
23fe169
feat: handle meta programmatically, add permalink prefix
dkoo Sep 17, 2020
bb2d887
fix: pull request feedback; refactor post types config
dkoo Sep 21, 2020
52a6a92
chore: use permalink slugs defined in constant; spaces in JSON
dkoo Sep 21, 2020
1d726fb
feat: add Curated List block; remove Curated List CPT
dkoo Sep 21, 2020
d3cdb46
chore: update class docblocks, move blocks to newspack category
dkoo Sep 21, 2020
59dfd86
chore: remove unused block_categories filter
dkoo Sep 21, 2020
59210b5
fix: remove fse action
dkoo Sep 22, 2020
d29df56
Merge branch 'feat/initial-setup' into feat/curated-list-block
dkoo Sep 22, 2020
ae881a5
feat: listing InnerBlocks inherit parent Curated List attributes
dkoo Sep 23, 2020
e0db05a
Merge branch 'master' into feat/curated-list-block
dkoo Sep 23, 2020
d879a7d
fix: wrong selector in sidebar JS
dkoo Sep 23, 2020
1f8adeb
feat: change curated list to dynamic render
dkoo Sep 24, 2020
1ac0cb4
feat: add "business" block pattern for Marketplace listings
dkoo Sep 24, 2020
2b52361
feat: map functionality via Jetpack Maps block
dkoo Sep 28, 2020
347d9ed
feat: check for jetpack/maps block before using
dkoo Sep 28, 2020
dac8564
feat: add Jetpack Contact Info block to Business Listing block pattern
dkoo Sep 28, 2020
49d93c8
feat: sync block content to post meta
dkoo Oct 1, 2020
9a0dafa
fix: use listing post meta to get locations in curated list block
dkoo Oct 2, 2020
b712515
chore: phpdoc types should be lowercase
dkoo Oct 2, 2020
28cf7bb
feat: don't register Curated List block in listings CPTs
dkoo Oct 2, 2020
8cd23d0
fix: css fixes for compatibility with core block editor (sans plugin)
dkoo Oct 5, 2020
231e4dc
fix: listing block parent should be list container, not curated list
dkoo Oct 5, 2020
917116b
fix: callable for settings fields
dkoo Oct 6, 2020
48d027c
fix: block patterns for core WP
dkoo Oct 15, 2020
bb32751
chore: remove redundant docblock comment
dkoo Oct 15, 2020
39d2912
fix: flush permalinks only after registering CPTs
dkoo Oct 15, 2020
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
43 changes: 26 additions & 17 deletions includes/class-newspack-listings-api.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php
/**
* Newspack Listings Core.
* Newspack Listings API.
*
* Registers custom post types and taxonomies.
* Custom API endpoints for Newspack Listings.
*
* @package Newspack_Listings
*/

namespace Newspack_Listings;

use \Newspack_Listings\Newspack_Listings_Core as Core;
use \Newspack_Listings\Utils as Utils;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -115,26 +116,34 @@ public static function get_items( $request ) {
return new \WP_REST_Response(
array_map(
function( $post ) use ( $fields ) {
$response = [
'id' => $post->ID,
'title' => $post->post_title,
];

// If $fields includes meta, get all Newspack Listings meta fields.
$post_meta = [];
// If $fields includes excerpt, get the post excerpt.
if ( in_array( 'excerpt', $fields ) ) {
$response['excerpt'] = wpautop( get_the_excerpt( $post->ID ) );
}

// If $fields includes media, get the featured image + caption.
if ( in_array( 'media', $fields ) ) {
$response['media'] = [
'image' => get_the_post_thumbnail_url( $post->ID, 'medium' ),
'caption' => get_the_post_thumbnail_caption( $post->ID ),
];
}

// If $fields includes meta, get all Newspack Listings meta fields.
if ( in_array( 'meta', $fields ) ) {
$post_meta = array_filter(
get_post_meta( $post->ID ),
function( $key ) {
return is_numeric( strpos( $key, 'newspack_listings_' ) );
},
ARRAY_FILTER_USE_KEY
);
$post_meta = Core::get_meta_values( $post->ID, $post->post_type );

if ( ! empty( $post_meta ) ) {
$response['meta'] = $post_meta;
}
}

return [
'id' => $post->ID,
'title' => $post->post_title,
'content' => wpautop( get_the_excerpt( $post->ID ) ),
'meta' => $post_meta,
];
return $response;
},
$query->posts
),
Expand Down
135 changes: 88 additions & 47 deletions includes/class-newspack-listings-blocks.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* Newspack Listings Core.
* Newspack Listings Blocks.
*
* Registers custom post types and taxonomies.
* Custom Gutenberg Blocks for Newspack Listings.
*
* @package Newspack_Listings
*/
Expand All @@ -18,6 +18,10 @@
* Sets up custom blocks for listings.
*/
final class Newspack_Listings_Blocks {
/**
* Slug for the block pattern category.
*/
const NEWSPACK_LISTINGS_BLOCK_PATTERN_CATEGORY = 'newspack-listings-patterns';

/**
* The single instance of the class.
Expand All @@ -43,53 +47,53 @@ public static function instance() {
* Constructor.
*/
public function __construct() {
add_filter( 'block_categories', [ __CLASS__, 'update_block_categories' ] );
add_action( 'enqueue_block_editor_assets', [ __CLASS__, 'manage_editor_assets' ] );
add_action( 'admin_init', [ __CLASS__, 'register_block_pattern_category' ], 10 );
add_action( 'admin_init', [ __CLASS__, 'register_block_patterns' ], 11 );
add_action( 'init', [ __CLASS__, 'manage_view_assets' ] );
}

/**
* Enqueue editor assets.
*/
public static function manage_editor_assets() {
if ( Core::is_curated_list() || Core::is_listing() ) {
wp_enqueue_script(
'newspack-listings-editor',
NEWSPACK_LISTINGS_URL . 'dist/editor.js',
[],
NEWSPACK_LISTINGS_VERSION,
true
);

$post_type = get_post_type();

wp_localize_script(
'newspack-listings-editor',
'newspack_listings_data',
[
'post_type' => get_post_type_object( $post_type )->labels->singular_name,
'post_types' => Core::NEWSPACK_LISTINGS_POST_TYPES,
'meta_fields' => Core::get_meta_fields( $post_type ),
]
);

wp_register_style(
'newspack-listings-editor',
plugins_url( '../dist/editor.css', __FILE__ ),
[],
NEWSPACK_LISTINGS_VERSION
);
wp_style_add_data( 'newspack-listings-editor', 'rtl', 'replace' );
wp_enqueue_style( 'newspack-listings-editor' );
}
wp_enqueue_script(
'newspack-listings-editor',
NEWSPACK_LISTINGS_URL . 'dist/editor.js',
[],
NEWSPACK_LISTINGS_VERSION,
true
);

$post_type = get_post_type();

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' => Core::NEWSPACK_LISTINGS_POST_TYPES,
'meta_fields' => Core::get_meta_fields( $post_type ),
]
);

wp_register_style(
'newspack-listings-editor',
plugins_url( '../dist/editor.css', __FILE__ ),
[],
NEWSPACK_LISTINGS_VERSION
);
wp_style_add_data( 'newspack-listings-editor', 'rtl', 'replace' );
wp_enqueue_style( 'newspack-listings-editor' );
}

/**
* Enqueue front-end assets.
*/
public static function manage_view_assets() {
// Do nothing in editor environment.
if ( is_admin() ) {
// In editor environment, do nothing.
return;
}

Expand Down Expand Up @@ -127,22 +131,59 @@ public static function manage_view_assets() {
}

/**
* Add custom block category.
*
* @param array $categories Default Gutenberg categories.
* @return array
* Register custom block pattern category for Newspack Listings.
*/
public static function update_block_categories( $categories ) {
return array_merge(
$categories,
[
[
'slug' => 'newspack-listings',
'title' => __( 'Newspack Listings', 'newspack-listings' ),
],
]
public static function register_block_pattern_category() {
return register_block_pattern_category(
self::NEWSPACK_LISTINGS_BLOCK_PATTERN_CATEGORY,
[ 'label' => __( 'Newspack Listings', 'newspack-listings' ) ]
);
}

/**
* Register custom block patterns for Newspack Listings.
* These patterns should only be available for certain CPTs.
*/
public static function register_block_patterns() {
// Block pattern config.
$block_patterns = [
'business' => [
'post_types' => [
Core::NEWSPACK_LISTINGS_POST_TYPES['marketplace'],
Core::NEWSPACK_LISTINGS_POST_TYPES['place'],
],
'settings' => [
'title' => __( 'Business Listing', 'newspack-listings' ),
'categories' => [ self::NEWSPACK_LISTINGS_BLOCK_PATTERN_CATEGORY ],
'description' => _x(
'Business description, website and social media links, and hours of operation.',
'Block pattern description',
'newspack-listings'
),
'content' => '<!-- wp:columns --><div class="wp-block-columns"><!-- wp:column {"width":66.66} --><div class="wp-block-column" style="flex-basis:66.66%"><!-- wp:paragraph --><p>Consectetur a urna hendrerit scelerisque suspendisse inceptos scelerisque neque parturient a mi adipiscing euismod mus. Ad felis morbi magna augue consectetur eleifend sit sem habitant suspendisse posuere amet felis adipiscing a himenaeos ipsum vivamus dictum vestibulum lacus consectetur vestibulum erat dignissim per sem integer. Cras class ac adipiscing inceptos a enim porta a elit scelerisque tincidunt hac ad netus accumsan parturient conubia vestibulum nec quisque parturient interdum fringilla curabitur cras sociosqu interdum. Porta aenean id a mus consectetur lacus lacus ut parturient sapien ut a sociosqu potenti ridiculus non tristique cursus a at parturient condimentum a duis convallis per. Dictum elementum ultricies ac risus vestibulum adipiscing placerat imperdiet malesuada scelerisque dictum mus adipiscing a at at fermentum scelerisque nisl a dignissim suscipit sapien taciti nulla curabitur vestibulum.</p><!-- /wp:paragraph --></div><!-- /wp:column --><!-- wp:column {"width":33.33} --><div class="wp-block-column" style="flex-basis:33.33%"><!-- wp:jetpack/map {"mapCenter":{"lng":-122.41941550000001,"lat":37.7749295},"mapHeight":null} -->
<div class="wp-block-jetpack-map" data-map-style="default" data-map-details="true" data-points="[]" data-zoom="13" data-map-center="{&quot;lng&quot;:-122.41941550000001,&quot;lat&quot;:37.7749295}" data-marker-color="red" data-show-fullscreen-button="true"></div>
<!-- /wp:jetpack/map --><!-- wp:jetpack/contact-info --><div class="wp-block-jetpack-contact-info"><!-- wp:jetpack/address /--><!-- wp:jetpack/email /--><!-- wp:jetpack/phone /--></div><!-- /wp:jetpack/contact-info --><!-- wp:buttons --><div class="wp-block-buttons"><!-- wp:button {"className":"is-style-fill"} --><div class="wp-block-button is-style-fill"><a class="wp-block-button__link" href="https://newspack.pub">Visit our website</a></div><!-- /wp:button --></div><!-- /wp:buttons --><!-- wp:social-links --><ul class="wp-block-social-links"><!-- wp:social-link {"url":"https://newspack.pub","service":"wordpress"} /--><!-- wp:social-link {"url":"https://facebook.com","service":"facebook"} /--><!-- wp:social-link {"url":"https://twitter.com","service":"twitter"} /--><!-- wp:social-link {"url":"https://instagram.com","service":"instagram"} /--><!-- wp:social-link {"service":"linkedin"} /--><!-- wp:social-link {"service":"youtube"} /--></ul><!-- /wp:social-links --><!-- wp:separator {"className":"is-style-wide"} --><hr class="wp-block-separator is-style-wide"/><!-- /wp:separator --><!-- wp:heading {"level":4} --><h4>Hours of Operation</h4><!-- /wp:heading --><!-- wp:jetpack/business-hours /--></div><!-- /wp:column --></div><!-- /wp:columns -->',
],
],
];

/**
* Register block patterns for particular post types. We need to get the post type using the
* post ID from $_REQUEST since the global $post is not available inside the admin_init hook.
* If we can't determine the current post type, just register the patterns anyway.
*/
$post_id = isset( $_REQUEST['post'] ) ? sanitize_text_field( $_REQUEST['post'] ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$current_post_type = ! empty( $post_id ) && function_exists( 'get_post_type' ) ? get_post_type( $post_id ) : null;

foreach ( $block_patterns as $pattern_name => $config ) {
if ( empty( $current_post_type ) || in_array( $current_post_type, $config['post_types'] ) ) {
$pattern = register_block_pattern(
'newspack-listings/' . $pattern_name,
$config['settings']
);
}
}
}
}

Newspack_Listings_Blocks::instance();
Loading