Skip to content

Commit 0876fc5

Browse files
Petter Walbø Johnsgårdtrinef
authored andcommitted
Add taxonomy support (#56)
* Add support for selection taxonomies * Make sure ‘content_type’ isset * Translate * Move filter * Make sure category field is only visible if any categories * Add return type * Dynamic cards from post types and categories (not or) (#59) * Lint fixes * Update language files * Update readme
1 parent 706c383 commit 0876fc5

File tree

4 files changed

+214
-87
lines changed

4 files changed

+214
-87
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ Dynamic content show card with content that is dynamically queried. Ex. showing
4545
'page' => __( 'Pages', 'hogan-grid' )
4646
]
4747
```
48+
- `hogan/module/grid/dynamic_content_taxonomies` - add taxonomies to use in dynamic selection. Ex. use:
49+
```
50+
function enable_dynamic_taxonomies() : array {
51+
return [ 'category', 'my_custom_taxonomy' ];
52+
}
53+
add_filter( 'hogan/module/grid/dynamic_content_taxonomies', __NAMESPACE__ . '\\enable_dynamic_taxonomies' );
54+
```
4855

4956
- `hogan/module/grid/card_sizes` - card sizes to use in module
5057
```

class-grid.php

Lines changed: 125 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ class Grid extends Module {
5656
*/
5757
public $fetched_posts = [];
5858

59+
/**
60+
* Supported taxonomies
61+
*
62+
* @var array
63+
*/
64+
private $taxonomies = [];
65+
5966
/**
6067
* Module constructor.
6168
*/
@@ -64,9 +71,12 @@ public function __construct() {
6471
$this->label = __( 'Card grid', 'hogan-grid' );
6572
$this->template = __DIR__ . '/assets/template.php';
6673

74+
$this->taxonomies = (array) apply_filters( 'hogan/module/grid/dynamic_content_taxonomies', [] );
75+
6776
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] );
6877

6978
add_filter( 'acf/fields/relationship/query/name=posts_list', [ $this, 'relationship_options_filter' ], 10, 2 );
79+
add_filter( 'acf/load_field/name=card_content_categories', [ $this, 'taxonomy_choices' ], 10, 3 );
7080
add_filter( 'hogan/module/outer_wrapper_classes', [ $this, 'add_wrapper_classname' ], 10, 2 );
7181

7282
parent::__construct();
@@ -233,58 +243,94 @@ public function get_fields() : array {
233243
'name' => 'dynamic_content',
234244
'label' => esc_html__( 'Dynamic content', 'hogan-grid' ),
235245
'display' => 'block',
236-
'sub_fields' => [
237-
$this->card_style_field( $this->field_key . '_dynamic_card_style' ),
238-
[
239-
'type' => 'select',
240-
'key' => $this->field_key . '_dynamic_card_content_type',
241-
'label' => __( 'Content Type', 'hogan-grid' ),
242-
'name' => 'card_content_type',
243-
'instructions' => __( 'Select content types to build cards from', 'hogan-grid' ),
244-
'required' => 1,
245-
'wrapper' => [
246-
'width' => '50',
247-
],
248-
'choices' => apply_filters( 'hogan/module/grid/dynamic_content_post_types', [
249-
'post' => __( 'Posts', 'hogan-grid' ),
250-
'page' => __( 'Pages', 'hogan-grid' ),
251-
], $this ),
252-
'allow_null' => 0,
253-
'multiple' => 1,
254-
'ui' => 1,
255-
'ajax' => 0,
256-
'return_format' => 'value',
257-
],
258-
[
259-
'type' => 'number',
260-
'key' => $this->field_key . '_number_of_items',
261-
'label' => __( 'Number of items', 'hogan-grid' ),
262-
'name' => 'number_of_items',
263-
'instructions' => __( 'Set the number of items to display', 'hogan-grid' ),
264-
'required' => 1,
265-
'default_value' => 3,
266-
'min' => 1,
267-
'max' => apply_filters( 'hogan/module/grid/dynamic_content_limit', 10 ),
268-
'step' => 1,
269-
'wrapper' => [
270-
'width' => '50',
271-
],
272-
],
273-
],
246+
'sub_fields' => $this->get_dynamic_sub_fields(),
247+
],
248+
],
249+
];
250+
251+
return $fields;
252+
}
253+
254+
/**
255+
* Dynamic sub fields
256+
*/
257+
private function get_dynamic_sub_fields() : array {
258+
$fields = [
259+
$this->card_style_field( $this->field_key . '_dynamic_card_style', 50 ),
260+
[
261+
'type' => 'number',
262+
'key' => $this->field_key . '_number_of_items',
263+
'label' => __( 'Number of items', 'hogan-grid' ),
264+
'name' => 'number_of_items',
265+
'instructions' => __( 'Set the number of items to display', 'hogan-grid' ),
266+
'required' => 1,
267+
'default_value' => 3,
268+
'min' => 1,
269+
'max' => 10,
270+
'step' => 1,
271+
'wrapper' => [
272+
'width' => '50',
274273
],
275274
],
276275
];
277276

277+
$post_types_field = [
278+
'type' => 'select',
279+
'key' => $this->field_key . '_dynamic_card_content_type',
280+
'label' => __( 'Post types', 'hogan-grid' ),
281+
'name' => 'card_content_type',
282+
'instructions' => __( 'Select post types to build cards from', 'hogan-grid' ),
283+
'required' => 1,
284+
'wrapper' => [
285+
'width' => '100',
286+
],
287+
'choices' => apply_filters( 'hogan/module/grid/dynamic_content_post_types', [
288+
'post' => __( 'Posts', 'hogan-grid' ),
289+
'page' => __( 'Pages', 'hogan-grid' ),
290+
], $this ),
291+
'allow_null' => 0,
292+
'multiple' => 1,
293+
'ui' => 1,
294+
'ajax' => 0,
295+
'return_format' => 'value',
296+
];
297+
298+
if ( ! empty( $this->taxonomies ) ) {
299+
$post_types_field['wrapper']['width'] = '50';
300+
}
301+
302+
$fields[] = $post_types_field;
303+
304+
if ( ! empty( $this->taxonomies ) ) {
305+
$fields[] = [
306+
'type' => 'select',
307+
'key' => $this->field_key . '_dynamic_card_tax',
308+
'label' => __( 'Categories', 'hogan-grid' ),
309+
'name' => 'card_content_categories',
310+
'instructions' => __( 'Select categories to build cards from', 'hogan-grid' ),
311+
'required' => 0,
312+
'wrapper' => [
313+
'width' => '50',
314+
],
315+
'allow_null' => 1,
316+
'multiple' => 1,
317+
'ui' => 1,
318+
'ajax' => 0,
319+
'return_format' => 'value',
320+
];
321+
}
322+
278323
return $fields;
279324
}
280325

