Skip to content

Commit

Permalink
moved Wp::isAdmin/isCli/isFrontEnd to app()
Browse files Browse the repository at this point in the history
  • Loading branch information
andrefelipe committed Jan 19, 2022
1 parent 64835eb commit e36f348
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 57 deletions.
20 changes: 18 additions & 2 deletions src/App/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Bond\Services\View;
use Bond\Utils\Cast;
use Bond\Utils\Link;
use Bond\Settings\Wp;
use League\Container\Container;
use League\Container\ReflectionContainer;
use Mobile_Detect;
Expand Down Expand Up @@ -218,7 +217,7 @@ protected function bootAppFolder()
}

// admin too
if (Wp::isAdmin() && method_exists($classname, 'bootAdmin')) {
if (app()->isAdmin() && method_exists($classname, 'bootAdmin')) {
call_user_func($classname . '::bootAdmin');
}
}
Expand All @@ -239,6 +238,21 @@ public function url(): string
return $this->config()->app->url ??= \untrailingslashit(c('WP_HOME') ?: \get_site_url());
}

public function isFrontEnd(): bool
{
return !\is_admin() && c('WP_USE_THEMES') && !$this->isCli();
}

public function isAdmin(): bool
{
return \is_admin() && \is_user_logged_in();
}

public function isCli(): bool
{
return (bool) c('WP_CLI');
}

public function isProduction(): bool
{
return $this->env === 'production';
Expand Down Expand Up @@ -278,6 +292,8 @@ public function isDesktop(): bool
return $this->is_desktop;
}



