Skip to content

Commit a730010

Browse files
committed
Refactor plugin into manager and runtime modules
1 parent 3cc48cf commit a730010

18 files changed

+309
-291
lines changed

README.md

+4-29
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,7 @@
1-
# Data Types with content areas
1+
# Create Content Model
22

3-
Run it with `npm install` and `npm run dev-server`.
3+
Create content models in WP Admin.
44

5-
[Watch the solution walkthrough here](https://www.youtube.com/watch?v=pA_lq7eeGlg).
5+
### Development
66

7-
## How it works
8-
9-
### Creating a data type
10-
11-
Much like the Hackathon, the Editor is used to create a new data type and define its fields.
12-
13-
**The "fields" in this prototype are limited to the content areas.**
14-
15-
When you select a Group block, a new inspector control ("Content area") is added to the sidebar,
16-
where you can bind the contents to a specific part of the post object:
17-
18-
- `post_content` binds to the `post_content` attribute.
19-
- Any other value binds to the post meta fields.
20-
21-
### Entering data
22-
23-
The data type template is displayed in the Editor. User enters content in the content area blocks that were added.
24-
25-
Upon clicking "Save", the back-end will intercept the post, extract the blocks from the content areas and
26-
save them to the appropriate places (e.g., `post_content` and post meta fields.)
27-
28-
### Displaying data
29-
30-
The `post_content` is not intercepted, and the stored content is displayed.
31-
32-
The plugin creates a block variation for each post meta field, and also exposes a block that renders the data type's template in the current Query Loop context.
7+
Run `npm install` and `npm run dev-server`.

create-content-model.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Plugin Name: Create Content Model
4+
* Description: Create content models in WP Admin.
5+
* Version: 1.0
6+
*
7+
* @package data-types
8+
*/
9+
10+
declare( strict_types = 1 );
11+
12+
define( 'CONTENT_MODEL_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
13+
14+
require_once __DIR__ . '/includes/manager/0-load.php';
15+
require_once __DIR__ . '/includes/runtime/0-load.php';

data-types.php

-15
This file was deleted.

dev.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"step": "defineWpConfigConsts",
66
"consts": {
77
"WP_DEBUG": true,
8-
"WP_DEBUG_LOG": "/var/www/html/wp-content/plugins/cf24-cpts/debug.log",
9-
"WP_PD_DEV": true
8+
"WP_DEBUG_LOG": "/var/www/html/wp-content/plugins/create-content-model/debug.log"
109
}
1110
}
1211
]

inc/data-type-management.php

-116
This file was deleted.

inc/templating.php

-109
This file was deleted.

includes/manager/0-load.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* Loads the manager.
4+
*
5+
* @package create-content-model
6+
*/
7+
8+
declare( strict_types = 1 );
9+
10+
require_once __DIR__ . '/add-content-model-cpt.php';
11+
require_once __DIR__ . '/content-area-binder.php';
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Adds the Content Model manager to the sidebar.
4+
*
5+
* @package create-content-model
6+
*/
7+
8+
add_action(
9+
'init',
10+
function () {
11+
if ( ! current_user_can( 'manage_options' ) ) {
12+
return;
13+
}
14+
15+
register_post_type(
16+
'content_model',
17+
array(
18+
'label' => 'Content Models',
19+
'public' => true,
20+
'show_in_menu' => true,
21+
'show_in_rest' => true,
22+
)
23+
);
24+
},
25+
0
26+
);

inc/content-area-binder.js includes/manager/content-area-binder.js

-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
function registerContentAreaInspector( wp ) {
2-
const loadedPagePostType = wp.data
3-
.select( 'core/editor' )
4-
.getCurrentPostType();
5-
62
const createHigherOrderComponent = wp.compose.createHigherOrderComponent;
73
const { Fragment } = wp.element;
84
const { InspectorControls } = wp.blockEditor;
@@ -30,8 +26,6 @@ function registerContentAreaInspector( wp ) {
3026
wp.element.createElement( TextControl, {
3127
key: 'location',
3228
label: 'location',
33-
readOnly:
34-
'data_types' !== loadedPagePostType,
3529
value: attributes.metadata?.[
3630
'data-types/binding'
3731
],

inc/content-area-binder.php includes/manager/content-area-binder.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
add_action(
99
'enqueue_block_editor_assets',
1010
function () {
11+
global $post;
12+
13+
if ( 'content_model' !== $post->post_type ) {
14+
return;
15+
}
16+
1117
wp_enqueue_script(
1218
'data-types/content-area-binder',
13-
plugin_dir_url( __FILE__ ) . '/content-area-binder.js',
19+
CONTENT_MODEL_PLUGIN_URL . '/includes/manager/content-area-binder.js',
1420
array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' ),
1521
'v1',
1622
true

includes/runtime/0-load.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Loads the runtime.
4+
*
5+
* @package create-content-model
6+
*/
7+
8+
declare( strict_types = 1 );
9+
10+
require_once __DIR__ . '/load-content-models.php';
11+
require_once __DIR__ . '/expose-cpt-template.php';
12+
require_once __DIR__ . '/expose-cpt-fields.php';
13+
require_once __DIR__ . '/data-entry.php';

0 commit comments

Comments
 (0)