|
| 1 | +# Metaboxes |
| 2 | + |
| 3 | +This is a brief document detailing how metabox support works in Gutenberg. With |
| 4 | +the superior developer and user experience of blocks however, especially once, |
| 5 | +block templates are available, **converting PHP metaboxes to blocks is highly |
| 6 | +encouraged!** |
| 7 | + |
| 8 | +## Breakdown |
| 9 | + |
| 10 | +Each metabox area is rendered by a React component containing an iframe. |
| 11 | +Each iframe will render a partial page containing only metaboxes for that area. |
| 12 | +Metabox data is collected and used for conditional rendering. |
| 13 | + |
| 14 | +### Metabox Data Collection |
| 15 | + |
| 16 | +On each Gutenberg page load, the global state of post.php is mimicked, this is |
| 17 | +hooked in as far back as `plugins_loaded`. |
| 18 | + |
| 19 | +See `lib/register.php gutenberg_trick_plugins_into_registering_metaboxes()` |
| 20 | + |
| 21 | +This will register two new actions, one that fakes the global post state, and |
| 22 | +one that collects the metabox data to determine if an area is empty. |
| 23 | + |
| 24 | +gutenberg_set_post_state() is hooked in early on admin_head to fake the post |
| 25 | +state. This is necessary for ACF to work, no other metabox frameworks seem to |
| 26 | +have this problem. ACF will grab the `$post->post_type` to determine whether a |
| 27 | +box should be registered. Later in `admin_head` ACF will register the metaboxes. |
| 28 | + |
| 29 | +Hooked in later on admin_head is gutenberg_collect_metabox_data(), this will |
| 30 | +run through the functions and hooks that post.php runs to register metaboxes; |
| 31 | +namely `add_meta_boxes, add_meta_boxes_{$post->post_type}`, and `do_meta_boxes`. |
| 32 | + |
| 33 | +A copy of the global $wp_meta_boxes is made then filtered through |
| 34 | +`apply_filters( 'filter_gutenberg_metaboxes', $_metaboxes_copy );`, which will |
| 35 | +strip out any core metaboxes along with standard custom taxonomy metaboxes. |
| 36 | + |
| 37 | +Then each location for this particular type of metabox is checked for whether it |
| 38 | +is active. If it is not empty a value of true is stored, if it is empty a value |
| 39 | +of false is stored. This metabox location data is then dispatched by the editor |
| 40 | +Redux store in `INITIALIZE_METABOX_STATE`. |
| 41 | + |
| 42 | +Ideally, this could be done at instantiation of the editor, and help simplify, |
| 43 | +this flow. However, it is not possible to know the metabox state before |
| 44 | +`admin_enqueue_scripts` where we are calling `createEditorInstance()`. This will |
| 45 | +have to do. |
| 46 | + |
| 47 | +### Redux and React Metabox Management. |
| 48 | + |
| 49 | +*The Redux store by default will hold all metaboxes as inactive*. When |
| 50 | +`INITIALIZE_METABOX_STATE` comes in, the store will update any active metabox |
| 51 | +areas by setting the `isActive` flag to `true`. Once this happens React will |
| 52 | +check for the new props sent in by Redux on the Metabox component. If that |
| 53 | +Metabox is now active, instead of rendering null, a MetaboxIframe component will |
| 54 | +be rendered. The Metabox component is the container component that mediates |
| 55 | +between the MetaboxIframe and the Redux Store. *If no metaboxes are active, |
| 56 | +nothing happens. This will be the default behavior, as all core metaboxes have |
| 57 | +been stripped.* |
| 58 | + |
| 59 | +#### MetaboxIframe Component |
| 60 | + |
| 61 | +When the component renders it will store a ref to the iframe, the component will |
| 62 | +set up a listener for post messages for resizing. assets/js/metabox.js is |
| 63 | +loaded inside the iframe and will send up postMessages for resizing, which the |
| 64 | +MetaboxIframe Component will use to manage its state. A mutation observer will |
| 65 | +also be created when the iframe loads. The observer will detect whether, any |
| 66 | +DOM changes have happened in the iframe, input and change event listeners will |
| 67 | +also be attached to check for changes. |
| 68 | + |
| 69 | +The change detection will store the current form's `FormData`, then whenever a |
| 70 | +change is detected the current form data will be checked vs, the original form |
| 71 | +data. This serves as a way to see if the metabox state is dirty. When the |
| 72 | +metabox state has been detected to have changed, a Redux action |
| 73 | +`METABOX_STATE_CHANGED` is dispatched, updating the store setting the isDirty |
| 74 | +flag to `true`. If the state ever returns back to the original form data, |
| 75 | +`METABOX_STATE_CHANGED` is dispatched again to set the isDirty flag to `false`. |
| 76 | +A selector `isMetaboxStateDirty()` is used to help check whether the post can be |
| 77 | +updated. It checks each metabox for whether it is dirty, and if there is at |
| 78 | +least one dirty metabox, it will return true. This dirty detection does not |
| 79 | +impact creating new posts, as the content will have to change before metaboxes |
| 80 | +can trigger the overall dirty state. |
| 81 | + |
| 82 | +When the post is updated, only metaboxes that are active and dirty, will be |
| 83 | +submitted. This removes any unnecessary requests being made. No extra revisions, |
| 84 | +are created either by the metabox submissions. A redux action will trigger on |
| 85 | +`REQUEST_POST_UPDATE` for any dirty metabox. See editor/effects.js. The |
| 86 | +`REQUEST_METABOX_UPDATE` action will set that metabox's state to isUpdating, |
| 87 | +the isUpdating prop will be sent into the MetaboxIframe and cause a form |
| 88 | +submission. After loading, the original change detection process is fired again |
| 89 | +to handle the new state. Double buffering the iframes here will improve the |
| 90 | +user experience quite a bit, as there currently is a flicker. |
| 91 | + |
| 92 | +### Iframe serving a partial page. |
| 93 | + |
| 94 | +Each iframe will point to an individual source. These are partial pages being |
| 95 | +served by post.php. Why this approach? By using post.php directly, we don't have |
| 96 | +to worry as much about getting the global state 100% correct for each and every |
| 97 | +use case of a metabox, especially when it comes to saving. Essentially, when |
| 98 | +post.php loads it will set up all of its state correctly, and when it hits the |
| 99 | +three `do_action( 'do_meta_boxes' )` hooks it will trigger our partial page. |
| 100 | + |
| 101 | +`gutenberg_metabox_partial_page()` is used to render the metaboxes for a context |
| 102 | +then exit the execution thread early. A `metabox` request parameter is used to |
| 103 | +trigger this early exit. The metabox request parameter should match one of |
| 104 | +`'advanced'`, `'normal'`, or `'side'`. This value will determine which metabox |
| 105 | +area is served. So an example url would look like: |
| 106 | + |
| 107 | +`mysite.com/wp-admin/post.php?post=1&action=edit&metabox=$location` |
| 108 | + |
| 109 | +This url is automatically passed into React via a _wpMetaboxUrl global variable. |
| 110 | +The partial page is very similar to post.php and pretty much imitates it and |
| 111 | +after rendering the metaboxes via do_meta_boxes() it exits early and does some |
| 112 | +hook clean up. There are two extra files that are enqueued; both with a handle |
| 113 | +metabox-gutenberg. One is the js file from assets/js/metabox.js, which resizes |
| 114 | +the iframe. The stylesheet is generated by webpack from |
| 115 | +editor/metaboxes/metabox-iframe.scss and built |
| 116 | +into editor/build/metabox-iframe.css |
| 117 | + |
| 118 | +These styles make use of some of the SASS variables, so that as the Gutenberg |
| 119 | +UI updates so will the Metaboxes. |
| 120 | + |
| 121 | +The partial page mimics the post.php post form, so when it is submitted it will |
| 122 | +normally fire all of the necessary hooks and actions, and have the proper global |
| 123 | +state to correctly fire any PHP metabox mumbo jumbo without needing to modify |
| 124 | +any existing code. On successful submission the page will be reloaded back to |
| 125 | +the same partial page with updated data. |
| 126 | + |
| 127 | +## Wrap Up. |
| 128 | + |
| 129 | +There are some other details I am probably forgetting but this is a pretty good |
| 130 | +overview. |
0 commit comments