Skip to content

Commit

Permalink
Editor: Move printing TinyMCE scripts into an action
Browse files Browse the repository at this point in the history
Moving this logic into an action allows plugins to override
how/when TinyMCE is loaded. Specifically, Gutenberg needs to
be able to prevent TinyMCE scripts from being written into
the page when TinyMCE should be loaded asynchronously when it
is actually needed.
  • Loading branch information
sarayourfriend committed Apr 23, 2020
1 parent 07bc94c commit 32115c7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/wp-includes/class-wp-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1556,9 +1556,7 @@ public static function print_tinymce_scripts() {
script_concat_settings();
}

wp_print_scripts( array( 'wp-tinymce' ) );

echo "<script type='text/javascript'>\n" . self::wp_mce_translation() . "</script>\n";
do_action( 'print_tinymce_scripts' );
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ function wp_register_tinymce_scripts( $scripts, $force_uncompressed = false ) {
$scripts->add( 'wp-tinymce-lists', includes_url( "js/tinymce/plugins/lists/plugin$suffix.js" ), array( 'wp-tinymce' ), $tinymce_version );
}

/**
* Function responsible for printing the TinyMCE scripts.
*
* @since 5.4.0
*/
function wp_print_tinymce_scripts() {
wp_print_scripts( array( 'wp-tinymce' ) );

echo "<script type='text/javascript'>\n" . _WP_Editors::wp_mce_translation() . "</script>\n";
}

/**
* Executed in _WP_Editors::print_tinymce_scripts
* after concatenation settings have been initialized.
*
* @see _WP_Editors::print_tinymce_scripts()
*/
add_action( 'print_tinymce_scripts', 'wp_print_tinymce_scripts' );

/**
* Registers all the WordPress vendor scripts that are in the standardized
* `js/dist/vendor/` location.
Expand Down
8 changes: 8 additions & 0 deletions tests/phpunit/tests/editor/wpEditors.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,12 @@ public function test_wp_link_query_returns_filtered_search_results() {
$actual
);
}

public function test_print_tinymce_scripts_calls_print_tinymce_scripts_action() {
$this->assertEquals( 0, did_action( 'print_tinymce_scripts' ) );

_WP_Editors::print_tinymce_scripts();

$this->assertEquals( 1, did_action( 'print_tinymce_scripts' ) );
}
}

0 comments on commit 32115c7

Please sign in to comment.