Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,39 @@ function gutenberg_safe_style_css_column_flex_basis( $attr ) {
return $attr;
}
add_filter( 'safe_style_css', 'gutenberg_safe_style_css_column_flex_basis' );

/**
* Adds an inline script to augment the default persistence method for data to
* use WordPress client user settings.
*
* @since 5.8.0
*/
function gutenberg_settings_cookie_persistence() {
// Ensure `utils` is a dependency, which makes available `userSettings`,
// `getUserSetting`, and `setUserSettings` window globals.
$data_script = wp_scripts()->query( 'wp-data', 'registered' );
if ( ! in_array( 'utils', $data_script->deps ) ) {
$data_script->deps[] = 'utils';
}

wp_add_inline_script(
'wp-data',
"
( function() {
wp.data.use( wp.data.plugins.persistence, {
storage: {
getItem: function() {
return (
window.getUserSetting( 'dataState' ) ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How complex it would be to transform this to a package. To be clear, I think the current proposal is good enough, just wondering if we should take this opportunity to packagify this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saw the size limitations, it's unfortunate. What's the alternative you're thinking of? an endpoint?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I like how nice this solution is. I'd give it a try and see how it scales :)

localStorage.getItem( 'WP_DATA_USER_' + window.userSettings.uid )
);
},
setItem: function( key, value ) {
window.setUserSetting( 'dataState', value );
Copy link
Member

@gziolo gziolo May 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The source code of the method:

https://github.com/WordPress/wordpress-develop/blob/8347d7e22f98520228309392757ea75fd7bca4db/src/js/_enqueues/lib/cookies.js

// Both name and value must be only ASCII letters, numbers or underscore
// and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text.

}
}
} );
} )();"
);
}
add_action( 'admin_enqueue_scripts', 'gutenberg_settings_cookie_persistence' );