Skip to content

Commit

Permalink
Stub basic post type and API endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
iandunn committed Oct 28, 2020
1 parent be09aeb commit 7dacd55
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@

namespace WordPressdotorg\Pattern_Directory;

require_once __DIR__ . '/includes/pattern-post-type.php';
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
);
}
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,
} );

0 comments on commit 7dacd55

Please sign in to comment.