From 7a43c8d2617022094e0bcbeedc55a604f22e0577 Mon Sep 17 00:00:00 2001 From: Brandon Date: Sat, 6 Feb 2021 19:22:07 -0600 Subject: [PATCH 1/8] =?UTF-8?q?wip(poet):=20Modularize=20Poet=20(WIP=20?= =?UTF-8?q?=E2=80=93=20VERY=20BROKEN)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 4 +- composer.lock | 82 +++--- src/Exceptions/LifecycleException.php | 10 + src/Modules/AdminMenuModule.php | 55 ++++ src/Modules/AnchorModule.php | 60 ++++ src/Modules/BlockCategoryModule.php | 72 +++++ src/Modules/BlockModule.php | 76 +++++ src/Modules/EditorPaletteModule.php | 65 +++++ src/Modules/Module.php | 39 +++ src/Modules/PostTypeModule.php | 89 ++++++ src/Modules/TaxonomyModule.php | 53 ++++ src/Poet.php | 382 ++------------------------ 12 files changed, 575 insertions(+), 412 deletions(-) create mode 100644 src/Exceptions/LifecycleException.php create mode 100644 src/Modules/AdminMenuModule.php create mode 100644 src/Modules/AnchorModule.php create mode 100644 src/Modules/BlockCategoryModule.php create mode 100644 src/Modules/BlockModule.php create mode 100644 src/Modules/EditorPaletteModule.php create mode 100644 src/Modules/Module.php create mode 100644 src/Modules/PostTypeModule.php create mode 100644 src/Modules/TaxonomyModule.php diff --git a/composer.json b/composer.json index c45cca7..fd48e29 100644 --- a/composer.json +++ b/composer.json @@ -24,8 +24,8 @@ }, "require": { "php": ">=7.2.5", - "johnbillion/extended-cpts": "^4.4", - "caseyamcl/toc": "^3.0" + "johnbillion/extended-cpts": "^4.3", + "caseyamcl/toc": "^2.2" }, "require-dev": { "squizlabs/php_codesniffer": "^3.5" diff --git a/composer.lock b/composer.lock index 94b12f3..1eb8a64 100644 --- a/composer.lock +++ b/composer.lock @@ -4,20 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7bef00cba1d9e826f99695b4615a917e", + "content-hash": "e1e6e0c9b71547eae7848bb08809f384", "packages": [ { "name": "caseyamcl/toc", - "version": "v3.0.1", + "version": "v2.2", "source": { "type": "git", "url": "https://github.com/caseyamcl/toc.git", - "reference": "8eb23fcb5166b9cb957a7b8afba2b95b91e681f9" + "reference": "27edd0deb48c73f30f8166d5021c9ab5c080f963" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/caseyamcl/toc/zipball/8eb23fcb5166b9cb957a7b8afba2b95b91e681f9", - "reference": "8eb23fcb5166b9cb957a7b8afba2b95b91e681f9", + "url": "https://api.github.com/repos/caseyamcl/toc/zipball/27edd0deb48c73f30f8166d5021c9ab5c080f963", + "reference": "27edd0deb48c73f30f8166d5021c9ab5c080f963", "shasum": "" }, "require": { @@ -25,12 +25,10 @@ "ext-dom": "*", "knplabs/knp-menu": "^3.0", "masterminds/html5": "^2.1", - "php": "^7.1|^8.0" + "php": "^7.1" }, "require-dev": { - "jaschilz/php-coverage-badger": "^2.0", - "phpstan/phpstan": "^0.12", - "phpunit/phpunit": "^7.5|^8.0|^9.0", + "phpunit/phpunit": "^7.5|^8.0", "squizlabs/php_codesniffer": "^3.5", "twig/twig": "^2.4|^3.0" }, @@ -38,8 +36,8 @@ "autoload": { "psr-4": { "TOC\\": [ - "src", - "tests" + "src/", + "tests/" ] } }, @@ -61,7 +59,7 @@ "TOC", "table of contents" ], - "time": "2020-12-07T19:20:03+00:00" + "time": "2020-04-12T15:23:17+00:00" }, { "name": "cocur/slugify", @@ -135,32 +133,32 @@ }, { "name": "johnbillion/extended-cpts", - "version": "4.4.1", + "version": "4.3.3", "source": { "type": "git", "url": "https://github.com/johnbillion/extended-cpts.git", - "reference": "b62d104bffbf9998a1063879565f61c289f0c6a9" + "reference": "4c4e292781c28a7d8666803f63c44e655cddf7ad" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/johnbillion/extended-cpts/zipball/b62d104bffbf9998a1063879565f61c289f0c6a9", - "reference": "b62d104bffbf9998a1063879565f61c289f0c6a9", + "url": "https://api.github.com/repos/johnbillion/extended-cpts/zipball/4c4e292781c28a7d8666803f63c44e655cddf7ad", + "reference": "4c4e292781c28a7d8666803f63c44e655cddf7ad", "shasum": "" }, "require": { "php": "^7.0" }, "require-dev": { - "automattic/phpcs-neutron-standard": "1.5.4", - "dealerdirect/phpcodesniffer-composer-installer": "^0.7", + "automattic/phpcs-neutron-standard": "^1.5", + "dealerdirect/phpcodesniffer-composer-installer": "^0.5", "johnbillion/falsey-assertequals-detector": "^2", "johnbillion/php-docs-standards": "^1", "phpcompatibility/phpcompatibility-wp": "^2", "phpunit/phpunit": "^6", "roots/wordpress": "*", "vlucas/phpdotenv": "^3", - "wp-cli/db-command": "^2.0", - "wp-coding-standards/wpcs": "2.3.0", + "wp-cli/wp-cli-bundle": "^2.1", + "wp-coding-standards/wpcs": "~2.1.0", "wp-phpunit/wp-phpunit": "*" }, "suggest": { @@ -191,30 +189,24 @@ ], "description": "A library which provides extended functionality to WordPress custom post types and taxonomies.", "homepage": "https://github.com/johnbillion/extended-cpts/", - "funding": [ - { - "url": "https://github.com/johnbillion", - "type": "github" - } - ], - "time": "2020-12-05T20:15:02+00:00" + "time": "2020-04-22T16:13:18+00:00" }, { "name": "knplabs/knp-menu", - "version": "v3.1.2", + "version": "v3.1.1", "source": { "type": "git", "url": "https://github.com/KnpLabs/KnpMenu.git", - "reference": "9996764bc4bdc4381ecdc4617817de23948f0cdb" + "reference": "254fb64d545f087451cc197a7fa5d47e03ffb038" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/KnpLabs/KnpMenu/zipball/9996764bc4bdc4381ecdc4617817de23948f0cdb", - "reference": "9996764bc4bdc4381ecdc4617817de23948f0cdb", + "url": "https://api.github.com/repos/KnpLabs/KnpMenu/zipball/254fb64d545f087451cc197a7fa5d47e03ffb038", + "reference": "254fb64d545f087451cc197a7fa5d47e03ffb038", "shasum": "" }, "require": { - "php": "^7.2 || ^8.0" + "php": "^7.2" }, "conflict": { "twig/twig": "<1.40 || >=2,<2.9" @@ -265,20 +257,20 @@ "menu", "tree" ], - "time": "2020-08-15T08:01:46+00:00" + "time": "2020-04-22T14:37:23+00:00" }, { "name": "masterminds/html5", - "version": "2.7.4", + "version": "2.7.0", "source": { "type": "git", "url": "https://github.com/Masterminds/html5-php.git", - "reference": "9227822783c75406cfe400984b2f095cdf03d417" + "reference": "104443ad663d15981225f99532ba73c2f1d6b6f2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Masterminds/html5-php/zipball/9227822783c75406cfe400984b2f095cdf03d417", - "reference": "9227822783c75406cfe400984b2f095cdf03d417", + "url": "https://api.github.com/repos/Masterminds/html5-php/zipball/104443ad663d15981225f99532ba73c2f1d6b6f2", + "reference": "104443ad663d15981225f99532ba73c2f1d6b6f2", "shasum": "" }, "require": { @@ -288,7 +280,9 @@ "php": ">=5.3.0" }, "require-dev": { - "phpunit/phpunit": "^4.8.35" + "phpunit/phpunit": "^4.8.35", + "sami/sami": "~2.0", + "satooshi/php-coveralls": "1.0.*" }, "type": "library", "extra": { @@ -330,22 +324,22 @@ "serializer", "xml" ], - "time": "2020-10-01T13:52:52+00:00" + "time": "2019-07-25T07:03:26+00:00" } ], "packages-dev": [ { "name": "squizlabs/php_codesniffer", - "version": "3.5.8", + "version": "3.5.5", "source": { "type": "git", "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "9d583721a7157ee997f235f327de038e7ea6dac4" + "reference": "73e2e7f57d958e7228fce50dc0c61f58f017f9f6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/9d583721a7157ee997f235f327de038e7ea6dac4", - "reference": "9d583721a7157ee997f235f327de038e7ea6dac4", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/73e2e7f57d958e7228fce50dc0c61f58f017f9f6", + "reference": "73e2e7f57d958e7228fce50dc0c61f58f017f9f6", "shasum": "" }, "require": { @@ -383,7 +377,7 @@ "phpcs", "standards" ], - "time": "2020-10-23T02:01:07+00:00" + "time": "2020-04-17T01:09:41+00:00" } ], "aliases": [], diff --git a/src/Exceptions/LifecycleException.php b/src/Exceptions/LifecycleException.php new file mode 100644 index 0000000..05b28d6 --- /dev/null +++ b/src/Exceptions/LifecycleException.php @@ -0,0 +1,10 @@ +config->get('menu'); + + if ($menu->isEmpty()) { + return; + } + + $GLOBALS['menu'] = collect($GLOBALS['menu'])->map(function ($item) use ($menu) { + if (! $menu->contains(Str::afterLast($item[2], '='))) { + return $item; + } + + if ($menu->get($item[2]) === false) { + return; + } + + array_push( + $GLOBALS['submenu']['tools.php'], + collect($item)->slice(0, 2)->push( + admin_url( + (is_string($menu->get($item[2])) ? $item[2] : Str::contains($item[2], '.php')) + ? $item[2] : Str::start($item[2], 'admin.php?page=') + ) + )->all() + ); + })->filter()->all(); + }, 20); + } +} diff --git a/src/Modules/AnchorModule.php b/src/Modules/AnchorModule.php new file mode 100644 index 0000000..837f789 --- /dev/null +++ b/src/Modules/AnchorModule.php @@ -0,0 +1,60 @@ +config + ->only('post') + ->collapse() + ->each(function ($value, $key) { + if ( + ! $anchors = Arr::get($value, 'anchors') || + ! (Str::is($key, get_post_type()) && is_singular()) + ) { + return; + } + + return add_filter('the_content', function ($content) use ($anchors) { + $anchors = collect($anchors)->filter(function ($value) { + return is_int($value); + }); + + return (new MarkupFixer())->fix( + $content, + ...$anchors->toArray() + ); + }); + }); + }, 20); + } +} diff --git a/src/Modules/BlockCategoryModule.php b/src/Modules/BlockCategoryModule.php new file mode 100644 index 0000000..65a5113 --- /dev/null +++ b/src/Modules/BlockCategoryModule.php @@ -0,0 +1,72 @@ +keyBy('slug'); + + return $this->config->get('categories')->map(function ($value, $key) use ($categories) { + if (empty($key) || is_int($key)) { + $key = $value; + } + + if ($categories->has($key)) { + if ($value === false) { + return $categories->forget($key); + } + + if (is_string($value)) { + $value = ['title' => Str::title($value)]; + } + + return $categories->put( + $key, + array_merge($categories->get($key), $value) + ); + } + + if (! is_array($value)) { + return [ + 'slug' => Str::slug($key), + 'title' => Str::title($value ?? $key), + 'icon' => null, + ]; + } + + return array_merge([ + 'slug' => Str::slug($key), + 'title' => Str::title($key), + 'icon' => null, + ], $value ?? []); + }) + ->merge($categories->all()) + ->filter() + ->sort() + ->values() + ->all(); + }); + } +} diff --git a/src/Modules/BlockModule.php b/src/Modules/BlockModule.php new file mode 100644 index 0000000..fbc8f45 --- /dev/null +++ b/src/Modules/BlockModule.php @@ -0,0 +1,76 @@ +config->get('block')->each(function ($value, $key) { + if (empty($key) || is_int($key)) { + $key = $value; + } + + $value = collect($value); + + if (! Str::contains($key, '/')) { + $key = Str::start($key, $this->namespace()); + } + + return register_block_type($key, [ + 'attributes' => $value->get('attributes', []), + 'render_callback' => function ($data, $content) use ($key, $value) { + return view($value->get('view', 'blocks.' . Str::after($key, '/')), [ + 'data' => (object) $data, + 'content' => $value->get('strip', true) && $this->isEmpty($content) ? false : $content + ]); + }, + ]); + }); + } + + /** + * Use the current theme's text domain as a namespace. + * + * @param string $delimiter + * @return string + */ + protected function namespace($delimiter = '/') + { + return (Str::slug( + wp_get_theme()->get('TextDomain') + ) ?? 'sage') . $delimiter; + } +} diff --git a/src/Modules/EditorPaletteModule.php b/src/Modules/EditorPaletteModule.php new file mode 100644 index 0000000..882f542 --- /dev/null +++ b/src/Modules/EditorPaletteModule.php @@ -0,0 +1,65 @@ +config->get('palette')->pop()) && $palette === true) || + is_string($palette) + ) { + $palette = json_decode( + asset(Str::finish(is_string($palette) ? $palette : 'palette', '.json'))->contents(), + true + ); + + if (empty($palette)) { + return; + } + + return add_theme_support('editor-color-palette', $palette); + } + + $palette = $this->config->get('palette')->map(function ($value, $key) { + if (! is_array($value)) { + return [ + 'name' => Str::title($key), + 'slug' => Str::slug($key), + 'color' => $value, + ]; + } + + return array_merge([ + 'name' => Str::title($key), + 'slug' => Str::slug($key), + ], $value ?? []); + }) + ->values() + ->filter(); + + if ($palette->isEmpty()) { + return; + } + + return add_theme_support('editor-color-palette', $palette->all()); + } +} diff --git a/src/Modules/Module.php b/src/Modules/Module.php new file mode 100644 index 0000000..e21291c --- /dev/null +++ b/src/Modules/Module.php @@ -0,0 +1,39 @@ +key)) { + throw new LifecycleException( + sprintf('Module %s is missing the key property.', get_class($this)) + ); + } + + $this->config = $config->get($this->key); + $this->register(); + } +} diff --git a/src/Modules/PostTypeModule.php b/src/Modules/PostTypeModule.php new file mode 100644 index 0000000..a5ede71 --- /dev/null +++ b/src/Modules/PostTypeModule.php @@ -0,0 +1,89 @@ +config + ->each(function ($value, $key) { + if (empty($key) || is_int($key)) { + return register_extended_post_type(...Arr::wrap($value)); + } + + if (post_type_exists($key)) { + if ($value === false) { + return $this->unregisterPostType($key); + } + + return $this->modify($key, $value); + } + + return register_extended_post_type( + $key, + $value, + Arr::get($value, 'labels', []) + ); + }); + } + + /** + * Modifies an existing post type or taxonomy object. + * + * @param string $name + * @param array $config + * @return void + */ + protected function modifyPostType($name, $config) + { + $object = get_post_type_object($name); + + if (! $object instanceof WP_Post_Type) { + return; + } + + foreach ($config as $key => $value) { + $object->{$key} = $value; + } + } + + /** + * Unregister an existing post type. + * + * @param string $type + * @return void + */ + protected function unregisterPostType($type) + { + $object = get_post_type_object($type); + + if (! $object instanceof WP_Post_Type) { + return; + } + + return unregister_post_type($object); + } +} diff --git a/src/Modules/TaxonomyModule.php b/src/Modules/TaxonomyModule.php new file mode 100644 index 0000000..b1626b8 --- /dev/null +++ b/src/Modules/TaxonomyModule.php @@ -0,0 +1,53 @@ +config->each(function ($value, $key) { + if (empty($key) || is_int($key)) { + return register_extended_taxonomy($value, 'post'); + } + + if ($this->exists($key)) { + if ($value === false) { + return $this->remove($key); + } + + return $this->modify($key, $value); + } + + return register_extended_taxonomy( + $key, + Arr::get($value, 'links', 'post'), + $value, + Arr::get($value, 'labels', []) + ); + }); + } +} diff --git a/src/Poet.php b/src/Poet.php index da38e19..344cb0a 100644 --- a/src/Poet.php +++ b/src/Poet.php @@ -5,12 +5,10 @@ use WP_Post_Type; use WP_Taxonomy; use TOC\MarkupFixer; -use Illuminate\Support\Str; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use function Roots\asset; -use function Roots\view; class Poet { @@ -22,346 +20,33 @@ class Poet protected $config; /** - * Create a new Poet instance. - * - * @param array $config - * @return void - */ - public function __construct($config = []) - { - $this->config = collect($config)->mapInto(Collection::class); - - add_filter('init', function () { - $this->config->has('post') && $this->registerPosts(); - $this->config->has('post') && $this->registerAnchors(); - $this->config->has('taxonomy') && $this->registerTaxonomies(); - $this->config->has('block') && $this->registerBlocks(); - $this->config->has('categories') && $this->registerCategories(); - $this->config->has('palette') && $this->registerPalette(); - $this->config->has('menu') && $this->registerMenu(); - }); - } - - /** - * Register the configured post types using Extended CPTs. - * ↪ https://github.com/johnbillion/extended-cpts - * - * If a post type already exists, the object will be modified instead. - * ↪ https://codex.wordpress.org/Function_Reference/get_post_type_object - * - * If a post type already exists and is set to `false`, the post type - * will be unregistered. - * ↪ https://developer.wordpress.org/reference/functions/unregister_post_type/ - * - * @return void - */ - protected function registerPosts() - { - $this->config - ->only('post') - ->collapse() - ->each(function ($value, $key) { - if (empty($key) || is_int($key)) { - return register_extended_post_type(...Arr::wrap($value)); - } - - if ($this->exists($key) && $value === false) { - return $this->remove($key); - } - - return register_extended_post_type( - $key, - $value, - Arr::get($value, 'labels', []) - ); - }); - } - - /** - * Add anchor ID attributes to post content heading selectors - * when enabled on created or modified post types. - * - * This is done by simply passing `true` to `anchors` when - * registering or modifying post types with Poet. - * - * If a heading already has a valid anchor ID present in the - * form of a slug, it will be skipped. - * - * You may also optionally pass an array to `anchors` setting - * a heading limit range. In this case, passing `4` would only - * add anchor ID's to tags h1–h4. - * ↪ https://github.com/caseyamcl/toc - * - * @return void - */ - protected function registerAnchors() - { - add_filter('the_post', function () { - $this->config - ->only('post') - ->collapse() - ->each(function ($value, $key) { - if ( - ! $anchors = Arr::get($value, 'anchors') || - ! (Str::is($key, get_post_type()) && is_singular()) - ) { - return; - } - - return add_filter('the_content', function ($content) use ($anchors) { - $anchors = collect($anchors)->filter(function ($value) { - return is_int($value); - }); - - return (new MarkupFixer())->fix( - $content, - ...$anchors->toArray() - ); - }); - }); - }, 20); - } - - /** - * Register the configured taxomonies using Extended CPTs. - * ↪ https://github.com/johnbillion/extended-cpts - * - * If a taxonomy already exists, the object will be modified instead. - * ↪ https://developer.wordpress.org/reference/functions/get_taxonomy/ - * - * If a taxonomy already exists and is set to `false`, the taxonomy - * will be unregistered. - * ↪ https://developer.wordpress.org/reference/functions/unregister_taxonomy_for_object_type/ - * https://developer.wordpress.org/reference/functions/unregister_taxonomy/ - * - * @return void - */ - protected function registerTaxonomies() - { - $this->config->get('taxonomy')->each(function ($value, $key) { - if (empty($key) || is_int($key)) { - return register_extended_taxonomy($value, 'post'); - } - - if ($this->exists($key)) { - if ($value === false) { - return $this->remove($key); - } - - return $this->modify($key, $value); - } - - return register_extended_taxonomy( - $key, - Arr::get($value, 'links', 'post'), - $value, - Arr::get($value, 'labels', []) - ); - }); - } - - /** - * Register the configured block types with the editor using Blade - * for rendering the registered block. - * - * If no namespace is provided on the block, Poet will default to - * the current theme text domain. - * - * Optionally, you may pass a block as an array containing: - * ↪ attributes – An array of custom block attributes. - * ↪ strip – When set to false, `$content` will always return true. - * - * Given the Block `sage/accordion`, the Block view would be located at: - * ↪ `views/blocks/accordion.blade.php` - * - * Block views have the following variables available: - * ↪ $data – An object containing the block data. - * ↪ $content – A string containing the InnerBlocks content. - * Returns `false` when empty. - * - * @return void - */ - protected function registerBlocks() - { - return $this->config->get('block')->each(function ($value, $key) { - if (empty($key) || is_int($key)) { - $key = $value; - } - - $value = collect($value); - - if (! Str::contains($key, '/')) { - $key = Str::start($key, $this->namespace()); - } - - return register_block_type($key, [ - 'attributes' => $value->get('attributes', []), - 'render_callback' => function ($data, $content) use ($key, $value) { - return view($value->get('view', 'blocks.' . Str::after($key, '/')), [ - 'data' => (object) $data, - 'content' => $value->get('strip', true) && $this->isEmpty($content) ? false : $content - ]); - }, - ]); - }); - } - - /** - * Register the configured block categories with the editor. - * - * If a category already exists, it will be modified instead. + * The Poet modules. * - * If a category already exists and is set to `false`, the category - * will be unregistered. - * - * @return void + * @var array */ - protected function registerCategories() - { - add_filter('block_categories', function ($categories) { - $categories = collect($categories)->keyBy('slug'); - - return $this->config->get('categories')->map(function ($value, $key) use ($categories) { - if (empty($key) || is_int($key)) { - $key = $value; - } - - if ($categories->has($key)) { - if ($value === false) { - return $categories->forget($key); - } - - if (is_string($value)) { - $value = ['title' => Str::title($value)]; - } - - return $categories->put( - $key, - array_merge($categories->get($key), $value) - ); - } - - if (! is_array($value)) { - return [ - 'slug' => Str::slug($key), - 'title' => Str::title($value ?? $key), - 'icon' => null, - ]; - } - - return array_merge([ - 'slug' => Str::slug($key), - 'title' => Str::title($key), - 'icon' => null, - ], $value ?? []); - }) - ->merge($categories->all()) - ->filter() - ->sort() - ->values() - ->all(); - }); - } + protected $modules = [ + \Log1x\Poet\Modules\AdminMenuModule::class, + \Log1x\Poet\Modules\AnchorModule::class, + \Log1x\Poet\Modules\BlockCategoryModule::class, + \Log1x\Poet\Modules\BlockModule::class, + \Log1x\Poet\Modules\EditorPaletteModule::class, + \Log1x\Poet\Modules\PostTypeModule::class, + \Log1x\Poet\Modules\TaxonomyModule::class, + ]; /** - * Register the configured color palette with the editor. - * - * @link https://developer.wordpress.org/block-editor/developers/themes/theme-support/#block-color-palettes - * @return void - */ - protected function registerPalette() - { - if ( - (is_bool($palette = $this->config->get('palette')->pop()) && $palette === true) || - is_string($palette) - ) { - $palette = json_decode( - asset(Str::finish(is_string($palette) ? $palette : 'palette', '.json'))->contents(), - true - ); - - if (empty($palette)) { - return; - } - - return add_theme_support('editor-color-palette', $palette); - } - - $palette = $this->config->get('palette')->map(function ($value, $key) { - if (! is_array($value)) { - return [ - 'name' => Str::title($key), - 'slug' => Str::slug($key), - 'color' => $value, - ]; - } - - return array_merge([ - 'name' => Str::title($key), - 'slug' => Str::slug($key), - ], $value ?? []); - }) - ->values() - ->filter(); - - if ($palette->isEmpty()) { - return; - } - - return add_theme_support('editor-color-palette', $palette->all()); - } - - /** - * Moves configured admin menu parent items into the Tools.php submenu. - * Items are configured by simply passing the `page` slug of each plugin. - * - * If an item is explicitly set to `false`, the menu item will be - * removed entirely instead. + * Create a new Poet instance. * + * @param array $config * @return void */ - protected function registerMenu() + public function __construct($config = []) { - add_filter('admin_menu', function () { - $menu = $this->config->get('menu'); - - if ($menu->isEmpty()) { - return; - } - - $GLOBALS['menu'] = collect($GLOBALS['menu'])->map(function ($item) use ($menu) { - if (! $menu->contains(Str::afterLast($item[2], '='))) { - return $item; - } - - if ($menu->get($item[2]) === false) { - return; - } - - array_push( - $GLOBALS['submenu']['tools.php'], - collect($item)->slice(0, 2)->push( - admin_url( - (is_string($menu->get($item[2])) ? $item[2] : Str::contains($item[2], '.php')) - ? $item[2] : Str::start($item[2], 'admin.php?page=') - ) - )->all() - ); - })->filter()->all(); + add_filter('init', function () { + // }, 20); } - /** - * Checks if a post type or taxonomy already exists. - * - * @param string $name - * @return bool - */ - protected function exists($name) - { - return post_type_exists($name) || taxonomy_exists($name); - } - /** * Modifies an existing post type or taxonomy object. * @@ -405,41 +90,6 @@ protected function remove($name) return unregister_post_type($object); } - /** - * Checks if the passed object is a valid WP_Post_Type or WP_Taxonomy instance. - * - * @param object $object - * @return bool - */ - protected function verify($object) - { - return $this->isPostType($object) || $this->isTaxonomy($object); - } - - /** - * Use the current theme's text domain as a namespace. - * - * @param string $delimiter - * @return string - */ - protected function namespace($delimiter = '/') - { - return (Str::slug( - wp_get_theme()->get('TextDomain') - ) ?? 'sage') . $delimiter; - } - - /** - * Check if an object is an instance of WP_Post_Type. - * - * @param mixed $object - * @return bool - */ - protected function isPostType($object) - { - return $object instanceof WP_Post_Type; - } - /** * Check if an object is an instance of WP_Taxonomy. * From a15dd15cfe22a2fa32235c6c557be8b0d5c6e203 Mon Sep 17 00:00:00 2001 From: Brandon Date: Sat, 6 Feb 2021 20:47:48 -0600 Subject: [PATCH 2/8] wip(poet): Major refactor. Currently BROKEN --- .circleci/config.yml | 29 ------------ .gitattributes | 1 - .github/workflows/Main.yml | 46 +++++++++++++++++++ CHANGELOG.md | 4 ++ LICENSE.md | 2 +- README.md | 12 ++--- config/poet.php | 4 +- src/Concerns/HasCollection.php | 19 ++++++++ src/Contracts/Module.php | 13 ++++++ .../{Module.php => AbstractModule.php} | 10 ++-- src/Modules/AdminMenuModule.php | 12 ++--- src/Modules/AnchorModule.php | 10 ++-- src/Modules/BlockCategoryModule.php | 6 +-- src/Modules/BlockModule.php | 8 ++-- src/Modules/EditorPaletteModule.php | 13 ++---- src/Modules/PostTypeModule.php | 39 ++++++++-------- src/Modules/TaxonomyModule.php | 8 ++-- src/Poet.php | 45 ++++++++++++------ src/PoetServiceProvider.php | 4 +- 19 files changed, 177 insertions(+), 108 deletions(-) delete mode 100644 .circleci/config.yml create mode 100644 .github/workflows/Main.yml create mode 100644 src/Concerns/HasCollection.php create mode 100644 src/Contracts/Module.php rename src/Modules/{Module.php => AbstractModule.php} (79%) diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 864e1f8..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,29 +0,0 @@ -version: 2.1 - -orbs: - default: - executors: - php-73: - docker: - - image: 'circleci/php:7.3-stretch' - jobs: - build-php: - executor: php-73 - steps: - - run: php -v - - checkout - - restore_cache: - keys: - - composer-v1-{{ checksum "composer.lock" }} - - composer-v1- - - run: composer install -n --prefer-dist --no-scripts --no-suggest - - run: composer lint - - save_cache: - key: composer-v1-{{ checksum "composer.lock" }} - paths: - - vendor - -workflows: - build: - jobs: - - default/build-php diff --git a/.gitattributes b/.gitattributes index 09511d5..aacb20e 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,2 @@ /.gitattributes export-ignore /.github export-ignore -/.circleci export-ignore diff --git a/.github/workflows/Main.yml b/.github/workflows/Main.yml new file mode 100644 index 0000000..0b2c0f0 --- /dev/null +++ b/.github/workflows/Main.yml @@ -0,0 +1,46 @@ +name: Main + +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + php: + name: PHP ${{ matrix.php }} + runs-on: ubuntu-latest + if: "!contains(github.event.head_commit.message, '[ci skip]')" + strategy: + matrix: + php: ['7.3', '7.4', '8.0'] + + steps: + - name: Checkout the project + uses: actions/checkout@v2 + + - name: Setup the PHP ${{ matrix.php }} environment on ${{ runner.os }} + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: xdebug + env: + COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Restore the Composer cache directory + id: composercache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - uses: actions/cache@v2 + with: + path: ${{ steps.composercache.outputs.dir }} + key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: ${{ runner.os }}-${{ matrix.php }}-composer- + + - name: Install Composer dependencies + run: composer install --no-progress --prefer-dist --optimize-autoloader --no-suggest + + - name: Execute the PHP lint script + run: composer run-script lint diff --git a/CHANGELOG.md b/CHANGELOG.md index fe8390d..2efa41f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v1.2.0 (02/06/21) + +- TBH + ## v1.1.4 (09/22/20) - fix(anchors): Fix error when spreading optional anchor config diff --git a/LICENSE.md b/LICENSE.md index 9d66fdb..becaf48 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020 Brandon Nifong +Copyright (c) Brandon Nifong Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 9f00047..5d426a5 100644 --- a/README.md +++ b/README.md @@ -223,7 +223,7 @@ Consider an accordion block that is registered with a `title` and `className` at Poet provides an easy to way register, modify, and unregister Gutenberg block categories. Looking in the config, you will see a commented out example for a Call to Action category: ```php -'categories' => [ +'blockCategories' => [ 'cta' => [ 'title' => 'Call to Action', 'icon' => 'star-filled', @@ -236,7 +236,7 @@ This would result in a block category with a slug of `cta`. Once your block cate In it's simplest form, you can simply pass a string: ```php -'categories' => [ +'blockCategories' => [ 'my-cool-blocks', ], ``` @@ -246,7 +246,7 @@ which would result in a `my-cool-blocks` category automatically converting the s You can also specify the title by passing a value to your slug: ```php -'categories' => [ +'blockCategories' => [ 'my-cool-blocks' => 'Best Blocks, World.', ], ``` @@ -254,7 +254,7 @@ You can also specify the title by passing a value to your slug: Like post types and taxonomies, modifying an existing block category is the same as registering one: ```php -'categories' => [ +'blockCategories' => [ 'layouts' => 'Sections', 'common' => ['icon' => 'star-filled'], ], @@ -263,7 +263,7 @@ Like post types and taxonomies, modifying an existing block category is the same You can unregister an existing block category by simply passing `false`: ```php -'categories' => [ +'blockCategories' => [ 'common' => false, ], ``` @@ -303,4 +303,4 @@ Contributing whether it be through PRs, reporting an issue, or suggesting an ide ## License -Poet is provided under the [MIT License](https://github.com/log1x/poet/blob/master/LICENSE.md). +Poet is provided under the [MIT License](LICENSE.md). diff --git a/config/poet.php b/config/poet.php index b2808cd..d94163d 100644 --- a/config/poet.php +++ b/config/poet.php @@ -79,7 +79,7 @@ | */ - 'categories' => [ + 'blockCategories' => [ // 'cta' => [ // 'title' => 'Call to Action', // 'icon' => 'star-filled', @@ -124,7 +124,7 @@ | */ - 'menu' => [ + 'adminMenu' => [ // 'gutenberg', ], diff --git a/src/Concerns/HasCollection.php b/src/Concerns/HasCollection.php new file mode 100644 index 0000000..45014c4 --- /dev/null +++ b/src/Concerns/HasCollection.php @@ -0,0 +1,19 @@ +config = $config->get($this->key); - $this->register(); } } diff --git a/src/Modules/AdminMenuModule.php b/src/Modules/AdminMenuModule.php index 85fa0f2..961f5b6 100644 --- a/src/Modules/AdminMenuModule.php +++ b/src/Modules/AdminMenuModule.php @@ -4,14 +4,14 @@ use Illuminate\Support\Str; -class AdminMenuModule extends Module +class AdminMenuModule extends AbstractModule { /** * The module key. * - * @param string[] + * @param string */ - protected $key = ['menu', 'adminMenu']; + protected $key = 'adminMenu'; /** * Moves configured admin menu parent items into the Tools.php submenu. @@ -22,7 +22,7 @@ class AdminMenuModule extends Module * * @return void */ - public function register() + public function handle() { add_filter('admin_menu', function () { $menu = $this->config->get('menu'); @@ -31,7 +31,7 @@ public function register() return; } - $GLOBALS['menu'] = collect($GLOBALS['menu'])->map(function ($item) use ($menu) { + $GLOBALS['menu'] = $this->collect($GLOBALS['menu'])->map(function ($item) use ($menu) { if (! $menu->contains(Str::afterLast($item[2], '='))) { return $item; } @@ -42,7 +42,7 @@ public function register() array_push( $GLOBALS['submenu']['tools.php'], - collect($item)->slice(0, 2)->push( + $this->collect($item)->slice(0, 2)->push( admin_url( (is_string($menu->get($item[2])) ? $item[2] : Str::contains($item[2], '.php')) ? $item[2] : Str::start($item[2], 'admin.php?page=') diff --git a/src/Modules/AnchorModule.php b/src/Modules/AnchorModule.php index 837f789..b74f434 100644 --- a/src/Modules/AnchorModule.php +++ b/src/Modules/AnchorModule.php @@ -4,14 +4,14 @@ use Illuminate\Support\Str; -class AdminMenuModule extends Module +class AdminMenuModule extends AbstractModule { /** * The module key. * - * @param string[] + * @param string */ - protected $key = ['menu', 'adminMenu']; + protected $key = 'post'; /** * Add anchor ID attributes to post content heading selectors @@ -30,7 +30,7 @@ class AdminMenuModule extends Module * * @return void */ - protected function register() + public function handle() { add_filter('the_post', function () { $this->config @@ -45,7 +45,7 @@ protected function register() } return add_filter('the_content', function ($content) use ($anchors) { - $anchors = collect($anchors)->filter(function ($value) { + $anchors = $this->collect($anchors)->filter(function ($value) { return is_int($value); }); diff --git a/src/Modules/BlockCategoryModule.php b/src/Modules/BlockCategoryModule.php index 65a5113..f19eb70 100644 --- a/src/Modules/BlockCategoryModule.php +++ b/src/Modules/BlockCategoryModule.php @@ -4,14 +4,14 @@ use Illuminate\Support\Str; -class BlockCategoryModule extends Module +class BlockCategoryModule extends AbstractModule { /** * The module key. * * @param string[] */ - protected $key = ['category', 'block_category']; + protected $key = 'blockCategory'; /** * Register the configured block categories with the editor. @@ -26,7 +26,7 @@ class BlockCategoryModule extends Module protected function registerCategories() { add_filter('block_categories', function ($categories) { - $categories = collect($categories)->keyBy('slug'); + $categories = $this->collect($categories)->keyBy('slug'); return $this->config->get('categories')->map(function ($value, $key) use ($categories) { if (empty($key) || is_int($key)) { diff --git a/src/Modules/BlockModule.php b/src/Modules/BlockModule.php index fbc8f45..114ffaa 100644 --- a/src/Modules/BlockModule.php +++ b/src/Modules/BlockModule.php @@ -6,14 +6,14 @@ use function Roots\view; -class BlockModule extends Module +class BlockModule extends AbstractModule { /** * The module key. * - * @param string[] + * @param string */ - protected $key = ['block', 'blocks']; + protected $key = 'block'; /** * Register the configured block types with the editor using Blade @@ -43,7 +43,7 @@ protected function registerBlocks() $key = $value; } - $value = collect($value); + $value = $this->collect($value); if (! Str::contains($key, '/')) { $key = Str::start($key, $this->namespace()); diff --git a/src/Modules/EditorPaletteModule.php b/src/Modules/EditorPaletteModule.php index 882f542..71b51a2 100644 --- a/src/Modules/EditorPaletteModule.php +++ b/src/Modules/EditorPaletteModule.php @@ -6,14 +6,14 @@ use function Roots\asset; -class EditorPaletteModule extends Module +class EditorPaletteModule extends AbstractModule { /** * The module key. * * @param string[] */ - protected $key = ['palette']; + protected $key = 'palette'; /** * Register the configured color palette with the editor. @@ -21,12 +21,9 @@ class EditorPaletteModule extends Module * @link https://developer.wordpress.org/block-editor/developers/themes/theme-support/#block-color-palettes * @return void */ - protected function register() + public function handle() { - if ( - (is_bool($palette = $this->config->get('palette')->pop()) && $palette === true) || - is_string($palette) - ) { + if ($this->config === true || is_string($this->config)) { $palette = json_decode( asset(Str::finish(is_string($palette) ? $palette : 'palette', '.json'))->contents(), true @@ -39,7 +36,7 @@ protected function register() return add_theme_support('editor-color-palette', $palette); } - $palette = $this->config->get('palette')->map(function ($value, $key) { + $palette = $this->config->map(function ($value, $key) { if (! is_array($value)) { return [ 'name' => Str::title($key), diff --git a/src/Modules/PostTypeModule.php b/src/Modules/PostTypeModule.php index a5ede71..c6e44c5 100644 --- a/src/Modules/PostTypeModule.php +++ b/src/Modules/PostTypeModule.php @@ -4,14 +4,14 @@ use Illuminate\Support\Arr; -class PostTypeModule extends Module +class PostTypeModule extends AbstractModule { /** * The module key. * * @param string[] */ - protected $key = ['post', 'post_type']; + protected $key = 'post'; /** * Register the configured post types using Extended CPTs. @@ -26,28 +26,27 @@ class PostTypeModule extends Module * * @return void */ - protected function register() + public function handle() { - $this->config - ->each(function ($value, $key) { - if (empty($key) || is_int($key)) { - return register_extended_post_type(...Arr::wrap($value)); - } - - if (post_type_exists($key)) { - if ($value === false) { - return $this->unregisterPostType($key); - } + $this->config->each(function ($value, $key) { + if (empty($key) || is_int($key)) { + return register_extended_post_type(...Arr::wrap($value)); + } - return $this->modify($key, $value); + if (post_type_exists($key)) { + if ($value === false) { + return $this->unregisterPostType($key); } - return register_extended_post_type( - $key, - $value, - Arr::get($value, 'labels', []) - ); - }); + return $this->modify($key, $value); + } + + return register_extended_post_type( + $key, + $value, + Arr::get($value, 'labels', []) + ); + }); } /** diff --git a/src/Modules/TaxonomyModule.php b/src/Modules/TaxonomyModule.php index b1626b8..586904a 100644 --- a/src/Modules/TaxonomyModule.php +++ b/src/Modules/TaxonomyModule.php @@ -4,14 +4,14 @@ use Illuminate\Support\Arr; -class TaxonomyModule extends Module +class TaxonomyModule extends AbstractModule { /** * The module key. * - * @param string[] + * @param string */ - protected $key = ['taxonomy']; + protected $key = 'taxonomy'; /** * Register the configured taxomonies using Extended CPTs. @@ -27,7 +27,7 @@ class TaxonomyModule extends Module * * @return void */ - protected function register() + public function handle() { $this->config->each(function ($value, $key) { if (empty($key) || is_int($key)) { diff --git a/src/Poet.php b/src/Poet.php index 344cb0a..23c0e9c 100644 --- a/src/Poet.php +++ b/src/Poet.php @@ -2,16 +2,23 @@ namespace Log1x\Poet; -use WP_Post_Type; -use WP_Taxonomy; -use TOC\MarkupFixer; -use Illuminate\Support\Arr; use Illuminate\Support\Collection; +use Log1x\Poet\Concerns\HasCollection; +use Log1x\Poet\Modules\AbstractModule; +use Log1x\Poet\Modules\AdminMenuModule; +use Log1x\Poet\Modules\AnchorModule; +use Log1x\Poet\Modules\BlockCategoryModule; +use Log1x\Poet\Modules\BlockModule; +use Log1x\Poet\Modules\EditorPaletteModule; +use Log1x\Poet\Modules\PostTypeModule; +use Log1x\Poet\Modules\TaxonomyModule; use function Roots\asset; class Poet { + use HasCollection; + /** * The Poet configuration. * @@ -25,13 +32,13 @@ class Poet * @var array */ protected $modules = [ - \Log1x\Poet\Modules\AdminMenuModule::class, - \Log1x\Poet\Modules\AnchorModule::class, - \Log1x\Poet\Modules\BlockCategoryModule::class, - \Log1x\Poet\Modules\BlockModule::class, - \Log1x\Poet\Modules\EditorPaletteModule::class, - \Log1x\Poet\Modules\PostTypeModule::class, - \Log1x\Poet\Modules\TaxonomyModule::class, + // AdminMenuModule::class, + // AnchorModule::class, + // BlockCategoryModule::class, + // BlockModule::class, + // EditorPaletteModule::class, + PostTypeModule::class, + TaxonomyModule::class, ]; /** @@ -42,8 +49,18 @@ class Poet */ public function __construct($config = []) { + $this->config = $this->collect($config)->map(function ($value) { + return is_array($value) ? $this->collect($value) : $value; + }); + add_filter('init', function () { - // + foreach ($this->modules as $module) { + if ($module instanceof AbstractModule) { + continue; + } + + (new $module($this->config))->handle(); + } }, 20); } @@ -62,7 +79,7 @@ protected function modify($name, $config) return; } - return collect($config)->map(function ($value, $key) use ($object) { + return $this->collect($config)->map(function ($value, $key) use ($object) { $object->{$key} = $value; }); } @@ -82,7 +99,7 @@ protected function remove($name) } if ($this->isTaxonomy($object)) { - return collect($object->object_type)->each(function ($key) use ($object) { + return $this->collect($object->object_type)->each(function ($key) use ($object) { return unregister_taxonomy_for_object_type($object->name, $key) ?? unregister_taxonomy($object); }); } diff --git a/src/PoetServiceProvider.php b/src/PoetServiceProvider.php index 7d4e83c..f914cef 100644 --- a/src/PoetServiceProvider.php +++ b/src/PoetServiceProvider.php @@ -14,7 +14,9 @@ class PoetServiceProvider extends ServiceProvider public function register() { $this->app->singleton('Log1x\Poet', function () { - return new Poet($this->app->config->get('poet')); + return new Poet( + $this->app->config->get('poet') + ); }); } From 0da3a8705c4c5f45eec2db6105e0cf3494285dfb Mon Sep 17 00:00:00 2001 From: Brandon Date: Sat, 6 Feb 2021 21:00:27 -0600 Subject: [PATCH 3/8] =?UTF-8?q?wip(poet):=20Modularize=20Poet=20(WIP=20?= =?UTF-8?q?=E2=80=93=20VERY=20BROKEN)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Contracts/Module.php | 2 +- src/Modules/AbstractModule.php | 11 +++++++++++ src/Modules/AdminMenuModule.php | 12 +++++------- src/Modules/BlockCategoryModule.php | 8 ++++++-- src/Modules/BlockModule.php | 17 +++++++++++++++-- src/Modules/EditorPaletteModule.php | 2 +- src/Modules/PostTypeModule.php | 3 ++- src/Modules/TaxonomyModule.php | 1 + src/Poet.php | 24 ------------------------ 9 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/Contracts/Module.php b/src/Contracts/Module.php index 2148afb..d79a4cd 100644 --- a/src/Contracts/Module.php +++ b/src/Contracts/Module.php @@ -5,7 +5,7 @@ interface Module { /** - * Initialize the Module handler. + * Handle the module. * * @return array */ diff --git a/src/Modules/AbstractModule.php b/src/Modules/AbstractModule.php index b96067c..a796b73 100644 --- a/src/Modules/AbstractModule.php +++ b/src/Modules/AbstractModule.php @@ -2,6 +2,7 @@ namespace Log1x\Poet\Modules; +use Illuminate\Support\Collection; use Log1x\Poet\Concerns\HasCollection; use Log1x\Poet\Contracts\Module; @@ -38,4 +39,14 @@ public function __construct(Collection $config) $this->config = $config->get($this->key); } + + /** + * Handle the module. + * + * @return array + */ + public function handle() + { + // + } } diff --git a/src/Modules/AdminMenuModule.php b/src/Modules/AdminMenuModule.php index 961f5b6..ecf00a8 100644 --- a/src/Modules/AdminMenuModule.php +++ b/src/Modules/AdminMenuModule.php @@ -25,18 +25,16 @@ class AdminMenuModule extends AbstractModule public function handle() { add_filter('admin_menu', function () { - $menu = $this->config->get('menu'); - - if ($menu->isEmpty()) { + if ($this->config->isEmpty()) { return; } - $GLOBALS['menu'] = $this->collect($GLOBALS['menu'])->map(function ($item) use ($menu) { - if (! $menu->contains(Str::afterLast($item[2], '='))) { + $GLOBALS['menu'] = $this->collect($GLOBALS['menu'])->map(function ($item) { + if (! $this->config->contains(Str::afterLast($item[2], '='))) { return $item; } - if ($menu->get($item[2]) === false) { + if ($this->config->get($item[2]) === false) { return; } @@ -44,7 +42,7 @@ public function handle() $GLOBALS['submenu']['tools.php'], $this->collect($item)->slice(0, 2)->push( admin_url( - (is_string($menu->get($item[2])) ? $item[2] : Str::contains($item[2], '.php')) + (is_string($this->config->get($item[2])) ? $item[2] : Str::contains($item[2], '.php')) ? $item[2] : Str::start($item[2], 'admin.php?page=') ) )->all() diff --git a/src/Modules/BlockCategoryModule.php b/src/Modules/BlockCategoryModule.php index f19eb70..fc93c59 100644 --- a/src/Modules/BlockCategoryModule.php +++ b/src/Modules/BlockCategoryModule.php @@ -9,7 +9,7 @@ class BlockCategoryModule extends AbstractModule /** * The module key. * - * @param string[] + * @param string */ protected $key = 'blockCategory'; @@ -25,10 +25,14 @@ class BlockCategoryModule extends AbstractModule */ protected function registerCategories() { + if ($this->config->isEmpty()) { + return; + } + add_filter('block_categories', function ($categories) { $categories = $this->collect($categories)->keyBy('slug'); - return $this->config->get('categories')->map(function ($value, $key) use ($categories) { + return $this->config->map(function ($value, $key) use ($categories) { if (empty($key) || is_int($key)) { $key = $value; } diff --git a/src/Modules/BlockModule.php b/src/Modules/BlockModule.php index 114ffaa..f32833a 100644 --- a/src/Modules/BlockModule.php +++ b/src/Modules/BlockModule.php @@ -36,9 +36,9 @@ class BlockModule extends AbstractModule * * @return void */ - protected function registerBlocks() + protected function handle() { - return $this->config->get('block')->each(function ($value, $key) { + return $this->config->each(function ($value, $key) { if (empty($key) || is_int($key)) { $key = $value; } @@ -73,4 +73,17 @@ protected function namespace($delimiter = '/') wp_get_theme()->get('TextDomain') ) ?? 'sage') . $delimiter; } + + /** + * Check if a string is empty after stripping tags and whitespace. + * + * @param string $value + * @return bool + */ + protected function isEmpty($value) + { + return empty( + wp_strip_all_tags($value, true) + ); + } } diff --git a/src/Modules/EditorPaletteModule.php b/src/Modules/EditorPaletteModule.php index 71b51a2..9855bf6 100644 --- a/src/Modules/EditorPaletteModule.php +++ b/src/Modules/EditorPaletteModule.php @@ -11,7 +11,7 @@ class EditorPaletteModule extends AbstractModule /** * The module key. * - * @param string[] + * @param string */ protected $key = 'palette'; diff --git a/src/Modules/PostTypeModule.php b/src/Modules/PostTypeModule.php index c6e44c5..b9edfa9 100644 --- a/src/Modules/PostTypeModule.php +++ b/src/Modules/PostTypeModule.php @@ -2,6 +2,7 @@ namespace Log1x\Poet\Modules; +use WP_Post_Type; use Illuminate\Support\Arr; class PostTypeModule extends AbstractModule @@ -9,7 +10,7 @@ class PostTypeModule extends AbstractModule /** * The module key. * - * @param string[] + * @param string */ protected $key = 'post'; diff --git a/src/Modules/TaxonomyModule.php b/src/Modules/TaxonomyModule.php index 586904a..768ebf5 100644 --- a/src/Modules/TaxonomyModule.php +++ b/src/Modules/TaxonomyModule.php @@ -2,6 +2,7 @@ namespace Log1x\Poet\Modules; +use WP_Taxonomy; use Illuminate\Support\Arr; class TaxonomyModule extends AbstractModule diff --git a/src/Poet.php b/src/Poet.php index 23c0e9c..5076102 100644 --- a/src/Poet.php +++ b/src/Poet.php @@ -106,28 +106,4 @@ protected function remove($name) return unregister_post_type($object); } - - /** - * Check if an object is an instance of WP_Taxonomy. - * - * @param mixed $object - * @return bool - */ - protected function isTaxonomy($object) - { - return $object instanceof WP_Taxonomy; - } - - /** - * Check if a string is empty after stripping tags and whitespace. - * - * @param string $value - * @return bool - */ - protected function isEmpty($value) - { - return empty( - wp_strip_all_tags($value, true) - ); - } } From 0104872c337c3460680afa5a8041050e792abe63 Mon Sep 17 00:00:00 2001 From: Brandon Date: Sun, 7 Feb 2021 06:54:28 -0600 Subject: [PATCH 4/8] wip --- src/Modules/AbstractModule.php | 30 ++++++++--- src/Modules/AdminMenuModule.php | 6 +-- src/Modules/AnchorModule.php | 60 --------------------- src/Modules/BlockCategoryModule.php | 9 +--- src/Modules/BlockModule.php | 20 +------ src/Modules/EditorPaletteModule.php | 7 ++- src/Modules/PostTypeModule.php | 42 ++++++++++----- src/Modules/TaxonomyModule.php | 81 ++++++++++++++++++++++++----- src/Poet.php | 73 +++++++------------------- src/PoetServiceProvider.php | 4 +- 10 files changed, 148 insertions(+), 184 deletions(-) delete mode 100644 src/Modules/AnchorModule.php diff --git a/src/Modules/AbstractModule.php b/src/Modules/AbstractModule.php index a796b73..84d978b 100644 --- a/src/Modules/AbstractModule.php +++ b/src/Modules/AbstractModule.php @@ -5,11 +5,19 @@ use Illuminate\Support\Collection; use Log1x\Poet\Concerns\HasCollection; use Log1x\Poet\Contracts\Module; +use Roots\Acorn\Application; -class AbstractModule implements Module +abstract class AbstractModule implements Module { use HasCollection; + /** + * The Application instance. + * + * @var \Roots\Acorn\Application + */ + protected $app; + /** * The module key. * @@ -27,9 +35,10 @@ class AbstractModule implements Module /** * Initialize the Module instance. * + * @param \Roots\Acorn\Application $app * @return void */ - public function __construct(Collection $config) + public function __construct(Application $app, Collection $config) { if (empty($this->key)) { throw new LifecycleException( @@ -37,16 +46,25 @@ public function __construct(Collection $config) ); } + $this->app = $app; $this->config = $config->get($this->key); + + $this->boot(); } /** - * Handle the module. + * Boot the module. * - * @return array + * @return void */ - public function handle() + protected function boot() { - // + if (empty($this->config)) { + return; + } + + $method = method_exists($this, 'handle') ? 'handle' : '__invoke'; + + $this->app->call([$this, $method]); } } diff --git a/src/Modules/AdminMenuModule.php b/src/Modules/AdminMenuModule.php index ecf00a8..4110673 100644 --- a/src/Modules/AdminMenuModule.php +++ b/src/Modules/AdminMenuModule.php @@ -14,11 +14,7 @@ class AdminMenuModule extends AbstractModule protected $key = 'adminMenu'; /** - * Moves configured admin menu parent items into the Tools.php submenu. - * Items are configured by simply passing the `page` slug of each plugin. - * - * If an item is explicitly set to `false`, the menu item will be - * removed entirely instead. + * Handle the module. * * @return void */ diff --git a/src/Modules/AnchorModule.php b/src/Modules/AnchorModule.php deleted file mode 100644 index b74f434..0000000 --- a/src/Modules/AnchorModule.php +++ /dev/null @@ -1,60 +0,0 @@ -config - ->only('post') - ->collapse() - ->each(function ($value, $key) { - if ( - ! $anchors = Arr::get($value, 'anchors') || - ! (Str::is($key, get_post_type()) && is_singular()) - ) { - return; - } - - return add_filter('the_content', function ($content) use ($anchors) { - $anchors = $this->collect($anchors)->filter(function ($value) { - return is_int($value); - }); - - return (new MarkupFixer())->fix( - $content, - ...$anchors->toArray() - ); - }); - }); - }, 20); - } -} diff --git a/src/Modules/BlockCategoryModule.php b/src/Modules/BlockCategoryModule.php index fc93c59..784ac0f 100644 --- a/src/Modules/BlockCategoryModule.php +++ b/src/Modules/BlockCategoryModule.php @@ -14,16 +14,11 @@ class BlockCategoryModule extends AbstractModule protected $key = 'blockCategory'; /** - * Register the configured block categories with the editor. - * - * If a category already exists, it will be modified instead. - * - * If a category already exists and is set to `false`, the category - * will be unregistered. + * Handle the module. * * @return void */ - protected function registerCategories() + public function handle() { if ($this->config->isEmpty()) { return; diff --git a/src/Modules/BlockModule.php b/src/Modules/BlockModule.php index f32833a..920cd5b 100644 --- a/src/Modules/BlockModule.php +++ b/src/Modules/BlockModule.php @@ -16,27 +16,11 @@ class BlockModule extends AbstractModule protected $key = 'block'; /** - * Register the configured block types with the editor using Blade - * for rendering the registered block. - * - * If no namespace is provided on the block, Poet will default to - * the current theme text domain. - * - * Optionally, you may pass a block as an array containing: - * ↪ attributes – An array of custom block attributes. - * ↪ strip – When set to false, `$content` will always return true. - * - * Given the Block `sage/accordion`, the Block view would be located at: - * ↪ `views/blocks/accordion.blade.php` - * - * Block views have the following variables available: - * ↪ $data – An object containing the block data. - * ↪ $content – A string containing the InnerBlocks content. - * Returns `false` when empty. + * Handle the module. * * @return void */ - protected function handle() + public function handle() { return $this->config->each(function ($value, $key) { if (empty($key) || is_int($key)) { diff --git a/src/Modules/EditorPaletteModule.php b/src/Modules/EditorPaletteModule.php index 9855bf6..c0b06fb 100644 --- a/src/Modules/EditorPaletteModule.php +++ b/src/Modules/EditorPaletteModule.php @@ -15,17 +15,16 @@ class EditorPaletteModule extends AbstractModule */ protected $key = 'palette'; - /** - * Register the configured color palette with the editor. + /** + * Handle the module. * - * @link https://developer.wordpress.org/block-editor/developers/themes/theme-support/#block-color-palettes * @return void */ public function handle() { if ($this->config === true || is_string($this->config)) { $palette = json_decode( - asset(Str::finish(is_string($palette) ? $palette : 'palette', '.json'))->contents(), + asset(Str::finish(is_string($this->config) ? $this->config : 'palette', '.json'))->contents(), true ); diff --git a/src/Modules/PostTypeModule.php b/src/Modules/PostTypeModule.php index b9edfa9..049e4e3 100644 --- a/src/Modules/PostTypeModule.php +++ b/src/Modules/PostTypeModule.php @@ -15,15 +15,7 @@ class PostTypeModule extends AbstractModule protected $key = 'post'; /** - * Register the configured post types using Extended CPTs. - * ↪ https://github.com/johnbillion/extended-cpts - * - * If a post type already exists, the object will be modified instead. - * ↪ https://codex.wordpress.org/Function_Reference/get_post_type_object - * - * If a post type already exists and is set to `false`, the post type - * will be unregistered. - * ↪ https://developer.wordpress.org/reference/functions/unregister_post_type/ + * Handle the module. * * @return void */ @@ -34,12 +26,12 @@ public function handle() return register_extended_post_type(...Arr::wrap($value)); } - if (post_type_exists($key)) { + if ($this->hasPostType($key)) { if ($value === false) { return $this->unregisterPostType($key); } - return $this->modify($key, $value); + return $this->modifyPostType($key, $value); } return register_extended_post_type( @@ -51,7 +43,29 @@ public function handle() } /** - * Modifies an existing post type or taxonomy object. + * Determine if the object is a post type. + * + * @param mixed $object + * @return bool + */ + protected function isPostType($object) + { + return $object instanceof WP_Post_Type; + } + + /** + * Determine if the post type exists. + * + * @param string $name + * @return bool + */ + protected function hasPostType($name) + { + return post_type_exists($name); + } + + /** + * Modifiy an existing post type. * * @param string $name * @param array $config @@ -61,7 +75,7 @@ protected function modifyPostType($name, $config) { $object = get_post_type_object($name); - if (! $object instanceof WP_Post_Type) { + if (! $this->isPostType($object)) { return; } @@ -80,7 +94,7 @@ protected function unregisterPostType($type) { $object = get_post_type_object($type); - if (! $object instanceof WP_Post_Type) { + if (! $this->isPostType($object)) { return; } diff --git a/src/Modules/TaxonomyModule.php b/src/Modules/TaxonomyModule.php index 768ebf5..d706d3a 100644 --- a/src/Modules/TaxonomyModule.php +++ b/src/Modules/TaxonomyModule.php @@ -15,16 +15,7 @@ class TaxonomyModule extends AbstractModule protected $key = 'taxonomy'; /** - * Register the configured taxomonies using Extended CPTs. - * ↪ https://github.com/johnbillion/extended-cpts - * - * If a taxonomy already exists, the object will be modified instead. - * ↪ https://developer.wordpress.org/reference/functions/get_taxonomy/ - * - * If a taxonomy already exists and is set to `false`, the taxonomy - * will be unregistered. - * ↪ https://developer.wordpress.org/reference/functions/unregister_taxonomy_for_object_type/ - * https://developer.wordpress.org/reference/functions/unregister_taxonomy/ + * Handle the module. * * @return void */ @@ -35,12 +26,12 @@ public function handle() return register_extended_taxonomy($value, 'post'); } - if ($this->exists($key)) { + if ($this->hasTaxonomy($key)) { if ($value === false) { - return $this->remove($key); + return $this->unregisterTaxonomy($key); } - return $this->modify($key, $value); + return $this->modifyTaxonomy($key, $value); } return register_extended_taxonomy( @@ -51,4 +42,68 @@ public function handle() ); }); } + + /** + * Determine if the object is a taxonomy. + * + * @param mixed $object + * @return bool + */ + protected function isTaxonomy($object) + { + return $object instanceof WP_Taxonomy; + } + + /** + * Determine if the taxonomy exists. + * + * @param string $name + * @return bool + */ + protected function hasTaxonomy($name) + { + return post_type_exists($name); + } + + /** + * Modify an existing taxonomy. + * + * @param string $name + * @param array $config + * @return void + */ + protected function modifyTaxonomy($name, $config) + { + $object = get_taxonomy($name); + + if (! $this->isTaxonomy($object)) { + return; + } + + foreach ($config as $key => $value) { + $object->{$key} = $value; + } + } + + /** + * Unregister an existing taxonomy. + * + * @link https://developer.wordpress.org/reference/functions/unregister_taxonomy_for_object_type/ + * @link https://developer.wordpress.org/reference/functions/unregister_taxonomy/ + * + * @param string $type + * @return void + */ + protected function unregisterTaxonomy($type) + { + $object = get_taxonomy($type); + + if (! $this->isTaxonomy($object)) { + return; + } + + foreach ($object->object_type as $key) { + unregister_taxonomy_for_object_type($object->name, $key) ?? unregister_taxonomy($object); + } + } } diff --git a/src/Poet.php b/src/Poet.php index 5076102..c3105d3 100644 --- a/src/Poet.php +++ b/src/Poet.php @@ -6,12 +6,12 @@ use Log1x\Poet\Concerns\HasCollection; use Log1x\Poet\Modules\AbstractModule; use Log1x\Poet\Modules\AdminMenuModule; -use Log1x\Poet\Modules\AnchorModule; use Log1x\Poet\Modules\BlockCategoryModule; use Log1x\Poet\Modules\BlockModule; use Log1x\Poet\Modules\EditorPaletteModule; use Log1x\Poet\Modules\PostTypeModule; use Log1x\Poet\Modules\TaxonomyModule; +use Roots\Acorn\Application; use function Roots\asset; @@ -19,6 +19,13 @@ class Poet { use HasCollection; + /** + * The Application instance. + * + * @var \Roots\Acorn\Application + */ + protected $app; + /** * The Poet configuration. * @@ -32,11 +39,10 @@ class Poet * @var array */ protected $modules = [ - // AdminMenuModule::class, - // AnchorModule::class, - // BlockCategoryModule::class, - // BlockModule::class, - // EditorPaletteModule::class, + AdminMenuModule::class, + BlockCategoryModule::class, + BlockModule::class, + EditorPaletteModule::class, PostTypeModule::class, TaxonomyModule::class, ]; @@ -47,11 +53,13 @@ class Poet * @param array $config * @return void */ - public function __construct($config = []) + public function __construct(Application $app) { - $this->config = $this->collect($config)->map(function ($value) { - return is_array($value) ? $this->collect($value) : $value; - }); + $this->app = $app; + $this->config = $this->collect($this->app->config->get('poet')) + ->map(function ($value) { + return is_array($value) ? $this->collect($value) : $value; + }); add_filter('init', function () { foreach ($this->modules as $module) { @@ -59,51 +67,8 @@ public function __construct($config = []) continue; } - (new $module($this->config))->handle(); + new $module($this->app, $this->config); } }, 20); } - - /** - * Modifies an existing post type or taxonomy object. - * - * @param string $name - * @param array $config - * @return void - */ - protected function modify($name, $config) - { - $object = get_post_type_object($name) ?: get_taxonomy($name); - - if (! $this->verify($object)) { - return; - } - - return $this->collect($config)->map(function ($value, $key) use ($object) { - $object->{$key} = $value; - }); - } - - /** - * Removes an existing post type or taxonomy object. - * - * @param string $name - * @return void - */ - protected function remove($name) - { - $object = get_post_type_object($name) ?: get_taxonomy($name); - - if (! $this->verify($object)) { - return; - } - - if ($this->isTaxonomy($object)) { - return $this->collect($object->object_type)->each(function ($key) use ($object) { - return unregister_taxonomy_for_object_type($object->name, $key) ?? unregister_taxonomy($object); - }); - } - - return unregister_post_type($object); - } } diff --git a/src/PoetServiceProvider.php b/src/PoetServiceProvider.php index f914cef..b07281b 100644 --- a/src/PoetServiceProvider.php +++ b/src/PoetServiceProvider.php @@ -14,9 +14,7 @@ class PoetServiceProvider extends ServiceProvider public function register() { $this->app->singleton('Log1x\Poet', function () { - return new Poet( - $this->app->config->get('poet') - ); + return new Poet($this->app); }); } From d59368b233d85dd339d6fc045962ed7562487218 Mon Sep 17 00:00:00 2001 From: Brandon Date: Thu, 11 Feb 2021 17:41:50 -0600 Subject: [PATCH 5/8] chore(deps): Bump dependencies --- composer.json | 5 +- composer.lock | 304 +++++--------------------------------------------- 2 files changed, 31 insertions(+), 278 deletions(-) diff --git a/composer.json b/composer.json index fd48e29..85da81d 100644 --- a/composer.json +++ b/composer.json @@ -23,9 +23,8 @@ } }, "require": { - "php": ">=7.2.5", - "johnbillion/extended-cpts": "^4.3", - "caseyamcl/toc": "^2.2" + "php": "^7.3|^8.0", + "johnbillion/extended-cpts": "^4.5" }, "require-dev": { "squizlabs/php_codesniffer": "^3.5" diff --git a/composer.lock b/composer.lock index 1eb8a64..6952627 100644 --- a/composer.lock +++ b/composer.lock @@ -4,161 +4,35 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e1e6e0c9b71547eae7848bb08809f384", + "content-hash": "ed53b739ba67d7aacf9dcbe28e97e06c", "packages": [ - { - "name": "caseyamcl/toc", - "version": "v2.2", - "source": { - "type": "git", - "url": "https://github.com/caseyamcl/toc.git", - "reference": "27edd0deb48c73f30f8166d5021c9ab5c080f963" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/caseyamcl/toc/zipball/27edd0deb48c73f30f8166d5021c9ab5c080f963", - "reference": "27edd0deb48c73f30f8166d5021c9ab5c080f963", - "shasum": "" - }, - "require": { - "cocur/slugify": "^1.0|^2.0|^3.0|^4.0", - "ext-dom": "*", - "knplabs/knp-menu": "^3.0", - "masterminds/html5": "^2.1", - "php": "^7.1" - }, - "require-dev": { - "phpunit/phpunit": "^7.5|^8.0", - "squizlabs/php_codesniffer": "^3.5", - "twig/twig": "^2.4|^3.0" - }, - "type": "library", - "autoload": { - "psr-4": { - "TOC\\": [ - "src/", - "tests/" - ] - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Casey McLaughlin", - "email": "caseyamcl@gmail.com", - "homepage": "http://caseymclaughlin.com", - "role": "Developer" - } - ], - "description": "Simple Table-of-Contents Generator for PHP. Generates TOCs based off H1...H6 tags", - "homepage": "http://github.com/caseyamcl/toc", - "keywords": [ - "TOC", - "table of contents" - ], - "time": "2020-04-12T15:23:17+00:00" - }, - { - "name": "cocur/slugify", - "version": "v4.0.0", - "source": { - "type": "git", - "url": "https://github.com/cocur/slugify.git", - "reference": "3f1ffc300f164f23abe8b64ffb3f92d35cec8307" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/cocur/slugify/zipball/3f1ffc300f164f23abe8b64ffb3f92d35cec8307", - "reference": "3f1ffc300f164f23abe8b64ffb3f92d35cec8307", - "shasum": "" - }, - "require": { - "ext-mbstring": "*", - "php": ">=7.0" - }, - "conflict": { - "symfony/config": "<3.4 || >=4,<4.3", - "symfony/dependency-injection": "<3.4 || >=4,<4.3", - "symfony/http-kernel": "<3.4 || >=4,<4.3", - "twig/twig": "<2.12.1" - }, - "require-dev": { - "laravel/framework": "~5.1", - "latte/latte": "~2.2", - "league/container": "^2.2.0", - "mikey179/vfsstream": "~1.6.8", - "mockery/mockery": "^1.3", - "nette/di": "~2.4", - "phpunit/phpunit": "^5.7.27", - "pimple/pimple": "~1.1", - "plumphp/plum": "~0.1", - "symfony/config": "^3.4 || ^4.3 || ^5.0", - "symfony/dependency-injection": "^3.4 || ^4.3 || ^5.0", - "symfony/http-kernel": "^3.4 || ^4.3 || ^5.0", - "twig/twig": "^2.12.1 || ~3.0", - "zendframework/zend-modulemanager": "~2.2", - "zendframework/zend-servicemanager": "~2.2", - "zendframework/zend-view": "~2.2" - }, - "type": "library", - "autoload": { - "psr-4": { - "Cocur\\Slugify\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Florian Eckerstorfer", - "email": "florian@eckerstorfer.co", - "homepage": "https://florian.ec" - }, - { - "name": "Ivo Bathke", - "email": "ivo.bathke@gmail.com" - } - ], - "description": "Converts a string into a slug.", - "keywords": [ - "slug", - "slugify" - ], - "time": "2019-12-14T13:04:14+00:00" - }, { "name": "johnbillion/extended-cpts", - "version": "4.3.3", + "version": "4.5.1", "source": { "type": "git", "url": "https://github.com/johnbillion/extended-cpts.git", - "reference": "4c4e292781c28a7d8666803f63c44e655cddf7ad" + "reference": "b8aef5ef64335d51784e391c9393145f50482fb4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/johnbillion/extended-cpts/zipball/4c4e292781c28a7d8666803f63c44e655cddf7ad", - "reference": "4c4e292781c28a7d8666803f63c44e655cddf7ad", + "url": "https://api.github.com/repos/johnbillion/extended-cpts/zipball/b8aef5ef64335d51784e391c9393145f50482fb4", + "reference": "b8aef5ef64335d51784e391c9393145f50482fb4", "shasum": "" }, "require": { - "php": "^7.0" + "php": "^7 | ^8" }, "require-dev": { - "automattic/phpcs-neutron-standard": "^1.5", - "dealerdirect/phpcodesniffer-composer-installer": "^0.5", + "automattic/phpcs-neutron-standard": "1.6.0", + "dealerdirect/phpcodesniffer-composer-installer": "^0.7", "johnbillion/falsey-assertequals-detector": "^2", - "johnbillion/php-docs-standards": "^1", "phpcompatibility/phpcompatibility-wp": "^2", "phpunit/phpunit": "^6", "roots/wordpress": "*", "vlucas/phpdotenv": "^3", - "wp-cli/wp-cli-bundle": "^2.1", - "wp-coding-standards/wpcs": "~2.1.0", + "wp-cli/db-command": "^2.0", + "wp-coding-standards/wpcs": "2.3.0", "wp-phpunit/wp-phpunit": "*" }, "suggest": { @@ -189,157 +63,32 @@ ], "description": "A library which provides extended functionality to WordPress custom post types and taxonomies.", "homepage": "https://github.com/johnbillion/extended-cpts/", - "time": "2020-04-22T16:13:18+00:00" - }, - { - "name": "knplabs/knp-menu", - "version": "v3.1.1", - "source": { - "type": "git", - "url": "https://github.com/KnpLabs/KnpMenu.git", - "reference": "254fb64d545f087451cc197a7fa5d47e03ffb038" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/KnpLabs/KnpMenu/zipball/254fb64d545f087451cc197a7fa5d47e03ffb038", - "reference": "254fb64d545f087451cc197a7fa5d47e03ffb038", - "shasum": "" + "support": { + "issues": "https://github.com/johnbillion/extended-cpts/issues", + "source": "https://github.com/johnbillion/extended-cpts/tree/4.5.1" }, - "require": { - "php": "^7.2" - }, - "conflict": { - "twig/twig": "<1.40 || >=2,<2.9" - }, - "require-dev": { - "phpspec/prophecy": "^1.8", - "psr/container": "^1.0", - "symfony/http-foundation": "^3.4 || ^4.2|| ^5.0", - "symfony/phpunit-bridge": "^3.3 || ^4.2|| ^5.0", - "symfony/routing": "^3.4 || ^4.2|| ^5.0", - "twig/twig": "^1.40 || ^2.9 || ^3.0" - }, - "suggest": { - "twig/twig": "for the TwigRenderer and the integration with your templates" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.2-dev" - } - }, - "autoload": { - "psr-4": { - "Knp\\Menu\\": "src/Knp/Menu" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "KnpLabs", - "homepage": "https://knplabs.com" - }, - { - "name": "Christophe Coevoet", - "email": "stof@notk.org" - }, + "funding": [ { - "name": "The Community", - "homepage": "https://github.com/KnpLabs/KnpMenu/contributors" + "url": "https://github.com/johnbillion", + "type": "github" } ], - "description": "An object oriented menu library", - "homepage": "https://knplabs.com", - "keywords": [ - "menu", - "tree" - ], - "time": "2020-04-22T14:37:23+00:00" - }, - { - "name": "masterminds/html5", - "version": "2.7.0", - "source": { - "type": "git", - "url": "https://github.com/Masterminds/html5-php.git", - "reference": "104443ad663d15981225f99532ba73c2f1d6b6f2" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/Masterminds/html5-php/zipball/104443ad663d15981225f99532ba73c2f1d6b6f2", - "reference": "104443ad663d15981225f99532ba73c2f1d6b6f2", - "shasum": "" - }, - "require": { - "ext-ctype": "*", - "ext-dom": "*", - "ext-libxml": "*", - "php": ">=5.3.0" - }, - "require-dev": { - "phpunit/phpunit": "^4.8.35", - "sami/sami": "~2.0", - "satooshi/php-coveralls": "1.0.*" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.7-dev" - } - }, - "autoload": { - "psr-4": { - "Masterminds\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Matt Butcher", - "email": "technosophos@gmail.com" - }, - { - "name": "Matt Farina", - "email": "matt@mattfarina.com" - }, - { - "name": "Asmir Mustafic", - "email": "goetas@gmail.com" - } - ], - "description": "An HTML5 parser and serializer.", - "homepage": "http://masterminds.github.io/html5-php", - "keywords": [ - "HTML5", - "dom", - "html", - "parser", - "querypath", - "serializer", - "xml" - ], - "time": "2019-07-25T07:03:26+00:00" + "time": "2021-01-26T10:10:55+00:00" } ], "packages-dev": [ { "name": "squizlabs/php_codesniffer", - "version": "3.5.5", + "version": "3.5.8", "source": { "type": "git", "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "73e2e7f57d958e7228fce50dc0c61f58f017f9f6" + "reference": "9d583721a7157ee997f235f327de038e7ea6dac4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/73e2e7f57d958e7228fce50dc0c61f58f017f9f6", - "reference": "73e2e7f57d958e7228fce50dc0c61f58f017f9f6", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/9d583721a7157ee997f235f327de038e7ea6dac4", + "reference": "9d583721a7157ee997f235f327de038e7ea6dac4", "shasum": "" }, "require": { @@ -377,7 +126,12 @@ "phpcs", "standards" ], - "time": "2020-04-17T01:09:41+00:00" + "support": { + "issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues", + "source": "https://github.com/squizlabs/PHP_CodeSniffer", + "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" + }, + "time": "2020-10-23T02:01:07+00:00" } ], "aliases": [], @@ -386,8 +140,8 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=7.2.5" + "php": "^7.3|^8.0" }, "platform-dev": [], - "plugin-api-version": "1.1.0" + "plugin-api-version": "2.0.0" } From 69b074ad46758d71df492a4b8638fb769abff867 Mon Sep 17 00:00:00 2001 From: Brandon Date: Wed, 17 Feb 2021 04:08:46 -0600 Subject: [PATCH 6/8] enhance(admin-menu): Rewrite the admin menu module to not mess with $GLOBALS fix(modules): Fix improper docblock on Module properties --- src/Modules/AbstractModule.php | 4 +-- src/Modules/AdminMenuModule.php | 42 +++++++++++++++-------------- src/Modules/BlockCategoryModule.php | 2 +- src/Modules/BlockModule.php | 2 +- src/Modules/EditorPaletteModule.php | 2 +- src/Modules/PostTypeModule.php | 2 +- src/Modules/TaxonomyModule.php | 2 +- 7 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/Modules/AbstractModule.php b/src/Modules/AbstractModule.php index 84d978b..7cfda19 100644 --- a/src/Modules/AbstractModule.php +++ b/src/Modules/AbstractModule.php @@ -21,14 +21,14 @@ abstract class AbstractModule implements Module /** * The module key. * - * @param string + * @var string */ protected $key; /** * The module config. * - * @param \Illuminate\Support\Collection + * @var \Illuminate\Support\Collection */ protected $config; diff --git a/src/Modules/AdminMenuModule.php b/src/Modules/AdminMenuModule.php index 4110673..53e4faa 100644 --- a/src/Modules/AdminMenuModule.php +++ b/src/Modules/AdminMenuModule.php @@ -9,7 +9,7 @@ class AdminMenuModule extends AbstractModule /** * The module key. * - * @param string + * @var string */ protected $key = 'adminMenu'; @@ -21,29 +21,31 @@ class AdminMenuModule extends AbstractModule public function handle() { add_filter('admin_menu', function () { + $this->config = $this->config->mapWithKeys(function ($value, $key) { + $page = admin_url("admin.php?page={$value}"); + + return is_int($key) ? [$value => $page] : [$key => $page]; + }); + if ($this->config->isEmpty()) { return; } - $GLOBALS['menu'] = $this->collect($GLOBALS['menu'])->map(function ($item) { - if (! $this->config->contains(Str::afterLast($item[2], '='))) { - return $item; - } - - if ($this->config->get($item[2]) === false) { - return; - } - - array_push( - $GLOBALS['submenu']['tools.php'], - $this->collect($item)->slice(0, 2)->push( - admin_url( - (is_string($this->config->get($item[2])) ? $item[2] : Str::contains($item[2], '.php')) - ? $item[2] : Str::start($item[2], 'admin.php?page=') - ) - )->all() - ); - })->filter()->all(); + $menus = $this->collect($GLOBALS['menu']) + ->flatMap(function ($value) { + $key = Str::afterLast($value[2], '='); + + if (empty($this->config->get($key))) { + return; + } + + return [$value[2] => [$value[0], $value[3], $value[1], $this->config->get($key)]]; + }); + + $menus->each(function ($value, $key) { + remove_menu_page($key); + add_submenu_page('tools.php', ...array_values($value)); + }); }, 20); } } diff --git a/src/Modules/BlockCategoryModule.php b/src/Modules/BlockCategoryModule.php index 784ac0f..2960eb3 100644 --- a/src/Modules/BlockCategoryModule.php +++ b/src/Modules/BlockCategoryModule.php @@ -9,7 +9,7 @@ class BlockCategoryModule extends AbstractModule /** * The module key. * - * @param string + * @var string */ protected $key = 'blockCategory'; diff --git a/src/Modules/BlockModule.php b/src/Modules/BlockModule.php index 920cd5b..bf0e7b4 100644 --- a/src/Modules/BlockModule.php +++ b/src/Modules/BlockModule.php @@ -11,7 +11,7 @@ class BlockModule extends AbstractModule /** * The module key. * - * @param string + * @var string */ protected $key = 'block'; diff --git a/src/Modules/EditorPaletteModule.php b/src/Modules/EditorPaletteModule.php index c0b06fb..d2a86bf 100644 --- a/src/Modules/EditorPaletteModule.php +++ b/src/Modules/EditorPaletteModule.php @@ -11,7 +11,7 @@ class EditorPaletteModule extends AbstractModule /** * The module key. * - * @param string + * @var string */ protected $key = 'palette'; diff --git a/src/Modules/PostTypeModule.php b/src/Modules/PostTypeModule.php index 049e4e3..3409f87 100644 --- a/src/Modules/PostTypeModule.php +++ b/src/Modules/PostTypeModule.php @@ -10,7 +10,7 @@ class PostTypeModule extends AbstractModule /** * The module key. * - * @param string + * @var string */ protected $key = 'post'; diff --git a/src/Modules/TaxonomyModule.php b/src/Modules/TaxonomyModule.php index d706d3a..5a5f165 100644 --- a/src/Modules/TaxonomyModule.php +++ b/src/Modules/TaxonomyModule.php @@ -10,7 +10,7 @@ class TaxonomyModule extends AbstractModule /** * The module key. * - * @param string + * @var string */ protected $key = 'taxonomy'; From 58e25c0e3d9b4533d84f0269371ca16fcc2bc60e Mon Sep 17 00:00:00 2001 From: Brandon Date: Wed, 17 Feb 2021 04:10:29 -0600 Subject: [PATCH 7/8] chore: Bump .gitignore and readme --- .gitignore | 2 +- CHANGELOG.md | 103 --------------------------------------------------- README.md | 8 ---- 3 files changed, 1 insertion(+), 112 deletions(-) delete mode 100644 CHANGELOG.md diff --git a/.gitignore b/.gitignore index 22d0d82..61ead86 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -vendor +/vendor diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 2efa41f..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,103 +0,0 @@ -## v1.2.0 (02/06/21) - -- TBH - -## v1.1.4 (09/22/20) - -- fix(anchors): Fix error when spreading optional anchor config - -## v1.1.3 (07/05/20) - -- fix(palette): Fix editor palette registration (Fixes #9) - -## v1.1.2 (05/22/20) - -- fix(config): Allow for undefined configuration keys. -- fix(poet): Fix deprecated unparenthesized ternaries (PHP 7.4) - -## v1.1.1 (05/15/20) - -- feat(post): Add support for automatically adding heading anchor ID attributes to post types. -- enhance(config): Handle config collections more efficiently -- wip(adminmenu): Add support for cleaning up admin menu items -- chore(package): Set package discovery/publish category to `config` -- chore(docs): Re-prioritize sections -- chore(docs): Add `anchors` mention to docs -- chore(docs): Add admin menu mention to docs - -## V1.1.0 (05/04/20) - -- fix(poet): Add missing view function namespace - -## v1.0.9 (04/30/20) - -- feat(poet): Add editor color palette support -- chore(readme): Add editor color palette examples to README -- chore(readme): General clean up - -## v1.0.8 (04/30/20) - -- feat(poet): Add block category support -- fix(post): Fix post type registration when using multiple string keys. -- fix(taxonomy): Fix taxonomy registration when using multiple string keys. -- enhance(poet): Make the post type, taxonomy, and block registration loop more performant. -- chore(deps): Bump dependencies -- chore(docs): Add block category examples to README - -## v1.0.7 (04/01/20) - -- enhance(posttype): Allow unregistering existing post types by setting it to `false` -- enhance(taxonomy): Allow unregistering existing taxonomies by setting it to `false` -- chore(docs): Update docs with unregister examples - -## v1.0.6 (03/31/20) - -- fix(post): Actually fix post type registration - -## v1.0.5 (03/31/20) - -- fix(post): Fix post type registration - -## v1.0.4 (03/31/20) - -- fix(block): Fix block registration - -## v1.0.3 (03/28/20) - -### Bug fixes - -- fix(block): Fix namespace TextDomain fallback returning empty -- fix(post): Fix registering posts with only a string -- fix(taxonomy): Fix registering taxonomies with only a string - -### Enhancements - -- enhance(poet): Clean up and seperate post type and taxonomy registration methods -- enhance(poet): Use `Arr::get()` for handling array getters with fallback values -- chore(config): Lowercase instances of "block" - -## v1.0.2 (03/25/20) - -### Enhancements - -- feat(block): Use current theme text domain if no namespace is given on registered block. -- enhance(poet): Improve and optimize the Poet class. -- enhance(poet): Optimize and split the `register()` method. -- enhance(poet): Improve and merge post type and taxonomy registration implementations. -- enhance(block): Improve the block registration implementation. -- enhance(block): Move content conditional to standalone `isEmpty()` method. -- chore(poet): Bump minimum PHP to 7.2.5 -- chore(poet): Make method docblocks more verbose. -- chore(config): Improve english in docblock. -- chore(docs): Improve README.md documentation -- chore(license): Bump license to 2020. - -## v1.0.1 (03/24/20) - -### Enhancements - -- feat(blocks): Add support for rendering registered Blocks with Blade. - -## v1.0.0 (11/10/19) - -- Initial release diff --git a/README.md b/README.md index 5d426a5..51a4eb6 100644 --- a/README.md +++ b/README.md @@ -77,14 +77,6 @@ To modify an existing post type, simply treat it as if you are creating a new po ], ``` -Optionally, Poet can also automatically add anchor `ID` attributes to post headings for configured post types by simply setting `anchor` to `true`: - -```php -'post' => [ - 'post' => ['anchors' => true], -], -``` - It is also possible to unregister an existing post type by simply passing `false`: ```php From fb429f45b9d002d8b631b58010639b5723477ba2 Mon Sep 17 00:00:00 2001 From: Brandon Date: Wed, 17 Feb 2021 04:11:49 -0600 Subject: [PATCH 8/8] chore(lint): Fix whitespace --- src/Modules/AdminMenuModule.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Modules/AdminMenuModule.php b/src/Modules/AdminMenuModule.php index 53e4faa..bcdd1bf 100644 --- a/src/Modules/AdminMenuModule.php +++ b/src/Modules/AdminMenuModule.php @@ -42,10 +42,10 @@ public function handle() return [$value[2] => [$value[0], $value[3], $value[1], $this->config->get($key)]]; }); - $menus->each(function ($value, $key) { - remove_menu_page($key); - add_submenu_page('tools.php', ...array_values($value)); - }); + $menus->each(function ($value, $key) { + remove_menu_page($key); + add_submenu_page('tools.php', ...array_values($value)); + }); }, 20); } }