Skip to content

Commit

Permalink
Merge pull request #494 from GlotPress/488-alternate-solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ocean90 committed Jul 6, 2016
2 parents 5d51b7a + f8b3fcd commit cd70032
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 26 deletions.
56 changes: 56 additions & 0 deletions gp-includes/misc.php
Expand Up @@ -508,3 +508,59 @@ function gp_wp_profile_options_update( $user_id ) {
$current_perm->delete();
}
}

/**
* Returns a multi-dimensional array with the sort by types, descriptions and SQL query for each.
*
* @since 2.1.0
*
* @return array An array of sort by field types.
*/
function gp_get_sort_by_fields() {
$sort_fields = array(
'original_date_added' => array(
'title' => __( 'Date added (original)', 'glotpress' ),
'sql_sort_by' => 'o.date_added %s',
),
'translation_date_added' => array(
'title' => __( 'Date added (translation)', 'glotpress' ),
'sql_sort_by' => 't.date_added %s',
),
'original' => array(
'title' => __( 'Original string', 'glotpress' ),
'sql_sort_by' => 'o.singular %s',
),
'translation' => array(
'title' => __( 'Translation', 'glotpress' ),
'sql_sort_by' => 't.translation_0 %s',
),
'priority' => array(
'title' => __( 'Priority', 'glotpress' ),
'sql_sort_by' => 'o.priority %s, o.date_added DESC',
),
'references' => array(
'title' => __( 'Filename in source', 'glotpress' ),
'sql_sort_by' => 'o.references',
),
'random' => array(
'title' => __( 'Random', 'glotpress' ),
'sql_sort_by' => 'o.priority DESC, RAND()',
),
);

/**
* Filter the sort by list to allow plugins to add or remove sort by types.
*
* Plugins can add, remove or resort the sort by types array which is used to create
* the sort by drop down in the translations page.
*
* @since 2.1.0
*
* @param array $sort_fields {
* A list of sort by types.
*
* @type array $sort_type An array with two keys, 'title' is a translated description of the key and 'sql_sort_by' which is a partial SQL SORT BY clause to use.
* }
*/
return apply_filters( 'gp_sort_by_fields', $sort_fields );
}
4 changes: 1 addition & 3 deletions gp-includes/things/translation.php
Expand Up @@ -296,9 +296,7 @@ public function for_translation( $project, $translation_set, $page, $filters = a

$join_type = 'INNER';

$sort_bys = array('original' => 'o.singular %s', 'translation' => 't.translation_0 %s', 'priority' => 'o.priority %s, o.date_added DESC',
'random' => 'o.priority DESC, RAND()', 'translation_date_added' => 't.date_added %s', 'original_date_added' => 'o.date_added %s',
'references' => 'o.references' );
$sort_bys = wp_list_pluck( gp_get_sort_by_fields(), 'sql_sort_by' );

$default_sort = get_user_option( 'gp_default_sort' );
if ( ! is_array( $default_sort ) ) {
Expand Down
16 changes: 3 additions & 13 deletions gp-templates/settings-edit.php
Expand Up @@ -31,19 +31,9 @@
<tr>
<th><label for="default_sort[by]"><?php _e( 'Default Sort By:', 'glotpress' ) ?></label></th>
<td><?php
echo gp_radio_buttons(
'default_sort[by]',
array(
'original_date_added' => __( 'Date added (original)', 'glotpress' ),
'translation_date_added' => __( 'Date added (translation)', 'glotpress' ),
'original' => __( 'Original string', 'glotpress' ),
'translation' => __( 'Translation', 'glotpress' ),
'priority' => __( 'Priority', 'glotpress' ),
'references' => __( 'Filename in source', 'glotpress' ),
'random' => __( 'Random', 'glotpress' ),
),
gp_array_get( $default_sort, 'by', 'priority' )
);
$sort_bys = wp_list_pluck( gp_get_sort_by_fields(), 'title' );

echo gp_radio_buttons( 'default_sort[by]', $sort_bys, gp_array_get( $default_sort, 'by', 'priority' ) );
?></td>
</tr>
<tr>
Expand Down
13 changes: 3 additions & 10 deletions gp-templates/translations.php
Expand Up @@ -146,16 +146,9 @@
);
}

echo gp_radio_buttons('sort[by]',
array(
'original_date_added' => __( 'Date added (original)', 'glotpress' ),
'translation_date_added' => __( 'Date added (translation)', 'glotpress' ),
'original' => __( 'Original string', 'glotpress' ),
'translation' => __( 'Translation', 'glotpress' ),
'priority' => __( 'Priority', 'glotpress' ),
'references' => __( 'Filename in source', 'glotpress' ),
'random' => __( 'Random', 'glotpress' ),
), gp_array_get( $sort, 'by', $default_sort['by'] ) );
$sort_bys = wp_list_pluck( gp_get_sort_by_fields(), 'title' );

echo gp_radio_buttons( 'sort[by]', $sort_bys, gp_array_get( $sort, 'by', $default_sort['by'] ) );
?>
</dd>
<dt><?php _e( 'Order:', 'glotpress' ); ?></dt>
Expand Down

0 comments on commit cd70032

Please sign in to comment.