-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathImportCoordinates.php
More file actions
121 lines (102 loc) · 2.45 KB
/
Copy pathImportCoordinates.php
File metadata and controls
121 lines (102 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace lloc\Msls\ContentImport;
use lloc\Msls\MslsBlogCollection;
class ImportCoordinates {
const IMPORTERS_GLOBAL_KEY = 'msls_importers';
/**
* @var int
*/
public $source_blog_id;
/**
* @var int
*/
public $source_post_id;
/**
* @var int
*/
public $dest_blog_id;
/**
* @var int
*/
public $dest_post_id;
/**
* @var ?\WP_Post
*/
public $source_post;
/**
* @var string
*/
public $source_lang;
/**
* @var string
*/
public $dest_lang;
/**
* @var array<string, string> An array keeping track of which importer (slug) should be used for
* a specific import type, shape [ <import-type> => <slug> ]
*/
public $importers = array();
/**
* Validates the coordinates.
*
* @return bool
*/
public function validate() {
if ( ! get_blog_post( $this->source_blog_id, $this->source_post_id ) ) {
return false;
}
if ( ! get_blog_post( $this->dest_blog_id, $this->dest_post_id ) ) {
return false;
}
if ( ! $this->source_post instanceof \WP_Post ) {
return false;
}
if ( MslsBlogCollection::get_blog_language( $this->source_blog_id ) !== $this->source_lang ) {
return false;
}
if ( MslsBlogCollection::get_blog_language( $this->dest_blog_id ) !== $this->dest_lang ) {
return false;
}
return true;
}
/**
* Returns the importer (slug) for a specific type of imports.
*
* @param string $importer_type
*
* @return string|null The import slug if set or `null` if not set.
*/
public function get_importer_for( $importer_type ) {
return $this->importers[ $importer_type ] ?? null;
}
/**
* Parses the importers from request superglobals.
*/
public function parse_importers_from_request(): void {
$importers = array();
foreach ( array( INPUT_POST, INPUT_GET ) as $input_type ) {
if ( filter_has_var( $input_type, self::IMPORTERS_GLOBAL_KEY ) ) {
$importers = filter_input( $input_type, self::IMPORTERS_GLOBAL_KEY, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
break;
}
}
if ( ! is_array( $importers ) ) {
return;
}
foreach ( $importers as $importer_type => $slug ) {
if ( ! is_string( $slug ) ) {
continue;
}
$this->set_importer_for( $importer_type, $slug );
}
}
/**
* Sets the slug of the importer that should be used for a type of import.
*
* @param string $importer_type
* @param string $slug
*/
public function set_importer_for( $importer_type, $slug ): void {
$this->importers[ $importer_type ] = $slug;
}
}