From 69778e1e4356e389198a0706c615ecadb45bc72c Mon Sep 17 00:00:00 2001 From: Zack Katz Date: Mon, 11 Mar 2024 22:44:43 +0800 Subject: [PATCH 1/2] Show my work so far MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/GravityKit/GravityView/pull/1882#issuecomment-1970212456 --- ...ass-gravityview-plugin-hooks-lifterlms.php | 58 ++++++++++++++++++- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/includes/plugin-and-theme-hooks/class-gravityview-plugin-hooks-lifterlms.php b/includes/plugin-and-theme-hooks/class-gravityview-plugin-hooks-lifterlms.php index 892caeccd..dbef140b5 100644 --- a/includes/plugin-and-theme-hooks/class-gravityview-plugin-hooks-lifterlms.php +++ b/includes/plugin-and-theme-hooks/class-gravityview-plugin-hooks-lifterlms.php @@ -88,6 +88,61 @@ 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_filter( 'gravityview/datatables/json/header/content_length', [ $this, 'filter_content_length' ], 10, 2 ); + } + + public function filter_content_length( $length ) { + 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' ] ); + return $length; + } + + /** + * 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; } /** @@ -234,7 +289,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 ); @@ -276,7 +330,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 ); } From 9f5fcac6e95e08372a05a5684ca30ea190c95694 Mon Sep 17 00:00:00 2001 From: Zack Katz Date: Wed, 13 Mar 2024 15:53:55 +0800 Subject: [PATCH 2/2] Use new DataTables action instead Requires DataTables 3.3.6 or newer https://github.com/GravityKit/DataTables/commit/77d686d --- .../class-gravityview-plugin-hooks-lifterlms.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/includes/plugin-and-theme-hooks/class-gravityview-plugin-hooks-lifterlms.php b/includes/plugin-and-theme-hooks/class-gravityview-plugin-hooks-lifterlms.php index dbef140b5..6f7e52fd0 100644 --- a/includes/plugin-and-theme-hooks/class-gravityview-plugin-hooks-lifterlms.php +++ b/includes/plugin-and-theme-hooks/class-gravityview-plugin-hooks-lifterlms.php @@ -90,16 +90,22 @@ protected function configure() { 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_filter( 'gravityview/datatables/json/header/content_length', [ $this, 'filter_content_length' ], 10, 2 ); + add_action( 'gk/gravityview/datatables/get-output-data/before', [ $this, 'datatables_setup_filters' ] ); } - public function filter_content_length( $length ) { + /** + * 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' ] ); - return $length; } /**