Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
afonic committed Feb 11, 2022
0 parents commit 9169443
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
vendor
mix-manifest.json
25 changes: 25 additions & 0 deletions README.md
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!
19 changes: 19 additions & 0 deletions composer.json
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"
]
}
}
}
58 changes: 58 additions & 0 deletions src/Console/Commands/PropagateEntries.php
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.');

}

}
9 changes: 9 additions & 0 deletions src/Exceptions/PropagateException.php
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
{

}
66 changes: 66 additions & 0 deletions src/Helpers/MultisitePropagatorHelper.php
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;
}
}
14 changes: 14 additions & 0 deletions src/ServiceProvider.php
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
];

}

0 comments on commit 9169443

Please sign in to comment.