Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 39 additions & 0 deletions includes/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

defined( 'ABSPATH' ) || exit;

use Newspack\Optional_Modules\Collections;

/**
* Newspack Blocks Class.
*/
Expand All @@ -26,8 +28,12 @@ public static function init() {
if ( wp_is_block_theme() ) {
require_once NEWSPACK_ABSPATH . 'src/blocks/avatar/class-avatar-block.php';
}
if ( Collections::is_module_active() ) {
require_once NEWSPACK_ABSPATH . 'src/blocks/collections/index.php';
}

\add_action( 'enqueue_block_editor_assets', [ __CLASS__, 'enqueue_block_editor_assets' ] );
\add_action( 'wp_enqueue_scripts', [ __CLASS__, 'enqueue_frontend_assets' ] );
}

/**
Expand Down Expand Up @@ -57,6 +63,7 @@ public static function enqueue_block_editor_assets() {
'has_recaptcha' => Recaptcha::can_use_captcha(),
'recaptcha_url' => admin_url( 'admin.php?page=newspack-settings' ),
'corrections_enabled' => wp_is_block_theme() && class_exists( 'Newspack\Corrections' ),
'collections_enabled' => Collections::is_module_active(),
]
);
\wp_enqueue_style(
Expand All @@ -66,5 +73,37 @@ public static function enqueue_block_editor_assets() {
NEWSPACK_PLUGIN_VERSION
);
}

/**
* Enqueue blocks scripts and styles for frontend.
* Only load if we have blocks on the page that need these styles.
*/
public static function enqueue_frontend_assets() {
if ( self::should_load_block_assets() ) {
\wp_enqueue_style(
'newspack-blocks-frontend',
Newspack::plugin_url() . '/dist/blocks.css',
[],
NEWSPACK_PLUGIN_VERSION
);
}
}

/**
* Check if we should load block assets on current page.
*
* @return bool Whether to load block assets.
*/
private static function should_load_block_assets() {
if (
is_singular() &&
Collections::is_module_active() &&
has_block( \Newspack\Blocks\Collections\Collections_Block::BLOCK_NAME, get_the_ID() )
) {
return true;
}

return false;
}
}
Blocks::init();
39 changes: 39 additions & 0 deletions includes/collections/class-collection-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public static function get_meta_definitions() {
*/
public static function init() {
add_action( 'init', [ __CLASS__, 'register_meta' ] );
add_action( 'rest_api_init', [ __CLASS__, 'register_rest_fields' ] );
}

/**
Expand Down Expand Up @@ -208,4 +209,42 @@ private static function sanitize_single_cta( $cta ) {

return $sanitized_cta;
}

/**
* Register custom REST API fields for collections.
*/
public static function register_rest_fields() {
register_rest_field(
Post_Type::get_post_type(),
'ctas',
[
'get_callback' => [ __CLASS__, 'get_collection_ctas_for_rest' ],
'schema' => [
'description' => __( 'Collection CTAs', 'newspack-plugin' ),
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'url' => [ 'type' => 'string' ],
'label' => [ 'type' => 'string' ],
'class' => [ 'type' => 'string' ],
],
],
'context' => [ 'view', 'edit' ],
],
]
);
}

/**
* Get collection CTAs for REST API response.
*
* @param array $post Post data.
* @return array Array of processed CTAs.
*
* @see Query_Helper::get_ctas()
*/
public static function get_collection_ctas_for_rest( $post ) {
return Query_Helper::get_ctas( $post['id'] );
}
}
73 changes: 73 additions & 0 deletions includes/collections/class-query-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,77 @@ function ( $post ) use ( $exclude ) {
*/
return apply_filters( 'newspack_collections_recent', array_slice( $filtered, 0, $limit ), $exclude, $limit );
}

