This repository has been archived by the owner on Oct 4, 2017. It is now read-only.
/
templating.php
374 lines (278 loc) · 11.1 KB
/
templating.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/**
* Functions for templating the Resume front end
*
* @author Benjamin J. Balter <ben@balter.com>
* @package WP_Resume
*/
class WP_Resume_Templating {
public $author;
public $author_id;
public $date_format = 'F Y';
public $future_signifier;
private $parent;
/**
* Stores parent and author within class
* @param class $parent (reference) the parent class
*/
function __construct( &$parent ) {
$this->parent = &$parent;
$this->author = &$parent->author;
$this->author_id = &$parent->author_id;
add_action( 'plugins_loaded', array( &$this, 'i18n_init' ) );
}
/**
* Delay i18ning until all plugins have a chance to load
*/
function i18n_init() {
//i18n: string appended to future date when translated
$this->future_signifier = __( ' (Anticipated)', 'wp-resume' );
if ( defined('QTRANS_INIT') || $this->parent->api->apply_filters( 'translate_date', false ) )
add_filter( 'wp_resume_date', array( &$this, 'translate_date' ), 10, 2 );
}
/**
* Applies filter and returns author's name
* @uses $author
* @returns string the author's name
*/
function get_name() {
$name = $this->parent->options->get_user_option( 'name', $this->author_id );
$name = $this->parent->api->apply_deprecated_filters( 'resume_name', '2.5', 'name', $name );
return $this->parent->api->apply_filters( 'name', $name );
}
/**
* Returns the title of the postition, or if rewriting is enabled, a link to the position
* @param int $ID the position ID
* @pararm bool $link (optional) whether to wrap title in link or not
* @return string the title, or the title link
*/
function get_title( $ID, $link = true ) {
//rewriting is disabled globally, or linking explicitly disabled via 2nd argument
// return just the text of the title
if ( !$link || !$this->parent->options->get_option( 'rewrite' ) ) {
$title = get_the_title();
//return the title wrapped in a link to the position's permalink
} else {
$title = '<a title="' . get_the_title() . '" href="' . get_permalink() . '">' . get_the_title() . '</a>';
$title = $this->parent->api->apply_deprecated_filters( 'resume_title_link', '2.5', 'title_link', $title );
$title = $this->parent->api->apply_filters( 'title_link', $title );
}
$title = $this->parent->api->apply_deprecated_filters( 'resume_position_title', '2.5', 'position_title', $title );
return $this->parent->api->apply_filters( 'position_title', $title );
}
/**
* Returns the section name, or a link to the section if rewrites are on
* @param object $section the section object
* @param bool $link (optional) whether to link if possible
* @returns string the section name or link to section
*/
function get_section_name( $section, $link = true ) {
return $this->get_taxonomy_name( $section, 'section', $link );
}
/**
* Returns the organization name, or a link to the organization if rewrites are on
* @param object $organization the organization object
* @param bool $link (optional) whether to link if possible
* @returns string the organization name or link to organization
*/
function get_organization_name( $organization, $link = true ) {
return $this->get_taxonomy_name( $organization, 'organization', $link );
}
/**
* Given a taxonomy object and taxonomy type, returns either the taxnomy name or a link to the taxnomy
* @param object $object the taxonomy object
* @param unknown $taxonomy
* @param bool $link whether to link if possible
* @returns string the formatted taxonomy name/link
*/
function get_taxonomy_name( $object, $taxonomy, $link ) {
global $post;
$rewrite = $this->parent->options->get_option( 'rewrite' );
if ( !$link ) {
$name = $this->parent->api->apply_deprecated_filters( "resume_{$taxonomy}_name", '2.5', "{$taxonomy}_name", $object->name );
return $this->parent->api->apply_filters( "{$taxonomy}_name", $name );
}
//org link
if ( $taxonomy == 'organization' && $this->parent->get_org_link( $object->term_id ) ) {
$link = $this->parent->get_org_link( $object->term_id );
//rewrite links
} else if ( $rewrite ) {
$link = get_term_link( $object, "resume_{$taxonomy}" );
//no link
} else {
$name = $this->parent->api->apply_deprecated_filters( "resume_{$taxonomy}_name", '2.5', "{$taxonomy}_name", $object->name );
return $this->parent->api->apply_filters( "{$taxonomy}_name", $name );
}
$title = '<a title="' . $object->name . '" itemprop="url" target="_new" href="' . $link . '">' . $object->name . '</a>';
$title = $this->parent->api->apply_deprecated_filters( "resume_{$taxonomy}_link", '2.5', "{$taxonomy}_link", $title );
$title = $this->parent->api->apply_filters( '{$taxonomy}_link', $title );
$title = $this->parent->api->apply_deprecated_filters( "resume_{$taxonomy}_name", '2.5', "{$taxonomy}_name", $title );
$title = $this->parent->api->apply_filters( '{$taxonomy}_name', $title );
return $title;
}
/**
* Returns the author's contact info
* @uses $author
* @returns array of contact info
*/
function get_contact_info() {
$contact_info = $this->parent->options->get_user_option( 'contact_info', $this->author_id );
$contact_info = $this->parent->api->apply_deprecated_filters( 'resume_contact_info', '2.5', 'contact_info', $contact_info );
return $this->parent->api->apply_filters( 'contact_info', $contact_info );
}
/**
* Returns the resume summary, if any
* @uses $author
* @returns string the resume summary
*/
function get_summary() {
$summary = $this->parent->options->get_user_option( 'summary', $this->author_id );
$summary = $this->parent->api->apply_deprecated_filters( 'resume_summary', '2.5', 'summary', $summary );
return $this->parent->api->apply_filters( 'summary', $summary );
}
/**
* Return true iff date is valid value for an HTML <time> element
* representing a date (with no time nor timezone).
* See https://html.spec.whatwg.org/multipage/semantics.html#the-time-element
*
* @param string $date the date as stored in post_meta
* @return true if date is in a valid format, else false
*/
function is_valid_html_date( $date ) {
$patterns = array(
'\d\d\d\d',
'\d\d\d\d-\d\d',
'\d\d\d\d-\d\d-\d\d',
'\d\d\d\d-W\d\d',
'\d\d-\d\d',
);
foreach( $patterns as $pattern )
if ( preg_match( '/\A' . $pattern . '\z/', $date ))
return true;
return false;
}
/**
* Function used to parse the date meta and move to human-readable format
*
* Both from and to are option, and if both are present, will be joined by an –
*
* @since 1.0a
* @uses resume_date
* @uses resume_date_formatted
* @param int $ID post ID to generate date for
* @return string the formatted date(s)
*/
function get_date( $ID ) {
$date = '';
foreach( array( 'from' => 'dtstart', 'to' => 'dtend' ) as $field => $class ) {
$value = get_post_meta( $ID, "wp_resume_{$field}", true );
$itemprop = ( $class = 'dtstart' ) ? 'startDate' : 'endDate';
$use_markup = true;
//we don't have this field, skip
if ( !$value)
continue;
//to ensure compliance with hResume format, markup should reflect ability to parse date
//@link https://github.com/benbalter/WP-Resume/issues/7
//if a valid HTML-format date, wrap in a <time> element with no 'datetime' attr.
if ( $this->is_valid_html_date( $value ) )
$date .= '<time itemprop="' . $itemprop . '" class="' . $class . '">';
// //if we can parse the date, append the proper class and formatted date
// //### Ignores the specified precision (year vs. month vs. day) and
// //### writes '01' for a missing month or day, which is misleading.
// else if ( strtotime( $value ) )
// $date .= '<time itemprop="' . $itemprop . '" class="' . $class . '" datetime="' . date( 'Y-m-d', strtotime( $value ) ) . '">';
//
//if we can't parse the date, don't mark it up
else
$use_markup = false;
$date .= $this->parent->api->apply_filters( 'date', $value, $field );
if ( $use_markup )
$date .= '</time>';
//this is the from field and there is a to field, append the dash
//it's okay that we're calling get_post_meta twice on "to" because it's cached automatically
if ( $field == 'from' && get_post_meta( $ID, 'wp_resume_to', true ) )
$date .= ' – ';
}
return $this->parent->api->apply_filters( 'date_formatted', $date, $ID );
}
/**
* Always dates to be translated and localized, e.g., by qTranslate
*
* @param string $date the date as stored in post_meta
* @param string $type the type, either "from" or "to"
* @param string $from the from date
* @param string $to the to date
* @return string the i18n'd date
*/
function translate_date( $date, $type ) {
//unix timestamp of date, false if not parsable
$timestamp = strtotime( $date );
//default date format
$date_format = $this->parent->api->apply_filters( 'date_format', $this->date_format );
//allow present to be translatable
if ( strtolower( trim( $date ) ) == 'Present' )
$date = __( 'Present', 'wp-resume' );
//not parsable, can't translate so return whatever we've got
if ( !$timestamp )
return $date;
//i18n date
$date = date_i18n( $date_format, strtotime( $date ) );
//we don't do anything else to start dates, so kick
if ( $type == 'from' )
return $date;
//to date is not in the future, again, can't do anything, so kick
if ( $timestamp < time() )
return $date;
//append e.g, ' (Anticipated)' to future dates
//note: this string won't appear in .POT files, but should still hit qTranslate when run (I hope)
$date .= __( $this->parent->api->apply_filters( 'future_signifier', $this->future_signifier ) );
return $date;
}
/**
* Translates hresume field names to author/postalAddress microformat fields
* @param string $field the hresume field
* @return string the schema.org compliant field
*/
function get_contact_info_itemprop( $field ) {
$trans = array(
'tel' => 'itemprop="telephone"',
'email' => 'itemprop="email"',
'adr' => 'itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"',
'street-address' => 'itemprop="streetAddress"',
'locality' => 'itemprop="addressLocality"',
'region' => 'itemprop="addressRegion"',
'postal-code' => 'itemprop="postalCode"',
'country-name' => 'itemprop="addressCountry"',
);
if ( !array_key_exists( $field, $trans ) )
return;
return $trans[ $field ];
}
/**
* Retrieves and echos contact info itemprop property
* @param string $field the hresume field
*/
function contact_info_itemprop( $field ) {
echo $this->get_contact_info_itemprop( $field );
}
/**
* Sort adr array keys from given order
* @param array $contact_info
* @param $sorted (optional) to order in given order
*/
function contact_info_sort_adr( $contact_info, $sorter = array() ) {
$sorted = array();
if( empty($sorter) ){
$sorter = array_keys($this->parent->contact_fields()['adr']);
}
if( isset( $contact_info['adr'] ) && !empty( $contact_info['adr'] ) ){
$temp = array();
foreach ( $sorter as $field ) {
if ( array_key_exists( $field, $contact_info['adr'] ) )
$temp[$field] = $contact_info['adr'][$field];
}
$sorted = $temp;
}
return $sorted;
}
}