-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stub basic post type and API endpoint.
- Loading branch information
Showing
5 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
|
||
namespace WordPressdotorg\Pattern_Directory; | ||
|
||
require_once __DIR__ . '/includes/pattern-post-type.php'; |
117 changes: 117 additions & 0 deletions
117
public_html/wp-content/plugins/pattern-directory/includes/pattern-post-type.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace WordPressdotorg\Pattern_Directory\Pattern_Post_Type; | ||
|
||
const POST_TYPE = 'wporg-pattern'; | ||
|
||
add_action( 'init', __NAMESPACE__ . '\register_post_type_data' ); | ||
add_filter( 'rest_' . POST_TYPE . '_item_schema', __NAMESPACE__ . '\embed_extra_fields_in_search_endpoint' ); | ||
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_editor_assets' ); | ||
|
||
|
||
/** | ||
* Registers post types and associated taxonomies, meta data, etc. | ||
*/ | ||
function register_post_type_data() { | ||
register_post_type( | ||
POST_TYPE, | ||
array( | ||
'label' => 'Block Pattern', | ||
'description' => 'Stores publicly shared Block Patterns (predefined block layouts, ready to insert and tweak).', | ||
'public' => true, | ||
'show_in_rest' => true, | ||
|
||
'supports' => array( | ||
'title', 'editor', 'revisions', 'author', 'excerpt', 'thumbnail', 'custom-fields', | ||
), | ||
) | ||
); | ||
|
||
register_taxonomy( | ||
'wporg-pattern-category', | ||
POST_TYPE, | ||
array( | ||
'public' => true, | ||
'hierarchical' => true, | ||
'show_in_rest' => true, | ||
'rest_base' => 'category', | ||
) | ||
); | ||
|
||
register_post_meta( | ||
POST_TYPE, | ||
'wpop_description', | ||
array( | ||
'type' => 'string', | ||
'description' => 'A description of the pattern', | ||
'single' => true, | ||
'default' => '', | ||
'sanitize_callback' => 'sanitize_text_field', | ||
'auth_callback' => __NAMESPACE__ . '\can_edit_this_pattern', | ||
'show_in_rest' => true, | ||
) | ||
); | ||
|
||
register_post_meta( | ||
POST_TYPE, | ||
'wpop_viewport_width', | ||
array( | ||
'type' => 'number', | ||
'description' => 'The width of the pattern in the block inserter.', | ||
'single' => true, | ||
'default' => 0, | ||
'sanitize_callback' => 'absint', | ||
'auth_callback' => __NAMESPACE__ . '\can_edit_this_pattern', | ||
'show_in_rest' => true, | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Determines if the current user can edit the given pattern post. | ||
* | ||
* This is a callback for the `auth_{$object_type}_meta_{$meta_key}` filter, and it's used to authorize access to | ||
* modifying post meta keys via the REST API. | ||
* | ||
* @param bool $allowed | ||
* @param string $meta_key | ||
* @param int $pattern_id | ||
* | ||
* @return bool | ||
*/ | ||
function can_edit_this_pattern( $allowed, $meta_key, $pattern_id ) { | ||
return current_user_can( 'edit_post', $pattern_id ); | ||
} | ||
|
||
/** | ||
* Add extra fields to the post type when it's embedded in the search results endpoint. | ||
* | ||
* @param array $schema | ||
* | ||
* @return array | ||
*/ | ||
function embed_extra_fields_in_search_endpoint( $schema ) { | ||
$schema['properties']['content']['context'][] = 'embed'; | ||
$schema['properties']['content']['properties']['rendered']['context'][] = 'embed'; | ||
$schema['properties']['meta']['context'][] = 'embed'; | ||
$schema['properties']['category']['context'][] = 'embed'; | ||
|
||
return $schema; | ||
} | ||
|
||
/** | ||
* Enqueue scripts for the block editor. | ||
*/ | ||
function enqueue_editor_assets() { | ||
if ( POST_TYPE !== get_current_screen()->id ) { | ||
return; | ||
} | ||
|
||
wp_enqueue_script( | ||
'pattern-post-type', | ||
trailingslashit( plugin_dir_url( __DIR__ ) ) . '/javascript/pattern-post-type.js', | ||
array( 'wp-element', 'wp-data', 'wp-components', 'wp-plugins', 'wp-edit-post' ), | ||
filemtime( dirname( __DIR__ ) . '/javascript/pattern-post-type.js' ), | ||
true | ||
); | ||
} |
70 changes: 70 additions & 0 deletions
70
public_html/wp-content/plugins/pattern-directory/javascript/pattern-post-type.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* There's no need for webpack etc, since we can assume wp-admin users will have modern browsers. | ||
* | ||
* JSX is the only thing that'd be nice to have, but it's not worth the tooling costs for just a few fields. | ||
* See https://reactjs.org/docs/react-without-jsx.html. | ||
*/ | ||
|
||
const PatternPostType = () => { | ||
const { useSelect, useDispatch } = wp.data; | ||
const { createElement, Fragment, useState, useEffect } = wp.element; | ||
const { TextControl, TextareaControl } = wp.components; | ||
const { PluginDocumentSettingPanel } = wp.editPost; | ||
const { editPost } = useDispatch( 'core/editor' ); | ||
|
||
const postMetaData = useSelect( select => select( 'core/editor' ).getEditedPostAttribute( 'meta' ) || {} ); | ||
const [ description, setDescription ] = useState( postMetaData.wpop_description ); | ||
const [ viewportWidth, setViewportWidth ] = useState( postMetaData.wpop_viewport_width ); | ||
|
||
useEffect( | ||
() => { | ||
editPost( { | ||
meta: { | ||
...postMetaData, | ||
wpop_description: description, | ||
wpop_viewport_width: viewportWidth | ||
}, | ||
} ); | ||
}, | ||
[ description, viewportWidth ] | ||
); | ||
|
||
const descriptionInput = createElement( | ||
TextareaControl, | ||
{ | ||
label: 'Description', | ||
value: description, | ||
onChange: setDescription | ||
} | ||
); | ||
|
||
const viewportWidthInput = createElement( | ||
TextControl, | ||
{ | ||
label: 'Viewport Width', | ||
value: viewportWidth, | ||
onChange: setViewportWidth, | ||
type: 'number' // Replace this with a `NumberControl` once it's stable. | ||
} | ||
); | ||
|
||
const container = createElement( | ||
Fragment, | ||
{}, | ||
[ descriptionInput, viewportWidthInput ] | ||
); | ||
|
||
return createElement( | ||
PluginDocumentSettingPanel, | ||
{ | ||
name: 'wporg-pattern-details', | ||
title: 'Pattern Details' | ||
}, | ||
container | ||
); | ||
}; | ||
|
||
wp.plugins.registerPlugin( 'pattern-post-type', { | ||
render: PatternPostType, | ||
icon: null, | ||
} ); |
File renamed without changes.