[GF-3978] fix: stop corrupt sidebar data from causing errors (#56)

* fix: stop corrupt sidebar data from causing errors

By filtering out sidebars without a name in get_sidebars.

The name is a required field when entering new sidebars,
so sidebars missing a name are likely corrupt.

Designed to address "Cannot access offset of type string on string"
when trying to access $sidebar['name'] for sidebars without a name.

See https://wordpress.org/support/topic/php-8-2-error-6/.

* chore: formatting
This commit is contained in:
Nick Cernis
2024-03-06 09:27:45 +01:00
committed by GitHub
parent 730d22e4d5
commit 0552098a50
@@ -15,7 +15,7 @@ class Genesis_Simple_Sidebars_Core {
/** /**
* The created sidebars. * The created sidebars.
* *
* @var string * @var array
*/ */
private $sidebars; private $sidebars;
@@ -161,13 +161,23 @@ class Genesis_Simple_Sidebars_Core {
*/ */
public function get_sidebars( $cache = true ) { public function get_sidebars( $cache = true ) {
if ( ! $cache ) { if ( $cache && ! is_null( $this->sidebars ) ) {
return stripslashes_deep( get_option( Genesis_Simple_Sidebars()->settings_field ) ); return $this->sidebars;
} }
if ( is_null( $this->sidebars ) ) { $sidebars = stripslashes_deep( get_option( Genesis_Simple_Sidebars()->settings_field ) );
$this->sidebars = stripslashes_deep( get_option( Genesis_Simple_Sidebars()->settings_field ) );
} /**
* Sidebars without a name are invalid. Filtering these prevents
* corrupted sidebar data from throwing errors, as seen in:
* https://wordpress.org/support/topic/php-8-2-error-6/#post-17472734
*/
$this->sidebars = array_filter(
(array) $sidebars,
function( $sidebar ) {
return isset( $sidebar['name'] );
}
);
return $this->sidebars; return $this->sidebars;