-
Notifications
You must be signed in to change notification settings - Fork 98
/
helpers.php
118 lines (104 loc) · 2.71 KB
/
helpers.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
<?php
/**
* Theme helper functions.
*
* @package WordPress_GitHub_SYnc
*/
/**
* Returns the HTML markup to view the current post on GitHub.
*
* @return string
*/
function get_the_github_view_link() {
return '<a href="' . get_the_github_view_url() . '">' . apply_filters( 'wpghs_view_link_text', __( 'View this post on GitHub.', 'wp-github-sync' ) ) . '</a>';
}
/**
* Returns the URL to view the current post on GitHub.
*
* @return string
*/
function get_the_github_view_url() {
$wpghs_post = new WordPress_GitHub_Sync_Post( get_the_ID(), WordPress_GitHub_Sync::$instance->api() );
return $wpghs_post->github_view_url();
}
/**
* Returns the HTML markup to edit the current post on GitHub.
*
* @return string
*/
function get_the_github_edit_link() {
return '<a href="' . get_the_github_edit_url() . '">' . apply_filters( 'wpghs_edit_link_text', __( 'Edit this post on GitHub.', 'wp-github-sync' ) ) . '</a>';
}
/**
* Returns the URL to edit the current post on GitHub.
*
* @return string
*/
function get_the_github_edit_url() {
$wpghs_post = new WordPress_GitHub_Sync_Post( get_the_ID(), WordPress_GitHub_Sync::$instance->api() );
return $wpghs_post->github_edit_url();
}
/**
* Common WPGHS function with attributes and shortcode
* - type: 'link' (default) to return a HTML anchor tag with text, or 'url' for bare URL.
* - target: 'view' (default) or 'edit' to return the respective link/url.
* - text: text to be included in the link. Ignored if type='url'.
*
* Returns either a HTML formatted anchor tag or the bare URL of the current post on GitHub.
*
* @return string
*/
function write_wpghs_link( $atts ) {
$args = shortcode_atts(
array(
'type' => 'link',
'target' => 'view',
'text' => '',
),
$atts
);
$type = esc_attr( $args['type'] );
$target = esc_attr( $args['target'] );
$text = esc_attr( $args['text'] );
$output = '';
switch ( $target ) {
case 'view': {
$getter = get_the_github_view_url();
if ( ! empty( $text ) ) {
$linktext = $text;
} else {
$linktext = __( 'View this post on GitHub', 'wp-github-sync' );
}
break;
}
case 'edit': {
$getter = get_the_github_edit_url();
if ( ! empty( $text ) ) {
$linktext = $text;
} else {
$linktext = __( 'Edit this post on GitHub', 'wp-github-sync' );
}
break;
}
default: {
$getter = get_the_github_view_url();
$linktext = __( 'View this post on GitHub', 'wp-github-sync' );
break;
}
}
switch ( $type ) {
case 'link': {
$output .= '<a href="' . $getter . '">' . $linktext . '</a>';
break;
}
case 'url': {
$output .= $getter;
break;
}
default: {
$output .= '<a href="' . $getter . '">' . $linktext . '</a>';
break;
}
}
return $output;
}