Skip to content

Commit

Permalink
Fix custom Entry slug on embedded Views (#2207) (#2222)
Browse files Browse the repository at this point in the history
  • Loading branch information
doekenorg authored Dec 2, 2024
2 parents 06a746f + 6dcf3f8 commit 9ed2157
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
56 changes: 54 additions & 2 deletions future/includes/class-gv-permalinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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' ] );
}

/**
Expand Down Expand Up @@ -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( (string) $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 );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
}
}
35 changes: 34 additions & 1 deletion future/includes/class-gv-shortcode-gravityview.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace GV\Shortcodes;

use GV\View;

/** If this file is called directly, abort. */
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
die();
Expand All @@ -20,6 +22,15 @@ class gravityview extends \GV\Shortcode {
*/
public static $callstack = array();

/**
* Keeps the current View remembered.
*
* @since $ver$
*
* @var View|null
*/
private static ?View $current_view = null;

/**
* Process and output the [gravityview] shortcode.
*
Expand Down Expand Up @@ -108,6 +119,7 @@ public function callback( $passed_atts, $content = '', $tag = '' ) {
*/
do_action( 'gravityview/shortcode/before-processing', $view, $post );

self::$current_view = $view;
gravityview()->views->set( $view );

/**
Expand Down Expand Up @@ -186,7 +198,7 @@ public function callback( $passed_atts, $content = '', $tag = '' ) {
'</a>'
);

return \GVCommon::generate_notice( '<p>' . $notice . '</p>', 'notice', array( 'delete_post' ), $view->ID );
return self::_return( \GVCommon::generate_notice( '<p>' . $notice . '</p>', 'notice', array( 'delete_post' ), $view->ID ) );
case 'no_direct_access':
case 'embed_only':
case 'not_public':
Expand Down Expand Up @@ -295,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
Expand Down Expand Up @@ -485,6 +505,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;
}
}
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h
#### 🐛 Fixed
* Merge tags in redirect URLs were not processed after editing or deleting an entry in the lightbox.
* Individual Checkboxes field inputs incorrectly exported in CSV.
* Custom permalinks were not used on embedded Views.
* When multiple Views were embedded on the same page, it would show the other Views when displaying a single entry.

#### 💻 Developer Updates
* Added `gravityview/template/field/csv/tick` filter to programmatically modify the checkbox "check" output in CSV.
* Added `gravityview/shortcode/after-processing` action after a `[gravityview]` shortcode is finished.

= 2.32 on November 21, 2024 =

Expand Down

0 comments on commit 9ed2157

Please sign in to comment.