Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove jQuery dependency from @wordpress/api-fetch #8311

Merged
merged 9 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
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
18 changes: 17 additions & 1 deletion lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function gutenberg_register_scripts_and_styles() {
wp_register_script(
'wp-api-fetch',
gutenberg_url( 'build/api-fetch/index.js' ),
array( 'wp-i18n' ),
array( 'wp-hooks', 'wp-i18n' ),
filemtime( gutenberg_dir_path() . 'build/api-fetch/index.js' ),
true
);
Expand Down Expand Up @@ -1093,11 +1093,27 @@ function gutenberg_editor_scripts_and_styles( $hook ) {

gutenberg_prepare_wp_components_script();

global $wp_scripts;

// Add "wp-hooks" as dependency of "heartbeat".
$heartbeat_script = $wp_scripts->query( 'heartbeat', 'registered' );
if ( $heartbeat_script && ! in_array( 'wp-hooks', $heartbeat_script->deps ) ) {
$heartbeat_script->deps[] = 'wp-hooks';
}

// Enqueue heartbeat separately as an "optional" dependency of the editor.
// Heartbeat is used for automatic nonce refreshing, but some hosts choose
// to disable it outright.
wp_enqueue_script( 'heartbeat' );

// Transform a "heartbeat-tick" jQuery event into "heartbeat.tick" hook action.
// This removes the need of using jQuery for listening to the event.
wp_add_inline_script(
'heartbeat',
'jQuery( document ).on( "heartbeat-tick", function ( event, response ) { wp.hooks.doAction( "heartbeat.tick", response ) } );',
'after'
);

// Ignore Classic Editor's `rich_editing` user option, aka "Disable visual
// editor". Forcing this to be true guarantees that TinyMCE and its plugins
// are available in Gutenberg. Fixes
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/api-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"module": "build-module/index.js",
"dependencies": {
"@babel/runtime": "^7.0.0-beta.52",
"@wordpress/i18n": "file:../i18n",
"jquery": "^3.3.1"
"@wordpress/hooks": "file:../hooks",
"@wordpress/i18n": "file:../i18n"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 2 additions & 2 deletions packages/api-fetch/src/middlewares/nonce.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import jQuery from 'jquery';
import { addAction } from '@wordpress/hooks';

const createNonceMiddleware = ( nonce ) => ( options, next ) => {
let usedNonce = nonce;
Expand All @@ -11,7 +11,7 @@ const createNonceMiddleware = ( nonce ) => ( options, next ) => {
* Configure heartbeat to refresh the wp-api nonce, keeping the editor
* authorization intact.
*/
jQuery( document ).on( 'heartbeat-tick', ( event, response ) => {
addAction( 'heartbeat.tick', 'core/api-fetch/create-nonce-middleware', ( response ) => {
if ( response[ 'rest-nonce' ] ) {
usedNonce = response[ 'rest-nonce' ];
}
Expand Down