diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0199b2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +vendor +mix-manifest.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..b2eb912 --- /dev/null +++ b/README.md @@ -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! diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..d0a87c1 --- /dev/null +++ b/composer.json @@ -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" + ] + } + } +} diff --git a/src/Console/Commands/PropagateEntries.php b/src/Console/Commands/PropagateEntries.php new file mode 100644 index 0000000..28c2a64 --- /dev/null +++ b/src/Console/Commands/PropagateEntries.php @@ -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.'); + + } + +} \ No newline at end of file diff --git a/src/Exceptions/PropagateException.php b/src/Exceptions/PropagateException.php new file mode 100644 index 0000000..27e485e --- /dev/null +++ b/src/Exceptions/PropagateException.php @@ -0,0 +1,9 @@ +<?php +namespace Reach\MultisitePropagator\Exceptions; + +use Exception; + +class PropagateException extends Exception +{ + +} \ No newline at end of file diff --git a/src/Helpers/MultisitePropagatorHelper.php b/src/Helpers/MultisitePropagatorHelper.php new file mode 100644 index 0000000..c58a352 --- /dev/null +++ b/src/Helpers/MultisitePropagatorHelper.php @@ -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; + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php new file mode 100644 index 0000000..8d21b58 --- /dev/null +++ b/src/ServiceProvider.php @@ -0,0 +1,14 @@ +<?php + +namespace Reach\MultisitePropagator; + +use Statamic\Providers\AddonServiceProvider; + +class ServiceProvider extends AddonServiceProvider +{ + + protected $commands = [ + Console\Commands\PropagateEntries::class + ]; + +}