Skip to content

Commit 5a2742d

Browse files
committed
refactor: prevent scripts from loading if not in a page with Give content
1 parent 19567e9 commit 5a2742d

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

includes/class-give-scripts.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public function init() {
6363
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_styles' ] );
6464
add_action( 'enqueue_block_editor_assets', [ $this, 'gutenberg_admin_scripts' ] );
6565
add_action( 'admin_head', [ $this, 'global_admin_head' ] );
66-
6766
} else {
6867
add_action( 'wp_enqueue_scripts', [ $this, 'public_enqueue_styles' ] );
6968
add_action( 'wp_enqueue_scripts', [ $this, 'public_enqueue_scripts' ] );
@@ -88,9 +87,13 @@ public static function register_script( $handle, $src, $dep = [], $ver = false )
8887
/**
8988
* Registers all plugin styles.
9089
*
90+
* @unreleased Prevent loading styles on non-Give pages.
9191
* @since 2.1.0
9292
*/
9393
public function register_styles() {
94+
if (!Give\Helpers\Frontend\Page::hasGiveContent()) {
95+
return;
96+
}
9497

9598
// Global WP-admin.
9699
wp_register_style( 'give-admin-global-styles', GIVE_PLUGIN_URL . 'build/assets/dist/css/admin-global' . $this->direction . '.css', [], GIVE_VERSION );
@@ -115,9 +118,14 @@ public function register_styles() {
115118
/**
116119
* Registers all plugin scripts.
117120
*
121+
* @unreleased Prevent loading scripts on non-Give pages.
118122
* @since 2.1.0
119123
*/
120124
public function register_scripts() {
125+
if (!Give\Helpers\Frontend\Page::hasGiveContent()) {
126+
return;
127+
}
128+
121129
// WP-Admin.
122130
EnqueueScript::make(
123131
'give-admin-scripts',
@@ -454,19 +462,29 @@ public function global_admin_head() {
454462
/**
455463
* Enqueues public styles.
456464
*
465+
* @unreleased Prevent loading styles on non-Give pages.
457466
* @since 2.1.0
458467
*/
459468
public function public_enqueue_styles() {
469+
if (!Give\Helpers\Frontend\Page::hasGiveContent()) {
470+
return;
471+
}
472+
460473
wp_enqueue_style( 'give-styles' );
461474
}
462475

463476

464477
/**
465478
* Enqueues public scripts.
466479
*
480+
* @unreleased Prevent loading scripts on non-Give pages.
467481
* @since 2.1.0
468482
*/
469483
public function public_enqueue_scripts() {
484+
if (!Give\Helpers\Frontend\Page::hasGiveContent()) {
485+
return;
486+
}
487+
470488
wp_enqueue_script( 'give' );
471489

472490
$this->public_localize_scripts();

includes/gateways/stripe/includes/give-stripe-scripts.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
/**
1717
* Load Frontend javascript
1818
*
19+
* @unreleased Prevent loading scripts on non-Give pages.
1920
* @since 2.5.0
2021
*
2122
* @return void
2223
*/
2324
function give_stripe_frontend_scripts() {
25+
if (!Give\Helpers\Frontend\Page::hasGiveContent()) {
26+
return;
27+
}
2428

2529
/**
2630
* Bailout, if Stripe account is not configured.

src/API/REST/V3/Entities/Actions/RegisterPublicEntities.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@
44

55
use Give\Framework\Support\Facades\Scripts\ScriptAsset;
66
use Give\Helpers\Language;
7+
use Give\Helpers\Frontend\Page;
78

89
/**
910
* @since 4.13.1
1011
*/
1112
class RegisterPublicEntities
1213
{
1314
/**
15+
* @unreleased Prevent loading scripts on non-Give pages.
1416
* @since 4.13.1
1517
*/
1618
public function __invoke()
1719
{
20+
if (!is_admin() && !Page::hasGiveContent()) {
21+
return;
22+
}
23+
1824
$handleName = 'givewp-entities-public';
1925
$scriptAsset = ScriptAsset::get(GIVE_PLUGIN_DIR . 'build/entitiesPublic.asset.php');
2026

src/Framework/DesignSystem/Actions/RegisterDesignSystemStyles.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22

33
namespace Give\Framework\DesignSystem\Actions;
44

5+
use Give\Helpers\Frontend\Page;
6+
57
class RegisterDesignSystemStyles
68
{
79
/**
10+
* @unreleased Prevent loading styles on non-Give pages.
811
* @since 2.26.0
912
*/
1013
public function __invoke()
1114
{
15+
if (!is_admin() && !Page::hasGiveContent()) {
16+
return;
17+
}
18+
1219
$version = file_get_contents(GIVE_PLUGIN_DIR . 'build/assets/dist/css/design-system/version');
1320

1421
wp_register_style(

0 commit comments

Comments
 (0)