281326
/**
282327
* Card style reusable field
283328
*
284329
* @param string $key Field key.
330+
* @param int $width Field width.
285331
* @return array
286332
*/
287-
private function card_style_field( string $key ) : array {
333+
private function card_style_field( string $key, int $width = 100 ) : array {
288334
$field = [
289335
'type' => 'button_group',
290336
'key' => $key,
@@ -296,6 +342,9 @@ private function card_style_field( string $key ) : array {
296342
'medium' => __( 'Double', 'hogan-grid' ),
297343
'large' => __( 'Full', 'hogan-grid' ),
298344
], $this ),
345+
'wrapper' => [
346+
'width' => $width,
347+
],
299348
'allow_null' => 0,
300349
'default_value' => 'automatic',
301350
'layout' => 'horizontal',
@@ -332,6 +381,25 @@ public function relationship_options_filter( array $options, array $field ) : ar
332381
return $options;
333382
}
334383

384+
/**
385+
* Filters taxonomy query to include multiple taxonomies
386+
*
387+
* @param array $field ACF field.
388+
* @return array
389+
*/
390+
public function taxonomy_choices( array $field ) : array {
391+
$terms = get_terms( [
392+
'taxonomy' => $this->taxonomies,
393+
'hide_empty' => true,
394+
]);
395+
396+
foreach ( $terms as $term ) {
397+
$field['choices'][ $term->term_id ] = $term->name;
398+
}
399+
400+
return $field;
401+
}
402+
335403
/**
336404
* Module wrapper classname.
337405
*
@@ -432,13 +500,29 @@ public function structure_card_data( array $data ) : array {
432500
break;
433501

434502
case 'dynamic_content':
435-
$cards_query = new \WP_Query( apply_filters( 'hogan/module/grid/dynamic_content_query', [
503+
$cards_query_args = [
436504
'fields' => 'ids',
437505
'post_type' => $group['card_content_type'],
438506
'post_status' => 'publish',
439507
'posts_per_page' => $group['number_of_items'],
440508
'post__not_in' => wp_parse_id_list( $this->fetched_posts ),
441-
] ) );
509+
];
510+
511+
if ( ! empty( $group['card_content_categories'] ) ) {
512+
$cards_query_args['tax_query'] = [ // phpcs:ignore
513+
'relation' => 'OR',
514+
];
515+
516+
foreach ( $this->taxonomies as $taxonomy ) {
517+
$cards_query_args['tax_query'][] = [
518+
'taxonomy' => $taxonomy,
519+
'field' => 'term_id',
520+
'terms' => $group['card_content_categories'],
521+
];
522+
}
523+
}
524+
525+
$cards_query = new \WP_Query( apply_filters( 'hogan/module/grid/dynamic_content_query', $cards_query_args ) );
442526

443527
if ( $cards_query->have_posts() ) {
444528
foreach ( $cards_query->posts as $post_id ) {

languages/hogan-grid-nb_NO.mo

492 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)