| 1 |
<?php |
| 2 |
/** |
| 3 |
* GamiPress Achievements Shortcode |
| 4 |
* |
| 5 |
* @package GamiPress\Shortcodes\Shortcode\GamiPress_Achievements |
| 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 |
/** |
| 13 |
* Register [gamipress_achievements] shortcode |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
function gamipress_register_achievements_shortcode() { |
| 18 |
|
| 19 |
$achievement_fields = GamiPress()->shortcodes['gamipress_achievement']->fields; |
| 20 |
|
| 21 |
unset( $achievement_fields['id'] ); |
| 22 |
|
| 23 |
gamipress_register_shortcode( 'gamipress_achievements', array( |
| 24 |
'name' => __( 'Achievement List', 'gamipress' ), |
| 25 |
'description' => __( 'Display a list of achievements.', 'gamipress' ), |
| 26 |
'icon' => 'awards', |
| 27 |
'group' => 'gamipress', |
| 28 |
'output_callback' => 'gamipress_achievements_shortcode', |
| 29 |
'tabs' => array( |
| 30 |
'general' => array( |
| 31 |
'icon' => 'dashicons-admin-generic', |
| 32 |
'title' => __( 'General', 'gamipress' ), |
| 33 |
'fields' => array( |
| 34 |
'type', |
| 35 |
'columns', |
| 36 |
'columns_small', |
| 37 |
'filter', |
| 38 |
'filter_value', |
| 39 |
'search', |
| 40 |
'search_value', |
| 41 |
'load_more', |
| 42 |
), |
| 43 |
), |
| 44 |
'achievement' => array( |
| 45 |
'icon' => 'dashicons-awards', |
| 46 |
'title' => __( 'Achievement', 'gamipress' ), |
| 47 |
'fields' => array_keys( $achievement_fields ), |
| 48 |
), |
| 49 |
'query' => array( |
| 50 |
'icon' => 'dashicons-search', |
| 51 |
'title' => __( 'Query', 'gamipress' ), |
| 52 |
'fields' => array( |
| 53 |
'limit', |
| 54 |
'orderby', |
| 55 |
'order', |
| 56 |
'current_user', |
| 57 |
'user_id', |
| 58 |
'include', |
| 59 |
'exclude', |
| 60 |
'wpms', |
| 61 |
), |
| 62 |
), |
| 63 |
), |
| 64 |
'fields' => array_merge( array( |
| 65 |
'type' => array( |
| 66 |
'name' => __( 'Achievement Type(s)', 'gamipress' ), |
| 67 |
'tooltip' => __( 'Achievement type(s) to display.', 'gamipress' ), |
| 68 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 69 |
'shortcode_desc' => __( 'Single or comma-separated list of achievement type(s) to display.', 'gamipress' ), |
| 70 |
'type' => 'advanced_select', |
| 71 |
'classes' => 'gamipress-selector', |
| 72 |
'attributes' => array( |
| 73 |
'data-placeholder' => __( 'Default: All', 'gamipress' ), |
| 74 |
), |
| 75 |
'multiple' => true, |
| 76 |
'options_cb' => 'gamipress_options_cb_achievement_types', |
| 77 |
'default' => 'all', |
| 78 |
), |
| 79 |
'columns' => array( |
| 80 |
'name' => __( 'Columns', 'gamipress' ), |
| 81 |
'tooltip' => __( 'Columns to divide achievements.', 'gamipress' ), |
| 82 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 83 |
'type' => 'select', |
| 84 |
'options' => array( |
| 85 |
'1' => __( '1 Column', 'gamipress' ), |
| 86 |
'2' => __( '2 Columns', 'gamipress' ), |
| 87 |
'3' => __( '3 Columns', 'gamipress' ), |
| 88 |
'4' => __( '4 Columns', 'gamipress' ), |
| 89 |
'5' => __( '5 Columns', 'gamipress' ), |
| 90 |
'6' => __( '6 Columns', 'gamipress' ), |
| 91 |
), |
| 92 |
'default' => '1' |
| 93 |
), |
| 94 |
'columns_small' => array( |
| 95 |
'name' => __( 'Columns in small screens', 'gamipress' ), |
| 96 |
'tooltip' => __( 'Columns to divide achievements in small screens.', 'gamipress' ), |
| 97 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 98 |
'type' => 'select', |
| 99 |
'options' => array( |
| 100 |
'1' => __( '1 Column', 'gamipress' ), |
| 101 |
'2' => __( '2 Columns', 'gamipress' ), |
| 102 |
'3' => __( '3 Columns', 'gamipress' ), |
| 103 |
'4' => __( '4 Columns', 'gamipress' ), |
| 104 |
'5' => __( '5 Columns', 'gamipress' ), |
| 105 |
'6' => __( '6 Columns', 'gamipress' ), |
| 106 |
), |
| 107 |
'default' => '1' |
| 108 |
), |
| 109 |
'filter' => array( |
| 110 |
'name' => __( 'Show Filter', 'gamipress' ), |
| 111 |
'tooltip' => __( 'Display filter input.', 'gamipress' ), |
| 112 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 113 |
'type' => 'checkbox', |
| 114 |
'classes' => 'gamipress-switch', |
| 115 |
'default' => 'yes' |
| 116 |
), |
| 117 |
'filter_value' => array( |
| 118 |
'name' => __( 'Initial Filter Value', 'gamipress' ), |
| 119 |
'tooltip' => __( 'Set filter initial value. If you hide the filter, user won\'t be able to change this value.', 'gamipress' ), |
| 120 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 121 |
'type' => 'select', |
| 122 |
'options' => array( |
| 123 |
'all' => __( 'All Achievements', 'gamipress' ), |
| 124 |
'completed' => __( 'Completed Achievements', 'gamipress' ), |
| 125 |
'not-completed' => __( 'Not Completed Achievements', 'gamipress' ), |
| 126 |
), |
| 127 |
'default' => 'all' |
| 128 |
), |
| 129 |
'search' => array( |
| 130 |
'name' => __( 'Show Search', 'gamipress' ), |
| 131 |
'tooltip' => __( 'Display a search input.', 'gamipress' ), |
| 132 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 133 |
'type' => 'checkbox', |
| 134 |
'classes' => 'gamipress-switch', |
| 135 |
'default' => 'yes' |
| 136 |
), |
| 137 |
'search_value' => array( |
| 138 |
'name' => __( 'Initial Search Value', 'gamipress' ), |
| 139 |
'tooltip' => __( 'Set search initial value. If you hide the search, user won\'t be able to change this value.', 'gamipress' ), |
| 140 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 141 |
'type' => 'text', |
| 142 |
'default' => '' |
| 143 |
), |
| 144 |
'load_more' => array( |
| 145 |
'name' => __( 'Show the "Load More" button', 'gamipress' ), |
| 146 |
'tooltip' => __( 'Display a load more button.', 'gamipress' ), |
| 147 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 148 |
'type' => 'checkbox', |
| 149 |
'classes' => 'gamipress-switch', |
| 150 |
'default' => 'yes' |
| 151 |
), |
| 152 |
'limit' => array( |
| 153 |
'name' => __( 'Limit', 'gamipress' ), |
| 154 |
'tooltip' => __( 'Number of achievements to display.', 'gamipress' ), |
| 155 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 156 |
'type' => 'text', |
| 157 |
'default' => 10, |
| 158 |
), |
| 159 |
'orderby' => array( |
| 160 |
'name' => __( 'Order By', 'gamipress' ), |
| 161 |
'tooltip' => __( 'Parameter to use for sorting.', 'gamipress' ), |
| 162 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 163 |
'type' => 'select', |
| 164 |
'options' => array( |
| 165 |
'menu_order' => __( 'Menu order', 'gamipress' ), |
| 166 |
'ID' => __( 'Achievement', 'gamipress' ), |
| 167 |
'title' => __( 'Title', 'gamipress' ), |
| 168 |
'date' => __( 'Published date', 'gamipress' ), |
| 169 |
'modified' => __( 'Last modified date', 'gamipress' ), |
| 170 |
'author' => __( 'Author', 'gamipress' ), |
| 171 |
'rand' => __( 'Random', 'gamipress' ), |
| 172 |
'points_awarded' => __( 'Points awarded', 'gamipress' ), |
| 173 |
'points_to_unlock' => __( 'Points to unlock', 'gamipress' ), |
| 174 |
), |
| 175 |
'default' => 'menu_order', |
| 176 |
), |
| 177 |
'order' => array( |
| 178 |
'name' => __( 'Order', 'gamipress' ), |
| 179 |
'tooltip' => __( 'Sort order.', 'gamipress' ), |
| 180 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 181 |
'type' => 'select', |
| 182 |
'options' => array( 'ASC' => __( 'Ascending', 'gamipress' ), 'DESC' => __( 'Descending', 'gamipress' ) ), |
| 183 |
'default' => 'ASC', |
| 184 |
), |
| 185 |
'current_user' => array( |
| 186 |
'name' => __( 'Current User', 'gamipress' ), |
| 187 |
'tooltip' => __( 'Show achievements earned by the current logged in user.', 'gamipress' ), |
| 188 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 189 |
'type' => 'checkbox', |
| 190 |
'classes' => 'gamipress-switch', |
| 191 |
), |
| 192 |
'user_id' => array( |
| 193 |
'name' => __( 'User', 'gamipress' ), |
| 194 |
'tooltip' => __( 'Show achievements earned by a specific user.', 'gamipress' ), |
| 195 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 196 |
'type' => 'select', |
| 197 |
'default' => '', |
| 198 |
'options_cb' => 'gamipress_options_cb_users' |
| 199 |
), |
| 200 |
'include' => array( |
| 201 |
'name' => __( 'Include', 'gamipress' ), |
| 202 |
'tooltip' => __( 'Achievements to include.', 'gamipress' ), |
| 203 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 204 |
'shortcode_desc' => __( 'Comma-separated list of specific achievement IDs to include.', 'gamipress' ), |
| 205 |
'type' => 'advanced_select', |
| 206 |
'multiple' => true, |
| 207 |
'classes' => 'gamipress-post-selector', |
| 208 |
'attributes' => array( |
| 209 |
'data-post-type' => implode( ',', gamipress_get_achievement_types_slugs() ), |
| 210 |
'data-placeholder' => __( 'Select achievements', 'gamipress' ), |
| 211 |
), |
| 212 |
'default' => '', |
| 213 |
'options_cb' => 'gamipress_options_cb_posts' |
| 214 |
), |
| 215 |
'exclude' => array( |
| 216 |
'name' => __( 'Exclude', 'gamipress' ), |
| 217 |
'tooltip' => __( 'Achievements to exclude.', 'gamipress' ), |
| 218 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 219 |
'shortcode_desc' => __( 'Comma-separated list of specific achievement IDs to exclude.', 'gamipress' ), |
| 220 |
'type' => 'advanced_select', |
| 221 |
'multiple' => true, |
| 222 |
'classes' => 'gamipress-post-selector', |
| 223 |
'attributes' => array( |
| 224 |
'data-post-type' => implode( ',', gamipress_get_achievement_types_slugs() ), |
| 225 |
'data-placeholder' => __( 'Select achievements', 'gamipress' ), |
| 226 |
), |
| 227 |
'default' => '', |
| 228 |
'options_cb' => 'gamipress_options_cb_posts' |
| 229 |
), |
| 230 |
'wpms' => array( |
| 231 |
'name' => __( 'Include Multisite Achievements', 'gamipress' ), |
| 232 |
'tooltip' => __( 'Show achievements from all network sites.', 'gamipress' ), |
| 233 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 234 |
'type' => 'checkbox', |
| 235 |
'classes' => 'gamipress-switch', |
| 236 |
), |
| 237 |
), $achievement_fields ), |
| 238 |
) ); |
| 239 |
|
| 240 |
} |
| 241 |
add_action( 'init', 'gamipress_register_achievements_shortcode' ); |
| 242 |
|
| 243 |
/** |
| 244 |
* Achievement List Shortcode |
| 245 |
* |
| 246 |
* @since 1.0.0 |
| 247 |
* |
| 248 |
* @param array $atts Shortcode attributes |
| 249 |
* @param string $content Shortcode content |
| 250 |
* |
| 251 |
* @return string HTML markup |
| 252 |
*/ |
| 253 |
function gamipress_achievements_shortcode( $atts = array(), $content = '' ) { |
| 254 |
|
| 255 |
global $gamipress_template_args; |
| 256 |
|
| 257 |
// Initialize GamiPress template args global |
| 258 |
$gamipress_template_args = array(); |
| 259 |
|
| 260 |
$shortcode = 'gamipress_achievements'; |
| 261 |
|
| 262 |
$atts = shortcode_atts( array_merge( array( |
| 263 |
// Achievements atts |
| 264 |
'type' => 'all', |
| 265 |
'columns' => '1', |
| 266 |
'columns_small' => '1', |
| 267 |
'filter' => 'yes', |
| 268 |
'filter_value' => 'all', |
| 269 |
'search' => 'yes', |
| 270 |
'search_value' => '', |
| 271 |
'load_more' => 'yes', |
| 272 |
'current_user' => 'no', |
| 273 |
'user_id' => '0', |
| 274 |
'limit' => '10', |
| 275 |
'orderby' => 'menu_order', |
| 276 |
'order' => 'ASC', |
| 277 |
'include' => '', |
| 278 |
'exclude' => '', |
| 279 |
'wpms' => 'no', |
| 280 |
), gamipress_achievement_shortcode_defaults() ), $atts, $shortcode ); |
| 281 |
|
| 282 |
// Single type check to use dynamic template |
| 283 |
$is_single_type = false; |
| 284 |
$types = explode( ',', $atts['type'] ); |
| 285 |
|
| 286 |
if ( $atts['type'] !== 'all' && count( $types ) === 1 ) { |
| 287 |
$is_single_type = true; |
| 288 |
} |
| 289 |
|
| 290 |
// --------------------------- |
| 291 |
// Shortcode Errors |
| 292 |
// --------------------------- |
| 293 |
|
| 294 |
if( $is_single_type ) { |
| 295 |
|
| 296 |
// Check if achievement type is valid |
| 297 |
if ( ! in_array( $atts['type'], gamipress_get_achievement_types_slugs() ) ) |
| 298 |
return gamipress_shortcode_error( __( 'The type provided isn\'t a valid registered achievement type.', 'gamipress' ), $shortcode ); |
| 299 |
|
| 300 |
} else if( $atts['type'] !== 'all' ) { |
| 301 |
|
| 302 |
// Let's check if all types provided are wrong |
| 303 |
$all_types_wrong = true; |
| 304 |
|
| 305 |
foreach( $types as $type ) { |
| 306 |
if ( in_array( $type, gamipress_get_achievement_types_slugs() ) ) |
| 307 |
$all_types_wrong = false; |
| 308 |
} |
| 309 |
|
| 310 |
// just notify error if all types are wrong |
| 311 |
if( $all_types_wrong ) |
| 312 |
return gamipress_shortcode_error( __( 'All types provided aren\'t valid registered achievement types.', 'gamipress' ), $shortcode ); |
| 313 |
|
| 314 |
} |
| 315 |
|
| 316 |
// --------------------------- |
| 317 |
// Shortcode Processing |
| 318 |
// --------------------------- |
| 319 |
|
| 320 |
// Enqueue assets |
| 321 |
gamipress_enqueue_scripts(); |
| 322 |
|
| 323 |
// Force to set current user as user ID |
| 324 |
if( $atts['current_user'] === 'yes' ) { |
| 325 |
$atts['user_id'] = get_current_user_id(); |
| 326 |
} else if( absint( $atts['user_id'] ) === 0 ) { |
| 327 |
$atts['user_id'] = get_current_user_id(); |
| 328 |
} |
| 329 |
|
| 330 |
// Setup a query args to get the first page of achievements |
| 331 |
$query_args = $atts; |
| 332 |
|
| 333 |
// Initializes filter and search values |
| 334 |
$query_args['filter'] = $atts['filter_value']; |
| 335 |
$query_args['search'] = isset( $atts['search_value'] ) ? $atts['search_value'] : ''; |
| 336 |
|
| 337 |
$query = gamipress_achievements_shortcode_query( $query_args ); |
| 338 |
|
| 339 |
// GamiPress template args global |
| 340 |
// Important! This var need to be set before render the achievements list to prevent get overwritten |
| 341 |
// by gamipress_render_achievement() used on gamipress_achievements_shortcode_query() |
| 342 |
$gamipress_template_args = $atts; |
| 343 |
$gamipress_template_args['query'] = $query; |
| 344 |
$gamipress_template_args['types'] = $types; |
| 345 |
|
| 346 |
// If we're dealing with multiple achievement types |
| 347 |
if ( 'all' === $gamipress_template_args['type'] ) { |
| 348 |
$gamipress_template_args['plural_label'] = __( 'achievements', 'gamipress' ); |
| 349 |
} else { |
| 350 |
$gamipress_template_args['plural_label'] = ( 1 == count( $types ) && ! empty( $types[0] ) ) ? get_post_type_object( $types[0] )->labels->name : __( 'achievements', 'gamipress' ); |
| 351 |
} |
| 352 |
|
| 353 |
ob_start(); |
| 354 |
if( $is_single_type ) { |
| 355 |
gamipress_get_template_part( 'achievements', $atts['type'] ); |
| 356 |
} else { |
| 357 |
gamipress_get_template_part( 'achievements' ); |
| 358 |
} |
| 359 |
$output = ob_get_clean(); |
| 360 |
|
| 361 |
/** |
| 362 |
* Filter to override shortcode output |
| 363 |
* |
| 364 |
* @since 1.6.5 |
| 365 |
* |
| 366 |
* @param string $output Final output |
| 367 |
* @param array $atts Shortcode attributes |
| 368 |
* @param string $content Shortcode content |
| 369 |
*/ |
| 370 |
return apply_filters( 'gamipress_achievements_shortcode_output', $output, $atts, $content ); |
| 371 |
|
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* Achievement List Shortcode Query |
| 376 |
* |
| 377 |
* @since 1.4.5 |
| 378 |
* |
| 379 |
* @param array $args |
| 380 |
* |
| 381 |
* @return array |
| 382 |
*/ |
| 383 |
function gamipress_achievements_shortcode_query( $args = array() ) { |
| 384 |
|
| 385 |
// Setup our AJAX query vars |
| 386 |
$type = isset( $args['type'] ) ? $args['type'] : false; |
| 387 |
$limit = isset( $args['limit'] ) ? $args['limit'] : false; |
| 388 |
$offset = isset( $args['offset'] ) ? $args['offset'] : false; |
| 389 |
$filter = isset( $args['filter'] ) ? $args['filter'] : false; |
| 390 |
$search = isset( $args['search'] ) ? $args['search'] : false; |
| 391 |
$current_user = isset( $args['current_user'] ) ? $args['current_user'] : false; |
| 392 |
$user_id = isset( $args['user_id'] ) ? $args['user_id'] : false; |
| 393 |
$orderby = isset( $args['orderby'] ) ? $args['orderby'] : false; |
| 394 |
$order = isset( $args['order'] ) ? $args['order'] : false; |
| 395 |
$wpms = isset( $args['wpms'] ) ? $args['wpms'] : false; |
| 396 |
$include = isset( $args['include'] ) ? $args['include'] : array(); |
| 397 |
$exclude = isset( $args['exclude'] ) ? $args['exclude'] : array(); |
| 398 |
$showed_ids = isset( $args['showed_ids'] ) ? $args['showed_ids'] : array(); |
| 399 |
|
| 400 |
// On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails |
| 401 |
$blog_id = gamipress_switch_to_main_site_if_network_wide_active(); |
| 402 |
|
| 403 |
// Turn no attributes to false |
| 404 |
if( $current_user === 'no' ) { |
| 405 |
$current_user = false; |
| 406 |
} |
| 407 |
|
| 408 |
if( $wpms === 'no' ) { |
| 409 |
$wpms = false; |
| 410 |
} |
| 411 |
|
| 412 |
// Force to set current user as user ID |
| 413 |
if( $current_user ) { |
| 414 |
$user_id = get_current_user_id(); |
| 415 |
} |
| 416 |
|
| 417 |
// Get the current user if one wasn't specified |
| 418 |
if( ! $user_id ) { |
| 419 |
$user_id = get_current_user_id(); |
| 420 |
} |
| 421 |
|
| 422 |
// Ensure user ID as int |
| 423 |
$user_id = absint( $user_id ); |
| 424 |
|
| 425 |
// Setup template vars |
| 426 |
$template_args = array( |
| 427 |
'user_id' => $user_id, // User ID on achievement is used to meet to which user apply earned checks |
| 428 |
); |
| 429 |
|
| 430 |
$achievement_fields = GamiPress()->shortcodes['gamipress_achievement']->fields; |
| 431 |
|
| 432 |
unset( $achievement_fields['id'] ); |
| 433 |
|
| 434 |
// Loop achievement shortcode fields to pass them to the achievement template |
| 435 |
foreach( $achievement_fields as $field_id => $field_args ) { |
| 436 |
if( isset( $args[$field_id] ) ) |
| 437 |
$template_args[$field_id] = $args[$field_id]; |
| 438 |
} |
| 439 |
|
| 440 |
// Convert $type to properly support multiple achievement types |
| 441 |
if ( $type === 'all') { |
| 442 |
$type = gamipress_get_achievement_types_slugs(); |
| 443 |
} else { |
| 444 |
$type = explode( ',', $type ); |
| 445 |
} |
| 446 |
|
| 447 |
// Prevent empty strings to be turned an array by explode() |
| 448 |
if ( ! is_array( $include ) && empty( $include ) ) { |
| 449 |
$include = array(); |
| 450 |
} |
| 451 |
|
| 452 |
// Build $exclude array |
| 453 |
if ( ! is_array( $exclude ) && empty( $exclude ) ) { |
| 454 |
$exclude = array(); |
| 455 |
} |
| 456 |
|
| 457 |
// Build $include array |
| 458 |
if ( ! is_array( $include ) ) { |
| 459 |
$include = explode( ',', $include ); |
| 460 |
} |
| 461 |
|
| 462 |
// Build $exclude array |
| 463 |
if ( ! is_array( $exclude ) ) { |
| 464 |
$exclude = explode( ',', $exclude ); |
| 465 |
} |
| 466 |
|
| 467 |
// Initialize our output and counters |
| 468 |
$achievements = ''; |
| 469 |
$achievement_count = 0; |
| 470 |
$query_count = 0; |
| 471 |
|
| 472 |
// Grab our hidden achievements (used to filter the query) |
| 473 |
$hidden = gamipress_get_hidden_achievement_ids( $type ); |
| 474 |
|
| 475 |
if( $wpms && $wpms !== 'no' ) { |
| 476 |
// If we're polling all sites, grab an array of site IDs |
| 477 |
$sites = gamipress_get_network_site_ids(); |
| 478 |
} else { |
| 479 |
// Otherwise, use only the current site |
| 480 |
$sites = array( get_current_blog_id() ); |
| 481 |
} |
| 482 |
|
| 483 |
// On network wide active installs, force to just loop main site |
| 484 |
if( gamipress_is_network_wide_active() ) |
| 485 |
$sites = array( get_main_site_id() ); |
| 486 |
|
| 487 |
// Loop through each site (default is current site only) |
| 488 |
foreach( $sites as $site_blog_id ) { |
| 489 |
|
| 490 |
// If we're not polling the current site, switch to the site we're polling |
| 491 |
$current_site_blog_id = get_current_blog_id(); |
| 492 |
|
| 493 |
if ( $current_site_blog_id != $site_blog_id ) |
| 494 |
switch_to_blog( $site_blog_id ); |
| 495 |
|
| 496 |
// Grab user earned achievements (used to filter the query) |
| 497 |
$earned_ids = gamipress_get_user_earned_achievement_ids( $user_id, $type ); |
| 498 |
|
| 499 |
// If filter is set to load earned achievements and user hasn't earned anything, don't need to continue |
| 500 |
if( ! ( $filter === 'completed' && empty( $earned_ids ) ) ) { |
| 501 |
|
| 502 |
// Query Achievements |
| 503 |
$query_args = array( |
| 504 |
'post_type' => $type, |
| 505 |
'orderby' => $orderby, |
| 506 |
'order' => $order, |
| 507 |
'posts_per_page' => absint( $limit ), |
| 508 |
'offset' => absint( $offset ), |
| 509 |
'post_status' => 'publish', |
| 510 |
'post__in' => array(), |
| 511 |
'post__not_in' => array_diff( $hidden, $earned_ids ) |
| 512 |
); |
| 513 |
|
| 514 |
// Filter - query completed or non completed achievements |
| 515 |
if( $filter === 'completed' && ! empty( $earned_ids ) ) { |
| 516 |
// Include earned achievements |
| 517 |
$query_args[ 'post__in' ] = $earned_ids; |
| 518 |
} else if( $filter === 'not-completed' && ! empty( $earned_ids ) ) { |
| 519 |
// Exclude earned achievements |
| 520 |
$query_args[ 'post__not_in' ] = array_merge( $hidden, $earned_ids ); |
| 521 |
} |
| 522 |
|
| 523 |
// Include certain achievements |
| 524 |
if( ! empty( $include ) ) { |
| 525 |
$query_args[ 'post__not_in' ] = array_diff( $query_args[ 'post__not_in' ], $include ); |
| 526 |
$query_args[ 'post__in' ] = array_merge( $query_args[ 'post__in' ], $include ); |
| 527 |
} |
| 528 |
|
| 529 |
// Exclude certain achievements |
| 530 |
if( ! empty( $exclude ) ) { |
| 531 |
$query_args[ 'post__not_in' ] = array_merge( $query_args[ 'post__not_in' ], $exclude ); |
| 532 |
} |
| 533 |
|
| 534 |
// Search |
| 535 |
if( $search ) { |
| 536 |
$query_args[ 's' ] = $search; |
| 537 |
} |
| 538 |
|
| 539 |
// Order By |
| 540 |
if( in_array( $orderby, array( 'points_awarded', 'points_to_unlock' ) ) ) { |
| 541 |
$query_args['meta_key'] = ( $orderby === 'points_awarded' ? '_gamipress_points' : '_gamipress_points_to_unlock' ); |
| 542 |
$query_args['orderby'] = 'meta_value_num'; |
| 543 |
} |
| 544 |
|
| 545 |
// Process already displayed achievements |
| 546 |
if( ! empty( $showed_ids ) ) { |
| 547 |
// Exclude already displayed achievements |
| 548 |
$query_args[ 'post__in' ] = array_diff( $query_args[ 'post__in' ], $showed_ids ); |
| 549 |
$query_args[ 'post__not_in' ] = array_merge( $query_args[ 'post__not_in' ], $showed_ids ); |
| 550 |
// Offset not needed since displayed post are getting already excluded |
| 551 |
unset( $query_args['offset'] ); |
| 552 |
} |
| 553 |
|
| 554 |
// Prevent to display posts excluded |
| 555 |
if( ! empty( $query_args[ 'post__in' ] ) && ! empty( $query_args[ 'post__not_in' ] ) ) { |
| 556 |
$query_args[ 'post__in' ] = array_diff( $query_args[ 'post__in' ], $query_args[ 'post__not_in' ] ); |
| 557 |
} |
| 558 |
|
| 559 |
/** |
| 560 |
* Filters achievements list query args |
| 561 |
* |
| 562 |
* @since 1.5.9 |
| 563 |
* |
| 564 |
* @param array $query_args Query args to be passed to WP_Query |
| 565 |
* @param array $args Function received args (Note: to pass your own args on achievements list request, check JS event 'gamipress_achievements_list_request_data') |
| 566 |
* |
| 567 |
* @return array |
| 568 |
*/ |
| 569 |
$query_args = apply_filters( 'gamipress_achievements_shortcode_query_args', $query_args, $args ); |
| 570 |
|
| 571 |
// Query achievements |
| 572 |
$achievement_posts = new WP_Query( $query_args ); |
| 573 |
$query_count = absint( $achievement_posts->found_posts ); |
| 574 |
|
| 575 |
// Add displayed post to the count since they have been excluded on the query |
| 576 |
if( ! empty( $showed_ids ) ) |
| 577 |
$query_count += count( $showed_ids ); |
| 578 |
|
| 579 |
// Loop achievements found |
| 580 |
while( $achievement_posts->have_posts() ) : $achievement_posts->the_post(); |
| 581 |
// Render the achievement passing the template args |
| 582 |
$achievements .= gamipress_render_achievement( get_the_ID(), $template_args ); |
| 583 |
$achievement_count++; |
| 584 |
endwhile; |
| 585 |
|
| 586 |
} |
| 587 |
|
| 588 |
// Display a message for no results |
| 589 |
if( empty( $achievements ) && $offset === 0 ) { |
| 590 |
|
| 591 |
$current = current( $type ); |
| 592 |
|
| 593 |
// If we have exactly one achievement type, get its plural name, otherwise use "achievements" |
| 594 |
$post_type_plural = ( count( $type ) == 1 && ! empty( $current ) ) ? gamipress_get_achievement_type_plural( $current ) : __( 'achievements' , 'gamipress' ); |
| 595 |
|
| 596 |
// Setup our completion message |
| 597 |
$achievements .= '<div class="gamipress-no-results">'; |
| 598 |
|
| 599 |
if ( 'completed' === $filter ) |
| 600 |
$no_results_text = sprintf( __( 'No completed %s to display.', 'gamipress' ), strtolower( $post_type_plural ) ); |
| 601 |
else if ( 'not-completed' === $filter ) |
| 602 |
$no_results_text = sprintf( __( 'You completed all %s.', 'gamipress' ), strtolower( $post_type_plural ) ); |
| 603 |
else |
| 604 |
$no_results_text = sprintf( __( 'No %s to display.', 'gamipress' ), strtolower( $post_type_plural ) ); |
| 605 |
|
| 606 |
/** |
| 607 |
* Filter achievements no results text |
| 608 |
* |
| 609 |
* @since 1.0.0 |
| 610 |
* |
| 611 |
* @param string $no_results_text |
| 612 |
* |
| 613 |
* @return string |
| 614 |
*/ |
| 615 |
$no_results_text = apply_filters( 'gamipress_achievements_no_results_text', $no_results_text ); |
| 616 |
|
| 617 |
$achievements .= '<p>' . $no_results_text . '</p>'; |
| 618 |
|
| 619 |
$achievements .= '</div><!-- .gamipress-no-results -->'; |
| 620 |
} |
| 621 |
|
| 622 |
// Come back to current blog |
| 623 |
if( $current_site_blog_id != $site_blog_id && is_multisite() ) |
| 624 |
restore_current_blog(); |
| 625 |
|
| 626 |
} |
| 627 |
|
| 628 |
// If switched to blog, return back to que current blog |
| 629 |
if( $blog_id !== get_current_blog_id() && is_multisite() ) |
| 630 |
restore_current_blog(); |
| 631 |
|
| 632 |
$query = array( |
| 633 |
'achievements' => $achievements, |
| 634 |
'offset' => $offset + $limit, |
| 635 |
'query_count' => $query_count, |
| 636 |
'achievement_count' => $achievement_count, |
| 637 |
); |
| 638 |
|
| 639 |
/** |
| 640 |
* Filter the achievements query |
| 641 |
* |
| 642 |
* @since 1.6.5 |
| 643 |
* |
| 644 |
* @param array $response |
| 645 |
* @param array $args |
| 646 |
* |
| 647 |
* @return array |
| 648 |
*/ |
| 649 |
return apply_filters( 'gamipress_achievements_shortcode_query', $query, $args ); |
| 650 |
|
| 651 |
} |
| 652 |
|