Created
November 29, 2020 22:57
-
-
Save hansschuijff/642b60833ee1f3e52082f4524ad65a37 to your computer and use it in GitHub Desktop.
Adds Module taxonomy colum to lessons list and adds it to quick edit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Makes the module column in the admin's lesson list sortable. | |
* | |
* @package DeWittePrins\CoreFunctionality\SenseiLMS | |
* @since 1.7.1 | |
* @author Hans Schuijff | |
* @link https://dewitteprins.nl | |
* @license GNU-2.0+ | |
*/ | |
namespace DeWittePrins\CoreFunctionality\SenseiLMS; | |
add_filter( 'register_taxonomy_args', __NAMESPACE__ . '\correct_module_taxonomy_args', 20, 3 ); | |
/** | |
* Enhance the registration of Sensei Module taxonomy to: | |
* - show modules in quick edit (this will enable bulk edits) | |
* - show modules in REST api (needed for quick edit) | |
* - show modules in posts list | |
* - add missing labels | |
* | |
* Normally setting show_admin_column to true, | |
* will setup wordpress to show the column automatically in admins post list screen | |
* However in case of Sensei LMS the columns in that screen are hardcoded | |
* and overwrite the default behaviour. | |
* | |
* @since 1.7.1 | |
* @param array $args | |
* @param string $taxonomy | |
* @return array $args | |
*/ | |
function correct_module_taxonomy_args( $args, $taxonomy ) { | |
if ( function_exists( 'Sensei' ) && 'module' === $taxonomy ) { | |
if ( ! isset( $args['labels']['view_item'] ) ) { | |
$args['labels']['view_item'] = __( 'View Module', 'sensei-lms' ); | |
} | |
if ( ! isset( $args['labels']['back_to_items'] ) ) { | |
$args['labels']['back_to_items'] = __( '← Back to Modules', 'sensei-lms' ); | |
} | |
$args['show_admin_column'] = true; | |
$args['show_in_quick_edit'] = true; | |
$args['show_in_rest'] = true; | |
} | |
return $args; | |
} | |
add_filter( 'manage_edit-lesson_sortable_columns', __NAMESPACE__ . '\set_module_column_to_sortable_in_lessons_list' ); | |
/** | |
* Makes the lesson-module column sortable in admins post list | |
* | |
* This only tells wordpress the column is sortable. | |
* It also needs to learn how to sort on that column, | |
* so the query must be programmed too to sort on this taxonomy | |
* | |
* @since 1.7.1 | |
* @param array $columns | |
* @return array $columns | |
*/ | |
function set_module_column_to_sortable_in_lessons_list( $columns ) { | |
$columns['taxonomy-module'] = 'taxonomy-module'; | |
return $columns; | |
} | |
add_filter( 'posts_clauses', __NAMESPACE__ . '\add_clause_to_order_lessons_by_module', 10, 2 ); | |
/** | |
* Tells wordpress how to sort lessons by lesson module | |
* | |
* Adds an order by clause to the query | |
* whenever the orderby query var is set to "lesson-module" | |
* | |
* This teaches wordpress how to sort the admin post list of lessons | |
* on lesson-module | |
* | |
* @since 1.7.1 | |
* @param array $clauses | |
* @return object $wp_query instance of WP_Query | |
*/ | |
function add_clause_to_order_lessons_by_module( $clauses, $wp_query ) { | |
global $wpdb; | |
if ( isset( $wp_query->query['orderby'] ) | |
&& 'taxonomy-module' == $wp_query->query['orderby'] ) { | |
$clauses['join'] .= | |
" LEFT JOIN ( | |
SELECT object_id, GROUP_CONCAT(name ORDER BY name ASC) AS module | |
FROM $wpdb->term_relationships | |
INNER JOIN $wpdb->term_taxonomy USING (term_taxonomy_id) | |
INNER JOIN $wpdb->terms USING (term_id) | |
WHERE taxonomy = 'module' | |
GROUP BY object_id | |
) AS module_terms ON ($wpdb->posts.ID = module_terms.object_id)"; | |
$clauses['orderby'] = 'module_terms.module '; | |
$clauses['orderby'] .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC'; | |
} | |
return $clauses; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment