From e56ad29233aeadc392b20cf79576c1ed42079731 Mon Sep 17 00:00:00 2001 From: Tang Rufus Date: Wed, 19 Apr 2017 17:59:47 +0800 Subject: [PATCH 1/5] Fix: OptionStore::getBadUsernames handles single bad username --- src/OptionStore.php | 5 ++-- tests/_support/Helper/Integration.php | 5 ++++ tests/integration/Ads/I18nPromoterTest.php | 4 ++- tests/integration/BadLogin/BadLoginTest.php | 31 ++------------------- tests/integration/Blacklist/HandlerTest.php | 9 ++---- tests/integration/OptionStoreTest.php | 24 ++++++++++------ 6 files changed, 31 insertions(+), 47 deletions(-) diff --git a/src/OptionStore.php b/src/OptionStore.php index 6bf56cc..0cd4b09 100644 --- a/src/OptionStore.php +++ b/src/OptionStore.php @@ -44,10 +44,11 @@ public function getApiKey() * * @return string[] */ - public function getBadUsernames(): array + public function getBadUsernames() { $value = $this->get('wpcfg_bad_login_bad_usernames'); - if (empty($value)) { + + if (! is_string($value)) { return []; } diff --git a/tests/_support/Helper/Integration.php b/tests/_support/Helper/Integration.php index c4bbb85..d762536 100644 --- a/tests/_support/Helper/Integration.php +++ b/tests/_support/Helper/Integration.php @@ -17,5 +17,10 @@ class Integration extends Module public function _after(TestInterface $test) { Test::clean(); + + delete_option('wpcfg_cloudflare_email'); + delete_option('wpcfg_cloudflare_api_key'); + delete_option('wpcfg_cloudflare_zone_id'); + delete_option('wpcfg_bad_login_bad_usernames'); } } diff --git a/tests/integration/Ads/I18nPromoterTest.php b/tests/integration/Ads/I18nPromoterTest.php index d15f1b1..b87fdc0 100644 --- a/tests/integration/Ads/I18nPromoterTest.php +++ b/tests/integration/Ads/I18nPromoterTest.php @@ -65,8 +65,10 @@ public function testYoastI18nWordPressOrgV2Initialized() ]); } - protected function _before() + public function setUp() { + parent::setUp(); + $container = $this->tester->getContainer(); $admin = Test::double( diff --git a/tests/integration/BadLogin/BadLoginTest.php b/tests/integration/BadLogin/BadLoginTest.php index 23fab01..41504a4 100644 --- a/tests/integration/BadLogin/BadLoginTest.php +++ b/tests/integration/BadLogin/BadLoginTest.php @@ -121,18 +121,6 @@ public function testSkipsEmptyUsername() $this->doActionMock->verifyNeverInvoked(); } - /** - * @covers \TypistTech\WPCFG\BadLogin\BadLogin - */ - public function testSkipsForFalseUsername() - { - update_option('wpcfg_bad_login_bad_usernames', false); - - $this->badLogin->emitBlacklistEventIfBadUsername(false); - - $this->doActionMock->verifyNeverInvoked(); - } - /** * @covers \TypistTech\WPCFG\BadLogin\BadLogin */ @@ -145,18 +133,6 @@ public function testSkipsForNotBadUsername() $this->doActionMock->verifyNeverInvoked(); } - /** - * @covers \TypistTech\WPCFG\BadLogin\BadLogin - */ - public function testSkipsForNullUsername() - { - update_option('wpcfg_bad_login_bad_usernames', null); - - $this->badLogin->emitBlacklistEventIfBadUsername(null); - - $this->doActionMock->verifyNeverInvoked(); - } - /** * @covers \TypistTech\WPCFG\BadLogin\BadLogin */ @@ -181,13 +157,10 @@ public function testsHookedIntoWpAuthenticate() $this->assertEquals($expected, $actual); } - protected function _after() + public function setUp() { - delete_option('wpcfg_bad_login_bad_usernames'); - } + parent::setUp(); - protected function _before() - { $container = $this->tester->getContainer(); $this->badLogin = $container->get(BadLogin::class); $this->doActionMock = Test::func(__NAMESPACE__, 'do_action', 'done'); diff --git a/tests/integration/Blacklist/HandlerTest.php b/tests/integration/Blacklist/HandlerTest.php index 7c5c457..57e14d8 100644 --- a/tests/integration/Blacklist/HandlerTest.php +++ b/tests/integration/Blacklist/HandlerTest.php @@ -90,15 +90,10 @@ public function testSkipsForNonBlacklistEvents() $this->accessRules->verifyNeverInvoked('create'); } - protected function _after() + public function setUp() { - delete_option('wpcfg_cloudflare_email'); - delete_option('wpcfg_cloudflare_api_key'); - delete_option('wpcfg_cloudflare_zone_id'); - } + parent::setUp(); - protected function _before() - { update_option('wpcfg_cloudflare_email', 'email@example.com'); update_option('wpcfg_cloudflare_api_key', 'API_KEY_123'); update_option('wpcfg_cloudflare_zone_id', 'abc123'); diff --git a/tests/integration/OptionStoreTest.php b/tests/integration/OptionStoreTest.php index f8b5d25..acc2f6a 100644 --- a/tests/integration/OptionStoreTest.php +++ b/tests/integration/OptionStoreTest.php @@ -46,6 +46,20 @@ public function testGetEmail() $this->assertSame('tester@example.com', $actual); } + /** + * @covers ::getBadUsernames + */ + public function testGetSingleBadUsername() + { + update_option('wpcfg_bad_login_bad_usernames', 'tom '); + + $actual = $this->optionStore->getBadUsernames(); + + $expected = [ 'tom' ]; + + $this->assertSame($expected, $actual); + } + /** * @covers ::getZoneId */ @@ -55,16 +69,10 @@ public function testGetZoneId() $this->assertSame('two46o1', $actual); } - protected function _after() + public function setUp() { - delete_option('wpcfg_cloudflare_email'); - delete_option('wpcfg_cloudflare_api_key'); - delete_option('wpcfg_cloudflare_zone_id'); - delete_option('wpcfg_bad_login_bad_usernames'); - } + parent::setUp(); - protected function _before() - { update_option('wpcfg_cloudflare_email', 'tester@example.com'); update_option('wpcfg_cloudflare_api_key', 'passkey123'); update_option('wpcfg_cloudflare_zone_id', 'two46o1'); From 870ee1bb2f3e1bdc1099d66057f27097a5ee1dd6 Mon Sep 17 00:00:00 2001 From: Tang Rufus Date: Wed, 19 Apr 2017 19:29:39 +0800 Subject: [PATCH 2/5] Fix Integration Test: Rename ::_before --> ::setUp --- tests/integration/Ads/I18nPromoterTest.php | 40 ++++++++++---------- tests/integration/BadLogin/BadLoginTest.php | 18 ++++----- tests/integration/Blacklist/HandlerTest.php | 42 ++++++++++----------- tests/integration/OptionStoreTest.php | 24 ++++++------ 4 files changed, 62 insertions(+), 62 deletions(-) diff --git a/tests/integration/Ads/I18nPromoterTest.php b/tests/integration/Ads/I18nPromoterTest.php index b87fdc0..370137d 100644 --- a/tests/integration/Ads/I18nPromoterTest.php +++ b/tests/integration/Ads/I18nPromoterTest.php @@ -25,6 +25,26 @@ class I18nPromoterTest extends WPTestCase */ private $i18nPromoter; + public function setUp() + { + parent::setUp(); + + $container = $this->tester->getContainer(); + + $admin = Test::double( + $container->get(Admin::class), + [ + 'getMenuSlugs' => [ + 'wpcfg-cloudflare', + 'wpcfg-bad-login', + ], + ] + ); + $container->share(Admin::class, $admin->getObject()); + + $this->i18nPromoter = $container->get(I18nPromoter::class); + } + /** * @covers ::getHooks */ @@ -64,24 +84,4 @@ public function testYoastI18nWordPressOrgV2Initialized() ], ]); } - - public function setUp() - { - parent::setUp(); - - $container = $this->tester->getContainer(); - - $admin = Test::double( - $container->get(Admin::class), - [ - 'getMenuSlugs' => [ - 'wpcfg-cloudflare', - 'wpcfg-bad-login', - ], - ] - ); - $container->share(Admin::class, $admin->getObject()); - - $this->i18nPromoter = $container->get(I18nPromoter::class); - } } diff --git a/tests/integration/BadLogin/BadLoginTest.php b/tests/integration/BadLogin/BadLoginTest.php index 41504a4..47153d1 100644 --- a/tests/integration/BadLogin/BadLoginTest.php +++ b/tests/integration/BadLogin/BadLoginTest.php @@ -29,6 +29,15 @@ class BadLoginTest extends WPTestCase */ private $doActionMock; + public function setUp() + { + parent::setUp(); + + $container = $this->tester->getContainer(); + $this->badLogin = $container->get(BadLogin::class); + $this->doActionMock = Test::func(__NAMESPACE__, 'do_action', 'done'); + } + /** * @covers \TypistTech\WPCFG\BadLogin\BadLogin */ @@ -156,13 +165,4 @@ public function testsHookedIntoWpAuthenticate() $this->assertEquals($expected, $actual); } - - public function setUp() - { - parent::setUp(); - - $container = $this->tester->getContainer(); - $this->badLogin = $container->get(BadLogin::class); - $this->doActionMock = Test::func(__NAMESPACE__, 'do_action', 'done'); - } } diff --git a/tests/integration/Blacklist/HandlerTest.php b/tests/integration/Blacklist/HandlerTest.php index 57e14d8..311f258 100644 --- a/tests/integration/Blacklist/HandlerTest.php +++ b/tests/integration/Blacklist/HandlerTest.php @@ -29,6 +29,27 @@ class HandlerTest extends WPTestCase */ private $handler; + public function setUp() + { + parent::setUp(); + + update_option('wpcfg_cloudflare_email', 'email@example.com'); + update_option('wpcfg_cloudflare_api_key', 'API_KEY_123'); + update_option('wpcfg_cloudflare_zone_id', 'abc123'); + + $container = $this->tester->getContainer(); + + $this->accessRules = Test::double( + $container->get(AccessRules::class), + [ + 'create' => null, + ] + ); + $container->add(AccessRules::class, $this->accessRules->getObject()); + + $this->handler = $container->get(Handler::class); + } + /** * @covers \TypistTech\WPCFG\Blacklist\Handler */ @@ -89,25 +110,4 @@ public function testSkipsForNonBlacklistEvents() $this->accessRules->verifyNeverInvoked('setAuthKey'); $this->accessRules->verifyNeverInvoked('create'); } - - public function setUp() - { - parent::setUp(); - - update_option('wpcfg_cloudflare_email', 'email@example.com'); - update_option('wpcfg_cloudflare_api_key', 'API_KEY_123'); - update_option('wpcfg_cloudflare_zone_id', 'abc123'); - - $container = $this->tester->getContainer(); - - $this->accessRules = Test::double( - $container->get(AccessRules::class), - [ - 'create' => null, - ] - ); - $container->add(AccessRules::class, $this->accessRules->getObject()); - - $this->handler = $container->get(Handler::class); - } } diff --git a/tests/integration/OptionStoreTest.php b/tests/integration/OptionStoreTest.php index acc2f6a..0412471 100644 --- a/tests/integration/OptionStoreTest.php +++ b/tests/integration/OptionStoreTest.php @@ -16,6 +16,18 @@ class OptionStoreTest extends WPTestCase */ private $optionStore; + public function setUp() + { + parent::setUp(); + + update_option('wpcfg_cloudflare_email', 'tester@example.com'); + update_option('wpcfg_cloudflare_api_key', 'passkey123'); + update_option('wpcfg_cloudflare_zone_id', 'two46o1'); + update_option('wpcfg_bad_login_bad_usernames', 'tom, mary,peter'); + + $this->optionStore = new OptionStore; + } + /** * @covers ::getApiKey */ @@ -68,16 +80,4 @@ public function testGetZoneId() $actual = $this->optionStore->getZoneId(); $this->assertSame('two46o1', $actual); } - - public function setUp() - { - parent::setUp(); - - update_option('wpcfg_cloudflare_email', 'tester@example.com'); - update_option('wpcfg_cloudflare_api_key', 'passkey123'); - update_option('wpcfg_cloudflare_zone_id', 'two46o1'); - update_option('wpcfg_bad_login_bad_usernames', 'tom, mary,peter'); - - $this->optionStore = new OptionStore; - } } From aee9c6cac2f8fb55ecaae218dee8e8e7a8374e61 Mon Sep 17 00:00:00 2001 From: Tang Rufus Date: Wed, 19 Apr 2017 19:30:01 +0800 Subject: [PATCH 3/5] Apply code style --- tests/_support/AcceptanceTester.php | 2 +- tests/_support/FunctionalTester.php | 2 +- tests/_support/IntegrationTester.php | 2 +- tests/_support/UnitTester.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/_support/AcceptanceTester.php b/tests/_support/AcceptanceTester.php index df2c4eb..0b6ee00 100644 --- a/tests/_support/AcceptanceTester.php +++ b/tests/_support/AcceptanceTester.php @@ -18,7 +18,7 @@ * @method void am($role) * @method void lookForwardTo($achieveValue) * @method void comment($description) - * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL) + * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = null) * * @SuppressWarnings(PHPMD) */ diff --git a/tests/_support/FunctionalTester.php b/tests/_support/FunctionalTester.php index ff2748e..2176215 100644 --- a/tests/_support/FunctionalTester.php +++ b/tests/_support/FunctionalTester.php @@ -18,7 +18,7 @@ * @method void am($role) * @method void lookForwardTo($achieveValue) * @method void comment($description) - * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL) + * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = null) * * @SuppressWarnings(PHPMD) */ diff --git a/tests/_support/IntegrationTester.php b/tests/_support/IntegrationTester.php index e1c2f1f..86c47fb 100644 --- a/tests/_support/IntegrationTester.php +++ b/tests/_support/IntegrationTester.php @@ -18,7 +18,7 @@ * @method void am($role) * @method void lookForwardTo($achieveValue) * @method void comment($description) - * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL) + * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = null) * * @SuppressWarnings(PHPMD) */ diff --git a/tests/_support/UnitTester.php b/tests/_support/UnitTester.php index a25e692..578e07e 100644 --- a/tests/_support/UnitTester.php +++ b/tests/_support/UnitTester.php @@ -18,7 +18,7 @@ * @method void am($role) * @method void lookForwardTo($achieveValue) * @method void comment($description) - * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL) + * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = null) * * @SuppressWarnings(PHPMD) */ From 15f7df7cc5a80ffadeb3b1489396afcae5cfd8a7 Mon Sep 17 00:00:00 2001 From: Tang Rufus Date: Wed, 19 Apr 2017 19:30:38 +0800 Subject: [PATCH 4/5] Use WP Contained Hook --- composer.json | 1 + composer.lock | 77 ++++++++++- src/AbstractHook.php | 131 ------------------ src/Action.php | 49 ------- src/Admin.php | 5 +- src/Ads/I18nPromoter.php | 4 +- src/BadLogin/Admin.php | 6 +- src/BadLogin/BadLogin.php | 4 +- src/Blacklist/Handler.php | 4 +- src/Cloudflare/Admin.php | 6 +- src/Container.php | 4 +- src/Filter.php | 50 ------- src/I18n.php | 4 +- src/LoadableInterface.php | 4 +- src/Loader.php | 145 -------------------- src/WPCFG.php | 13 +- tests/integration/Ads/I18nPromoterTest.php | 4 +- tests/integration/BadLogin/BadLoginTest.php | 4 +- tests/integration/Blacklist/HandlerTest.php | 4 +- 19 files changed, 115 insertions(+), 404 deletions(-) delete mode 100644 src/AbstractHook.php delete mode 100644 src/Action.php delete mode 100644 src/Filter.php delete mode 100644 src/Loader.php diff --git a/composer.json b/composer.json index 935723f..461ee11 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ "typisttech/cloudflare-wp-api": "^0.3.0", "typisttech/imposter-plugin": "^0.2.5", "typisttech/wp-better-settings": "^0.11.0", + "typisttech/wp-contained-hook": "^0.1.0", "yoast/i18n-module": "^2.0" }, "require-dev": { diff --git a/composer.lock b/composer.lock index aca760b..3e8f2c8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "fb5a420c2519cc0ee98cb5417f323023", + "content-hash": "497c34976046d2033563764b935a9069", "packages": [ { "name": "cloudflare/cf-ip-rewrite", @@ -481,6 +481,81 @@ ], "time": "2017-04-18T10:10:05+00:00" }, + { + "name": "typisttech/wp-contained-hook", + "version": "0.1.0", + "source": { + "type": "git", + "url": "https://github.com/TypistTech/wp-contained-hook.git", + "reference": "86278407d272f588df330d5e73c5f2c7a62a66b4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/TypistTech/wp-contained-hook/zipball/86278407d272f588df330d5e73c5f2c7a62a66b4", + "reference": "86278407d272f588df330d5e73c5f2c7a62a66b4", + "shasum": "" + }, + "require": { + "league/container": "^2.4", + "php": "^7.0" + }, + "require-dev": { + "codeception/aspect-mock": "^2.0", + "codeception/base": "^2.2", + "neronmoon/scriptsdev": "^0.1.1", + "wp-coding-standards/wpcs": "^0.11.0" + }, + "suggest": { + "league/container": "> 2.3.0 Provides psr/container-implementation", + "php-di/php-di": "> 5.4.2 Provides psr/container-implementation" + }, + "type": "library", + "extra": { + "scripts-dev": { + "post-install-cmd": [ + "@install-dev-extra" + ], + "post-update-cmd": [ + "@install-dev-extra" + ] + } + }, + "autoload": { + "psr-4": { + "TypistTech\\WPContainedHook\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Tang Rufus", + "email": "tangrufus@gmail.com", + "homepage": "https://www.typist.tech", + "role": "Developer" + }, + { + "name": "Typist Tech", + "email": "wp-contained-hook@typist.tech", + "homepage": "https://www.typist.tech" + } + ], + "description": "Lazily instantiate objects from dependency injection container to WordPress hooks (actions and filters)", + "homepage": "https://www.typist.tech/projects/wp-contained-hook", + "keywords": [ + "HOOK", + "action", + "container", + "dependency", + "di", + "filter", + "injection", + "wordpress" + ], + "time": "2017-04-06T19:03:41+00:00" + }, { "name": "yoast/i18n-module", "version": "2.0.1", diff --git a/src/AbstractHook.php b/src/AbstractHook.php deleted file mode 100644 index c8201aa..0000000 --- a/src/AbstractHook.php +++ /dev/null @@ -1,131 +0,0 @@ - - * @copyright 2017 Typist Tech - * @license GPL-2.0+ - * - * @see https://www.typist.tech/projects/wp-cloudflare-guard - * @see https://wordpress.org/plugins/wp-cloudflare-guard/ - */ - -declare(strict_types=1); - -namespace TypistTech\WPCFG; - -use Closure; -use TypistTech\WPCFG\Vendor\Psr\Container\ContainerInterface; - -/** - * Abstract class AbstractHook. - * Data transfer object that holds WordPress action information. - */ -abstract class AbstractHook -{ - /** - * The number of arguments that should be passed to the $callback. - * - * @var int - */ - protected $acceptedArgs; - - /** - * The callback method name. - * - * @var string - */ - protected $callbackMethod; - - /** - * Identifier of the entry to look for from container. - * - * @var string - */ - protected $classIdentifier; - - /** - * The name of the WordPress hook that is being registered. - * - * @var string - */ - protected $hook; - - /** - * The priority at which the function should be fired. - * - * @var int - */ - protected $priority; - - /** - * Filter constructor. - * - * @param string $classIdentifier Identifier of the entry to look for from container. - * @param string $hook The name of the WordPress hook that is being registered. - * @param string $callbackMethod The callback method name. - * @param int|null $priority Optional.The priority at which the function should be fired. Default is 10. - * @param int|null $acceptedArgs Optional. The number of arguments that should be passed to the $callback. - * Default is 1. - */ - public function __construct( - string $classIdentifier, - string $hook, - string $callbackMethod, - int $priority = null, - int $acceptedArgs = null - ) { - $this->classIdentifier = $classIdentifier; - $this->hook = $hook; - $this->callbackMethod = $callbackMethod; - $this->priority = $priority ?? 10; - $this->acceptedArgs = $acceptedArgs ?? 1; - } - - /** - * Callback closure getter. - * - * The actual callback that WordPress going to fire. - * - * @param ContainerInterface $container The container. - * - * @return Closure - */ - abstract public function getCallbackClosure(ContainerInterface $container): Closure; - - /** - * AcceptedArgs getter. - * - * @return int - */ - public function getAcceptedArgs(): int - { - return $this->acceptedArgs; - } - - /** - * Hook getter. - * - * @return string - */ - public function getHook(): string - { - return $this->hook; - } - - /** - * Priority getter. - * - * @return int - */ - public function getPriority(): int - { - return $this->priority; - } -} diff --git a/src/Action.php b/src/Action.php deleted file mode 100644 index d849f93..0000000 --- a/src/Action.php +++ /dev/null @@ -1,49 +0,0 @@ - - * @copyright 2017 Typist Tech - * @license GPL-2.0+ - * - * @see https://www.typist.tech/projects/wp-cloudflare-guard - * @see https://wordpress.org/plugins/wp-cloudflare-guard/ - */ - -declare(strict_types=1); - -namespace TypistTech\WPCFG; - -use Closure; -use TypistTech\WPCFG\Vendor\Psr\Container\ContainerInterface; - -/** - * Final class Action - * - * Data transfer object that holds WordPress action information. - */ -final class Action extends AbstractHook -{ - /** - * Callback closure getter. - * - * The actual callback that WordPress going to fire. - * - * @param ContainerInterface $container The container. - * - * @return Closure - */ - public function getCallbackClosure(ContainerInterface $container): Closure - { - return function (...$args) use ($container) { - $instance = $container->get($this->classIdentifier); - $instance->{$this->callbackMethod}(...$args); - }; - } -} diff --git a/src/Admin.php b/src/Admin.php index d763ca5..e904cb6 100644 --- a/src/Admin.php +++ b/src/Admin.php @@ -26,6 +26,7 @@ use TypistTech\WPCFG\Vendor\TypistTech\WPBetterSettings\Pages\SubmenuPage; use TypistTech\WPCFG\Vendor\TypistTech\WPBetterSettings\Section; use TypistTech\WPCFG\Vendor\TypistTech\WPBetterSettings\SettingRegister; +use TypistTech\WPCFG\Vendor\TypistTech\WPContainedHook\Action; /** * Final class Admin. @@ -71,8 +72,8 @@ public function __construct(OptionStore $optionStore) public static function getHooks(): array { return [ - new Action(__CLASS__, 'admin_menu', 'registerPages'), - new Action(__CLASS__, 'admin_init', 'registerSettings'), + new Action('admin_menu', __CLASS__, 'registerPages'), + new Action('admin_init', __CLASS__, 'registerSettings'), ]; } diff --git a/src/Ads/I18nPromoter.php b/src/Ads/I18nPromoter.php index f708f54..4be4e3f 100644 --- a/src/Ads/I18nPromoter.php +++ b/src/Ads/I18nPromoter.php @@ -20,10 +20,10 @@ namespace TypistTech\WPCFG\Ads; -use TypistTech\WPCFG\Action; use TypistTech\WPCFG\Admin; use TypistTech\WPCFG\Container; use TypistTech\WPCFG\LoadableInterface; +use TypistTech\WPCFG\Vendor\TypistTech\WPContainedHook\Action; use TypistTech\WPCFG\Vendor\Yoast_I18n_WordPressOrg_v2; /** @@ -63,7 +63,7 @@ public function __construct(Admin $admin, Container $container) public static function getHooks(): array { return [ - new Action(__CLASS__, 'admin_menu', 'addYoastI18nModuleToMenuPages', 20), + new Action('admin_menu', __CLASS__, 'addYoastI18nModuleToMenuPages'), ]; } diff --git a/src/BadLogin/Admin.php b/src/BadLogin/Admin.php index 226248b..968500d 100644 --- a/src/BadLogin/Admin.php +++ b/src/BadLogin/Admin.php @@ -20,11 +20,11 @@ namespace TypistTech\WPCFG\BadLogin; -use TypistTech\WPCFG\Filter; use TypistTech\WPCFG\LoadableInterface; use TypistTech\WPCFG\Vendor\TypistTech\WPBetterSettings\Fields\Textarea; use TypistTech\WPCFG\Vendor\TypistTech\WPBetterSettings\Pages\SubmenuPage; use TypistTech\WPCFG\Vendor\TypistTech\WPBetterSettings\Section; +use TypistTech\WPCFG\Vendor\TypistTech\WPContainedHook\Filter; /** * Final class Admin. @@ -39,8 +39,8 @@ final class Admin implements LoadableInterface public static function getHooks(): array { return [ - new Filter(__CLASS__, 'wpcfg_pages', 'addPage'), - new Filter(__CLASS__, 'wpcfg_settings_sections', 'addSettingsSection'), + new Filter('wpcfg_pages', __CLASS__, 'addPage'), + new Filter('wpcfg_settings_sections', __CLASS__, 'addSettingsSection'), ]; } diff --git a/src/BadLogin/BadLogin.php b/src/BadLogin/BadLogin.php index 46ad679..4fa00db 100644 --- a/src/BadLogin/BadLogin.php +++ b/src/BadLogin/BadLogin.php @@ -20,11 +20,11 @@ namespace TypistTech\WPCFG\BadLogin; -use TypistTech\WPCFG\Action; use TypistTech\WPCFG\Blacklist\Event; use TypistTech\WPCFG\Container; use TypistTech\WPCFG\LoadableInterface; use TypistTech\WPCFG\OptionStore; +use TypistTech\WPCFG\Vendor\TypistTech\WPContainedHook\Action; /** * Final class BadLogin. @@ -65,7 +65,7 @@ public function __construct(OptionStore $optionStore, Container $container) public static function getHooks(): array { return [ - new Action(__CLASS__, 'wp_authenticate', 'emitBlacklistEventIfBadUsername'), + new Action('wp_authenticate', __CLASS__, 'emitBlacklistEventIfBadUsername'), ]; } diff --git a/src/Blacklist/Handler.php b/src/Blacklist/Handler.php index 340377a..3943b72 100644 --- a/src/Blacklist/Handler.php +++ b/src/Blacklist/Handler.php @@ -20,10 +20,10 @@ namespace TypistTech\WPCFG\Blacklist; -use TypistTech\WPCFG\Action; use TypistTech\WPCFG\Cloudflare\AccessRules; use TypistTech\WPCFG\LoadableInterface; use TypistTech\WPCFG\OptionStore; +use TypistTech\WPCFG\Vendor\TypistTech\WPContainedHook\Action; /** * Final class Handler. @@ -64,7 +64,7 @@ public function __construct(OptionStore $optionStore, AccessRules $accessRules) public static function getHooks(): array { return [ - new Action(__CLASS__, 'wpcfg_blacklist', 'handleBlacklist'), + new Action('wpcfg_blacklist', __CLASS__, 'handleBlacklist'), ]; } diff --git a/src/Cloudflare/Admin.php b/src/Cloudflare/Admin.php index c1b6684..c14c34d 100644 --- a/src/Cloudflare/Admin.php +++ b/src/Cloudflare/Admin.php @@ -20,12 +20,12 @@ namespace TypistTech\WPCFG\Cloudflare; -use TypistTech\WPCFG\Filter; use TypistTech\WPCFG\LoadableInterface; use TypistTech\WPCFG\Vendor\TypistTech\WPBetterSettings\Fields\Email; use TypistTech\WPCFG\Vendor\TypistTech\WPBetterSettings\Fields\Text; use TypistTech\WPCFG\Vendor\TypistTech\WPBetterSettings\Pages\MenuPage; use TypistTech\WPCFG\Vendor\TypistTech\WPBetterSettings\Section; +use TypistTech\WPCFG\Vendor\TypistTech\WPContainedHook\Filter; /** * Final class Admin. @@ -40,8 +40,8 @@ final class Admin implements LoadableInterface public static function getHooks(): array { return [ - new Filter(__CLASS__, 'wpcfg_pages', 'addPage'), - new Filter(__CLASS__, 'wpcfg_settings_sections', 'addSettingsSection'), + new Filter('wpcfg_pages', __CLASS__, 'addPage'), + new Filter('wpcfg_settings_sections', __CLASS__, 'addSettingsSection'), ]; } diff --git a/src/Container.php b/src/Container.php index 624dcd3..59d8faf 100644 --- a/src/Container.php +++ b/src/Container.php @@ -33,9 +33,9 @@ use TypistTech\WPCFG\Vendor\Yoast_I18n_WordPressOrg_v2; /** - * Class Container. + * Final class Container. */ -class Container extends LeagueContainer +final class Container extends LeagueContainer { /** * Initialize container. diff --git a/src/Filter.php b/src/Filter.php deleted file mode 100644 index cd4f44f..0000000 --- a/src/Filter.php +++ /dev/null @@ -1,50 +0,0 @@ - - * @copyright 2017 Typist Tech - * @license GPL-2.0+ - * - * @see https://www.typist.tech/projects/wp-cloudflare-guard - * @see https://wordpress.org/plugins/wp-cloudflare-guard/ - */ - -declare(strict_types=1); - -namespace TypistTech\WPCFG; - -use Closure; -use TypistTech\WPCFG\Vendor\Psr\Container\ContainerInterface; - -/** - * Final class Filter - * - * Data transfer object that holds WordPress filter information. - */ -final class Filter extends AbstractHook -{ - /** - * Callback closure getter. - * - * The actual callback that WordPress going to fire. - * - * @param ContainerInterface $container The container. - * - * @return Closure - */ - public function getCallbackClosure(ContainerInterface $container): Closure - { - return function (...$args) use ($container) { - $instance = $container->get($this->classIdentifier); - - return $instance->{$this->callbackMethod}(...$args); - }; - } -} diff --git a/src/I18n.php b/src/I18n.php index 99f97db..d3c0715 100644 --- a/src/I18n.php +++ b/src/I18n.php @@ -20,6 +20,8 @@ namespace TypistTech\WPCFG; +use TypistTech\WPCFG\Vendor\TypistTech\WPContainedHook\Action; + /** * Define the internationalization functionality. * @@ -34,7 +36,7 @@ final class I18n implements LoadableInterface public static function getHooks(): array { return [ - new Action(__CLASS__, 'plugins_loaded', 'loadPluginTextdomain'), + new Action('plugins_loaded', __CLASS__, 'loadPluginTextdomain'), ]; } diff --git a/src/LoadableInterface.php b/src/LoadableInterface.php index 982a028..d50f2d3 100644 --- a/src/LoadableInterface.php +++ b/src/LoadableInterface.php @@ -21,14 +21,14 @@ namespace TypistTech\WPCFG; /** - * Interface AbstractLoadable. + * Interface LoadableInterface */ interface LoadableInterface { /** * Hooks getter. * - * @return AbstractHook[] + * @return \TypistTech\WPCFG\Vendor\TypistTech\WPContainedHook\AbstractHook[] */ public static function getHooks(): array; } diff --git a/src/Loader.php b/src/Loader.php deleted file mode 100644 index e1a7a8a..0000000 --- a/src/Loader.php +++ /dev/null @@ -1,145 +0,0 @@ - - * @copyright 2017 Typist Tech - * @license GPL-2.0+ - * - * @see https://www.typist.tech/projects/wp-cloudflare-guard - * @see https://wordpress.org/plugins/wp-cloudflare-guard/ - */ - -declare(strict_types=1); - -namespace TypistTech\WPCFG; - -use InvalidArgumentException; -use TypistTech\WPCFG\Vendor\Psr\Container\ContainerInterface; - -/** - * Register all actions and filters for the plugin. - * - * Maintain a list of all hooks that are registered throughout - * the plugin, and register them with the WordPress API. Call the - * run function to execute the list of actions and filters. - */ -class Loader -{ - /** - * The array of actions registered with WordPress. - * - * @var Action[] The actions registered with WordPress to fire when the plugin loads. - */ - private $actions; - - /** - * The WPCFG container. - * - * @var ContainerInterface - */ - private $container; - - /** - * The array of filters registered with WordPress. - * - * @var Filter[] The filters registered with WordPress to fire when the plugin loads. - */ - private $filters; - - /** - * Initialize the collections used to maintain the actions and filters. - * - * @param ContainerInterface $container The plugin container. - */ - public function __construct(ContainerInterface $container) - { - $this->container = $container; - $this->actions = []; - $this->filters = []; - } - - /** - * Add hooks from LoadableInterface[]. - * - * @todo Check for LoadableInterface. - * - * @param string[]|array ...$classes Array of Loadable classes. - * - * @return void - */ - public function load(string ...$classes) - { - foreach ($classes as $loadable) { - $hooks = $loadable::getHooks(); - $this->add(...$hooks); - } - } - - /** - * Add new hook to the collection to be registered with WordPress. - * - * @todo Refactor. - * - * @param AbstractHook[]|array ...$hooks Hooks to be registered. - * Expecting Filters or Actions. - * - * @throws InvalidArgumentException If $hooks are not made of Filters or Actions. - * - * @return void - */ - private function add(AbstractHook ...$hooks) - { - foreach ($hooks as $hook) { - if ($hook instanceof Filter) { - $this->filters[] = $hook; - continue; - } - - if ($hook instanceof Action) { - $this->actions[] = $hook; - continue; - } - - $errorMessage = sprintf( - 'Hook must be one of %1$s or %2$s. However, %3$s is given', - Filter::class, - Action::class, - get_class($hook) - ); - throw new InvalidArgumentException($errorMessage); - } - } - - /** - * Register the filters and actions with WordPress. - * - * @return void - */ - public function run() - { - foreach ($this->filters as $filter) { - add_filter( - $filter->getHook(), - $filter->getCallbackClosure($this->container), - $filter->getPriority(), - $filter->getAcceptedArgs() - ); - } - - foreach ($this->actions as $action) { - add_action( - $action->getHook(), - $action->getCallbackClosure($this->container), - $action->getPriority(), - $action->getAcceptedArgs() - ); - } - } -} diff --git a/src/WPCFG.php b/src/WPCFG.php index 212eb21..c5a5fa3 100644 --- a/src/WPCFG.php +++ b/src/WPCFG.php @@ -25,6 +25,8 @@ use TypistTech\WPCFG\BadLogin\BadLogin; use TypistTech\WPCFG\Blacklist\Handler; use TypistTech\WPCFG\Cloudflare\Admin as CloudflareAdmin; +use TypistTech\WPCFG\Vendor\TypistTech\WPContainedHook\Action; +use TypistTech\WPCFG\Vendor\TypistTech\WPContainedHook\Loader; /** * Final class WPCFG @@ -59,7 +61,7 @@ public function __construct() $this->container->initialize(); $this->container->add('\\' . self::class, $this); - $this->loader->load( + $loadables = [ __CLASS__, Admin::class, BadLogin::class, @@ -67,8 +69,13 @@ public function __construct() Handler::class, CloudflareAdmin::class, I18n::class, - I18nPromoter::class - ); + I18nPromoter::class, + ]; + + foreach ($loadables as $loadable) { + /* @var LoadableInterface $loadable */ + $this->loader->add(...$loadable::getHooks()); + } } /** diff --git a/tests/integration/Ads/I18nPromoterTest.php b/tests/integration/Ads/I18nPromoterTest.php index 370137d..c80f302 100644 --- a/tests/integration/Ads/I18nPromoterTest.php +++ b/tests/integration/Ads/I18nPromoterTest.php @@ -6,8 +6,8 @@ use AspectMock\Test; use Codeception\TestCase\WPTestCase; -use TypistTech\WPCFG\Action; use TypistTech\WPCFG\Admin; +use TypistTech\WPCFG\Vendor\TypistTech\WPContainedHook\Action; use TypistTech\WPCFG\Vendor\Yoast_I18n_WordPressOrg_v2; /** @@ -53,7 +53,7 @@ public function testHookedIntoAdminMenu() $actual = I18nPromoter::getHooks(); $expected = [ - new Action(I18nPromoter::class, 'admin_menu', 'addYoastI18nModuleToMenuPages', 20), + new Action('admin_menu', I18nPromoter::class, 'addYoastI18nModuleToMenuPages'), ]; $this->assertEquals($expected, $actual); diff --git a/tests/integration/BadLogin/BadLoginTest.php b/tests/integration/BadLogin/BadLoginTest.php index 47153d1..5fd8b08 100644 --- a/tests/integration/BadLogin/BadLoginTest.php +++ b/tests/integration/BadLogin/BadLoginTest.php @@ -6,8 +6,8 @@ use AspectMock\Test; use Codeception\TestCase\WPTestCase; -use TypistTech\WPCFG\Action; use TypistTech\WPCFG\Blacklist\Event; +use TypistTech\WPCFG\Vendor\TypistTech\WPContainedHook\Action; /** * @coversDefaultClass \TypistTech\WPCFG\BadLogin\BadLogin @@ -160,7 +160,7 @@ public function testsHookedIntoWpAuthenticate() $actual = BadLogin::getHooks(); $expected = [ - new Action(BadLogin::class, 'wp_authenticate', 'emitBlacklistEventIfBadUsername'), + new Action('wp_authenticate', BadLogin::class, 'emitBlacklistEventIfBadUsername'), ]; $this->assertEquals($expected, $actual); diff --git a/tests/integration/Blacklist/HandlerTest.php b/tests/integration/Blacklist/HandlerTest.php index 311f258..a82a09e 100644 --- a/tests/integration/Blacklist/HandlerTest.php +++ b/tests/integration/Blacklist/HandlerTest.php @@ -6,8 +6,8 @@ use AspectMock\Test; use Codeception\TestCase\WPTestCase; -use TypistTech\WPCFG\Action; use TypistTech\WPCFG\Cloudflare\AccessRules; +use TypistTech\WPCFG\Vendor\TypistTech\WPContainedHook\Action; /** * @coversDefaultClass TypistTech\WPCFG\Blacklist\Handler @@ -88,7 +88,7 @@ public function testHookedIntoWpcfgBlacklist() $actual = Handler::getHooks(); $expected = [ - new Action(Handler::class, 'wpcfg_blacklist', 'handleBlacklist'), + new Action('wpcfg_blacklist', Handler::class, 'handleBlacklist'), ]; $this->assertEquals($expected, $actual); From 53d0b661aac938e2f92902500824a1b41b644e9f Mon Sep 17 00:00:00 2001 From: Tang Rufus Date: Wed, 19 Apr 2017 20:15:51 +0800 Subject: [PATCH 5/5] OptionStore: Add return type hint --- src/OptionStore.php | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/OptionStore.php b/src/OptionStore.php index 0cd4b09..b2c1cad 100644 --- a/src/OptionStore.php +++ b/src/OptionStore.php @@ -32,11 +32,13 @@ final class OptionStore extends WPBSOptionStore /** * Cloudflare api key getter. * - * @return mixed + * @return string */ - public function getApiKey() + public function getApiKey(): string { - return $this->get('wpcfg_cloudflare_api_key'); + $value = $this->get('wpcfg_cloudflare_api_key'); + + return is_string($value) ? $value : ''; } /** @@ -44,7 +46,7 @@ public function getApiKey() * * @return string[] */ - public function getBadUsernames() + public function getBadUsernames(): array { $value = $this->get('wpcfg_bad_login_bad_usernames'); @@ -60,20 +62,24 @@ public function getBadUsernames() /** * Cloudflare email getter. * - * @return mixed + * @return string */ - public function getEmail() + public function getEmail(): string { - return $this->get('wpcfg_cloudflare_email'); + $value = $this->get('wpcfg_cloudflare_email'); + + return is_string($value) ? $value : ''; } /** * Cloudflare zone id getter. * - * @return mixed + * @return string */ - public function getZoneId() + public function getZoneId(): string { - return $this->get('wpcfg_cloudflare_zone_id'); + $value = $this->get('wpcfg_cloudflare_zone_id'); + + return is_string($value) ? $value : ''; } }