WPBP/WordPress-Plugin-Boilerplate-Powered

Class 'CMB2_Field' not found

Closed this issue · 4 comments

I'm trying to create a new CMB2_Field in PostTypes->initialize() like this:

$class_date = new \CMB2_Field([ etc...

Getting the following error:

Uncaught Error: Class 'CMB2_Field' not found in /var/www/html/wp-content/plugins/mz-mindbody-classes/internals/PostTypes.php:51\nStack trace:\n
#0 /var/www/html/wp-content/plugins/mz-mindbody-classes/engine/Initialize.php(95): MZ_Mindbody_Classes\\Internals\\PostTypes->initialize()\n
#1 /var/www/html/wp-content/plugins/mz-mindbody-classes/engine/Initialize.php(82): MZ_Mindbody_Classes\\Engine\\Initialize->load_classes()\n
#2 /var/www/html/wp-content/plugins/mz-mindbody-classes/mz-mindbody-classes.php(143): MZ_Mindbody_Classes\\Engine\\Initialize->__construct(Object(Composer\\Autoload\\ClassLoader))\n
#3 /var/www/html/wp-includes/class-wp-hook.php(287): {closure}('')\n
#4 /var/www/html/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array)\n
#5 /var/www/html/wp-includes/plugin.php(478): WP_Hook->do_action(Array)\n
#6 /var/www/html/wp-settings.php(403): do_action('plugins_loaded')\n
#7 /var/www/html/wp-config.php(98): require_once('/var/www/html/w...')\n
#8 /var/www/html/wp-load.php(37): require_once('/var/www/html/w...' in /var/www/html/wp-content/plugins/mz-mindbody-classes/internals/PostTypes.php on line 51, referer: http://project.test/wp-admin/post.php?post=20706&action=edit

Glanced at the composer autoload files. Should CMB2 be present in composer/autoload_namespaces.php?:

return array(
    'xrstf\\Composer52' => array($vendorDir . '/xrstf/composer-php52/lib'),
    'tad_' => array($vendorDir . '/lucatume/args/src'),
    'tad\\FunctionMocker' => array($vendorDir . '/lucatume/function-mocker/src'),
    'cli' => array($vendorDir . '/wp-cli/php-cli-tools/lib'),
    'WP_CLI\\' => array($vendorDir . '/wp-cli/wp-cli/php'),
    'Requests' => array($vendorDir . '/rmccue/requests/library'),
    'PHPMD\\' => array($vendorDir . '/phpmd/phpmd/src/main/php'),
    'Oxymel' => array($vendorDir . '/nb/oxymel'),
    'Mustache' => array($vendorDir . '/mustache/mustache/src'),
    'Hautelook' => array($vendorDir . '/hautelook/phpass/src'),
    'Flow\\JSONPath\\Test' => array($vendorDir . '/flow/jsonpath/tests'),
    'Flow\\JSONPath' => array($vendorDir . '/flow/jsonpath/src'),
    'Behat\\Gherkin' => array($vendorDir . '/behat/gherkin/src'),
    'Arg' => array($vendorDir . '/lucatume/args/src'),
);

Maybe this will be insightful:

10:58 $ grep -ri CPT_columns vendor/composer/
vendor/composer//autoload_static.php:        '33463dafc79559dfcabd4bb559eb4b57' => __DIR__ . '/..' . '/wpbp/cpt_columns/CPT_Columns.php',
vendor/composer//installed.json:        "name": "wpbp/cpt_columns",
vendor/composer//installed.json:            "url": "https://github.com/WPBP/CPT_Columns.git",
vendor/composer//installed.json:            "url": "https://api.github.com/repos/WPBP/CPT_Columns/zipball/6cc1b2fa6ebb8bbf3f0a1db06685463af0c8ebab",
vendor/composer//installed.json:                "CPT_Columns.php"
vendor/composer//installed.json:        "homepage": "https://github.com/WPBP/CPT_Columns",
vendor/composer//autoload_files.php:    '33463dafc79559dfcabd4bb559eb4b57' => $vendorDir . '/wpbp/cpt_columns/CPT_Columns.php',
✔ ~/Docker/WP/mzmbo/mz-mindbody-classes [master|✚ 3] 
11:08 $ bb vendor/composer/autoload_static.php 
✔ ~/Docker/WP/mzmbo/mz-mindbody-classes [master|✚ 3] 
11:09 $ grep -ri CMB2 vendor/composer/
vendor/composer//installed.json:        "name": "cmb2/cmb2",
vendor/composer//installed.json:            "url": "https://github.com/CMB2/CMB2.git",
vendor/composer//installed.json:            "url": "https://api.github.com/repos/CMB2/CMB2/zipball/438c26dda7ac110d0741448d65614b3ec6379034",
vendor/composer//installed.json:        "description": "CMB2 is a metabox, custom fields, and forms library for WordPress that will blow your mind.",
vendor/composer//installed.json:        "homepage": "https://github.com/CMB2/CMB2",
vendor/composer//installed.json:        "name": "origgami/cmb2-grid",
vendor/composer//installed.json:            "url": "https://github.com/origgami/CMB2-grid.git",
vendor/composer//installed.json:            "url": "https://api.github.com/repos/origgami/CMB2-grid/zipball/ad2b6163164a805bb89d07deb955e3a7ee5c72a8",
Mte90 commented

Well this is not the right way to do it basically. I don't know where you saw this code to create a new field.
In the past I created new CMB2 fields using the filters provided with CMB2 like this one https://github.com/CMB2/CMB2-Snippet-Library/blob/6e7f2be286825e74cef9095320ad67e483749000/custom-field-types/multicheck_posttype-field_type.php

So you need to create a class with a namespace, put in a folder like integrations and autoamtically the plugin will execute it like the others.

Fantastic. I see now that you have an example already at integrations/CMB.php.

I'm perplexed as to why you have CMB in integrations, while PostTypes is in internals. I would expect them to be in the same namespace.

Mte90 commented

Because CMB is an external library instead post type is a native wordpress feature.