-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9169443
Showing
7 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
vendor | ||
mix-manifest.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Multisite Propagator | ||
|
||
> Have you ever added a new site in your multisite Statamic setup and had to manually enable and publish entries from the origin? No more! | ||
## Features | ||
|
||
Use this Multisite Propagator to automatically create entries and save a few seconds, minutes or hours. | ||
|
||
## How to Install | ||
|
||
You can search for this addon in the `Tools > Addons` section of the Statamic control panel and click **install**, or run the following command from your project root: | ||
|
||
``` bash | ||
composer require reachweb/multisite-propagator | ||
``` | ||
|
||
## How to Use | ||
|
||
This extension adds a `propagate:entries` command! | ||
|
||
Simply use it like this: `php please propagate:entries collection site` and it will work its magic. | ||
|
||
Please note that it will create an entry even if it already exists. So use it right at the start of adding a new site to your multisite setup. | ||
|
||
Also we only use it in our setup, your milage may vary. *Data loss may occur.* You have been warned! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "reachweb/multisite-propagator", | ||
"autoload": { | ||
"psr-4": { | ||
"Reach\\MultisitePropagator\\": "src" | ||
} | ||
}, | ||
"extra": { | ||
"statamic": { | ||
"name": "Multisite Propagator", | ||
"description": "Multisite Propagator addon" | ||
}, | ||
"laravel": { | ||
"providers": [ | ||
"Reach\\MultisitePropagator\\ServiceProvider" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Reach\MultisitePropagator\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Statamic\Facades\Stache; | ||
use Statamic\Console\EnhancesCommands; | ||
use Statamic\Console\RunsInPlease; | ||
use Illuminate\Support\Facades\Cache; | ||
use Reach\MultisitePropagator\Helpers\MultisitePropagatorHelper; | ||
use Reach\MultisitePropagator\Exceptions\PropagateException; | ||
|
||
class PropagateEntries extends Command | ||
{ | ||
use RunsInPlease, EnhancesCommands; | ||
|
||
protected $name = 'statamic:propagate'; | ||
protected $signature = 'statamic:propagate:entries {collection} {site}'; | ||
protected $description = 'Propagate your existing entries to another site'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
$collection = $this->argument('collection'); | ||
$site = $this->argument('site'); | ||
|
||
$confirmed = $this->confirm("I will now create entries for [<comment>{$site}</comment>], for the [<comment>{$collection}</comment>] collection. THIS WILL REWRITE EXISTING ENTRIES. Is this okay?"); | ||
|
||
if (! $confirmed) { | ||
$this->crossLine('You stopped the prop before I even started!'); | ||
return; | ||
} | ||
|
||
Stache::disableUpdatingIndexes(); | ||
|
||
$this->comment('Creating...'); | ||
|
||
try { | ||
(new MultisitePropagatorHelper($collection, $site))->propagate(); | ||
} catch (PropagateException $e) { | ||
$this->crossLine($e->getMessage()); | ||
return; | ||
} | ||
|
||
$this->checkLine('Entries proped and ready to go!'); | ||
|
||
Cache::clear(); | ||
$this->checkLine('Cache cleared.'); | ||
Stache::clear(); | ||
$this->checkLine('Stache cleared.'); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
namespace Reach\MultisitePropagator\Exceptions; | ||
|
||
use Exception; | ||
|
||
class PropagateException extends Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Reach\MultisitePropagator\Helpers; | ||
|
||
use Reach\MultisitePropagator\Exceptions\PropagateException; | ||
use Statamic\Facades\Collection; | ||
use Statamic\Entries\Collection as StatamicCollection; | ||
use Statamic\Facades\Entry; | ||
use Statamic\Facades\Site; | ||
use Statamic\Sites\Site as StatamicSite; | ||
|
||
class MultisitePropagatorHelper | ||
{ | ||
// The collection to search for | ||
protected $collection; | ||
|
||
// The site to propagate to | ||
protected $site; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct($handle, $site) | ||
{ | ||
$this->collection = $this->findCollection($handle); | ||
$this->site = $this->findSite($site); | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
* | ||
* @return void | ||
*/ | ||
public function propagate() | ||
{ | ||
$entries = $this->collection->queryEntries()->get(); | ||
$entries->each(function($entry) { | ||
Entry::make() | ||
->collection($this->collection) | ||
->origin($entry) | ||
->locale($this->site) | ||
->slug($entry->slug()) | ||
->save(); | ||
}); | ||
} | ||
|
||
protected function findCollection(string $handle): StatamicCollection | ||
{ | ||
$collection = Collection::findByHandle($handle); | ||
if ($collection == null) { | ||
throw new PropagateException('I cannot find this collection bro! Did you use the correct handle?'); | ||
} | ||
return $collection; | ||
} | ||
|
||
protected function findSite(string $handle): StatamicSite | ||
{ | ||
$site = Site::get($handle); | ||
if ($site == null) { | ||
throw new PropagateException('There is no such site bro! Remember, use the handle, not the name!'); | ||
} | ||
return $site; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Reach\MultisitePropagator; | ||
|
||
use Statamic\Providers\AddonServiceProvider; | ||
|
||
class ServiceProvider extends AddonServiceProvider | ||
{ | ||
|
||
protected $commands = [ | ||
Console\Commands\PropagateEntries::class | ||
]; | ||
|
||
} |