Skip to content

Instantly share code, notes, and snippets.

View EdwardBock's full-sized avatar
🚒

Edward EdwardBock

🚒
View GitHub Profile
@EdwardBock
EdwardBock / README.md
Created December 18, 2021 15:45 — forked from justincy/README.md
Configure Storybook to work with Next.js, TypeScript, and CSS Modules

In addition to the Storybook for React setup, you'll also need to install these packages:

npm i -D @babel/core babel-loader css-loader style-loader
@EdwardBock
EdwardBock / ReadingTimeFeature.php
Last active October 17, 2021 13:17
Theme interface segregation - proof of concept
<?php
namespace PublicFunctionOrg\WordPress\Theme\Sample;
interface ReadingTimeFeature{
public function render($post_id);
}
@EdwardBock
EdwardBock / functions.php
Last active October 17, 2021 11:56
Add reading time to theme with a proxy function.
<?php
namespace PublicFunctionOrg\Theme\ReadingTimeSample;
// ...
function reading_time($post_id){
if(function_exists('reading_time_render')){
reading_time_render($post_id);
}
}
@EdwardBock
EdwardBock / reading-time-public-functions.php
Last active October 17, 2021 11:34
Public render function of the reading time plugin with overwritable template
<?php
function reading_time_render($post_id){
// https://medium.com/write-better-wordpress-code/do-not-use-post-meta-fec12a7661
$db = new \PublicFunctionOrg\WordPress\ReadingTime\Database();
$reading_time = $db->get($postId);
if(!$reading_time) return;
// https://medium.com/write-better-wordpress-code/provide-overwritable-templates-from-your-plugin-c5205f8a3138
$templates = new \PublicFunctionOrg\WordPress\Templates();
@EdwardBock
EdwardBock / reading-time-by-gutenberg-block.js
Last active October 17, 2021 11:34
Add reading time to post content with a custom Gutenberg block.
import {registerBlockType} from '@wordpress/blocks';
import ServerSideRender from '@wordpress/server-side-render';
registerBlockType( 'public-function-org/reading-time', {
title: "Reading time",
icon: 'clock',
category: 'common',
edit: () => {
return <ServerSideRender
block="public-function-org/reading-times"
@EdwardBock
EdwardBock / reading-time-by-custom-shortcode.php
Last active October 17, 2021 11:35
Add reading time to post content with a custom shortcode.
<?php
// [reading_time]
// [reading_time post_id="..."]
add_shortcode( 'reading_time', function($atts){
$a = shortcode_atts( array(
'post_id' => false,
), $atts );
@EdwardBock
EdwardBock / reading-time-by-the-content-filter.php
Last active October 17, 2021 11:36
Add reading time to post content with the_content filter.
<?php
add_filter("the_content", function($content, $post){
if ( is_singular() && in_the_loop() && is_main_query() ) {
// https://gist.github.com/EdwardBock/fa659b67bcf4f568289d0c49c175f42b
$add = reading_time_get_content($post->ID);
return $add.$content;
@EdwardBock
EdwardBock / reading-time-sidebar.js
Last active October 1, 2021 17:32
Add reading time field to a custom sidebar
import { registerPlugin } from "@wordpress/plugins";
import { PluginSidebarMoreMenuItem, PluginSidebar } from "@wordpress/edit-post";
import { TextControl } from "@wordpress/components";
import { useReadingTime } from "./reading-time-hooks.js";
const name = "reading-time-sidebar";
registerPlugin(
"reading-time",
@EdwardBock
EdwardBock / reading-time-document-setting-panel.js
Last active October 17, 2021 10:28
Add reading time field to document setting panel
import { registerPlugin } from "@wordpress/plugins";
import { PluginDocumentSettingPanel } from "@wordpress/edit-post";
import { TextControl } from "@wordpress/components";
import {useReadingTime} from "./reading-time-hooks.js";
registerPlugin(
"reading-time",
{
render: ()=> {
@EdwardBock
EdwardBock / reading-time-post-status-info.js
Last active October 1, 2021 15:47
Add reading time field to post status info
import { registerPlugin } from "@wordpress/plugins";
import { PluginPostStatusInfo } from "@wordpress/edit-post";
import { TextControl } from "@wordpress/components";
import {useReadingTime} from "./reading-time-hooks.js";
registerPlugin(
'reading-time',
{
render: ()=> {