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

fix: only enqueue editor scripts on editor screen #337

Merged
merged 2 commits into from
Apr 1, 2020
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
114 changes: 0 additions & 114 deletions assets/js/admin/admin.js
Original file line number Diff line number Diff line change
@@ -1,114 +0,0 @@
/* global acf */

const { __, sprintf } = wp.i18n;
const { speak } = wp.a11y;
const daysInMonth = require( 'days-in-month' );

jQuery( document ).ready( function( $ ) {
const yearField = acf.getField( 'field_5e56ee8953584' );
const monthField = acf.getField( 'field_5e56eef559a76' );
const dayField = acf.getField( 'field_5e56f04ee5a20' );

/**
* Populate options for the publication day field that are appropriate
* to the selected year and month.
*/
const setupDays = () => {
const year = yearField.val();
const month = monthField.val();
if ( year && month ) {
const day = dayField.val();
const daySelect = $( `#acf-${dayField.data.key}` );
const dayCount = daysInMonth( year, month );
if ( day > dayCount ) {
const errorText = sprintf( __( 'The publication day has been reset as there are only %1$d days in the selected month, and your choice of %2$d is no longer valid.', 'coop-library-framework' ), dayCount, day );
dayField.showNotice( {
text: errorText,
type: 'warning',
dismiss: true,
} );
speak( errorText );
}
daySelect.children().not( '[value=""]' ).remove();
for ( let i = 1; i < dayCount + 1; i++ ) {
const option = document.createElement( 'option' );
const val = 9 > i ? `0${i}` : i;
option.setAttribute( 'value', val );
option.innerText = i;
daySelect.append( option );
}
if ( day ) {
dayField.val( day );
}
}
};

yearField.on( 'change', setupDays );
monthField.on( 'change', setupDays );

const $form = $( '#post' );
const $titleRow = $( '#titlewrap' );

/**
* Validate the form.
*
* @param {Event} event
*/
function validateForm( event ) {
const $errorFields = [];
let $firstError = null;
$( '.error' ).remove();

/**
* Add required flag to a form row.
*
* @param {jQuery} $row
*/
function addRequiredError( $row ) {
const $label = $row.find( 'label' );
const labelText = $label.text().replace( 'Add title', 'Title' );
$errorFields.push(
{ id: $label.attr( 'for' ), label: labelText, type: 'required' }
);
$row.addClass( 'form-invalid' );
/* translators: %s: The label of the required field. */
const errorText = sprintf( __( 'A %s is required.', 'coop-library-framework' ), labelText.toLowerCase() );
const error = $( `<p class="error">${errorText}</p>` );
$row.append( error );
$firstError = $firstError ? $firstError : $row;
}

if ( 0 === $titleRow.children( 'input' ).val().length ) {
addRequiredError( $titleRow );
}

if ( $firstError ) {
event.preventDefault();
$( '#validation-message' ).remove();
const errorMessage = __( 'The form contains errors:', 'coop-library-framework' );
const errorList = $errorFields.reduce( ( html, field, index ) => {
let errorText;
if ( 'required' === field.type ) {
/* translators: %s: The label of the required field. */
errorText = sprintf( __( 'A %s is required.', 'coop-library-framework' ), field.label.toLowerCase() );
}
if ( 0 < index ) {
return `${html}<li><a href="#${field.id}">${errorText}</a></li>`;
} else {
return `<li><a href="#${field.id}">${errorText}</a></li>`;
}
}, $errorFields[0] );
const errorContainer = $( '<div role="alert"></div>' );
const error = $(
`<div id="validation-message" class="notice notice-error">
<p>${errorMessage}</p>
<ul>${errorList}</ul>
</div>` );
$( '.wp-header-end' ).after( errorContainer );
$( errorContainer ).append( error );
}
return true;
}

$form.on( 'submit', validateForm );
} );
114 changes: 114 additions & 0 deletions assets/js/editor/editor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/* global acf */

const { __, sprintf } = wp.i18n;
const { speak } = wp.a11y;
const daysInMonth = require( 'days-in-month' );

