Skip to content

Commit

Permalink
Merge pull request #56 from amvisor/fix-published-scope
Browse files Browse the repository at this point in the history
ensure only posts with status 'publish' are being shown in public
  • Loading branch information
atmonshi authored Nov 8, 2022
2 parents 7ac43b8 + 0b8acea commit 5df723a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
.php-cs-fixer.cache
26 changes: 14 additions & 12 deletions src/Http/Livewire/Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ public function render()
$category = request('category');

$posts = Post::NotSticky()
->Search($search)
->ForCatogery($category)
->search($search)
->forCategory($category)
->published()
->orderBy('published_at', 'desc')
->get();

$pages = Post::page()
->Search($search)
->ForCatogery($category)
->search($search)
->forCategory($category)
->published()
->orderBy('published_at', 'desc')
->whereNull('parent_id')
->get();
Expand All @@ -32,6 +34,7 @@ public function render()
$posts = $this->highlightSearchResults($posts, $search);

$recent = Post::posts()
->published()
->limit(config('zeus-sky.site_recent_count', 5))
->orderBy('published_at', 'desc')
->get();
Expand All @@ -45,14 +48,13 @@ public function render()
->withUrl()
->twitter();

return view(app('theme') . '.home')
->with([
'posts' => $posts,
'pages' => $pages,
'recent' => $recent,
'tags' => Tag::withCount('postsPublished')->where('type', 'category')->get(),
'stickies' => Post::sticky()->get(),
])
return view(app('theme') . '.home')->with([
'posts' => $posts,
'pages' => $pages,
'recent' => $recent,
'tags' => Tag::withCount('postsPublished')->where('type', 'category')->get(),
'stickies' => Post::sticky()->published()->get(),
])
->layout(config('zeus-sky.layout'));
}
}
35 changes: 19 additions & 16 deletions src/Models/PostScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ public function scopeSticky($query)

public function scopeNotSticky($query)
{
$query->wherePostType('post')
->where(function ($q) {
return $q->whereDate('sticky_until', '<=', now())->orWhereNull('sticky_until');
})
->whereDate('published_at', '<=', now());
$query->wherePostType('post')->where(function ($q) {
return $q->whereDate('sticky_until', '<=', now())->orWhereNull('sticky_until');
})
->whereDate('published_at', '<=', now());
}

public function scopePublished($query)
Expand Down Expand Up @@ -49,17 +48,19 @@ public function scopePosts($query)
->whereDate('published_at', '<=', now());
}

public function scopeForCatogery($query, $category)
public function scopeForCategory($query, $category)
{
if ($category === null) {
return $query;
}

return $query->where(function ($query) use ($category) {
$query->withAnyTags([$category], 'category');
return $query->where(
function ($query) use ($category) {
$query->withAnyTags([$category], 'category');

return $query;
});
return $query;
}
);
}

public function scopeSearch($query, $term)
Expand All @@ -68,12 +69,14 @@ public function scopeSearch($query, $term)
return $query;
}

return $query->where(function ($query) use ($term) {
foreach (['title', 'slug', 'content', 'description'] as $attribute) {
$query->orWhere(DB::raw("lower($attribute)"), 'like', "%$term%");
}
return $query->where(
function ($query) use ($term) {
foreach (['title', 'slug', 'content', 'description'] as $attribute) {
$query->orWhere(DB::raw("lower({$attribute})"), 'like', "%{$term}%");
}

return $query;
});
return $query;
}
);
}
}
5 changes: 0 additions & 5 deletions tests/ExampleTest.php

This file was deleted.

0 comments on commit 5df723a

Please sign in to comment.