/
output-generator.php
339 lines (279 loc) · 8.94 KB
/
output-generator.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* Generates the output
*
* @package Contextual_Related_Posts
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Returns the link attributes.
*
* @since 2.2.0
*
* @param array $args Array of arguments.
* @return string Space separated list of link attributes.
*/
function crp_link_attributes( $args ) {
$rel_attribute = ( $args['link_nofollow'] ) ? ' rel="nofollow" ' : ' ';
$target_attribute = ( $args['link_new_window'] ) ? ' target="_blank" ' : ' ';
$link_attributes = array(
'rel_attribute' => $rel_attribute,
'target_attribute' => $target_attribute,
);
/**
* Filter the title of the Related Posts list
*
* @since 2.2.0
*
* @param array $link_attributes Array of link attributes
* @param array $args Array of arguments
*/
$link_attributes = apply_filters( 'crp_link_attributes', $link_attributes, $args );
// Convert it to a string.
$link_attributes = implode( ' ', $link_attributes );
return $link_attributes;
}
/**
* Returns the heading of the related posts.
*
* @since 2.2.0
*
* @param array $args Array of arguments.
* @return string Space separated list of link attributes
*/
function crp_heading_title( $args ) {
global $post;
$title = '';
if ( $args['heading'] && ! $args['is_widget'] ) {
$title = str_replace( '%postname%', $post->post_title, $args['title'] ); // Replace %postname% with the title of the current post.
}
/**
* Filter the title of the Related Posts list
*
* @since 1.9
*
* @param string $title Title/heading of the Related Posts list
* @param array $args Array of arguments
*/
return apply_filters( 'crp_heading_title', $title, $args );
}
/**
* Returns the opening tag of the related posts list.
*
* @since 2.2.0
*
* @param array $args Array of arguments.
* @return string Space separated list of link attributes
*/
function crp_before_list( $args ) {
$before_list = $args['before_list'];
/**
* Filter the opening tag of the related posts list
*
* @since 1.9
*
* @param string $before_list Opening tag set in the Settings Page
* @param array $args Array of arguments
*/
return apply_filters( 'crp_before_list', $before_list, $args );
}
/**
* Returns the closing tag of the related posts list.
*
* @since 2.2.0
*
* @param array $args Array of arguments.
* @return string Space separated list of link attributes
*/
function crp_after_list( $args ) {
$after_list = $args['after_list'];
/**
* Filter the closing tag of the related posts list
*
* @since 1.9
*
* @param string $after_list Closing tag set in the Settings Page
* @param array $args Array of arguments
*/
return apply_filters( 'crp_after_list', $after_list, $args );
}
/**
* Returns the opening tag of each list item.
*
* @since 2.2.0
*
* @param array $args Array of arguments.
* @param object $result Object of the current post result.
* @return string Space separated list of link attributes
*/
function crp_before_list_item( $args, $result ) {
$before_list_item = $args['before_list_item'];
/**
* Filter the opening tag of each list item.
*
* @since 1.9
*
* @param string $before_list_item Tag before each list item. Can be defined in the Settings page.
* @param object $result Object of the current post result
* @param array $args Array of arguments
*/
return apply_filters( 'crp_before_list_item', $before_list_item, $result, $args );
}
/**
* Returns the closing tag of each list item.
*
* @since 2.2.0
*
* @param array $args Array of arguments.
* @param object $result Object of the current post result.
* @return string Space separated list of link attributes
*/
function crp_after_list_item( $args, $result ) {
$after_list_item = $args['after_list_item'];
/**
* Filter the closing tag of each list item.
*
* @since 1.9
*
* @param string $after_list_item Tag after each list item. Can be defined in the Settings page.
* @param object $result Object of the current post result
* @param array $args Array of arguments
*/
return apply_filters( 'crp_after_list_item', $after_list_item, $result, $args );
}
/**
* Returns the title of each list item.
*
* @since 2.2.0
*
* @param array $args Array of arguments.
* @param object $result Object of the current post result.
* @return string Space separated list of link attributes
*/
function crp_title( $args, $result ) {
$title = crp_trim_char( $result->post_title, $args['title_length'] ); // Get the post title and crop it if needed.
/**
* Filter the title of each list item.
*
* @since 1.9
*
* @param string $title Title of the post.
* @param object $result Object of the current post result
* @param array $args Array of arguments
*/
return apply_filters( 'crp_title', $title, $result, $args );
}
/**
* Returns the author of each list item.
*
* @since 2.2.0
*
* @param array $args Array of arguments.
* @param object $result Object of the current post result.
* @return string Space separated list of link attributes
*/
function crp_author( $args, $result ) {
$author_info = get_userdata( $result->post_author );
$author_link = ( false === $author_info ) ? '' : get_author_posts_url( $author_info->ID );
$author_name = ( false === $author_info ) ? '' : ucwords( trim( stripslashes( $author_info->display_name ) ) );
/**
* Filter the author name.
*
* @since 1.9.1
*
* @param string $author_name Proper name of the post author.
* @param object $author_info WP_User object of the post author
*/
$author_name = apply_filters( 'crp_author_name', $author_name, $author_info );
if ( ! empty( $author_name ) ) {
$crp_author = '<span class="crp_author"> ' . __( ' by ', 'contextual-related-posts' ) . '<a href="' . $author_link . '">' . $author_name . '</a></span> ';
} else {
$crp_author = '';
}
/**
* Filter the text with the author details.
*
* @since 2.0.0
*
* @param string $crp_author Formatted string with author details and link
* @param object $author_info WP_User object of the post author
* @param object $result Object of the current post result
* @param array $args Array of arguments
*/
return apply_filters( 'crp_author', $crp_author, $author_info, $result, $args );
}
/**
* Returns the permalink of each list item.
*
* @since 2.5.0
*
* @param array $args Array of arguments.
* @param object $result Object of the current post result.
* @return string Space separated list of link attributes
*/
function crp_permalink( $args, $result ) {
$link = get_permalink( $result->ID );
/**
* Filter the title of each list item.
*
* @since 2.5.0
*
* @param string $title Permalink of the post.
* @param object $result Object of the current post result
* @param array $args Array of arguments
*/
return apply_filters( 'crp_permalink', $link, $result, $args );
}
/**
* Returns the formatted list item with link and and thumbnail for each list item.
*
* @since 2.2.0
*
* @param array $args Array of arguments.
* @param object $result Object of the current post result.
* @return string Space separated list of link attributes
*/
function crp_list_link( $args, $result ) {
$output = '';
$title = crp_title( $args, $result );
$link = crp_permalink( $args, $result );
$link_attributes = crp_link_attributes( $args );
$output .= '<a href="' . $link . '" ' . $link_attributes . ' class="crp_link ' . $result->post_type . '-' . $result->ID . '">';
if ( 'after' === $args['post_thumb_op'] ) {
$output .= '<span class="crp_title">' . $title . '</span>'; // Add title when required by settings.
}
if ( 'inline' === $args['post_thumb_op'] || 'after' === $args['post_thumb_op'] || 'thumbs_only' === $args['post_thumb_op'] ) {
$output .= '<figure>';
$output .= crp_get_the_post_thumbnail(
array(
'postid' => $result,
'thumb_height' => $args['thumb_height'],
'thumb_width' => $args['thumb_width'],
'thumb_meta' => $args['thumb_meta'],
'thumb_html' => $args['thumb_html'],
'thumb_default' => $args['thumb_default'],
'thumb_default_show' => $args['thumb_default_show'],
'scan_images' => $args['scan_images'],
'class' => 'crp_thumb',
)
);
$output .= '</figure>';
}
if ( 'inline' === $args['post_thumb_op'] || 'text_only' === $args['post_thumb_op'] ) {
$output .= '<span class="crp_title">' . $title . '</span>'; // Add title when required by settings.
}
$output .= '</a>';
/**
* Filter Formatted list item with link and and thumbnail.
*
* @since 2.2.0
*
* @param string $output Formatted list item with link and and thumbnail
* @param object $result Object of the current post result
* @param array $args Array of arguments
*/
return apply_filters( 'crp_list_link', $output, $result, $args );
}