|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Provide entity-related services. |
|
5
|
|
|
* |
|
6
|
|
|
* @since 3.1.0 |
|
7
|
|
|
*/ |
|
8
|
|
|
class Wordlift_Entity_Service { |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* The Log service. |
|
12
|
|
|
* |
|
13
|
|
|
* @since 3.2.0 |
|
14
|
|
|
* @access private |
|
15
|
|
|
* @var \Wordlift_Log_Service $log_service The Log service. |
|
16
|
|
|
*/ |
|
17
|
|
|
private $log_service; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The UI service. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 3.2.0 |
|
23
|
|
|
* @access private |
|
24
|
|
|
* @var \Wordlift_UI_Service $ui_service The UI service. |
|
25
|
|
|
*/ |
|
26
|
|
|
private $ui_service; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The entity post type name. |
|
30
|
|
|
* |
|
31
|
|
|
* @since 3.1.0 |
|
32
|
|
|
*/ |
|
33
|
|
|
const TYPE_NAME = 'entity'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The alternate label meta key. |
|
37
|
|
|
* |
|
38
|
|
|
* @since 3.2.0 |
|
39
|
|
|
*/ |
|
40
|
|
|
const ALTERNATE_LABEL_META_KEY = '_wl_alt_label'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The alternative label input template. |
|
44
|
|
|
* |
|
45
|
|
|
* @since 3.2.0 |
|
46
|
|
|
*/ |
|
47
|
|
|
// TODO: this should be moved to a class that deals with HTML code. |
|
48
|
|
|
const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
49
|
|
|
<label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
|
50
|
|
|
<input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
|
51
|
|
|
<button class="button wl-delete-button">%s</button> |
|
52
|
|
|
</div>'; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* A singleton instance of the Entity service. |
|
56
|
|
|
* |
|
57
|
|
|
* @since 3.2.0 |
|
58
|
|
|
* @access private |
|
59
|
|
|
* @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
60
|
|
|
*/ |
|
61
|
|
|
private static $instance; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Create a Wordlift_Entity_Service instance. |
|
65
|
|
|
* |
|
66
|
|
|
* @since 3.2.0 |
|
67
|
|
|
* |
|
68
|
|
|
* @param \Wordlift_UI_Service $ui_service The UI service. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function __construct( $ui_service ) { |
|
71
|
|
|
|
|
72
|
|
|
$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
73
|
|
|
|
|
74
|
|
|
// Set the UI service. |
|
75
|
|
|
$this->ui_service = $ui_service; |
|
76
|
|
|
|
|
77
|
|
|
// Set the singleton instance. |
|
78
|
|
|
self::$instance = $this; |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the singleton instance of the Entity service. |
|
84
|
|
|
* |
|
85
|
|
|
* @since 3.2.0 |
|
86
|
|
|
* @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function get_instance() { |
|
89
|
|
|
|
|
90
|
|
|
return self::$instance; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to |
|
95
|
|
|
* its specific function). |
|
96
|
|
|
* |
|
97
|
|
|
* @since 3.1.0 |
|
98
|
|
|
* |
|
99
|
|
|
* @return array An array of post IDs. |
|
100
|
|
|
*/ |
|
101
|
|
|
public function get_all_related_to_last_50_published_posts() { |
|
102
|
|
|
|
|
103
|
|
|
// Global timeline. Get entities from the latest posts. |
|
104
|
|
|
$latest_posts_ids = get_posts( array( |
|
105
|
|
|
'numberposts' => 50, |
|
106
|
|
|
'fields' => 'ids', //only get post IDs |
|
107
|
|
|
'post_type' => 'post', |
|
108
|
|
|
'post_status' => 'publish' |
|
109
|
|
|
) ); |
|
110
|
|
|
|
|
111
|
|
|
if ( empty( $latest_posts_ids ) ) { |
|
112
|
|
|
// There are no posts. |
|
113
|
|
|
return array(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// Collect entities related to latest posts |
|
117
|
|
|
$entity_ids = array(); |
|
118
|
|
|
foreach ( $latest_posts_ids as $id ) { |
|
119
|
|
|
$entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
120
|
|
|
'status' => 'publish' |
|
121
|
|
|
) ) ); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $entity_ids; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Determines whether a post is an entity or not. |
|
129
|
|
|
* |
|
130
|
|
|
* @since 3.1.0 |
|
131
|
|
|
* |
|
132
|
|
|
* @param int $post_id A post id. |
|
133
|
|
|
* |
|
134
|
|
|
* @return true if the post is an entity otherwise false. |
|
135
|
|
|
*/ |
|
136
|
|
|
public function is_entity( $post_id ) { |
|
137
|
|
|
|
|
138
|
|
|
return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Fires once a post has been saved. |
|
143
|
|
|
* |
|
144
|
|
|
* @since 3.2.0 |
|
145
|
|
|
* |
|
146
|
|
|
* @param int $post_id Post ID. |
|
147
|
|
|
* @param WP_Post $post Post object. |
|
148
|
|
|
* @param bool $update Whether this is an existing post being updated or not. |
|
149
|
|
|
*/ |
|
150
|
|
|
public function save_post( $post_id, $post, $update ) { |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
// If it's not an entity, return. |
|
153
|
|
|
if ( ! $this->is_entity( $post_id ) ) { |
|
154
|
|
|
return; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// Get the alt labels from the request (or empty array). |
|
158
|
|
|
$alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
159
|
|
|
|
|
160
|
|
|
// Set the alternative labels. |
|
161
|
|
|
$this->set_alternative_labels( $post_id, $alt_labels ); |
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Set the alternative labels. |
|
167
|
|
|
* |
|
168
|
|
|
* @since 3.2.0 |
|
169
|
|
|
* |
|
170
|
|
|
* @param int $post_id The post id. |
|
171
|
|
|
* @param array $alt_labels An array of labels. |
|
172
|
|
|
*/ |
|
173
|
|
|
private function set_alternative_labels( $post_id, $alt_labels ) { |
|
174
|
|
|
|
|
175
|
|
|
$this->log_service->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
176
|
|
|
|
|
177
|
|
|
// Delete all the existing alternate labels. |
|
178
|
|
|
delete_post_meta( $post_id, self::ALTERNATE_LABEL_META_KEY ); |
|
179
|
|
|
|
|
180
|
|
|
// Set the alternative labels. |
|
181
|
|
|
foreach ( $alt_labels as $alt_label ) { |
|
182
|
|
|
if ( ! empty( $alt_label ) ) { |
|
183
|
|
|
add_post_meta( $post_id, self::ALTERNATE_LABEL_META_KEY, $alt_label ); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Retrieve the alternate labels. |
|
191
|
|
|
* |
|
192
|
|
|
* @since 3.2.0 |
|
193
|
|
|
* |
|
194
|
|
|
* @param int $post_id Post id. |
|
195
|
|
|
* |
|
196
|
|
|
* @return mixed An array of alternative labels. |
|
197
|
|
|
*/ |
|
198
|
|
|
public function get_alternate_labels( $post_id ) { |
|
199
|
|
|
|
|
200
|
|
|
return get_post_meta( $post_id, self::ALTERNATE_LABEL_META_KEY ); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
205
|
|
|
* |
|
206
|
|
|
* @since 3.2.0 |
|
207
|
|
|
* |
|
208
|
|
|
* @param WP_Post $post Post object. |
|
209
|
|
|
*/ |
|
210
|
|
|
public function edit_form_before_permalink( $post ) { |
|
211
|
|
|
|
|
212
|
|
|
// If it's not an entity, return. |
|
213
|
|
|
if ( ! $this->is_entity( $post->ID ) ) { |
|
214
|
|
|
return; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
// Print the input template. |
|
218
|
|
|
$this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
219
|
|
|
|
|
220
|
|
|
// Print all the currently set alternative labels. |
|
221
|
|
|
foreach ( $this->get_alternate_labels( $post->ID ) as $alt_label ) { |
|
222
|
|
|
|
|
223
|
|
|
echo $this->get_alternative_label_input( $alt_label ); |
|
224
|
|
|
|
|
225
|
|
|
}; |
|
226
|
|
|
|
|
227
|
|
|
// Print the button. |
|
228
|
|
|
$this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
229
|
|
|
|
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Get the alternative label input HTML code. |
|
234
|
|
|
* |
|
235
|
|
|
* @since 3.2.0 |
|
236
|
|
|
* |
|
237
|
|
|
* @param string $value The input value. |
|
238
|
|
|
* |
|
239
|
|
|
* @return string The input HTML code. |
|
240
|
|
|
*/ |
|
241
|
|
|
private function get_alternative_label_input( $value = '' ) { |
|
242
|
|
|
|
|
243
|
|
|
return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
} |
|
247
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.