Skip to content

Commit a7ab52b

Browse files
authored
Merge pull request #62 from BenjaminMedia/FORD-48-import-composites-by-source
Added source argument to composites import CDM
2 parents 69f2d46 + 28cdb99 commit a7ab52b

File tree

4 files changed

+73
-9
lines changed

4 files changed

+73
-9
lines changed

src/Commands/BaseCmd.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@
1313
class BaseCmd extends WP_CLI_Command
1414
{
1515
protected function map_sites($callable) {
16-
collect(Plugin::instance()->settings->get_languages())->pluck('locale')->map(function($locale) use($callable){
16+
$this->get_sites()->each($callable);
17+
}
18+
19+
protected function get_sites() {
20+
return collect(Plugin::instance()->settings->get_languages())->pluck('locale')->map(function($locale){
1721
return Plugin::instance()->settings->get_site($locale);
18-
})->rejectNullValues()->each($callable);
22+
})->rejectNullValues();
23+
}
24+
25+
protected function get_site() {
26+
return $this->get_sites()->first();
1927
}
2028
}

src/Commands/Composites.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Bonnier\WP\ContentHub\Editor\Commands;
44

5-
use Bonnier\WP\Cache\Models\Post;
5+
use Bonnier\WP\Cache\Models\Post as BonnierCachePost;
66
use Bonnier\WP\ContentHub\Editor\Commands\Taxonomy\Helpers\WpTerm;
77
use Bonnier\WP\ContentHub\Editor\Helpers\SlugHelper;
88
use Bonnier\WP\ContentHub\Editor\Models\WpAttachment;
99
use Bonnier\WP\ContentHub\Editor\Models\WpComposite;
1010
use Bonnier\WP\ContentHub\Editor\Models\WpTaxonomy;
1111
use Bonnier\WP\ContentHub\Editor\Repositories\Scaphold\CompositeRepository;
12+
use Bonnier\WP\Cxense\Models\Post as CxensePost;
1213
use Illuminate\Support\Collection;
1314
use WP_CLI;
1415
use WP_Post;
@@ -36,6 +37,9 @@ public static function register()
3637
* [--id=<id>]
3738
* : The id of a single composite to import.
3839
*
40+
* [--source=<source_code>]
41+
* : The the source code to fetch content from
42+
*
3943
* ## EXAMPLES
4044
* wp contenthub editor composites import
4145
*
@@ -49,19 +53,24 @@ public function import($args, $assocArgs)
4953
return [];
5054
});
5155

52-
// Disable on save hook to prevent call to content hub on import
56+
// Disable on save hook to prevent call to content hub, Cxense and Bonnier Cache Manager
5357
remove_action( 'save_post', [WpComposite::class, 'on_save'], 10, 2 );
54-
// Disable on save hook in wp bonnier cache, to avoid triggering cxense crawl on import
55-
remove_action( 'save_post', [Post::class, 'update_post'], 10, 1 );
56-
58+
remove_action( 'save_post', [BonnierCachePost::class, 'update_post'], 10, 1 );
59+
remove_action( 'save_post', [CxensePost::class, 'update_post'], 10, 1 );
60+
5761
if($id = $assocArgs['id'] ?? null) {
5862
$this->import_composite(CompositeRepository::find_by_id($id));
5963
return;
6064
}
61-
$this->map_sites(function ($site) {
62-
$this->map_composites_by_brand_id($site->brand->content_hub_id, function ($composite) {
65+
$brandId = $this->get_site()->brand->content_hub_id;
66+
if($source = $assocArgs['source'] ?? null) {
67+
$this->map_composites_by_brand_id_and_source($brandId, $source, function ($composite) {
6368
$this->import_composite($composite);
6469
});
70+
return;
71+
}
72+
$this->map_composites_by_brand_id($brandId, function ($composite) {
73+
$this->import_composite($composite);
6574
});
6675
}
6776

@@ -120,6 +129,22 @@ private function map_composites_by_brand_id($id, callable $callable)
120129
}
121130
}
122131

132+
private function map_composites_by_brand_id_and_source($id, $source, callable $callable)
133+
{
134+
$compositeQuery = CompositeRepository::find_by_brand_id_and_source($id, $source);
135+
136+
while ($compositeQuery) {
137+
$categories = collect($compositeQuery->edges);
138+
$categories->pluck('node')->each(function ($compositeInfo) use ($callable) {
139+
$callable(CompositeRepository::find_by_id($compositeInfo->id));
140+
});
141+
if (isset($compositeQuery->pageInfo->hasNextPage) && $compositeQuery->pageInfo->hasNextPage)
142+
$compositeQuery = CompositeRepository::map_composites_by_brand_id_and_source($id, $categories->last()->cursor);
143+
else
144+
$compositeQuery = null;
145+
}
146+
}
147+
123148
private function import_composite($composite)
124149
{
125150
WP_CLI::line('Beginning import of: ' . $composite->title . ' id: ' . $composite->id);

src/Repositories/Scaphold/CompositeRepository.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ public static function find_by_brand_id($id, $cursor = '', $limit = 100)
3737
])->allComposites;
3838
}
3939

40+
public static function find_by_brand_id_and_source($id, $source, $cursor = '', $limit = 100)
41+
{
42+
return Client::query(Queries::GET_COMPOSITES_BY_BRAND_AND_SOURCE, [
43+
'source' => $source,
44+
'brandId' => $id,
45+
'cursor' => $cursor,
46+
'limit' => $limit
47+
])->allComposites;
48+
}
49+
4050
public static function create($input)
4151
{
4252
return Client::query(Queries::CREATE_COMPOSITE, [

src/Scaphold/Queries.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,27 @@ class Queries
5151
}
5252
';
5353

54+
const GET_COMPOSITES_BY_BRAND_AND_SOURCE = '
55+
query GetComposites($brandId: ID!, $source: String!, $cursor: String!, $limit: Int!) {
56+
viewer {
57+
allComposites(where: {brand: {id: {eq: $brandId}}, source: {code: {eq: $source}}}, first: $limit, after: $cursor) {
58+
aggregations {
59+
count
60+
}
61+
pageInfo {
62+
hasNextPage
63+
}
64+
edges {
65+
cursor
66+
node {
67+
id
68+
}
69+
}
70+
}
71+
}
72+
}
73+
';
74+
5475
const GET_COMPOSITE = '
5576
query GetComposite($id: ID!) {
5677
getComposite(id: $id) {

0 commit comments

Comments
 (0)