Giter Club home page Giter Club logo

Comments (3)

maevelander avatar maevelander commented on June 12, 2024

This could cause confusion for users so I've updated the readmes and external docs with a note

from gutenberg-ramp.

pyronaur avatar pyronaur commented on June 12, 2024

I recently merged a method to check if a post type can support Gutenberg

I can port it to the main class and we can check whther a post type can support Gutenberg before storing it in options.

However, checking for what a post type can do should wait for at least init action because the Codex suggests that post types are registered at init:

register_post_type

Description

Create or modify a post type. register_post_type should only be invoked through the 'init' action. It will not work if called before 'init', and aspects of the newly created or modified post type will work incorrectly if called later.

This means that if we perform the post_type check in ramp_for_gutenberg_load_gutenberg and the function is simply added in theme functions.php - ramp_for_gutenberg_load_gutenberg will be called way before init and will fail for post types that haven't yet been registered.

Rubber ducking: 🦆

Method 1
Modify the documentation and ask people to wait at least until init to register ramp support, so, something like this:

add_action('init', function() {
    ramp_for_gutenberg_load_gutenberg( array('post_types' => array('post') ) );
});

Caveats:

  • People don't read the docs carefully and might miss that they have to wait for init
  • It's lengthy to type out

Method 2
Turn the ramp_for_gutenberg_load_gutenberg function into a wrapping function that automattically does everything on init hook. Something like this:

function ramp_for_gutenberg_load_gutenberg_public( $criteria ) {
	if( did_action('init') ) {
		echo "You're too late! Please move this execute this function before `init` hook.";
		return false;
	}
	
	add_action('init', function() use ($criteria) {
		ramp_for_gutenberg_load_gutenberg($criteria);
	} );
}

Caveats:

  • PHP 5.3 is required for this

Method 3
Store options instantly, perform an additional cleanup/check on shutdown or init:priority = 10000 and remove post types that aren't supported. This would be messy on the back-end a bit, but PHP 5.2 compatible and with a clean interface function.

Caveats:

  • A lot of database/cache touching

Method 4
Almost the same as Method 3, but instead - store all options in a Singleton class variable and on init:priority = 10000 or shutdown save everything to options and make sure the post types can be stored in one go.

Caveats:
None? Rubber ducking worked nicely again.

from gutenberg-ramp.

mattoperry avatar mattoperry commented on June 12, 2024

@justnorris as you predicted I like method 4 ... ie: delay the saving of the criteria until late enough. I can't really think of any reason why this would be bad .. it doesn't really matter when the option is written as long as it happens in the same page generation.

from gutenberg-ramp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.