/
template.php
562 lines (485 loc) · 16.7 KB
/
template.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
<?php
function gp_tmpl_load( $template, $args = array(), $template_path = null ) {
$args = gp_tmpl_filter_args( $args );
/**
* Fires before a template is loaded.
*
* @since 1.0.0
*
* @param string $template The template name.
* @param array $args Arguments passed to the template. Passed by reference.
*/
do_action_ref_array( 'gp_pre_tmpl_load', array( $template, &$args ) );
require_once GP_TMPL_PATH . 'helper-functions.php';
$locations = array( GP_TMPL_PATH );
if ( !is_null( $template_path ) ) {
array_unshift( $locations, untrailingslashit( $template_path ) . '/' );
}
/**
* Filter the locations to load template files from.
*
* @since 1.0.0
*
* @param array $locations File paths of template locations.
* @param string $template The template name.
* @param array $args Arguments passed to the template.
* @param string|null $template_path Priority template location, if any.
*/
$locations = apply_filters( 'gp_tmpl_load_locations', $locations, $template, $args, $template_path );
if ( isset( $args['http_status'] ) )
status_header( $args['http_status'] );
foreach( $locations as $location ) {
$file = $location . "$template.php";
if ( is_readable( $file ) ) {
extract( $args, EXTR_SKIP );
include $file;
break;
}
}
/**
* Fires after a template was loaded.
*
* @since 1.0.0
*
* @param string $template The template name.
* @param array $args Arguments passed to the template. Passed by reference.
*/
do_action_ref_array( 'gp_post_tmpl_load', array( $template, &$args ) );
}
function gp_tmpl_get_output() {
$args = func_get_args();
ob_start();
call_user_func_array( 'gp_tmpl_load', $args );
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
function gp_tmpl_header( $args = array( ) ) {
gp_tmpl_load( 'header', $args );
}
function gp_tmpl_footer( $args = array( ) ) {
gp_tmpl_load( 'footer', $args );
}
function gp_head() {
/**
* Fires inside the head element on the header template.
*
* @since 1.0.0
*/
do_action( 'gp_head' );
}
function gp_footer() {
/**
* Fires at the end of the page, on the footer template.
*
* @since 1.0.0
*/
do_action( 'gp_footer' );
}
function gp_nav_menu( $location = 'main' ) {
$html = '';
$items = gp_nav_menu_items( $location );
foreach ( $items as $link => $title ) {
$html .= '<a href="' . $link . '">' . $title . '</a>';
}
return $html;
}
function gp_nav_menu_items( $location = 'main' ) {
$items = array();
if ( 'main' === $location ) {
$items[ gp_url( '/projects' ) ] = __( 'Projects', 'glotpress' );
$items[ gp_url( '/languages' ) ] = __( 'Locales', 'glotpress' );
}
elseif ( 'side' === $location ) {
if ( is_user_logged_in() ) {
$items[ gp_url( '/profile' ) ] = __( 'Profile', 'glotpress' );
$items[ esc_url( wp_logout_url( gp_url_current() ) ) ] = __( 'Log out', 'glotpress' );
}
else {
$items[ esc_url( wp_login_url( gp_url_current() ) ) ] = __( 'Log in', 'glotpress' );
}
}
/**
* Filter the list of navigation menu items.
*
* @since 1.0.0
*
* @param array $items Menu items. URL as the key, menu label as the value.
* @param string $location Location of the menu.
*/
return apply_filters( 'gp_nav_menu_items', $items, $location );
}
function gp_tmpl_filter_args( $args ) {
$clean_args = array();
foreach( $args as $k => $v )
if ( $k[0] != '_' && $k != 'GLOBALS' && !gp_startswith( $k, 'HTTP' ) && !gp_startswith( $k, 'PHP' ) )
$clean_args[$k] = $v;
return $clean_args;
}
function gp_tmpl_404( $args = array()) {
gp_tmpl_load( '404', $args + array('title' => __('Not Found', 'glotpress' ), 'http_status' => 404 ) );
exit();
}
function gp_title( $title = null ) {
if ( ! is_null( $title ) ) {
add_filter( 'gp_title', function() use ( $title ) {
return $title;
}, 5 );
} else {
/**
* Filter the title of a page.
*
* @since 1.0.0
*
* @param string $title The title of a page.
*/
return apply_filters( 'gp_title', '' );
}
}
function gp_breadcrumb( $breadcrumb = null, $args = array() ) {
if ( $breadcrumb ) {
$breadcrumb = gp_array_flatten( $breadcrumb );
add_filter( 'gp_breadcrumb_items', function( $breadcrumbs ) use ( $breadcrumb ) {
return array_merge( $breadcrumbs, $breadcrumb );
}, 1 );
} else {
/**
* Filter the list of breadcrumb navigation items.
*
* @since 1.0.0
*
* @param array $breadcrumb_items Breadcrumb items as HTML string.
*/
$breadcrumbs = apply_filters( 'gp_breadcrumb_items', array() );
if ( $breadcrumbs ) {
$defaults = array(
/* translators: separates links in the navigation breadcrumb */
'before' => '<li>',
'after' => '</li>',
'breadcrumb-template' => '<ul class="breadcrumb">{breadcrumb}</ul>',
);
$args = array_merge( $defaults, $args );
$whole_breadcrumb = '';
foreach ( $breadcrumbs as $breadcrumb ) {
$whole_breadcrumb .= $args['before'] . $breadcrumb . $args['after'];
}
$whole_breadcrumb = str_replace( '{breadcrumb}', $whole_breadcrumb, $args['breadcrumb-template'] );
/**
* Filter the breadcrumb HTML output.
*
* @since 1.0.0
*
* @param string $whole_breadcrumb Breadcrumb HTML.
*/
return apply_filters( 'gp_breadcrumb', $whole_breadcrumb );
}
}
}
function gp_project_names_from_root( $leaf_project ) {
$names = array();
$path_from_root = array_reverse( $leaf_project->path_to_root() );
foreach ( $path_from_root as $project ) {
$names[] = esc_html($project->name);
}
$project_path = implode( " | ", $names );
return $project_path;
}
function gp_project_links_from_root( $leaf_project ) {
$links = array();
$path_from_root = array_reverse( $leaf_project->path_to_root() );
$links[] = empty( $path_from_root)? __( 'Projects', 'glotpress' ) : gp_link_get( gp_url( '/projects' ), __( 'Projects', 'glotpress' ) );
foreach( $path_from_root as $project ) {
$links[] = gp_link_project_get( $project, esc_html( $project->name ) );
}
return $links;
}
function gp_breadcrumb_project( $project ) {
return gp_breadcrumb( gp_project_links_from_root( $project ) );
}
function gp_js_focus_on( $html_id ) {
return '<script type="text/javascript">document.getElementById("'.$html_id.'").focus();</script>';
}
function gp_select( $name_and_id, $options, $selected_key, $attrs = array() ) {
$attributes = gp_html_attributes( $attrs );
$attributes = $attributes? " $attributes" : '';
$res = "<select name='" . esc_attr( $name_and_id ) . "' id='" . esc_attr( $name_and_id ) . "' $attributes>\n";
foreach( $options as $value => $label ) {
$selected = $value == $selected_key? " selected='selected'" : '';
$res .= "\t<option value='".esc_attr( $value )."' $selected>" . esc_html( $label ) . "</option>\n";
}
$res .= "</select>\n";
return $res;
}
function gp_radio_buttons( $name, $radio_buttons, $checked_key ) {
$res = '';
foreach( $radio_buttons as $value => $label ) {
$checked = $value == $checked_key? " checked='checked'" : '';
// TODO: something more flexible than <br />
$res .= "\t<input type='radio' name='$name' value='".esc_attr( $value )."' $checked id='{$name}[{$value}]'/> ";
$res .= "<label for='{$name}[{$value}]'>".esc_html( $label )."</label><br />\n";
}
return $res;
}
function gp_pagination( $page, $per_page, $objects ) {
$surrounding = 2;
$first = $prev_dots = $prev_pages = $next_pages = $next_dots = $last = '';
$page = intval( $page )? intval( $page ) : 1;
$pages = ceil( $objects / $per_page );
if ( $page > $pages ) return '';
if ( $page > 1 )
$prev = gp_link_get( add_query_arg( array( 'page' => $page - 1 ) ), '←', array('class' => 'previous') );
else
$prev = '<span class="previous disabled">←</span>';
if ( $page < $pages )
$next = gp_link_get( add_query_arg( array( 'page' => $page + 1)), '→', array('class' => 'next') );
else
$next = '<span class="next disabled">→</span>';
$current = '<span class="current">'.$page.'</span>';
if ( $page > 1 ) {
$prev_pages = array();
foreach( range( max( 1, $page - $surrounding ), $page - 1 ) as $prev_page ) {
$prev_pages[] = gp_link_get( add_query_arg( array( 'page' => $prev_page ) ), $prev_page );
}
$prev_pages = implode( ' ', $prev_pages );
if ( $page - $surrounding > 1 ) $prev_dots = '<span class="dots">…</span>';
}
if ( $page < $pages ) {
$next_pages = array();
foreach( range( $page + 1, min( $pages, $page + $surrounding ) ) as $next_page ) {
$next_pages[] = gp_link_get( add_query_arg( array( 'page' => $next_page ) ), $next_page );
}
$next_pages = implode( ' ', $next_pages );
if ( $page + $surrounding < $pages ) $next_dots = '<span class="dots">…</span>';
}
if ( $prev_dots ) $first = gp_link_get( add_query_arg( array( 'page' => 1 ) ), 1 );
if ( $next_dots ) $last = gp_link_get( add_query_arg( array( 'page' => $pages ) ), $pages );
$html = <<<HTML
<div class="paging">
$prev
$first
$prev_dots
$prev_pages
$current
$next_pages
$next_dots
$last
$next
</div>
HTML;
/**
* Filter the pagination HTML output.
*
* @since 1.0.0
*
* @param string $html The pagination HTML.
* @param int $page Current page number.
* @param int $per_page Objects per page.
* @param int $objects Total number of objects to page.
*/
return apply_filters( 'gp_pagination', $html, $page, $per_page, $objects );
}
function gp_html_attributes( $attrs ) {
$attrs = wp_parse_args( $attrs );
$strings = array();
foreach( $attrs as $key => $value ) {
$strings[] = $key.'="'.esc_attr( $value ).'"';
}
return implode( ' ', $strings );
}
function gp_attrs_add_class( $attrs, $class_name ) {
$attrs['class'] = isset( $attrs['class'] )? $attrs['class'] . ' ' . $class_name : $class_name;
return $attrs;
}
/**
* Returns HTML markup for a select element for all locales of a project.
*
* @since 1.0.0
*
* @param int $project_id ID of a project.
* @param string $name_and_id Name and ID of the select element.
* @param string $selected_slug Slug of the current selected locale.
* @param array $attrs Extra attributes.
* @return string HTML markup for a select element.
*/
function gp_locales_by_project_dropdown( $project_id, $name_and_id, $selected_slug = null, $attrs = array() ) {
$locales = GP_Locales::locales();
if ( null != $project_id ) {
$sets = GP::$translation_set->by_project_id( $project_id );
$temp_locales = array();
foreach( $sets as $set ) {
$temp_locales[ $set->locale ] = $locales[ $set->locale ];
}
if ( count( $temp_locales ) > 0 ) {
$locales = $temp_locales;
}
}
ksort( $locales );
$options = array( '' => __( '— Locale —', 'glotpress' ) );
foreach ( $locales as $key => $locale ) {
$options[ $key ] = sprintf( '%s — %s', $locale->slug, $locale->english_name );
}
return gp_select( $name_and_id, $options, $selected_slug, $attrs );
}
/**
* Returns HTML markup for a select element for all locales.
*
* @since 1.0.0
*
* @param string $name_and_id Name and ID of the select element.
* @param string $selected_slug Slug of the current selected locale.
* @param array $attrs Extra attributes.
* @return string HTML markup for a select element.
*/
function gp_locales_dropdown( $name_and_id, $selected_slug = null, $attrs = array() ) {
return gp_locales_by_project_dropdown( null, $name_and_id, $selected_slug, $attrs );
}
function gp_projects_dropdown( $name_and_id, $selected_project_id = null, $attrs = array(), $exclude = array() ) {
if ( ! is_array( $exclude ) ) {
$exclude = array( $exclude );
}
$projects = GP::$project->all();
// TODO: mark which nodes are editable by the current user
$tree = array();
$top = array();
foreach( $projects as $p ) {
$tree[$p->id]['self'] = $p;
if ( $p->parent_project_id ) {
$tree[$p->parent_project_id]['children'][] = $p->id;
} else {
$top[] = $p->id;
}
}
$options = array( '' => __( '— No parent —', 'glotpress' ) );
foreach( $top as $top_id ) {
$stack = array( $top_id );
while ( !empty( $stack ) ) {
$id = array_pop( $stack );
if ( in_array( $id, $exclude ) ) {
continue;
}
$tree[$id]['level'] = gp_array_get( $tree[$id], 'level', 0 );
$options[$id] = str_repeat( '-', $tree[$id]['level'] ) . $tree[$id]['self']->name;
foreach( gp_array_get( $tree[$id], 'children', array() ) as $child_id ) {
$stack[] = $child_id;
$tree[$child_id]['level'] = $tree[$id]['level'] + 1;
}
}
}
return gp_select( $name_and_id, $options, $selected_project_id, $attrs );
}
function gp_array_of_things_to_json( $array ) {
return wp_json_encode( array_map( function( $thing ) { return $thing->fields(); }, $array ) );
}
function gp_array_of_array_of_things_to_json( $array ) {
$map_to_fields = function( $array ) {
return array_map( function( $thing ) {
return $thing->fields();
}, $array );
};
return wp_json_encode( array_map( $map_to_fields, $array ) );
}
function things_to_fields( $data ) {
if( is_array( $data ) ) {
foreach( $data as $item_id => $item ) {
$data[ $item_id ] = things_to_fields( $item );
}
}
else if ( $data instanceof GP_Thing ) {
$data = $data->fields();
}
return $data;
}
function gp_preferred_sans_serif_style_tag( $locale ) {
if ( $locale->preferred_sans_serif_font_family ) {
echo <<<HTML
<style type="text/css">
.foreign-text {
font-family: "$locale->preferred_sans_serif_font_family", inherit;
}
</style>
HTML;
}
}
function gp_html_excerpt( $str, $count, $ellipsis = '…') {
$excerpt = trim( wp_html_excerpt( $str, $count ) );
if ( $str != $excerpt ) {
$excerpt .= $ellipsis;
}
return $excerpt;
}
function gp_checked( $checked ) {
if ( $checked ) {
echo 'checked="checked"';
}
}
function gp_project_actions( $project, $translation_sets ) {
$actions = array(
gp_link_get( gp_url_project( $project, 'import-originals' ), __( 'Import originals', 'glotpress' ) ),
gp_link_get( gp_url_project( $project, array( '-permissions' ) ), __( 'Permissions', 'glotpress') ),
gp_link_get( gp_url_project( '', '-new', array('parent_project_id' => $project->id) ), __( 'New Sub-Project', 'glotpress' ) ),
gp_link_get( gp_url( '/sets/-new', array( 'project_id' => $project->id ) ), __( 'New Translation Set', 'glotpress' ) ),
gp_link_get( gp_url_project( $project, array( '-mass-create-sets' ) ), __( 'Mass-create Translation Sets', 'glotpress' ) ),
gp_link_get( gp_url_project( $project, '-branch'), __( 'Branch Project', 'glotpress' ) ),
gp_link_with_ays_get( gp_url_project( $project, '-delete'), __( 'Delete Project', 'glotpress' ), array( 'ays-text' => __( 'Do you really want to delete this project?', 'glotpress' ) ) )
);
/**
* Project action links.
*
* @since 1.0.0
*
* @param array $actions Links as HTML strings.
* @param GP_Project $project The project.
*/
$actions = apply_filters( 'gp_project_actions', $actions, $project );
echo '<ul>';
foreach( $actions as $action ) {
echo '<li>' . $action . '</li>';
}
if ( $translation_sets ) {
echo '<li>' . gp_project_options_form( $project ) . '</li>';
}
echo '</ul>';
}
function gp_project_options_form( $project ) {
return '
<a href="#" class="personal-options" id="personal-options-toggle"> ' . __( 'Personal project options ↓', 'glotpress' ) . '</a>
<div class="personal-options">
<form action="' . gp_url_project( $project, '-personal' ) . '" method="post">
<dl>
<dt><label for="source-url-template">' . __( 'Source file URL', 'glotpress' ) . '</label></dt>
<dd>
<input type="text" value="' . esc_html( $project->source_url_template() ) . '" name="source-url-template" id="source-url-template" />
<small>' . sprintf(
/* translators: 1: %file%, 2: %line%, 3: https://trac.example.org/browser/%file%#L%line% */
__( 'URL to a source file in the project. You can use %1$s and %2$s. Ex. %3$s', 'glotpress' ),
'<code>%file%</code>',
'<code>%line%</code>',
'<code>https://trac.example.org/browser/%file%#L%line%</code>'
) . '</small>
</dd>
</dl>
<p>
<input type="submit" name="submit" value="' . esc_attr__( 'Save →', 'glotpress' ) . '" id="save" />
<a class="ternary" href="#" onclick="jQuery(\'#personal-options-toggle\').click();return false;">' . __( 'Cancel', 'glotpress' ) . '</a>
</p>
</form>
</div>';
}
function gp_entry_actions( $seperator = ' • ' ) {
$actions = array(
'<a href="#" class="copy" tabindex="-1">' . __( 'Copy from original', 'glotpress' ) . '</a>'
);
/**
* Filter entry action links.
*
* @since 1.0.0
*
* @param array $actions Links as HTML strings.
*/
$actions = apply_filters( 'gp_entry_actions', $actions );
echo implode( $seperator, $actions );
/*
<a href="#" class="copy" tabindex="-1"><?php _e( 'Copy from original', 'glotpress' ); ?></a> •
<a href="#" class="gtranslate" tabindex="-1"><?php _e( 'Translation from Google', 'glotpress' ); ?></a>
*/
}