ze_css_delivery_method' !== $field_args['id'] ) {
return $field_args;
}
$value = '';
if ( (bool) $this->options->get( 'async_css', 0 ) ) {
$value = 'async_css';
}
if ( (bool) $this->options->get( 'remove_unused_css', 0 ) ) {
$value = 'remove_unused_css';
}
$field_args['value'] = $value;
return $field_args;
}
/**
* Displays the RUCSS currently processing notice
*
* @since 3.11
*
* @return void
*/
public function display_processing_notice() {
if ( $this->has_saas_error_notice() ) {
return;
}
if ( ! $this->can_display_notice() ) {
return;
}
if ( ! $this->used_css->exists() ) {
return;
}
$transient = get_transient( 'rocket_rucss_processing' );
if ( false === $transient ) {
return;
}
$current_time = time();
if ( $transient < $current_time ) {
return;
}
$remaining = $transient - $current_time;
$message = sprintf(
// translators: %1$s = plugin name, %2$s = number of seconds.
__( '%1$s: Please wait %2$s seconds. The Remove Unused CSS service is processing your pages.', 'rocket' ),
'WP Rocket',
'' . $remaining . ''
);
rocket_notice_html(
[
'status' => 'info',
'message' => $message,
'id' => 'rocket-notice-rucss-processing',
]
);
}
/**
* Displays the RUCSS success notice
*
* @since 3.11
*
* @return void
*/
public function display_success_notice() {
if ( ! $this->can_display_notice() ) {
return;
}
if ( ! $this->used_css->exists() ) {
return;
}
if ( $this->has_saas_error_notice() ) {
return;
}
$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
if ( in_array( 'rucss_success_notice', (array) $boxes, true ) ) {
return;
}
$transient = get_transient( 'rocket_rucss_processing' );
$class = '';
if ( false !== $transient ) {
$class = 'hidden';
}
$message = sprintf(
// translators: %1$s = plugin name, %2$s = number of URLs, %3$s = number of seconds.
__( '%1$s: The Used CSS of your homepage has been processed. WP Rocket will continue to generate Used CSS for up to %2$s URLs per %3$s second(s).', 'rocket' ),
'WP Rocket',
apply_filters( 'rocket_rucss_pending_jobs_cron_rows_count', 100 ),
apply_filters( 'rocket_rucss_pending_jobs_cron_interval', MINUTE_IN_SECONDS )
);
if ( ! $this->options->get( 'manual_preload', 0 ) ) {
$message .= ' ' . sprintf(
// translators: %1$s = opening link tag, %2$s = closing link tag.
__( 'We suggest enabling %1$sPreload%2$s for the fastest results.', 'rocket' ),
'',
''
);
}
$beacon = $this->beacon->get_suggest( 'async_opti' );
$message .= '
' . sprintf(
// translators: %1$s = opening link tag, %2$s = closing link tag.
__( 'To learn more about the process check our %1$sdocumentation%2$s.', 'rocket' ),
'',
''
);
rocket_notice_html(
[
'message' => $message,
'dismissible' => $class,
'id' => 'rocket-notice-rucss-success',
'dismiss_button' => 'rucss_success_notice',
'dismiss_button_class' => 'button-primary',
]
);
}
/**
* Checks if we can display the RUCSS notices
*
* @param bool $check_enabled check if RUCSS is enabled.
*
* @since 3.11
*
* @return bool
*/
private function can_display_notice( $check_enabled = true ): bool {
$screen = get_current_screen();
if ( ! rocket_direct_filesystem()->is_writable( rocket_get_constant( 'WP_ROCKET_USED_CSS_PATH' ) ) ) {
return false;
}
if (
isset( $screen->id )
&&
'settings_page_wprocket' !== $screen->id
) {
return false;
}
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return false;
}
if ( $check_enabled && ! $this->is_enabled() ) {
return false;
}
return true;
}
/**
* Adds the notice end time to WP Rocket localize script data
*
* @since 3.11
*
* @param array $data Localize script data.
*
* @return array
*/
public function add_localize_script_data( $data ): array {
if ( ! is_array( $data ) ) {
$data = (array) $data;
}
if ( ! $this->is_enabled() ) {
return $data;
}
$transient = get_transient( 'rocket_rucss_processing' );
if ( false === $transient ) {
return $data;
}
$data['notice_end_time'] = $transient;
return $data;
}
/**
* Disable combine CSS option when RUCSS is enabled
*
* @since 3.11
*
* @param array $value The new, unserialized option value.
* @param array $old_value The old option value.
*
* @return array
*/
public function maybe_disable_combine_css( $value, $old_value ): array {
if ( ! isset( $value['remove_unused_css'], $value['minify_concatenate_css'] ) ) {
return $value;
}
if (
0 === $value['minify_concatenate_css']
||
0 === $value['remove_unused_css']
) {
return $value;
}
if (
isset( $old_value['remove_unused_css'], $old_value['minify_concatenate_css'] )
&&
$value['remove_unused_css'] === $old_value['remove_unused_css']
&&
$value['minify_concatenate_css'] === $old_value['minify_concatenate_css']
&&
0 === $old_value['minify_concatenate_css']
) {
return $value;
}
$value['minify_concatenate_css'] = 0;
return $value;
}
/**
* Disables combine CSS if RUCSS is enabled when updating to 3.11
*
* @since 3.11
*
* @param string $old_version Previous plugin version.
*
* @return void
*/
public function set_option_on_update( $old_version ) {
if ( version_compare( $old_version, '3.11', '>=' ) ) {
return;
}
$options = get_option( 'wp_rocket_settings', [] );
if ( 'local' === wp_get_environment_type() ) {
$options['optimize_css_delivery'] = 0;
$options['remove_unused_css'] = 0;
$options['async_css'] = 0;
}
if (
isset( $options['remove_unused_css'] )
&&
1 === (int) $options['remove_unused_css']
) {
$options['minify_concatenate_css'] = 0;
}
update_option( 'wp_rocket_settings', $options );
}
/**
* Updates safelist items for new SaaS compatibility
*
* @since 3.11.0.2
*
* @param string $old_version Previous plugin version.
*
* @return void
*/
public function update_safelist_items( $old_version ) {
if ( version_compare( $old_version, '3.11.0.2', '>=' ) ) {
return;
}
$options = get_option( 'wp_rocket_settings', [] );
if ( empty( $options['remove_unused_css_safelist'] ) ) {
return;
}
foreach ( $options['remove_unused_css_safelist'] as $key => $value ) {
if ( str_contains( $value, '.css' ) ) {
continue;
}
if ( str_starts_with( $value, '(' ) ) {
continue;
}
$options['remove_unused_css_safelist'][ $key ] = '(.*)' . $value;
}
update_option( 'wp_rocket_settings', $options );
}
/**
* Display a notification on wrong license.
*
* @return void
*/
public function display_wrong_license_notice() {
if ( ! $this->can_display_notice( false ) ) {
return;
}
$main_message = __( "We couldn't generate the used CSS because you're using a nulled version of WP Rocket. You need an active license to use the Remove Unused CSS feature and further improve your website's performance.", 'rocket' );
$cta_message = sprintf(
// translators: %1$s = promo percentage.
__( 'Click here to get a WP Rocket single license at %1$s off!', 'rocket' ),
'10%%'
);
$message = sprintf(
// translators: %1$s = plugin name, %2$s = opening anchor tag, %3$s = closing anchor tag.
"%1\$s:
$main_message
%2\$s$cta_message%3\$s", 'WP Rocket', '', '' ); rocket_notice_html( [ 'status' => 'error', 'dismissible' => '', 'message' => $message, 'id' => 'rocket-notice-rucss-wrong-licence', ] ); } /** * Display an error notice when the connection to the server fails * * @return void */ public function display_saas_error_notice() { if ( ! $this->has_saas_error_notice() ) { $boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true ); if ( in_array( 'rucss_saas_error_notice', (array) $boxes, true ) ) { unset( $boxes['rucss_saas_error_notice'] ); update_user_meta( get_current_user_id(), 'rocket_boxes', $boxes ); } return; } if ( ! $this->can_display_notice() ) { return; } $boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true ); if ( in_array( 'rucss_error_notice', (array) $boxes, true ) ) { return; } $firewall_beacon = $this->beacon->get_suggest( 'rucss_firewall_ips' ); $main_message = sprintf( // translators: %1$s = open tag, %2$s = closing tag. __( 'It seems a security plugin or the server\'s firewall prevents WP Rocket from accessing the Remove Unused CSS generator. IPs listed %1$shere in our documentation%2$s should be added to your allowlists:', 'rocket' ), '', '' ); $security_message = __( '- In the security plugin, if you are using one', 'rocket' ); $firewall_message = __( "- In the server's firewall. Your host can help you with this", 'rocket' ); $message = "WP Rocket: $main_message