From 0ac51d7b8062aed707424c11336fe8ccde421a70 Mon Sep 17 00:00:00 2001 From: mateuswetah Date: Wed, 10 May 2023 14:57:33 -0300 Subject: [PATCH] Creates option to change default order of related items list. --- .../archive-elements/default-view-mode.php | 2 +- .../single-elements/items-related-to-this.php | 17 ++++++++++-- tainacan-blocksy/inc/plugin.php | 27 ++++++++++++++++--- ...acan-item-single-items-related-to-this.php | 15 +++++++++-- 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/tainacan-blocksy/inc/options/archive-elements/default-view-mode.php b/tainacan-blocksy/inc/options/archive-elements/default-view-mode.php index 7b05dec..235a167 100644 --- a/tainacan-blocksy/inc/options/archive-elements/default-view-mode.php +++ b/tainacan-blocksy/inc/options/archive-elements/default-view-mode.php @@ -4,7 +4,7 @@ $view_modes = tainacan_get_default_view_mode_choices(); $options = [ $prefix . 'default_view_mode' => [ - 'label' => __('Default view mode', 'blocksy'), + 'label' => __('Default view mode', 'tainacan-blocksy'), 'type' => 'ct-select', 'value' => $view_modes['default_view_mode'], 'view' => 'text', diff --git a/tainacan-blocksy/inc/options/single-elements/items-related-to-this.php b/tainacan-blocksy/inc/options/single-elements/items-related-to-this.php index 2d2c951..36d10d6 100644 --- a/tainacan-blocksy/inc/options/single-elements/items-related-to-this.php +++ b/tainacan-blocksy/inc/options/single-elements/items-related-to-this.php @@ -10,6 +10,8 @@ if (! isset($enabled)) { $enabled = 'yes'; } +$order_options = tainacan_get_default_order_choices(); + $options = [ $prefix . 'display_items_related_to_this' => [ 'label' => __( 'Display "Items related to this"', 'tainacan-blocksy' ), @@ -51,7 +53,7 @@ $options = [ ], 'options' => [ $prefix . 'items_related_to_this_max_items_per_screen' => [ - 'label' => __( 'Max amount of items per slide', 'blocksy' ), + 'label' => __( 'Max amount of items per slide', 'tainacan-blocksy' ), 'type' => 'ct-number', 'design' => 'inline', 'value' => 6, @@ -68,7 +70,7 @@ $options = [ ], 'options' => [ $prefix . 'items_related_to_this_max_columns_count' => [ - 'label' => __( 'Max amount of items columns', 'blocksy' ), + 'label' => __( 'Max amount of items columns', 'tainacan-blocksy' ), 'type' => 'ct-number', 'design' => 'inline', 'value' => 4, @@ -77,6 +79,17 @@ $options = [ 'sync' => '' ] ] + ], + $prefix . 'items_related_to_this_order' => [ + 'label' => __('Order by', 'blocksy'), + 'type' => 'ct-select', + 'value' => 'title_asc', + 'view' => 'text', + 'design' => 'inline', + 'sync' => '', + 'choices' => blocksy_ordered_keys( + $order_options + ) ] ] ] diff --git a/tainacan-blocksy/inc/plugin.php b/tainacan-blocksy/inc/plugin.php index fa2a2dd..febcf91 100644 --- a/tainacan-blocksy/inc/plugin.php +++ b/tainacan-blocksy/inc/plugin.php @@ -133,10 +133,10 @@ if ( !function_exists('tainacan_get_default_view_mode_choices') ) { } else { $default_view_mode = 'masonry'; $enabled_view_modes = [ - 'masonry' => __('Masonry', 'tainacan-interface'), - 'cards' => __('Cards', 'tainacan-interface'), - 'table' => __('Table', 'tainacan-interface'), - 'grid' => __('Grid', 'tainacan-interface') + 'masonry' => __('Masonry', 'tainacan-blocksy'), + 'cards' => __('Cards', 'tainacan-blocksy'), + 'table' => __('Table', 'tainacan-blocksy'), + 'grid' => __('Grid', 'tainacan-blocksy') ]; } return [ @@ -147,4 +147,23 @@ if ( !function_exists('tainacan_get_default_view_mode_choices') ) { } +/** + * Retrieves possible orderby and order options to offer as default + * + * @return array An associative array with orderby and order options + */ +if ( !function_exists('tainacan_get_default_order_choices') ) { + function tainacan_get_default_order_choices() { + return [ + 'title_asc' => __( 'Title A-Z', 'tainacan-blocksy'), + 'title_desc' => __( 'Title Z-A', 'tainacan-blocksy'), + 'date_asc' => __( 'Latest created last', 'tainacan-blocksy'), + 'date_desc' => __( 'Latest created first', 'tainacan-blocksy'), + 'modified_asc' => __( 'Latest modified last', 'tainacan-blocksy'), + 'modified_desc' => __( 'Latest modified first', 'tainacan-blocksy'), + ]; + } +} + + ?> \ No newline at end of file diff --git a/tainacan-blocksy/template-parts/tainacan-item-single-items-related-to-this.php b/tainacan-blocksy/template-parts/tainacan-item-single-items-related-to-this.php index bb3d7ec..21feac8 100644 --- a/tainacan-blocksy/template-parts/tainacan-item-single-items-related-to-this.php +++ b/tainacan-blocksy/template-parts/tainacan-item-single-items-related-to-this.php @@ -5,6 +5,17 @@ $items_related_to_this_layout = get_theme_mod( $prefix . '_items_related_to_this_layout', 'carousel' ); $max_columns_count = get_theme_mod( $prefix . '_items_related_to_this_max_columns_count', 4 ); $max_items_per_screen = get_theme_mod( $prefix . '_items_related_to_this_max_items_per_screen', 6 ); + $order_option = get_theme_mod( $prefix . '_items_related_to_this_order', 'title_asc' ); + + $order_option_split = explode( '_', $order_option ); + $order_by = $order_option_split[0] ? $order_option_split[0] : 'title'; + $order = $order_option_split[1] ? $order_option_split[1] : 'asc'; + + if ( !in_array($order_by, [ 'title', 'date', 'modified' ]) ) + $order_by = 'title'; + + if ( !in_array($order, [ 'asc', 'desc' ]) ) + $order = 'asc'; if ( function_exists('tainacan_the_related_items_carousel') && (get_theme_mod( $prefix . '_display_items_related_to_this', 'no' ) === 'yes') && tainacan_has_related_items() ) : ?> @@ -20,8 +31,8 @@ tainacan_the_related_items_carousel([ 'items_list_layout' => $items_related_to_this_layout, 'collection_heading_tag' => 'h3', - 'order' => 'asc', - 'orderby' => 'title', + 'order' => $order, + 'orderby' => $order_by, 'dynamic_items_args' => [ 'max_columns_count' => $max_columns_count ],