From cee204d0dfac455688d7e00c97d6fc7c951c3f51 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Thu, 06 Oct 2016 17:59:33 +0100 Subject: [PATCH] Meta helpers for post & autobind post to the method params --- src/Models/Post.php | 26 ++++++++++++++++++++++++++ src/Providers/WordpressServiceProvider.php | 1 + src/Routing/Route.php | 40 ---------------------------------------- src/Routing/SingularRoute.php | 26 ++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 40 deletions(-) diff --git a/src/Models/Post.php b/src/Models/Post.php index c33ecc2..bd610dc 100644 --- a/src/Models/Post.php +++ a/src/Models/Post.php @@ -1,7 +1,8 @@ hasMany(Meta::class); + } + + /** + * Get meta values for this Post. + * + * @param array|string|null $key + * @return mixed + */ + public function getMeta($key = null) + { + /** + * @var Collection + */ + $meta = $this->meta; + + if (is_array($key)) { + $meta = $meta->whereIn('meta_key', $key); + } elseif (is_string($key)) { + $meta = $meta->where('meta_key', $key)->first(); + return $meta ? $meta->meta_value : null; + } + + return $meta->mapWithKeys(function ($item) { + return [$item->meta_key => $item->meta_value]; + })->all(); } /** diff --git a/src/Providers/WordpressServiceProvider.php b/src/Providers/WordpressServiceProvider.php index 183b476..d9aa5f5 100644 --- a/src/Providers/WordpressServiceProvider.php +++ a/src/Providers/WordpressServiceProvider.php @@ -76,6 +76,7 @@ if ($this->app->runningInConsole()) { // allow wordpress to run, even when running from console (ie. artisan compiling) $_SERVER['SERVER_PROTOCOL'] = 'https'; + $_SERVER['HTTP_HOST'] = parse_url(config('app.url'))['host']; } require ABSPATH . 'wp-settings.php'; diff --git a/src/Routing/Route.php b/src/Routing/Route.php deleted file mode 100644 index cd50c6e..0000000 100644 --- a/src/Routing/Route.php +++ /dev/null @@ -1,40 +1,0 @@ - - */ -class Route extends LaravelRoute -{ - /** - * Determine if the route matches given request. - * - * @param \Illuminate\Http\Request $request - * @param bool $includingMethod - * @return bool - */ - public function matches(Request $request, $includingMethod = true) - { - $this->compileRoute(); - - foreach ($this->getValidators() as $validator) { - if (!$includingMethod && $validator instanceof MethodValidator) { - continue; - } - - if (!$validator->matches($this, $request)) { - return false; - } - } - - return true; - } -} diff --git a/src/Routing/SingularRoute.php b/src/Routing/SingularRoute.php index 8753b29..09904ec 100644 --- a/src/Routing/SingularRoute.php +++ a/src/Routing/SingularRoute.php @@ -1,9 +1,11 @@ types = $this->uri; $this->uri = ''; + } + + /** + * Run the route action and return the response. + * + * @return mixed + */ + protected function runCallable() + { + // bind the current post to the parameters of the function + $function = new ReflectionFunction($this->action['uses']); + $params = $function->getParameters(); + + foreach ($params as $param) { + if ($param->getClass() + && ($param->getClass()->isSubclassOf(Post::class) || $param->getClass()->getName() === Post::class)) { + $builder = $param->getClass()->getMethod('query')->invoke(null); + $post = $builder->find(Wordpress::id()); + + $this->setParameter($param->getName(), $post); + } + } + + return parent::runCallable(); } /** -- rgit 0.1.5