• Resolved Lukasz

    (@wpfed)


    Two errors I have logged, if you can look into it:

    PHP Warning: ?Attempt to read property “childNodes” on null in \wp-content\plugins\wordpress-popular-posts\src\Output.php on line 181

    PHP Warning: ?foreach() argument must be of type array|object, null given in \wp-content\plugins\wordpress-popular-posts\src\Output.php on line 181

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hey @wpfed,

    Someone else reported this PHP warning some time ago (see link). I couldn’t reproduce the problem back then and the reporter didn’t provide any further details (otherwise I would have probably fixed it by now.)

    Could you please share reproduction steps? (As in, how are you using the plugin, settings, etc.)

    • This reply was modified 2 months ago by Hector Cabrera. Reason: Fixed a couple of typos
    • This reply was modified 2 months ago by Hector Cabrera. Reason: Added link to previous report
    Thread Starter Lukasz

    (@wpfed)

    Hey @hcabrera,

    Interesting, I will have a look at the thread in greater details and share reproduction steps.

    Thread Starter Lukasz

    (@wpfed)

    This appears to happen when there is no popular posts in certain categories, for example I have pages with associated categories, the category for the page is returned and then wpp checks for any posts in that category and passes null to the output which is then attempted to be processed. I’ll try to see if I an add some error handling for this situation.

    This is from my page.php:

    global $post;

    ? ? ? ? ? ? ? ? ? ? if ( is_page( $post->ID ) && ! is_page( $contactUsPage ) ) {

    ? ? ? ? ? ? ? ? ? ? ? ? $helpfulArticles = esc_html__( 'You might also find these helpful.', 'domain' );

    ? ? ? ? ? ? ? ? ? ? ? ? $key_name = get_post_custom_values( $key = 'cat' );

    ? ? ? ? ? ? ? ? ? ? ? ? $cat_ID ? = get_cat_ID( $key_name[0] );

    ? ? ? ? ? ? ? ? ? ? ? ? if ( function_exists( 'pll_register_string' ) ) {

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? $currentID = pll_get_post( 300 );

    ? ? ? ? ? ? ? ? ? ? ? ? }

    ? ? ? ? ? ? ? ? ? ? ? ? $closerForAside = '<li><a href="' . esc_url( get_permalink( $currentID ) ) .

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? '">'

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? . esc_html__( 'See all Articles', 'domain' ) . '<i class="fa fa-angle-right" aria-hidden="true"></i></a></li></ul></section></div>';

    ? ? ? ? ? ? ? ? ? ? ? ? $args = array(

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'post_type' ? => 'post',

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'limit' ? ? ? => 3,

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'cat' ? ? ? ? => pll_get_term( $cat_ID, 'en' ),

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'stats_views' => 0,

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'range' ? ? ? => 'all',

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'freshness' ? => 1,

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'wpp_start' ? => '<div id="popularPosts" class="sidebar popularPosts" aria-labelledby="helpfulArticles">

    ? ? ? ? ? ? ? ? <section class="sidebarHelpTopics"><h2 id="helpfulArticles">' . $helpfulArticles . '</h2><ul>',

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'wpp_end' ? ? => $closerForAside,

    ? ? ? ? ? ? ? ? ? ? ? ? );

    ? ? ? ? ? ? ? ? ? ? ? ? wpp_get_mostpopular( $args );

    ? ? ? ? ? ? ? ? ? ? }
    Plugin Author Hector Cabrera

    (@hcabrera)

    Thanks! I’ll give that code you shared a try sometime later today and see if I can trigger that PHP warning. I’ll keep you posted.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Quick update: tested the code, no PHP warnings at all. I modified it a bit though, removed all pll_ calls since I don’t think that the warning is related to Polylang.

    Could you please try this version of your code and see if you still see that PHP warning?

    $helpfulArticles = esc_html__( 'You might also find these helpful.', 'domain' );
    $cat_ID = 1; // Replace this category ID with one from your site

    $closerForAside = '<li><a href="' . esc_url( get_permalink() ) . '">'

    . esc_html__( 'See all Articles', 'domain' ) . '<i class="fa fa-angle-right" aria-hidden="true"></i></a></li></ul></section></div>';

    $args = array(
    'post_type' => 'post',
    'limit' => 3,
    'cat' => $cat_ID,
    'stats_views' => 0,
    'range' => 'all',
    'freshness' => 1,
    'wpp_start' => '<div id="popularPosts" class="sidebar popularPosts" aria-labelledby="helpfulArticles">

    <section class="sidebarHelpTopics"><h2 id="helpfulArticles">' . $helpfulArticles . '</h2><ul>',
    'wpp_end' => $closerForAside,
    );

    wpp_get_mostpopular( $args );
    Thread Starter Lukasz

    (@wpfed)

    @hcabrera thanks for getting back. I copied in your code and just changed to one of the my categories which does not have any popular posts and received the exact same errors as before. As soon as I add a view to one of the posts in the category, the error disappears, so it’s only posts with empty views that throw the error, this wont really be issue in prod.

    But, I tried your code on my functions.php, and it worked, no error, just blank if their is no views and link if their is views. So could it be a timing issue on page.php, maybe the function should be called within a hook or something to fire at the right time? This may not be sufficient:

    global $post;
    if ( is_page( $post->ID ) && ! is_page( $contactUsPage ) ) {

    Edit: The is_page doesn’t make a difference, removed or not. Something to do with outputting the results on page.php.

    • This reply was modified 1 month, 4 weeks ago by Lukasz.
    Plugin Author Hector Cabrera

    (@hcabrera)

    The one thing that caught my attention from what you said is that you’re getting a blank output when there are no posts. That should not be the case, you should be seeing a “Sorry, no data so far” text when WPP can’t find any popular posts ??

    I’m thinking I could provide you with a version of the plugin with some logging enabled so we can hopefully see what is actually happening when the plugin can’t find posts on your site. If you’re OK with that please use my contact form and I’ll get back to you as soon as I can.

    Thread Starter Lukasz

    (@wpfed)

    Found the issue! – when I disable this filter, the error goes away, it seems null is being passed from this function when no views found

    function my_custom_no_posts_found( $no_data_html ) {
    //$output = '<style type="text/css">.popular-posts { display:none; }</style>';
    $output = '';
    return $output;
    }
    add_filter( 'wpp_no_data', 'my_custom_no_posts_found', 10, 1 );

    I think I will just set .wpp-no-data as display none, to get the desired output.

    Phew, thank you for your time on this, very much appreciated!

    • This reply was modified 1 month, 4 weeks ago by Lukasz.
    Plugin Author Hector Cabrera

    (@hcabrera)

    Oh, I was thinking that it might be related to that filter but wasn’t sure. Great finding! I was able to replicate that PHP warning now, everything makes sense.

    Alright, I’ll patch that up in the next version so you can have that filter hook return that empty string without triggering a PHP warning. Thank you for sharing this.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.