Skip to content

Commit

Permalink
Add sort by image fields (#204)
Browse files Browse the repository at this point in the history
* added sort by image fields

* improved and simplified the sort code

* changed sorting to take into account empty sortable values

We've reworked this code to work correctly with empty sortable values. Now images with filled values ​​will be sorted first. And empty images will be inserted later at the very end of the array.

* simplified image custom sort code

---------

Co-authored-by: Nikita <[email protected]>
  • Loading branch information
Fellan-91 and nk-o authored Apr 30, 2024
1 parent 5158f89 commit 2c9b52d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 30 deletions.
13 changes: 9 additions & 4 deletions classes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1773,10 +1773,15 @@ public function register_controls() {
'group' => 'images_order',
'default' => 'default',
'options' => array(
'default' => esc_html__( 'Default', 'visual-portfolio' ),
'date' => esc_html__( 'Uploaded', 'visual-portfolio' ),
'title' => esc_html__( 'Title', 'visual-portfolio' ),
'rand' => esc_html__( 'Random', 'visual-portfolio' ),
'default' => esc_html__( 'Default', 'visual-portfolio' ),
'title' => esc_html__( 'Item Title', 'visual-portfolio' ),
'description' => esc_html__( 'Item Description', 'visual-portfolio' ),
'image_title' => esc_html__( 'Image Title', 'visual-portfolio' ),
'image_caption' => esc_html__( 'Image Caption', 'visual-portfolio' ),
'image_alt' => esc_html__( 'Image Alt', 'visual-portfolio' ),
'image_description' => esc_html__( 'Image Description', 'visual-portfolio' ),
'date' => esc_html__( 'Image Uploaded', 'visual-portfolio' ),
'rand' => esc_html__( 'Random', 'visual-portfolio' ),
),
)
);
Expand Down
77 changes: 51 additions & 26 deletions classes/class-get-portfolio.php
Original file line number Diff line number Diff line change
Expand Up @@ -1268,12 +1268,16 @@ public static function get_query_params( $options, $for_filter = false, $layout_
// prepare titles and descriptions.
foreach ( $images as $k => $img ) {
$img_meta = array(
'title' => '',
'description' => '',
'caption' => '',
'alt' => '',
'none' => '',
'date' => '',
'title' => '',
'image_title' => '',
'image_description' => '',
'image_caption' => '',
'image_alt' => '',
'description' => '',
'caption' => '',
'alt' => '',
'none' => '',
'date' => '',
);

// Find current attachment post data.
Expand All @@ -1298,14 +1302,26 @@ public static function get_query_params( $options, $for_filter = false, $layout_

// title.
if ( 'custom' !== $options['images_titles_source'] ) {
$images[ $k ]['title'] = isset( $img_meta[ $options['images_titles_source'] ] ) ? $img_meta[ $options['images_titles_source'] ] : '';
$images[ $k ]['title'] = $img_meta[ $options['images_titles_source'] ] ?? '';
}

// image title.
$images[ $k ]['image_title'] = $img_meta['title'] ?? '';

// description.
if ( 'custom' !== $options['images_descriptions_source'] ) {
$images[ $k ]['description'] = isset( $img_meta[ $options['images_descriptions_source'] ] ) ? $img_meta[ $options['images_descriptions_source'] ] : '';
$images[ $k ]['description'] = $img_meta[ $options['images_descriptions_source'] ] ?? '';
}

// image description.
$images[ $k ]['image_description'] = $img_meta['description'] ?? '';

// image caption.
$images[ $k ]['image_caption'] = $img_meta['caption'] ?? '';

// image alt.
$images[ $k ]['image_alt'] = $img_meta['alt'] ?? '';

// add published date.
$images[ $k ]['published_time'] = get_the_date( 'Y-m-d H:i:s', $attachment );
}
Expand Down Expand Up @@ -1346,24 +1362,32 @@ public static function get_query_params( $options, $for_filter = false, $layout_
switch ( $custom_order ) {
case 'date':
case 'title':
$sort_tmp = array();
$new_images = array();
$sort_by = 'date';

if ( 'title' === $custom_order ) {
$sort_by = 'title';
}

foreach ( $images as &$ma ) {
$sort_tmp[] = &$ma[ $sort_by ];
case 'description':
case 'image_title':
case 'image_caption':
case 'image_alt':
case 'image_description':
/**
* We've reworked this code to work correctly with empty sortable values.
* Now images with filled values ​​will be sorted first.
* And empty images will be inserted later at the very end of the array.
*/
$non_empty_images = array();
$empty_images = array();
$sort_keys = array();

foreach ( $images as $image ) {
if ( empty( $image[ $custom_order ] ) ) {
$empty_images[] = $image;
} else {
$non_empty_images[] = $image;
$sort_keys[] = $image[ $custom_order ];
}
}

array_multisort( $sort_tmp, $images );
foreach ( $images as &$ma ) {
$new_images[] = $ma;
}
array_multisort( $sort_keys, 'desc' === $custom_order_direction ? SORT_DESC : SORT_ASC, $non_empty_images );

$images = $new_images;
$images = array_merge( $non_empty_images, $empty_images );
break;
case 'rand':
// We don't need to randomize order for filter,
Expand All @@ -1380,11 +1404,12 @@ public static function get_query_params( $options, $for_filter = false, $layout_
}
}

if ( 'desc' === $custom_order_direction ) {
$images = array_reverse( $images );
}

break;
}
if ( 'desc' === $custom_order_direction ) {
$images = array_reverse( $images );
}
}

// pages count.
Expand Down

0 comments on commit 2c9b52d

Please sign in to comment.