From 0056c403571768e7b650bcd1139affbae8e1bb38 Mon Sep 17 00:00:00 2001 From: Doeke Norg Date: Wed, 27 Nov 2024 12:09:52 +0100 Subject: [PATCH 1/6] Add action after processing shortcode --- .../class-gv-shortcode-gravityview.php | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/future/includes/class-gv-shortcode-gravityview.php b/future/includes/class-gv-shortcode-gravityview.php index 7fc74ca01..e28c45780 100644 --- a/future/includes/class-gv-shortcode-gravityview.php +++ b/future/includes/class-gv-shortcode-gravityview.php @@ -1,6 +1,8 @@ views->set( $view ); /** @@ -186,7 +198,7 @@ public function callback( $passed_atts, $content = '', $tag = '' ) { '' ); - return \GVCommon::generate_notice( '

' . $notice . '

', 'notice', array( 'delete_post' ), $view->ID ); + return self::_return( \GVCommon::generate_notice( '

' . $notice . '

', 'notice', array( 'delete_post' ), $view->ID ) ); case 'no_direct_access': case 'embed_only': case 'not_public': @@ -485,6 +497,19 @@ private function detail( $view, $entries, $atts ) { */ private static function _return( $value ) { array_pop( self::$callstack ); + $view = self::$current_view; + + self::$current_view = null; // Clear for future calls. + + /** + * @action `gravityview/shortcode/after-processing` Runs after the GV shortcode is processed. + * + * @since $ver$ + * + * @param \GV\View|null $view The View object. + */ + do_action( 'gravityview/shortcode/after-processing', $view ); + return $value; } } From fc5cc1f1c4e84e34662c8b18d22f232e888af319 Mon Sep 17 00:00:00 2001 From: Doeke Norg Date: Wed, 27 Nov 2024 12:12:35 +0100 Subject: [PATCH 2/6] Prevent display other embedded View when a single entry is displayed --- future/includes/class-gv-shortcode-gravityview.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/future/includes/class-gv-shortcode-gravityview.php b/future/includes/class-gv-shortcode-gravityview.php index e28c45780..3a2a7c76e 100644 --- a/future/includes/class-gv-shortcode-gravityview.php +++ b/future/includes/class-gv-shortcode-gravityview.php @@ -307,6 +307,14 @@ public function callback( $passed_atts, $content = '', $tag = '' ) { * Just this view. */ } else { + /** + * When viewing a specific View don't render the other Views. + */ + $selected_view = (int) \GV\Utils::_GET( 'gvid',0 ); + if ( $selected_view && (int) $view->ID !== $selected_view ) { + return self::_return( '' ); + } + if ( $is_reembedded ) { // Mock the request with the actual View, not the global one From 2d149019c0ce9e34226811eab30f4d3a2586551b Mon Sep 17 00:00:00 2001 From: Doeke Norg Date: Wed, 27 Nov 2024 12:16:53 +0100 Subject: [PATCH 3/6] Fix permalinks on embedded Views --- future/includes/class-gv-permalinks.php | 56 ++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/future/includes/class-gv-permalinks.php b/future/includes/class-gv-permalinks.php index 93cf2c4e1..4fd2250d0 100644 --- a/future/includes/class-gv-permalinks.php +++ b/future/includes/class-gv-permalinks.php @@ -21,6 +21,16 @@ final class Permalinks { */ private Plugin_Settings $settings; + /** + * A memoization of the current View. + * + * This is used to determine the View, when rendering through a short code. + * + * @since $ver$ + * @var View|null + */ + private ?View $current_view; + /** * The default slug values. * @@ -170,6 +180,9 @@ public function __construct( Plugin_Settings $settings ) { add_action( 'init', [ $this, 'maybe_update_rewrite_rules' ], 1 ); add_action( 'admin_enqueue_scripts', [ $this, 'add_view_settings_scripts' ], 1500 ); + + add_action( 'gravityview/shortcode/before-processing', [ $this, 'capture_view' ] ); + add_action( 'gravityview/shortcode/after-processing', [ $this, 'clear_captured_view' ] ); } /** @@ -267,7 +280,7 @@ public function set_entry_endpoint( $endpoint ): string { */ public function set_entry_slug( $slug, $entry_id, array $entry ): string { $new_slug = trim( $this->settings->get( 'entry_slug', $slug ) ); - $view = View::from_post( get_post() ); + $view = $this->get_current_view(); if ( $view && (int) $view->form->ID === (int) $entry['form_id'] ) { $new_slug = trim( (string) $view->settings->get( 'single_entry_slug' ) ?: $new_slug ); @@ -295,7 +308,7 @@ public function is_custom_entry_slug( bool $is_custom_slug ): bool { $is_global_entry_slug = '' !== trim( (string) $this->settings->get( 'entry_slug', '' ) ); $is_view_entry_slug = false; - $view = View::from_post( get_post() ); + $view = $this->get_current_view(); if ( $view ) { $entry_slug = (string) $view->settings->get( 'single_entry_slug' ); $is_view_entry_slug = (bool) trim( $entry_slug ); @@ -742,4 +755,43 @@ public function add_global_settings_scripts( array $scripts ): array { return $scripts; } + + /** + * Captures the current View when it is rendered through a shortcode or block. + * @since $ver$ + * @param View|null $view The View object. + */ + public function capture_view( $view ): void { + /** + * When viewing an entry don't render multiple views. + */ + $selected_view = (int) ( $_GET['gvid'] ?? 0 ); + if ( $selected_view && (int) $view->ID !== $selected_view ) { + return; + } + + if ( $view instanceof View ) { + $this->current_view = $view; + } + } + + /** + * Clears the captured View object. + * + * @since $ver$ + */ + public function clear_captured_view(): void { + $this->current_view = null; + } + + /** + * Returns the current View. + * + * @since $ver$ + */ + private function get_current_view(): ?View { + $view = $this->current_view ?? View::from_post( get_post() ); + + return $view instanceof View ? $view : null; + } } From 05ea088a2d73c8515d8cd9655ebc4b6efecd5246 Mon Sep 17 00:00:00 2001 From: Doeke Norg Date: Wed, 27 Nov 2024 12:19:10 +0100 Subject: [PATCH 4/6] Update readme --- readme.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/readme.txt b/readme.txt index e8a4d7fa0..352127a14 100644 --- a/readme.txt +++ b/readme.txt @@ -23,6 +23,9 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h = develop = +* Fixed: Custom permalinks were not used on embedded Views. +* Fixed: When multiple Views were embedded on the same page, it would show the other Views when displaying a single entry. + = 2.32 on November 21, 2024 = This release adds a new form notification option for updated entries, resolves file upload issues on the Edit Entry screen, and includes developer-focused enhancements. From 7ebe083195ad0ac2cee75d1d63b9392b4a358c28 Mon Sep 17 00:00:00 2001 From: Doeke Norg Date: Wed, 27 Nov 2024 13:07:14 +0100 Subject: [PATCH 5/6] Update Readme developer hook --- readme.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/readme.txt b/readme.txt index 352127a14..4417bca34 100644 --- a/readme.txt +++ b/readme.txt @@ -26,6 +26,9 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h * Fixed: Custom permalinks were not used on embedded Views. * Fixed: When multiple Views were embedded on the same page, it would show the other Views when displaying a single entry. +__Developer updates__: +* Added `gravityview/shortcode/after-processing` action after a `[gravityview]` shortcode is finished. + = 2.32 on November 21, 2024 = This release adds a new form notification option for updated entries, resolves file upload issues on the Edit Entry screen, and includes developer-focused enhancements. From 8c06b24504a874515fc124a7e8ed34b667df3510 Mon Sep 17 00:00:00 2001 From: Doeke Norg Date: Wed, 27 Nov 2024 16:58:43 +0100 Subject: [PATCH 6/6] Fix trim call --- future/includes/class-gv-permalinks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/future/includes/class-gv-permalinks.php b/future/includes/class-gv-permalinks.php index 4fd2250d0..850226dcd 100644 --- a/future/includes/class-gv-permalinks.php +++ b/future/includes/class-gv-permalinks.php @@ -279,7 +279,7 @@ public function set_entry_endpoint( $endpoint ): string { * @return string The slug. */ public function set_entry_slug( $slug, $entry_id, array $entry ): string { - $new_slug = trim( $this->settings->get( 'entry_slug', $slug ) ); + $new_slug = trim( (string) $this->settings->get( 'entry_slug', $slug ) ); $view = $this->get_current_view(); if ( $view && (int) $view->form->ID === (int) $entry['form_id'] ) {