badgeos_rank.php
6.09 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
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
<?php
/**
* Register the [badgeos_rank] shortcode.
*
* @since 1.4.0
*/
function badgeos_register_rank_shortcode() {
$badgeos_settings = ( $exists = badgeos_utilities::get_option( 'badgeos_settings' ) ) ? $exists : array();
$rank_types = get_posts( array(
'post_type' => $badgeos_settings['ranks_main_post_type'],
'posts_per_page' => -1,
) );
$ranks = array();
foreach( $rank_types as $rtype ) {
$records = get_posts( array(
'post_type' => $rtype->post_name,
'posts_per_page' => -1,
) );
foreach( $records as $record ) {
$ranks[ $record->ID ] = $record->post_title;
}
}
badgeos_register_shortcode( array(
'name' => __( 'Single Rank', 'badgeos' ),
'slug' => 'badgeos_rank',
'output_callback' => 'badgeos_rank_shortcode',
'description' => __( 'Render a single rank.', 'badgeos' ),
'attributes' => array(
'id' => array(
'name' => __( 'Rank ID', 'badgeos' ),
'description' => __( 'The ID of the rank to render.', 'badgeos' ),
'type' => 'select',
'values' => $ranks,
'default' => '',
),
'show_title' => array (
'name' => __( 'Show Title', 'badgeos' ),
'description' => __( 'Display Rank Title.', 'badgeos' ),
'type' => 'select',
'values' => array (
'true' => __( 'True', 'badgeos' ),
'false' => __( 'False', 'badgeos' )
),
'default' => 'true',
),
'show_thumb' => array (
'name' => __( 'Show Thumbnail', 'badgeos' ),
'description' => __( 'Display Thumbnail Image.', 'badgeos' ),
'type' => 'select',
'values' => array(
'true' => __( 'True', 'badgeos' ),
'false' => __( 'False', 'badgeos' )
),
'default' => 'true',
),
'show_description' => array (
'name' => __( 'Show Description', 'badgeos' ),
'description' => __( 'Display Short Description.', 'badgeos' ),
'type' => 'select',
'values' => array(
'true' => __( 'True', 'badgeos' ),
'false' => __( 'False', 'badgeos' )
),
'default' => 'true',
),
'image_width' => array (
'name' => __( 'Thumnail Width', 'badgeos' ),
'description' => __( "Rank's image width.", 'badgeos' ),
'type' => 'text',
'default' => '',
),
'image_height' => array (
'name' => __( 'Thumnail Height', 'badgeos' ),
'description' => __( "Rank's image height.", 'badgeos' ),
'type' => 'text',
'default' => '',
),
),
) );
}
add_action( 'init', 'badgeos_register_rank_shortcode' );
/**
* Single rank Shortcode.
*
* @since 1.0.0
*
* @param array $atts Shortcode attributes.
* @return string HTML markup.
*/
function badgeos_rank_shortcode( $atts = array() ) {
global $wpdb;
// get the post id
$atts = shortcode_atts( array(
'id' => '',
'show_title' => 'true',
'show_thumb' => 'true',
'show_description' => 'true',
'image_width' => '',
'image_height' => '',
), $atts, 'badgeos_rank' );
// return if post id not specified
$settings = ( $exists = badgeos_utilities::get_option( 'badgeos_settings' ) ) ? $exists : array();
if ( empty($atts['id']) ) {
$ranks = $wpdb->get_results( $wpdb->prepare( "select * from ".$wpdb->prefix."badgeos_ranks where rank_type!=%s and user_id=%d order by actual_date_earned desc limit 1", trim( $settings['ranks_step_post_type'] ), get_current_user_id() ) );
if( count( $ranks ) > 0 ) {
$atts['id'] = $ranks[0]->rank_id;
}
}
if ( empty($atts['id']) ) {
return false;
}
wp_enqueue_style( 'badgeos-front' );
wp_enqueue_script( 'badgeos-achievements' );
// get the post content and format the badge display
$rank = badgeos_utilities::badgeos_get_post( $atts['id'] );
$output = '';
// If we're dealing with an rank post
if ( badgeos_is_rank( $rank ) ) {
$output .= '<div id="badgeos-rank-item-' . $rank->ID . '" class="badgeos-list-item badgeos-rank-item badgeos-rank-item-' . $rank->ID . '">';
// Achievement Image
if( $atts['show_thumb'] == 'true' ) {
$output .= '<div class="badgeos-item-image">';
$output .= '<a href="' . get_permalink( $rank->ID ) . '">' . badgeos_get_rank_image( $rank->ID, $atts['image_width'], $atts['image_height'] ) . '</a>';
$output .= '</div><!-- .badgeos-item-image -->';
}
// Achievement Content
$output .= '<div class="badgeos-item-detail">';
if( $atts['show_title'] == 'true' ) {
$title = get_the_title($rank->ID);
// Achievement Title
$output .= '<h2 class="badgeos-item-title"><a href="'.get_permalink( $rank->ID ).'">'.$title.'</a></h2>';
}
// Achievement Short Description
if( $atts['show_description'] == 'true' ) {
$post = badgeos_utilities::badgeos_get_post( $rank->ID );
if( $post ) {
$output .= '<div class="badgeos-item-excerpt">';
$excerpt = !empty( $post->post_excerpt ) ? $post->post_excerpt : $post->post_content;
$output .= wpautop( apply_filters( 'get_the_excerpt', $excerpt ) );
$output .= '</div><!-- .badgeos-item-excerpt -->';
}
}
$output .= '</div><!-- .badgeos-item-description -->';
$output .= apply_filters( 'badgeos_after_single_rank', '', $rank );
$output .= '</div>';
}
// Return our rendered rank
return $output;
}