Bug report | Cause of an eternal loop
-
Hi!
During the investigation of the site breakdowns, we found a bug (I suppose) in your plugin that causes the eternal loop.
Steps:
1. Add at least 2 filters on the page with different attributes (in our case it’s “topic” and “document-type”) but with same-named terms (in our case both “topic” and “document-type” have a term “specification”).
2. On the page select at least one term in a filter (not the duplicate one).
3. Now select the duplicate term in any of those filters.
(for example, there are two filters: Document Type and Topic, I select the “Posters” term under the “Document Type” filter and then select the “Specification” at the same filter)
4. Observe page is not loading and site breakdown.The reason is the part in the code nearby this https://plugins.trac.www.ads-software.com/browser/yith-woocommerce-ajax-navigation/tags/4.3.0/includes/widgets/class-yith-wcan-navigation-widget.php#L624 row (wp-content/plugins/yith-woocommerce-ajax-navigation/includes/widgets/class.yith-wcan-navigation-widget.php:624)
// All current filters. if ( $_chosen_attributes ) { foreach ( $_chosen_attributes as $name => $data ) { if ( $name !== $taxonomy ) { // Exclude query arg for current term archive term. while ( $in_array_function( $term->slug, $data['terms'] ) ) { $key = array_search( $current_term, $data ); unset( $data['terms'][ $key ] ); } // Remove pa_ and sanitize. $filter_name = urldecode( sanitize_title( str_replace( 'pa_', '', $name ) ) ); if ( ! empty( $data['terms'] ) ) { $link = add_query_arg( 'filter_' . $filter_name, implode( apply_filters( "yith_wcan_{$display_type}_filter_operator", ',', $display_type, $query_type ), $data['terms'] ), $link ); } if ( 'or' === $data['query_type'] ) { $link = add_query_arg( 'query_type_' . $filter_name, 'or', $link ); } } } }
At first, I can’t really understand, why you need to remove the term from the $_chosen attributes if this term with the same name but from the other taxonomy is already there, but this is not the cause of the bug, so it’s OK.
The cause is in the “while” loop you check this:
$in_array_function( $term->slug, $data['terms'] )
but after that you get the $key with other attributes:
$key = array_search( $current_term, $data );
where you search in the “$data” array instead of “$data[‘terms’]
and then you remove the item from “$data[‘terms’]” array by that key.$data = {array} [2] terms = {array} [2] 0 = "posters" 1 = "specification" query_type = "or"
In our case $current_term = “”, so the array_search() function returns “false” that PHP transforms into 0 here
unset( $data['terms'][ $key ] );
The 0 element in the array in the example from the steps above will be “posters”, it will be removed, but “unset” doesn’t change keys of the remaining elements in the array, so the “specification” element will not be removed, and the while loop never ends.
$data = {array} [2] terms = {array} [1] 1 = "specification" query_type = "or"
Please feel free to ask something, I hope the explanation above is clear.
For now, we changed the duplicate term’s name for one of the attributes so it’s not entered the loop.
- The topic ‘Bug report | Cause of an eternal loop’ is closed to new replies.