Skip to content

Instantly share code, notes, and snippets.

@thelovekesh
Created April 12, 2022 15:14
Show Gist options
  • Select an option

  • Save thelovekesh/059236de6a2e7c17d1437618017f9cce to your computer and use it in GitHub Desktop.

Select an option

Save thelovekesh/059236de6a2e7c17d1437618017f9cce to your computer and use it in GitHub Desktop.

Features Plugin POC

This plugin is a POC to introduce new Plguin Skeleton for future plugin projects which ships with new functionalites discussed below.

Plugin Structure

Expand Plugin Structure
|-- Features_Plugin
    |-- .browserslistrc
    |-- .editorconfig
    |-- .eslintignore
    |-- .eslintrc
    |-- .gitignore
    |-- .lando.yml
    |-- .lintstagedrc.js
    |-- .npmrc
    |-- .nvmrc
    |-- .stylelintignore
    |-- .stylelintrc.json
    |-- .wp-env.json
    |-- README.md
    |-- babel.config.js
    |-- composer.json
    |-- composer.lock
    |-- features-plugin-v2.php
    |-- package-lock.json
    |-- package.json
    |-- phpcs.xml.dist
    |-- phpunit.xml.dist
    |-- webpack.config.js
    |-- .lando
    |   |-- nginx.conf
    |   |-- php.ini
    |   |-- wp-cli.yml
    |   |-- xdebug.sh
    |-- assets
    |   |-- src
    |       |-- blocks
    |       |   |-- todo
    |       |       |-- edit.js
    |       |       |-- editor.scss
    |       |       |-- index.js
    |       |       |-- save.js
    |       |       |-- style.scss
    |       |-- components
    |       |   |-- addition
    |       |   |   |-- index.js
    |       |   |   |-- test
    |       |   |       |-- addition.js
    |       |   |-- multiplication
    |       |       |-- index.js
    |       |       |-- test
    |       |           |-- multiplication.js
    |       |-- css
    |       |   |-- example.scss
    |       |-- js
    |           |-- example-asset-admin.js
    |           |-- example-asset-customizer.js
    |           |-- example-asset.js
    |           |-- example.js
    |-- bin
    |   |-- husky.sh
    |   |-- phpcbf.sh
    |   |-- pre-commit-hook.sh
    |   |-- create-block
    |       |-- create-block.js
    |       |-- template
    |           |-- block
    |               |-- block.php
    |               |-- edit.js
    |               |-- editor.scss
    |               |-- index.js
    |               |-- save.js
    |               |-- style.scss
    |-- includes
    |   |-- REST
    |   |   |-- example-route
    |   |       |-- class-rest-example.php
    |   |       |-- class-test-route.php
    |   |-- assets
    |   |   |-- class-example-asset.php
    |   |-- blocks
    |   |   |-- class-example-block.php
    |   |-- metaboxes
    |   |   |-- class-example-metabox.php
    |   |   |-- class-fm-example-metabox.php
    |   |-- post-types
    |   |   |-- class-example-cpt.php
    |   |-- rewrite-rules
    |   |   |-- class-example-rewrite-rule.php
    |   |-- taxonomy
    |   |   |-- class-example-taxonomy.php
    |   |-- widgets
    |       |-- class-example-widget.php
    |-- src
    |   |-- Plugin.php
    |   |-- Interfaces
    |   |   |-- Fieldmanager.php
    |   |   |-- Manager.php
    |   |   |-- Module.php
    |   |-- Managers
    |   |   |-- AssetManager.php
    |   |   |-- BlockManager.php
    |   |   |-- MetaBoxManager.php
    |   |   |-- PostTypeManager.php
    |   |   |-- RESTRouteManager.php
    |   |   |-- RewriteRuleManager.php
    |   |   |-- TaxonomyManager.php
    |   |   |-- WidgetManager.php
    |   |-- Modules
    |       |-- Asset.php
    |       |-- Block.php
    |       |-- MetaBox.php
    |       |-- PostType.php
    |       |-- RewriteRule.php
    |       |-- Taxonomy.php
    |       |-- Labels
    |       |   |-- Labels.php
    |       |   |-- PostTypeLabels.php
    |       |   |-- TaxonomyLabels.php
    |       |-- REST
    |           |-- RESTContainer.php
    |           |-- Route.php
    |-- tests
        |-- js
        |   |-- jest.config.js
        |   |-- setup-globals.js
        |-- logs
        |   |-- lcov.info
        |   |-- lcov-report
        |       |-- base.css
        |       |-- block-navigation.js
        |       |-- favicon.png
        |       |-- index.html
        |       |-- prettify.css
        |       |-- prettify.js
        |       |-- sort-arrow-sprite.png
        |       |-- sorter.js
        |       |-- addition
        |       |   |-- index.html
        |       |   |-- index.js.html
        |       |-- multiplication
        |           |-- index.html
        |           |-- index.js.html
        |-- php
            |-- .phpunit.result.cache
            |-- DataProvider.php
            |-- TestCase.php
            |-- bootstrap.php
            |-- includes
            |   |-- REST
            |   |   |-- example-route
            |   |       |-- class-rest-example-test.php
            |   |       |-- class-test-route-test.php
            |   |-- assets
            |   |   |-- class-example-asset-test.php
            |   |-- blocks
            |   |   |-- class-example-block-test.php
            |   |-- metaboxes
            |   |   |-- class-example-metabox-test.php
            |   |-- post-types
            |   |   |-- class-example-cpt-tests.php
            |   |-- rewrite-rules
            |   |   |-- class-example-rewrite-rule-test.php
            |   |-- taxonomy
            |   |   |-- class-example-taxonomy-test.php
            |   |-- widgets
            |       |-- class-example-widget-test.php
            |-- src
                |-- PluginTest.php
                |-- Managers
                |   |-- AssetManagerTest.php
                |   |-- BlockManagerTest.php
                |   |-- MetaBoxManagerTest.php
                |   |-- PostTypeManagerTest.php
                |   |-- RESTRouteManagerTest.php
                |   |-- RewriteRuleManagerTest.php
                |   |-- TaxonomyManagerTest.php
                |   |-- WidgetManagerTest.php
                |-- Modules
                    |-- BlockTest.php
                    |-- PostTypeTest.php
                    |-- RewriteRuleTest.php
                    |-- TaxonomyTest.php