/**
* Build and run the collections query based on block attributes.
*
* @param array $attributes Block attributes (sanitized).
* @return array Array of WP_Post collection objects.
*/
public static function get_collections_by_attributes( $attributes ) {
$query_args = [
'post_type' => Post_Type::get_post_type(),
'post_status' => 'publish',
'posts_per_page' => $attributes['numberOfItems'],
'orderby' => 'date',
'order' => 'DESC',
'offset' => $attributes['offset'],
];

// Handle specific collections selection.
if ( 'specific' === ( $attributes['queryType'] ?? '' ) && ! empty( $attributes['selectedCollections'] ) ) {
$query_args['post__in'] = $attributes['selectedCollections'];
$query_args['orderby'] = 'post__in';
unset( $query_args['offset'] ); // Offset doesn't apply to specific post selections.
}

// Handle category filtering.
if ( ! empty( $attributes['includeCategories'] ) || ! empty( $attributes['excludeCategories'] ) ) {
$tax_query = [];

if ( ! empty( $attributes['includeCategories'] ) ) {
$tax_query[] = [
'taxonomy' => Collection_Category_Taxonomy::get_taxonomy(),
'field' => 'term_id',
'terms' => $attributes['includeCategories'],
'operator' => 'IN',
];
}

if ( ! empty( $attributes['excludeCategories'] ) ) {
$tax_query[] = [
'taxonomy' => Collection_Category_Taxonomy::get_taxonomy(),
'field' => 'term_id',
'terms' => $attributes['excludeCategories'],
'operator' => 'NOT IN',
];
}

if ( count( $tax_query ) > 1 ) {
$tax_query['relation'] = 'AND';
}

$query_args['tax_query'] = $tax_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
}

/**
* Generic filter for collections query args.
*
* @param array $query_args Query args.
* @param array $attributes Block attributes.
*/
$query_args = apply_filters( 'newspack_collections_query_args', $query_args, $attributes );

$query = new \WP_Query( $query_args );
$posts = $query->posts;

/**
* Filter the collections posts returned by the query.
*
* @param array $posts Array of WP_Post objects.
* @param array $query_args Final query args.
* @param array $attributes Block attributes.
*/
return apply_filters( 'newspack_collections_query_posts', $posts, $query_args, $attributes );
}
}
1 change: 1 addition & 0 deletions packages/icons/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { default as aspectPortrait } from './src/aspect-portrait';
export { default as aspectSquare } from './src/aspect-square';
export { default as ballotBox } from './src/ballot-box';
export { default as broadcast } from './src/broadcast';
export { default as collections } from './src/collections';
export { default as contentCarousel } from './src/content-carousel';
export { default as contentLoop } from './src/content-loop';
export { default as corrections } from './src/corrections';
Expand Down
12 changes: 12 additions & 0 deletions packages/icons/src/collections.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

135 changes: 135 additions & 0 deletions src/blocks/collections/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "newspack/collections",
"version": "1.0.0",
"title": "Collections",
"category": "newspack",
"description": "An advanced block that allows displaying collections based on different parameters and visual configurations.",
"keywords": [ "collections", "issues", "publications", "magazines", "groups" ],
"supports": {
"html": false,
"align": [ "wide", "full" ],
"spacing": {
"margin": true,
"padding": true
}
},
"attributes": {
"queryType": {
"type": "string",
"default": "recent"
},
"numberOfItems": {
"type": "number",
"default": 4
},
"offset": {
"type": "number",
"default": 0
},
"selectedCollections": {
"type": "array",
"default": []
},
"includeCategories": {
"type": "array",
"default": []
},
"excludeCategories": {
"type": "array",
"default": []
},
"layout": {
"type": "string",
"default": "grid"
},
"columns": {
"type": "number",
"default": 4
},
"imageAlignment": {
"type": "string",
"default": "left"
},
"imageSize": {
"type": "string",
"default": "small"
},
"showFeaturedImage": {
"type": "boolean",
"default": true
},
"showTitle": {
"type": "boolean",
"default": true
},
"showCategory": {
"type": "boolean",
"default": true
},
"showExcerpt": {
"type": "boolean",
"default": false
},
"showPeriod": {
"type": "boolean",
"default": true
},
"showVolume": {
"type": "boolean",
"default": true
},
"showNumber": {
"type": "boolean",
"default": true
},
"showCTAs": {
"type": "boolean",
"default": true
},
"numberOfCTAs": {
"type": "number",
"default": 1
},
"showSubscriptionUrl": {
"type": "boolean",
"default": true
},
"showOrderUrl": {
"type": "boolean",
"default": true
},
"specificCTAs": {
"type": "string",
"default": ""
},
"showSeeAllLink": {
"type": "boolean",
"default": true
},
"seeAllLinkText": {
"type": "string",
"default": ""
}
},
"example": {
"attributes": {
"queryType": "recent",
"numberOfItems": 4,
"layout": "grid",
"columns": 4,
"showFeaturedImage": true,
"showTitle": true,
"showPeriod": true,
"showVolume": true,
"showNumber": true,
"showCTAs": true,
"numberOfCTAs": 1,
"showSeeAllLink": true,
"seeAllLinkText": "See all collections"
}
},
"textdomain": "newspack-plugin",
"editorScript": "file:./index.js"
}
Loading
Loading