Skip to content

gpi-waiting-list.php: Added waiting list message on order summary page. #1117

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions gp-inventory/gpi-waiting-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public function add_hooks() {

// Allow negative stock to be used for conditional logic validation.
add_filter( 'gpi_allow_negative_stock', array( $this, 'allow_negative_stock_for_conditional_logic' ), 10, 3 );

add_filter( 'gform_order_summary', array( $this, 'add_waiting_list_to_order_summary' ), 11, 3 );
}

public function is_applicable_form( $form ) {
Expand Down Expand Up @@ -270,6 +272,39 @@ public function allow_negative_stock_for_conditional_logic( $allow_negative_stoc
return true;
}

public function add_waiting_list_to_order_summary( $markup, $form, $entry ) {
if ( empty( $entry ) ) {
return $markup;
}

$products = GFCommon::get_product_fields( $form, $entry );
if ( empty( $products['products'] ) ) {
return $markup;
}

libxml_use_internal_errors( true );
$dom = new DOMDocument();
$dom->loadHTML( mb_convert_encoding( $markup, 'HTML-ENTITIES', 'UTF-8' ) );
Comment on lines +285 to +287
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Handle DOM parsing errors and consider markup preservation.

The DOMDocument parsing could fail or alter the original markup structure. Consider adding error handling and preserving the original markup format.

 libxml_use_internal_errors( true );
 $dom = new DOMDocument();
-$dom->loadHTML( mb_convert_encoding( $markup, 'HTML-ENTITIES', 'UTF-8' ) );
+$success = $dom->loadHTML( mb_convert_encoding( $markup, 'HTML-ENTITIES', 'UTF-8' ), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
+if ( ! $success ) {
+    return $markup; // Return original markup if parsing fails
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
libxml_use_internal_errors( true );
$dom = new DOMDocument();
$dom->loadHTML( mb_convert_encoding( $markup, 'HTML-ENTITIES', 'UTF-8' ) );
libxml_use_internal_errors( true );
$dom = new DOMDocument();
$success = $dom->loadHTML( mb_convert_encoding( $markup, 'HTML-ENTITIES', 'UTF-8' ), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
if ( ! $success ) {
return $markup; // Return original markup if parsing fails
}
🤖 Prompt for AI Agents
In gp-inventory/gpi-waiting-list.php around lines 285 to 287, the DOMDocument
loadHTML call lacks error handling and may alter the original markup. Add checks
to capture and handle any parsing errors from libxml after calling loadHTML, and
consider preserving the original markup by storing it before parsing or using
alternative methods that maintain the markup structure.


$xpath = new DOMXPath( $dom );
$productNodes = $xpath->query( "//div[contains(@class, 'product_name')]" );
foreach ( $productNodes as $node ) {
foreach ( $products['products'] as $product_id => $product ) {
if ( $product['name'] == $node->nodeValue ) {
$field = GFAPI::get_field( $form, $product_id );
$gpi_instance = gp_inventory_type_simple()::$type === rgar( $field, 'gpiInventory' ) ? gp_inventory_type_simple() : gp_inventory_type_advanced();
$available = (int) $gpi_instance->get_available_stock( $field );
if ( $available <= 0 ) {
$node->nodeValue .= $this->waitlist_message;
}
Comment on lines +294 to +299
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation for inventory field compatibility.

The code assumes the field is an inventory field but doesn't validate this before calling inventory methods, which could cause errors.

 $field        = GFAPI::get_field( $form, $product_id );
+
+// Validate that this is an inventory field
+if ( ! gp_inventory_type_simple()->is_applicable_field( $field ) && 
+     ! gp_inventory_type_advanced()->is_applicable_field( $field ) ) {
+    continue;
+}
+
 $gpi_instance = gp_inventory_type_simple()::$type === rgar( $field, 'gpiInventory' ) ? gp_inventory_type_simple() : gp_inventory_type_advanced();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$field = GFAPI::get_field( $form, $product_id );
$gpi_instance = gp_inventory_type_simple()::$type === rgar( $field, 'gpiInventory' ) ? gp_inventory_type_simple() : gp_inventory_type_advanced();
$available = (int) $gpi_instance->get_available_stock( $field );
if ( $available <= 0 ) {
$node->nodeValue .= $this->waitlist_message;
}
$field = GFAPI::get_field( $form, $product_id );
// Validate that this is an inventory field
if ( ! gp_inventory_type_simple()->is_applicable_field( $field ) &&
! gp_inventory_type_advanced()->is_applicable_field( $field ) ) {
continue;
}
$gpi_instance = gp_inventory_type_simple()::$type === rgar( $field, 'gpiInventory' )
? gp_inventory_type_simple()
: gp_inventory_type_advanced();
$available = (int) $gpi_instance->get_available_stock( $field );
if ( $available <= 0 ) {
$node->nodeValue .= $this->waitlist_message;
}
🤖 Prompt for AI Agents
In gp-inventory/gpi-waiting-list.php around lines 294 to 299, the code assumes
the field is an inventory field without validating it, which may cause errors
when calling inventory methods. Add a check to confirm the field is indeed an
inventory field before proceeding with inventory-related operations. If the
field is not compatible, skip the inventory logic to prevent potential errors.

break;
}
}
}

$markup = $dom->saveHTML();
return $markup;
Comment on lines +305 to +306
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Preserve original markup structure when returning modified HTML.

The saveHTML() method may add DOCTYPE and wrapper elements that weren't in the original markup, potentially breaking the layout.

-$markup = $dom->saveHTML();
-return $markup;
+// Get only the body content to preserve original structure
+$body = $dom->getElementsByTagName( 'body' )->item( 0 );
+if ( $body ) {
+    $markup = '';
+    foreach ( $body->childNodes as $child ) {
+        $markup .= $dom->saveHTML( $child );
+    }
+} else {
+    $markup = $dom->saveHTML();
+}
+return $markup;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$markup = $dom->saveHTML();
return $markup;
// Get only the body content to preserve original structure
$body = $dom->getElementsByTagName( 'body' )->item( 0 );
if ( $body ) {
$markup = '';
foreach ( $body->childNodes as $child ) {
$markup .= $dom->saveHTML( $child );
}
} else {
$markup = $dom->saveHTML();
}
return $markup;
🤖 Prompt for AI Agents
In gp-inventory/gpi-waiting-list.php around lines 305 to 306, the use of
$dom->saveHTML() returns the entire document including added DOCTYPE and wrapper
elements, which can alter the original markup structure and break layout. To fix
this, extract and return only the inner HTML of the specific element you
modified instead of the whole document. This preserves the original markup
structure without extra wrappers.

}
}

new GPI_Waiting_List();
Expand Down
Loading