Skip to content

Add function_exists( 'add_action' ) check to initialization file #1270

Closed

Description

@salcode

When using CMB2 via composer and using a PHP based tool (e.g. PHP Code Sniffer), the PHP based tool throws an error.

Expected Behavior:

When installing CMB2 and PHP Code Sniffer via composer with a composer.json of

{
    "require": {
        "cmb2/cmb2": "^2.6"
    },
    "require-dev": {
        "squizlabs/php_codesniffer": "3.*"
    },
    "autoload"    : {
        "files": [
            "vendor/cmb2/cmb2/init.php"
        ]
    },
    "extra": {
        "installer-paths": {
            "vendor/cmb2/cmb2": ["cmb2/cmb2"]
        }
    }
}

I expect the following to run PHP Code Sniffer (phpcs)

$ composer install
$ ./vendor/bin/phpcs

Actual Behavior:

I get a fatal error because the function add_action() is undefined.

$ composer install
$ ./vendor/bin/phpcs
Fatal error: Uncaught Error: Call to undefined function add_action() in vendor/cmb2/cmb2/init.php:126

Possible Solution

If we add a function_exists() check for add_action() before calling it, we can prevent throwing this fatal error.

Possible Solution's Risk Level

This seems to be a relatively low risk change. Since we're checking if the function add_action() exists directly before calling it, the only behavior change we should get is avoiding throwing this fatal error.

Steps to reproduce (I have confirmed I can reproduce this issue on the develop branch):

  1. Clone https://github.com/salcode/salcode-cmb2-autoload-demo
  2. Run composer install
  3. Run ./vendor/bin/phpcs

Your Environment

Mac OS X Command Line

Activity

  1. MichaelHabib commented on Jul 11, 2019

    @MichaelHabib

    I had the same issue with the latest version of CMB2 ,
    I added the following code to the top of init.php
    if (!defined('ABSPATH')) exit; // Exit if accessed directly

    Also, after a simple google search, it seems this issue has crashed few websites !

  2. tw2113 commented on Jul 11, 2019

    @tw2113
    Contributor

    Based on #1271 it's a change that will get included in a future release, we just haven't gotten to the point of actually releasing yet. :D

  3. salcode commented on Jul 11, 2019

    @salcode
    ContributorAuthor

    Also, after a simple google search, it seems this issue has crashed few websites !

    @MichaelHabib I just wanted to chime in with some clarification about the issue I originally submitted, I assume your Google search was something like "Uncaught Error: Call to undefined function add_action() in vendor/cmb2/cmb2/init.php". The results that come back for me have pretty "deep" URLs (e.g. http://example.com/wp-content/plugins/cmb2/init.php) and with the sites I checked, if I went to the root of the website instead (e.g. http://example.com/) I didn't see the error.

    I agree the preferred behavior would be to show nothing, rather than the error message. Based on all of this I'm glad to see this being added in the next release but I understand it is not an high priority to get the next release out. 😀

  4. MichaelHabib commented on Jul 12, 2019

    @MichaelHabib

    Thanks for the quick updates
    I'll use the work around I've applied for now, as long the fix is in the next release it's all good :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions