/
wordlift_core_schema_api.php
163 lines (132 loc) · 4.81 KB
/
wordlift_core_schema_api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* Schema API functions.
*
* @since 3.0.0
* @package Wordlift
* @subpackage Wordlift/modules/core
*/
/**
* Retrieves the value of the specified property for the entity.
*
* @param int $post_id The numeric post ID.
* @param string $property_name Name of the property (e.g. name, for the http://schema.org/name property).
*
* @return array|null An array of values or NULL in case of no values (or error).
*/
function wl_schema_get_value( $post_id, $property_name ) {
// Property name must be defined.
if ( ! isset( $property_name ) || is_null( $property_name ) ) {
return null;
}
// store eventual schema name in different variable
$property_schema_name = wl_build_full_schema_uri_from_schema_slug( $property_name );
// Establish entity id.
if ( is_null( $post_id ) || ! is_numeric( $post_id ) ) {
$post_id = get_the_ID();
if ( is_null( $post_id ) || ! is_numeric( $post_id ) ) {
return null;
}
}
// Get custom fields.
$term_mapping = wl_entity_taxonomy_get_custom_fields( $post_id );
// Search for the required meta value (by constant name or schema name)
foreach ( $term_mapping as $wl_constant => $property_info ) {
$found_constant = ( $wl_constant == $property_name );
$found_predicate = ( isset( $property_info['predicate'] ) && $property_info['predicate'] == $property_schema_name );
if ( $found_constant || $found_predicate ) {
return get_post_meta( $post_id, $wl_constant );
}
}
return null;
}
/**
* Set the value for the specified property and post ID, deleting what was there before.
*
* @param int $post_id The numeric post ID.
* @param string $property_name Name of the property (e.g. name, for the http://schema.org/name property)
* @param mixed $property_value Value to save into the property.
*
* @return boolean The method returns true if everything went ok, an error string otherwise.
*/
function wl_schema_set_value( $post_id, $property_name, $property_value ) {
// Some checks on the parameters
if ( ! is_numeric( $post_id ) || is_null( $property_name ) || empty( $property_value ) || is_null( $property_value ) ) {
return false;
}
// Build full schema uri if necessary
$property_name = wl_build_full_schema_uri_from_schema_slug( $property_name );
// Get accepted properties
$accepted_fields = wl_entity_taxonomy_get_custom_fields( $post_id );
// Find the name of the custom-field managing the schema property
foreach ( $accepted_fields as $wl_constant => $field ) {
if ( $field['predicate'] == $property_name ) {
// Deal with single values
if ( ! is_array( $property_value ) ) {
$property_value = array( $property_value );
}
// Delete present meta
delete_post_meta( $post_id, $wl_constant );
foreach ( $property_value as $value ) {
add_post_meta( $post_id, $wl_constant, $value );
}
return true;
}
}
return false;
}
/**
* Retrieves the entity types for the specified post ID, where
*
* @param int $post_id The numeric post ID.
*
* @return array Array of type(s) (e.g. Type, for the http://schema.org/Type)
* or NULL in case of no values (or error).
*/
function wl_schema_get_types( $post_id ) {
// Some checks on the parameters
if ( ! is_numeric( $post_id ) ) {
return null;
}
$type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
if ( isset( $type['uri'] ) ) {
return array( $type['uri'] );
}
return null;
}
/**
* Sets the entity type(s) for the specified post ID. Support is now for only one type per entity.
*
* @param int $post_id The numeric post ID
* @param array $type_names An array of strings, each defining a type (e.g. Type, for the http://schema.org/Type)
*
* @return boolean True if everything went ok, an error string otherwise.
*/
function wl_schema_set_types( $post_id, $type_names ) {
// Some checks on the parameters
if ( ! is_numeric( $post_id ) || empty( $type_names ) || is_null( $type_names ) ) {
return null;
}
// TODO: support more than one type.
if ( is_array( $type_names ) ) {
$type_names = $type_names[0];
}
// Get the schema URI (e.g. http://schema.org/Thing)
$type_names = wl_build_full_schema_uri_from_schema_slug( $type_names );
// Actually sets the taxonomy type
wl_set_entity_main_type( $post_id, $type_names );
}
/**
* Build full schema uri starting from a slug. If the uri is already correct, nothing is done.
*
* @param string $schema_name Slug or full uri of a schema property or type (es. 'location' or 'http://schema.org/location')
*
* @return string The full schema uri (es. 'latitude' returns 'http://schema.org/latitude')
*/
function wl_build_full_schema_uri_from_schema_slug( $schema_name ) {
$schema_root_address = 'http://schema.org/';
if ( strpos( $schema_name, $schema_root_address ) === false ) { // === necessary
$schema_name = $schema_root_address . $schema_name;
}
return $schema_name;
}