| @@ -1,0 +1,175 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Admin Achievements | |
| 4 | + * | |
| 5 | + * @package GamiPress\Admin\Achievements | |
| 6 | + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.3.9.5 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Register achievements related hooks | |
| 14 | + * | |
| 15 | + * @since 1.3.9.5 | |
| 16 | + */ | |
| 17 | +function gamipress_load_achievements_admin_hooks() { | |
| 18 | + | |
| 19 | + foreach( gamipress_get_achievement_types() as $slug => $achievement ) { | |
| 20 | + add_filter( "manage_{$slug}_posts_columns", 'gamipress_achievement_posts_columns' ); | |
| 21 | + add_action( "manage_{$slug}_posts_custom_column", 'gamipress_achievement_posts_custom_columns', 10, 2 ); | |
| 22 | + } | |
| 23 | + | |
| 24 | +} | |
| 25 | +add_action( 'admin_init', 'gamipress_load_achievements_admin_hooks' ); | |
| 26 | + | |
| 27 | +/** | |
| 28 | + * Register our custom achievements columns | |
| 29 | + * | |
| 30 | + * @since 1.3.9.5 | |
| 31 | + * | |
| 32 | + * @param array $posts_columns | |
| 33 | + * | |
| 34 | + * @return array | |
| 35 | + */ | |
| 36 | +function gamipress_achievement_posts_columns( $posts_columns ) { | |
| 37 | + | |
| 38 | + $posts_columns['title'] = __( 'Name', 'gamipress' ); | |
| 39 | + | |
| 40 | + // Prepend the thumbnail column | |
| 41 | + $chunks = array_chunk( $posts_columns, 1, true ); | |
| 42 | + $chunks[0]['thumbnail'] = __( 'Image', 'gamipress' ); | |
| 43 | + | |
| 44 | + $posts_columns = call_user_func_array( 'array_merge', $chunks ); | |
| 45 | + | |
| 46 | + // Try to place our column before date column | |
| 47 | + $pos = array_search( 'author', array_keys( $posts_columns ) ); | |
| 48 | + | |
| 49 | + if ( ! is_int( $pos ) ) { | |
| 50 | + $pos = 1; | |
| 51 | + } | |
| 52 | + | |
| 53 | + // Place our column in our desired position | |
| 54 | + $chunks = array_chunk( $posts_columns, $pos, true ); | |
| 55 | + $chunks[0]['points'] = __( 'Points Awarded', 'gamipress' ); | |
| 56 | + $chunks[0]['earned_by'] = __( 'Earned By', 'gamipress' ); | |
| 57 | + $chunks[0]['maximum_earnings'] = __( 'Max. Earnings', 'gamipress' ); | |
| 58 | + $chunks[0]['unlock_with_points'] = __( 'Unlock with Points', 'gamipress' ); | |
| 59 | + | |
| 60 | + return call_user_func_array( 'array_merge', $chunks ); | |
| 61 | + | |
| 62 | +} | |
| 63 | + | |
| 64 | +/** | |
| 65 | + * Output for our custom achievements columns | |
| 66 | + * | |
| 67 | + * @since 1.3.9.5 | |
| 68 | + * | |
| 69 | + * @param string $column_name | |
| 70 | + * @param integer $post_id | |
| 71 | + */ | |
| 72 | +function gamipress_achievement_posts_custom_columns( $column_name, $post_id ) { | |
| 73 | + | |
| 74 | + $prefix = '_gamipress_'; | |
| 75 | + | |
| 76 | + switch( $column_name ) { | |
| 77 | + case 'thumbnail': | |
| 78 | + $can_edit_post = current_user_can( 'edit_post', $post_id ); | |
| 79 | + | |
| 80 | + if( $can_edit_post ) { | |
| 81 | + printf( | |
| 82 | + '<a href="%s" aria-label="%s">%s</a>', | |
| 83 | + get_edit_post_link( $post_id ), | |
| 84 | + /* translators: %s: post title */ | |
| 85 | + esc_attr( sprintf( __( '“%s” (Edit)' ), get_post_field( 'post_title', $post_id ) ) ), | |
| 86 | + gamipress_get_the_post_thumbnail( $post_id, array( 32, 32 ) ) | |
| 87 | + ); | |
| 88 | + } else { | |
| 89 | + echo gamipress_get_the_post_thumbnail( $post_id, array( 32, 32 ) ); | |
| 90 | + } | |
| 91 | + | |
| 92 | + break; | |
| 93 | + case 'points': | |
| 94 | + | |
| 95 | + // Get the points vars | |
| 96 | + $awarded_points_type = gamipress_get_post_meta( $post_id, $prefix . 'points_type' ); | |
| 97 | + $points = absint( gamipress_get_post_meta( $post_id, $prefix . 'points' ) ); | |
| 98 | + | |
| 99 | + if( $points !== 0 ) { | |
| 100 | + echo gamipress_format_points( $points, $awarded_points_type ); | |
| 101 | + } | |
| 102 | + | |
| 103 | + break; | |
| 104 | + case 'earned_by': | |
| 105 | + | |
| 106 | + $earned_by_options = apply_filters( 'gamipress_achievement_earned_by', array( | |
| 107 | + 'triggers' => __( 'Completing Steps', 'gamipress' ), | |
| 108 | + 'points' => __( 'Minimum Number of Points', 'gamipress' ), | |
| 109 | + 'rank' => __( 'Reach a Rank', 'gamipress' ), | |
| 110 | + 'admin' => __( 'Admin-awarded Only', 'gamipress' ), | |
| 111 | + ) ); | |
| 112 | + | |
| 113 | + $earned_by = gamipress_get_post_meta( $post_id, $prefix . 'earned_by' ); | |
| 114 | + | |
| 115 | + echo ( isset( $earned_by_options[$earned_by] ) ? $earned_by_options[$earned_by] : $earned_by ); | |
| 116 | + | |
| 117 | + switch( $earned_by ) { | |
| 118 | + case 'points': | |
| 119 | + | |
| 120 | + // Get the points vars | |
| 121 | + $required_points_type = gamipress_get_post_meta( $post_id, $prefix . 'points_type_required' ); | |
| 122 | + $required_points = absint( gamipress_get_post_meta( $post_id, $prefix . 'points_required' ) ); | |
| 123 | + | |
| 124 | + if( $required_points !== 0 ) { | |
| 125 | + echo '<br><strong>' . gamipress_format_points( $required_points, $required_points_type ) . '</strong>'; | |
| 126 | + } | |
| 127 | + | |
| 128 | + break; | |
| 129 | + case 'rank': | |
| 130 | + | |
| 131 | + // Get the rank ID | |
| 132 | + $required_rank_id = absint( gamipress_get_post_meta( $post_id, $prefix . 'rank_required' ) ); | |
| 133 | + | |
| 134 | + if( $required_rank_id !== 0 ) { | |
| 135 | + echo '<br><strong><a href="' . get_edit_post_link( $required_rank_id ) . '">' . gamipress_get_post_field( 'post_title', $required_rank_id ) . '</a></strong>'; | |
| 136 | + } | |
| 137 | + | |
| 138 | + break; | |
| 139 | + } | |
| 140 | + | |
| 141 | + break; | |
| 142 | + case 'maximum_earnings': | |
| 143 | + $unlimited = __( 'Unlimited', 'gamipress' ); | |
| 144 | + $unlimited_symbol = '<span title="' . esc_attr( $unlimited ) . '">∞</span>'; | |
| 145 | + $maximum_earnings = absint( gamipress_get_post_meta( $post_id, $prefix . 'maximum_earnings' ) ); | |
| 146 | + $label = '<strong>' . ( $maximum_earnings === 0 ? $unlimited_symbol : $maximum_earnings ) . '</strong>'; | |
| 147 | + | |
| 148 | + echo sprintf( __( '%s time(s) per user', 'gamipress' ), $label ); | |
| 149 | + echo "<br>"; | |
| 150 | + | |
| 151 | + $global_maximum_earnings = absint( gamipress_get_post_meta( $post_id, $prefix . 'global_maximum_earnings' ) ); | |
| 152 | + $label = '<strong>' . ( $global_maximum_earnings === 0 ? $unlimited_symbol : $global_maximum_earnings ) . '</strong>'; | |
| 153 | + | |
| 154 | + echo sprintf( __( '%s time(s) for all users', 'gamipress' ), $label ); | |
| 155 | + break; | |
| 156 | + case 'unlock_with_points': | |
| 157 | + | |
| 158 | + $unlock_with_points = gamipress_get_post_meta( $post_id, $prefix . 'unlock_with_points' ); | |
| 159 | + | |
| 160 | + if( ! (bool) $unlock_with_points ) { | |
| 161 | + break; | |
| 162 | + } | |
| 163 | + | |
| 164 | + // Get the points vars | |
| 165 | + $points_type_to_unlock = gamipress_get_post_meta( $post_id, $prefix . 'points_type_to_unlock' ); | |
| 166 | + $points_to_unlock = absint( gamipress_get_post_meta( $post_id, $prefix . 'points_to_unlock' ) ); | |
| 167 | + | |
| 168 | + if( $points_to_unlock !== 0 ) { | |
| 169 | + echo gamipress_format_points( $points_to_unlock, $points_type_to_unlock ); | |
| 170 | + } | |
| 171 | + | |
| 172 | + break; | |
| 173 | + } | |
| 174 | + | |
| 175 | +} | |