Completed
Push — develop ( 4baabc...89b677 )
by David
03:15 queued 11s
created
src/wordlift_entity_functions.php 2 patches
Indentation   +106 added lines, -106 removed lines patch added patch discarded remove patch
@@ -12,35 +12,35 @@  discard block
 block discarded – undo
12 12
  */
13 13
 function wl_get_entity_post_ids_by_uris( $uris ) {
14 14
 
15
-	if ( empty( $uris ) ) {
16
-		return array();
17
-	}
18
-
19
-	$query = new WP_Query( array(
20
-			'fields'      => 'ids',
21
-			'post_status' => 'any',
22
-			'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
23
-			'meta_query'  => array(
24
-				'relation' => 'OR',
25
-				array(
26
-					'key'     => Wordlift_Schema_Service::FIELD_SAME_AS,
27
-					'value'   => $uris,
28
-					'compare' => 'IN',
29
-				),
30
-				array(
31
-					'key'     => 'entity_url',
32
-					'value'   => $uris,
33
-					'compare' => 'IN',
34
-				),
35
-			),
36
-		)
37
-	);
38
-
39
-	// Get the matching entity posts.
40
-	$posts = $query->get_posts();
41
-
42
-	// Return the array
43
-	return $posts;
15
+    if ( empty( $uris ) ) {
16
+        return array();
17
+    }
18
+
19
+    $query = new WP_Query( array(
20
+            'fields'      => 'ids',
21
+            'post_status' => 'any',
22
+            'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
23
+            'meta_query'  => array(
24
+                'relation' => 'OR',
25
+                array(
26
+                    'key'     => Wordlift_Schema_Service::FIELD_SAME_AS,
27
+                    'value'   => $uris,
28
+                    'compare' => 'IN',
29
+                ),
30
+                array(
31
+                    'key'     => 'entity_url',
32
+                    'value'   => $uris,
33
+                    'compare' => 'IN',
34
+                ),
35
+            ),
36
+        )
37
+    );
38
+
39
+    // Get the matching entity posts.
40
+    $posts = $query->get_posts();
41
+
42
+    // Return the array
43
+    return $posts;
44 44
 }
45 45
 
46 46
 /**
@@ -55,29 +55,29 @@  discard block
 block discarded – undo
55 55
  */
56 56
 function wl_build_entity_uri( $post_id ) {
57 57
 
58
-	// Get the post.
59
-	$post = get_post( $post_id );
58
+    // Get the post.
59
+    $post = get_post( $post_id );
60 60
 
61
-	if ( null === $post ) {
62
-		wl_write_log( "wl_build_entity_uri : error [ post ID :: $post_id ][ post :: null ]" );
61
+    if ( null === $post ) {
62
+        wl_write_log( "wl_build_entity_uri : error [ post ID :: $post_id ][ post :: null ]" );
63 63
 
64
-		return null;
65
-	}
64
+        return null;
65
+    }
66 66
 
67
-	// Create an ID given the title.
68
-	$entity_slug = wl_sanitize_uri_path( $post->post_title );
69
-	// If the entity slug is empty, i.e. there's no title, use the post ID as path.
70
-	if ( empty( $entity_slug ) ) {
71
-		return sprintf( '%s/%s/%s',
72
-			wl_configuration_get_redlink_dataset_uri(),
73
-			$post->post_type,
74
-			"id/$post->ID"
75
-		);
76
-	}
67
+    // Create an ID given the title.
68
+    $entity_slug = wl_sanitize_uri_path( $post->post_title );
69
+    // If the entity slug is empty, i.e. there's no title, use the post ID as path.
70
+    if ( empty( $entity_slug ) ) {
71
+        return sprintf( '%s/%s/%s',
72
+            wl_configuration_get_redlink_dataset_uri(),
73
+            $post->post_type,
74
+            "id/$post->ID"
75
+        );
76
+    }
77 77
 
78
-	return Wordlift_Uri_Service::get_instance()->build_uri(
79
-		$entity_slug,
80
-		$post->post_type );
78
+    return Wordlift_Uri_Service::get_instance()->build_uri(
79
+        $entity_slug,
80
+        $post->post_type );
81 81
 
82 82
 }
83 83
 
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
  */
96 96
 function wl_get_entity_uri( $post_id ) {
97 97
 
98
-	return Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
98
+    return Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
99 99
 }
100 100
 
101 101
 /**
@@ -108,9 +108,9 @@  discard block
 block discarded – undo
108 108
  */
109 109
 function wl_set_entity_uri( $post_id, $uri ) {
110 110
 
111
-	// wl_write_log( "wl_set_entity_uri [ post id :: $post_id ][ uri :: $uri ]" );
111
+    // wl_write_log( "wl_set_entity_uri [ post id :: $post_id ][ uri :: $uri ]" );
112 112
 
113
-	return update_post_meta( $post_id, WL_ENTITY_URL_META_NAME, $uri );
113
+    return update_post_meta( $post_id, WL_ENTITY_URL_META_NAME, $uri );
114 114
 }
115 115
 
116 116
 
@@ -125,7 +125,7 @@  discard block
 block discarded – undo
125 125
  */
126 126
 function wl_get_entity_rdf_types( $post_id ) {
127 127
 
128
-	return get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE );
128
+    return get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE );
129 129
 }
130 130
 
131 131
 /**
@@ -136,21 +136,21 @@  discard block
 block discarded – undo
136 136
  */
137 137
 function wl_set_entity_rdf_types( $post_id, $type_uris = array() ) {
138 138
 
139
-	// Avoid errors because of null values.
140
-	if ( is_null( $type_uris ) ) {
141
-		$type_uris = array();
142
-	}
143
-
144
-	// Ensure there are no duplicates.
145
-	$type_uris = array_unique( $type_uris );
146
-
147
-	delete_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE );
148
-	foreach ( $type_uris as $type_uri ) {
149
-		if ( empty( $type_uri ) ) {
150
-			continue;
151
-		}
152
-		add_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE, $type_uri );
153
-	}
139
+    // Avoid errors because of null values.
140
+    if ( is_null( $type_uris ) ) {
141
+        $type_uris = array();
142
+    }
143
+
144
+    // Ensure there are no duplicates.
145
+    $type_uris = array_unique( $type_uris );
146
+
147
+    delete_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE );
148
+    foreach ( $type_uris as $type_uri ) {
149
+        if ( empty( $type_uri ) ) {
150
+            continue;
151
+        }
152
+        add_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE, $type_uri );
153
+    }
154 154
 }
155 155
 
156 156
 ///**
@@ -199,59 +199,59 @@  discard block
 block discarded – undo
199 199
  */
200 200
 function wl_entity_taxonomy_get_custom_fields( $entity_id = null ) {
201 201
 
202
-	if ( is_null( $entity_id ) ) {
202
+    if ( is_null( $entity_id ) ) {
203 203
 
204
-		// Return all custom fields.
205
-		// Get taxonomy terms
206
-		$terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'get' => 'all' ) );
204
+        // Return all custom fields.
205
+        // Get taxonomy terms
206
+        $terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'get' => 'all' ) );
207 207
 
208
-		if ( is_wp_error( $terms ) ) {
209
-			return null;
210
-		}
208
+        if ( is_wp_error( $terms ) ) {
209
+            return null;
210
+        }
211 211
 
212
-		$custom_fields = array();
213
-		foreach ( $terms as $term ) {
214
-			// Get custom_fields
215
-			$term_options = Wordlift_Schema_Service::get_instance()
216
-			                                       ->get_schema( $term->slug );
212
+        $custom_fields = array();
213
+        foreach ( $terms as $term ) {
214
+            // Get custom_fields
215
+            $term_options = Wordlift_Schema_Service::get_instance()
216
+                                                    ->get_schema( $term->slug );
217 217
 
218
-			if ( ! isset( $term_options['uri'] ) || ! isset( $term_options['custom_fields'] ) ) {
219
-				continue;
220
-			}
218
+            if ( ! isset( $term_options['uri'] ) || ! isset( $term_options['custom_fields'] ) ) {
219
+                continue;
220
+            }
221 221
 
222
-			$custom_fields[ $term_options['uri'] ] = $term_options['custom_fields'];
223
-		}
222
+            $custom_fields[ $term_options['uri'] ] = $term_options['custom_fields'];
223
+        }
224 224
 
225
-		return $custom_fields;
225
+        return $custom_fields;
226 226
 
227
-	}
227
+    }
228 228
 
229
-	// Return custom fields for this specific entity's type.
230
-	$types = Wordlift_Entity_Type_Service::get_instance()->get_ids( $entity_id );
229
+    // Return custom fields for this specific entity's type.
230
+    $types = Wordlift_Entity_Type_Service::get_instance()->get_ids( $entity_id );
231 231
 
232
-	/** @var WP_Term[] $terms */
233
-	$terms = array_filter( array_map( function ( $item ) {
234
-		return get_term( $item );
235
-	}, $types ), function ( $item ) {
236
-		return isset( $item ) && is_a( $item, 'WP_Term' );
237
-	} );
232
+    /** @var WP_Term[] $terms */
233
+    $terms = array_filter( array_map( function ( $item ) {
234
+        return get_term( $item );
235
+    }, $types ), function ( $item ) {
236
+        return isset( $item ) && is_a( $item, 'WP_Term' );
237
+    } );
238 238
 
239
-	$term_slugs = array_map( function ( $item ) {
240
-		return $item->slug;
241
-	}, $terms );
239
+    $term_slugs = array_map( function ( $item ) {
240
+        return $item->slug;
241
+    }, $terms );
242 242
 
243
-	$term_slugs[] = 'thing';
243
+    $term_slugs[] = 'thing';
244 244
 
245
-	$schema_service = Wordlift_Schema_Service::get_instance();
245
+    $schema_service = Wordlift_Schema_Service::get_instance();
246 246
 
247
-	return array_reduce( $term_slugs, function ( $carry, $item ) use ( $schema_service ) {
247
+    return array_reduce( $term_slugs, function ( $carry, $item ) use ( $schema_service ) {
248 248
 
249
-		$schema = $schema_service->get_schema( $item );
249
+        $schema = $schema_service->get_schema( $item );
250 250
 
251
-		if ( ! isset( $schema['custom_fields'] ) ) {
252
-			return $carry;
253
-		}
251
+        if ( ! isset( $schema['custom_fields'] ) ) {
252
+            return $carry;
253
+        }
254 254
 
255
-		return $carry + $schema['custom_fields'];
256
-	}, array() );
255
+        return $carry + $schema['custom_fields'];
256
+    }, array() );
257 257
 }
Please login to merge, or discard this patch.
Spacing   +42 added lines, -42 removed lines patch added patch discarded remove patch
@@ -10,13 +10,13 @@  discard block
 block discarded – undo
10 10
  *
11 11
  * @return array A WP_Post instance or null if not found.
12 12
  */
