From 533362dd6f161970113e4d32ace8ff37c2818f81 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Fri, 18 Nov 2016 21:56:07 +0000 Subject: [PATCH] Allow registering hooks before wordpress has been loaded --- composer.lock | 170 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------- src/Providers/WordpressServiceProvider.php | 40 ++++++++++++++++++++++++++++------------ src/Support/Action.php | 14 ++++++++++++++ 3 files changed, 127 insertions(+), 97 deletions(-) diff --git a/composer.lock b/composer.lock index aff464b..7dc55fb 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" ], - "hash": "e07950081a6df76470a24f8aa9be508e", + "hash": "6e9befbdea08fef4c1039d20d92e69d0", "content-hash": "68f5b8ac3064bd34ac440dec5170d899", "packages": [ { @@ -76,16 +76,16 @@ }, { "name": "illuminate/container", - "version": "v5.3.4", + "version": "v5.3.23", "source": { "type": "git", "url": "https://github.com/illuminate/container.git", - "reference": "360f4900dbaa7e76ecfbb58e0ad4b244a90edfe3" + "reference": "8047b47e1f731c975d9aa0fe0b269064d3f1346d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/container/zipball/360f4900dbaa7e76ecfbb58e0ad4b244a90edfe3", - "reference": "360f4900dbaa7e76ecfbb58e0ad4b244a90edfe3", + "url": "https://api.github.com/repos/illuminate/container/zipball/8047b47e1f731c975d9aa0fe0b269064d3f1346d", + "reference": "8047b47e1f731c975d9aa0fe0b269064d3f1346d", "shasum": "" }, "require": { @@ -115,20 +115,20 @@ ], "description": "The Illuminate Container package.", "homepage": "https://laravel.com", - "time": "2016-08-05 14:48:10" + "time": "2016-10-02 01:14:30" }, { "name": "illuminate/contracts", - "version": "v5.3.4", + "version": "v5.3.23", "source": { "type": "git", "url": "https://github.com/illuminate/contracts.git", - "reference": "f766fc0452d82d545b866a8ad5860b20ccd50763" + "reference": "ce5d73c6015b2054d32f3f8530767847b358ae4e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/contracts/zipball/f766fc0452d82d545b866a8ad5860b20ccd50763", - "reference": "f766fc0452d82d545b866a8ad5860b20ccd50763", + "url": "https://api.github.com/repos/illuminate/contracts/zipball/ce5d73c6015b2054d32f3f8530767847b358ae4e", + "reference": "ce5d73c6015b2054d32f3f8530767847b358ae4e", "shasum": "" }, "require": { @@ -157,20 +157,20 @@ ], "description": "The Illuminate Contracts package.", "homepage": "https://laravel.com", - "time": "2016-08-21 12:37:00" + "time": "2016-09-26 20:36:27" }, { "name": "illuminate/database", - "version": "v5.3.4", + "version": "v5.3.23", "source": { "type": "git", "url": "https://github.com/illuminate/database.git", - "reference": "048a65ba4a7ed250e16c82b6bb323daf38410050" + "reference": "8db1197b3d3e8a7393153643774d2924cdc6d906" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/database/zipball/048a65ba4a7ed250e16c82b6bb323daf38410050", - "reference": "048a65ba4a7ed250e16c82b6bb323daf38410050", + "url": "https://api.github.com/repos/illuminate/database/zipball/8db1197b3d3e8a7393153643774d2924cdc6d906", + "reference": "8db1197b3d3e8a7393153643774d2924cdc6d906", "shasum": "" }, "require": { @@ -217,20 +217,20 @@ "orm", "sql" ], - "time": "2016-08-25 15:46:46" + "time": "2016-11-14 15:37:58" }, { "name": "illuminate/http", - "version": "v5.3.4", + "version": "v5.3.23", "source": { "type": "git", "url": "https://github.com/illuminate/http.git", - "reference": "333263fe8aa6db11187a256ea55c2e9e97f28c4b" + "reference": "e51d9585898bdb51e9674ce9667f6930f9882c9b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/http/zipball/333263fe8aa6db11187a256ea55c2e9e97f28c4b", - "reference": "333263fe8aa6db11187a256ea55c2e9e97f28c4b", + "url": "https://api.github.com/repos/illuminate/http/zipball/e51d9585898bdb51e9674ce9667f6930f9882c9b", + "reference": "e51d9585898bdb51e9674ce9667f6930f9882c9b", "shasum": "" }, "require": { @@ -263,11 +263,11 @@ ], "description": "The Illuminate Http package.", "homepage": "https://laravel.com", - "time": "2016-08-22 10:25:11" + "time": "2016-09-22 13:54:12" }, { "name": "illuminate/pipeline", - "version": "v5.3.4", + "version": "v5.3.23", "source": { "type": "git", "url": "https://github.com/illuminate/pipeline.git", @@ -311,16 +311,16 @@ }, { "name": "illuminate/routing", - "version": "v5.3.4", + "version": "v5.3.23", "source": { "type": "git", "url": "https://github.com/illuminate/routing.git", - "reference": "3dafc280a93178c8290828218b450ec4906235b4" + "reference": "b2abf8f73d6a0f27f117b90ea038e3b273c3b65e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/routing/zipball/3dafc280a93178c8290828218b450ec4906235b4", - "reference": "3dafc280a93178c8290828218b450ec4906235b4", + "url": "https://api.github.com/repos/illuminate/routing/zipball/b2abf8f73d6a0f27f117b90ea038e3b273c3b65e", + "reference": "b2abf8f73d6a0f27f117b90ea038e3b273c3b65e", "shasum": "" }, "require": { @@ -363,20 +363,20 @@ ], "description": "The Illuminate Routing package.", "homepage": "https://laravel.com", - "time": "2016-08-26 21:12:19" + "time": "2016-11-03 13:09:27" }, { "name": "illuminate/session", - "version": "v5.3.4", + "version": "v5.3.23", "source": { "type": "git", "url": "https://github.com/illuminate/session.git", - "reference": "0f818548aa248f64d89f8129229a4de84184f59e" + "reference": "c531bd1e485adc927653018152eff7d37d57f8e0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/session/zipball/0f818548aa248f64d89f8129229a4de84184f59e", - "reference": "0f818548aa248f64d89f8129229a4de84184f59e", + "url": "https://api.github.com/repos/illuminate/session/zipball/c531bd1e485adc927653018152eff7d37d57f8e0", + "reference": "c531bd1e485adc927653018152eff7d37d57f8e0", "shasum": "" }, "require": { @@ -413,20 +413,20 @@ ], "description": "The Illuminate Session package.", "homepage": "https://laravel.com", - "time": "2016-08-11 22:24:39" + "time": "2016-10-24 14:59:49" }, { "name": "illuminate/support", - "version": "v5.3.4", + "version": "v5.3.23", "source": { "type": "git", "url": "https://github.com/illuminate/support.git", - "reference": "1b4f32dfa799dd8d0e0d11e0aaaf677e2829d6e8" + "reference": "050d0ed3e1c0e1d129d73b2eaa14044e46a66f77" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/support/zipball/1b4f32dfa799dd8d0e0d11e0aaaf677e2829d6e8", - "reference": "1b4f32dfa799dd8d0e0d11e0aaaf677e2829d6e8", + "url": "https://api.github.com/repos/illuminate/support/zipball/050d0ed3e1c0e1d129d73b2eaa14044e46a66f77", + "reference": "050d0ed3e1c0e1d129d73b2eaa14044e46a66f77", "shasum": "" }, "require": { @@ -470,7 +470,7 @@ ], "description": "The Illuminate Support package.", "homepage": "https://laravel.com", - "time": "2016-08-26 17:26:49" + "time": "2016-11-03 15:25:28" }, { "name": "nesbot/carbon", @@ -521,16 +521,16 @@ }, { "name": "paragonie/random_compat", - "version": "v2.0.2", + "version": "v2.0.4", "source": { "type": "git", "url": "https://github.com/paragonie/random_compat.git", - "reference": "088c04e2f261c33bed6ca5245491cfca69195ccf" + "reference": "a9b97968bcde1c4de2a5ec6cbd06a0f6c919b46e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/paragonie/random_compat/zipball/088c04e2f261c33bed6ca5245491cfca69195ccf", - "reference": "088c04e2f261c33bed6ca5245491cfca69195ccf", + "url": "https://api.github.com/repos/paragonie/random_compat/zipball/a9b97968bcde1c4de2a5ec6cbd06a0f6c919b46e", + "reference": "a9b97968bcde1c4de2a5ec6cbd06a0f6c919b46e", "shasum": "" }, "require": { @@ -565,20 +565,20 @@ "pseudorandom", "random" ], - "time": "2016-04-03 06:00:07" + "time": "2016-11-07 23:38:38" }, { "name": "psr/log", - "version": "1.0.1", + "version": "1.0.2", "source": { "type": "git", "url": "https://github.com/php-fig/log.git", - "reference": "5277094ed527a1c4477177d102fe4c53551953e0" + "reference": "4ebe3a8bf773a19edfe0a84b6585ba3d401b724d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/log/zipball/5277094ed527a1c4477177d102fe4c53551953e0", - "reference": "5277094ed527a1c4477177d102fe4c53551953e0", + "url": "https://api.github.com/repos/php-fig/log/zipball/4ebe3a8bf773a19edfe0a84b6585ba3d401b724d", + "reference": "4ebe3a8bf773a19edfe0a84b6585ba3d401b724d", "shasum": "" }, "require": { @@ -612,20 +612,20 @@ "psr", "psr-3" ], - "time": "2016-09-19 16:02:08" + "time": "2016-10-10 12:19:37" }, { "name": "symfony/debug", - "version": "v3.1.4", + "version": "v3.1.6", "source": { "type": "git", "url": "https://github.com/symfony/debug.git", - "reference": "34f6ac18c2974ca5fce68adf419ee7d15def6f11" + "reference": "e2b3f74a67fc928adc3c1b9027f73e1bc01190a8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/debug/zipball/34f6ac18c2974ca5fce68adf419ee7d15def6f11", - "reference": "34f6ac18c2974ca5fce68adf419ee7d15def6f11", + "url": "https://api.github.com/repos/symfony/debug/zipball/e2b3f74a67fc928adc3c1b9027f73e1bc01190a8", + "reference": "e2b3f74a67fc928adc3c1b9027f73e1bc01190a8", "shasum": "" }, "require": { @@ -669,20 +669,20 @@ ], "description": "Symfony Debug Component", "homepage": "https://symfony.com", - "time": "2016-08-23 13:39:15" + "time": "2016-09-06 11:02:40" }, { "name": "symfony/event-dispatcher", - "version": "v3.1.4", + "version": "v3.1.6", "source": { "type": "git", "url": "https://github.com/symfony/event-dispatcher.git", - "reference": "c0c00c80b3a69132c4e55c3e7db32b4a387615e5" + "reference": "28b0832b2553ffb80cabef6a7a812ff1e670c0bc" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/c0c00c80b3a69132c4e55c3e7db32b4a387615e5", - "reference": "c0c00c80b3a69132c4e55c3e7db32b4a387615e5", + "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/28b0832b2553ffb80cabef6a7a812ff1e670c0bc", + "reference": "28b0832b2553ffb80cabef6a7a812ff1e670c0bc", "shasum": "" }, "require": { @@ -729,20 +729,20 @@ ], "description": "Symfony EventDispatcher Component", "homepage": "https://symfony.com", - "time": "2016-07-19 10:45:57" + "time": "2016-10-13 06:28:43" }, { "name": "symfony/finder", - "version": "v3.1.4", + "version": "v3.1.6", "source": { "type": "git", "url": "https://github.com/symfony/finder.git", - "reference": "e568ef1784f447a0e54dcb6f6de30b9747b0f577" + "reference": "205b5ffbb518a98ba2ae60a52656c4a31ab00c6f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/finder/zipball/e568ef1784f447a0e54dcb6f6de30b9747b0f577", - "reference": "e568ef1784f447a0e54dcb6f6de30b9747b0f577", + "url": "https://api.github.com/repos/symfony/finder/zipball/205b5ffbb518a98ba2ae60a52656c4a31ab00c6f", + "reference": "205b5ffbb518a98ba2ae60a52656c4a31ab00c6f", "shasum": "" }, "require": { @@ -778,20 +778,20 @@ ], "description": "Symfony Finder Component", "homepage": "https://symfony.com", - "time": "2016-08-26 12:04:02" + "time": "2016-09-28 00:11:12" }, { "name": "symfony/http-foundation", - "version": "v3.1.4", + "version": "v3.1.6", "source": { "type": "git", "url": "https://github.com/symfony/http-foundation.git", - "reference": "63592e00fd90632b57ee50220a1ddb29b6bf3bb4" + "reference": "f21e5a8b88274b7720779aa88f9c02c6d6ec08d7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-foundation/zipball/63592e00fd90632b57ee50220a1ddb29b6bf3bb4", - "reference": "63592e00fd90632b57ee50220a1ddb29b6bf3bb4", + "url": "https://api.github.com/repos/symfony/http-foundation/zipball/f21e5a8b88274b7720779aa88f9c02c6d6ec08d7", + "reference": "f21e5a8b88274b7720779aa88f9c02c6d6ec08d7", "shasum": "" }, "require": { @@ -831,20 +831,20 @@ ], "description": "Symfony HttpFoundation Component", "homepage": "https://symfony.com", - "time": "2016-08-22 12:11:19" + "time": "2016-10-24 15:52:44" }, { "name": "symfony/http-kernel", - "version": "v3.1.4", + "version": "v3.1.6", "source": { "type": "git", "url": "https://github.com/symfony/http-kernel.git", - "reference": "aeda215d6b01f119508c090d2a09ebb5b0bc61f3" + "reference": "c235f1b13ba67012e283996a5427f22e2e04be14" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-kernel/zipball/aeda215d6b01f119508c090d2a09ebb5b0bc61f3", - "reference": "aeda215d6b01f119508c090d2a09ebb5b0bc61f3", + "url": "https://api.github.com/repos/symfony/http-kernel/zipball/c235f1b13ba67012e283996a5427f22e2e04be14", + "reference": "c235f1b13ba67012e283996a5427f22e2e04be14", "shasum": "" }, "require": { @@ -852,7 +852,7 @@ "psr/log": "~1.0", "symfony/debug": "~2.8|~3.0", "symfony/event-dispatcher": "~2.8|~3.0", - "symfony/http-foundation": "~2.8.8|~3.0.8|~3.1.2|~3.2" + "symfony/http-foundation": "~2.8.13|~3.1.6|~3.2" }, "conflict": { "symfony/config": "<2.8" @@ -913,20 +913,20 @@ ], "description": "Symfony HttpKernel Component", "homepage": "https://symfony.com", - "time": "2016-09-03 15:28:24" + "time": "2016-10-27 02:38:31" }, { "name": "symfony/polyfill-mbstring", - "version": "v1.2.0", + "version": "v1.3.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "dff51f72b0706335131b00a7f49606168c582594" + "reference": "e79d363049d1c2128f133a2667e4f4190904f7f4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/dff51f72b0706335131b00a7f49606168c582594", - "reference": "dff51f72b0706335131b00a7f49606168c582594", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/e79d363049d1c2128f133a2667e4f4190904f7f4", + "reference": "e79d363049d1c2128f133a2667e4f4190904f7f4", "shasum": "" }, "require": { @@ -938,7 +938,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.2-dev" + "dev-master": "1.3-dev" } }, "autoload": { @@ -972,11 +972,11 @@ "portable", "shim" ], - "time": "2016-05-18 14:26:46" + "time": "2016-11-14 01:06:16" }, { "name": "symfony/routing", - "version": "v3.1.4", + "version": "v3.1.6", "source": { "type": "git", "url": "https://github.com/symfony/routing.git", @@ -1051,16 +1051,16 @@ }, { "name": "symfony/translation", - "version": "v3.1.4", + "version": "v3.1.6", "source": { "type": "git", "url": "https://github.com/symfony/translation.git", - "reference": "a35edc277513c9bc0f063ca174c36b346f974528" + "reference": "ff1285087397d2f64041b35e591f3025881c90cd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/translation/zipball/a35edc277513c9bc0f063ca174c36b346f974528", - "reference": "a35edc277513c9bc0f063ca174c36b346f974528", + "url": "https://api.github.com/repos/symfony/translation/zipball/ff1285087397d2f64041b35e591f3025881c90cd", + "reference": "ff1285087397d2f64041b35e591f3025881c90cd", "shasum": "" }, "require": { @@ -1111,7 +1111,7 @@ ], "description": "Symfony Translation Component", "homepage": "https://symfony.com", - "time": "2016-08-05 08:37:39" + "time": "2016-10-18 04:30:12" }, { "name": "watson/rememberable", diff --git a/src/Providers/WordpressServiceProvider.php b/src/Providers/WordpressServiceProvider.php index 92f8268..e290937 100644 --- a/src/Providers/WordpressServiceProvider.php +++ b/src/Providers/WordpressServiceProvider.php @@ -33,6 +33,9 @@ class WordpressServiceProvider extends ServiceProvider ); $this->setConfig(); + + Action::hook('after_setup_theme', [$this, 'addThemeSupport']); + Action::hook('widgets_init', [$this, 'addSidebarSupport']); } /** @@ -49,11 +52,11 @@ class WordpressServiceProvider extends ServiceProvider $this->setDatabaseConstants($table_prefix); require ABSPATH . 'wp-settings.php'; - $this->triggerHooks(); - // Set up the WordPress query. wp(); + $this->triggerHooks(); + if (!$this->app->runningInConsole() && (defined('WP_ADMIN') || str_contains(Request::server('SCRIPT_NAME'), strrchr(wp_login_url(), '/')))) { // disable query caching when in Wordpress admin @@ -68,13 +71,13 @@ class WordpressServiceProvider extends ServiceProvider */ public function addThemeSupport() { - foreach (config('supports') as $key => $value) { + collect(config('supports'))->each(function ($value, $key) { if (is_string($key)) { add_theme_support($key, $value); } else { add_theme_support($value); } - } + }); } /** @@ -127,14 +130,6 @@ class WordpressServiceProvider extends ServiceProvider $_SERVER['SERVER_PROTOCOL'] = 'https'; $_SERVER['HTTP_HOST'] = parse_url(config('app.url'))['host']; } - - // we need to register this hook before wp-settings.php is included, which also means - // that add_filter hasn't been included yet so we have to add to the wp_filter global - // manually. - $GLOBALS['wp_filter']['after_setup_theme'][10][] = [ - 'function' => [$this, 'addThemeSupport'], - 'accepted_args' => 0, - ]; } /** @@ -163,6 +158,27 @@ class WordpressServiceProvider extends ServiceProvider } /** + * Register custom sidebars with Wordpress. + * + * @return void + */ + public function addSidebarSupport() + { + collect(config('sidebars'))->each(function ($value) { + register_sidebar(array_merge([ + 'name' => '', + 'id' => str_slug('sidebar-' . ($value['name'] ?? count($GLOBALS['wp_registered_sidebars']) + 1)), + 'description' => '', + 'class' => '', + 'before_widget' => '
', + 'after_widget' => '
', + 'before_title' => '

', + 'after_title' => '

' + ], $value)); + }); + } + + /** * Register all the site's custom post types with Wordpress. * * @return void diff --git a/src/Support/Action.php b/src/Support/Action.php index 8d95163..44dc99f 100644 --- a/src/Support/Action.php +++ b/src/Support/Action.php @@ -18,6 +18,20 @@ class Action */ public static function hook($tag, $function, $priority = 10, $acceptedArgs = 1) { + if (!function_exists('add_filter')) { + // we need to register this hook before wp-settings.php is included, which also means + // that add_filter hasn't been included yet so we have to add to the wp_filter global + // manually. + $GLOBALS['wp_filter'][$tag][$priority][] = [ + 'function' => function (...$args) use ($function) { + return app()->call($function, $args); + }, + 'accepted_args' => $acceptedArgs, + ]; + + return; + } + add_filter($tag, function (...$args) use ($function) { return app()->call($function, $args); }, $priority, $acceptedArgs); -- libgit2 1.7.2