Skip to content

Commit dde34c1

Browse files
Add active statuses config option with default values
1 parent 93fd5a9 commit dde34c1

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

src/Plugin.php

+12-29
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use craft\base\Model;
88
use craft\base\Plugin as BasePlugin;
99
use craft\elements\db\ElementQuery;
10-
use craft\elements\Entry;
1110
use craft\events\ModelEvent;
1211
use craft\helpers\ElementHelper;
1312
use craft\helpers\Queue;
@@ -80,40 +79,24 @@ static function (ModelEvent $event) use ($autoSyncIndices): void {
8079
return;
8180
}
8281

83-
$status = $sender->getStatus();
84-
85-
// Determine which status to use to check if the element is active.
86-
if ($sender instanceof Entry) {
87-
$activeStatus = Entry::STATUS_LIVE;
88-
} else {
89-
$activeStatus = Element::STATUS_ENABLED;
90-
}
91-
92-
if ($status === $activeStatus) {
93-
// If an element is active, then we should update it in the index
94-
$autoSyncIndices->each(static function (Index $index) use ($sender): void {
95-
/** @var ElementQuery<array-key, Element> $query */
96-
$query = $index->query;
82+
$autoSyncIndices->each(static function (Index $index) use ($sender): void {
83+
/** @var ElementQuery<array-key, Element> $query */
84+
$query = $index->query;
85+
if (in_array($sender->getStatus(), $index->activeStatuses, true)) {
86+
// If an element is active, then we should update it in the index
9787
if ($query->id($sender->id)->exists()) {
9888
Queue::push(new SyncJob([
9989
'indexName' => $index->handle,
10090
'identifier' => $sender->id,
10191
]));
10292
}
103-
});
104-
} else {
105-
// Otherwise, we should make sure that it is not in the index
106-
$autoSyncIndices->each(static function (Index $index) use ($sender): void {
107-
/** @var ElementQuery<array-key, Element> $query */
108-
$query = $index->query;
109-
if ($query->status(null)->id($sender->id)->exists()) {
110-
Queue::push(new DeleteJob([
111-
'indexName' => $index->handle,
112-
'identifier' => $sender->id,
113-
]));
114-
}
115-
});
116-
}
93+
} elseif ($query->status(null)->id($sender->id)->exists()) {
94+
Queue::push(new DeleteJob([
95+
'indexName' => $index->handle,
96+
'identifier' => $sender->id,
97+
]));
98+
}
99+
});
117100
}
118101
);
119102

src/builders/IndexBuilder.php

+17
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace fostercommerce\meilisearch\builders;
44

5+
use craft\base\Element;
56
use craft\elements\db\ElementQuery;
67
use craft\elements\db\ElementQueryInterface;
8+
use craft\elements\Entry;
79
use fostercommerce\meilisearch\helpers\Fetch;
810

911
class IndexBuilder
@@ -21,6 +23,11 @@ class IndexBuilder
2123

2224
private bool $autoSync = true;
2325

26+
/**
27+
* @var string[]
28+
*/
29+
private array $activeStatuses = [Element::STATUS_ENABLED, Entry::STATUS_LIVE];
30+
2431
/**
2532
* @var ?callable
2633
*/
@@ -90,6 +97,15 @@ public function withAutoSync(bool $enabled = true): self
9097
return $this;
9198
}
9299

100+
/**
101+
* @param string[] $statuses
102+
*/
103+
public function withActiveStatuses(array $statuses = [Element::STATUS_ENABLED, Entry::STATUS_LIVE]): self
104+
{
105+
$this->activeStatuses = $statuses;
106+
return $this;
107+
}
108+
93109
/**
94110
* Set the callable that is used by the plugin when reporting progress on synchronization tasks.
95111
*
@@ -154,6 +170,7 @@ public function build(): array
154170
'pageSize' => $this->pageSize,
155171
'query' => $this->query,
156172
'autoSync' => $this->autoSync,
173+
'activeStatuses' => $this->activeStatuses,
157174
'pages' => $this->pagesFn,
158175
'fetch' => $this->fetchFn,
159176
];

src/models/Index.php

+9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace fostercommerce\meilisearch\models;
44

5+
use craft\base\Element;
56
use craft\base\Model;
67
use craft\elements\db\ElementQuery;
78
use craft\elements\db\ElementQueryInterface;
9+
use craft\elements\Entry;
810
use Generator;
911

1012
/**
@@ -48,6 +50,13 @@ class Index extends Model
4850
*/
4951
public bool $autoSync = true;
5052

53+
/**
54+
* Statuses to use during auto-sync to check whether an element is active or not.
55+
*
56+
* @var string[]
57+
*/
58+
public array $activeStatuses = [Element::STATUS_ENABLED, Entry::STATUS_LIVE];
59+
5160
/**
5261
* An optional callable that is used by the plugin when reporting progress on synchronization tasks.
5362
*

0 commit comments

Comments
 (0)