Skip to content

Commit

Permalink
[WIP] LifterLMS DataTables support (#2008)
Browse files Browse the repository at this point in the history
This should not be merged as-is…I just wanted to show @mrcasual my work
toward resolving the fact that DataTables doesn't generate single entry
links that work:
#1882 (comment)
  • Loading branch information
zackkatz authored Mar 13, 2024
2 parents 472c8f1 + 9f5fcac commit e82e072
Showing 1 changed file with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,67 @@ protected function configure() {
add_filter( 'llms_get_student_dashboard_tabs', [ $this, 'filter_student_dashboard_tabs' ], 1 );
add_action( 'lifterlms_student_dashboard_index', [ $this, 'add_student_dashboard_my_forms' ] );
add_action( 'lifterlms_settings_save_integrations', [ $this, 'save' ], 30 );

// Early hook inside DataTables layout output to allow for the endpoint to be added to the URL.
add_action( 'gk/gravityview/datatables/get-output-data/before', [ $this, 'datatables_setup_filters' ] );
}

/**
* Add hooks to the DataTables output to fix Lifter dashboard behavior.
*
* @since TODO
*
* @return void
*/
public function datatables_setup_filters() {
add_filter( 'option_permalink_structure', [ $this, 'return_false' ] );

// Append the LifterLMS GravityView endpoint to the directory link.
add_filter( 'gravityview_directory_link', [ $this, 'add_endpoint_to_directory_link' ] );
add_filter( 'gravityview_go_back_url', [ $this, 'single_entry_go_back_url' ] );
}

/**
* Fix the permalinks to the entry for the DataTables layout.
*
* @since TODO
*
* @param string $permalink
*
* @return string The filtered output of the DataTables extension.
*/
public function filter_datatables_permalink( $permalink ) {

$parts = explode( '?', $permalink );

return $this->add_endpoint_to_directory_link( $parts[0] ) . '?' . $parts[1];
}

/**
* Fix the permalinks to the entry for the DataTables layout.
*
* @since TODO
*
* @param string $output The output of the DataTables extension.
* @param \GV\View $view The View context.
*
* @return string The filtered output of the DataTables extension.
*/
public function filter_datatables_output( $output, $view ) {

if ( empty( $output['data'] ) ) {
return $output;
}

$entry_endpoint = \GV\Entry::get_endpoint_name();
foreach ( $output['data'] as &$entry ) {

foreach( $entry as $key => $column ) {
$entry[ $key ] = preg_replace( '/\/(' . preg_quote( $entry_endpoint ) . ')\/(.*?)\/?/is', '/?$1=$2', $column );
}
}

return $output;
}

/**
Expand Down Expand Up @@ -234,7 +295,6 @@ public function dashboard_content() {

// Append the LifterLMS GravityView endpoint to the directory link.
add_filter( 'gravityview_directory_link', [ $this, 'add_endpoint_to_directory_link' ] );

add_filter( 'gravityview_go_back_url', [ $this, 'single_entry_go_back_url' ] );

echo do_shortcode( $content );
Expand Down Expand Up @@ -276,7 +336,7 @@ public function return_false() {
*/
public function add_endpoint_to_directory_link( $permalink ) {

// Check against empty string (WordPress) instead of false (as returned by this class).
/** Check against empty string (WordPress) instead of false (as returned by {@see return_false}). */
if ( '' === get_option( 'permalink_structure' ) ) {
return add_query_arg( [ $this->get_endpoint() => 1 ], $permalink );
}
Expand Down

0 comments on commit e82e072

Please sign in to comment.