amarinediary / WordPress-simple-URL-based-breadcrumb

🍞 A non-invasive WordPress unofficial plugin, minimalist and SEO friendly. both lightweight and lightning fast, adding URL based breadcrumb support. Plug-and-play, with no required configuration.
https://stackoverflow.com/a/67453887/3645650
Creative Commons Zero v1.0 Universal
27 stars 5 forks source link

Warning: strpos(): Empty needle #7

Closed hanyseyedy closed 2 years ago

hanyseyedy commented 2 years ago

hi

(1) I get "strpos(): Empty needle" error in Normal Archive and Search Result page. how can I solve this problem? archive: https://hanytheme.foxdevelop.ir/ search result: https://hanytheme.foxdevelop.ir/?s=test

(2) and another case: in Category and Tag page, crumb Not displayed correctly. https://hanytheme.foxdevelop.ir/category/%d8%af%d8%b3%d8%aa%d9%87%d8%a8%d9%86%d8%af%db%8c-%d9%86%d8%b4%d8%af%d9%87/ https://hanytheme.foxdevelop.ir/tag/%d8%a8%d8%b1%da%86%d8%b3%d8%a8/

this site is in Persian Language. on a single post , the title is displayed correctly in the crump. I want that in category and tag pages. single post Example: https://hanytheme.foxdevelop.ir/%d8%b3%d9%84%d8%a7%d9%85-%d8%af%d9%86%db%8c%d8%a7/

thank you

amarinediary commented 2 years ago

For future issues please open 1 new issue per problem you're having.

Regarding (1)

Search and Archive page are user queried based and are not supported.

The plugin strip all parameters, anything after ? won't be outputted. Tho you shouldn't get an error.

I would advise using get_search_query to display the current search query.

Regarding (2)

In regards to the persian language not being decoded properly i'm pushing an update that should fix it shortly.

amarinediary commented 2 years ago

The following will be deployed in 1.0.6. View the changelog.

In response to (2):

hanyseyedy commented 2 years ago

thanks for solve the crumb slug through urldecode()

Regarding (1) I don't know exactly what should to do. I displayed search result with "get_search_query" in my search page. but don't understand where I should to add the get_search_query in your crumb source. and I Don't know what Can I solve "strpos(): Empty needle" problem in archive and search page. This error is related to line 67 of the source: $url = esc_url( $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/' . substr( implode( '/', $flour ), 0, strpos( implode( '/', $flour ), $crumb ) ) . $crumb. '/' );

By the way, my host php version is 7.4 That's why I had to comment line 26 temporarily. // if ( version_compare( PHP_VERSION, '8.0.0', '<' ) ) return; Does the php version affect this error?

amarinediary commented 2 years ago

Yes, the version_compare isn't just a pretty line. It indicate that the minimum version should be at least 8.0.0.

That's indeed why you're getting the error. The plugins uses str_contains and str_ends_with which are both dependent on PHP > 8.0.0to properly filter the url.

Alternative are available prior to PHP > 8.0.0 but I won't update the plugin to suit an older PHP version as it will require to refactor a bunch of code. To suit pre-8.0.0 version you would need to break away from native PHP 8.0.0 functions and upcoming plugin version won't be covered.

Here is an alternative to the str_ends_with pre-8.0.0 version:

/**
 * Does the string end with a given string?
 * 
 * @param String $haystack
 * @param String $needle
 * 
 * @return Boolean
 */
if ( ! function_exists( 'endsWith' ) ) {

    function endsWith( $haystack, $needle ) {

        $length = strlen( $needle );

        if( ! $length ) {

            return true;

        };

        return substr( $haystack, -$length ) === $needle;

    };

};

You would then need to replace if ( str_ends_with( $flour, '/' ) ) { by if ( endsWith( $flour, '/' ) ) {.

For str_contains the alternative is a bit simpler we can use str_pos in a boolean conditional statement:

You would need to replace if ( str_contains( $flour, '?' ) ) { by if ( false !== strpos( $flour, '?' ) ) {.

This is untested but should work out.

hanyseyedy commented 2 years ago

Thank you very much. I made the necessary changes for the version before php 8.0.0 Error "strpos (): Empty needle" still exists. After doing some research, I realized that the page where this error is displayed is not an archive page. I have currently fixed this issue by adding a condition to my archive page. Thank you again for your support