/
ClearSite.php
107 lines (93 loc) · 2.42 KB
/
ClearSite.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Statamic\Console\RunsInPlease;
use Statamic\Facades\Entry;
use Statamic\Facades\GlobalSet;
class ClearSite extends Command
{
use RunsInPlease;
/**
* The name of the console command.
*
* @var string
*/
protected $name = 'peak:clear-site';
/**
* The console command description.
*
* @var string
*/
protected $description = "Clear all default Peak content.";
/**
* Execute the console command.
*
* @return bool|null
*/
public function handle()
{
$clear_site = ($this->confirm('Are you sure you want to clear all default Peak content?', true)) ? true : false;
if ($clear_site) {
$this->trashAssets();
$this->clearGlobalSocialMedia();
$this->clearPageBuilder('/');
$this->trashPagesButHome();
Artisan::call('statamic:glide:clear');
Artisan::call('cache:clear');
$this->info("Your view from the peak is clear.");
}
}
/**
* Trash all assets.
*
* @return bool|null
*/
protected function trashAssets()
{
$files = new Filesystem;
$path = public_path('assets');
if ($files->exists($path))
$files->cleanDirectory($path);
}
/**
* Trash global social media data.
*
* @return bool|null
*/
protected function clearGlobalSocialMedia()
{
$set = GlobalSet::findByHandle('social_media');
$set->inDefaultSite()->set('social_media', null)->save();
}
/**
* Clear the page builder.
*
* @return bool|null
*/
protected function clearPageBuilder($uri)
{
Entry::findByUri($uri)
->set('page_builder', null)
->save();
}
/**
* Trash all pages but home.
*
* @return bool|null
*/
protected function trashPagesButHome()
{
$pages = Entry::query()
->where('collection', 'pages')
->where('id', '!=', 'home')
->get();
foreach ($pages as $page) {
$file_path = base_path("content/collections/pages/{$page->slug()}.md");
if (File::exists($file_path))
File::delete($file_path);
}
}
}