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

PartyTown Integration #987

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions FeatureShowcase_Plugin_Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,22 @@ private static function get_cards() {

return array(
'new' => array(
'partytown' => array(
'title' => esc_html__( 'PartyTown', 'w3-total-cache' ),
'icon' => 'dashicons-performance',
'text' => esc_html__( 'This feature allows you to optimize third-party scripts by offloading them to web workers using PartyTown. It significantly improves your site\'s performance by moving heavy JavaScript tasks off the main thread.', 'w3-total-cache' ),
'button' => '<button class="button" onclick="window.location=\'' . (
UserExperience_PartyTown_Extension::is_enabled() ?
esc_url( Util_Ui::admin_url( 'admin.php?page=w3tc_userexperience#partytown' ) ) :
esc_url( Util_Ui::admin_url( 'admin.php?page=w3tc_general#userexperience' ) )
) . '\'">' .
__( 'Settings', 'w3-total-cache' ) . '</button>',
'link' => '<a target="_blank" href="' . esc_url( 'https://www.boldgrid.com/support/w3-total-cache/partytown/?utm_source=w3tc&utm_medium=feature_showcase&utm_campaign=partytown' ) .
'">' . __( 'More info', 'w3-total-cache' ) . '<span class="dashicons dashicons-external"></span></a>',
'is_premium' => true,
'is_new' => true,
'version' => 'X.X.X',
),
'alwayscached' => array(
'title' => esc_html__( 'Always Cached', 'w3-total-cache' ),
'icon' => 'dashicons-yes',
Expand Down
1 change: 1 addition & 0 deletions Generic_Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ function ob_callback( $buffer ) {
'swarmify',
'lazyload',
'removecssjs',
'partytown',
'deferscripts',
'minify',
'newrelic',
Expand Down
1 change: 1 addition & 0 deletions Root_Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ private function get_handlers() {
new Cdn_Environment(),
new Extension_ImageService_Environment(),
new Extension_AlwaysCached_Environment(),
new UserExperience_PartyTown_Environment(),
);

return $a;
Expand Down
34 changes: 34 additions & 0 deletions UserExperience_GeneralPage_View.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,40 @@
)
);

Util_Ui::config_item_extension_enabled(
array(
'extension_id' => 'user-experience-partytown',
'checkbox_label' => esc_html__( 'Enable PartyTown', 'w3-total-cache' ),
'description' => __(
'This feature allows you to optimize third-party scripts by offloading them to web workers using PartyTown.',
'w3-total-cache'
) . (
UserExperience_PartyTown_Extension::is_enabled()
? wp_kses(
sprintf(
// translators: 1 opening HTML a tag to W3TC User Experience page, 2 closing HTML a tag.
__(
' Settings can be found on the %1$sUser Experience page%2$s.',
'w3-total-cache'
),
'<a href="' . Util_Ui::admin_url( 'admin.php?page=w3tc_userexperience#partytown' ) . '">',
'</a>'
),
array(
'a' => array(
'href' => array(),
),
)
)
: ''
),
'label_class' => 'w3tc_single_column',
'pro' => true,
'disabled' => ! Util_Environment::is_w3tc_pro( $config ) ? true : false,
'show_learn_more' => true,
)
);

Util_Ui::config_item_extension_enabled(
array(
'extension_id' => 'user-experience-preload-requests',
Expand Down
182 changes: 182 additions & 0 deletions UserExperience_PartyTown_Environment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php
/**
* File: UserExperience_PartyTown_Environment.php
*
* @since X.X.X
*
* @package W3TC
*/

namespace W3TC;

/**
* Class: UserExperience_PartyTown_Environment
*/
class UserExperience_PartyTown_Environment {
/**
* Fixes environment in each wp-admin request.
*
* @since X.X.X
*
* @param Config $config Configuration.
* @param bool $force_all_checks Force all checks.
*
* @throws Util_Environment_Exceptions Exceptions.
*/
public function fix_on_wpadmin_request( $config, $force_all_checks ) {
$exs = new Util_Environment_Exceptions();

if ( $config->get_boolean( 'config.check' ) || $force_all_checks ) {
if ( UserExperience_PartyTown_Extension::is_enabled() ) {
try {
$this->rules_add( $config, $exs );
} catch ( Util_WpFile_FilesystemOperationException $ex ) {
$exs->push( $ex );
}
} else {
$this->rules_remove( $exs );
}
}

if ( count( $exs->exceptions() ) > 0 ) {
throw $exs;
}
}

/**
* Fixes environment once event occurs.
*
* @since X.X.X
*
* @param Config $config Config object.
* @param mixed $event Event.
* @param Config $old_config Old config object.
*/
public function fix_on_event( $config, $event, $old_config = null ) {
}

/**
* Fixes environment after plugin deactivation
*
* @since X.X.X
*
* @throws Util_Environment_Exceptions Exceptions.
*/
public function fix_after_deactivation() {
$exs = new Util_Environment_Exceptions();

$this->rules_remove( $exs );

if ( count( $exs->exceptions() ) > 0 ) {
throw $exs;
}
}

/**
* Returns required rules for module.
*
* @since X.X.X
*
* @param Config $config Configuration object.
* @return array
*/
public function get_required_rules( $config ) {
return array(
array(
'filename' => Util_Rule::get_browsercache_rules_cache_path(),
'content' => $this->rules_generate(),
),
);
}

/**
* Write rewrite rules.
*
* @since X.X.X
*
* @param Config $config Configuration.
* @param Util_Environment_Exceptions $exs Exceptions.
*
* @throws Util_WpFile_FilesystemOperationException S/FTP form if it can't get the required filesystem credentials.
*/
private function rules_add( $config, $exs ) {
Util_Rule::add_rules(
$exs,
Util_Rule::get_browsercache_rules_cache_path(),
$this->rules_generate(),
W3TC_MARKER_BEGIN_PARTYTOWN,
W3TC_MARKER_END_PARTYTOWN,
array(
W3TC_MARKER_BEGIN_BROWSERCACHE_CACHE => 0,
W3TC_MARKER_BEGIN_WORDPRESS => 0,
)
);
}

/**
* Generate rewrite rules.
*
* @since X.X.X
*
* @see Dispatcher::nginx_rules_for_browsercache_section()
*
* @return string
*/
private function rules_generate() {
$config = Dispatcher::config();

switch ( true ) {
case Util_Environment::is_apache():
case Util_Environment::is_litespeed():
return '
# BEGIN W3TC PartyTown
# Serve all Partytown JavaScript files with the correct MIME type
<FilesMatch "partytown-.*\.js$">
ForceType application/javascript
</FilesMatch>

' . ( $config->get_boolean( array( 'user-experience-partytown', 'atomics' ) ) ? '
Header set Cross-Origin-Opener-Policy "same-origin"
Header set Cross-Origin-Embedder-Policy "require-corp"
' : '' ) . '
# END W3TC PartyTown
';

case Util_Environment::is_nginx():
return '
# BEGIN W3TC PartyTown
# Serve the actual PartyTown JavaScript files with the correct MIME type
location ~ ^' . preg_quote( plugin_dir_path( __FILE__ ) . '/lib/PartyTown/lib/' ) . 'partytown-.*\.js$ {
default_type application/javascript;
}

' . ( $config->get_boolean( array( 'user-experience-partytown', 'atomics' ) ) ? '
add_header Cross-Origin-Opener-Policy same-origin;
add_header Cross-Origin-Embedder-Policy require-corp;
' : '' ) . '
# END W3TC PartyTown
';

default:
return '';
}
}

/**
* Removes cache directives
*
* @since X.X.X
*
* @param Util_Environment_Exceptions $exs Exceptions.
*
* @throws Util_WpFile_FilesystemOperationException S/FTP form if it can't get the required filesystem credentials.
*/
private function rules_remove( $exs ) {
Util_Rule::remove_rules(
$exs,
Util_Rule::get_browsercache_rules_cache_path(),
W3TC_MARKER_BEGIN_PARTYTOWN,
W3TC_MARKER_END_PARTYTOWN
);
}
}
Loading