• Resolved Matt Keys

    (@mattkeys)


    Version 3.0.2 of the plugin is producing a PHP Notice on our site:

    PHP Notice: Undefined variable: hub_id in /wordpress_install_dir/wp-content/plugins/gravityforms-hubspot/src/Hooks.php on line 57

    Even though this is just a notice, it is actually a serious issue as the Async HubSpot Analytics Code never gets loaded as a result.

    Looking through the code; the if statement is structured in a way to write less code where it sets the $hub_id var in the statement itself, and then checks its strlen(). I’ve seen and written code like this myself in the past without issues, but in this example when it gets to the strlen() part of the statement it produces the above notice.

    If I restructure the code to first declare the $hub_id variable outside of the if(), the error goes away and the Async Analytics Code loads as expected.

    • This topic was modified 7 years, 7 months ago by Matt Keys.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Matt Keys

    (@mattkeys)

    The refactored function I am using now as a workaround until this can get officially patched:

        public static function addAnalyticsToFooter () {
            $gf_hubspot = gf_hubspot();
    
            $hub_id = $gf_hubspot->getSetting('hub_id');
    
            if ($hub_id && strlen($hub_id) > 0) {
                if ( !is_admin() ) {
                    ?>
                    <!-- Start of Async HubSpot Analytics Code -->
                    <script type="text/javascript">
                        (function(d,s,i,r) {
                            if (d.getElementById(i)){return;}
                            var n=d.createElement(s),e=d.getElementsByTagName(s)[0];
                            n.id=i;n.src='//js.hs-analytics.net/analytics/'+(Math.ceil(new Date()/r)*r)+'/<?php echo $hub_id; ?>.js';
                            e.parentNode.insertBefore(n, e);
                        })(document,"script","hs-analytics",300000);
                    </script>
                    <!-- End of Async HubSpot Analytics Code -->
                    <?php
                }
            } else {
                Tracking::log(__METHOD__ . '(): Hub ID missing, Analytics not included');
            }
        } // function
    Plugin Author Chris Lagasse

    (@soben)

    Hey Matt,

    Thanks for that!

    You’re right, typically PHP conditionals are lazy (so if the first one fails, don’t bother with the second part of an &&)… so it’s interesting this is causing a warning at all. It wasn’t happening on my local either (and I run with WP_DEBUG on all times when testing!)

    That said, I just pushed up 3.0.3 with your fix. Appreciate it!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘PHP Notice: Undefined variable: hub_id’ is closed to new replies.