From 06ebc63ed8108efe0156128f3fe889b3ad6d1e39 Mon Sep 17 00:00:00 2001 From: Maciej Palmowski Date: Tue, 4 Jun 2019 15:36:59 +0200 Subject: [PATCH] CS fixing --- .editorconfig | 24 +++++ composer.json | 6 +- composer.lock | 167 +++++++++++++++++++++++++++++++- phpcs.xml | 75 +++++++++++++++ timber-acf-wp-blocks.php | 201 ++++++++++++++++++++------------------- 5 files changed, 374 insertions(+), 99 deletions(-) create mode 100644 .editorconfig create mode 100644 phpcs.xml diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4e745fa --- /dev/null +++ b/.editorconfig @@ -0,0 +1,24 @@ +# This file is for unifying the coding style for different editors and IDEs +# editorconfig.org + +# WordPress Coding Standards +# http://make.wordpress.org/core/handbook/coding-standards/ + +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = tab + +[*.json] +indent_style = space +indent_size = 2 + +[*.md] +indent_style = space + +[*.txt,wp-config-sample.php] +end_of_line = crlf diff --git a/composer.json b/composer.json index 1102c18..bbb8f06 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "palmiak/timber_acf_wp_blocks", + "name": "palmiak/timber-acf-wp-blocks", "description": "Create Gutenberg blocks from Twig templates and ACF fields.", "keywords": ["wordpress", "gutenberg", "advanced custom fields"], "require": { @@ -21,5 +21,9 @@ ], "autoload": { "files": ["timber-acf-wp-blocks.php"] + }, + "require-dev": { + "wp-coding-standards/wpcs": "2.0", + "dealerdirect/phpcodesniffer-composer-installer": "0.5.0" } } diff --git a/composer.lock b/composer.lock index b03fa51..de02654 100644 --- a/composer.lock +++ b/composer.lock @@ -4,9 +4,172 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9d0ca1d587ce424fe3d2227840cd3f0e", + "content-hash": "5302a66693ac9b6dbd7939d8f3c5709f", "packages": [], - "packages-dev": [], + "packages-dev": [ + { + "name": "dealerdirect/phpcodesniffer-composer-installer", + "version": "v0.5.0", + "source": { + "type": "git", + "url": "https://github.com/Dealerdirect/phpcodesniffer-composer-installer.git", + "reference": "e749410375ff6fb7a040a68878c656c2e610b132" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Dealerdirect/phpcodesniffer-composer-installer/zipball/e749410375ff6fb7a040a68878c656c2e610b132", + "reference": "e749410375ff6fb7a040a68878c656c2e610b132", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^1.0", + "php": "^5.3|^7", + "squizlabs/php_codesniffer": "^2|^3" + }, + "require-dev": { + "composer/composer": "*", + "phpcompatibility/php-compatibility": "^9.0", + "sensiolabs/security-checker": "^4.1.0" + }, + "type": "composer-plugin", + "extra": { + "class": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin" + }, + "autoload": { + "psr-4": { + "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Franck Nijhof", + "email": "franck.nijhof@dealerdirect.com", + "homepage": "http://www.frenck.nl", + "role": "Developer / IT Manager" + } + ], + "description": "PHP_CodeSniffer Standards Composer Installer Plugin", + "homepage": "http://www.dealerdirect.com", + "keywords": [ + "PHPCodeSniffer", + "PHP_CodeSniffer", + "code quality", + "codesniffer", + "composer", + "installer", + "phpcs", + "plugin", + "qa", + "quality", + "standard", + "standards", + "style guide", + "stylecheck", + "tests" + ], + "time": "2018-10-26T13:21:45+00:00" + }, + { + "name": "squizlabs/php_codesniffer", + "version": "3.4.2", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8", + "reference": "b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "bin": [ + "bin/phpcs", + "bin/phpcbf" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", + "keywords": [ + "phpcs", + "standards" + ], + "time": "2019-04-10T23:49:02+00:00" + }, + { + "name": "wp-coding-standards/wpcs", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git", + "reference": "c9eaadaafefce36b3cb7e06eb15305b8c4cae9ce" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/WordPress-Coding-Standards/WordPress-Coding-Standards/zipball/c9eaadaafefce36b3cb7e06eb15305b8c4cae9ce", + "reference": "c9eaadaafefce36b3cb7e06eb15305b8c4cae9ce", + "shasum": "" + }, + "require": { + "php": ">=5.4", + "squizlabs/php_codesniffer": "^3.3.1" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0", + "phpcompatibility/php-compatibility": "^9.0", + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.4.3 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically." + }, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Contributors", + "homepage": "https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/graphs/contributors" + } + ], + "description": "PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions", + "keywords": [ + "phpcs", + "standards", + "wordpress" + ], + "time": "2019-01-16T10:13:16+00:00" + } + ], "aliases": [], "minimum-stability": "stable", "stability-flags": [], diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..6a015a8 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,75 @@ + + + A custom set of rules to check coding standards for Timber. + + + + + */tests/* + *.twig + + + ./lib + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/timber-acf-wp-blocks.php b/timber-acf-wp-blocks.php index 2c93a53..18ddfb3 100644 --- a/timber-acf-wp-blocks.php +++ b/timber-acf-wp-blocks.php @@ -1,112 +1,121 @@ isDot() && !$template->isDir()) { - //var_dump( $template ); - // Strip the file extension to get the slug - $slug = str_replace( '.twig', '', $template->getFilename() ); +add_action( + 'acf/init', + function () { + // Get an array of directories containing blocks. + $directories = apply_filters( 'timber/acf-gutenberg-blocks-templates', [] ); + // Check whether ACF exists before continuing. + foreach ( $directories as $dir ) { + // Sanity check whether the directory we're iterating over exists first. + if ( ! file_exists( \locate_template( $dir ) ) ) { + return; + } + // Iterate over the directories provided and look for templates. + $template_directory = new \DirectoryIterator( \locate_template( $dir ) ); + foreach ( $template_directory as $template ) { + + if ( ! $template->isDot() && ! $template->isDir() ) { + // Strip the file extension to get the slug. + $slug = str_replace( '.twig', '', $template->getFilename() ); + + // Get header info from the found template file(s). + $file_path = locate_template( $dir . "/${slug}.twig" ); + $file_headers = get_file_data( + $file_path, + [ + 'title' => 'Title', + 'description' => 'Description', + 'category' => 'Category', + 'icon' => 'Icon', + 'keywords' => 'Keywords', + 'mode' => 'Mode', + 'align' => 'Align', + 'post_types' => 'PostTypes', + 'supports_align' => 'SupportsAlign', + 'supports_mode' => 'SupportsMode', + 'supports_multiple' => 'SupportsMultiple', + ] + ); - // Get header info from the found template file(s) - $file_path = locate_template($dir."/${slug}.twig"); - $file_headers = get_file_data($file_path, [ - 'title' => 'Title', - 'description' => 'Description', - 'category' => 'Category', - 'icon' => 'Icon', - 'keywords' => 'Keywords', - 'mode' => 'Mode', - 'align' => 'Align', - 'post_types' => 'PostTypes', - 'supports_align' => 'SupportsAlign', - 'supports_mode' => 'SupportsMode', - 'supports_multiple' => 'SupportsMultiple', - ]); + if ( empty( $file_headers['title'] ) ) { + continue; + } + if ( empty( $file_headers['category'] ) ) { + continue; + } + // Set up block data for registration. + $data = [ + 'name' => $slug, + 'title' => $file_headers['title'], + 'description' => $file_headers['description'], + 'category' => $file_headers['category'], + 'icon' => $file_headers['icon'], + 'keywords' => explode( ' ', $file_headers['keywords'] ), + 'mode' => $file_headers['mode'], + 'render_callback' => 'timber_blocks_callback', + ]; + // If the PostTypes header is set in the template, restrict this block to those types. + if ( ! empty( $file_headers['post_types'] ) ) { + $data['post_types'] = explode( ' ', $file_headers['post_types'] ); + } + // If the SupportsAlign header is set in the template, restrict this block to those aligns. + if ( ! empty( $file_headers['supports_align'] ) ) { + $data['supports']['align'] = in_array( $file_headers['supports_align'], [ 'true', 'false' ], true ) ? filter_var( $file_headers['supports_align'], FILTER_VALIDATE_BOOLEAN ) : explode( ' ', $file_headers['supports_align'] ); + } + // If the SupportsMode header is set in the template, restrict this block mode feature. + if ( ! empty( $file_headers['supports_mode'] ) ) { + $data['supports']['mode'] = 'true' === $file_headers['supports_mode'] ? true : false; + } + // If the SupportsMultiple header is set in the template, restrict this block multiple feature. + if ( ! empty( $file_headers['supports_multiple'] ) ) { + $data['supports']['multiple'] = 'true' === $file_headers['supports_multiple'] ? true : false; + } + // Register the block with ACF. + acf_register_block_type( $data ); + } + } + } + } +); - if (empty($file_headers['title'])) { - continue; - } - if (empty($file_headers['category'])) { - continue; - } - // Set up block data for registration - $data = [ - 'name' => $slug, - 'title' => $file_headers['title'], - 'description' => $file_headers['description'], - 'category' => $file_headers['category'], - 'icon' => $file_headers['icon'], - 'keywords' => explode(' ', $file_headers['keywords']), - 'mode' => $file_headers['mode'], - 'render_callback' => 'timber_blocks_callback', - ]; - // If the PostTypes header is set in the template, restrict this block to those types - if (!empty($file_headers['post_types'])) { - $data['post_types'] = explode(' ', $file_headers['post_types']); - } - // If the SupportsAlign header is set in the template, restrict this block to those aligns - if (!empty($file_headers['supports_align'])) { - $data['supports']['align'] = in_array($file_headers['supports_align'], ['true', 'false'], true) ? filter_var($file_headers['supports_align'], FILTER_VALIDATE_BOOLEAN) : explode(' ', $file_headers['supports_align']); - } - // If the SupportsMode header is set in the template, restrict this block mode feature - if (!empty($file_headers['supports_mode'])) { - $data['supports']['mode'] = $file_headers['supports_mode'] === 'true' ? true : false; - } - // If the SupportsMultiple header is set in the template, restrict this block multiple feature - if (!empty($file_headers['supports_multiple'])) { - $data['supports']['multiple'] = $file_headers['supports_multiple'] === 'true' ? true : false; - } - - // Register the block with ACF - acf_register_block_type($data); - } - } - //die(); - } -}); /** * Callback to register blocks */ -function timber_blocks_callback($block, $content = '', $is_preview = false, $post_id = 0) -{ - // Set up the slug to be useful - $context = Timber::get_context(); - $slug = str_replace('acf/', '', $block['name']); +function timber_blocks_callback( $block, $content = '', $is_preview = false, $post_id = 0 ) { + // Set up the slug to be useful. + $context = Timber::get_context(); + $slug = str_replace( 'acf/', '', $block['name'] ); - $context['block'] = $block; - $context['post_id'] = $post_id; - $context['slug'] = $slug; - $context['is_preview'] = $is_preview; - $context['fields'] = get_fields(); + $context['block'] = $block; + $context['post_id'] = $post_id; + $context['slug'] = $slug; + $context['is_preview'] = $is_preview; + $context['fields'] = get_fields(); - Timber::render("views/blocks/${slug}.twig", $context); + Timber::render( "views/blocks/${slug}.twig", $context ); }