Skip to content
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

Fixing the Uncaught TypeError in ListingRepository.php when using PHP 8 #334

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shapeyourbits
Copy link

Uncaught TypeError: Cannot access offset of type string on string in /wp-content/plugins/wp-nested-pages/app/Entities/Listing/ListingRepository.php

Uncaught TypeError: Cannot access offset of type string on string in /wp-content/plugins/wp-nested-pages/app/Entities/Listing/ListingRepository.php
@shapeyourbits
Copy link
Author

shapeyourbits commented Feb 1, 2023

Fatal Error using PHP 8.

Uncaught TypeError: Cannot access offset of type string on string in /home/wpsitedirectory/wp-content/plugins/wp-nested-pages/app/Entities/Listing/ListingRepository.php:40

Stack trace:
#0 /home/wpsitedirectory/wp-content/plugins/wp-nested-pages/app/Entities/Listing/Listing.php(317): NestedPages\Entities\Listing\ListingRepository->visiblePages('page')
#1 /home/wpsitedirectory/wp-content/plugins/wp-nested-pages/app/Entities/Listing/Listing.php(392): NestedPages\Entities\Listing\Listing->listOpening(Array, 0)
#2 /home/wpsitedirectory/wp-content/plugins/wp-nested-pages/app/Entities/Listing/Listing.php(369): NestedPages\Entities\Listing\Listing->listPostLevel()
#3 /home/wpsitedirectory/wp-content/plugins/wp-nested-pages/app/Views/listing.php(98): NestedPages\Entities\Listing\Listing->getPosts()
#4 /home/wpsitedirectory/wp-content/plugins/wp-nested-pages/app/Entities/Listing/Listing.php(280): include('/home/wpsi...')
#5 /home/wpsitedirectory/wp-includes/class-wp-hook.php(308): NestedPages\Entities\Listing\Listing->listPosts('')
#6 /home/wpsitedirectory/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters('', Array)
#7 /home/wpsitedirectory/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#8 /home/wpsitedirectory/wp-admin/admin.php(259): do_action('pages_page_nest...')
#9 /home/wpsitedirectory/wp-admin/edit.php(10): require_once('/home/wpsi...')
#10 {main} thrown in /home/wpsitedirectory/wp-content/plugins/wp-nested-pages/app/Entities/Listing/ListingRepository.php on line 40

changed line 40 from

if ( !$visible || !isset($visible[$post_type]) ) $visible[$post_type] = [];
to

if ( !$visible || !isset($visible[$post_type]) ) {
	$visible= array();
	$visible[$post_type] = [];
}

I should note, I didn't actively seek out any other errors, with this small change the plugin began to function.

@PLUS-RobertEhrenleitner
Copy link
Contributor

PLUS-RobertEhrenleitner commented Feb 28, 2023

The original thing did not work because in PHP 8 it is no longer allowed to add array values to an uninitialized variable. However, you could do it in one command:

if ( !$visible || !isset($visible[$post_type]) ) $visible = [$post_type => []];

Anyway, your fix is perfect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants