{"id":20711806,"url":"https://github.com/helick/blocks","last_synced_at":"2025-04-23T06:48:42.784Z","repository":{"id":56983919,"uuid":"192779754","full_name":"helick/blocks","owner":"helick","description":"Easily create Gutenberg blocks with Carbon Fields","archived":false,"fork":false,"pushed_at":"2019-11-26T20:56:28.000Z","size":50,"stargazers_count":6,"open_issues_count":1,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-23T06:48:36.293Z","etag":null,"topics":["composer","wordpress","wordpress-plugin"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/helick.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-19T17:51:47.000Z","updated_at":"2022-05-31T16:32:03.000Z","dependencies_parsed_at":"2022-08-21T11:20:39.711Z","dependency_job_id":null,"html_url":"https://github.com/helick/blocks","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helick%2Fblocks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helick%2Fblocks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helick%2Fblocks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helick%2Fblocks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/helick","download_url":"https://codeload.github.com/helick/blocks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250386746,"owners_count":21422026,"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":["composer","wordpress","wordpress-plugin"],"created_at":"2024-11-17T02:17:01.411Z","updated_at":"2025-04-23T06:48:42.759Z","avatar_url":"https://github.com/helick.png","language":"PHP","readme":"# Helick Block\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Total Downloads][ico-downloads]][link-downloads]\n[![Software License][ico-license]](LICENSE.md)\n[![Quality Score][ico-code-quality]][link-code-quality]\n\nThe package assists you in easily creating Gutenberg blocks with Carbon Fields.\n\n## Requirements\n\nMake sure all dependencies have been installed before moving on:\n\n* [PHP](http://php.net/manual/en/install.php) \u003e= 7.1\n* [Composer](https://getcomposer.org/download/)\n* [Carbon Fields](https://docs.carbonfields.net/#/quickstart) \u003e= 3.0\n\n## Install\n\nInstall via Composer:\n\n``` bash\n$ composer require helick/blocks\n```\n\n## Usage\n\nWithin your theme declare your block, attach its fields, and provide data for your template:\n\n``` php\nuse Carbon_Fields\\Field;\nuse Helick\\Blocks\\Block;\nuse WP_Query;\n\nfinal class ExampleBlock extends Block\n{\n    /**\n     * The block's display name.\n     *\n     * @var string\n     */\n    protected $name = 'Example';\n\n    /**\n     * The block's description.\n     *\n     * @var string\n     */\n    protected $description = 'This is an example block';\n\n    /**\n     * The block's template.\n     *\n     * @var string|string[]\n     */\n    protected $template = 'partials/blocks/example.php';\n\n    /**\n     * Fields to be attached to the block.\n     *\n     * @return array\n     */\n    public function fields(): array\n    {\n        return [\n            Field::make('text', 'heading', 'Heading'),\n            Field::make('image', 'image', 'Image'),\n            Field::make('rich_text', 'content', 'Content'),\n            Field::make('association', 'associations', 'Associations')\n                 -\u003eset_types([\n                     [\n                         'type'      =\u003e 'post',\n                         'post_type' =\u003e 'post',\n                     ]\n                 ])\n        ];\n    }\n\n    /**\n     * Data to be passed to the rendered block.\n     *\n     * @param array $fields\n     *\n     * @return array\n     */\n    public function with(array $fields): array\n    {\n        return [\n            'associations' =\u003e $this-\u003equeryAssociations($fields['associations'])\n        ];\n    }\n\n    /**\n     * Query the associations.\n     *\n     * @param array $associations\n     *\n     * @return WP_Query\n     */\n    private function queryAssociations(array $associations): WP_Query\n    {\n        $associationIds = array_column($associations, 'id');\n        $associationIds = array_map('intval', $associationIds);\n\n        return new WP_Query([\n            'no_found_rows' =\u003e true,\n            'post__in'      =\u003e $associationIds,\n            'orderby'       =\u003e 'post__in',\n        ]);\n    }\n}\n\n```\n\nCreate your block template:\n\n``` php\n\u003cdiv class=\"block\"\u003e\n    \u003cdiv class=\"block__heading\"\u003e\n        \u003ch1\u003e\u003c?= esc_html($fields['heading']) ?\u003e\u003c/h1\u003e\n    \u003c/div\u003e\n    \u003cdiv class=\"block__image\"\u003e\n        \u003c?= wp_get_attachment_image($fields['image'], 'full') ?\u003e\n    \u003c/div\u003e\n    \u003cdiv class=\"block__content\"\u003e\n        \u003c?= apply_filters('the_content', $fields['content']) ?\u003e\n    \u003c/div\u003e\n    \u003c?php if ($associations-\u003ehave_posts()) : ?\u003e\n        \u003cdiv class=\"block__associations\"\u003e\n            \u003cul class=\"block__associations-list\"\u003e\n                \u003c?php while ($associations-\u003ehave_posts()) : $associations-\u003ethe_post(); ?\u003e\n                    \u003cli class=\"block__associations-item\"\u003e\n                        \u003ca class=\"block__associations-link\" href=\"\u003c?= esc_url(get_the_permalink()) ?\u003e\"\u003e\n                            \u003c?= esc_html(get_the_title()) ?\u003e\n                        \u003c/a\u003e\n                    \u003c/li\u003e\n                \u003c?php endwhile; ?\u003e\n            \u003c/ul\u003e\n        \u003c/div\u003e\n        \u003c?php wp_reset_postdata(); ?\u003e\n    \u003c?php endif; ?\u003e\n\u003c/div\u003e\n\n```\n\nFinally, register your block in theme's `functions.php`:\n\n``` php\nExampleBlock::boot();\n```\n\n## Caching\n\nThe easiest and probably the best method is to cache the **complete HTML output**, and PHP's output buffering functions will help us implement that without moving too much code around:\n\n``` php\nuse Carbon_Fields\\Field;\nuse Helick\\Blocks\\Block;\nuse Exception;\n\nfinal class ExampleBlock extends Block\n{\n    // Your block declaration goes in here ...\n\n    /**\n     * Render the block.\n     *\n     * @param array $fields\n     * @param array $attributes\n     * @param array $blocks\n     *\n     * @return void\n     *\n     * @throws Exception\n     */\n    public function render(array $fields, array $attributes, array $blocks): void\n    {\n        // Compose the render arguments\n        $args = compact('fields', 'attributes', 'blocks');\n\n        // Generate cache key based on the given arguments\n        $cacheKey   = sprintf('example_block_%s', hash('md5', wp_json_encode($args)));\n        $cacheGroup = 'blocks';\n\n        // Check whether we have the block's cached output\n        $output = wp_cache_get($cacheKey, $cacheGroup);\n\n        // If nothing is found, catch the block render output\n        if (false === $output) {\n            ob_start();\n\n            try {\n                parent::render($fields, $attributes, $blocks);\n            } catch (Exception $e) {\n                // In case something goes wrong we clear the output buffer\n                ob_end_clean();\n\n                // Re-throw an exception so we don't cache the actual error output\n                throw $e;\n            }\n\n            $output = ob_get_clean();\n\n            // Cache the block's output for 5 minutes (300 secs)\n            wp_cache_set($cacheKey, $output, $cacheGroup, 5 * MINUTE_IN_SECONDS);\n        }\n\n        echo $output;\n        echo \"\u003c!-- Cache Key: {$cacheKey} --\u003e\";\n    }\n}\n```\n\nWith this way we're only storing the actual output in our cache, no posts, no metadata, no terms. Just the HTML.\n\nYou can also inspect your cache by using [WP CLI](https://wp-cli.org/):\n\n``` bash\n# Get the block's output from the object cache\n$ wp cache get example_block_098f6bcd4621d373cade4e832627b4f6 blocks\n...your block's output...\n\n# Remove the block's output from the object cache\n$ wp cache delete example_block_098f6bcd4621d373cade4e832627b4f6 blocks\nSuccess: Object deleted.\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) and [CODE_OF_CONDUCT](CODE_OF_CONDUCT.md) for details.\n\n## Security\n\nIf you discover any security related issues, please email evgenii@helick.io instead of using the issue tracker.\n\n## Credits\n\n- [Evgenii Nasyrov][link-author]\n- [All Contributors][link-contributors]\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n[ico-version]: https://img.shields.io/packagist/v/helick/blocks.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[ico-code-quality]: https://img.shields.io/scrutinizer/g/helick/blocks.svg?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/helick/blocks.svg?style=flat-square\n\n[link-packagist]: https://packagist.org/packages/helick/blocks\n[link-code-quality]: https://scrutinizer-ci.com/g/helick/blocks\n[link-downloads]: https://packagist.org/packages/helick/blocks\n[link-author]: https://github.com/nasyrov\n[link-contributors]: ../../contributors\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelick%2Fblocks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhelick%2Fblocks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelick%2Fblocks/lists"}