{"id":18755289,"url":"https://github.com/sixach/wp-block-api","last_synced_at":"2025-07-24T12:37:02.524Z","repository":{"id":40410005,"uuid":"402505572","full_name":"sixach/wp-block-api","owner":"sixach","description":null,"archived":false,"fork":false,"pushed_at":"2023-12-15T14:45:52.000Z","size":80,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-01-31T05:04:49.242Z","etag":null,"topics":["library"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sixach.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-09-02T17:23:47.000Z","updated_at":"2022-05-03T22:07:22.000Z","dependencies_parsed_at":"2023-12-15T15:55:00.835Z","dependency_job_id":null,"html_url":"https://github.com/sixach/wp-block-api","commit_stats":{"total_commits":120,"total_committers":3,"mean_commits":40.0,"dds":"0.033333333333333326","last_synced_commit":"7c563856519e0c208dbeefa0166f5935b418beed"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sixach%2Fwp-block-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sixach%2Fwp-block-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sixach%2Fwp-block-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sixach%2Fwp-block-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sixach","download_url":"https://codeload.github.com/sixach/wp-block-api/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239644125,"owners_count":19673580,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["library"],"created_at":"2024-11-07T17:32:20.782Z","updated_at":"2025-02-19T11:15:03.738Z","avatar_url":"https://github.com/sixach.png","language":"PHP","readme":"# WordPress Block API\n\nSmall collection of helpful interfaces and abstract classes\nto make the development of WordPress Blocks and Extensions easier\nand more consistent.\n\n## Requirements\n\n* PHP version 7.3 or greater\n* WordPress version 5.7 or greater\n\n## Installation\n\nYou will need [Composer](https://getcomposer.org/) installed\non your computer in order to build and use this package.\n\n* Add this package to the `require` field in your `composer.json`\n* Import `vendor/autoload.php` in your project\n\n```bash\ncomposer require sixach/wp-block-api\n```\n\n---\n\n# Usage\n\n## In Blocks \u0026 Extensions\nSimply extend the block or extension class that fits the requirements of your package\nand extend all relevant interfaces (for extensions):\n\n```PHP\nnamespace Sixa_Blocks;\n\nfinal class My_Block extends Block {\n\n\tpublic static function register(): void {\n\t\tregister_block_type_from_metadata( plugin_dir_path( __DIR__ ) );\n\t}\n\n}\n```\n\n```PHP\nnamespace Sixa_Blocks;\n\nfinal class My_Extension extends Extension {\n\n\tpublic static function register(): void {\n\t\tFunctions::register_extension_from_metadata( plugin_dir_path( __DIR__ ) );\n\t}\n\n}\n```\n\nDevelopment and usage is simplified if all blocks and extensions use `namespace Sixa_Blocks`.\n\n### Create an `extension.json`\n\nRegistration of extensions is done using a JSON configuration file akin to the `block.json`\nfile that WordPress Core uses.\n\nExample:\n\n```JSON\n{\n\t\"name\": \"sixa-wp-extension-awesome-feature\",\n\t\"frontendScript\": \"file:./build/script.js\",\n\t\"frontendStyle\": \"file:./build/style.css\",\n\t\"script\": \"file:./build/both.js\",\n\t\"style\": \"file:./build/style-index.css\",\n\t\"editorScript\": \"file:./build/index.js\",\n\t\"editorStyle\": \"file:./build/index.css\",\n\t\"requires\": [\n\t\t\"sixa/add-to-cart\"\n\t]\n}\n```\n\nCurrently, `extension.json` uses the following fields:\n\n#### name\nDefines the name of the extension. This field is heavily utilized for asset handles and must be unique.\n\n#### frontendScript\nFile handle used for the script that's only enqueued in the frontend. Use `file:` prefix if you are\npassing a path to a local file. The path must be relative to `extension.json`.\n\n#### frontendStyle\nFile handle used for the style that's only enqueued in the frontend. Use `file:` prefix if you are\npassing a path to a local file. The path must be relative to `extension.json`.\n\n#### script\nFile handle used for the script that's enqueued in the editor and the frontend. Use `file:` prefix\nif you are passing a path to a local file. The path must be relative to `extension.json`.\n\n#### style\nFile handle used for the style that's enqueued in the editor and the frontend. Use `file:` prefix \nif you are passing a path to a local file. The path must be relative to `extension.json`.\n\n#### editorScript\nFile handle used for the script that's only enqueued in the editor. Use `file:` prefix if you are\npassing a path to a local file. The path must be relative to `extension.json`.\n\n#### editorStyle\nFile handle used for the style that's only enqueued in the editor. Use `file:` prefix if you are\npassing a path to a local file. The path must be relative to `extension.json`.\n\n#### requires\nAn array of block names that the given extension requires. During asset enqueueing, the post\ncontent is check if at least one of the passed blocks is present. If it is not, the extension\nassets are not enqueued to improve performance.\n\n## In Projects\nEach block and extension includes an `init` function that can be called to initialize\nthe class. No other function needs to be called.\n\n### As a Plugin\nTo run your block or extension as a standalone plugin, simply create a plugin file \n(e.g. `index.php`) that calls the `init` function of your class.\n\n```PHP\n\u003c?php\n/**\n * My Block.\n *\n * @wordpress-plugin\n * Plugin Name:          Sixa - My Block\n * Description:          My block for WordPress editor.\n * Version:              1.0.0\n * Requires at least:    5.7\n * Requires PHP:         7.2\n * Author:               sixa AG\n * License:              GPL v3 or later\n * License URI:          https://www.gnu.org/licenses/gpl-3.0.html\n * Text Domain:          sixa\n *\n * @package              sixa\n */\n\ndefined( 'ABSPATH' ) || exit; // Exit if accessed directly.\n\n/**\n * Include the namespace of this block.\n */\nuse Sixa_Blocks\\My_Block;\n\n/**\n * Composer autoload is needed in this package even if\n * it doesn't use any libraries to autoload the classes\n * from this package.\n *\n * @see    https://getcomposer.org/doc/01-basic-usage.md#autoloading\n */\nrequire __DIR__ . '/vendor/autoload.php';\n\n/**\n * Initialize your block.\n *\n * Other than this function call, this file should not include any logic\n * and should merely be used as an entry point to use this package as\n * a WordPress plugin.\n */\nMy_Block::init();\n```\n\n### As a package\nInstall your block or extension as a composer package and import `vendor/autoload.php`\nsomewhere in your project.\n\nNext, simply call the `init` function:\n\n```PHP\nSixa_Blocks\\My_Block::init();\n```\n\nNotice that there is no need to defer the `init` call to any WordPress hook in your project.\nBlocks or extensions implement all relevant hooks.\n\n---\n# Available Classes\n\nIn this section we elaborate on the available classes and interfaces and outline\nhow we intend them to be used.\n\n## Abstract Class Block\n\nA simple block class that includes the default implementation of a block class.\nParticularly, `Sixa_Blocks\\Block` includes and performs block initialization\nand adds `Sixa_Blocks\\Block::register()` to the WordPress action hook `init`.\n\n`Sixa_Blocks\\Block::register()` is not implemented. In its most basic form, a\nsimple block only needs to implement this function and perform `register_block_type_from_metadata`\naccording to the block requirements.\n\n### Basic Example\n\n```PHP\nnamespace Sixa_Blocks;\n\nfinal class My_Basic_Block extends Block {\n\n\tpublic static function register(): void {\n\t\tregister_block_type_from_metadata( plugin_dir_path( __DIR__ ) );\n\t}\n\n}\n```\n\nNotice the level `2` used in `dirname`. This is because the block class is inside a\ndirectory in the root of the project (i.e. not inside the same directory as `block.json`).\n\n### Intermediate Example\n\n```PHP\nnamespace Sixa_Blocks;\n\nfinal class My_Block extends Block {\n\n\tpublic static function register(): void {\n\t\tself::some_setup();\n\t\tregister_block_type_from_metadata(\n\t\t\tplugin_dir_path( __DIR__ ),\n\t\t\tarray(\n\t\t\t\t'render_callback' =\u003e array( __CLASS__, 'render' ),\n\t\t\t)\n\t\t);\n\t}\n\t\n\tpublic static function render( array $attributes = array() ): string {\n\t\t// Add your render callback here.\n\t}\n\n\tprivate static function some_setup(): void {\n\t\t// Add your setup code here (e.g. post type registration).\n\t}\n}\n```\n\n## Abstract Class WooCommerce Block\n\nA simple block class that extends `Sixa_Blocks\\Block` with a few additional convenience\nfunctions to enable WooCommerce specific block initialization.\nParticularly, for a set of WooCommerce blocks (i.e. blocks that require WooCommerce), \ninitialization and registration of the block is only necessary if WooCommerce is also installed.\nIn some cases the block might even produce a fatal error if WooCommerce is not activated.\n\nFor these instances, we intend to skip block registration.\n`Sixa_Blocks\\WooCommerce_Block` extends `Sixa_Blocks\\Block` with a simple check in\n`Sixa_Blocks\\WooCommerce_Block::init()` that prevents the block from being registered \n(i.e. `Sixa_Blocks\\Block::register()` from being added in `init`) if WooCommerce is not installed\nand activated.\n\nOther than that, `Sixa_Blocks\\WooCommerce_Block` is identical to `Sixa_Blocks\\Block`.\n\n\n### Basic Example\n\n```PHP\nnamespace Sixa_Blocks;\n\nfinal class My_WooCommerce_Block extends WooCommerce_Block {\n\n\tpublic static function register(): void {\n\t\tregister_block_type_from_metadata( plugin_dir_path( __DIR__ ) );\n\t}\n\n}\n```\n\n`Sixa_Blocks\\My_WooCommerce_Block::register()` is automatically **not** called if \nWooCommerce is not installed.\n\n## Abstract Class Extension\n\nA simple extension class that includes the default implementation of an extension. Particularly,\n`Sixa_Blocks\\Extension` includes and performs block initialization and adds \n`Sixa_Blocks\\Extension::register()` to the WordPress action hook `init`.\n\n`Sixa_Blocks\\Extension::register()` is not implemented. In its most basic form, a\nsimple block only needs to implement this function and perform \n`Sixa_Blocks\\Functions::register_extension_from_metadata` according to the  extension requirements.\n\n### Basic Example\n\n```PHP\nnamespace Sixa_Blocks;\n\nfinal class My_Extension extends Extension {\n\n\tpublic static function register(): void {\n\t\tFunctions::register_extension_from_metadata( plugin_dir_path( __DIR__ ) );\n\t}\n\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsixach%2Fwp-block-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsixach%2Fwp-block-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsixach%2Fwp-block-api/lists"}