From 81f8619f2be565f2ff323c554c9859d782d14d35 Mon Sep 17 00:00:00 2001 From: Derek P Sifford Date: Fri, 18 Jan 2019 21:01:21 -0500 Subject: [PATCH] fix issue with fetching initial post-meta in wordpress core version of gutenberg --- src/js/stores/data/resolvers.ts | 16 ++++++++++++++-- src/php/editor.php | 5 +++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/js/stores/data/resolvers.ts b/src/js/stores/data/resolvers.ts index e27581eb..35b2b38e 100644 --- a/src/js/stores/data/resolvers.ts +++ b/src/js/stores/data/resolvers.ts @@ -1,9 +1,15 @@ import { select } from '@wordpress/data'; -import { SavedState } from './'; +import { SavedState, Style } from './'; import { Actions } from './constants'; import { fetchCitationStyles } from './controls'; +declare const ABT: Maybe<{ + options: { + style: Style; + }; +}>; + export function* getCitationStyles() { const styles = yield fetchCitationStyles(); return { @@ -33,7 +39,13 @@ function getSavedState(): SavedState { 'core/editor', ).getCurrentPostAttribute('meta'); if (!meta || !meta._abt_state) { - throw new Error('Unable to retrieve registered post meta for ABT'); + if (!ABT) { + throw new Error('Could not resolve default citation style.'); + } + return { + references: [], + style: ABT.options.style, + }; } return JSON.parse(meta._abt_state); } diff --git a/src/php/editor.php b/src/php/editor.php index 5c244bf9..09890d56 100644 --- a/src/php/editor.php +++ b/src/php/editor.php @@ -33,6 +33,11 @@ function enqueue_scripts(): void { wp_enqueue_script( get_handle( 'editor', 'script' ) ); init_editor_state( $post->ID ); + wp_add_inline_script( + get_handle( 'editor-stores', 'script' ), + 'var ABT = ABT || {};' . + 'ABT.options = ' . wp_json_encode( get_option( ABT_OPTIONS_KEY ) ) . ';' + ); } } add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\enqueue_scripts' );