shortcodes.php
4.36 KB
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
<?php
/**
* Custom Shortcodes.
*
* @package BadgeOS
* @subpackage Front-end
* @author LearningTimes, LLC
* @license http://www.gnu.org/licenses/agpl.txt GNU AGPL v3.0
* @link https://credly.com
*/
require plugin_dir_path( dirname( __FILE__ ) ) . 'includes/shortcodes/badgeos_achievements_list.php';
require plugin_dir_path( dirname( __FILE__ ) ) . 'includes/shortcodes/badgeos_achievement.php';
require plugin_dir_path( dirname( __FILE__ ) ) . 'includes/shortcodes/badgeos_user_earned_achievements.php';
require plugin_dir_path( dirname( __FILE__ ) ) . 'includes/shortcodes/badgeos_user_earned_ranks.php';
require plugin_dir_path( dirname( __FILE__ ) ) . 'includes/shortcodes/badgeos_user_earned_points.php';
require plugin_dir_path( dirname( __FILE__ ) ) . 'includes/shortcodes/badgeos_rank.php';
require plugin_dir_path( dirname( __FILE__ ) ) . 'includes/shortcodes/badgeos_ranks.php';
require plugin_dir_path( dirname( __FILE__ ) ) . 'includes/shortcodes/evidence-shortcode.php';
require plugin_dir_path( dirname( __FILE__ ) ) . 'includes/shortcodes/user-dashboard.php';
/**
* Register a new BadgeOS Shortcode
*
* @since 1.4.0
*
* @param array $args Shortcode Args.
* @return object Shortcode Object.
*/
function badgeos_register_shortcode( $args ) {
return new BadgeOS_Shortcode( $args );
}
/**
* Get all registered BadgeOS shortcodes.
*
* @since 1.4.0
*
* @return array Registered BadgeOS shortcodes.
*/
function badgeos_get_shortcodes() {
return apply_filters( 'badgeos_shortcodes', array() );
}
/**
* Add all shortcodes to the help page.
*
* @since 1.4.0
*/
function badgeos_help_support_page_shortcodes() {
foreach ( badgeos_get_shortcodes() as $shortcode ) {
badgeos_shortcode_help_render_help( $shortcode );
}
}
add_action( 'badgeos_help_support_page_shortcodes', 'badgeos_help_support_page_shortcodes' );
/**
* Render help section for a given shortcode.
*
* @since 1.4.0
*
* @param object $shortcode Shortcode object.
*/
function badgeos_shortcode_help_render_help( $shortcode = array() ) {
printf(
'
<hr/>
<h3>%1$s – [%2$s]</h3>
<p>%3$s</p>
<ul style="margin:1em 2em; padding:1em;">
<li><strong>%4$s</strong></li>
%5$s
</ul>
<p>%6$s</p>
',
esc_html( $shortcode->name ),
esc_html( $shortcode->slug ),
esc_html( $shortcode->description ),
esc_html__( 'Attributes:', 'badgeos' ),
wp_kses_post( badgeos_shortcode_help_render_attributes( $shortcode->attributes ) ),
wp_kses_post( badgeos_shortcode_help_render_example( $shortcode ) )
);
}
/**
* Render attributes portion of shordcode help section.
*
* @since 1.4.0
*
* @param array $attributes Shortcode attributes.
* @return string HTML Markup.
*/
function badgeos_shortcode_help_render_attributes( $attributes ) {
$output = '';
if ( ! empty( $attributes ) ) {
foreach ( $attributes as $attribute => $details ) {
$accepts = ! empty( $details['values'] ) ? sprintf( esc_html__( 'Accepts: %s', 'badgeos' ), '<code>' . implode( ', ', $details['values'] ) . '</code>' ) : '';
$default = ! empty( $details['default'] ) ? sprintf( esc_html__( 'Default: %s', 'badgeos' ), '<code>' . $details['default'] . '</code>' ) : '';
$output .= sprintf(
'<li><strong>%1$s</strong> – %2$s <em>%3$s %4$s</em></li>',
esc_attr( $attribute ),
esc_html( $details['description'] ),
$accepts,
$default
);
}
}
return $output;
}
/**
* Render example shortcode usage for help section.
*
* @since 1.4.0
*
* @param array $shortcode Shortcode object.
* @return string HTML Markup.
*/
function badgeos_shortcode_help_render_example( $shortcode = array() ) {
$attributes = wp_list_pluck( $shortcode->attributes, 'default' );
$examples = array_map( 'badgeos_shortcode_help_attributes', array_keys( $attributes ), array_values( $attributes ) );
$flattened_examples = implode( ' ', $examples );
return sprintf( esc_html__( 'Example: %s', 'badgeos' ), "<code>[{$shortcode->slug} {$flattened_examples}]</code>" );
}
/**
* Render attribute="value" for attributes in shortcode example.
*
* @since 1.4.0
*
* @param string $key Key name.
* @param string $value Value.
* @return string key="value".
*/
function badgeos_shortcode_help_attributes( $key, $value ) {
return "{$key}=\"$value\"";
}