Features

  • Plugin is build on the top of OOPs SOLID principle.
  • Plugin is auto-configured with package managers like Composer and NPM.
  • PHPCS and PHPCBF can be exececuted from npm commands npm run lint:php and npm run lint:php:fix
  • PHPUnit and Jest are pre-configured for PHP and JS unit testing.
  • Plugin comes with the lando and docker based developemnt environemts.
    • Run lando start to start dev environment
    • Run npm run test:php to run phpunit tests in docker(uses wp-env internally)
  • Asset Bundling, JS transpiling, Linting with WPCS for PHP, JS CSS/SCSS is pre-configured.
  • Create Block functioanlity is shiiped with the plugin itself. Just run npm run create-block <block-name> and custom <block-name> is ready to use.
  • Also comes with Pre commit hook setup:
    • Run npm run install:husky to setup husky for pre-commit hook
    • Run npm run install:pre-commit-hook to setup bash based pre-commit hook
    • To remove pre-commit hooks then you can use npm run remove:(husky|pre-commit-hook) commands.

DEMO

Register a block

Common way to register a block

class Example_Block {
	public function __construct() {
		add_action( 'init', array( $this, 'register_block' ) );
	}

	public function register_block() {
		register_block_type(
			'features-plugin-v2/example-block',
			array(
				// ...args
			)
		);
	}
}

With new Features Plugin Core

class Example_Block extends Block {
	public function build() {
		$this->set_block_type( 'features-plugin-v2/example-block' )
			->set_block_type_args(
				array(
					// ...args
				)
			);
	}
}
Register a post type

Common way to register a CPT

class Example_Post_Type {
	public function __construct() {
		add_action( 'init', array( $this, 'register_post_type' ) );
	}

	public function register_post_type() {
		register_post_type( 'example', array(
			'labels' => array(
				'name' => __( 'Example' ),
				'singular_name' => __( 'Example' ),
			),
			'public' => true,
			'has_archive' => true,
			'rewrite' => array(
				'slug' => 'example',
			),
			// ... other arguments
		) );
	}
}

With new Features Plugin Core

class Example_Post_Type extends PostType {
	public function build() {
		
		$this->labels->set_name( __( 'CPT', 'features-plugin-v2' ) );
		$this->labels->set_singular_name( __( 'CPT', 'features-plugin-v2' ) )
			->set_menu_name( __( 'Example CPT', 'features-plugin-v2' ) );

		$this->set_name( 'CPT' )
			->set_slug( 'cpt' )
			->has_archive()
			->is_public()
			->show_in_rest()
			->menu_position( 5 )
			->set_menu_icon( 'dashicons-book' );
	}
}

Same way can be followed to register post types, taxonomies. Basically for all things that we have added as Modules. Since arguments are defined as methods in plugin core, when you will use a module( e.g. register post type ), your IDE intellisense will automatically suggests all the arguments need to be passed in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment