1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace lloc\Msls\ContentImport; |
4
|
|
|
|
5
|
|
|
use lloc\Msls\MslsOptions; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Relations |
9
|
|
|
* |
10
|
|
|
* Manages and tracks the relations between elements managed by MSLS that are created in the context of an import. |
11
|
|
|
* |
12
|
|
|
* @package lloc\Msls\ContentImport |
13
|
|
|
*/ |
14
|
|
|
class Relations { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var MslsOptions[] |
18
|
|
|
*/ |
19
|
|
|
public $to_create = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var MslsOptions[] |
23
|
|
|
*/ |
24
|
|
|
protected $local_options = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ImportCoordinates |
28
|
|
|
*/ |
29
|
|
|
protected $import_coordinates; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Relations constructor. |
33
|
|
|
* |
34
|
|
|
* @param ImportCoordinates $import_coordinates |
35
|
|
|
*/ |
36
|
|
|
public function __construct( ImportCoordinates $import_coordinates ) { |
37
|
|
|
$this->import_coordinates = $import_coordinates; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Merges the data from a Relations object into this one. |
42
|
|
|
* |
43
|
|
|
* @param Relations|null $relations |
44
|
|
|
*/ |
45
|
|
|
public function merge( Relations $relations = null ) { |
46
|
|
|
if ( null === $relations ) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->to_create = array_merge_recursive( $this->to_create, $relations->get_data() ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public function get_data() { |
57
|
|
|
return $this->to_create; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Creates the relations between the source blog elements and the destination one. |
62
|
|
|
*/ |
63
|
|
|
public function create() { |
64
|
|
|
$this->create_source_to_local(); |
65
|
|
|
$this->create_local_to_source(); |
66
|
|
|
restore_current_blog(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function create_source_to_local() { |
70
|
|
|
switch_to_blog( $this->import_coordinates->source_blog_id ); |
|
|
|
|
71
|
|
|
|
72
|
|
|
foreach ( $this->to_create as $relation ) { |
73
|
|
|
/** @var MslsOptions $option */ |
74
|
|
|
list( $option, $lang, $id ) = $relation; |
75
|
|
|
$option->save( [ $lang => $id ] ); |
76
|
|
|
$source_id = $option->get_arg( 0, $id ); |
77
|
|
|
|
78
|
|
|
$this->local_options[ $source_id ] = [ $option, $id ]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function create_local_to_source() { |
83
|
|
|
switch_to_blog( $this->import_coordinates->dest_blog_id ); |
|
|
|
|
84
|
|
|
|
85
|
|
|
/** @var MslsOptions $local_option */ |
86
|
|
|
foreach ( $this->local_options as $source_id => $option_data ) { |
87
|
|
|
list( $source_option, $local_id ) = $option_data; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Allows plugins to filter the local to source relation creation and override the class creation method completely. |
91
|
|
|
* |
92
|
|
|
* @param mixed $created If not `null` then the class will not create the local to source relation. |
93
|
|
|
* @param int $local_id |
94
|
|
|
* @param int $source_id |
95
|
|
|
* @param MslsOptions $source_option |
96
|
|
|
*/ |
97
|
|
|
$created = apply_filters( 'msls_content_import_relation_local_to_source_create', null, $local_id, $source_id, $source_option ); |
98
|
|
|
if ( null !== $created ) { |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$option_class = get_class( $source_option ); |
103
|
|
|
$local_option = call_user_func( [ $option_class, 'create' ], $local_id ); |
104
|
|
|
$local_option->save( [ $this->import_coordinates->source_lang => $source_id ] ); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Sets a relation that should be created. |
110
|
|
|
* |
111
|
|
|
* @param MslsOptions $creator |
112
|
|
|
* @param string $dest_lang |
113
|
|
|
* @param string $dest_post_id |
114
|
|
|
*/ |
115
|
|
|
public function should_create( MslsOptions $creator, $dest_lang, $dest_post_id ) { |
116
|
|
|
$this->to_create[] = [ $creator, $dest_lang, $dest_post_id ]; |
117
|
|
|
} |
118
|
|
|
} |