Skip to content

Commit

Permalink
First pass at Gutenberg support.
Browse files Browse the repository at this point in the history
See #6.
  • Loading branch information
r-a-y committed Mar 20, 2019
1 parent 0dc6cd3 commit 298b601
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 16 deletions.
9 changes: 9 additions & 0 deletions assets/gutenberg.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.ray-unlisted {
position: relative;
}

.ray-unlisted input[type="checkbox"] {
position: absolute;
top: 3px;
right: -12px;
}
60 changes: 60 additions & 0 deletions assets/gutenberg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
( function( wp, $ ) {
var registerPlugin = wp.plugins.registerPlugin;
var PluginPostStatusInfo = wp.editPost.PluginPostStatusInfo;
var el = wp.element.createElement;
var __ = wp.i18n.__;

var checkBox = wp.components.CheckboxControl;
var withSelect = wp.data.withSelect;
var withDispatch = wp.data.withDispatch;
var compose = wp.compose.compose;

var metaCheckbox = compose(
withDispatch( function( dispatch, props ) {
return {
setMetaFieldValue: function( value ) {
dispatch( 'core/editor' ).editPost(
{ meta: { [ props.fieldName ]: value } }
);
}
}
} ),
withSelect( function( select, props ) {
return {
metaFieldValue: select( 'core/editor' ).getEditedPostAttribute( 'meta' )[ props.fieldName ],
}
} )

)( function( props ) {
// https://github.com/WordPress/gutenberg/tree/master/packages/components/src/checkbox-control
return el( checkBox, {
label: props.label,
checked: props.metaFieldValue,
onChange: function( retVal ) {
props.updater( retVal );
props.setMetaFieldValue( retVal );
},
} );
} );

var unlistedUpdater = function( retVal ) {
// If unlisted, switch post status to 'private'.
if ( retVal ) {
wp.data.dispatch( 'core/editor' ).editPost( { status: 'private' } );
}
};

registerPlugin( 'ray-unlisted-posts', {
render: function() {
return el( PluginPostStatusInfo, {
className: 'ray-unlisted'
},
el( metaCheckbox, {
label: __( 'Unlisted?' ),
fieldName: 'ray_unlisted',
updater: unlistedUpdater
} )
);
},
} );
} )( window.wp, jQuery );
44 changes: 28 additions & 16 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,38 @@ function rest_oembed( $retval, $server, $request ) {
}
add_filter( 'rest_request_before_callbacks', __NAMESPACE__ . '\\rest_oembed', 10, 3 );


/* GUTENBERG ********************************************************/

/**
* Add compatibility for the Classic Editor.
* Register our post meta for Gutenberg support.
*/
function load_classic() {
require_once DIR . '/hooks/classic.php';
function register_meta() {
\register_meta( 'post', 'ray_unlisted', array(
'show_in_rest' => true,
'single' => true,
'type' => 'boolean',
) );
}
add_action( 'post_submitbox_minor_actions', __NAMESPACE__ . '\\load_classic' );
add_action( 'init', __NAMESPACE__ . '\\register_meta' );

/**
* Saves our unlisted option if selected in the Classic Editor.
*
* @todo This needs an audit to see if it conflicts with Gutenberg.
*
* @param int $post_id Post ID.
* Register our assets for Gutenberg support.
*/
function save_unlisted_option( $post_id ) {
if ( empty( $_POST['ray_unlisted'] ) ) {
delete_post_meta( $post_id, 'ray_unlisted' );
} else {
update_post_meta( $post_id, 'ray_unlisted', 1 );
}
function register_assets() {
wp_register_script( 'ray-unlisted-posts', plugin_dir_url( __FILE__ ) . 'assets/gutenberg.js', array(
'wp-plugins', 'wp-edit-post', 'wp-element', 'wp-compose', 'wp-data'
) );

wp_register_style( 'ray-unlisted-posts', plugin_dir_url( __FILE__ ) . 'assets/gutenberg.css' );
}
add_action( 'init', __NAMESPACE__ . '\\register_assets' );

/**
* Enqueue our assets for Gutenberg support.
*/
function block_enqueue_assets() {
wp_enqueue_script( 'ray-unlisted-posts' );
wp_enqueue_style( 'ray-unlisted-posts' );
}
add_action( 'wp_insert_post', __NAMESPACE__ . '\\save_unlisted_option' );
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\block_enqueue_assets' );

0 comments on commit 298b601

Please sign in to comment.