Skip to content
Open
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
42 changes: 10 additions & 32 deletions includes/settings-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,8 @@ function rt_settings_init() {

/**
* Section Description callback.
*
* @param array $args arguments passed.
*/
function rt_scripts_optimizer_settings_callback( $args ) {
function rt_scripts_optimizer_settings_callback() {
?>
<p>
<?php esc_html_e( 'Add scripts to exclude from the RT Scripts Optimizer by providing it\'s handle or path.', 'RT_Script_Optimizer' ); ?>
Expand All @@ -134,10 +132,8 @@ function rt_scripts_optimizer_settings_callback( $args ) {

/**
* Field callback to accept handles to exclude.
*
* @param array $args arguments passed.
*/
function rt_scripts_optimizer_handles_field_callback( $args ) {
function rt_scripts_optimizer_handles_field_callback() {

// option value.
$handles = get_option( 'rt_scripts_optimizer_exclude_handles' );
Expand All @@ -160,10 +156,8 @@ function rt_scripts_optimizer_handles_field_callback( $args ) {

/**
* Field callback to accept handles of stylesheets to be dequeued when user not logged in.
*
* @param array $args arguments passed.
*/
function rt_scripts_optimizer_style_dequeue_non_logged_handles_callback( $args ) {
function rt_scripts_optimizer_style_dequeue_non_logged_handles_callback() {

// option value.
$paths = get_option( 'rt_scripts_optimizer_style_dequeue_non_logged_handles' );
Expand All @@ -186,10 +180,8 @@ function rt_scripts_optimizer_style_dequeue_non_logged_handles_callback( $args )

/**
* Field callback to accept handles of stylesheets to be loaded asynchronously.
*
* @param array $args arguments passed.
*/
function rt_scripts_optimizer_style_async_handles_callback( $args ) {
function rt_scripts_optimizer_style_async_handles_callback() {

// option value.
$paths = get_option( 'rt_scripts_optimizer_style_async_handles' );
Expand All @@ -212,10 +204,8 @@ function rt_scripts_optimizer_style_async_handles_callback( $args ) {

/**
* Field callback to accept handles of stylesheets to be loaded asynchronously on windows event.
*
* @param array $args arguments passed.
*/
function rt_scripts_optimizer_style_async_handles_onevent_callback( $args ) {
function rt_scripts_optimizer_style_async_handles_onevent_callback() {

// option value.
$paths = get_option( 'rt_scripts_optimizer_style_async_handles_onevent' );
Expand All @@ -238,10 +228,8 @@ function rt_scripts_optimizer_style_async_handles_onevent_callback( $args ) {

/**
* Field callback to take input of whether to include amp-boilerplate css or not.
*
* @param array $args arguments passed.
*/
function rt_scripts_optimizer_load_amp_boilerplate_style_callback( $args ) {
function rt_scripts_optimizer_load_amp_boilerplate_style_callback() {

// option value.
$load_amp_css = get_option( 'rt_scripts_optimizer_load_amp_boilerplate_style' );
Expand All @@ -259,10 +247,8 @@ function rt_scripts_optimizer_load_amp_boilerplate_style_callback( $args ) {

/**
* Field callback to take input of whether to skip all CSS concatination or not.
*
* @param array $args arguments passed.
*/
function rt_scripts_optimizer_skip_css_concatination_all_callback( $args ) {
function rt_scripts_optimizer_skip_css_concatination_all_callback() {

// option value.
$skip_css_concatination = get_option( 'rt_scripts_optimizer_skip_css_concatination_all' );
Expand All @@ -280,10 +266,8 @@ function rt_scripts_optimizer_skip_css_concatination_all_callback( $args ) {

/**
* Field callback to take input of stylesheet handles which are not to be concated.
*
* @param array $args arguments passed.
*/
function rt_scripts_optimizer_skip_css_concatination_handles_callback( $args ) {
function rt_scripts_optimizer_skip_css_concatination_handles_callback() {

// option value.
$handles = get_option( 'rt_scripts_optimizer_skip_css_concatination_handles' );
Expand All @@ -306,10 +290,8 @@ function rt_scripts_optimizer_skip_css_concatination_handles_callback( $args ) {

/**
* Field callback to accept page IDs where the plugin should be disabled.
*
* @param array $args arguments passed.
*/
function rt_scripts_optimizer_disabled_page_ids_callback( $args ) {
function rt_scripts_optimizer_disabled_page_ids_callback() {

// option value.
$page_ids = get_option( 'rt_scripts_optimizer_disabled_page_ids' );
Expand All @@ -333,10 +315,8 @@ function rt_scripts_optimizer_disabled_page_ids_callback( $args ) {

/**
* Field callback to accept paths of scripts to exclude.
*
* @param array $args arguments passed.
*/
function rt_scripts_optimizer_paths_field_callback( $args ) {
function rt_scripts_optimizer_paths_field_callback() {

// option value.
$paths = get_option( 'rt_scripts_optimizer_exclude_paths' );
Expand Down Expand Up @@ -372,10 +352,8 @@ function rt_scripts_optimizer_options_submenu() {
'rt-scripts-optimizer-settings',
'rt_scripts_optimizer_settings_template'
);

}


/**
* Top level menu callback function
*/
Expand Down
46 changes: 23 additions & 23 deletions rt-scripts-optimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,42 @@ function rt_scripts_optimizer_check_disabled_page_ids( $disable ) {
if ( $disable ) {
return $disable;
}

// Get the disabled page IDs setting.
$disabled_page_ids = get_option( 'rt_scripts_optimizer_disabled_page_ids', '' );

// If no disabled page IDs are set, return current status.
if ( empty( $disabled_page_ids ) ) {
return $disable;
}
// Get current page/post ID - get_queried_object_id() works reliably with 'wp' hook

// Get current page/post ID - get_queried_object_id() works reliably with 'wp' hook.
$current_id = get_queried_object_id();

// If no current ID found, return current status.
if ( ! $current_id ) {
return $disable;
}

// Convert CSV string to array and trim whitespace.
$disabled_ids_array = array_map( 'trim', explode( ',', $disabled_page_ids ) );

// Remove empty values and non-numeric values from array.
$disabled_ids_array = array_filter( $disabled_ids_array, function( $value ) {
return ! empty( $value ) && is_numeric( $value ) && intval( $value ) > 0;
} );

$disabled_ids_array = array_filter(
$disabled_ids_array,
function ( $value ) {
return ! empty( $value ) && is_numeric( $value ) && intval( $value ) > 0;
}
);

// Convert to integers for comparison.
$disabled_ids_array = array_map( 'intval', $disabled_ids_array );

// Check if current page ID is in the disabled list.
if ( in_array( $current_id, $disabled_ids_array, true ) ) {
return true;
}

return $disable;
}

Expand Down Expand Up @@ -101,28 +104,28 @@ function rt_scripts_optimizer_init() {
return;
}

// Register all hooks
// Register all hooks.
add_action( 'wp_head', 'rt_head_scripts', 0 );
add_action( 'wp_footer', 'rt_footer_scripts' );
add_filter( 'script_loader_tag', 'rt_scripts_handler', 10, 3 );
add_filter( 'style_loader_tag', 'load_async_styles', 10, 2 );
add_action( 'wp_footer', 'style_enqueue_script' );
add_action( 'wp_print_styles', 'dequeue_styles' );
add_filter( 'js_do_concat', '__return_false' );

if ( '1' === get_option( 'rt_scripts_optimizer_skip_css_concatination_all' ) ) {
add_filter( 'css_do_concat', '__return_false' );
} else {
add_filter( 'css_do_concat', 'skip_css_concatination', 10, 2 );
}

add_action( 'init', 'disable_emojis' );
add_action( 'wp_enqueue_scripts', 'rt_scripts_optimizer_load_scripts' );
add_action( 'the_content', 'rt_scripts_optimizer_iframe_lazy_loading', PHP_INT_MAX );
add_filter( 'render_block', 'rt_scripts_optimizer_modify_embeds', 10, 2 );
}

// Hook to 'wp' action - runs after post/page is loaded
// Hook to 'wp' action - runs after post/page is loaded.
add_action( 'wp', 'rt_scripts_optimizer_init' );

// Variable to store the scripts to be excluded.
Expand Down Expand Up @@ -232,8 +235,8 @@ function rt_scripts_handler( $tag, $handle, $src ) {
);

return $tag;

}

/**
* Loads the specified stylesheets asynchronously.
*
Expand Down Expand Up @@ -415,9 +418,7 @@ function disable_emojis_remove_dns_prefetch( $urls, $relation_type ) {
* Enqueues loadCSS scripts.
*/
function rt_scripts_optimizer_load_scripts() {

wp_enqueue_script( 'loadCSS', RT_SCRIPTS_OPTIMIZER_DIR_URL . '/assets/js/loadCSS.min.js', array(), filemtime( RT_SCRIPTS_OPTIMIZER_DIR_PATH . '/assets/js/loadCSS.min.js' ), false );

}

/**
Expand All @@ -440,7 +441,7 @@ function rt_scripts_optimizer_iframe_lazy_loading( $content ) {
);

foreach ( $iframes_to_lzay_load as $iframe_to_lzay_load ) {
$content = preg_replace( '~<iframe[^>]*\K (?=src=[^>]*('. $iframe_to_lzay_load .')[^>]*)~i', ' data-', $content );
$content = preg_replace( '~<iframe[^>]*\K (?=src=[^>]*(' . $iframe_to_lzay_load . ')[^>]*)~i', ' data-', $content );
}

return $content;
Expand All @@ -454,12 +455,11 @@ function rt_scripts_optimizer_iframe_lazy_loading( $content ) {
*/
function rt_scripts_optimizer_modify_embeds( $block_content, $block ) {

if ( 'core/embed' === $block['blockName'] && in_array( $block['attrs']['providerNameSlug'], [ 'reddit', 'twitter', 'instagram' ], true ) ) {
if ( 'core/embed' === $block['blockName'] && in_array( $block['attrs']['providerNameSlug'], array( 'reddit', 'twitter', 'instagram' ), true ) ) {

$block_content = preg_replace( '~<script~i', '<script type=\'text/rtscript-noautoload\'', $block_content );

}

return $block_content;

}