Skip to content

Commit

Permalink
Fixed issue with localized data variables not working in Full Site Ed…
Browse files Browse the repository at this point in the history
…iting (FSE) themes.
  • Loading branch information
dcooney committed Jan 7, 2023
1 parent d30c08b commit 7ccb41a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
4 changes: 4 additions & 0 deletions ajax-load-more.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* @package AjaxLoadMore
*/

/*
* FIX: Fixed issue with localized data variables not working in Full Site Editing (FSE) themes.
*/

define( 'ALM_VERSION', '5.5.5' );
define( 'ALM_RELEASE', 'January 5, 2023' );
define( 'ALM_STORE_URL', 'https://connekthq.com' );
Expand Down
16 changes: 7 additions & 9 deletions core/classes/class-alm-localize.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
/**
* A class for adding localize ALM JS variables to the screen.
* A class for adding localized ALM JS variables to the screen.
* Note: Localized data is used by the ALM JS to access information about the query instance.
* Post count, total posts etc are all stored in localized variables.
*
* @package ajaxloadmore
* @since 3.7
Expand Down Expand Up @@ -44,16 +46,12 @@ public static function add_localized_var( $key = '', $value = '', $id = 'ajax-lo
}

/**
* Create <script> variables for use with Preloaded addon.
* Return the localized data.
*
* @param string $id The ALM ID.
* @since 3.7
* @return array The localized data.
*/
public static function create_script_vars( $id ) {
if ( ! empty( self::$vars ) && isset( self::$vars[ $id ] ) && is_array( self::$vars[ $id ] ) ) {
// Render wp_localize_script vars with 'ajax-load-more' script handle.
wp_localize_script( 'ajax-load-more', alm_convert_dashes_to_underscore( $id ) . '_vars', self::$vars[ $id ] );
}
public static function return_localized_data() {
return self::$vars;
}

}
Expand Down
18 changes: 14 additions & 4 deletions core/classes/class-alm-shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -1385,15 +1385,25 @@ public static function alm_render_shortcode( $atts ) {
}
}

// Add localized vars.
/**
* Build localized script variables for each ALM instance.
*/

// Add basic localized vars.
ALM_LOCALIZE::add_localized_var( 'id', $master_id, $localize_id );
ALM_LOCALIZE::add_localized_var( 'script', alm_convert_dashes_to_underscore( $localize_id ) . '_vars', $localize_id );

// Get the localized data.
$localized_data = ALM_LOCALIZE::return_localized_data( $localize_id );

/**
* Build localized script vars for each ALM instance.
* Append the localized data `<script/>` using `wp_footer`.
*
* @return <script>
* @see https://developer.wordpress.org/reference/functions/wp_add_inline_script/#comment-5828
*/
ALM_LOCALIZE::create_script_vars( $localize_id );
add_action('wp_footer', function() use ( $localized_data, $localize_id ) {
printf('<script type="text/javascript" id="' . $localized_data[$localize_id]['script'] .'">var ' . $localized_data[$localize_id]['script'] .' = %s</script>', json_encode( $localized_data[$localize_id] ) );
});

// End $ajaxloadmore element.
return $ajaxloadmore;
Expand Down

0 comments on commit 7ccb41a

Please sign in to comment.