Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom field files get deleted when upgrading CMB2 included as Composer package #1492

Open
dfumagalli opened this issue Jul 14, 2023 · 5 comments

Comments

@dfumagalli
Copy link

Describe the bug

helper-functions.php line 44 tries to autoload 3rd party custom field files from the includes sub-directory even when CMB2 is being included as Composer package.

This is quite bad, because when a new CMB2 release is out, it can erase the 3rd party files and break the website.

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

  1. Download a 3rd party custom field. I tested with the Textarea with checkbox provided in your own CMB2-Snippet-Library.
  2. Place it in the includes directory
  3. It works
  4. Perform a composer require and get a new version of CMB2. Included file will be lost.

Possible Solution

Add an override constant in the init file or some way to specify a path, for example in composer.json.

Possible Solution's Risk Level

Having multiple plugins including the same 3rd party field types could lead to conflicts.

Screenshots

If applicable, add screenshots to help explain your problem.

CMB2 Field Registration Code:

	$cmb->add_field(
		array(
			'name'    => __( 'Text', DT_TEXTDOMAIN ),
			'desc'    => __( 'field description (optional)', DT_TEXTDOMAIN ),
			'id'      => '_field-textarea-with-checkbox',
			'type'    => 'textarea_with_checkbox',
			'default' => 'Default Text',
		)
	);

Your Environment

Browser name and version: Firefox latest version
Operating System and version (desktop or mobile): Windows 10 Pro

Additional context

@tw2113
Copy link
Contributor

tw2113 commented Jul 14, 2023

Have you checked in the database directly to see if the meta fields are actually gone? Or is it just visually in the editor screen that it looks gone?

Asking because _field-textarea-with-checkbox feels like it'd be one that isn't exposed to the custom fields stuff based on the underscore prefix, but I think CMB2 should be showing regardless of that part.

Also, unless you're saving at the same time, simply upgrading CMB2 via composer shouldn't be causing any file loss unless the files are stored in a spot where the upgrade process replaces the same folder.

Line 44 in question: https://github.com/CMB2/CMB2/blob/develop/includes/helper-functions.php#L44

@tw2113
Copy link
Contributor

tw2113 commented Jul 14, 2023

Place it in the includes directory

Actually that's probably exactly why it's getting lost, you're adding things to the place where it would be getting updated and thus lost. The 3rd party code shouldn't be getting added to the cmb2 folder directly. It should be getting loaded on its own.

@dfumagalli
Copy link
Author

dfumagalli commented Jul 15, 2023

Place it in the includes directory

Actually that's probably exactly why it's getting lost, you're adding things to the place where it would be getting updated and thus lost. The 3rd party code shouldn't be getting added to the cmb2 folder directly. It should be getting loaded on its own.

I implemented that and what happens is... it works BUT helper-functions.php emits 2 errors about not being able to find the file.
In fact, even if I successfully load and display the field elsewhere, the helper-functions.php still performs an include within its fixed includes directory.

@dfumagalli
Copy link
Author

dfumagalli commented Jul 15, 2023

I found another "bug". If I write my own CMB_Type_XXXX or a REST piece, the helper-functions.php attempts to include them within the wp-content/plugins/my-plugin-name/vendor/cmb2/includes/[.|types|rest-api] directory too.

I have found a relatively simple solution: to transform the includes path into an array, whose first element is set at the original includes path during the initialization.

Example:

  • Create one array in some globally accessible class. I've seen "CMB2_Utils" could be it:

$cmb_includes = array( cmb2_dir( 'includes' ));

  • put in some methods to let the plugin software developer add his own include paths into this array.

  • replace line 44 with a foreach ( $cmb_includes as $cmb_include ). Inside the loop, the code checks if the class file exists at $cmb_include and if it does, then it includes it.

@tw2113
Copy link
Contributor

tw2113 commented Jul 15, 2023

Part of what I was getting at yesterday is that you should not be editing or adding any custom files to any CMB2 core folders. Leave those alone and load your customized code elsewhere. I don't believe the helper-functions.php file necessarily needs to be aware of your file that's loading your custom code, as long as it gets loaded correctly.

Also something to note is that we have a pretty interesting way to handle loading of the latest CMB2 even if other plugins are also bundling CMB2. It can be seen at https://github.com/CMB2/CMB2/blob/develop/init.php#L108-L132

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants