|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Metaboxes: URI Field Metabox. |
|
4
|
|
|
* |
|
5
|
|
|
* @since 3.0.0 |
|
6
|
|
|
* @package Wordlift |
|
7
|
|
|
* @subpackage Wordlift/admin/WL_Metabox |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Define the {@link WL_Metabox_Field_uri} class. |
|
12
|
|
|
* |
|
13
|
|
|
* @since 3.0.0 |
|
14
|
|
|
* @package Wordlift |
|
15
|
|
|
* @subpackage Wordlift/admin/WL_Metabox |
|
16
|
|
|
*/ |
|
17
|
|
|
class WL_Metabox_Field_uri extends WL_Metabox_Field { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Only accept URIs or local entity IDs. |
|
21
|
|
|
* Build new entity if the user inputted a name that is not present in DB. |
|
22
|
|
|
* |
|
23
|
|
|
* @param mixed $value The value to sanitize. |
|
24
|
|
|
* |
|
25
|
|
|
* @return int|mixed|WP_Error |
|
26
|
|
|
*/ |
|
27
|
|
|
public function sanitize_data_filter( $value ) { |
|
28
|
|
|
|
|
29
|
|
|
if ( empty( $value ) ) { |
|
30
|
|
|
return null; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
// Check that the inserted URI, ID or name does not point to a saved |
|
34
|
|
|
// entity or when the editor types a string in the input box, we try to |
|
35
|
|
|
// find an entity with that title and, if not found, we create that entity. |
|
36
|
|
|
$absent_from_db = is_numeric( $value ) |
|
37
|
|
|
? is_null( get_post( $value ) ) |
|
38
|
|
|
: ! $this->exists( $value ); |
|
39
|
|
|
|
|
40
|
|
|
// Is it an URI? |
|
41
|
|
|
$name_is_uri = strpos( $value, 'http' ) === 0; |
|
42
|
|
|
|
|
43
|
|
|
// We create a new entity only if the entity is not present in the DB. |
|
44
|
|
|
// In the case of an external uri, we just save the uri. |
|
45
|
|
|
if ( $absent_from_db && ! $name_is_uri ) { |
|
46
|
|
|
|
|
47
|
|
|
// ...we create a new entity! |
|
48
|
|
|
$new_entity_id = wp_insert_post( array( |
|
49
|
|
|
'post_status' => 'publish', |
|
50
|
|
|
'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
51
|
|
|
'post_title' => $value, |
|
52
|
|
|
) ); |
|
53
|
|
|
|
|
54
|
|
|
$type = 'http://schema.org/' . ( isset( $this->expected_uri_type ) ? $this->expected_uri_type[0] : 'Thing' ); |
|
55
|
|
|
|
|
56
|
|
|
wl_set_entity_main_type( $new_entity_id, $type ); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
// Build uri for this entity. |
|
59
|
|
|
$new_uri = wl_build_entity_uri( $new_entity_id ); |
|
60
|
|
|
wl_set_entity_uri( $new_entity_id, $new_uri ); |
|
61
|
|
|
|
|
62
|
|
|
Wordlift_Linked_Data_Service::get_instance()->push( $new_entity_id ); |
|
63
|
|
|
|
|
64
|
|
|
// Update the value that will be saved as meta. |
|
65
|
|
|
$value = $new_entity_id; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $value; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Check whether an entity exists given a value. |
|
73
|
|
|
* |
|
74
|
|
|
* @since 3.15.0 |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $value An entity URI or a title string.. |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool True if the entity exists otherwise false. |
|
79
|
|
|
*/ |
|
80
|
|
|
private function exists( $value ) { |
|
81
|
|
|
|
|
82
|
|
|
// When the editor types a string in the input box, we try to find |
|
83
|
|
|
// an entity with that title and, if not found, we create that entity. |
|
84
|
|
|
$entity_service = Wordlift_Entity_Service::get_instance(); |
|
85
|
|
|
|
|
86
|
|
|
// Try looking for an entity by URI. |
|
87
|
|
|
$found_by_uri = null !== $entity_service->get_entity_post_by_uri( $value ); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
// Return true if found. |
|
90
|
|
|
if ( $found_by_uri ) { |
|
91
|
|
|
$this->log->debug( "Found entity for $value." ); |
|
92
|
|
|
|
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Try looking for an entity by title, get any potential candidate. |
|
97
|
|
|
$candidate = get_page_by_title( $value, OBJECT, Wordlift_Entity_Service::valid_entity_post_types() ); |
|
98
|
|
|
|
|
99
|
|
|
// If a candidate has been found and it's an entity. |
|
100
|
|
|
return null !== $candidate && $entity_service->is_entity( $candidate->ID ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @inheritdoc |
|
105
|
|
|
*/ |
|
106
|
|
|
public function html_wrapper_open() { |
|
107
|
|
|
|
|
108
|
|
|
// The containing <div> contains info on cardinality and expected types. |
|
109
|
|
|
$html = "<div class='wl-field' data-cardinality='$this->cardinality'"; |
|
110
|
|
|
|
|
111
|
|
|
if ( isset( $this->expected_uri_type ) && ! is_null( $this->expected_uri_type ) ) { |
|
112
|
|
|
|
|
113
|
|
|
if ( is_array( $this->expected_uri_type ) ) { |
|
114
|
|
|
$html .= " data-expected-types='" . implode( ',', $this->expected_uri_type ) . "'"; |
|
115
|
|
|
} else { |
|
116
|
|
|
$html .= " data-expected-types='$this->expected_uri_type'"; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$html .= '>'; |
|
121
|
|
|
|
|
122
|
|
|
return $html; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @inheritdoc |
|
127
|
|
|
*/ |
|
128
|
|
|
public function html_input( $default_entity_identifier ) { |
|
129
|
|
|
|
|
130
|
|
|
if ( empty( $default_entity_identifier ) ) { |
|
131
|
|
|
$entity = null; |
|
132
|
|
|
} elseif ( is_numeric( $default_entity_identifier ) ) { |
|
133
|
|
|
$entity = get_post( $default_entity_identifier ); |
|
134
|
|
|
} else { |
|
135
|
|
|
// @todo: we cannot be so sure this is a URI. |
|
136
|
|
|
// It is an URI |
|
137
|
|
|
$entity = Wordlift_Entity_Service |
|
|
|
|
|
|
138
|
|
|
::get_instance() |
|
139
|
|
|
->get_entity_post_by_uri( $default_entity_identifier ); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if ( ! is_null( $entity ) ) { |
|
143
|
|
|
$label = $entity->post_title; |
|
144
|
|
|
$value = $entity->ID; |
|
145
|
|
|
} else { |
|
146
|
|
|
// No ID and no internal uri. Just leave as is. |
|
147
|
|
|
$label = $default_entity_identifier; |
|
148
|
|
|
$value = $default_entity_identifier; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// Write saved value in page |
|
152
|
|
|
// The <input> tags host the meta value. |
|
153
|
|
|
// The visible <input> has the human readable value (i.e. entity name or uri) |
|
154
|
|
|
// and is accompained by an hidden <input> tag, passed to the server, |
|
155
|
|
|
// that contains the raw value (i.e. the uri or entity id). |
|
156
|
|
|
@ob_start(); |
|
|
|
|
|
|
157
|
|
|
?> |
|
158
|
|
|
<div class="wl-input-wrapper wl-autocomplete-wrapper"> |
|
159
|
|
|
<input |
|
160
|
|
|
type="text" |
|
161
|
|
|
class="<?php echo esc_attr( $this->meta_name ); ?> wl-autocomplete" |
|
162
|
|
|
value="<?php echo esc_attr( $label ); ?>" |
|
163
|
|
|
style="width:88%" |
|
164
|
|
|
/> |
|
165
|
|
|
<input |
|
166
|
|
|
type="hidden" |
|
167
|
|
|
class="<?php echo esc_attr( $this->meta_name ); ?>" |
|
168
|
|
|
name="wl_metaboxes[<?php echo $this->meta_name ?>][]" |
|
169
|
|
|
value="<?php echo esc_attr( $value ); ?>" |
|
170
|
|
|
/> |
|
171
|
|
|
|
|
172
|
|
|
<button class="button wl-remove-input wl-button" type="button"> |
|
173
|
|
|
<?php esc_html_e( 'Remove', 'wordlift' ); ?> |
|
174
|
|
|
</button> |
|
175
|
|
|
|
|
176
|
|
|
<div class="wl-input-notice"></div> |
|
177
|
|
|
</div> |
|
178
|
|
|
<?php |
|
179
|
|
|
$html = ob_get_clean(); |
|
180
|
|
|
|
|
181
|
|
|
return $html; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.