Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Great, I think it’s pretty safe. As I say, it’s working well for me. I’ve been using checks like this for a while. If you don’t see anything glaring, I think we’re good to go! Thanks for getting back

    Hi,

    I’ve been encountering the same problem. I thought that I’ve been seeing it because I’m calling WFS functions from admin-ajax.php and somewhere along the line WFS is calling wfs_is_foodstore_page.

    That function goes:

    
    global $post, $wpdb;
    
    // ...
    
    if ( has_shortcode( $post->post_content, 'foodstore' ) ) {
    

    Naturally, this isn’t going to work if called when there’s no $post.

    I manually added

    
    if ( null === $post ) {
      return false;
    }
    

    after the global $post because if there is no post, there is definitely not a foodstore shortcode there! I haven’t encountered the error since but I am aware that the fix will be lost when I update the plugin.

    I was about to start a new thread about it because this is getting on my nerves while developing my site, however the above post by @thx4jh contradicts my reasoning by saying that he’s encountering the error on every page load. $post should always have content if a page is being loaded.

    Do you see any potential risks in adding a null check against $post before accessing $post->post_content? I think it should be in there and I’d like to see it in future versions. I’m not sure if there should be a null check anywhere else, I haven’t encountered any others.

    The version of wfs_is_foodstore_page I’m using looks like:

    
    function wfs_is_foodstore_page() {
    
      global $post, $wpdb;
    
      if ( null === $post ) {
        return false;
      }
    
      $shortcode_found = false;
      $pattern = get_shortcode_regex();
    
      if ( has_shortcode( $post->post_content, 'foodstore' ) ) {
        $shortcode_found = true;
      } else if ( preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches ) && array_key_exists( 2, $matches ) && in_array( 'foodstore', $matches[2] ) ) {
        $shortcode_found = true;
      } else if ( isset( $post->ID ) ) {
        $result = $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->postmeta WHERE post_id = %d AND meta_value LIKE '%%my_shortcode%%'", $post->ID ) );
        $shortcode_found = ! empty( $result );
      }
    
      return apply_filters( 'wfs_is_foodstore_page', $shortcode_found );
    }
    
Viewing 2 replies - 1 through 2 (of 2 total)