| 1 |
<?php |
| 2 |
/** |
| 3 |
* GamiPress Last Achievements Earned Shortcode |
| 4 |
* |
| 5 |
* @package GamiPress\Shortcodes\Shortcode\GamiPress_Last_Achievements_Earned |
| 6 |
* @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
// Register the singular separately to keep backward compatibility |
| 13 |
add_shortcode( 'gamipress_last_achievement_earned', 'gamipress_last_achievements_earned_shortcode' ); |
| 14 |
|
| 15 |
/** |
| 16 |
* Register [gamipress_last_achievements_earned] shortcode |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
*/ |
| 20 |
function gamipress_register_last_achievements_earned_shortcode() { |
| 21 |
|
| 22 |
$achievement_fields = GamiPress()->shortcodes['gamipress_achievement']->fields; |
| 23 |
|
| 24 |
unset( $achievement_fields['id'] ); |
| 25 |
|
| 26 |
// Register as singular to keep backward compatibility |
| 27 |
gamipress_register_shortcode( 'gamipress_last_achievements_earned', array( |
| 28 |
'name' => __( 'Last Achievements Earned', 'gamipress' ), |
| 29 |
'description' => __( 'Display the last achievements earned by the current user or a desired user.', 'gamipress' ), |
| 30 |
'icon' => 'awards', |
| 31 |
'group' => 'gamipress', |
| 32 |
'output_callback' => 'gamipress_last_achievements_earned_shortcode', |
| 33 |
'tabs' => array( |
| 34 |
'general' => array( |
| 35 |
'icon' => 'dashicons-admin-generic', |
| 36 |
'title' => __( 'General', 'gamipress' ), |
| 37 |
'fields' => array( |
| 38 |
'type', |
| 39 |
'current_user', |
| 40 |
'user_id', |
| 41 |
'limit', |
| 42 |
'columns', |
| 43 |
'columns_small', |
| 44 |
), |
| 45 |
), |
| 46 |
'achievement' => array( |
| 47 |
'icon' => 'dashicons-awards', |
| 48 |
'title' => __( 'Achievement', 'gamipress' ), |
| 49 |
'fields' => array_keys( $achievement_fields ), |
| 50 |
), |
| 51 |
), |
| 52 |
'fields' => array_merge( array( |
| 53 |
'type' => array( |
| 54 |
'name' => __( 'Achievement Type', 'gamipress' ), |
| 55 |
'tooltip' => __( 'The type of the last achievement earned.', 'gamipress' ), |
| 56 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 57 |
'type' => 'select', |
| 58 |
'classes' => 'gamipress-selector', |
| 59 |
'option_none' => true, |
| 60 |
'option_all' => false, |
| 61 |
'options_cb' => 'gamipress_options_cb_achievement_types', |
| 62 |
'default' => '', |
| 63 |
), |
| 64 |
'current_user' => array( |
| 65 |
'name' => __( 'Current User', 'gamipress' ), |
| 66 |
'tooltip' => __( 'Show the last achievement earned by the current logged in user.', 'gamipress' ), |
| 67 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 68 |
'type' => 'checkbox', |
| 69 |
'classes' => 'gamipress-switch', |
| 70 |
'default' => 'yes', |
| 71 |
), |
| 72 |
'user_id' => array( |
| 73 |
'name' => __( 'User', 'gamipress' ), |
| 74 |
'tooltip' => __( 'Show the last achievement earned by a specific user.', 'gamipress' ), |
| 75 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 76 |
'type' => 'select', |
| 77 |
'default' => '', |
| 78 |
'options_cb' => 'gamipress_options_cb_users' |
| 79 |
), |
| 80 |
'limit' => array( |
| 81 |
'name' => __( 'Limit', 'gamipress' ), |
| 82 |
'tooltip' => __( 'Number of achievements to display.', 'gamipress' ), |
| 83 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 84 |
'type' => 'text', |
| 85 |
'default' => '1', |
| 86 |
), |
| 87 |
'columns' => array( |
| 88 |
'name' => __( 'Columns', 'gamipress' ), |
| 89 |
'tooltip' => __( 'Columns to divide achievements (only used when limit is higher than 1).', 'gamipress' ), |
| 90 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 91 |
'type' => 'select', |
| 92 |
'options' => array( |
| 93 |
'1' => __( '1 Column', 'gamipress' ), |
| 94 |
'2' => __( '2 Columns', 'gamipress' ), |
| 95 |
'3' => __( '3 Columns', 'gamipress' ), |
| 96 |
'4' => __( '4 Columns', 'gamipress' ), |
| 97 |
'5' => __( '5 Columns', 'gamipress' ), |
| 98 |
'6' => __( '6 Columns', 'gamipress' ), |
| 99 |
), |
| 100 |
'default' => '1' |
| 101 |
), |
| 102 |
'columns_small' => array( |
| 103 |
'name' => __( 'Columns in small screens', 'gamipress' ), |
| 104 |
'tooltip' => __( 'Columns to divide achievements in small screens (only used when limit is higher than 1).', 'gamipress' ), |
| 105 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 106 |
'type' => 'select', |
| 107 |
'options' => array( |
| 108 |
'1' => __( '1 Column', 'gamipress' ), |
| 109 |
'2' => __( '2 Columns', 'gamipress' ), |
| 110 |
'3' => __( '3 Columns', 'gamipress' ), |
| 111 |
'4' => __( '4 Columns', 'gamipress' ), |
| 112 |
'5' => __( '5 Columns', 'gamipress' ), |
| 113 |
'6' => __( '6 Columns', 'gamipress' ), |
| 114 |
), |
| 115 |
'default' => '1' |
| 116 |
), |
| 117 |
), $achievement_fields ), |
| 118 |
) ); |
| 119 |
|
| 120 |
} |
| 121 |
add_action( 'init', 'gamipress_register_last_achievements_earned_shortcode' ); |
| 122 |
|
| 123 |
/** |
| 124 |
* Last Achievements Earned Shortcode |
| 125 |
* |
| 126 |
* @since 1.0.0 |
| 127 |
* |
| 128 |
* @param array $atts Shortcode attributes |
| 129 |
* @param string $content Shortcode content |
| 130 |
* |
| 131 |
* @return string HTML markup |
| 132 |
*/ |
| 133 |
function gamipress_last_achievements_earned_shortcode( $atts = array(), $content = '' ) { |
| 134 |
|
| 135 |
global $gamipress_template_args; |
| 136 |
|
| 137 |
// Initialize GamiPress template args global |
| 138 |
$gamipress_template_args = array(); |
| 139 |
|
| 140 |
$shortcode = 'gamipress_last_achievements_earned'; |
| 141 |
|
| 142 |
$atts = shortcode_atts( array_merge( array( |
| 143 |
'type' => '', |
| 144 |
'current_user' => 'yes', |
| 145 |
'user_id' => '0', |
| 146 |
'limit' => '1', |
| 147 |
'columns' => '1', |
| 148 |
'columns_small' => '1', |
| 149 |
), gamipress_achievement_shortcode_defaults() ), $atts, $shortcode ); |
| 150 |
|
| 151 |
// --------------------------- |
| 152 |
// Shortcode Errors |
| 153 |
// --------------------------- |
| 154 |
|
| 155 |
// Check if achievement type is valid |
| 156 |
if ( ! in_array( $atts['type'], gamipress_get_achievement_types_slugs() ) ) { |
| 157 |
return gamipress_shortcode_error( __( 'The type provided isn\'t a valid registered achievement type.', 'gamipress' ), $shortcode ); |
| 158 |
} |
| 159 |
|
| 160 |
// --------------------------- |
| 161 |
// Shortcode Processing |
| 162 |
// --------------------------- |
| 163 |
|
| 164 |
// Enqueue assets |
| 165 |
gamipress_enqueue_scripts(); |
| 166 |
|
| 167 |
// Force to set current user as user ID |
| 168 |
if( $atts['current_user'] === 'yes' ) { |
| 169 |
$atts['user_id'] = get_current_user_id(); |
| 170 |
} else if( absint( $atts['user_id'] ) === 0 ) { |
| 171 |
$atts['user_id'] = get_current_user_id(); |
| 172 |
} |
| 173 |
|
| 174 |
if( absint( $atts['user_id'] ) === 0 ) { |
| 175 |
/** |
| 176 |
* Filter to override shortcode output when no user has been found |
| 177 |
* |
| 178 |
* @since 2.1.2 |
| 179 |
* |
| 180 |
* @param string $output Final output |
| 181 |
* @param array $atts Shortcode attributes |
| 182 |
* @param string $content Shortcode content |
| 183 |
*/ |
| 184 |
return apply_filters( 'gamipress_last_achievements_earned_shortcode_no_user_output', '', $atts, $content ); |
| 185 |
} |
| 186 |
|
| 187 |
$atts['limit'] = absint( $atts['limit'] ); |
| 188 |
|
| 189 |
|
| 190 |
if( $atts['limit'] === 1 ) { |
| 191 |
// Display a single achievement |
| 192 |
|
| 193 |
$achievement_id = gamipress_get_last_earning_post_id( array( |
| 194 |
'post_type' => $atts['type'], |
| 195 |
'user_id' => $atts['user_id'], |
| 196 |
) ); |
| 197 |
|
| 198 |
if( absint( $achievement_id ) === 0 ) { |
| 199 |
/** |
| 200 |
* Filter to override the shortcode output when no achievement has been found |
| 201 |
* |
| 202 |
* @since 2.1.2 |
| 203 |
* |
| 204 |
* @param string $output Final output |
| 205 |
* @param array $atts Shortcode attributes |
| 206 |
* @param string $content Shortcode content |
| 207 |
*/ |
| 208 |
return apply_filters( 'gamipress_last_achievements_earned_shortcode_no_achievement_output', '', $atts, $content ); |
| 209 |
} |
| 210 |
|
| 211 |
// Set the achievement ID |
| 212 |
$atts['id'] = $achievement_id; |
| 213 |
|
| 214 |
$output = gamipress_achievement_shortcode( $atts, $content ); |
| 215 |
} else { |
| 216 |
// Display a list of achievements |
| 217 |
|
| 218 |
$last_earnings = gamipress_get_last_earnings( array( |
| 219 |
'post_type' => $atts['type'], |
| 220 |
'user_id' => $atts['user_id'], |
| 221 |
'limit' => $atts['limit'], |
| 222 |
) ); |
| 223 |
|
| 224 |
if( ! $last_earnings ) { |
| 225 |
/** |
| 226 |
* Filter to override the shortcode output when no achievements has been found |
| 227 |
* |
| 228 |
* @since 2.1.2 |
| 229 |
* |
| 230 |
* @param string $output Final output |
| 231 |
* @param array $atts Shortcode attributes |
| 232 |
* @param string $content Shortcode content |
| 233 |
*/ |
| 234 |
return apply_filters( 'gamipress_last_achievements_earned_shortcode_no_achievement_output', '', $atts, $content ); |
| 235 |
} |
| 236 |
|
| 237 |
// Render the achievements |
| 238 |
$achievements = ''; |
| 239 |
|
| 240 |
foreach( $last_earnings as $earning ) { |
| 241 |
$achievements .= gamipress_render_achievement( $earning->post_id, $atts ); |
| 242 |
} |
| 243 |
|
| 244 |
// Setup the template args |
| 245 |
$gamipress_template_args = $atts; |
| 246 |
$gamipress_template_args['filter'] = 'no'; |
| 247 |
$gamipress_template_args['filter_value'] = 'all'; |
| 248 |
$gamipress_template_args['search'] = 'no'; |
| 249 |
$gamipress_template_args['load_more'] = 'no'; |
| 250 |
$gamipress_template_args['query'] = array( |
| 251 |
'achievements' => $achievements, |
| 252 |
'offset' => $atts['limit'], |
| 253 |
'query_count' => $atts['limit'], |
| 254 |
'achievement_count' => $atts['limit'], |
| 255 |
); |
| 256 |
|
| 257 |
ob_start(); |
| 258 |
gamipress_get_template_part( 'achievements', $atts['type'] ); |
| 259 |
$output = ob_get_clean(); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
/** |
| 264 |
* Filter to override shortcode output |
| 265 |
* |
| 266 |
* @since 2.1.2 |
| 267 |
* |
| 268 |
* @param string $output Final output |
| 269 |
* @param array $atts Shortcode attributes |
| 270 |
* @param string $content Shortcode content |
| 271 |
*/ |
| 272 |
return apply_filters( 'gamipress_last_achievements_earned_shortcode_output', $output, $atts, $content ); |
| 273 |
|
| 274 |
} |
| 275 |
|