evidence-shortcode.php
7.74 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
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
<?php
/**
* Custom Shortcodes - BadgeOS Evidence.
*
* @package BadgeOS
*/
/**
* Register the [badgeos_achievement] shortcode.
*/
function badgeos_register_ob_evidence_shortcode() {
// Setup a custom array of achievement types.
$badgeos_settings = ! empty( badgeos_utilities::get_option( 'badgeos_settings' ) ) ? badgeos_utilities::get_option( 'badgeos_settings' ) : array();
$achievement_types = get_posts(
array(
'post_type' => $badgeos_settings['achievement_main_post_type'],
'posts_per_page' => -1,
'post_status' => 'publish',
)
);
$post_list = array();
foreach ( $achievement_types as $type ) {
$posts = get_posts(
array(
'post_type' => $type->post_name,
'posts_per_page' => -1,
)
);
foreach ( $posts as $post ) {
if ( badgeos_get_option_open_badge_enable_baking( $post->ID ) ) {
$post_list[ $post->ID ] = $post->post_title;
}
}
}
badgeos_register_shortcode(
array(
'name' => __( 'Achievement Evidence', 'badgeos' ),
'slug' => 'badgeos_evidence',
'output_callback' => 'badgeos_openbadge_evidence_shortcode',
'description' => __( "Render a single achievement's evidence.", 'badgeos' ),
'attributes' => array(
'achievement' => array(
'name' => __( 'Achievement', 'badgeos' ),
'description' => __( 'Achievement ID to show.', 'badgeos' ),
'type' => 'select',
'values' => $post_list,
),
'user_id1' => array(
'name' => __( 'Select User (Type 3 chars)', 'badgeos' ),
'description' => __( 'Achievement Earned by.', 'badgeos' ),
'type' => 'text',
'autocomplete_name' => 'user_id',
),
'award_id1' => array(
'name' => __( 'Award Id', 'badgeos' ),
'description' => __( 'User awarded achievement record.', 'badgeos' ),
'type' => 'text',
'autocomplete_name' => 'award_id',
),
),
)
);
}
add_action( 'init', 'badgeos_register_ob_evidence_shortcode' );
/**
* Single Achievement Shortcode.
*
* @param array $atts Shortcode attributes.
* @return string HTML markup.
*/
function badgeos_openbadge_evidence_shortcode( $atts = array() ) {
global $wpdb;
wp_enqueue_script( 'thickbox' );
wp_enqueue_style( 'thickbox' );
/**
* Get the post id.
*/
$atts = shortcode_atts(
array(
'show_sharing_opt' => 'Yes',
'achievement' => 0,
'user_id' => 0,
'award_id' => 0,
),
$atts,
'badgeos_evidence'
);
$achievement_id = 0;
if ( ! empty( $_REQUEST['bg'] ) ) {
$achievement_id = absint( wp_unslash( $_REQUEST['bg'] ) );
} elseif ( isset( $atts['achievement'] ) && absint( $atts['achievement'] ) > 0 ) {
$achievement_id = absint( $atts['achievement'] );
}
$entry_id = 0;
if ( ! empty( $_REQUEST['eid'] ) ) {
$entry_id = absint( wp_unslash( $_REQUEST['eid'] ) );
} elseif ( isset( $atts['award_id'] ) && absint( $atts['award_id'] ) > 0 ) {
$entry_id = absint( $atts['award_id'] );
}
$user_id = 0;
if ( ! empty( $_REQUEST['uid'] ) ) {
$user_id = absint( wp_unslash( $_REQUEST['uid'] ) );
} elseif ( isset( $atts['user_id'] ) && absint( $atts['user_id'] ) > 0 ) {
$user_id = absint( $atts['user_id'] );
}
if ( empty( $achievement_id ) || empty( $user_id ) || empty( $entry_id ) ) {
return '<p>Evidence is not avialable.</p>';
}
$output = '';
$recs = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}badgeos_achievements WHERE entry_id=%d AND user_id=%d AND ID=%d",
$entry_id,
$user_id,
$achievement_id
)
);
if ( count( $recs ) > 0 ) {
$rec = $recs[0];
$expiration = ( badgeos_utilities::get_post_meta( $rec->ID, '_open_badge_expiration', true ) ? badgeos_utilities::get_post_meta( $rec->ID, '_open_badge_expiration', true ) : '0' );
$expiration_type = ( badgeos_utilities::get_post_meta( $rec->ID, '_open_badge_expiration_type', true ) ? badgeos_utilities::get_post_meta( $rec->ID, '_open_badge_expiration_type', true ) : 'Day' );
$user = get_user_by( 'ID', $rec->user_id );
$achievement = badgeos_utilities::badgeos_get_post( $rec->ID );
wp_enqueue_style( 'badgeos-front' );
wp_enqueue_script( 'badgeos-achievements' );
$dirs = wp_upload_dir();
$baseurl = trailingslashit( $dirs['baseurl'] );
$basedir = trailingslashit( $dirs['basedir'] );
$badge_directory = trailingslashit( $basedir . 'user_badges/' . $user_id );
$badge_url = trailingslashit( $baseurl . 'user_badges/' . $user_id );
ob_start();
wp_enqueue_style( 'badgeos-font-awesome' );
wp_enqueue_style( 'badgeos-front' );
?>
<div class="evidence_main">
<div class="left_col">
<?php if ( ! empty( $rec->image ) && file_exists( $badge_directory . $rec->image ) ) { ?>
<img src="<?php echo esc_url( $badge_url . $rec->image ); ?>" with="100%" />
<?php } else { ?>
<?php echo wp_kses_post( apply_filters( 'badgeos_attachment_email', badgeos_get_achievement_post_thumbnail( $achievement_id, 'full' ), $achievement_id ) ); ?>
<?php } ?>
<div class="verification">
<input id="open-badgeos-verification" href="javascript:;" data-bg="<?php echo esc_attr( $achievement_id ); ?>" data-eid="<?php echo esc_attr( $entry_id ); ?>" data-uid="<?php echo esc_attr( $user_id ); ?>" class="verify-open-badge" value="<?php echo esc_attr_e( 'Verify', 'badgeos' ); ?>" type="button" />
</div>
<?php echo esc_attr( apply_filters( 'badgeos_evidence_after_left_column', '', $achievement ) ); ?>
</div>
<div class="right_col">
<h3 class="title"><?php echo esc_attr( $rec->achievement_title ); ?></h3>
<?php $content = ''; ?>
<?php echo wp_kses_post( apply_filters( 'badgeos_evidence_before_post_content', '', $content, $achievement ) ); ?>
<?php if ( $achievement ) { ?>
<?php $content = $achievement->post_content; ?>
<p>
<?php echo wp_kses_post( wpautop( $content ) ); ?>
</p>
<?php } ?>
<?php echo wp_kses_post( apply_filters( 'badgeos_evidence_after_post_content', '', $content, $achievement ) ); ?>
<div class="badgeos_user_name"><strong><?php echo esc_attr_e( 'Receiver', 'badgeos' ); ?>:</strong> <?php echo esc_attr( $user->first_name . ' ' . $user->last_name ); ?></div>
<div class="badgeos_issuer_name"><strong><?php echo esc_attr_e( 'Issuer', 'badgeos' ); ?>:</strong> <?php bloginfo( 'name' ); ?></div>
<div class="badgeos_issue_date"><strong><?php echo esc_attr_e( 'Issue Date', 'badgeos' ); ?>:</strong> <?php echo esc_attr( date( badgeos_utilities::get_option( 'date_format' ), strtotime( $rec->date_earned ) ) ); ?></div>
<?php if ( intval( $expiration ) > 0 ) { ?>
<div class="badgeos_expiry_date"><strong><?php echo esc_attr_e( 'Expiry Date', 'badgeos' ); ?>:</strong> <?php echo esc_attr( date( badgeos_utilities::get_option( 'date_format' ), strtotime( '+' . $expiration . ' ' . $expiration_type, strtotime( $rec->date_earned ) ) ) ); ?></div>
<?php } else { ?>
<div class="badgeos_expiry_date"><strong><?php echo esc_attr_e( 'Expiry Date', 'badgeos' ); ?>:</strong> <?php echo esc_attr_e( 'None', 'badgeos' ); ?></div>
<?php } ?>
<?php echo esc_attr( apply_filters( 'badgeos_evidence_after_right_column', '', $achievement ) ); ?>
</div>
</div>
<div id="modal" class="badgeos_verification_modal_popup">
<header class="badgeos_verification_popup_header">
<h2><?php echo esc_attr_e( 'Verification', 'badgeos' ); ?></h2>
<span class="controls">
<a href="#" class="badgeos_verification_close"></a>
</span>
</header>
<div class="badgeos_verification_modal_panel">
</div>
</div>
<?php
$output = ob_get_clean();
return apply_filters( 'badgeos_evidence_page_content', $output, $achievement );
}
/**
* Return our rendered achievement.
*/
return $output;
}