jQuery( document ).ready( function( $ ) {
const yearField = acf.getField( 'field_5e56ee8953584' );
const monthField = acf.getField( 'field_5e56eef559a76' );
const dayField = acf.getField( 'field_5e56f04ee5a20' );

/**
* Populate options for the publication day field that are appropriate
* to the selected year and month.
*/
const setupDays = () => {
const year = yearField.val();
const month = monthField.val();
if ( year && month ) {
const day = dayField.val();
const daySelect = $( `#acf-${dayField.data.key}` );
const dayCount = daysInMonth( year, month );
if ( day > dayCount ) {
const errorText = sprintf( __( 'The publication day has been reset as there are only %1$d days in the selected month, and your choice of %2$d is no longer valid.', 'coop-library-framework' ), dayCount, day );
dayField.showNotice( {
text: errorText,
type: 'warning',
dismiss: true,
} );
speak( errorText );
}
daySelect.children().not( '[value=""]' ).remove();
for ( let i = 1; i < dayCount + 1; i++ ) {
const option = document.createElement( 'option' );
const val = 9 > i ? `0${i}` : i;
option.setAttribute( 'value', val );
option.innerText = i;
daySelect.append( option );
}
if ( day ) {
dayField.val( day );
}
}
};

yearField.on( 'change', setupDays );
monthField.on( 'change', setupDays );

const $form = $( '#post' );
const $titleRow = $( '#titlewrap' );

/**
* Validate the form.
*
* @param {Event} event
*/
function validateForm( event ) {
const $errorFields = [];
let $firstError = null;
$( '.error' ).remove();

/**
* Add required flag to a form row.
*
* @param {jQuery} $row
*/
function addRequiredError( $row ) {
const $label = $row.find( 'label' );
const labelText = $label.text().replace( 'Add title', 'Title' );
$errorFields.push(
{ id: $label.attr( 'for' ), label: labelText, type: 'required' }
);
$row.addClass( 'form-invalid' );
/* translators: %s: The label of the required field. */
const errorText = sprintf( __( 'A %s is required.', 'coop-library-framework' ), labelText.toLowerCase() );
const error = $( `<p class="error">${errorText}</p>` );
$row.append( error );
$firstError = $firstError ? $firstError : $row;
}

if ( 0 === $titleRow.children( 'input' ).val().length ) {
addRequiredError( $titleRow );
}

if ( $firstError ) {
event.preventDefault();
$( '#validation-message' ).remove();
const errorMessage = __( 'The form contains errors:', 'coop-library-framework' );
const errorList = $errorFields.reduce( ( html, field, index ) => {
let errorText;
if ( 'required' === field.type ) {
/* translators: %s: The label of the required field. */
errorText = sprintf( __( 'A %s is required.', 'coop-library-framework' ), field.label.toLowerCase() );
}
if ( 0 < index ) {
return `${html}<li><a href="#${field.id}">${errorText}</a></li>`;
} else {
return `<li><a href="#${field.id}">${errorText}</a></li>`;
}
}, $errorFields[0] );
const errorContainer = $( '<div role="alert"></div>' );
const error = $(
`<div id="validation-message" class="notice notice-error">
<p>${errorMessage}</p>
<ul>${errorList}</ul>
</div>` );
$( '.wp-header-end' ).after( errorContainer );
$( errorContainer ).append( error );
}
return true;
}

$form.on( 'submit', validateForm );
} );
2 changes: 0 additions & 2 deletions config/webpack.common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* global process, module, require */

const path = require( 'path' );
const CleanWebpackPlugin = require( 'clean-webpack-plugin' );
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
Expand Down
2 changes: 0 additions & 2 deletions config/webpack.dev.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* global module, require */

const merge = require( 'webpack-merge' );
const common = require( './webpack.common.js' );
const BrowserSyncPlugin = require( 'browser-sync-webpack-plugin' );
Expand Down
2 changes: 0 additions & 2 deletions config/webpack.prod.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* global module, require */

const merge = require( 'webpack-merge' );
const common = require( './webpack.common.js' );
const TerserPlugin = require( 'terser-webpack-plugin' );
Expand Down
3 changes: 1 addition & 2 deletions config/webpack.settings.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* global module */

