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

Scope module assets for calendar #564

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions common/php/class-module.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,15 @@ function filter_posts_link( $slug, $post_type = 'post' ) {
*/
function enqueue_datepicker_resources() {

// Add the first day of the week as an available variable to wp_head
echo "<script type=\"text/javascript\">var ef_week_first_day=\"" . get_option( 'start_of_week' ) . "\";</script>";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line was making the PHPUnit tests around enqueuing noisy, so moved it to use wp_add_inline_script


wp_enqueue_script( 'jquery-ui-datepicker' );

//Timepicker needs to come after jquery-ui-datepicker and jquery
wp_enqueue_script( 'edit_flow-timepicker', EDIT_FLOW_URL . 'common/js/jquery-ui-timepicker-addon.js', array( 'jquery', 'jquery-ui-datepicker' ), EDIT_FLOW_VERSION, true );
wp_enqueue_script( 'edit_flow-date_picker', EDIT_FLOW_URL . 'common/js/ef_date.js', array( 'jquery', 'jquery-ui-datepicker', 'edit_flow-timepicker' ), EDIT_FLOW_VERSION, true );

// Add the first day of the week as an available variable
wp_add_inline_script( 'edit_flow-date_picker', 'var ef_week_first_day = ' . wp_json_encode( get_option( 'start_of_week' ) ) . ';' );

// Now styles
wp_enqueue_style( 'jquery-ui-datepicker', EDIT_FLOW_URL . 'common/css/jquery.ui.datepicker.css', array( 'wp-jquery-ui-dialog' ), EDIT_FLOW_VERSION, 'screen' );
wp_enqueue_style( 'jquery-ui-theme', EDIT_FLOW_URL . 'common/css/jquery.ui.theme.css', false, EDIT_FLOW_VERSION, 'screen' );
Expand Down
54 changes: 32 additions & 22 deletions modules/calendar/calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function init() {

add_action( 'admin_init', array( $this, 'register_settings' ) );
add_action( 'admin_menu', array( $this, 'action_admin_menu' ) );
add_action( 'admin_print_styles', array( $this, 'add_admin_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );

// Ajax manipulation for the calendar
Expand Down Expand Up @@ -173,11 +173,12 @@ function action_admin_menu() {
*
* @uses wp_enqueue_style()
*/
function add_admin_styles() {
global $pagenow;
// Only load calendar styles on the calendar page
if ( $pagenow == 'index.php' && isset( $_GET['page'] ) && $_GET['page'] == 'calendar' )
wp_enqueue_style( 'edit-flow-calendar-css', $this->module_url . 'lib/calendar.css', false, EDIT_FLOW_VERSION );
function enqueue_admin_styles() {
if ( ! $this->is_calendar_view() ) {
return;
}

wp_enqueue_style( 'edit-flow-calendar-css', $this->module_url . 'lib/calendar.css', false, EDIT_FLOW_VERSION );
}

/**
Expand All @@ -188,24 +189,28 @@ function add_admin_styles() {
*/
function enqueue_admin_scripts() {

if ( ! $this->is_calendar_view() ) {
return;
}

$this->enqueue_datepicker_resources();

if ( $this->is_whitelisted_functional_view() ) {
$js_libraries = array(
'jquery',
'jquery-ui-core',
'jquery-ui-sortable',
'jquery-ui-draggable',
'jquery-ui-droppable',
);
foreach( $js_libraries as $js_library ) {
wp_enqueue_script( $js_library );
}
wp_enqueue_script( 'edit-flow-calendar-js', $this->module_url . 'lib/calendar.js', $js_libraries, EDIT_FLOW_VERSION, true );

$ef_cal_js_params = array( 'can_add_posts' => current_user_can( $this->create_post_cap ) ? 'true' : 'false' );
wp_localize_script( 'edit-flow-calendar-js', 'ef_calendar_params', $ef_cal_js_params );
$js_libraries = array(
'jquery',
'jquery-ui-core',
'jquery-ui-sortable',
'jquery-ui-draggable',
'jquery-ui-droppable',
);

foreach( $js_libraries as $js_library ) {
wp_enqueue_script( $js_library );
}

wp_enqueue_script( 'edit-flow-calendar-js', $this->module_url . 'lib/calendar.js', $js_libraries, EDIT_FLOW_VERSION, true );

$ef_cal_js_params = array( 'can_add_posts' => current_user_can( $this->create_post_cap ) ? 'true' : 'false' );
wp_localize_script( 'edit-flow-calendar-js', 'ef_calendar_params', $ef_cal_js_params );

}

Expand Down Expand Up @@ -1851,7 +1856,12 @@ public function fix_post_date_on_update_part_two( $post_ID, $post_after, $post_b
$wpdb->update( $wpdb->posts, array( 'post_date' => $post_date ), array( 'ID' => $post_ID ) );
clean_post_cache( $post_ID );
}


public function is_calendar_view() {
global $pagenow;

return ( 'index.php' === $pagenow && isset( $_GET['page'] ) && $_GET['page'] === 'calendar' );
}
} // EF_Calendar

} // class_exists('EF_Calendar')
100 changes: 100 additions & 0 deletions tests/test-edit-flow-calendar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

class WP_Test_Edit_Flow_Calendar extends WP_UnitTestCase {

protected static $admin_user_id;
protected $old_wp_scripts;

public static function wpSetUpBeforeClass( $factory ) {
global $edit_flow;

self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );

/**
* There's a capability's check in EF_Calendar `init` that will prevent embedding of assets
* and setting of the `create_post_cap` property, so we set that capability here
*
* @see https://github.com/Automattic/Edit-Flow/blob/b4430c5d652b1c87736c3980ab8cf032bf49b6ad/modules/calendar/calendar.php#L84
* @see https://github.com/Automattic/Edit-Flow/blob/b4430c5d652b1c87736c3980ab8cf032bf49b6ad/modules/calendar/calendar.php#L87
*/
$edit_flow->calendar->create_post_cap = 'edit_posts';

$edit_flow->calendar->install();
$edit_flow->calendar->init();
}

public static function wpTearDownAfterClass() {
self::delete_user( self::$admin_user_id );
}

function setUp() {
parent::setUp();

$this->old_wp_scripts = isset( $GLOBALS['wp_scripts'] ) ? $GLOBALS['wp_scripts'] : null;
remove_action( 'wp_default_scripts', 'wp_default_scripts' );
remove_action( 'wp_default_scripts', 'wp_default_packages' );
$GLOBALS['wp_scripts'] = new WP_Scripts();
$GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' );

global $pagenow;
$pagenow = 'post.php';
}

function tearDown() {
$GLOBALS['wp_scripts'] = $this->old_wp_scripts;
add_action( 'wp_default_scripts', 'wp_default_scripts' );

global $pagenow;
$pagenow = 'index.php';

parent::tearDown();
}

/**
* The calendar js should be enqueued on pages like post.php with a valid post type
*/
public function test_scripts_enqueued_calendar_admin() {
global $edit_flow, $pagenow, $wp_scripts;

set_current_screen( 'admin' );

wp_default_scripts( $wp_scripts );
wp_default_packages( $wp_scripts );

$pagenow = 'index.php';
$_GET['page'] = 'calendar';

wp_set_current_user( self::$admin_user_id );

$edit_flow->calendar->enqueue_admin_scripts();

$expected = "<script type='text/javascript' src='" . $edit_flow->calendar->module_url . 'lib/calendar.js?ver=' . EDIT_FLOW_VERSION . "'></script>";

$footer = get_echo( 'wp_print_footer_scripts' );

$this->assertContains( $expected, $footer );
}
/**
* The custom status js should not be enqueued on pages like admin.php
*/
public function test_scripts_not_enqueued_calendar_admin() {
global $edit_flow, $pagenow, $wp_scripts;

set_current_screen( 'admin' );

wp_default_scripts( $wp_scripts );
wp_default_packages( $wp_scripts );

$pagenow = 'admin.php';

wp_set_current_user( self::$admin_user_id );

$edit_flow->calendar->enqueue_admin_scripts();

$expected = "<script type='text/javascript' src='" . $edit_flow->calendar->module_url . 'lib/calendar.js?ver=' . EDIT_FLOW_VERSION . "'></script>";

$footer = get_echo( 'wp_print_footer_scripts' );

$this->assertNotContains( $expected, $footer );
}
}