Meta helpers for post & autobind post to the method params
Diff
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(-)
@@ -1,7 +1,8 @@
<?php
namespace Koselig\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -47,6 +48,31 @@
public function meta()
{
return $this->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();
}
/**
@@ -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';
@@ -1,40 +1,0 @@
<?php
namespace Koselig\Routing;
use Illuminate\Http\Request;
use Illuminate\Routing\Route as LaravelRoute;
use Illuminate\Support\Facades\Input;
use Symfony\Component\Routing\Route as SymfonyRoute;
/**
* Extend the base Laravel routing functionality to add multisite support
* to Route::get, Route::post, etc methods.
*
* @author Jordan Doyle <jordan@doyle.wf>
*/
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;
}
}
@@ -1,9 +1,11 @@
<?php
namespace Koselig\Routing;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Koselig\Models\Post;
use Koselig\Support\Wordpress;
use ReflectionFunction;
/**
* Singular route, this route is matched when the user
@@ -34,6 +36,30 @@
$this->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();
}
/**