Skip to content

Commit

Permalink
Register mce plugin scripts (#863)
Browse files Browse the repository at this point in the history
* Register mce plugin scripts

* Use whole src URL to cache scripts

* Do not download vendor scripts during the PHPUnit build
  • Loading branch information
ellatrix authored and nylen committed May 24, 2017
1 parent dc453b3 commit b712b00
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 2 deletions.
57 changes: 55 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ function gutenberg_register_scripts() {
'tinymce-nightly',
'https://fiddle.azurewebsites.net/tinymce/nightly/tinymce' . $suffix . '.js'
);
gutenberg_register_vendor_script(
'tinymce-nightly-lists',
'https://fiddle.azurewebsites.net/tinymce/nightly/plugins/lists/plugin' . $suffix . '.js',
array( 'tinymce-nightly' )
);
wp_register_script(
'wp-i18n',
plugins_url( 'i18n/build/index.js', __FILE__ ),
Expand All @@ -193,7 +198,7 @@ function gutenberg_register_scripts() {
wp_register_script(
'wp-blocks',
plugins_url( 'blocks/build/index.js', __FILE__ ),
array( 'wp-element', 'tinymce-nightly' ),
array( 'wp-element', 'tinymce-nightly', 'tinymce-nightly-lists' ),
filemtime( plugin_dir_path( __FILE__ ) . 'blocks/build/index.js' )
);

Expand All @@ -207,6 +212,50 @@ function gutenberg_register_scripts() {
}
add_action( 'init', 'gutenberg_register_scripts' );

/**
* Retrieves a unique and reasonably short and human-friendly filename for a
* vendor script based on a URL.
*
* @param string $src Full URL of the external script.
*
* @return string Script filename suitable for local caching.
*
* @since 0.1.0
*/
function gutenberg_vendor_script_filename( $src ) {
$filename = basename( $src );
$hash = substr( md5( $src ), 0, 8 );

$match = preg_match(
'/^'
. '(?P<prefix>.*?)'
. '(?P<ignore>\.development|\.production)?'
. '(?P<suffix>\.min)?'
. '(?P<extension>\.js)'
. '(?P<extra>.*)'
. '$/',
$filename,
$filename_pieces
);

if ( ! $match ) {
return "$filename.$hash.js";
}

$match = preg_match(
'@tinymce.*/plugins/([^/]+)/plugin(\.min)?\.js$@',
$src,
$tinymce_plugin_pieces
);
if ( $match ) {
$filename_pieces['prefix'] = 'tinymce-plugin-' . $tinymce_plugin_pieces[1];
}

return $filename_pieces['prefix'] . $filename_pieces['suffix']
. '.' . $hash
. $filename_pieces['extension'];
}

/**
* Registers a vendor script from a URL, preferring a locally cached version if
* possible, or downloading it if the cached version is unavailable or
Expand All @@ -220,7 +269,11 @@ function gutenberg_register_scripts() {
* @since 0.1.0
*/
function gutenberg_register_vendor_script( $handle, $src, $deps = array() ) {
$filename = basename( $src );
if ( defined( 'GUTENBERG_LOAD_VENDOR_SCRIPTS' ) && ! GUTENBERG_LOAD_VENDOR_SCRIPTS ) {
return;
}

$filename = gutenberg_vendor_script_filename( $src );
$full_path = plugin_dir_path( __FILE__ ) . 'vendor/' . $filename;

$needs_fetch = (
Expand Down
12 changes: 12 additions & 0 deletions phpcs.ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@
<rule ref="Squiz.Commenting.FunctionComment.Missing">
<exclude-pattern>phpunit/*</exclude-pattern>
</rule>
<rule ref="Squiz.Commenting.FileComment.Missing">
<exclude-pattern>phpunit/*</exclude-pattern>
</rule>
<rule ref="Squiz.Commenting.ClassComment.SpacingAfter">
<exclude-pattern>phpunit/*</exclude-pattern>
</rule>
<rule ref="Squiz.Commenting.FunctionComment.MissingParamTag">
<exclude-pattern>phpunit/*</exclude-pattern>
</rule>
<rule ref="Generic.Commenting.DocComment.MissingShort">
<exclude-pattern>phpunit/*</exclude-pattern>
</rule>
</ruleset>
4 changes: 4 additions & 0 deletions phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';

// Do not try to load JavaScript files from an external URL - this takes a
// while.
define( 'GUTENBERG_LOAD_VENDOR_SCRIPTS', false );

/**
* Manually load the plugin being tested.
*/
Expand Down
82 changes: 82 additions & 0 deletions phpunit/class-vendor-script-filename-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Test gutenberg_vendor_script_filename()
*
* @package Gutenberg
*/

class Vendor_Script_Filename_Test extends WP_UnitTestCase {
function vendor_script_filename_cases() {
return array(
// Development mode scripts.
array(
'https://unpkg.com/react@next/umd/react.development.js',
'react.HASH.js',
),
array(
'https://unpkg.com/react-dom@next/umd/react-dom.development.js',
'react-dom.HASH.js',
),
array(
'https://unpkg.com/react-dom@next/umd/react-dom-server.development.js',
'react-dom-server.HASH.js',
),
array(
'https://fiddle.azurewebsites.net/tinymce/nightly/tinymce.js',
'tinymce.HASH.js',
),
array(
'https://fiddle.azurewebsites.net/tinymce/nightly/plugins/lists/plugin.js',
'tinymce-plugin-lists.HASH.js',
),
// Production mode scripts.
array(
'https://unpkg.com/react@next/umd/react.production.min.js',
'react.min.HASH.js',
),
array(
'https://unpkg.com/react-dom@next/umd/react-dom.production.min.js',
'react-dom.min.HASH.js',
),
array(
'https://unpkg.com/react-dom@next/umd/react-dom-server.production.min.js',
'react-dom-server.min.HASH.js',
),
array(
'https://fiddle.azurewebsites.net/tinymce/nightly/tinymce.min.js',
'tinymce.min.HASH.js',
),
array(
'https://fiddle.azurewebsites.net/tinymce/nightly/plugins/lists/plugin.min.js',
'tinymce-plugin-lists.min.HASH.js',
),
// Other cases.
array(
'http://localhost/something.js?querystring',
'something.HASH.js',
),
array(
'http://localhost/something.min.js?querystring',
'something.min.HASH.js',
),
array(
'http://localhost/idkwhatthisis',
'idkwhatthisis.HASH.js',
),
);
}

/**
* @dataProvider vendor_script_filename_cases
*/
function test_gutenberg_vendor_script_filename( $url, $expected_filename_pattern ) {
$hash = substr( md5( $url ), 0, 8 );
$actual_filename = gutenberg_vendor_script_filename( $url );
$actual_filename_pattern = str_replace( $hash, 'HASH', $actual_filename );
$this->assertEquals(
$expected_filename_pattern,
$actual_filename_pattern,
'Cacheable filename for ' . $url
);
}
}

0 comments on commit b712b00

Please sign in to comment.