// Webpack settings exports.
module.exports = {
entries: {
// JS files.
'admin': './assets/js/admin/admin.js',
'editor': './assets/js/editor/editor.js',
'frontend': './assets/js/frontend/frontend.js',
'shared': './assets/js/shared/shared.js',

Expand Down
34 changes: 26 additions & 8 deletions includes/functions/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function resource_init() {
array(
'admin_cols' => [
'title',
'resource-language' => [
'resource-language' => [
'title' => __( 'Resource Language', 'coop-library-framework' ),
'meta_key' => 'language',
'function' => function() {
Expand All @@ -89,19 +89,19 @@ function resource_init() {
'title' => __( 'Display Language', 'coop-library-framework' ),
'taxonomy' => 'language',
],
'format' => [
'format' => [
'title' => __( 'Format', 'coop-library-framework' ),
'taxonomy' => 'lc_format',
],
'topic' => [
'topic' => [
'title' => __( 'Topics', 'coop-library-framework' ),
'taxonomy' => 'lc_topic',
],
'published' => [
'published' => [
'title' => __( 'Date Published', 'coop-library-framework' ),
'meta_key' => 'lc_resource_publication_date',
],
'added' => [
'added' => [
'title' => __( 'Date Added', 'coop-library-framework' ),
'post_field' => 'post_date',
'default' => 'DESC',
Expand Down Expand Up @@ -593,7 +593,7 @@ function deactivate() {
* @return array
*/
function get_enqueue_contexts() {
return [ 'admin', 'frontend', 'shared' ];
return [ 'admin', 'editor', 'frontend', 'shared' ];
}

/**
Expand Down Expand Up @@ -655,9 +655,11 @@ function scripts() {
/**
* Enqueue scripts for admin.
*
* @param string $hook_suffix The suffix for the current page.
*
* @return void
*/
function admin_scripts() {
function admin_scripts( $hook_suffix ) {
wp_enqueue_script(
'coop_library_framework_shared',
script_url( 'shared', 'shared' ),
Expand All @@ -669,10 +671,26 @@ function admin_scripts() {
wp_enqueue_script(
'coop_library_framework_admin',
script_url( 'admin', 'admin' ),
[ 'wp-a11y', 'wp-i18n' ],
[],
COOP_LIBRARY_FRAMEWORK_VERSION,
true
);

$cpt = 'lc_resource';

if ( in_array( $hook_suffix, array( 'post.php', 'post-new.php' ), true ) ) {
$screen = get_current_screen();

if ( is_object( $screen ) && $cpt === $screen->post_type ) {
wp_enqueue_script(
'coop_library_framework_editor',
script_url( 'editor', 'editor' ),
[ 'wp-a11y', 'wp-i18n' ],
COOP_LIBRARY_FRAMEWORK_VERSION,
true
);
}
}
}

/**
Expand Down
22 changes: 0 additions & 22 deletions includes/functions/internationalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,6 @@ function setup() {
$n = function( $function ) {
return __NAMESPACE__ . "\\$function";
};

add_filter( 'pll_copy_post_metas', $n( 'copy_post_metas' ) );
}

/**
* Determine which post meta should be synchronized between translations.
*
* @param array $metas The array of metadata keys to synchronize.
*/
function copy_post_metas( $metas ) {
$unset = [
// Link will differ for the translated version.
'lc_resource_permanent_link',
// Translator will be specific to the translated version.
'lc_resource_translator',
];
foreach ( $metas as $key => $value ) {
if ( in_array( $value, $unset, true ) ) {
unset( $metas[ $key ] );
}
}
return $metas;
}

/**
Expand Down
9 changes: 0 additions & 9 deletions tests/phpunit/test-tools/Internationalization_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ class Internationalization_Tests extends Base\TestCase {
'functions/internationalization.php'
];

public function test_copy_post_metas() {
$meta_keys_to_copy = copy_post_metas( [
0 => 'lc_resource_permanent_link',
1 => 'lc_resource_publisher_name',
] );

$this->assertNotContains( 'lc_resource_permanent_link', $meta_keys_to_copy );
}

public function test_get_country_list() {
$countries_de = get_country_list( 'de_DE' );
$this->assertEquals( $countries_de['CA'], 'Kanada' );
Expand Down