Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for WordPress Multisite #101

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions src/Models/Meta/UserMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class UserMeta extends AbstractMeta
*/
protected $table = 'usermeta';

/**
* @inheritdoc
*/
protected bool $useBasePrefix = true;

/**
* @return HasOne
*/
Expand Down
78 changes: 78 additions & 0 deletions src/Models/Multisite/Blog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Copyright © Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <[email protected]>
*/

namespace Dbout\WpOrm\Models\Multisite;

use Carbon\Carbon;
use Dbout\WpOrm\Orm\AbstractModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
* @property-read int $blog_id
* @property int $site_id
* @property string $domain
* @property string $path
* @property Carbon $registered
* @property Carbon $last_updated
* @property bool $public
* @property bool $archived
* @property bool $mature
* @property bool $spam
* @property bool $deleted
* @property int $lang_id
*
* @property-read Site $site
* @property-read BlogVersion|null $version
*/
class Blog extends AbstractModel
{
public const CREATED_AT = self::REGISTERED;
public const UPDATED_AT = self::LAST_UPDATED;
final public const BLOG_ID = 'blog_id';
final public const SITE_ID = 'site_id';
final public const DOMAIN = 'domain';
final public const PATH = 'path';
final public const REGISTERED = 'registered';
final public const LAST_UPDATED = 'last_updated';
final public const PUBLIC = 'public';
final public const ARCHIVED = 'archived';
final public const MATURE = 'mature';
final public const SPAM = 'spam';
final public const DELETED = 'deleted';
final public const LANG_ID = 'lang_id';

protected $primaryKey = self::BLOG_ID;

protected bool $useBasePrefix = true;

protected $casts = [
self::BLOG_ID => 'int',
self::SITE_ID => 'int',
self::REGISTERED => 'datetime',
self::LAST_UPDATED => 'datetime',
self::PUBLIC => 'bool',
self::ARCHIVED => 'bool',
self::MATURE => 'bool',
self::SPAM => 'bool',
self::DELETED => 'bool',
self::LANG_ID => 'int',
];

protected $table = 'blogs';

public function site(): BelongsTo
{
return $this->belongsTo(Site::class, self::SITE_ID);
}

public function version(): HasOne
{
return $this->hasOne(BlogVersion::class, BlogVersion::BLOG_ID);
}
}
44 changes: 44 additions & 0 deletions src/Models/Multisite/BlogVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright © Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <[email protected]>
*/

namespace Dbout\WpOrm\Models\Multisite;

use Carbon\Carbon;
use Dbout\WpOrm\Orm\AbstractModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property-read int $blog_id
* @property string $db_version
* @property Carbon $last_updated
*
* @property-read Blog $blog
*/
class BlogVersion extends AbstractModel
{
public const CREATED_AT = null;
public const UPDATED_AT = self::LAST_UPDATED;
final public const BLOG_ID = 'blog_id';
final public const DB_VERSION = 'db_version';
final public const LAST_UPDATED = 'last_updated';

protected bool $useBasePrefix = true;

protected $table = 'blog_versions';

protected $primaryKey = self::BLOG_ID;

protected $casts = [
self::LAST_UPDATED => 'datetime',
];

public function blog(): BelongsTo
{
return $this->belongsTo(Blog::class, self::BLOG_ID);
}
}
49 changes: 49 additions & 0 deletions src/Models/Multisite/RegistrationLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <[email protected]>
*/

namespace Dbout\WpOrm\Models\Multisite;

use Carbon\Carbon;
use Dbout\WpOrm\Orm\AbstractModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property-read int $ID
* @property string $email
* @property string $IP
* @property int $blog_id
* @property Carbon $date_registered
*
* @property-read Blog|null $blog
*/
class RegistrationLog extends AbstractModel
{
public const CREATED_AT = self::DATE_REGISTERED;
public const UPDATED_AT = null;
final public const ID = 'ID';
final public const EMAIL = 'email';
final public const IP = 'IP';
final public const BLOG_ID = 'blog_id';
final public const DATE_REGISTERED = 'date_registered';

protected bool $useBasePrefix = true;

protected $table = 'registration_log';

protected $primaryKey = self::ID;

protected $casts = [
self::BLOG_ID => 'int',
self::DATE_REGISTERED => 'datetime',
];

public function blog(): BelongsTo
{
return $this->belongsTo(Blog::class, self::BLOG_ID);
}
}
63 changes: 63 additions & 0 deletions src/Models/Multisite/Signup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <[email protected]>
*/

