Skip to content

Commit be84a77

Browse files
authored
PLANET-7968: GF conditionally enqueue CSS files (#2818)
- GravityForms: conditionally enqueue its css/js files only where needed
1 parent f1c2819 commit be84a77

File tree

1 file changed

+159
-69
lines changed

1 file changed

+159
-69
lines changed

src/GravityFormsExtensions.php

Lines changed: 159 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ private function hooks(): void
125125
add_action('gform_post_payment_action', [ $this, 'check_stripe_payment_status' ], 10, 2);
126126
add_action('gform_pre_render', [$this, 'enqueue_share_buttons'], 10, 2);
127127
add_action('admin_enqueue_scripts', [$this, 'enqueue_gf_custom_scripts']);
128+
add_action('wp_enqueue_scripts', [$this, 'dequeue_gf_cripts'], 999);
129+
}
130+
131+
/**
132+
* Dequeue styles and scripts for pages that don't use GF forms.
133+
*
134+
*/
135+
public function dequeue_gf_cripts(): void
136+
{
137+
if (self::page_contains_gravity_form()) {
138+
return;
139+
}
140+
141+
wp_dequeue_style('gravity-forms-style');
128142
}
129143

130144
/**
@@ -931,75 +945,6 @@ public function p4_client_side_gravityforms_prefill($form): array|bool
931945
return $form;
932946
}
933947

934-
/**
935-
* Find all posts that contain a form.
936-
*
937-
* @param int $form_id The ID of the form to look for
938-
* @param array $post_types Post types that are searched
939-
*
940-
*/
941-
private function get_posts_by_gf_id(int $form_id, array $post_types = [ 'post', 'page' ]): array
942-
{
943-
$args = [
944-
's' => '<!-- wp:gravityforms/form ',
945-
"numberposts" => - 1,
946-
'post_type' => $post_types,
947-
'post_status' => [ 'publish', 'private' ],
948-
];
949-
950-
$posts = get_posts($args);
951-
952-
$post_ids = [];
953-
954-
foreach ($posts as $post) {
955-
$blocks = parse_blocks(get_the_content(null, null, $post));
956-
957-
$gf_blocks = $this->find_nested_blocks('gravityforms/form', $blocks);
958-
959-
foreach ($gf_blocks as $gf_block) {
960-
if (intval($gf_block['attrs']['formId']) === $form_id) {
961-
$post_ids[] = $post->ID;
962-
963-
break;
964-
}
965-
}
966-
}
967-
968-
return $post_ids;
969-
}
970-
971-
/**
972-
* Recursively search blocks array for block with $block_name and return as flat array.
973-
*
974-
* @param string $block_name Block name to find
975-
* @param array $blocks Blocks to search for nested blocks
976-
*
977-
*/
978-
private function find_nested_blocks(string $block_name, array $blocks): array
979-
{
980-
$matching_blocks = [];
981-
982-
foreach ($blocks as $block) {
983-
if (isset($block['blockName']) && $block['blockName'] === $block_name) {
984-
$matching_blocks[] = $block;
985-
}
986-
987-
if (!is_array($block['innerBlocks'])) {
988-
continue;
989-
}
990-
991-
$inner_blocks = $this->find_nested_blocks($block_name, $block['innerBlocks']);
992-
993-
if (count($inner_blocks) <= 0) {
994-
continue;
995-
}
996-
997-
$matching_blocks = array_merge($matching_blocks, $inner_blocks);
998-
}
999-
1000-
return $matching_blocks;
1001-
}
1002-
1003948
/**
1004949
* Redirect using Javascript after form submission instead of sending a header.
1005950
* Makes it possible to send tag manager events before redirecting.
@@ -1156,4 +1101,149 @@ public function p4_gf_hb_form_object_pre_save_feed($hs_form)
11561101

11571102
return $hs_form;
11581103
}
1104+
1105+
/**
1106+
* Check whether a page or post contains a GF form by its ID.
1107+
*
1108+
* @param int|null $post_id - The post or page ID.
1109+
*
1110+
*/
1111+
public function page_contains_gravity_form(?int $post_id = null): bool
1112+
{
1113+
$post_id = $post_id ?: get_the_ID();
1114+
if (!$post_id) {
1115+
return false;
1116+
}
1117+
1118+
$content = get_post_field('post_content', $post_id);
1119+
1120+
if (count($this->extract_shortcode_form_ids($content)) > 0) {
1121+
return true;
1122+
}
1123+
1124+
$blocks = parse_blocks($content);
1125+
1126+
return $this->walk_blocks_recursively($blocks, function ($block) {
1127+
return isset($block['blockName']) && $block['blockName'] === 'gravityforms/form';
1128+
});
1129+
}
1130+
1131+
/**
1132+
* Get all pages or posts using a specific GF form.
1133+
*
1134+
* @param int $form_id - The form ID.
1135+
* @param array $post_types - The post types to check.
1136+
*
1137+
*/
1138+
private function get_posts_by_gf_id(int $form_id, array $post_types = ['post', 'page']): array
1139+
{
1140+
$args = [
1141+
'numberposts' => -1,
1142+
'post_type' => $post_types,
1143+
'post_status' => ['publish', 'private'],
1144+
];
1145+
1146+
$posts = get_posts($args);
1147+
$matching = [];
1148+
1149+
foreach ($posts as $post) {
1150+
if (!$this->page_contains_form_id($post->ID, $form_id)) {
1151+
continue;
1152+
}
1153+
1154+
$matching[] = $post->ID;
1155+
}
1156+
1157+
return $matching;
1158+
}
1159+
1160+
/**
1161+
* Check whether a page or post uses a specific GF form.
1162+
*
1163+
* @param int $post_id - The post ID.
1164+
* @param int $form_id - The form ID.
1165+
*
1166+
*/
1167+
private function page_contains_form_id(int $post_id, int $form_id): bool
1168+
{
1169+
$content = get_post_field('post_content', $post_id);
1170+
1171+
if (in_array($form_id, $this->extract_shortcode_form_ids($content), true)) {
1172+
return true;
1173+
}
1174+
1175+
$blocks = parse_blocks($content);
1176+
1177+
return $this->walk_blocks_recursively($blocks, function ($block) use ($form_id) {
1178+
return
1179+
isset($block['blockName']) &&
1180+
$block['blockName'] === 'gravityforms/form' &&
1181+
isset($block['attrs']['formId']) &&
1182+
intval($block['attrs']['formId']) === $form_id
1183+
;
1184+
});
1185+
}
1186+
1187+
/**
1188+
* Get the form ID from the GF shortcode.
1189+
*
1190+
* @param string $content - The post content.
1191+
*
1192+
*/
1193+
private function extract_shortcode_form_ids(string $content): array
1194+
{
1195+
$form_ids = [];
1196+
1197+
if (strpos($content, '[gravityform') === false) {
1198+
return $form_ids;
1199+
}
1200+
1201+
preg_match_all('/\[gravityform[^\]]*id="(\d+)"[^\]]*\]/', $content, $matches);
1202+
1203+
foreach ($matches[1] as $id) {
1204+
$form_ids[] = intval($id);
1205+
}
1206+
1207+
return $form_ids;
1208+
}
1209+
1210+
/**
1211+
* Recursively walks blocks and executes a callback on each block.
1212+
*
1213+
* @param array $blocks - The list of blocks.
1214+
* @param callable $callback - The callback function to execute.
1215+
*
1216+
*/
1217+
private function walk_blocks_recursively(array $blocks, callable $callback): bool
1218+
{
1219+
foreach ($blocks as $block) {
1220+
if ($callback($block)) {
1221+
return true;
1222+
}
1223+
1224+
// Handle reusable blocks (core/block)
1225+
if ($block['blockName'] === 'core/block' && !empty($block['attrs']['ref'])) {
1226+
$ref_post = get_post($block['attrs']['ref']);
1227+
1228+
if ($ref_post) {
1229+
$ref_blocks = parse_blocks($ref_post->post_content);
1230+
1231+
if ($this->walk_blocks_recursively($ref_blocks, $callback)) {
1232+
return true;
1233+
}
1234+
}
1235+
}
1236+
1237+
// Walk inner blocks
1238+
if (empty($block['innerBlocks'])) {
1239+
continue;
1240+
}
1241+
1242+
if ($this->walk_blocks_recursively($block['innerBlocks'], $callback)) {
1243+
return true;
1244+
}
1245+
}
1246+
1247+
return false;
1248+
}
11591249
}

0 commit comments

Comments
 (0)