Home>Support>$widget_info is uncleared after passing it by reference to a loop

$widget_info is uncleared after passing it by reference to a loop

Hi,

It is a fantastic plugin, thank you for it.

I have a problem. The $panels_data is changed unexpectedly, because a variable passed by reference to a foreach loop and the variable is not cleared (unset) after the loop. This causes that in another loop the same variable changes the $panels_data variable unexpectedly. The problem is described below.
The problem can be solved by inserting the following unset line after line 1009 of siteorigin-panels.php (after the closing tag of the $panels_data[‘widgets’] loop):

unset( $widget_info );

Page Builder Version: 2.4.19

DETAILED DESCRIPTION:

I have a function that is added to siteorigin_panels_row_cell_attributes filter. The value of $panels_data parameter that my function get (this is the second parameter of this filter) was not the same than in the previously called siteorigin_panels_data filter.

The last element of $panels_data[‘widgets’] array are changed, but “nobody” want to changed it.

When I debug the problem, I found that the content of the $panels_data variable is changed at this line:

foreach ( $widgets as $pi => $widget_info ) {

(line 1127 of siteorigin-panels.php in siteorigin_panels_render function)

It seemed to be very strange, how can this foreach loop line change the $panels_data!?

Then I realized that in another line the same $widget_info variable is applied, and the parameter is passed by reference to a foreach loop:

foreach ( $panels_data['widgets'] as $wid => &$widget_info ) { 

(line 997 of siteorigin-panels.php in siteorigin_panels_render function)

The $widget_info parameter is not “unseted” after the loop in line 997, that is why it causes the problem in the loop using the same variable in the loop of line 1127.

Here is a description about the same situation:
https://coderwall.com/p/qx3fpa/php-foreach-pass-by-reference-do-it-right-or-better-not-at-all

SUGGESTED SOLUTION:
Please add the following line below the end of first loop after line 1009 of siteorigin-panels.php:

unset( $widget_info );

Here is the whole code:

	// Filter the widgets to add indexes
	if ( !empty( $panels_data['widgets'] ) ) {
		$last_gi = 0;
		$last_ci = 0;
		$last_wi = 0;
		foreach ( $panels_data['widgets'] as $wid => &$widget_info ) {

			if ( $widget_info['panels_info']['grid'] != $last_gi ) {
				$last_gi = $widget_info['panels_info']['grid'];
				$last_ci = 0;
				$last_wi = 0;
			}
			elseif ( $widget_info['panels_info']['cell'] != $last_ci ) {
				$last_ci = $widget_info['panels_info']['cell'];
				$last_wi = 0;
			}
			$widget_info['panels_info']['cell_index'] = $last_wi++;
		}
		unset( $widget_info ); //SUGGESTED ADDITIONAL LINE
	}              

I have tested it, it solves the problem, the last widget element of panels data will not be changed unexpectedly.

Thanks in advance,

Peter

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 7 years, 10 months ago Alex S
    Hi, I Work Here

    Hi Peter,

    Thank you for the very detailed bug report and solution! :)

    Can you please submit a pull request for SiteOrigin Page Builder with your solution? This can be done via our GitHub Repo? If you’re not familiar with doing this or you would rather not, I can make the required PR for you if you would prefer – just let me know mate. :)

Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More