namespace Dbout\WpOrm\Models\Multisite;

use Carbon\Carbon;
use Dbout\WpOrm\Models\User;
use Dbout\WpOrm\Orm\AbstractModel;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
* @property-read int $signup_id
* @property string $domain
* @property string $path
* @property string $title
* @property string $user_login
* @property string $user_email
* @property Carbon $registered
* @property Carbon $activated
* @property bool $active
* @property string $activation_key
* @property string|null $meta
*
* @property-read User|null $user
*/
class Signup extends AbstractModel
{
public const CREATED_AT = self::REGISTERED;
public const UPDATED_AT = null;
final public const SIGNUP_ID = 'signup_id';
final public const DOMAIN = 'domain';
final public const PATH = 'path';
final public const TITLE = 'title';
final public const USER_LOGIN = 'user_login';
final public const USER_EMAIL = 'user_email';
final public const REGISTERED = 'registered';
final public const ACTIVATED = 'activated';
final public const ACTIVE = 'active';
final public const ACTIVATION_KEY = 'activation_key';
final public const META = 'meta';

protected bool $useBasePrefix = true;

protected $table = 'signups';

protected $primaryKey = self::SIGNUP_ID;

protected $casts = [
self::REGISTERED => 'datetime',
self::ACTIVATED => 'datetime',
self::ACTIVE => 'bool',
];

public function user(): HasOne
{
return $this->hasOne(User::class, User::EMAIL, self::USER_EMAIL);
}
}
50 changes: 50 additions & 0 deletions src/Models/Multisite/Site.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <[email protected]>
*/

namespace Dbout\WpOrm\Models\Multisite;

use Dbout\WpOrm\Concerns\HasMetas;
use Dbout\WpOrm\MetaMappingConfig;
use Dbout\WpOrm\Orm\AbstractModel;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* @property-read int $id
* @property string $domain
* @property string $path
*
* @property-read Collection<SiteMeta> $metas
* @property-read Collection<Blog> $blogs
*/
class Site extends AbstractModel
{
use HasMetas;

public const CREATED_AT = null;
public const UPDATED_AT = null;
final public const ID = 'id';
final public const DOMAIN = 'domain';
final public const PATH = 'path';

protected bool $useBasePrefix = true;

protected $table = 'site';

protected $primaryKey = self::ID;

public function blogs(): HasMany
{
return $this->hasMany(Blog::class, Blog::SITE_ID);
}

public function getMetaConfigMapping(): MetaMappingConfig
{
return new MetaMappingConfig(SiteMeta::class, SiteMeta::SITE_ID);
}
}
37 changes: 37 additions & 0 deletions src/Models/Multisite/SiteMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright © Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <[email protected]>
*/

namespace Dbout\WpOrm\Models\Multisite;

use Dbout\WpOrm\Models\Meta\AbstractMeta;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
* @property-read int $meta_id
* @property int $site_id
* @property string|null $meta_key
* @property mixed|null $meta_value
*
* @property-read Site $site
*/
class SiteMeta extends AbstractMeta
{
final public const META_ID = 'meta_id';
final public const SITE_ID = 'site_id';

protected bool $useBasePrefix = true;

protected $table = 'sitemeta';

protected $primaryKey = self::META_ID;

public function site(): HasOne
{
return $this->hasOne(Site::class, Site::ID, self::SITE_ID);
}
}
5 changes: 5 additions & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class User extends AbstractModel implements WithMetaModelInterface
*/
protected $table = 'users';

/**
* @inheritdoc
*/
protected bool $useBasePrefix = true;

/**
* @inheritDoc
*/
Expand Down
15 changes: 13 additions & 2 deletions src/Orm/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ abstract class AbstractModel extends Model
*/
protected $guarded = [];

/**
* Indicates if the model should use base prefix for multisite shared tables.
* @var bool
*/
protected bool $useBasePrefix = false;

/**
* @param array $attributes
*/
Expand All @@ -34,9 +40,14 @@ public function __construct(array $attributes = [])
/**
* @inheritDoc
*/
public function getTable()
public function getTable(): ?string
{
$prefix = $this->getConnection()->getTablePrefix();
/** @var Database $connection */
$connection = $this->getConnection();
$prefix = $this->useBasePrefix
? $connection->getBaseTablePrefix()
: $connection->getTablePrefix();

if ($this->table !== null && $this->table !== '') {
return str_starts_with($this->table, $prefix) ? $this->table : $prefix . $this->table;
}
Expand Down
Loading
Loading