public function basePath(): string
{
return $this->base_path;
Expand Down
4 changes: 0 additions & 4 deletions src/App/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ protected function appSettings()

protected function cacheSettings()
{
if (Wp::isCli()) {
$this->cache->enabled = false;
}

if ($this->cache->class) {
$this->container->addShared('cache', $this->cache->class);
}
Expand Down
11 changes: 5 additions & 6 deletions src/Services/AdminColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Bond\Post;
use Bond\Settings\Language;
use Bond\Settings\Wp;
use Bond\Term;
use Bond\User;
use Bond\Utils\Cast;
Expand All @@ -28,7 +27,7 @@ public function config(?bool $enabled = null)

public function enable()
{
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand Down Expand Up @@ -146,7 +145,7 @@ public function addHandler(
string $css = null
) {
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}
$this->columns[$name] = compact('handler', 'width', 'css');
Expand Down Expand Up @@ -186,7 +185,7 @@ protected function handleColumn(string $name, $item)
public function setPostTypeColumns(string $post_type, array $columns)
{
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand All @@ -207,7 +206,7 @@ function ($defaults) use ($columns) {
public function setTaxonomyColumns(string $taxonomy, array $columns)
{
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}
\add_filter(
Expand All @@ -221,7 +220,7 @@ function ($defaults) use ($columns) {
public function setUsersColumns(array $columns)
{
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand Down
6 changes: 4 additions & 2 deletions src/Services/Cache/AbstractCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Bond\Services\Cache;

use Bond\Settings\Wp;
use Bond\Utils\Str;

// TODO implement Service interface too and update the enabled and config methods
// clean Config.php afterwards

abstract class AbstractCache implements CacheInterface
{
protected int $ttl = 0;
Expand All @@ -13,7 +15,7 @@ abstract class AbstractCache implements CacheInterface
public function __construct()
{
// disable initially if WP CLI
if (Wp::isCli()) {
if (app()->isCli()) {
$this->enabled(false);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Services/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Bond\Services;

use Bond\Settings\Wp;
use Bond\Settings\Language;
use Bond\Utils\Cast;
use Bond\Utils\Image;
Expand Down Expand Up @@ -79,7 +78,7 @@ public function enable()
{
// do not enable when it is on WP admin
// nor when programatically loading WP
if (Wp::isFrontEnd()) {
if (app()->isFrontEnd()) {
\add_action('wp', [$this, 'setDefaults'], 2);
\add_action('wp_head', [$this, 'printAllTags'], 99);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Services/Rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Bond\Services\View;
use Bond\Settings\Language;
use Bond\Settings\Rewrite;
use Bond\Settings\Wp;
use Bond\Utils\Link;
use Bond\Utils\Query;

Expand Down Expand Up @@ -80,7 +79,7 @@ public function disableWpRss()
\remove_action('wp_head', 'feed_links', 2);

// rewrite rules
if (Wp::isAdmin()) {
if (app()->isAdmin()) {
\add_filter('rewrite_rules_array', function (array $rules) {

foreach ($rules as $regex => $match) {
Expand Down
22 changes: 11 additions & 11 deletions src/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Admin
public static function setEditorImageSizes(array $sizes)
{
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand All @@ -28,7 +28,7 @@ public static function setEditorImageSizes(array $sizes)
public static function removeUpdateNag()
{
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand All @@ -45,7 +45,7 @@ public static function removeUpdateNag()
public static function addEditorCss()
{
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand Down Expand Up @@ -83,7 +83,7 @@ public static function addLoginCss()
public static function addColorScheme()
{
// don't even add the hook if not on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand All @@ -103,7 +103,7 @@ public static function addColorScheme()
public static function addAdminCss()
{
// don't even add the hook if not on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand Down Expand Up @@ -157,13 +157,13 @@ public static function cleanupAdminBar()

public static function hidePosts()
{
if (Wp::isAdmin() || config('html.admin_bar') !== false) {
if (app()->isAdmin() || config('html.admin_bar') !== false) {
\add_action('wp_before_admin_bar_render', function () {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('new-post');
});
}
if (Wp::isAdmin()) {
if (app()->isAdmin()) {
\add_action('admin_menu', function () {
\remove_menu_page('edit.php');
}, 999);
Expand All @@ -174,7 +174,7 @@ public static function hidePosts()
public static function addFooterCredits()
{
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand All @@ -186,7 +186,7 @@ public static function addFooterCredits()
public static function removeWpVersion()
{
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand All @@ -212,7 +212,7 @@ public static function setSideMenuParams(string $post_type, array $params)
public static function replaceDashboard()
{
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand Down Expand Up @@ -276,7 +276,7 @@ public static function removeAdministrationMenus()

public static function hideTitle($post_types)
{
if (empty($post_types) || !Wp::isAdmin()) {
if (empty($post_types) || !app()->isAdmin()) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Settings/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function disableHeader()
public static function disableDefaultRoutes()
{
// do not disable if is on admin
if (Wp::isAdmin()) {
if (app()->isAdmin()) {
return;
}

Expand All @@ -50,7 +50,7 @@ public static function disableDefaultRoutes()
public static function disableRootRoute()
{
// does not disable if is on admin
if (Wp::isAdmin()) {
if (app()->isAdmin()) {
return;
}

Expand Down
16 changes: 8 additions & 8 deletions src/Settings/Rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Rewrite
public static function reset()
{
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand Down Expand Up @@ -61,7 +61,7 @@ public static function tag(string $name, bool $set_global = false)
public static function pages()
{
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand Down Expand Up @@ -108,7 +108,7 @@ public static function search(
array $extra_params = []
) {
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand Down Expand Up @@ -160,7 +160,7 @@ public static function rss(
bool $multilanguage = false
) {
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand Down Expand Up @@ -214,7 +214,7 @@ public static function archive(
array $extra_params = []
) {
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand Down Expand Up @@ -282,7 +282,7 @@ public static function single(
array $extra_params = []
) {
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand Down Expand Up @@ -323,7 +323,7 @@ public static function rewrite(
array $params = []
) {
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand Down Expand Up @@ -355,7 +355,7 @@ public static function page(
array $extra_params = []
) {
// only needed on admin
if (!Wp::isAdmin()) {
if (!app()->isAdmin()) {
return;
}

Expand Down
20 changes: 2 additions & 18 deletions src/Settings/Wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,12 @@
// Ideas for later:
// Wp::disableComments, for now using plugin disable-comments

// TODO IDEA, migrate to App and remove wp.php config entirelly
// TODO IDEA, migrate everything to App and remove wp.php config entirelly
// 'Wp' naming is not being autofilled by VS Code
// looks like the image may become a service
// looks like the image may become a service too, maybe Uploads or Media...

class Wp
{
public static function isFrontEnd(): bool
{
return !\is_admin() && c('WP_USE_THEMES') && !static::isCli();
}

public static function isAdmin(): bool
{
return \is_admin() && \is_user_logged_in();
}

public static function isCli(): bool
{
return (bool) c('WP_CLI');
}


// Images

public static function sanitizeFilenames()
Expand Down

0 comments on commit e36f348

Please sign in to comment.