enqueue styles and scripts if only Gutenberg block is present
Resources have only been added if shortcode or widget hooks have been triggered before. If we switch to native Gutenberg block, none of the checks is true and no scripts are available.
This commit is contained in:
@ -80,11 +80,8 @@ class SCLiveticker {
|
|||||||
// Add shortcode.
|
// Add shortcode.
|
||||||
add_shortcode( 'liveticker', array( __CLASS__, 'shortcode_ticker_show' ) );
|
add_shortcode( 'liveticker', array( __CLASS__, 'shortcode_ticker_show' ) );
|
||||||
|
|
||||||
// Enqueue styles.
|
// Enqueue styles and JavaScript.
|
||||||
add_action( 'wp_footer', array( __CLASS__, 'enqueue_styles' ) );
|
add_action( 'wp_footer', array( __CLASS__, 'enqueue_resources' ) );
|
||||||
|
|
||||||
// Enqueue JavaScript.
|
|
||||||
add_action( 'wp_footer', array( __CLASS__, 'enqueue_scripts' ) );
|
|
||||||
|
|
||||||
// Add AJAX hook if configured.
|
// Add AJAX hook if configured.
|
||||||
if ( 1 === self::$options['enable_ajax'] ) {
|
if ( 1 === self::$options['enable_ajax'] ) {
|
||||||
@ -244,32 +241,15 @@ class SCLiveticker {
|
|||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Register frontend CSS.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public static function enqueue_styles() {
|
|
||||||
// Only add if shortcode is present.
|
|
||||||
if ( self::$shortcode_present || self::$widget_present ) {
|
|
||||||
wp_enqueue_style(
|
|
||||||
'wplt-css',
|
|
||||||
SCLIVETICKER_BASE . 'styles/liveticker.min.css',
|
|
||||||
'',
|
|
||||||
self::VERSION,
|
|
||||||
'all'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register frontend JS.
|
* Register frontend JS.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
|
* @since 1.1 Combined former methods "enqueue_styles" and "enqueue_scripts".
|
||||||
*/
|
*/
|
||||||
public static function enqueue_scripts() {
|
public static function enqueue_resources() {
|
||||||
// Only add if shortcode is present.
|
// Only add if shortcode is present.
|
||||||
if ( self::$shortcode_present || self::$widget_present ) {
|
if ( self::$shortcode_present || self::$widget_present || self::block_present() ) {
|
||||||
wp_enqueue_script(
|
wp_enqueue_script(
|
||||||
'scliveticker-js',
|
'scliveticker-js',
|
||||||
SCLIVETICKER_BASE . 'scripts/liveticker.min.js',
|
SCLIVETICKER_BASE . 'scripts/liveticker.min.js',
|
||||||
@ -288,6 +268,14 @@ class SCLiveticker {
|
|||||||
'poll_interval' => self::$options['poll_interval'] * 1000,
|
'poll_interval' => self::$options['poll_interval'] * 1000,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
wp_enqueue_style(
|
||||||
|
'sclt-css',
|
||||||
|
SCLIVETICKER_BASE . 'styles/liveticker.min.css',
|
||||||
|
'',
|
||||||
|
self::VERSION,
|
||||||
|
'all'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -458,4 +446,27 @@ class SCLiveticker {
|
|||||||
. '<span class="sclt-widget-title">' . $title . '</span>'
|
. '<span class="sclt-widget-title">' . $title . '</span>'
|
||||||
. '</li>';
|
. '</li>';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the Gutenberg block is present in current post.
|
||||||
|
*
|
||||||
|
* @return boolean True, if Gutenberg block is present.
|
||||||
|
* @since 1.1
|
||||||
|
*/
|
||||||
|
private static function block_present() {
|
||||||
|
// We are in WP 5.x environment and blocks are generally present.
|
||||||
|
if ( function_exists( 'has_blocks' ) && has_blocks() ) {
|
||||||
|
/*
|
||||||
|
* The slightly faster call to has_block( 'scliveticker/ticker' ) produces an "undefined function" error for
|
||||||
|
* no good reason. Iteration over pased blocks however works fine.
|
||||||
|
*/
|
||||||
|
foreach ( parse_blocks( get_post()->post_content ) as $b ) {
|
||||||
|
if ( 'scliveticker/ticker' === $b['blockName'] ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user