Skip to content

Commit

Permalink
Simplify Entry Notes asset loading
Browse files Browse the repository at this point in the history
This fixes script registration occurring after localization,
affecting Twenty Twenty-Two and newer themes (#2266).
  • Loading branch information
mrcasual committed Jan 29, 2025
1 parent d402c41 commit 97a16d0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
59 changes: 30 additions & 29 deletions includes/extensions/entry-notes/class-gravityview-field-notes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* @since 1.17
*/
class GravityView_Field_Notes extends GravityView_Field {

/**
* @var string Current __FILE__
* @since 1.17
Expand Down Expand Up @@ -79,7 +78,6 @@ private function add_hooks() {
add_filter( 'gravityview_template_paths', array( $this, 'add_template_path' ) );
add_filter( 'gravityview/template/fields_template_paths', array( $this, 'add_template_path' ) );

add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );
add_action( 'gravityview/field/notes/scripts', array( $this, 'enqueue_scripts' ) );

add_filter( 'gravityview_entry_default_fields', array( $this, 'add_entry_default_field' ), 10, 3 );
Expand Down Expand Up @@ -121,45 +119,48 @@ public function add_entry_default_field( $entry_default_fields, $form, $zone ) {
* @return void
*/
public function register_scripts() {
$css_file = gravityview_css_url( 'entry-notes.css', self::$path . 'assets/css/' );
wp_register_style( 'gravityview-notes', $css_file, array(), GV_PLUGIN_VERSION );
wp_register_script( 'gravityview-notes', plugins_url( '/assets/js/entry-notes.js', self::$file ), array( 'jquery' ), GV_PLUGIN_VERSION, true );

}

/**
* Enqueue, localize field scripts and styles
* Registers, enqueues, and localizes scripts and styles.
*
* @since 1.17
*
* @return void
*/
public function enqueue_scripts() {
global $wp_actions;
$script_handle = 'gravityview-notes';

if ( ! wp_script_is( 'gravityview-notes', 'enqueued' ) ) {
wp_enqueue_style( 'gravityview-notes' );
wp_enqueue_script( 'gravityview-notes' );
if ( wp_script_is( $script_handle ) ) {
return;
}

if ( ! wp_script_is( 'gravityview-notes', 'done' ) ) {

$strings = self::strings();

wp_localize_script(
'gravityview-notes',
'GVNotes',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'text' => array(
'processing' => $strings['processing'],
'delete_confirm' => $strings['delete-confirm'],
'note_added' => $strings['added-note'],
'error_invalid' => $strings['error-invalid'],
'error_empty_note' => $strings['error-empty-note'],
),
)
);
}

$strings = self::strings();

$css_file = gravityview_css_url( 'entry-notes.css', self::$path . 'assets/css/' );

wp_register_style( $script_handle, $css_file, [], GV_PLUGIN_VERSION );
wp_register_script( $script_handle, plugins_url( '/assets/js/entry-notes.js', self::$file ), [ 'jquery' ], GV_PLUGIN_VERSION, true );

wp_localize_script(
$script_handle,
'GVNotes',
[
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'text' => [
'processing' => $strings['processing'],
'delete_confirm' => $strings['delete-confirm'],
'note_added' => $strings['added-note'],
'error_invalid' => $strings['error-invalid'],
'error_empty_note' => $strings['error-empty-note'],
],
]
);

wp_enqueue_style( $script_handle );
wp_enqueue_script( $script_handle );
}

/**
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h
* The Search Bar would not always be visible in Views using the Layout Builder.
* Users belonging to the main network site in a multisite environment couldn’t delete their own entries on subsites.
* Entry locking not working.
* JavaScript error preventing entry notes from being added when using the Twenty Twenty-Two theme or newer.

#### 💻 Developer Updates
* Added `gk/gravityview/edit-entry/renderer/enqueue-entry-lock-assets` filter to override whether to load the entry lock UI assets.
Expand Down

0 comments on commit 97a16d0

Please sign in to comment.