Skip to content

Commit

Permalink
improved and simplified the sort code
Browse files Browse the repository at this point in the history
  • Loading branch information
Fellan-91 committed Apr 25, 2024
1 parent 24601d6 commit 2c5da0b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 36 deletions.
4 changes: 2 additions & 2 deletions classes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1777,8 +1777,8 @@ public function register_controls() {
'title' => esc_html__( 'Item Title', 'visual-portfolio' ),
'description' => esc_html__( 'Item Description', 'visual-portfolio' ),
'image_title' => esc_html__( 'Image Title', 'visual-portfolio' ),
'caption' => esc_html__( 'Image Caption', 'visual-portfolio' ),
'alt' => esc_html__( 'Image Alt', '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
43 changes: 9 additions & 34 deletions classes/class-get-portfolio.php
Original file line number Diff line number Diff line change
Expand Up @@ -1302,25 +1302,25 @@ 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'] = isset( $img_meta['title'] ) ? $img_meta['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'] = isset( $img_meta['description'] ) ? $img_meta['description'] : '';
$images[ $k ]['image_description'] = $img_meta['description'] ?? '';

// image caption.
$images[ $k ]['image_caption'] = isset( $img_meta['caption'] ) ? $img_meta['caption'] : '';
$images[ $k ]['image_caption'] = $img_meta['caption'] ?? '';

// image alt.
$images[ $k ]['image_alt'] = isset( $img_meta['alt'] ) ? $img_meta['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 @@ -1364,39 +1364,14 @@ public static function get_query_params( $options, $for_filter = false, $layout_
case 'title':
case 'description':
case 'image_title':
case 'caption':
case 'alt':
case 'image_caption':
case 'image_alt':
case 'image_description':
$sort_tmp = array();
$new_images = array();
$sort_by = 'date';

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

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

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

if ( 'caption' === $custom_order ) {
$sort_by = 'image_caption';
}

if ( 'alt' === $custom_order ) {
$sort_by = 'image_alt';
}

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

foreach ( $images as &$ma ) {
$sort_tmp[] = &$ma[ $sort_by ];
$sort_tmp[] = &$ma[ $custom_order ];
}

array_multisort( $sort_tmp, $images );
Expand Down

0 comments on commit 2c5da0b

Please sign in to comment.