Completed
Push — master ( 829da2...8585d5 )
by David
03:05
created

Wordlift_Import_Service::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 5
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Define a service class which extends WP's features to support import of linked
5
 * data content.
6
 */
7
8
/**
9
 * Define the Wordlift_Import_Service class.
10
 *
11
 * @since 3.6.0
12
 */
13
class Wordlift_Import_Service {
14
15
	/**
16
	 * A {@link Wordlift_Log_Service} instance.
17
	 *
18
	 * @since 3.6.0
19
	 * @access private
20
	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
21
	 */
22
	private $log;
23
24
	/**
25
	 * A {@link Wordlift_Entity_Type_Service} instance.
26
	 *
27
	 * @since 3.6.0
28
	 * @access private
29
	 * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
30
	 */
31
	private $entity_type_service;
32
33
	/**
34
	 * The entity service instance.
35
	 *
36
	 * @since 3.6.0
37
	 * @access private
38
	 * @var \Wordlift_Entity_Service $entity_service The entity service instance.
39
	 */
40
	private $entity_service;
41
42
	/**
43
	 * The schema service instance.
44
	 *
45
	 * @since 3.6.0
46
	 * @access private
47
	 * @var \Wordlift_Schema_Service $schema_service The schema service instance.
48
	 */
49
	private $schema_service;
50
51
	private $sparql_service;
52
53
	/**
54
	 * The dataset URI for this WordPress web site.
55
	 *
56
	 * @since 3.6.0
57
	 * @access private
58
	 * @var string $dataset_uri The dataset URI for this WordPress web site.
59
	 */
60
	private $dataset_uri;
61
62
	/**
63
	 * Create a Wordlift_Import_Service instance.
64
	 *
65
	 * @since 3.6.0
66
	 *
67
	 * @param \Wordlift_Entity_Type_Service $entity_type_service
68
	 * @param \Wordlift_Entity_Service $entity_service
69
	 * @param \Wordlift_Schema_Service $schema_service
70
	 * @param \Wordlift_Sparql_Service $sparql_service
71
	 * @param string $dataset_uri
72
	 */
73
	public function __construct( $entity_type_service, $entity_service, $schema_service, $sparql_service, $dataset_uri ) {
74
75
		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Import_Service' );
76
77
		$this->entity_type_service = $entity_type_service;
78
		$this->entity_service      = $entity_service;
79
		$this->schema_service      = $schema_service;
80
		$this->sparql_service      = $sparql_service;
81
		$this->dataset_uri         = $dataset_uri;
82
83
	}
84
85
	/**
86
	 * Handle the `wp_import_post_meta` filter by checking the `entity_url` meta.
87
	 * If the `entity_url` meta value is not within the WP web site dataset URI,
88
	 * it is changed into an `entity_same_as` meta key.
89
	 *
90
	 * @since 3.6.0
91
	 *
92
	 * @param array $postmeta An array of indexed post meta.
93
	 * @param int $post_id The post ID being imported.
94
	 * @param array $post An array of post properties.
95
	 *
96
	 * @return array An array of indexed post meta.
97
	 */
98
	public function wp_import_post_meta( $postmeta, $post_id, $post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
100
		// If we're not dealing with entity posts, return the original post meta.
101
		if ( $post['post_type'] !== $this->entity_type_service->get_post_type() ) {
102
			return $postmeta;
103
		}
104
105
		// Get a reference to the entity URL meta.
106
		$entity_url_meta = NULL;
107
108
		foreach ( $postmeta as &$meta ) {
109
			if ( 'entity_url' === $meta['key'] ) {
110
				$entity_url_meta = &$meta;
111
				break;
112
			}
113
		}
114
115
		// If the entity URI is within the dataset URI, we don't change anything.
116
		if ( NULL === $entity_url_meta || 0 === strpos( $entity_url_meta['value'], $this->dataset_uri ) ) {
117
			return $postmeta;
118
		}
119
120
		// Since the entity URL doesn't belong to this WP install, as the dataset
121
		// URI doesn't match the start of the entity URL, we turn the entity URL
122
		// meta to an entity sameAs. $entity_url_meta is a reference so it should
123
		// update the item in the postmeta array directly.
124
		$entity_url_meta['key'] = 'entity_same_as';
125
126
		return $postmeta;
127
	}
128
129
	/**
130
	 * Catch post meta updates.
131
	 *
132
	 * @since 3.6.0
133
	 *
134
	 * @param int $mid The meta ID after successful update.
135
	 * @param int $object_id Object ID.
136
	 * @param string $meta_key Meta key.
137
	 * @param mixed $meta_value Meta value.
138
	 */
139
	public function added_post_meta( $mid, $object_id, $meta_key, $meta_value ) {
140
141
		// Get the entity URI.
142
		$s = $this->entity_service->get_uri( $object_id );
143
144
		// Get the field with the specified meta key. Return if the field is not defined.
145
		if ( NULL === ( $field = $this->schema_service->get_field( $meta_key ) ) ) {
146
			return;
147
		}
148
149
		// Get the field predicate.
150
		$p = $field['predicate'];
151
152
		// Format the object value according to the field type.
153
		$o = $this->sparql_service->format( $meta_value, $field['type'] );
154
155
		// Create the statement.
156
		$stmt = sprintf( 'INSERT DATA { <%s> <%s> %s };', Wordlift_Sparql_Service::escape_uri( $s ), Wordlift_Sparql_Service::escape_uri( $p ), $o );
157
158
		// Finally queue the statement.
159
		$this->sparql_service->queue( $stmt );
160
161
	}
162
163
	/**
164
	 * When an import is running, hook the {@link added_post_meta} function in order
165
	 * to insert metadata from an import in Linked Data.
166
	 *
167
	 * @since 3.6.0
168
	 */
169
	public function import_start() {
170
171
		add_action( 'added_post_meta', array(
172
			$this,
173
			'added_post_meta'
174
		), 10, 4 );
175
176
	}
177
178
	/**
179
	 * When an import ends, remove the hook previously added by {@link import_start}.
180
	 *
181
	 * @since 3.6.0
182
	 */
183
	public function import_end() {
184
185
		remove_action( 'added_post_meta', array(
186
			$this,
187
			'added_post_meta'
188
		), 10 );
189
190
	}
191
192
}
193