13
-function wl_get_entity_post_ids_by_uris( $uris ) {
13
+function wl_get_entity_post_ids_by_uris($uris) {
14 14
 
15
-	if ( empty( $uris ) ) {
15
+	if (empty($uris)) {
16 16
 		return array();
17 17
 	}
18 18
 
19
-	$query = new WP_Query( array(
19
+	$query = new WP_Query(array(
20 20
 			'fields'      => 'ids',
21 21
 			'post_status' => 'any',
22 22
 			'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
@@ -53,22 +53,22 @@  discard block
 block discarded – undo
53 53
  * @uses wl_configuration_get_redlink_dataset_uri() to get the dataset base URI.
54 54
  *
55 55
  */
56
-function wl_build_entity_uri( $post_id ) {
56
+function wl_build_entity_uri($post_id) {
57 57
 
58 58
 	// Get the post.
59
-	$post = get_post( $post_id );
59
+	$post = get_post($post_id);
60 60
 
61
-	if ( null === $post ) {
62
-		wl_write_log( "wl_build_entity_uri : error [ post ID :: $post_id ][ post :: null ]" );
61
+	if (null === $post) {
62
+		wl_write_log("wl_build_entity_uri : error [ post ID :: $post_id ][ post :: null ]");
63 63
 
64 64
 		return null;
65 65
 	}
66 66
 
67 67
 	// Create an ID given the title.
68
-	$entity_slug = wl_sanitize_uri_path( $post->post_title );
68
+	$entity_slug = wl_sanitize_uri_path($post->post_title);
69 69
 	// If the entity slug is empty, i.e. there's no title, use the post ID as path.
70
-	if ( empty( $entity_slug ) ) {
71
-		return sprintf( '%s/%s/%s',
70
+	if (empty($entity_slug)) {
71
+		return sprintf('%s/%s/%s',
72 72
 			wl_configuration_get_redlink_dataset_uri(),
73 73
 			$post->post_type,
74 74
 			"id/$post->ID"
@@ -93,9 +93,9 @@  discard block
 block discarded – undo
93 93
  *
94 94
  * @uses       wl_build_entity_uri() to create a new URI if the entity doesn't have an URI yet.
95 95
  */
96
-function wl_get_entity_uri( $post_id ) {
96
+function wl_get_entity_uri($post_id) {
97 97
 
98
-	return Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
98
+	return Wordlift_Entity_Service::get_instance()->get_uri($post_id);
99 99
 }
100 100
 
101 101
 /**
@@ -106,11 +106,11 @@  discard block
 block discarded – undo
106 106
  *
107 107
  * @return bool True if successful, otherwise false.
108 108
  */
109
-function wl_set_entity_uri( $post_id, $uri ) {
109
+function wl_set_entity_uri($post_id, $uri) {
110 110
 
111 111
 	// wl_write_log( "wl_set_entity_uri [ post id :: $post_id ][ uri :: $uri ]" );
112 112
 
113
-	return update_post_meta( $post_id, WL_ENTITY_URL_META_NAME, $uri );
113
+	return update_post_meta($post_id, WL_ENTITY_URL_META_NAME, $uri);
114 114
 }
115 115
 
116 116
 
@@ -123,9 +123,9 @@  discard block
 block discarded – undo
123 123
  * @since 3.0.0
124 124
  *
125 125
  */
126
-function wl_get_entity_rdf_types( $post_id ) {
126
+function wl_get_entity_rdf_types($post_id) {
127 127
 
128
-	return get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE );
128
+	return get_post_meta($post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE);
129 129
 }
130 130
 
131 131
 /**
@@ -134,22 +134,22 @@  discard block
 block discarded – undo
134 134
  * @param int $post_id The entity post ID.
135 135
  * @param array $type_uris An array of type URIs.
136 136
  */
137
-function wl_set_entity_rdf_types( $post_id, $type_uris = array() ) {
137
+function wl_set_entity_rdf_types($post_id, $type_uris = array()) {
138 138
 
139 139
 	// Avoid errors because of null values.
140
-	if ( is_null( $type_uris ) ) {
140
+	if (is_null($type_uris)) {
141 141
 		$type_uris = array();
142 142
 	}
143 143
 
144 144
 	// Ensure there are no duplicates.
145
-	$type_uris = array_unique( $type_uris );
145
+	$type_uris = array_unique($type_uris);
146 146
 
147
-	delete_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE );
148
-	foreach ( $type_uris as $type_uri ) {
149
-		if ( empty( $type_uri ) ) {
147
+	delete_post_meta($post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE);
148
+	foreach ($type_uris as $type_uri) {
149
+		if (empty($type_uri)) {
150 150
 			continue;
151 151
 		}
152
-		add_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE, $type_uri );
152
+		add_post_meta($post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE, $type_uri);
153 153
 	}
154 154
 }
155 155
 
@@ -197,29 +197,29 @@  discard block
 block discarded – undo
197 197
  *
198 198
  * @return array|null if $entity_id was specified, return custom_fields for that entity's type. Otherwise returns all custom_fields
199 199
  */
200
-function wl_entity_taxonomy_get_custom_fields( $entity_id = null ) {
200
+function wl_entity_taxonomy_get_custom_fields($entity_id = null) {
201 201
 
202
-	if ( is_null( $entity_id ) ) {
202
+	if (is_null($entity_id)) {
203 203
 
204 204
 		// Return all custom fields.
205 205
 		// Get taxonomy terms
206
-		$terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'get' => 'all' ) );
206
+		$terms = get_terms(Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array('get' => 'all'));
207 207
 
208
-		if ( is_wp_error( $terms ) ) {
208
+		if (is_wp_error($terms)) {
209 209
 			return null;
210 210
 		}
211 211
 
212 212
 		$custom_fields = array();
213
-		foreach ( $terms as $term ) {
213
+		foreach ($terms as $term) {
214 214
 			// Get custom_fields
215 215
 			$term_options = Wordlift_Schema_Service::get_instance()
216
-			                                       ->get_schema( $term->slug );
216
+			                                       ->get_schema($term->slug);
217 217
 
218
-			if ( ! isset( $term_options['uri'] ) || ! isset( $term_options['custom_fields'] ) ) {
218
+			if ( ! isset($term_options['uri']) || ! isset($term_options['custom_fields'])) {
219 219
 				continue;
220 220
 			}
221 221
 
222
-			$custom_fields[ $term_options['uri'] ] = $term_options['custom_fields'];
222
+			$custom_fields[$term_options['uri']] = $term_options['custom_fields'];
223 223
 		}
224 224
 
225 225
 		return $custom_fields;
@@ -227,31 +227,31 @@  discard block
 block discarded – undo
227 227
 	}
228 228
 
229 229
 	// Return custom fields for this specific entity's type.
230
-	$types = Wordlift_Entity_Type_Service::get_instance()->get_ids( $entity_id );
230
+	$types = Wordlift_Entity_Type_Service::get_instance()->get_ids($entity_id);
231 231
 
232 232
 	/** @var WP_Term[] $terms */
233
-	$terms = array_filter( array_map( function ( $item ) {
234
-		return get_term( $item );
235
-	}, $types ), function ( $item ) {
236
-		return isset( $item ) && is_a( $item, 'WP_Term' );
233
+	$terms = array_filter(array_map(function($item) {
234
+		return get_term($item);
235
+	}, $types), function($item) {
236
+		return isset($item) && is_a($item, 'WP_Term');
237 237
 	} );
238 238
 
239
-	$term_slugs = array_map( function ( $item ) {
239
+	$term_slugs = array_map(function($item) {
240 240
 		return $item->slug;
241
-	}, $terms );
241
+	}, $terms);
242 242
 
243 243
 	$term_slugs[] = 'thing';
244 244
 
245 245
 	$schema_service = Wordlift_Schema_Service::get_instance();
246 246
 
247
-	return array_reduce( $term_slugs, function ( $carry, $item ) use ( $schema_service ) {
247
+	return array_reduce($term_slugs, function($carry, $item) use ($schema_service) {
248 248
 
249
-		$schema = $schema_service->get_schema( $item );
249
+		$schema = $schema_service->get_schema($item);
250 250
 
251
-		if ( ! isset( $schema['custom_fields'] ) ) {
251
+		if ( ! isset($schema['custom_fields'])) {
252 252
 			return $carry;
253 253
 		}
254 254
 
255 255
 		return $carry + $schema['custom_fields'];
256
-	}, array() );
256
+	}, array());
257 257
 }
Please login to merge, or discard this patch.
src/admin/WL_Metabox/class-wl-metabox.php 2 patches
Indentation   +298 added lines, -298 removed lines patch added patch discarded remove patch
@@ -27,346 +27,346 @@
 block discarded – undo
27 27
  */
28 28
 class WL_Metabox {
29 29
 
30
-	/**
31
-	 * The metabox custom fields for the current {@link WP_Post}.
32
-	 *
33
-	 * @since  3.1.0
34
-	 * @access public
35
-	 * @var array $fields The metabox custom fields.
36
-	 */
37
-	public $fields;
38
-
39
-	/**
40
-	 * A {@link Wordlift_Log_Service} instance.
41
-	 *
42
-	 * @since 3.15.4
43
-	 *
44
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
45
-	 */
46
-	private $log;
47
-
48
-	/**
49
-	 * WL_Metabox constructor.
50
-	 *
51
-	 * @since 3.1.0
52
-	 */
53
-	public function __construct() {
54
-
55
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
56
-
57
-		// Add hooks to print metaboxes and save submitted data.
58
-		add_action( 'add_meta_boxes', array( $this, 'add_main_metabox' ) );
59
-		add_action( 'wl_linked_data_save_post', array( $this, 'save_form_data', ) );
60
-
61
-		// Enqueue js and css.
62
-		$this->enqueue_scripts_and_styles();
63
-
64
-	}
65
-
66
-	/**
67
-	 * Add a callback to print the metabox in page.
68
-	 * Wordpress will fire the $this->html() callback at the right time.
69
-	 */
70
-	public function add_main_metabox() {
71
-
72
-		// Build the fields we need to print.
73
-		$this->instantiate_fields( get_the_ID() );
74
-
75
-		// Bailout if there are no actual fields, we do not need a metabox in that case.
76
-		if ( empty( $this->fields ) ) {
77
-			return;
78
-		}
79
-
80
-		// Add main metabox (will print also the inner fields).
81
-		$id    = uniqid( 'wl-metabox-' );
82
-		$title = get_the_title() . ' ' . __( 'properties', 'wordlift' );
83
-
84
-		// WordPress 4.2 do not accept an array of screens as parameter, have to do be explicit.
85
-		foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $screen ) {
86
-			add_meta_box( $id, $title, array(
87
-				$this,
88
-				'html',
89
-			), $screen, 'normal', 'high' );
90
-		}
91
-
92
-		// Add filter to change the metabox CSS class.
93
-		//
94
-		// @since 3.20.0 Since we support post types other than `entity` for entities, we need to set the `screen`
95
-		//  dynamically according to the `get_current_screen()` function.
96
-		$current_screen = get_current_screen();
97
-		$screen         = $current_screen ? $current_screen->post_type : 'entity';
98
-		add_filter( "postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class' );
99
-
100
-	}
101
-
102
-	/**
103
-	 * Called from WP to print the metabox content in page.
104
-	 *
105
-	 * @param WP_Post $post The post.
106
-	 *
107
-	 * @since 3.1.0
108
-	 *
109
-	 */
110
-	public function html( $post ) {
111
-
112
-		// Loop over the fields.
113
-		foreach ( $this->fields as $field ) {
114
-
115
-			// load data from DB (values will be available in $field->data).
116
-			$field->get_data();
117
-
118
-			// print field HTML (nonce included).
119
-			echo $field->html();
120
-		}
121
-
122
-	}
123
-
124
-	/**
125
-	 * Read the WL <-> Schema mapping and build the Fields for the entity being edited.
126
-	 *
127
-	 * Note: the first function that calls this method will instantiate the fields.
128
-	 * Why it isn't called from the constructor? Because we need to hook this process as late as possible.
129
-	 *
130
-	 * @param int $post_id The post id.
131
-	 *
132
-	 * @since 3.1.0
133
-	 *
134
-	 */
135
-	public function instantiate_fields( $post_id ) {
136
-
137
-		$this->log->trace( "Instantiating fields for entity post $post_id..." );
138
-
139
-		// This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering.
140
-		if ( isset( $this->fields ) ) {
141
-			return;
142
-		}
143
-
144
-		$entity_type = wl_entity_taxonomy_get_custom_fields( $post_id );
145
-
146
-		if ( isset( $entity_type ) ) {
147
-
148
-			/*
30
+    /**
31
+     * The metabox custom fields for the current {@link WP_Post}.
32
+     *
33
+     * @since  3.1.0
34
+     * @access public
35
+     * @var array $fields The metabox custom fields.
36
+     */
37
+    public $fields;
38
+
39
+    /**
40
+     * A {@link Wordlift_Log_Service} instance.
41
+     *
42
+     * @since 3.15.4
43
+     *
44
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
45
+     */
46
+    private $log;
47
+
48
+    /**
49
+     * WL_Metabox constructor.
50
+     *
51
+     * @since 3.1.0
52
+     */
53
+    public function __construct() {
54
+
55
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
56
+
57
+        // Add hooks to print metaboxes and save submitted data.
58
+        add_action( 'add_meta_boxes', array( $this, 'add_main_metabox' ) );
59
+        add_action( 'wl_linked_data_save_post', array( $this, 'save_form_data', ) );
60
+
61
+        // Enqueue js and css.
62
+        $this->enqueue_scripts_and_styles();
63
+
64
+    }
65
+
66
+    /**
67
+     * Add a callback to print the metabox in page.
68
+     * Wordpress will fire the $this->html() callback at the right time.
69
+     */
70
+    public function add_main_metabox() {
71
+
72
+        // Build the fields we need to print.
73
+        $this->instantiate_fields( get_the_ID() );
74
+
75
+        // Bailout if there are no actual fields, we do not need a metabox in that case.
76
+        if ( empty( $this->fields ) ) {
77
+            return;
78
+        }
79
+
80
+        // Add main metabox (will print also the inner fields).
81
+        $id    = uniqid( 'wl-metabox-' );
82
+        $title = get_the_title() . ' ' . __( 'properties', 'wordlift' );
83
+
84
+        // WordPress 4.2 do not accept an array of screens as parameter, have to do be explicit.
85
+        foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $screen ) {
86
+            add_meta_box( $id, $title, array(
87
+                $this,
88
+                'html',
89
+            ), $screen, 'normal', 'high' );
90
+        }
91
+
92
+        // Add filter to change the metabox CSS class.
93
+        //
94
+        // @since 3.20.0 Since we support post types other than `entity` for entities, we need to set the `screen`
95
+        //  dynamically according to the `get_current_screen()` function.
96
+        $current_screen = get_current_screen();
97
+        $screen         = $current_screen ? $current_screen->post_type : 'entity';
98
+        add_filter( "postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class' );
99
+
100
+    }
101
+
102
+    /**
103
+     * Called from WP to print the metabox content in page.
104
+     *
105
+     * @param WP_Post $post The post.
106
+     *
107
+     * @since 3.1.0
108
+     *
109
+     */
110
+    public function html( $post ) {
111
+
112
+        // Loop over the fields.
113
+        foreach ( $this->fields as $field ) {
114
+
115
+            // load data from DB (values will be available in $field->data).
116
+            $field->get_data();
117
+
118
+            // print field HTML (nonce included).
119
+            echo $field->html();
120
+        }
121
+
122
+    }
123
+
124
+    /**
125
+     * Read the WL <-> Schema mapping and build the Fields for the entity being edited.
126
+     *
127
+     * Note: the first function that calls this method will instantiate the fields.
128
+     * Why it isn't called from the constructor? Because we need to hook this process as late as possible.
129
+     *
130
+     * @param int $post_id The post id.
131
+     *
132
+     * @since 3.1.0
133
+     *
134
+     */
135
+    public function instantiate_fields( $post_id ) {
136
+
137
+        $this->log->trace( "Instantiating fields for entity post $post_id..." );
138
+
139
+        // This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering.
140
+        if ( isset( $this->fields ) ) {
141
+            return;
142
+        }
143
+
144
+        $entity_type = wl_entity_taxonomy_get_custom_fields( $post_id );
145
+
146
+        if ( isset( $entity_type ) ) {
147
+
148
+            /*
149 149
 			 * Might not have any relevant meta box field, for example for articles,
150 150
 			 * therefor make sure fields are at least an empty array to help the considered
151 151
 			 * in other functions using it.
152 152
 			 */
153
-			$this->fields = array();
153
+            $this->fields = array();
154 154
 
155
-			/**
156
-			 * In some special case, properties must be grouped in one field (e.g. coordinates) or dealed with custom methods.
157
-			 * We must divide fields in two groups:
158
-			 * - simple: accept values for one property
159
-			 * - grouped: accept values for more properties, or for one property that needs a specific metabox.
160
-			 */
161
-			$metaboxes         = $this->group_properties_by_input_field( $entity_type );
162
-			$simple_metaboxes  = $metaboxes[0];
163
-			$grouped_metaboxes = $metaboxes[1];
155
+            /**
156
+             * In some special case, properties must be grouped in one field (e.g. coordinates) or dealed with custom methods.
157
+             * We must divide fields in two groups:
158
+             * - simple: accept values for one property
159
+             * - grouped: accept values for more properties, or for one property that needs a specific metabox.
160
+             */
161
+            $metaboxes         = $this->group_properties_by_input_field( $entity_type );
162
+            $simple_metaboxes  = $metaboxes[0];
163
+            $grouped_metaboxes = $metaboxes[1];
164 164
 
165
-			// Loop over simple entity properties.
166
-			foreach ( $simple_metaboxes as $key => $property ) {
165
+            // Loop over simple entity properties.
166
+            foreach ( $simple_metaboxes as $key => $property ) {
167 167
 
168
-				// Info passed to the metabox.
169
-				$info         = array();
170
-				$info[ $key ] = $property;
168
+                // Info passed to the metabox.
169
+                $info         = array();
170
+                $info[ $key ] = $property;
171 171
 
172
-				// Build the requested field as WL_Metabox_Field_ object.
173
-				$this->add_field( $info );
172
+                // Build the requested field as WL_Metabox_Field_ object.
173
+                $this->add_field( $info );
174 174
 
175
-			}
175
+            }
176 176
 
177
-			// Loop over grouped properties.
178
-			foreach ( $grouped_metaboxes as $key => $property ) {
177
+            // Loop over grouped properties.
178
+            foreach ( $grouped_metaboxes as $key => $property ) {
179 179
 
180
-				// Info passed to the metabox.
181
-				$info         = array();
182
-				$info[ $key ] = $property;
180
+                // Info passed to the metabox.
181
+                $info         = array();
182
+                $info[ $key ] = $property;
183 183
 
184
-				// Build the requested field group as WL_Metabox_Field_ object.
185
-				$this->add_field( $info, true );
184
+                // Build the requested field group as WL_Metabox_Field_ object.
185
+                $this->add_field( $info, true );
186 186
 
187
-			}
188
-		}
187
+            }
188
+        }
189 189
 
190
-	}
190
+    }
191 191
 
192
-	/**
193
-	 * Separates metaboxes in simple and grouped.
194
-	 *
195
-	 * @param array $custom_fields Information on the entity type.
196
-	 *
197
-	 * @return array
198
-	 */
199
-	public function group_properties_by_input_field( $custom_fields ) {
192
+    /**
193
+     * Separates metaboxes in simple and grouped.
194
+     *
195
+     * @param array $custom_fields Information on the entity type.
196
+     *
197
+     * @return array
198
+     */
199
+    public function group_properties_by_input_field( $custom_fields ) {
200 200
 
201
-		$simple_properties  = array();
202
-		$grouped_properties = array();
201
+        $simple_properties  = array();
202
+        $grouped_properties = array();
203 203
 
204
-		// Loop over possible entity properties.
205
-		foreach ( $custom_fields as $key => $property ) {
204
+        // Loop over possible entity properties.
205
+        foreach ( $custom_fields as $key => $property ) {
206 206
 
207
-			// Check presence of predicate and type.
208
-			if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) {
207
+            // Check presence of predicate and type.
208
+            if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) {
209 209
 
210
-				// Check if input_field is defined.
211
-				if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) {
210
+                // Check if input_field is defined.
211
+                if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) {
212 212
 
213
-					$grouped_key = $property['input_field'];
213
+                    $grouped_key = $property['input_field'];
214 214
 
215
-					// Update list of grouped properties.
216
-					$grouped_properties[ $grouped_key ][ $key ] = $property;
215
+                    // Update list of grouped properties.
216
+                    $grouped_properties[ $grouped_key ][ $key ] = $property;
217 217
 
218
-				} else {
218
+                } else {
219 219
 
220
-					// input_field not defined, add simple metabox.
221
-					$simple_properties[ $key ] = $property;
222
-				}
223
-			}
224
-		}
220
+                    // input_field not defined, add simple metabox.
221
+                    $simple_properties[ $key ] = $property;
222
+                }
223
+            }
224
+        }
225 225
 
226
-		return array( $simple_properties, $grouped_properties );
227
-	}
226
+        return array( $simple_properties, $grouped_properties );
227
+    }
228 228
 
229
-	/**
230
-	 * Add a Field to the current Metabox, based on the description of the Field.
231
-	 * This method is a rude factory for Field objects.
232
-	 *
233
-	 * @param array $args The field's information.
234
-	 * @param bool $grouped Flag to distinguish between simple and grouped fields.
235
-	 */
236
-	public function add_field( $args, $grouped = false ) {
229
+    /**
230
+     * Add a Field to the current Metabox, based on the description of the Field.
231
+     * This method is a rude factory for Field objects.
232
+     *
233
+     * @param array $args The field's information.
234
+     * @param bool $grouped Flag to distinguish between simple and grouped fields.
235
+     */
236
+    public function add_field( $args, $grouped = false ) {
237 237
 
238
-		if ( $grouped ) {
238
+        if ( $grouped ) {
239 239
 
240
-			// Special fields (sameas, coordinates, etc.).
241
-			//
242
-			// Build Field with a custom class (e.g. WL_Metabox_Field_date).
243
-			$field_class = 'WL_Metabox_Field_' . key( $args );
240
+            // Special fields (sameas, coordinates, etc.).
241
+            //
242
+            // Build Field with a custom class (e.g. WL_Metabox_Field_date).
243
+            $field_class = 'WL_Metabox_Field_' . key( $args );
244 244
 
245
-		} else {
245
+        } else {
246 246
 
247
-			// Simple fields (string, uri, boolean, etc.).
248
-			//
249
-			// Which field? We want to use the class that is specific for the field.
250
-			$meta      = key( $args );
251
-			$this_meta = $args[ $meta ];
247
+            // Simple fields (string, uri, boolean, etc.).
248
+            //
249
+            // Which field? We want to use the class that is specific for the field.
250
+            $meta      = key( $args );
251
+            $this_meta = $args[ $meta ];
252 252
 
253
-			// If the field declares what metabox it wants, use that one.
254
-			if ( isset( $this_meta['metabox']['class'] ) ) {
253
+            // If the field declares what metabox it wants, use that one.
254
+            if ( isset( $this_meta['metabox']['class'] ) ) {
255 255
 
256
-				$field_class = $this_meta['metabox']['class'];
256
+                $field_class = $this_meta['metabox']['class'];
257 257
 
258
-			} elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) {
258
+            } elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) {
259 259
 
260
-				// TODO: all fields should explicitly declare the required WL_Metabox.
261
-				// When they will remove this.
262
-				//
263
-				// Use default WL_Metabox_Field (manages strings).
264
-				$field_class = 'WL_Metabox_Field';
260
+                // TODO: all fields should explicitly declare the required WL_Metabox.
261
+                // When they will remove this.
262
+                //
263
+                // Use default WL_Metabox_Field (manages strings).
264
+                $field_class = 'WL_Metabox_Field';
265 265
 
266
-			} else {
266
+            } else {
267 267
 
268
-				// TODO: all fields should explicitly declare the required WL_Metabox.
269
-				// When they will remove this.
270
-				//
271
-				// Build Field with a custom class (e.g. WL_Metabox_Field_date).
272
-				$field_class = 'WL_Metabox_Field_' . $this_meta['type'];
268
+                // TODO: all fields should explicitly declare the required WL_Metabox.
269
+                // When they will remove this.
270
+                //
271
+                // Build Field with a custom class (e.g. WL_Metabox_Field_date).
272
+                $field_class = 'WL_Metabox_Field_' . $this_meta['type'];
273 273
 
274
-			}
274
+            }
275 275
 
276
-		}
277
-		// End if().
276
+        }
277
+        // End if().
278 278
 
279
-		// Call apropriate constructor (e.g. WL_Metabox_Field_... ).
280
-		$this->fields[] = new $field_class( $args );
281
-	}
279
+        // Call apropriate constructor (e.g. WL_Metabox_Field_... ).
280
+        $this->fields[] = new $field_class( $args );
281
+    }
282 282
 
283
-	/**
284
-	 * Save the form data for the specified entity {@link WP_Post}'s id.
285
-	 *
286
-	 * @param int $entity_id The entity's {@link WP_Post}'s id.
287
-	 *
288
-	 * @since 3.5.4
289
-	 *
290
-	 */
291
-	public function save_form_data( $entity_id ) {
292
-
293
-		$this->log->trace( "Saving form data for entity post $entity_id..." );
294
-
295
-		// Build Field objects.
296
-		$this->instantiate_fields( $entity_id );
283
+    /**
284
+     * Save the form data for the specified entity {@link WP_Post}'s id.
285
+     *
286
+     * @param int $entity_id The entity's {@link WP_Post}'s id.
287
+     *
288
+     * @since 3.5.4
289
+     *
290
+     */
291
+    public function save_form_data( $entity_id ) {
292
+
293
+        $this->log->trace( "Saving form data for entity post $entity_id..." );
294
+
295
+        // Build Field objects.
296
+        $this->instantiate_fields( $entity_id );
297 297
 
298
-		// Check if WL metabox form was posted.
299
-		if ( ! isset( $_POST['wl_metaboxes'] ) ) {
300
-			$this->log->debug( "`wl_metaboxes`, skipping..." );
298
+        // Check if WL metabox form was posted.
299
+        if ( ! isset( $_POST['wl_metaboxes'] ) ) {
300
+            $this->log->debug( "`wl_metaboxes`, skipping..." );
301 301
 
302
-			return;
303
-		}
302
+            return;
303
+        }
304 304
 
305
-		foreach ( $this->fields as $field ) {
305
+        foreach ( $this->fields as $field ) {
306 306
 
307
-			// Verify nonce.
308
-			$valid_nonce = $field->verify_nonce();
309
-			if ( $valid_nonce ) {
310
-
311
-				$posted_data = $_POST['wl_metaboxes'];
312
-				$field_name  = $field->meta_name;
313
-
314
-				// Each Filed only deals with its values.
315
-				if ( isset( $posted_data[ $field_name ] ) ) {
316
-
317
-					$values = $posted_data[ $field_name ];
318
-					if ( ! is_array( $values ) ) {
319
-						$values = array( $values );
320
-					}
321
-
322
-					// Save data permanently
323
-					$field->save_data( $values );
324
-				}
325
-			}
326
-		}
327
-
328
-		/**
329
-		 * Filter: 'wl_save_form_pre_push_entity' - Allow to hook right
330
-		 * before the triples are pushed to the linked dataset.
331
-		 *
332
-		 * @param int $entity_id The entity id.
333
-		 * @param int $id The post data.
334
-		 *
335
-		 * @since  3.18.2
336
-		 *
337
-		 */
338
-		do_action( 'wl_save_form_pre_push_entity', $entity_id, $_POST );
339
-
340
-		Wordlift_Linked_Data_Service::get_instance()->push( $entity_id );
341
-
342
-	}
343
-
344
-	/**
345
-	 * Enqueue scripts and styles.
346
-	 *
347
-	 * @since 3.0.0
348
-	 */
349
-	public function enqueue_scripts_and_styles() {
350
-
351
-		// Use the minified version if PW_DEBUG isn't set.
352
-		$min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : '';
353
-
354
-		// Load the jquery-ui-timepicker-addon library.
355
-		wp_enqueue_style( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.css", array(), '3.0.6' );
356
-		wp_enqueue_script( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true );
357
-
358
-		// Leaflet.
359
-		wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
360
-		wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0' );
361
-
362
-		// Add AJAX autocomplete to facilitate metabox editing.
363
-		wp_enqueue_script( 'wl-entity-metabox-utility', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wl_entity_metabox_utilities.js' );
364
-		wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array(
365
-				'ajax_url' => admin_url( 'admin-ajax.php' ),
366
-				'action'   => 'entity_by_title',
367
-			)
368
-		);
369
-
370
-	}
307
+            // Verify nonce.
308
+            $valid_nonce = $field->verify_nonce();
309
+            if ( $valid_nonce ) {
310
+
311
+                $posted_data = $_POST['wl_metaboxes'];
312
+                $field_name  = $field->meta_name;
313
+
314
+                // Each Filed only deals with its values.
315
+                if ( isset( $posted_data[ $field_name ] ) ) {
316
+
317
+                    $values = $posted_data[ $field_name ];
318
+                    if ( ! is_array( $values ) ) {
319
+                        $values = array( $values );
320
+                    }
321
+
322
+                    // Save data permanently
323
+                    $field->save_data( $values );
324
+                }
325
+            }
326
+        }
327
+
328
+        /**
329
+         * Filter: 'wl_save_form_pre_push_entity' - Allow to hook right
330
+         * before the triples are pushed to the linked dataset.
331
+         *
332
+         * @param int $entity_id The entity id.
333
+         * @param int $id The post data.
334
+         *
335
+         * @since  3.18.2
336
+         *
337
+         */
338
+        do_action( 'wl_save_form_pre_push_entity', $entity_id, $_POST );
339
+
340
+        Wordlift_Linked_Data_Service::get_instance()->push( $entity_id );
341
+
342
+    }
343
+
344
+    /**
345
+     * Enqueue scripts and styles.
346
+     *
347
+     * @since 3.0.0
348
+     */
349
+    public function enqueue_scripts_and_styles() {
350
+
351
+        // Use the minified version if PW_DEBUG isn't set.
352
+        $min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : '';
353
+
354
+        // Load the jquery-ui-timepicker-addon library.
355
+        wp_enqueue_style( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.css", array(), '3.0.6' );
356
+        wp_enqueue_script( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true );
357
+
358
+        // Leaflet.
359
+        wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
360
+        wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0' );
361
+
362
+        // Add AJAX autocomplete to facilitate metabox editing.
363
+        wp_enqueue_script( 'wl-entity-metabox-utility', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wl_entity_metabox_utilities.js' );
364
+        wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array(
365
+                'ajax_url' => admin_url( 'admin-ajax.php' ),
366
+                'action'   => 'entity_by_title',
367
+            )
368
+        );
369
+
370
+    }
371 371
 
372 372
 }
Please login to merge, or discard this patch.
Spacing   +73 added lines, -73 removed lines patch added patch discarded remove patch
@@ -7,16 +7,16 @@  discard block
 block discarded – undo
7 7
  * @subpackage Wordlift/admin/WL_Metabox
8 8
  */
9 9
 
10
-require_once( 'class-wl-metabox-field.php' );
11
-require_once( 'class-wl-metabox-field-uri.php' );
12
-require_once( 'class-wl-metabox-field-sameas.php' );
13
-require_once( 'WL_Metabox_Field_date.php' );
14
-require_once( 'WL_Metabox_Field_coordinates.php' );
15
-require_once( 'WL_Metabox_Field_address.php' );
16
-require_once( 'class-wordlift-metabox-field-duration.php' );
17
-require_once( 'class-wordlift-metabox-field-multiline.php' );
18
-require_once( 'class-wordlift-metabox-field-integer.php' );
19
-require_once( 'class-wordlift-metabox-field-select.php' );
10
+require_once('class-wl-metabox-field.php');
11
+require_once('class-wl-metabox-field-uri.php');
12
+require_once('class-wl-metabox-field-sameas.php');
13
+require_once('WL_Metabox_Field_date.php');
14
+require_once('WL_Metabox_Field_coordinates.php');
15
+require_once('WL_Metabox_Field_address.php');
16
+require_once('class-wordlift-metabox-field-duration.php');
17
+require_once('class-wordlift-metabox-field-multiline.php');
18
+require_once('class-wordlift-metabox-field-integer.php');
19
+require_once('class-wordlift-metabox-field-select.php');
20 20
 
21 21
 /**
22 22
  * Define the {@link WL_Metabox} class.
@@ -52,11 +52,11 @@  discard block
 block discarded – undo
52 52
 	 */
53 53
 	public function __construct() {
54 54
 
55
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
55
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
56 56
 
57 57
 		// Add hooks to print metaboxes and save submitted data.
58
-		add_action( 'add_meta_boxes', array( $this, 'add_main_metabox' ) );
59
-		add_action( 'wl_linked_data_save_post', array( $this, 'save_form_data', ) );
58
+		add_action('add_meta_boxes', array($this, 'add_main_metabox'));
59
+		add_action('wl_linked_data_save_post', array($this, 'save_form_data',));
60 60
 
61 61
 		// Enqueue js and css.
62 62
 		$this->enqueue_scripts_and_styles();
@@ -70,23 +70,23 @@  discard block
 block discarded – undo
70 70
 	public function add_main_metabox() {
71 71
 
72 72
 		// Build the fields we need to print.
73
-		$this->instantiate_fields( get_the_ID() );
73
+		$this->instantiate_fields(get_the_ID());
74 74
 
75 75
 		// Bailout if there are no actual fields, we do not need a metabox in that case.
76
-		if ( empty( $this->fields ) ) {
76
+		if (empty($this->fields)) {
77 77
 			return;
78 78
 		}
79 79
 
80 80
 		// Add main metabox (will print also the inner fields).
81
-		$id    = uniqid( 'wl-metabox-' );
82
-		$title = get_the_title() . ' ' . __( 'properties', 'wordlift' );
81
+		$id    = uniqid('wl-metabox-');
82
+		$title = get_the_title().' '.__('properties', 'wordlift');
83 83
 
84 84
 		// WordPress 4.2 do not accept an array of screens as parameter, have to do be explicit.
85
-		foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $screen ) {
86
-			add_meta_box( $id, $title, array(
85
+		foreach (Wordlift_Entity_Service::valid_entity_post_types() as $screen) {
86
+			add_meta_box($id, $title, array(
87 87
 				$this,
88 88
 				'html',
89
-			), $screen, 'normal', 'high' );
89
+			), $screen, 'normal', 'high');
90 90
 		}
91 91
 
92 92
 		// Add filter to change the metabox CSS class.
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
 		//  dynamically according to the `get_current_screen()` function.
96 96
 		$current_screen = get_current_screen();
97 97
 		$screen         = $current_screen ? $current_screen->post_type : 'entity';
98
-		add_filter( "postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class' );
98
+		add_filter("postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class');
99 99
 
100 100
 	}
101 101
 
@@ -107,10 +107,10 @@  discard block
 block discarded – undo
107 107
 	 * @since 3.1.0
108 108
 	 *
109 109
 	 */
110
-	public function html( $post ) {
110
+	public function html($post) {
111 111
 
112 112
 		// Loop over the fields.
113
-		foreach ( $this->fields as $field ) {
113
+		foreach ($this->fields as $field) {
114 114
 
115 115
 			// load data from DB (values will be available in $field->data).
116 116
 			$field->get_data();
@@ -132,18 +132,18 @@  discard block
 block discarded – undo
132 132
 	 * @since 3.1.0
133 133
 	 *
134 134
 	 */
135
-	public function instantiate_fields( $post_id ) {
135
+	public function instantiate_fields($post_id) {
136 136
 
137
-		$this->log->trace( "Instantiating fields for entity post $post_id..." );
137
+		$this->log->trace("Instantiating fields for entity post $post_id...");
138 138
 
139 139
 		// This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering.
140
-		if ( isset( $this->fields ) ) {
140
+		if (isset($this->fields)) {
141 141
 			return;
142 142
 		}
143 143
 
144
-		$entity_type = wl_entity_taxonomy_get_custom_fields( $post_id );
144
+		$entity_type = wl_entity_taxonomy_get_custom_fields($post_id);
145 145
 
146
-		if ( isset( $entity_type ) ) {
146
+		if (isset($entity_type)) {
147 147
 
148 148
 			/*
149 149
 			 * Might not have any relevant meta box field, for example for articles,
@@ -158,31 +158,31 @@  discard block
 block discarded – undo
158 158
 			 * - simple: accept values for one property
159 159
 			 * - grouped: accept values for more properties, or for one property that needs a specific metabox.
160 160
 			 */
161
-			$metaboxes         = $this->group_properties_by_input_field( $entity_type );
161
+			$metaboxes         = $this->group_properties_by_input_field($entity_type);
162 162
 			$simple_metaboxes  = $metaboxes[0];
163 163
 			$grouped_metaboxes = $metaboxes[1];
164 164
 
165 165
 			// Loop over simple entity properties.
166
-			foreach ( $simple_metaboxes as $key => $property ) {
166
+			foreach ($simple_metaboxes as $key => $property) {
167 167
 
168 168
 				// Info passed to the metabox.
169 169
 				$info         = array();
170
-				$info[ $key ] = $property;
170
+				$info[$key] = $property;
171 171
 
172 172
 				// Build the requested field as WL_Metabox_Field_ object.
173
-				$this->add_field( $info );
173
+				$this->add_field($info);
174 174
 
175 175
 			}
176 176
 
177 177
 			// Loop over grouped properties.
178
-			foreach ( $grouped_metaboxes as $key => $property ) {
178
+			foreach ($grouped_metaboxes as $key => $property) {
179 179
 
180 180
 				// Info passed to the metabox.
181 181
 				$info         = array();
182
-				$info[ $key ] = $property;
182
+				$info[$key] = $property;
183 183
 
184 184
 				// Build the requested field group as WL_Metabox_Field_ object.
185
-				$this->add_field( $info, true );
185
+				$this->add_field($info, true);
186 186
 
187 187
 			}
188 188
 		}
@@ -196,34 +196,34 @@  discard block
 block discarded – undo
196 196
 	 *
197 197
 	 * @return array
198 198
 	 */
199
-	public function group_properties_by_input_field( $custom_fields ) {
199
+	public function group_properties_by_input_field($custom_fields) {
200 200
 
201 201
 		$simple_properties  = array();
202 202
 		$grouped_properties = array();
203 203
 
204 204
 		// Loop over possible entity properties.
205
-		foreach ( $custom_fields as $key => $property ) {
205
+		foreach ($custom_fields as $key => $property) {
206 206
 
207 207
 			// Check presence of predicate and type.
208
-			if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) {
208
+			if (isset($property['predicate']) && isset($property['type'])) {
209 209
 
210 210
 				// Check if input_field is defined.
211
-				if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) {
211
+				if (isset($property['input_field']) && '' !== $property['input_field']) {
212 212
 
213 213
 					$grouped_key = $property['input_field'];
214 214
 
215 215
 					// Update list of grouped properties.
216
-					$grouped_properties[ $grouped_key ][ $key ] = $property;
216
+					$grouped_properties[$grouped_key][$key] = $property;
217 217
 
218 218
 				} else {
219 219
 
220 220
 					// input_field not defined, add simple metabox.
221
-					$simple_properties[ $key ] = $property;
221
+					$simple_properties[$key] = $property;
222 222
 				}
223 223
 			}
224 224
 		}
225 225
 
226
-		return array( $simple_properties, $grouped_properties );
226
+		return array($simple_properties, $grouped_properties);
227 227
 	}
228 228
 
229 229
 	/**
@@ -233,29 +233,29 @@  discard block
 block discarded – undo
233 233
 	 * @param array $args The field's information.
234 234
 	 * @param bool $grouped Flag to distinguish between simple and grouped fields.
235 235
 	 */
236
-	public function add_field( $args, $grouped = false ) {
236
+	public function add_field($args, $grouped = false) {
237 237
 
238
-		if ( $grouped ) {
238
+		if ($grouped) {
239 239
 
240 240
 			// Special fields (sameas, coordinates, etc.).
241 241
 			//
242 242
 			// Build Field with a custom class (e.g. WL_Metabox_Field_date).
243
-			$field_class = 'WL_Metabox_Field_' . key( $args );
243
+			$field_class = 'WL_Metabox_Field_'.key($args);
244 244
 
245 245
 		} else {
246 246
 
247 247
 			// Simple fields (string, uri, boolean, etc.).
248 248
 			//
249 249
 			// Which field? We want to use the class that is specific for the field.
250
-			$meta      = key( $args );
251
-			$this_meta = $args[ $meta ];
250
+			$meta      = key($args);
251
+			$this_meta = $args[$meta];
252 252
 
253 253
 			// If the field declares what metabox it wants, use that one.
254
-			if ( isset( $this_meta['metabox']['class'] ) ) {
254
+			if (isset($this_meta['metabox']['class'])) {
255 255
 
256 256
 				$field_class = $this_meta['metabox']['class'];
257 257
 
258
-			} elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) {
258
+			} elseif ( ! isset($this_meta['type']) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type']) {
259 259
 
260 260
 				// TODO: all fields should explicitly declare the required WL_Metabox.
261 261
 				// When they will remove this.
@@ -269,7 +269,7 @@  discard block
 block discarded – undo
269 269
 				// When they will remove this.
270 270
 				//
271 271
 				// Build Field with a custom class (e.g. WL_Metabox_Field_date).
272
-				$field_class = 'WL_Metabox_Field_' . $this_meta['type'];
272
+				$field_class = 'WL_Metabox_Field_'.$this_meta['type'];
273 273
 
274 274
 			}
275 275
 
@@ -277,7 +277,7 @@  discard block
 block discarded – undo
277 277
 		// End if().
278 278
 
279 279
 		// Call apropriate constructor (e.g. WL_Metabox_Field_... ).
280
-		$this->fields[] = new $field_class( $args );
280
+		$this->fields[] = new $field_class($args);
281 281
 	}
282 282
 
283 283
 	/**
@@ -288,39 +288,39 @@  discard block
 block discarded – undo
288 288
 	 * @since 3.5.4
289 289
 	 *
290 290
 	 */
291
-	public function save_form_data( $entity_id ) {
291
+	public function save_form_data($entity_id) {
292 292
 
293
-		$this->log->trace( "Saving form data for entity post $entity_id..." );
293
+		$this->log->trace("Saving form data for entity post $entity_id...");
294 294
 
295 295
 		// Build Field objects.
296
-		$this->instantiate_fields( $entity_id );
296
+		$this->instantiate_fields($entity_id);
297 297
 
298 298
 		// Check if WL metabox form was posted.
299
-		if ( ! isset( $_POST['wl_metaboxes'] ) ) {
300
-			$this->log->debug( "`wl_metaboxes`, skipping..." );
299
+		if ( ! isset($_POST['wl_metaboxes'])) {
300
+			$this->log->debug("`wl_metaboxes`, skipping...");
301 301
 
302 302
 			return;
303 303
 		}
304 304
 
305
-		foreach ( $this->fields as $field ) {
305
+		foreach ($this->fields as $field) {
306 306
 
307 307
 			// Verify nonce.
308 308
 			$valid_nonce = $field->verify_nonce();
309
-			if ( $valid_nonce ) {
309
+			if ($valid_nonce) {
310 310
 
311 311
 				$posted_data = $_POST['wl_metaboxes'];
312 312
 				$field_name  = $field->meta_name;
313 313
 
314 314
 				// Each Filed only deals with its values.
315
-				if ( isset( $posted_data[ $field_name ] ) ) {
315
+				if (isset($posted_data[$field_name])) {
316 316
 
317
-					$values = $posted_data[ $field_name ];
318
-					if ( ! is_array( $values ) ) {
319
-						$values = array( $values );
317
+					$values = $posted_data[$field_name];
318
+					if ( ! is_array($values)) {
319
+						$values = array($values);
320 320
 					}
321 321
 
322 322
 					// Save data permanently
323
-					$field->save_data( $values );
323
+					$field->save_data($values);
324 324
 				}
325 325
 			}
326 326
 		}
@@ -335,9 +335,9 @@  discard block
 block discarded – undo
335 335
 		 * @since  3.18.2
336 336
 		 *
337 337
 		 */
338
-		do_action( 'wl_save_form_pre_push_entity', $entity_id, $_POST );
338
+		do_action('wl_save_form_pre_push_entity', $entity_id, $_POST);
339 339
 
340
-		Wordlift_Linked_Data_Service::get_instance()->push( $entity_id );
340
+		Wordlift_Linked_Data_Service::get_instance()->push($entity_id);
341 341
 
342 342
 	}
343 343
 
@@ -349,20 +349,20 @@  discard block
 block discarded – undo
349 349
 	public function enqueue_scripts_and_styles() {
350 350
 
351 351
 		// Use the minified version if PW_DEBUG isn't set.
352
-		$min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : '';
352
+		$min = ! defined('WP_DEBUG') || ! WP_DEBUG ? '.min' : '';
353 353
 
354 354
 		// Load the jquery-ui-timepicker-addon library.
355
-		wp_enqueue_style( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.css", array(), '3.0.6' );
356
-		wp_enqueue_script( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true );
355
+		wp_enqueue_style('wl-flatpickr', dirname(plugin_dir_url(__FILE__))."/js/flatpickr/flatpickr$min.css", array(), '3.0.6');
356
+		wp_enqueue_script('wl-flatpickr', dirname(plugin_dir_url(__FILE__))."/js/flatpickr/flatpickr$min.js", array('jquery'), '3.0.6', true);
357 357
 
358 358
 		// Leaflet.
359
-		wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
360
-		wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0' );
359
+		wp_enqueue_style('wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0');
360
+		wp_enqueue_script('wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0');
361 361
 
362 362
 		// Add AJAX autocomplete to facilitate metabox editing.
363
-		wp_enqueue_script( 'wl-entity-metabox-utility', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wl_entity_metabox_utilities.js' );
364
-		wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array(
365
-				'ajax_url' => admin_url( 'admin-ajax.php' ),
363
+		wp_enqueue_script('wl-entity-metabox-utility', dirname(plugin_dir_url(__FILE__)).'/js/wl_entity_metabox_utilities.js');
364
+		wp_localize_script('wl-entity-metabox-utility', 'wlEntityMetaboxParams', array(
365
+				'ajax_url' => admin_url('admin-ajax.php'),
366 366
 				'action'   => 'entity_by_title',
367 367
 			)
368 368
 		);
Please login to merge, or discard this patch.
src/admin/wordlift_admin_meta_box_entities.php 2 patches
Indentation   +152 added lines, -152 removed lines patch added patch discarded remove patch
@@ -10,16 +10,16 @@  discard block
 block discarded – undo
10 10
  */
11 11
 function wl_register_metaboxes() {
12 12
 
13
-	// Load metabox classes.
14
-	require_once( 'WL_Metabox/class-wl-metabox.php' );
13
+    // Load metabox classes.
14
+    require_once( 'WL_Metabox/class-wl-metabox.php' );
15 15
 
16
-	new WL_Metabox();     // Everything is done inside here with the correct timing.
16
+    new WL_Metabox();     // Everything is done inside here with the correct timing.
17 17
 
18 18
 }
19 19
 
20 20
 if ( is_admin() ) {
21
-	add_action( 'load-post.php', 'wl_register_metaboxes' );
22
-	add_action( 'load-post-new.php', 'wl_register_metaboxes' );
21
+    add_action( 'load-post.php', 'wl_register_metaboxes' );
22
+    add_action( 'load-post-new.php', 'wl_register_metaboxes' );
23 23
 }
24 24
 
25 25
 /**
@@ -29,28 +29,28 @@  discard block
 block discarded – undo
29 29
  */
30 30
 function wl_admin_add_entities_meta_box( $post_type ) {
31 31
 
32
-	/*
32
+    /*
33 33
 	 * Call the `wl_can_see_classification_box` filter to determine whether we can display the classification box.
34 34
 	 *
35 35
 	 * @since 3.20.3
36 36
 	 *
37 37
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/914
38 38
 	 */
39
-	if ( ! apply_filters( 'wl_can_see_classification_box', true ) ) {
40
-		return;
41
-	}
42
-
43
-	// Bail out if the post type doesn't support a TinyMCE editor.
44
-	if ( ! wl_post_type_supports_editor( $post_type ) ) {
45
-		return;
46
-	}
47
-
48
-	if ( ! Wordlift_Admin::is_gutenberg() ) {
49
-		// Add main meta box for related entities and 4W only if not Gutenberg
50
-		add_meta_box(
51
-			'wordlift_entities_box', __( 'WordLift', 'wordlift' ), 'wl_entities_box_content', $post_type, 'side', 'high'
52
-		);
53
-	}
39
+    if ( ! apply_filters( 'wl_can_see_classification_box', true ) ) {
40
+        return;
41
+    }
42
+
43
+    // Bail out if the post type doesn't support a TinyMCE editor.
44
+    if ( ! wl_post_type_supports_editor( $post_type ) ) {
45
+        return;
46
+    }
47
+
48
+    if ( ! Wordlift_Admin::is_gutenberg() ) {
49
+        // Add main meta box for related entities and 4W only if not Gutenberg
50
+        add_meta_box(
51
+            'wordlift_entities_box', __( 'WordLift', 'wordlift' ), 'wl_entities_box_content', $post_type, 'side', 'high'
52
+        );
53
+    }
54 54
 
55 55
 }
56 56
 
@@ -67,19 +67,19 @@  discard block
 block discarded – undo
67 67
  */
68 68
 function wl_post_type_supports_editor( $post_type ) {
69 69
 
70
-	$default = post_type_supports( $post_type, 'editor' );
71
-
72
-	/**
73
-	 * Allow 3rd parties to force the classification to load.
74
-	 *
75
-	 * @param bool $default The preset value as gathered by the `post_type_supports` call.
76
-	 *
77
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/847.
78
-	 *
79
-	 * @since 3.19.4
80
-	 *
81
-	 */
82
-	return apply_filters( 'wl_post_type_supports_editor', $default, $post_type );
70
+    $default = post_type_supports( $post_type, 'editor' );
71
+
72
+    /**
73
+     * Allow 3rd parties to force the classification to load.
74
+     *
75
+     * @param bool $default The preset value as gathered by the `post_type_supports` call.
76
+     *
77
+     * @see https://github.com/insideout10/wordlift-plugin/issues/847.
78
+     *
79
+     * @since 3.19.4
80
+     *
81
+     */
82
+    return apply_filters( 'wl_post_type_supports_editor', $default, $post_type );
83 83
 }
84 84
 
85 85
 /**
@@ -89,135 +89,135 @@  discard block
 block discarded – undo
89 89
  */
90 90
 function wl_entities_box_content( $post, $wrapper = true ) {
91 91
 
92
-	// Angularjs edit-post widget wrapper.
93
-	if ( $wrapper ) {
94
-		echo '<div id="wordlift-edit-post-outer-wrapper"></div>';
95
-	}
92
+    // Angularjs edit-post widget wrapper.
93
+    if ( $wrapper ) {
94
+        echo '<div id="wordlift-edit-post-outer-wrapper"></div>';
95
+    }
96 96
 }
97 97
 
98 98
 function wl_entities_box_content_scripts() {
99
-	$post = get_post();
99
+    $post = get_post();
100 100
 
101
-	// Angularjs edit-post widget classification boxes configuration.
102
-	$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
101
+    // Angularjs edit-post widget classification boxes configuration.
102
+    $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
103 103
 
104
-	// Array to store all related entities ids.
105
-	$all_referenced_entities_ids = array();
104
+    // Array to store all related entities ids.
105
+    $all_referenced_entities_ids = array();
106 106
 
107
-	// Add selected entities to classification_boxes.
108
-	foreach ( $classification_boxes as $i => $box ) {
109
-		// Build the proper relation name.
110
-		$relation_name = $box['id'];
107
+    // Add selected entities to classification_boxes.
108
+    foreach ( $classification_boxes as $i => $box ) {
109
+        // Build the proper relation name.
110
+        $relation_name = $box['id'];
111 111
 
112
-		// Get the entity referenced from the post content.
113
-		/*
112
+        // Get the entity referenced from the post content.
113
+        /*
114 114
 		 * Allow 3rd parties to provide another post content.
115 115
 		 *
116 116
 		 * @since 3.20.0
117 117
 		 */
118
-		$post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
119
-		$entity_uris  = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris( $post_content );
120
-
121
-		// Enhance current box selected entities.
122
-		$classification_boxes[ $i ]['selectedEntities'] = $entity_uris;
123
-
124
-		// Maps the URIs to entity posts.
125
-		$entity_service = Wordlift_Entity_Service::get_instance();
126
-
127
-		// Replace all entity URI's with post ID's if found or null if there is no related post.
128
-		$entity_ids = array_map( function ( $item ) use ( $entity_service ) {
129
-			// Return entity post by the entity URI or null.
130
-			$post = $entity_service->get_entity_post_by_uri( $item );
131
-
132
-			// Check that the post object is not null.
133
-			if ( ! empty( $post ) ) {
134
-				return $post->ID;
135
-			}
136
-		}, $entity_uris );
137
-		// Store the entity ids for all the 4W.
138
-		$all_referenced_entities_ids = array_merge( $all_referenced_entities_ids, $entity_ids );
139
-
140
-	}
141
-
142
-	// Json encoding for classification boxes structure.
143
-	$classification_boxes = wp_json_encode( $classification_boxes );
144
-
145
-	// Ensure there are no repetitions of the referenced entities.
146
-	$all_referenced_entities_ids = array_unique( $all_referenced_entities_ids );
147
-
148
-	// Remove all null, false and empty strings.
149
-	// NULL is being returned in some cases, when there is not related post, so we need to remove it.
150
-	$all_referenced_entities_ids = array_filter( $all_referenced_entities_ids );
151
-
152
-	// Build the entity storage object.
153
-	$referenced_entities_obj = array();
154
-	foreach ( $all_referenced_entities_ids as $referenced_entity ) {
155
-		$entity = wl_serialize_entity( $referenced_entity );
156
-		// Set a default confidence of `PHP_INT_MAX` for already annotated entities.
157
-		$referenced_entities_obj[ $entity['id'] ] = $entity
158
-		                                            + array( 'confidence' => PHP_INT_MAX );
159
-	}
160
-
161
-	$referenced_entities_obj = empty( $referenced_entities_obj ) ?
162
-		'{}' : wp_json_encode( $referenced_entities_obj );
163
-
164
-	$published_place_id  = get_post_meta(
165
-		$post->ID, Wordlift_Schema_Service::FIELD_LOCATION_CREATED, true
166
-	);
167
-	$published_place_obj = ( $published_place_id ) ?
168
-		wp_json_encode( wl_serialize_entity( $published_place_id ) ) :
169
-		null;
170
-
171
-	$topic_id  = get_post_meta(
172
-		$post->ID, Wordlift_Schema_Service::FIELD_TOPIC, true
173
-	);
174
-	$topic_obj = ( $topic_id ) ?
175
-		wp_json_encode( wl_serialize_entity( $topic_id ) ) :
176
-		null;
177
-
178
-	$configuration_service = Wordlift_Configuration_Service::get_instance();
179
-
180
-	$default_thumbnail_path = WL_DEFAULT_THUMBNAIL_PATH;
181
-	$default_path           = WL_DEFAULT_PATH;
182
-	$dataset_uri            = $configuration_service->get_dataset_uri();
183
-	$current_post_uri       = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID );
184
-	$is_entity              = Wordlift_Entity_Service::get_instance()->is_entity( $post->ID );
185
-
186
-	// Retrieve the current post author.
187
-	$post_author = get_userdata( $post->post_author )->display_name;
188
-	// Retrive the published date.
189
-	$published_date = get_the_time( 'Y-m-d', $post->ID );
190
-	// Current language.
191
-	$current_language            = $configuration_service->get_language_code();
192
-	$wordlift_timeline_shortcode = new Wordlift_Timeline_Shortcode();
193
-	$timelinejs_default_options  = json_encode( $wordlift_timeline_shortcode->get_timelinejs_default_options(), JSON_PRETTY_PRINT );
194
-	$addslashes_post_author      = addslashes( $post_author );
195
-
196
-	$metabox_settings = array(
197
-		"classificationBoxes"      => json_decode( $classification_boxes ),
198
-		"entities"                 => json_decode( $referenced_entities_obj ),
199
-		"currentPostId"            => intval( $post->ID ),
200
-		"currentPostUri"           => $current_post_uri,
201
-		"currentPostType"          => $post->post_type,
202
-		"isEntity"                 => ! empty( $is_entity ),
203
-		"defaultThumbnailPath"     => $default_thumbnail_path,
204
-		"defaultWordLiftPath"      => $default_path,
205
-		"datasetUri"               => $dataset_uri,
206
-		"currentUser"              => $addslashes_post_author,
207
-		"publishedDate"            => $published_date,
208
-		"publishedPlace"           => $published_place_obj,
209
-		"topic"                    => json_decode( $topic_obj ),
210
-		"currentLanguage"          => $current_language,
211
-		"timelinejsDefaultOptions" => json_decode( $timelinejs_default_options ),
212
-		"ajax_url"                 => admin_url( 'admin-ajax.php' ),
213
-	);
214
-
215
-	// Allow Classic and Block Editor scripts to register first.
216
-	// Hook to the Block Editor script.
217
-	wp_localize_script( 'wl-block-editor', '_wlMetaBoxSettings', array( 'settings' => $metabox_settings ) );
218
-
219
-	// Hook to the Classic Editor script, see Wordlift_Admin_Post_Edit_Page.
220
-	wp_localize_script( 'wl-classic-editor', '_wlMetaBoxSettings', array( 'settings' => $metabox_settings ) );
118
+        $post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
119
+        $entity_uris  = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris( $post_content );
120
+
121
+        // Enhance current box selected entities.
122
+        $classification_boxes[ $i ]['selectedEntities'] = $entity_uris;
123
+
124
+        // Maps the URIs to entity posts.
125
+        $entity_service = Wordlift_Entity_Service::get_instance();
126
+
127
+        // Replace all entity URI's with post ID's if found or null if there is no related post.
128
+        $entity_ids = array_map( function ( $item ) use ( $entity_service ) {
129
+            // Return entity post by the entity URI or null.
130
+            $post = $entity_service->get_entity_post_by_uri( $item );
131
+
132
+            // Check that the post object is not null.
133
+            if ( ! empty( $post ) ) {
134
+                return $post->ID;
135
+            }
136
+        }, $entity_uris );
137
+        // Store the entity ids for all the 4W.
138
+        $all_referenced_entities_ids = array_merge( $all_referenced_entities_ids, $entity_ids );
139
+
140
+    }
141
+
142
+    // Json encoding for classification boxes structure.
143
+    $classification_boxes = wp_json_encode( $classification_boxes );
144
+
145
+    // Ensure there are no repetitions of the referenced entities.
146
+    $all_referenced_entities_ids = array_unique( $all_referenced_entities_ids );
147
+
148
+    // Remove all null, false and empty strings.
149
+    // NULL is being returned in some cases, when there is not related post, so we need to remove it.
150
+    $all_referenced_entities_ids = array_filter( $all_referenced_entities_ids );
151
+
152
+    // Build the entity storage object.
153
+    $referenced_entities_obj = array();
154
+    foreach ( $all_referenced_entities_ids as $referenced_entity ) {
155
+        $entity = wl_serialize_entity( $referenced_entity );
156
+        // Set a default confidence of `PHP_INT_MAX` for already annotated entities.
157
+        $referenced_entities_obj[ $entity['id'] ] = $entity
158
+                                                    + array( 'confidence' => PHP_INT_MAX );
159
+    }
160
+
161
+    $referenced_entities_obj = empty( $referenced_entities_obj ) ?
162
+        '{}' : wp_json_encode( $referenced_entities_obj );
163
+
164
+    $published_place_id  = get_post_meta(
165
+        $post->ID, Wordlift_Schema_Service::FIELD_LOCATION_CREATED, true
166
+    );
167
+    $published_place_obj = ( $published_place_id ) ?
168
+        wp_json_encode( wl_serialize_entity( $published_place_id ) ) :
169
+        null;
170
+
171
+    $topic_id  = get_post_meta(
172
+        $post->ID, Wordlift_Schema_Service::FIELD_TOPIC, true
173
+    );
174
+    $topic_obj = ( $topic_id ) ?
175
+        wp_json_encode( wl_serialize_entity( $topic_id ) ) :
176
+        null;
177
+
178
+    $configuration_service = Wordlift_Configuration_Service::get_instance();
179
+
180
+    $default_thumbnail_path = WL_DEFAULT_THUMBNAIL_PATH;
181
+    $default_path           = WL_DEFAULT_PATH;
182
+    $dataset_uri            = $configuration_service->get_dataset_uri();
183
+    $current_post_uri       = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID );
184
+    $is_entity              = Wordlift_Entity_Service::get_instance()->is_entity( $post->ID );
185
+
186
+    // Retrieve the current post author.
187
+    $post_author = get_userdata( $post->post_author )->display_name;
188
+    // Retrive the published date.
189
+    $published_date = get_the_time( 'Y-m-d', $post->ID );
190
+    // Current language.
191
+    $current_language            = $configuration_service->get_language_code();
192
+    $wordlift_timeline_shortcode = new Wordlift_Timeline_Shortcode();
193
+    $timelinejs_default_options  = json_encode( $wordlift_timeline_shortcode->get_timelinejs_default_options(), JSON_PRETTY_PRINT );
194
+    $addslashes_post_author      = addslashes( $post_author );
195
+
196
+    $metabox_settings = array(
197
+        "classificationBoxes"      => json_decode( $classification_boxes ),
198
+        "entities"                 => json_decode( $referenced_entities_obj ),
199
+        "currentPostId"            => intval( $post->ID ),
200
+        "currentPostUri"           => $current_post_uri,
201
+        "currentPostType"          => $post->post_type,
202
+        "isEntity"                 => ! empty( $is_entity ),
203
+        "defaultThumbnailPath"     => $default_thumbnail_path,
204
+        "defaultWordLiftPath"      => $default_path,
205
+        "datasetUri"               => $dataset_uri,
206
+        "currentUser"              => $addslashes_post_author,
207
+        "publishedDate"            => $published_date,
208
+        "publishedPlace"           => $published_place_obj,
209
+        "topic"                    => json_decode( $topic_obj ),
210
+        "currentLanguage"          => $current_language,
211
+        "timelinejsDefaultOptions" => json_decode( $timelinejs_default_options ),
212
+        "ajax_url"                 => admin_url( 'admin-ajax.php' ),
213
+    );
214
+
215
+    // Allow Classic and Block Editor scripts to register first.
216
+    // Hook to the Block Editor script.
217
+    wp_localize_script( 'wl-block-editor', '_wlMetaBoxSettings', array( 'settings' => $metabox_settings ) );
218
+
219
+    // Hook to the Classic Editor script, see Wordlift_Admin_Post_Edit_Page.
220
+    wp_localize_script( 'wl-classic-editor', '_wlMetaBoxSettings', array( 'settings' => $metabox_settings ) );
221 221
 
222 222
 }
223 223
 
Please login to merge, or discard this patch.
Spacing   +56 added lines, -58 removed lines patch added patch discarded remove patch
@@ -11,15 +11,15 @@  discard block
 block discarded – undo
11 11
 function wl_register_metaboxes() {
12 12
 
13 13
 	// Load metabox classes.
14
-	require_once( 'WL_Metabox/class-wl-metabox.php' );
14
+	require_once('WL_Metabox/class-wl-metabox.php');
15 15
 
16
-	new WL_Metabox();     // Everything is done inside here with the correct timing.
16
+	new WL_Metabox(); // Everything is done inside here with the correct timing.
17 17
 
18 18
 }
19 19
 
20
-if ( is_admin() ) {
21
-	add_action( 'load-post.php', 'wl_register_metaboxes' );
22
-	add_action( 'load-post-new.php', 'wl_register_metaboxes' );
20
+if (is_admin()) {
21
+	add_action('load-post.php', 'wl_register_metaboxes');
22
+	add_action('load-post-new.php', 'wl_register_metaboxes');
23 23
 }
24 24
 
25 25
 /**
@@ -27,7 +27,7 @@  discard block
 block discarded – undo
27 27
  *
28 28
  * @param string $post_type The type of the current open post.
29 29
  */
30
-function wl_admin_add_entities_meta_box( $post_type ) {
30
+function wl_admin_add_entities_meta_box($post_type) {
31 31
 
32 32
 	/*
33 33
 	 * Call the `wl_can_see_classification_box` filter to determine whether we can display the classification box.
@@ -36,25 +36,25 @@  discard block
 block discarded – undo
36 36
 	 *
37 37
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/914
38 38
 	 */
39
-	if ( ! apply_filters( 'wl_can_see_classification_box', true ) ) {
39
+	if ( ! apply_filters('wl_can_see_classification_box', true)) {
40 40
 		return;
41 41
 	}
42 42
 
43 43
 	// Bail out if the post type doesn't support a TinyMCE editor.
44
-	if ( ! wl_post_type_supports_editor( $post_type ) ) {
44
+	if ( ! wl_post_type_supports_editor($post_type)) {
45 45
 		return;
46 46
 	}
47 47
 
48
-	if ( ! Wordlift_Admin::is_gutenberg() ) {
48
+	if ( ! Wordlift_Admin::is_gutenberg()) {
49 49
 		// Add main meta box for related entities and 4W only if not Gutenberg
50 50
 		add_meta_box(
51
-			'wordlift_entities_box', __( 'WordLift', 'wordlift' ), 'wl_entities_box_content', $post_type, 'side', 'high'
51
+			'wordlift_entities_box', __('WordLift', 'wordlift'), 'wl_entities_box_content', $post_type, 'side', 'high'
52 52
 		);
53 53
 	}
54 54
 
55 55
 }
56 56
 
57
-add_action( 'add_meta_boxes', 'wl_admin_add_entities_meta_box' );
57
+add_action('add_meta_boxes', 'wl_admin_add_entities_meta_box');
58 58
 
59 59
 /**
60 60
  * Whether the post type supports the editor UI.
@@ -65,9 +65,9 @@  discard block
 block discarded – undo
65 65
  *
66 66
  * @return bool True if the editor UI is supported otherwise false.
67 67
  */
68
-function wl_post_type_supports_editor( $post_type ) {
68
+function wl_post_type_supports_editor($post_type) {
69 69
 
70
-	$default = post_type_supports( $post_type, 'editor' );
70
+	$default = post_type_supports($post_type, 'editor');
71 71
 
72 72
 	/**
73 73
 	 * Allow 3rd parties to force the classification to load.
@@ -79,7 +79,7 @@  discard block
 block discarded – undo
79 79
 	 * @since 3.19.4
80 80
 	 *
81 81
 	 */
82
-	return apply_filters( 'wl_post_type_supports_editor', $default, $post_type );
82
+	return apply_filters('wl_post_type_supports_editor', $default, $post_type);
83 83
 }
84 84
 
85 85
 /**
@@ -87,10 +87,10 @@  discard block
 block discarded – undo
87 87
  *
88 88
  * @param WP_Post $post The current post.
89 89
  */
90
-function wl_entities_box_content( $post, $wrapper = true ) {
90
+function wl_entities_box_content($post, $wrapper = true) {
91 91
 
92 92
 	// Angularjs edit-post widget wrapper.
93
-	if ( $wrapper ) {
93
+	if ($wrapper) {
94 94
 		echo '<div id="wordlift-edit-post-outer-wrapper"></div>';
95 95
 	}
96 96
 }
@@ -99,13 +99,13 @@  discard block
 block discarded – undo
99 99
 	$post = get_post();
100 100
 
101 101
 	// Angularjs edit-post widget classification boxes configuration.
102
-	$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
102
+	$classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES);
103 103
 
104 104
 	// Array to store all related entities ids.
105 105
 	$all_referenced_entities_ids = array();
106 106
 
107 107
 	// Add selected entities to classification_boxes.
108
-	foreach ( $classification_boxes as $i => $box ) {
108
+	foreach ($classification_boxes as $i => $box) {
109 109
 		// Build the proper relation name.
110 110
 		$relation_name = $box['id'];
111 111
 
@@ -115,111 +115,109 @@  discard block
 block discarded – undo
115 115
 		 *
116 116
 		 * @since 3.20.0
117 117
 		 */
118
-		$post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
119
-		$entity_uris  = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris( $post_content );
118
+		$post_content = apply_filters('wl_post_content', $post->post_content, $post);
119
+		$entity_uris  = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris($post_content);
120 120
 
121 121
 		// Enhance current box selected entities.
122
-		$classification_boxes[ $i ]['selectedEntities'] = $entity_uris;
122
+		$classification_boxes[$i]['selectedEntities'] = $entity_uris;
123 123
 
124 124
 		// Maps the URIs to entity posts.
125 125
 		$entity_service = Wordlift_Entity_Service::get_instance();
126 126
 
127 127
 		// Replace all entity URI's with post ID's if found or null if there is no related post.
128
-		$entity_ids = array_map( function ( $item ) use ( $entity_service ) {
128
+		$entity_ids = array_map(function($item) use ($entity_service) {
129 129
 			// Return entity post by the entity URI or null.
130
-			$post = $entity_service->get_entity_post_by_uri( $item );
130
+			$post = $entity_service->get_entity_post_by_uri($item);
131 131
 
132 132
 			// Check that the post object is not null.
133
-			if ( ! empty( $post ) ) {
133
+			if ( ! empty($post)) {
134 134
 				return $post->ID;
135 135
 			}
136
-		}, $entity_uris );
136
+		}, $entity_uris);
137 137
 		// Store the entity ids for all the 4W.
138
-		$all_referenced_entities_ids = array_merge( $all_referenced_entities_ids, $entity_ids );
138
+		$all_referenced_entities_ids = array_merge($all_referenced_entities_ids, $entity_ids);
139 139
 
140 140
 	}
141 141
 
142 142
 	// Json encoding for classification boxes structure.
143
-	$classification_boxes = wp_json_encode( $classification_boxes );
143
+	$classification_boxes = wp_json_encode($classification_boxes);
144 144
 
145 145
 	// Ensure there are no repetitions of the referenced entities.
146
-	$all_referenced_entities_ids = array_unique( $all_referenced_entities_ids );
146
+	$all_referenced_entities_ids = array_unique($all_referenced_entities_ids);
147 147
 
148 148
 	// Remove all null, false and empty strings.
149 149
 	// NULL is being returned in some cases, when there is not related post, so we need to remove it.
150
-	$all_referenced_entities_ids = array_filter( $all_referenced_entities_ids );
150
+	$all_referenced_entities_ids = array_filter($all_referenced_entities_ids);
151 151
 
152 152
 	// Build the entity storage object.
153 153
 	$referenced_entities_obj = array();
154
-	foreach ( $all_referenced_entities_ids as $referenced_entity ) {
155
-		$entity = wl_serialize_entity( $referenced_entity );
154
+	foreach ($all_referenced_entities_ids as $referenced_entity) {
155
+		$entity = wl_serialize_entity($referenced_entity);
156 156
 		// Set a default confidence of `PHP_INT_MAX` for already annotated entities.
157
-		$referenced_entities_obj[ $entity['id'] ] = $entity
158
-		                                            + array( 'confidence' => PHP_INT_MAX );
157
+		$referenced_entities_obj[$entity['id']] = $entity
158
+		                                            + array('confidence' => PHP_INT_MAX);
159 159
 	}
160 160
 
161
-	$referenced_entities_obj = empty( $referenced_entities_obj ) ?
162
-		'{}' : wp_json_encode( $referenced_entities_obj );
161
+	$referenced_entities_obj = empty($referenced_entities_obj) ?
162
+		'{}' : wp_json_encode($referenced_entities_obj);
163 163
 
164 164
 	$published_place_id  = get_post_meta(
165 165
 		$post->ID, Wordlift_Schema_Service::FIELD_LOCATION_CREATED, true
166 166
 	);
167
-	$published_place_obj = ( $published_place_id ) ?
168
-		wp_json_encode( wl_serialize_entity( $published_place_id ) ) :
169
-		null;
167
+	$published_place_obj = ($published_place_id) ?
168
+		wp_json_encode(wl_serialize_entity($published_place_id)) : null;
170 169
 
171 170
 	$topic_id  = get_post_meta(
172 171
 		$post->ID, Wordlift_Schema_Service::FIELD_TOPIC, true
173 172
 	);
174
-	$topic_obj = ( $topic_id ) ?
175
-		wp_json_encode( wl_serialize_entity( $topic_id ) ) :
176
-		null;
173
+	$topic_obj = ($topic_id) ?
174
+		wp_json_encode(wl_serialize_entity($topic_id)) : null;
177 175
 
178 176
 	$configuration_service = Wordlift_Configuration_Service::get_instance();
179 177
 
180 178
 	$default_thumbnail_path = WL_DEFAULT_THUMBNAIL_PATH;
181 179
 	$default_path           = WL_DEFAULT_PATH;
182 180
 	$dataset_uri            = $configuration_service->get_dataset_uri();
183
-	$current_post_uri       = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID );
184
-	$is_entity              = Wordlift_Entity_Service::get_instance()->is_entity( $post->ID );
181
+	$current_post_uri       = Wordlift_Entity_Service::get_instance()->get_uri($post->ID);
182
+	$is_entity              = Wordlift_Entity_Service::get_instance()->is_entity($post->ID);
185 183
 
186 184
 	// Retrieve the current post author.
187
-	$post_author = get_userdata( $post->post_author )->display_name;
185
+	$post_author = get_userdata($post->post_author)->display_name;
188 186
 	// Retrive the published date.
189
-	$published_date = get_the_time( 'Y-m-d', $post->ID );
187
+	$published_date = get_the_time('Y-m-d', $post->ID);
190 188
 	// Current language.
191 189
 	$current_language            = $configuration_service->get_language_code();
192 190
 	$wordlift_timeline_shortcode = new Wordlift_Timeline_Shortcode();
193
-	$timelinejs_default_options  = json_encode( $wordlift_timeline_shortcode->get_timelinejs_default_options(), JSON_PRETTY_PRINT );
194
-	$addslashes_post_author      = addslashes( $post_author );
191
+	$timelinejs_default_options  = json_encode($wordlift_timeline_shortcode->get_timelinejs_default_options(), JSON_PRETTY_PRINT);
192
+	$addslashes_post_author      = addslashes($post_author);
195 193
 
196 194
 	$metabox_settings = array(
197
-		"classificationBoxes"      => json_decode( $classification_boxes ),
198
-		"entities"                 => json_decode( $referenced_entities_obj ),
199
-		"currentPostId"            => intval( $post->ID ),
195
+		"classificationBoxes"      => json_decode($classification_boxes),
196
+		"entities"                 => json_decode($referenced_entities_obj),
197
+		"currentPostId"            => intval($post->ID),
200 198
 		"currentPostUri"           => $current_post_uri,
201 199
 		"currentPostType"          => $post->post_type,
202
-		"isEntity"                 => ! empty( $is_entity ),
200
+		"isEntity"                 => ! empty($is_entity),
203 201
 		"defaultThumbnailPath"     => $default_thumbnail_path,
204 202
 		"defaultWordLiftPath"      => $default_path,
205 203
 		"datasetUri"               => $dataset_uri,
206 204
 		"currentUser"              => $addslashes_post_author,
207 205
 		"publishedDate"            => $published_date,
208 206
 		"publishedPlace"           => $published_place_obj,
209
-		"topic"                    => json_decode( $topic_obj ),
207
+		"topic"                    => json_decode($topic_obj),
210 208
 		"currentLanguage"          => $current_language,
211
-		"timelinejsDefaultOptions" => json_decode( $timelinejs_default_options ),
212
-		"ajax_url"                 => admin_url( 'admin-ajax.php' ),
209
+		"timelinejsDefaultOptions" => json_decode($timelinejs_default_options),
210
+		"ajax_url"                 => admin_url('admin-ajax.php'),
213 211
 	);
214 212
 
215 213
 	// Allow Classic and Block Editor scripts to register first.
216 214
 	// Hook to the Block Editor script.
217
-	wp_localize_script( 'wl-block-editor', '_wlMetaBoxSettings', array( 'settings' => $metabox_settings ) );
215
+	wp_localize_script('wl-block-editor', '_wlMetaBoxSettings', array('settings' => $metabox_settings));
218 216
 
219 217
 	// Hook to the Classic Editor script, see Wordlift_Admin_Post_Edit_Page.
220
-	wp_localize_script( 'wl-classic-editor', '_wlMetaBoxSettings', array( 'settings' => $metabox_settings ) );
218
+	wp_localize_script('wl-classic-editor', '_wlMetaBoxSettings', array('settings' => $metabox_settings));
221 219
 
222 220
 }
223 221
 
224
-add_action( 'admin_print_scripts-post.php', 'wl_entities_box_content_scripts', 11 );
225
-add_action( 'admin_print_scripts-post-new.php', 'wl_entities_box_content_scripts', 11 );
222
+add_action('admin_print_scripts-post.php', 'wl_entities_box_content_scripts', 11);
223
+add_action('admin_print_scripts-post-new.php', 'wl_entities_box_content_scripts', 11);
Please login to merge, or discard this patch.
src/includes/class-wordlift-entity-type-service.php 2 patches
Indentation   +374 added lines, -374 removed lines patch added patch discarded remove patch
@@ -19,129 +19,129 @@  discard block
 block discarded – undo
19 19
  */
20 20
 class Wordlift_Entity_Type_Service {
21 21
 
22
-	/**
23
-	 * The {@link Wordlift_Schema_Service} instance.
24
-	 *
25
-	 * @since  3.7.0
26
-	 * @access private
27
-	 * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
28
-	 */
29
-	private $schema_service;
30
-
31
-	/**
32
-	 * A {@link Wordlift_Log_Service} instance.
33
-	 *
34
-	 * @since  3.8.0
35
-	 * @access private
36
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
37
-	 */
38
-	private $log;
39
-
40
-	/**
41
-	 * The {@link Wordlift_Entity_Type_Service} singleton instance.
42
-	 *
43
-	 * @since  3.7.0
44
-	 * @access private
45
-	 * @var \Wordlift_Entity_Type_Service $instance The {@link Wordlift_Entity_Type_Service} singleton instance.
46
-	 */
47
-	private static $instance;
48
-
49
-	/**
50
-	 * Wordlift_Entity_Type_Service constructor.
51
-	 *
52
-	 * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
53
-	 *
54
-	 * @since 3.7.0
55
-	 *
56
-	 */
57
-	public function __construct( $schema_service ) {
58
-
59
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' );
60
-
61
-		$this->schema_service = $schema_service;
62
-
63
-		self::$instance = $this;
64
-
65
-	}
66
-
67
-	/**
68
-	 * Get the {@link Wordlift_Entity_Type_Service} singleton instance.
69
-	 *
70
-	 * @return \Wordlift_Entity_Type_Service The {@link Wordlift_Entity_Type_Service} singleton instance.
71
-	 * @since 3.7.0
72
-	 */
73
-	public static function get_instance() {
74
-
75
-		return self::$instance;
76
-	}
77
-
78
-	/**
79
-	 * Get the types associated with the specified entity post id.
80
-	 *
81
-	 * We have a strategy to define the entity type, given that everything is
82
-	 * an entity, i.e. also posts/pages and custom post types.
83
-	 *
84
-	 * @param int $post_id The post id.
85
-	 *
86
-	 * @return array|null {
87
-	 * An array of type properties or null if no term is associated
88
-	 *
89
-	 * @type string css_class     The css class, e.g. `wl-thing`.
90
-	 * @type string uri           The schema.org class URI, e.g. `http://schema.org/Thing`.
91
-	 * @type array  same_as       An array of same as attributes.
92
-	 * @type array  custom_fields An array of custom fields.
93
-	 * @type array  linked_data   An array of {@link Wordlift_Sparql_Tuple_Rendition}.
94
-	 * }
95
-	 * @since 3.7.0
96
-	 *
97
-	 * @since 3.20.0 This function will **not** return entity types introduced with 3.20.0.
98
-	 *
99
-	 * @since 3.18.0 The cases are the following:
100
-	 *  1. the post has a term from the Entity Types Taxonomy: the term defines
101
-	 *     the entity type, e.g. Organization, Person, ...
102
-	 *  2. the post doesn't have a term from the Entity Types Taxonomy:
103
-	 *      a) the post is a `wl_entity` custom post type, then the post is
104
-	 *           assigned the `Thing` entity type by default.
105
-	 *      b) the post is a `post` post type, then the post is
106
-	 *           assigned the `Article` entity type by default.
107
-	 *      c) the post is a custom post type then it is
108
-	 *          assigned the `WebPage` entity type by default.
109
-	 *
110
-	 */
111
-	public function get( $post_id ) {
112
-
113
-		$this->log->trace( "Getting the post type for post $post_id..." );
114
-
115
-		// Get the post type.
116
-		$post_type = get_post_type( $post_id );
117
-
118
-		// Return `web-page` for non entities.
119
-		if ( ! self::is_valid_entity_post_type( $post_type ) ) {
120
-			$this->log->info( "Returning `web-page` for post $post_id." );
121
-
122
-			return $this->schema_service->get_schema( 'web-page' );
123
-		}
124
-
125
-		// Get the type from the associated classification.
126
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
127
-
128
-		// Return the schema type if there is a term found.
129
-		if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
130
-			// Cycle through the terms and return the first one with a valid schema.
131
-			foreach ( $terms as $term ) {
132
-				$this->log->debug( "Found `{$term->slug}` term for post $post_id." );
133
-
134
-				// Try to get the schema for the term.
135
-				$schema = $this->schema_service->get_schema( $term->slug );
136
-
137
-				// If found, return it, ignoring the other types.
138
-				if ( null !== $schema ) {
139
-					// Return the entity type with the specified id.
140
-					return $schema;
141
-				}
142
-			}
143
-
144
-			/*
22
+    /**
23
+     * The {@link Wordlift_Schema_Service} instance.
24
+     *
25
+     * @since  3.7.0
26
+     * @access private
27
+     * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
28
+     */
29
+    private $schema_service;
30
+
31
+    /**
32
+     * A {@link Wordlift_Log_Service} instance.
33
+     *
34
+     * @since  3.8.0
35
+     * @access private
36
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
37
+     */
38
+    private $log;
39
+
40
+    /**
41
+     * The {@link Wordlift_Entity_Type_Service} singleton instance.
42
+     *
43
+     * @since  3.7.0
44
+     * @access private
45
+     * @var \Wordlift_Entity_Type_Service $instance The {@link Wordlift_Entity_Type_Service} singleton instance.
46
+     */
47
+    private static $instance;
48
+
49
+    /**
50
+     * Wordlift_Entity_Type_Service constructor.
51
+     *
52
+     * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
53
+     *
54
+     * @since 3.7.0
55
+     *
56
+     */
57
+    public function __construct( $schema_service ) {
58
+
59
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' );
60
+
61
+        $this->schema_service = $schema_service;
62
+
63
+        self::$instance = $this;
64
+
65
+    }
66
+
67
+    /**
68
+     * Get the {@link Wordlift_Entity_Type_Service} singleton instance.
69
+     *
70
+     * @return \Wordlift_Entity_Type_Service The {@link Wordlift_Entity_Type_Service} singleton instance.
71
+     * @since 3.7.0
72
+     */
73
+    public static function get_instance() {
74
+
75
+        return self::$instance;
76
+    }
77
+
78
+    /**
79
+     * Get the types associated with the specified entity post id.
80
+     *
81
+     * We have a strategy to define the entity type, given that everything is
82
+     * an entity, i.e. also posts/pages and custom post types.
83
+     *
84
+     * @param int $post_id The post id.
85
+     *
86
+     * @return array|null {
87
+     * An array of type properties or null if no term is associated
88
+     *
89
+     * @type string css_class     The css class, e.g. `wl-thing`.
90
+     * @type string uri           The schema.org class URI, e.g. `http://schema.org/Thing`.
91
+     * @type array  same_as       An array of same as attributes.
92
+     * @type array  custom_fields An array of custom fields.
93
+     * @type array  linked_data   An array of {@link Wordlift_Sparql_Tuple_Rendition}.
94
+     * }
95
+     * @since 3.7.0
96
+     *
97
+     * @since 3.20.0 This function will **not** return entity types introduced with 3.20.0.
98
+     *
99
+     * @since 3.18.0 The cases are the following:
100
+     *  1. the post has a term from the Entity Types Taxonomy: the term defines
101
+     *     the entity type, e.g. Organization, Person, ...
102
+     *  2. the post doesn't have a term from the Entity Types Taxonomy:
103
+     *      a) the post is a `wl_entity` custom post type, then the post is
104
+     *           assigned the `Thing` entity type by default.
105
+     *      b) the post is a `post` post type, then the post is
106
+     *           assigned the `Article` entity type by default.
107
+     *      c) the post is a custom post type then it is
108
+     *          assigned the `WebPage` entity type by default.
109
+     *
110
+     */
111
+    public function get( $post_id ) {
112
+
113
+        $this->log->trace( "Getting the post type for post $post_id..." );
114
+
115
+        // Get the post type.
116
+        $post_type = get_post_type( $post_id );
117
+
118
+        // Return `web-page` for non entities.
119
+        if ( ! self::is_valid_entity_post_type( $post_type ) ) {
120
+            $this->log->info( "Returning `web-page` for post $post_id." );
121
+
122
+            return $this->schema_service->get_schema( 'web-page' );
123
+        }
124
+
125
+        // Get the type from the associated classification.
126
+        $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
127
+
128
+        // Return the schema type if there is a term found.
129
+        if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
130
+            // Cycle through the terms and return the first one with a valid schema.
131
+            foreach ( $terms as $term ) {
132
+                $this->log->debug( "Found `{$term->slug}` term for post $post_id." );
133
+
134
+                // Try to get the schema for the term.
135
+                $schema = $this->schema_service->get_schema( $term->slug );
136
+
137
+                // If found, return it, ignoring the other types.
138
+                if ( null !== $schema ) {
139
+                    // Return the entity type with the specified id.
140
+                    return $schema;
141
+                }
142
+            }
143
+
144
+            /*
145 145
 			 * When a schema isn't found, we return `thing`. Schema may not be found because
146 146
 			 * the new schema classes that we support since #852 aren't configured in the schema
147 147
 			 * service.
@@ -151,88 +151,88 @@  discard block
 block discarded – undo
151 151
 			 * @since 3.20.0
152 152
 			 */
153 153
 
154
-			return $this->schema_service->get_schema( 'thing' );
155
-		}
156
-
157
-		// If it's a page or post return `Article`.
158
-		if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
159
-			$this->log->debug( "Post $post_id has no terms, and it's a `post` type, returning `Article`." );
160
-
161
-			// Return "Article" schema type for posts.
162
-			return $this->schema_service->get_schema( 'article' );
163
-		}
164
-
165
-		// Return "Thing" schema type for entities.
166
-		$this->log->debug( "Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`." );
167
-
168
-		// Return the entity type with the specified id.
169
-		return $this->schema_service->get_schema( 'thing' );
170
-
171
-	}
172
-
173
-	/**
174
-	 * Get the term ids of the entity types associated to the specified post.
175
-	 *
176
-	 * @param int $post_id The post id.
177
-	 *
178
-	 * @return array|WP_Error An array of entity types ids or a {@link WP_Error}.
179
-	 * @since 3.20.0
180
-	 *
181
-	 */
182
-	public function get_ids( $post_id ) {
183
-
184
-		return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'fields' => 'ids', ) );
185
-	}
186
-
187
-	/**
188
-	 * Get the camel case names of the entity types associated to the specified post.
189
-	 *
190
-	 * @param int $post_id The post id.
191
-	 *
192
-	 * @return array|WP_Error An array of entity types camel case names or a {@link WP_Error}.
193
-	 * @since 3.20.0
194
-	 *
195
-	 */
196
-	public function get_names( $post_id ) {
197
-
198
-		$ids = $this->get_ids( $post_id );
199
-
200
-		return array_map( function ( $id ) {
201
-			return get_term_meta( $id, '_wl_name', true );
202
-		}, $ids );
203
-	}
204
-
205
-	/**
206
-	 * Set the main type for the specified entity post, given the type URI.
207
-	 *
208
-	 * @param int $post_id The post id.
209
-	 * @param string $type_uri The type URI.
210
-	 * @param bool $replace Whether the provided type must replace the existing types, by default `true`.
211
-	 *
212
-	 * @since 3.8.0
213
-	 *
214
-	 */
215
-	public function set( $post_id, $type_uri, $replace = true ) {
216
-
217
-		// If the type URI is empty we remove the type.
218
-		if ( empty( $type_uri ) ) {
219
-			$this->log->debug( "Removing entity type for post $post_id..." );
220
-
221
-			wp_set_object_terms( $post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
222
-
223
-			return;
224
-		}
225
-
226
-		$this->log->debug( "Setting entity type for post $post_id..." );
227
-
228
-		// if the `$type_uri` starts with `wl-`, we're looking at the class name, which is `wl-` + slug.
229
-		$term = ( 0 === strpos( $type_uri, 'wl-' ) )
230
-			// Get term by slug.
231
-			? $this->get_term_by_slug( substr( $type_uri, 3 ) )
232
-			// Get term by URI.
233
-			: $this->get_term_by_uri( $type_uri );
234
-
235
-		/*
154
+            return $this->schema_service->get_schema( 'thing' );
155
+        }
156
+
157
+        // If it's a page or post return `Article`.
158
+        if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
159
+            $this->log->debug( "Post $post_id has no terms, and it's a `post` type, returning `Article`." );
160
+
161
+            // Return "Article" schema type for posts.
162
+            return $this->schema_service->get_schema( 'article' );
163
+        }
164
+
165
+        // Return "Thing" schema type for entities.
166
+        $this->log->debug( "Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`." );
167
+
168
+        // Return the entity type with the specified id.
169
+        return $this->schema_service->get_schema( 'thing' );
170
+
171
+    }
172
+
173
+    /**
174
+     * Get the term ids of the entity types associated to the specified post.
175
+     *
176
+     * @param int $post_id The post id.
177
+     *
178
+     * @return array|WP_Error An array of entity types ids or a {@link WP_Error}.
179
+     * @since 3.20.0
180
+     *
181
+     */
182
+    public function get_ids( $post_id ) {
183
+
184
+        return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'fields' => 'ids', ) );
185
+    }
186
+
187
+    /**
188
+     * Get the camel case names of the entity types associated to the specified post.
189
+     *
190
+     * @param int $post_id The post id.
191
+     *
192
+     * @return array|WP_Error An array of entity types camel case names or a {@link WP_Error}.
193
+     * @since 3.20.0
194
+     *
195
+     */
196
+    public function get_names( $post_id ) {
197
+
198
+        $ids = $this->get_ids( $post_id );
199
+
200
+        return array_map( function ( $id ) {
201
+            return get_term_meta( $id, '_wl_name', true );
202
+        }, $ids );
203
+    }
204
+
205
+    /**
206
+     * Set the main type for the specified entity post, given the type URI.
207
+     *
208
+     * @param int $post_id The post id.
209
+     * @param string $type_uri The type URI.
210
+     * @param bool $replace Whether the provided type must replace the existing types, by default `true`.
211
+     *
212
+     * @since 3.8.0
213
+     *
214
+     */
215
+    public function set( $post_id, $type_uri, $replace = true ) {
216
+
217
+        // If the type URI is empty we remove the type.
218
+        if ( empty( $type_uri ) ) {
219
+            $this->log->debug( "Removing entity type for post $post_id..." );
220
+
221
+            wp_set_object_terms( $post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
222
+
223
+            return;
224
+        }
225
+
226
+        $this->log->debug( "Setting entity type for post $post_id..." );
227
+
228
+        // if the `$type_uri` starts with `wl-`, we're looking at the class name, which is `wl-` + slug.
229
+        $term = ( 0 === strpos( $type_uri, 'wl-' ) )
230
+            // Get term by slug.
231
+            ? $this->get_term_by_slug( substr( $type_uri, 3 ) )
232
+            // Get term by URI.
233
+            : $this->get_term_by_uri( $type_uri );
234
+
235
+        /*
236 236
 		 * We always want to assign a type to an entity otherwise it won't show in the Vocabulary and it won't be
237 237
 		 * connected to Articles via mentions. We realized that the client JS code is passing `wl-other` when the
238 238
 		 * entity type isn't "notable". In which case we couldn't find an entity type.
@@ -243,174 +243,174 @@  discard block
 block discarded – undo
243 243
 		 *
244 244
 		 * @since 3.23.4
245 245
 		 */
246
-		if ( false === $term ) {
247
-			$this->log->warn( "No term found for URI $type_uri, will use Thing." );
248
-
249
-			$term = $this->get_term_by_slug( 'thing' );
250
-
251
-			// We still need to be able to bali out here, for example WordPress 5.1 tests create posts before our taxonomy
252
-			// is installed.
253
-			if ( false === $term ) {
254
-				return;
255
-			}
256
-		}
257
-
258
-		$this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..." );
259
-
260
-		// `$replace` is passed to decide whether to replace or append the term.
261
-		wp_set_object_terms( $post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace );
262
-
263
-	}
264
-
265
-	/**
266
-	 * Get an entity type term given its slug.
267
-	 *
268
-	 * @param string $slug The slug.
269
-	 *
270
-	 * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
271
-	 *                             or `$term` was not found.
272
-	 * @since 3.20.0
273
-	 *
274
-	 */
275
-	private function get_term_by_slug( $slug ) {
276
-
277
-		return get_term_by( 'slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
278
-	}
279
-
280
-	/**
281
-	 * Get an entity type term given its URI.
282
-	 *
283
-	 * @param string $uri The uri.
284
-	 *
285
-	 * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
286
-	 *                             or `$term` was not found.
287
-	 * @since 3.20.0
288
-	 *
289
-	 */
290
-	public function get_term_by_uri( $uri ) {
291
-
292
-		$terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
293
-			'fields'     => 'all',
294
-			'get'        => 'all',
295
-			'number'     => 1,
296
-			'meta_query' => array(
297
-				array(
298
-					// Don't use a reference to Wordlift_Schemaorg_Class_Service, unless
299
-					// `WL_ALL_ENTITY_TYPES` is set to true.
300
-					'key'   => '_wl_uri',
301
-					'value' => $uri,
302
-				),
303
-			),
304
-			'orderby'    => 'term_id',
305
-			'order'      => 'ASC',
306
-		) );
307
-
308
-		return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : false;
309
-	}
310
-
311
-	/**
312
-	 * Check whether an entity type is set for the {@link WP_Post} with the
313
-	 * specified id.
314
-	 *
315
-	 * @param int $post_id The {@link WP_Post}'s `id`.
316
-	 * @param string $uri The entity type URI.
317
-	 *
318
-	 * @return bool True if an entity type is set otherwise false.
319
-	 * @since 3.15.0
320
-	 *
321
-	 */
322
-	public function has_entity_type( $post_id, $uri = null ) {
323
-
324
-		$this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." );
325
-
326
-		// If an URI hasn't been specified just check whether we have at least
327
-		// one entity type.
328
-		if ( null === $uri ) {
329
-
330
-			// Get the post terms for the specified post ID.
331
-			$terms = $this->get_post_terms( $post_id );
332
-
333
-			$this->log->debug( "Post $post_id has " . count( $terms ) . ' type(s).' );
334
-
335
-			// True if there's at least one term bound to the post.
336
-			return ( 0 < count( $terms ) );
337
-		}
338
-
339
-		$has_entity_type = ( null !== $this->has_post_term_by_uri( $post_id, $uri ) );
340
-
341
-		$this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) );
342
-
343
-		// Check whether the post has an entity type with that URI.
344
-		return $has_entity_type;
345
-	}
346
-
347
-	/**
348
-	 * Get the list of entity types' terms for the specified {@link WP_Post}.
349
-	 *
350
-	 * @param int $post_id The {@link WP_Post} id.
351
-	 *
352
-	 * @return array|WP_Error An array of entity types' terms or {@link WP_Error}.
353
-	 * @since 3.15.0
354
-	 *
355
-	 */
356
-	private function get_post_terms( $post_id ) {
357
-
358
-		return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
359
-			'hide_empty' => false,
360
-			// Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'.
361
-			// An issue has been opened with the AAM plugin author as well.
362
-			//
363
-			// see https://github.com/insideout10/wordlift-plugin/issues/334
364
-			// see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863
365
-			'fields'     => 'all',
366
-		) );
367
-	}
368
-
369
-	/**
370
-	 * Get an entity type term given its URI.
371
-	 *
372
-	 * @param int $post_id The {@link WP_Post} id.
373
-	 * @param string $uri The entity type URI.
374
-	 *
375
-	 * @return bool True if the post has that type URI bound to it otherwise false.
376
-	 * @since 3.15.0
377
-	 *
378
-	 * @since 3.20.0 function renamed to `has_post_term_by_uri` and return type changed to `bool`.
379
-	 */
380
-	private function has_post_term_by_uri( $post_id, $uri ) {
381
-
382
-		// Get the post terms bound to the specified post.
383
-		$terms = $this->get_post_terms( $post_id );
384
-
385
-		// Look for a term if the specified URI.
386
-		foreach ( $terms as $term ) {
387
-			$term_uri = get_term_meta( $term->term_id, '_wl_uri', true );
388
-
389
-			if ( $uri === $term_uri ) {
390
-				return true;
391
-			}
392
-		}
393
-
394
-		// Return null.
395
-		return false;
396
-	}
397
-
398
-
399
-	/**
400
-	 * Determines whether a post type can be used for entities.
401
-	 *
402
-	 * Criteria is that the post type is public. The list of valid post types
403
-	 * can be overridden with a filter.
404
-	 *
405
-	 * @param string $post_type A post type name.
406
-	 *
407
-	 * @return bool Return true if the post type can be used for entities, otherwise false.
408
-	 * @since 3.15.0
409
-	 *
410
-	 */
411
-	public static function is_valid_entity_post_type( $post_type ) {
412
-
413
-		return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true );
414
-	}
246
+        if ( false === $term ) {
247
+            $this->log->warn( "No term found for URI $type_uri, will use Thing." );
248
+
249
+            $term = $this->get_term_by_slug( 'thing' );
250
+
251
+            // We still need to be able to bali out here, for example WordPress 5.1 tests create posts before our taxonomy
252
+            // is installed.
253
+            if ( false === $term ) {
254
+                return;
255
+            }
256
+        }
257
+
258
+        $this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..." );
259
+
260
+        // `$replace` is passed to decide whether to replace or append the term.
261
+        wp_set_object_terms( $post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace );
262
+
263
+    }
264
+
265
+    /**
266
+     * Get an entity type term given its slug.
267
+     *
268
+     * @param string $slug The slug.
269
+     *
270
+     * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
271
+     *                             or `$term` was not found.
272
+     * @since 3.20.0
273
+     *
274
+     */
275
+    private function get_term_by_slug( $slug ) {
276
+
277
+        return get_term_by( 'slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
278
+    }
279
+
280
+    /**
281
+     * Get an entity type term given its URI.
282
+     *
283
+     * @param string $uri The uri.
284
+     *
285
+     * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
286
+     *                             or `$term` was not found.
287
+     * @since 3.20.0
288
+     *
289
+     */
290
+    public function get_term_by_uri( $uri ) {
291
+
292
+        $terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
293
+            'fields'     => 'all',
294
+            'get'        => 'all',
295
+            'number'     => 1,
296
+            'meta_query' => array(
297
+                array(
298
+                    // Don't use a reference to Wordlift_Schemaorg_Class_Service, unless
299
+                    // `WL_ALL_ENTITY_TYPES` is set to true.
300
+                    'key'   => '_wl_uri',
301
+                    'value' => $uri,
302
+                ),
303
+            ),
304
+            'orderby'    => 'term_id',
305
+            'order'      => 'ASC',
306
+        ) );
307
+
308
+        return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : false;
309
+    }
310
+
311
+    /**
312
+     * Check whether an entity type is set for the {@link WP_Post} with the
313
+     * specified id.
314
+     *
315
+     * @param int $post_id The {@link WP_Post}'s `id`.
316
+     * @param string $uri The entity type URI.
317
+     *
318
+     * @return bool True if an entity type is set otherwise false.
319
+     * @since 3.15.0
320
+     *
321
+     */
322
+    public function has_entity_type( $post_id, $uri = null ) {
323
+
324
+        $this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." );
325
+
326
+        // If an URI hasn't been specified just check whether we have at least
327
+        // one entity type.
328
+        if ( null === $uri ) {
329
+
330
+            // Get the post terms for the specified post ID.
331
+            $terms = $this->get_post_terms( $post_id );
332
+
333
+            $this->log->debug( "Post $post_id has " . count( $terms ) . ' type(s).' );
334
+
335
+            // True if there's at least one term bound to the post.
336
+            return ( 0 < count( $terms ) );
337
+        }
338
+
339
+        $has_entity_type = ( null !== $this->has_post_term_by_uri( $post_id, $uri ) );
340
+
341
+        $this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) );
342
+
343
+        // Check whether the post has an entity type with that URI.
344
+        return $has_entity_type;
345
+    }
346
+
347
+    /**
348
+     * Get the list of entity types' terms for the specified {@link WP_Post}.
349
+     *
350
+     * @param int $post_id The {@link WP_Post} id.
351
+     *
352
+     * @return array|WP_Error An array of entity types' terms or {@link WP_Error}.
353
+     * @since 3.15.0
354
+     *
355
+     */
356
+    private function get_post_terms( $post_id ) {
357
+
358
+        return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
359
+            'hide_empty' => false,
360
+            // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'.
361
+            // An issue has been opened with the AAM plugin author as well.
362
+            //
363
+            // see https://github.com/insideout10/wordlift-plugin/issues/334
364
+            // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863
365
+            'fields'     => 'all',
366
+        ) );
367
+    }
368
+
369
+    /**
370
+     * Get an entity type term given its URI.
371
+     *
372
+     * @param int $post_id The {@link WP_Post} id.
373
+     * @param string $uri The entity type URI.
374
+     *
375
+     * @return bool True if the post has that type URI bound to it otherwise false.
376
+     * @since 3.15.0
377
+     *
378
+     * @since 3.20.0 function renamed to `has_post_term_by_uri` and return type changed to `bool`.
379
+     */
380
+    private function has_post_term_by_uri( $post_id, $uri ) {
381
+
382
+        // Get the post terms bound to the specified post.
383
+        $terms = $this->get_post_terms( $post_id );
384
+
385
+        // Look for a term if the specified URI.
386
+        foreach ( $terms as $term ) {
387
+            $term_uri = get_term_meta( $term->term_id, '_wl_uri', true );
388
+
389
+            if ( $uri === $term_uri ) {
390
+                return true;
391
+            }
392
+        }
393
+
394
+        // Return null.
395
+        return false;
396
+    }
397
+
398
+
399
+    /**
400
+     * Determines whether a post type can be used for entities.
401
+     *
402
+     * Criteria is that the post type is public. The list of valid post types
403
+     * can be overridden with a filter.
404
+     *
405
+     * @param string $post_type A post type name.
406
+     *
407
+     * @return bool Return true if the post type can be used for entities, otherwise false.
408
+     * @since 3.15.0
409
+     *
410
+     */
411
+    public static function is_valid_entity_post_type( $post_type ) {
412
+
413
+        return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true );
414
+    }
415 415
 
416 416
 }
Please login to merge, or discard this patch.
Spacing   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -54,9 +54,9 @@  discard block
 block discarded – undo
54 54
 	 * @since 3.7.0
55 55
 	 *
56 56
 	 */
57
-	public function __construct( $schema_service ) {
57
+	public function __construct($schema_service) {
58 58
 
59
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' );
59
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Type_Service');
60 60
 
61 61
 		$this->schema_service = $schema_service;
62 62
 
@@ -108,34 +108,34 @@  discard block
 block discarded – undo
108 108
 	 *          assigned the `WebPage` entity type by default.
109 109
 	 *
110 110
 	 */
111
-	public function get( $post_id ) {
111
+	public function get($post_id) {
112 112
 
113
-		$this->log->trace( "Getting the post type for post $post_id..." );
113
+		$this->log->trace("Getting the post type for post $post_id...");
114 114
 
115 115
 		// Get the post type.
116
-		$post_type = get_post_type( $post_id );
116
+		$post_type = get_post_type($post_id);
117 117
 
118 118
 		// Return `web-page` for non entities.
119
-		if ( ! self::is_valid_entity_post_type( $post_type ) ) {
120
-			$this->log->info( "Returning `web-page` for post $post_id." );
119
+		if ( ! self::is_valid_entity_post_type($post_type)) {
120
+			$this->log->info("Returning `web-page` for post $post_id.");
121 121
 
122
-			return $this->schema_service->get_schema( 'web-page' );
122
+			return $this->schema_service->get_schema('web-page');
123 123
 		}
124 124
 
125 125
 		// Get the type from the associated classification.
126
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
126
+		$terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
127 127
 
128 128
 		// Return the schema type if there is a term found.
129
-		if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
129
+		if ( ! is_wp_error($terms) && ! empty($terms)) {
130 130
 			// Cycle through the terms and return the first one with a valid schema.
131
-			foreach ( $terms as $term ) {
132
-				$this->log->debug( "Found `{$term->slug}` term for post $post_id." );
131
+			foreach ($terms as $term) {
132
+				$this->log->debug("Found `{$term->slug}` term for post $post_id.");
133 133
 
134 134
 				// Try to get the schema for the term.
135
-				$schema = $this->schema_service->get_schema( $term->slug );
135
+				$schema = $this->schema_service->get_schema($term->slug);
136 136
 
137 137
 				// If found, return it, ignoring the other types.
138
-				if ( null !== $schema ) {
138
+				if (null !== $schema) {
139 139
 					// Return the entity type with the specified id.
140 140
 					return $schema;
141 141
 				}
@@ -151,22 +151,22 @@  discard block
 block discarded – undo
151 151
 			 * @since 3.20.0
152 152
 			 */
153 153
 
154
-			return $this->schema_service->get_schema( 'thing' );
154
+			return $this->schema_service->get_schema('thing');
155 155
 		}
156 156
 
157 157
 		// If it's a page or post return `Article`.
158
-		if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
159
-			$this->log->debug( "Post $post_id has no terms, and it's a `post` type, returning `Article`." );
158
+		if (in_array($post_type, array('post', 'page'))) {
159
+			$this->log->debug("Post $post_id has no terms, and it's a `post` type, returning `Article`.");
160 160
 
161 161
 			// Return "Article" schema type for posts.
162
-			return $this->schema_service->get_schema( 'article' );
162
+			return $this->schema_service->get_schema('article');
163 163
 		}
164 164
 
165 165
 		// Return "Thing" schema type for entities.
166
-		$this->log->debug( "Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`." );
166
+		$this->log->debug("Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`.");
167 167
 
168 168
 		// Return the entity type with the specified id.
169
-		return $this->schema_service->get_schema( 'thing' );
169
+		return $this->schema_service->get_schema('thing');
170 170
 
171 171
 	}
172 172
 
@@ -179,9 +179,9 @@  discard block
 block discarded – undo
179 179
 	 * @since 3.20.0
180 180
 	 *
181 181
 	 */
182
-	public function get_ids( $post_id ) {
182
+	public function get_ids($post_id) {
183 183
 
184
-		return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'fields' => 'ids', ) );
184
+		return wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array('fields' => 'ids',));
185 185
 	}
186 186
 
187 187
 	/**
@@ -193,13 +193,13 @@  discard block
 block discarded – undo
193 193
 	 * @since 3.20.0
194 194
 	 *
195 195
 	 */
196
-	public function get_names( $post_id ) {
196
+	public function get_names($post_id) {
197 197
 
198
-		$ids = $this->get_ids( $post_id );
198
+		$ids = $this->get_ids($post_id);
199 199
 
200
-		return array_map( function ( $id ) {
201
-			return get_term_meta( $id, '_wl_name', true );
202
-		}, $ids );
200
+		return array_map(function($id) {
201
+			return get_term_meta($id, '_wl_name', true);
202
+		}, $ids);
203 203
 	}
204 204
 
205 205
 	/**
@@ -212,25 +212,25 @@  discard block
 block discarded – undo
212 212
 	 * @since 3.8.0
213 213
 	 *
214 214
 	 */
215
-	public function set( $post_id, $type_uri, $replace = true ) {
215
+	public function set($post_id, $type_uri, $replace = true) {
216 216
 
217 217
 		// If the type URI is empty we remove the type.
218
-		if ( empty( $type_uri ) ) {
219
-			$this->log->debug( "Removing entity type for post $post_id..." );
218
+		if (empty($type_uri)) {
219
+			$this->log->debug("Removing entity type for post $post_id...");
220 220
 
221
-			wp_set_object_terms( $post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
221
+			wp_set_object_terms($post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
222 222
 
223 223
 			return;
224 224
 		}
225 225
 
226
-		$this->log->debug( "Setting entity type for post $post_id..." );
226
+		$this->log->debug("Setting entity type for post $post_id...");
227 227
 
228 228
 		// if the `$type_uri` starts with `wl-`, we're looking at the class name, which is `wl-` + slug.
229
-		$term = ( 0 === strpos( $type_uri, 'wl-' ) )
229
+		$term = (0 === strpos($type_uri, 'wl-'))
230 230
 			// Get term by slug.
231
-			? $this->get_term_by_slug( substr( $type_uri, 3 ) )
231
+			? $this->get_term_by_slug(substr($type_uri, 3))
232 232
 			// Get term by URI.
233
-			: $this->get_term_by_uri( $type_uri );
233
+			: $this->get_term_by_uri($type_uri);
234 234
 
235 235
 		/*
236 236
 		 * We always want to assign a type to an entity otherwise it won't show in the Vocabulary and it won't be
@@ -243,22 +243,22 @@  discard block
 block discarded – undo
243 243
 		 *
244 244
 		 * @since 3.23.4
245 245
 		 */
246
-		if ( false === $term ) {
247
-			$this->log->warn( "No term found for URI $type_uri, will use Thing." );
246
+		if (false === $term) {
247
+			$this->log->warn("No term found for URI $type_uri, will use Thing.");
248 248
 
249
-			$term = $this->get_term_by_slug( 'thing' );
249
+			$term = $this->get_term_by_slug('thing');
250 250
 
251 251
 			// We still need to be able to bali out here, for example WordPress 5.1 tests create posts before our taxonomy
252 252
 			// is installed.
253
-			if ( false === $term ) {
253
+			if (false === $term) {
254 254
 				return;
255 255
 			}
256 256
 		}
257 257
 
258
-		$this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..." );
258
+		$this->log->debug("Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]...");
259 259
 
260 260
 		// `$replace` is passed to decide whether to replace or append the term.
261
-		wp_set_object_terms( $post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace );
261
+		wp_set_object_terms($post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace);
262 262
 
263 263
 	}
264 264
 
@@ -272,9 +272,9 @@  discard block
 block discarded – undo
272 272
 	 * @since 3.20.0
273 273
 	 *
274 274
 	 */
275
-	private function get_term_by_slug( $slug ) {
275
+	private function get_term_by_slug($slug) {
276 276
 
277
-		return get_term_by( 'slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
277
+		return get_term_by('slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
278 278
 	}
279 279
 
280 280
 	/**
@@ -287,9 +287,9 @@  discard block
 block discarded – undo
287 287
 	 * @since 3.20.0
288 288
 	 *
289 289
 	 */
290
-	public function get_term_by_uri( $uri ) {
290
+	public function get_term_by_uri($uri) {
291 291
 
292
-		$terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
292
+		$terms = get_terms(Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
293 293
 			'fields'     => 'all',
294 294
 			'get'        => 'all',
295 295
 			'number'     => 1,
@@ -303,9 +303,9 @@  discard block
 block discarded – undo
303 303
 			),
304 304
 			'orderby'    => 'term_id',
305 305
 			'order'      => 'ASC',
306
-		) );
306
+		));
307 307
 
308
-		return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : false;
308
+		return is_array($terms) && ! empty($terms) ? $terms[0] : false;
309 309
 	}
310 310
 
311 311
 	/**
@@ -319,26 +319,26 @@  discard block
 block discarded – undo
319 319
 	 * @since 3.15.0
320 320
 	 *
321 321
 	 */
322
-	public function has_entity_type( $post_id, $uri = null ) {
322
+	public function has_entity_type($post_id, $uri = null) {
323 323
 
324
-		$this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." );
324
+		$this->log->debug("Checking if post $post_id has an entity type [ $uri ]...");
325 325
 
326 326
 		// If an URI hasn't been specified just check whether we have at least
327 327
 		// one entity type.
328
-		if ( null === $uri ) {
328
+		if (null === $uri) {
329 329
 
330 330
 			// Get the post terms for the specified post ID.
331
-			$terms = $this->get_post_terms( $post_id );
331
+			$terms = $this->get_post_terms($post_id);
332 332
 
333
-			$this->log->debug( "Post $post_id has " . count( $terms ) . ' type(s).' );
333
+			$this->log->debug("Post $post_id has ".count($terms).' type(s).');
334 334
 
335 335
 			// True if there's at least one term bound to the post.
336
-			return ( 0 < count( $terms ) );
336
+			return (0 < count($terms));
337 337
 		}
338 338
 
339
-		$has_entity_type = ( null !== $this->has_post_term_by_uri( $post_id, $uri ) );
339
+		$has_entity_type = (null !== $this->has_post_term_by_uri($post_id, $uri));
340 340
 
341
-		$this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) );
341
+		$this->log->debug("Post $post_id has $uri type: ".($has_entity_type ? 'yes' : 'no'));
342 342
 
343 343
 		// Check whether the post has an entity type with that URI.
344 344
 		return $has_entity_type;
@@ -353,9 +353,9 @@  discard block
 block discarded – undo
353 353
 	 * @since 3.15.0
354 354
 	 *
355 355
 	 */
356
-	private function get_post_terms( $post_id ) {
356
+	private function get_post_terms($post_id) {
357 357
 
358
-		return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
358
+		return wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
359 359
 			'hide_empty' => false,
360 360
 			// Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'.
361 361
 			// An issue has been opened with the AAM plugin author as well.
@@ -363,7 +363,7 @@  discard block
 block discarded – undo
363 363
 			// see https://github.com/insideout10/wordlift-plugin/issues/334
364 364
 			// see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863
365 365
 			'fields'     => 'all',
366
-		) );
366
+		));
367 367
 	}
368 368
 
369 369
 	/**
@@ -377,16 +377,16 @@  discard block
 block discarded – undo
377 377
 	 *
378 378
 	 * @since 3.20.0 function renamed to `has_post_term_by_uri` and return type changed to `bool`.
379 379
 	 */
380
-	private function has_post_term_by_uri( $post_id, $uri ) {
380
+	private function has_post_term_by_uri($post_id, $uri) {
381 381
 
382 382
 		// Get the post terms bound to the specified post.
383
-		$terms = $this->get_post_terms( $post_id );
383
+		$terms = $this->get_post_terms($post_id);
384 384
 
385 385
 		// Look for a term if the specified URI.
386
-		foreach ( $terms as $term ) {
387
-			$term_uri = get_term_meta( $term->term_id, '_wl_uri', true );
386
+		foreach ($terms as $term) {
387
+			$term_uri = get_term_meta($term->term_id, '_wl_uri', true);
388 388
 
389
-			if ( $uri === $term_uri ) {
389
+			if ($uri === $term_uri) {
390 390
 				return true;
391 391
 			}
392 392
 		}
@@ -408,9 +408,9 @@  discard block
 block discarded – undo
408 408
 	 * @since 3.15.0
409 409
 	 *
410 410
 	 */
411
-	public static function is_valid_entity_post_type( $post_type ) {
411
+	public static function is_valid_entity_post_type($post_type) {
412 412
 
413
-		return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true );
413
+		return in_array($post_type, Wordlift_Entity_Service::valid_entity_post_types(), true);
414 414
 	}
415 415
 
416 416
 }
Please login to merge, or discard this patch.