Skip to content

Instantly share code, notes, and snippets.

View httpstersk's full-sized avatar

Lubos Belak httpstersk

View GitHub Profile
@httpstersk
httpstersk / gutenberg-snippets-26.js
Created February 22, 2019 10:46
➋➏ Get a revisions count of the current post
const { select } = wp.data;
const { getCurrentPostRevisionsCount } = select( 'core/editor' );
const revisions = getCurrentPostRevisionsCount();
@httpstersk
httpstersk / gutenberg-snippets-25.js
Created February 20, 2019 13:29
➋➎ Get the post author's display name
const { select } = wp.data;
const { getAuthors } = select( 'core' );
const { getEditedPostAttribute } = select( 'core/editor' );
const authorID = getEditedPostAttribute( 'author' );
const { name } = getAuthors().find(a => a.id === authorID);
@httpstersk
httpstersk / gutenberg-snippets-24.js
Created February 10, 2019 15:44
➋➍ Schedule a post for tomorrow (24 hours from now)
const { dispatch } = wp.data;
const { editPost } = dispatch( 'core/editor' );
const NOW = new Date();
const ONE_DAY_IN_MS = 24 * 60 * 60 * 1000;
const TOMORROW = new Date( NOW.getTime() + ONE_DAY_IN_MS );
const schedulePost = date => editPost( { date } );
schedulePost( TOMORROW );
@httpstersk
httpstersk / gutenberg-snippets-23.js
Last active February 9, 2019 17:37
➋➌ Save a post as a draft
const { dispatch } = wp.data;
const { editPost, savePost } = dispatch( 'core/editor' );
const setStatusToDraft = ( async () => {
await editPost( { status: 'draft' } );
await savePost();
} )();
@httpstersk
httpstersk / gutenberg-snippets-22.js
Created February 6, 2019 14:40
➋➋ Check if the post has any Gutenberg blocks
const { select } = wp.data;
const { getBlockCount } = select( 'core/editor' );
const hasBlocks = () => !! getBlockCount();
hasBlocks();
@httpstersk
httpstersk / gutenberg-snippets-21.js
Created February 5, 2019 17:25
➋➊ Count the words of the post content
const { select } = wp.data;
const { count } = wp.wordcount;
const { getEditedPostAttribute } = select( 'core/editor' );
const content = getEditedPostAttribute( 'content' );
const countWords = count( content, 'words' );
@httpstersk
httpstersk / gutenberg-snippets-20.js
Created February 4, 2019 17:36
➋×➓ Check if a logged-in user can upload files
const { select } = wp.data;
const { hasUploadPermissions } = select( 'core' );
const canUpload = hasUploadPermissions();
@httpstersk
httpstersk / gutenberg-snippets-19.js
Created February 3, 2019 13:47
➊➒ Get the maximum upload size allowed
const { select } = wp.data;
const { getEditorSettings } = select( 'core/editor' );
const { maxUploadFileSize } = getEditorSettings();
const maxMB = Number( maxUploadFileSize / Math.pow( 1024, 2 ) );
@httpstersk
httpstersk / gutenberg-snippets-18.js
Created January 31, 2019 20:15
➊➑ Retrieve the details of a specific post
const POST_ID = 25;
const { select } = wp.data;
const { getEntityRecords } = select( 'core' );
const [ postDetails ] = getEntityRecords( 'postType', 'post', { include: POST_ID } );
@httpstersk
httpstersk / gutenberg-snippets-17.js
Created January 30, 2019 12:23
➊➐ Get a list of registered taxonomy objects
const { select } = wp.data;
const { getTaxonomies } = select( 'core' );
const taxonomies = getTaxonomies();