Skip to content

Commit

Permalink
Merge pull request #859 from owlchester/refactor/remove-blade-macros
Browse files Browse the repository at this point in the history
Remove blade macros
  • Loading branch information
ilestis authored Apr 24, 2024
2 parents f2550c8 + 3ac13de commit 488ca27
Show file tree
Hide file tree
Showing 30 changed files with 268 additions and 245 deletions.
5 changes: 5 additions & 0 deletions app/Models/Ad.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public function scopeSection(Builder $query, int $section)
{
return $query->where('section', $section);
}

public function isSidebar(): bool
{
return $this->section == self::SECTION_SIDEBAR;
}
}
4 changes: 4 additions & 0 deletions app/Models/Concerns/UserBoosters.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ trait UserBoosters
*/
public function hasBoosters(): bool
{
// Force the page to think that the user has no boosters
if (request()->get('_booster') === '0') {
return false;
}
return $this->isGoblin();
}

Expand Down
128 changes: 0 additions & 128 deletions app/Providers/MacroServiceProvider.php

This file was deleted.

75 changes: 75 additions & 0 deletions app/View/Components/Ad.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\View\Components;

use App\Models\Campaign;
use App\User;
use Carbon\Carbon;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class Ad extends Component
{
public ?string $section;
public ?Campaign $campaign;
protected User $user;

/**
* Create a new component instance.
*/
public function __construct(
string $section = null,
Campaign $campaign = null,
) {
$this->section = $section;
$this->campaign = $campaign;
}

/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
if (auth()->check()) {
$this->user = auth()->user();
}
if (!$this->hasAd()) {
return '';
}
return view('components.ad');
}

/**
* Determine if ads should be displayed
*/
protected function hasAd(): bool
{
// If we don't have venatus enabled, then we don't have any ads to show
if (!config('tracking.venatus.enabled')) {
return false;
}

// If requesting a section that isn't set up, don't show
if (!empty($this->section) && empty(config('tracking.venatus.' . $this->section))) {
return false;
}
// Parameter to force ads to be displayed
if (request()->has('_showads')) {
return true;
}
if (isset($this->user)) {
// Subscribed users don't have ads
if ($this->user->isSubscriber()) {
return false;
}
// User has been created less than 24 hours ago
if ($this->user->created_at->diffInHours(Carbon::now()) < 24) {
return false;
}
}

// Premium campaigns don't have ads displayed to their members
return !empty($this->campaign) && !$this->campaign->boosted();
}
}
82 changes: 82 additions & 0 deletions app/View/Components/Ads/Native.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace App\View\Components\Ads;

use App\Facades\AdCache;
use App\Models\Ad;
use App\Models\Campaign;
use App\User;
use Carbon\Carbon;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class Native extends Component
{
public int $section;
public Campaign $campaign;
public User $user;
public Ad $ad;

/**
* Create a new component instance.
*/
public function __construct(
int $section,
Campaign $campaign = null,
) {
$this->section = $section;
if ($campaign) {
$this->campaign = $campaign;
}
if (auth()->check()) {
$this->user = auth()->user();
}
}

/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
if (!$this->noAds()) {
return '';
}
$this->ad = AdCache::get();
return view('components.ads.native');
}

protected function noAds(): bool
{
// No admin panel set up, no ads possible, since the admin project provides the tables
if (!config('app.admin')) {
return false;
}
// If we provided an ad test, override that
if (request()->has('_adtest') && auth()->user()->hasRole('admin')) {
return AdCache::test($this->section, request()->get('_adtest'));
}
// If the section has no ads, don't try and show anything
if (!AdCache::has($this->section)) {
return false;
}
// Force ads displayed
if (request()->get('_boost') === '0') {
return true;
}

if (isset($this->user)) {
// Subscribed users don't have ads
if ($this->user->isSubscriber()) {
return false;
}
// User has been created less than 24 hours ago
if ($this->user->created_at->diffInHours(Carbon::now()) < 24) {
return false;
}
}

// Premium campaigns don't either have ads displayed to their members
return isset($this->campaign) && !$this->campaign->boosted();
}
}
1 change: 0 additions & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\MacroServiceProvider::class,
App\Providers\CampaignLocalizationServiceProvider::class,
App\Providers\MentionsServiceProvider::class,
App\Providers\BreadcrumbServiceProvider::class,
Expand Down
1 change: 0 additions & 1 deletion resources/js/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ const initDashboardAdminUI = () => {
new Sortable(el, {
handle: '.handle',
onEnd: function (/**Event*/evt) {
// Allow ajax requests to use the X_CSRF_TOKEN for deletes
$.post({
url: $('#widgets').data('url'),
dataType: 'json',
Expand Down
3 changes: 1 addition & 2 deletions resources/js/vendor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
// Also add it to jQuery while we are at it, so we don't have to do it every time either
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': token.content
}
});
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}

/**
Expand Down
Loading

0 comments on commit 488ca27

Please sign in to comment.