| @@ -1,0 +1,214 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Admin Dashboard | |
| 4 | + * | |
| 5 | + * @package GamiPress\Admin\Dashboard | |
| 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 dashboard widget. | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + * @updated 1.4.8 Added network and user checks | |
| 17 | + */ | |
| 18 | +function gamipress_dashboard_widgets() { | |
| 19 | + | |
| 20 | + // Bail if GamiPress is active network wide and we are not in main site | |
| 21 | + if( gamipress_is_network_wide_active() && ! is_main_site() ) { | |
| 22 | + return; | |
| 23 | + } | |
| 24 | + | |
| 25 | + // Bail if current user can manage GamiPress | |
| 26 | + if( ! current_user_can( gamipress_get_manager_capability() ) ) { | |
| 27 | + return; | |
| 28 | + } | |
| 29 | + | |
| 30 | + wp_add_dashboard_widget( 'gamipress', 'GamiPress', 'gamipress_dashboard_widget' ); | |
| 31 | + | |
| 32 | +} | |
| 33 | +add_action( 'wp_dashboard_setup', 'gamipress_dashboard_widgets' ); | |
| 34 | + | |
| 35 | +/** | |
| 36 | + * GamiPress dashboard widget output. | |
| 37 | + * | |
| 38 | + * @since 1.0.0 | |
| 39 | + */ | |
| 40 | +function gamipress_dashboard_widget() { | |
| 41 | + | |
| 42 | + // Get our types | |
| 43 | + $achievement_types = gamipress_get_achievement_types(); | |
| 44 | + $points_types = gamipress_get_points_types(); | |
| 45 | + $rank_types = gamipress_get_rank_types(); | |
| 46 | + ?> | |
| 47 | + | |
| 48 | + <h3> | |
| 49 | + <a href="<?php echo admin_url( 'edit.php?post_type=points-type' ); ?>" id="points-types"> | |
| 50 | + <?php echo gamipress_dashicon( 'star-filled' ); ?> | |
| 51 | + <?php ( count( $points_types ) === 0 ? esc_html_e( 'Points Types', 'gamipress' ) : printf( _n( '%d Points Type', '%d Points Types', count( $points_types ) ), count( $points_types ) ) ); ?> | |
| 52 | + </a> | |
| 53 | + </h3> | |
| 54 | + | |
| 55 | + <?php if( count( $points_types ) === 0 ) : ?> | |
| 56 | + | |
| 57 | + <p><?php esc_html_e( 'Points acts as a digital wallet for users. They can collect points while interacting with your site, then use them in different ways.', 'gamipress' ); ?></p> | |
| 58 | + | |
| 59 | + <div class="center"> | |
| 60 | + <a href="<?php echo admin_url( 'post-new.php?post_type=points-type' ); ?>" class="button button-primary"><?php esc_html_e( 'Create your first points type', 'gamipress' ); ?></a> | |
| 61 | + </div> | |
| 62 | + | |
| 63 | + <?php endif; ?> | |
| 64 | + | |
| 65 | + <div id="gamipress-registered-points" class="gamipress-registered-points"> | |
| 66 | + <ul> | |
| 67 | + <?php foreach( $points_types as $points_type_slug => $points_type) : ?> | |
| 68 | + <li> | |
| 69 | + <a href="<?php echo get_edit_post_link( $points_type['ID'] ); ?>"> | |
| 70 | + <?php echo esc_html( $points_type['plural_name'] ); ?> | |
| 71 | + </a> | |
| 72 | + </li> | |
| 73 | + <?php endforeach; ?> | |
| 74 | + </ul> | |
| 75 | + </div> | |
| 76 | + | |
| 77 | + <h3> | |
| 78 | + <a href="<?php echo admin_url( 'edit.php?post_type=achievement-type' ); ?>" id="achievement-types"> | |
| 79 | + <?php echo gamipress_dashicon( 'awards' ); ?> | |
| 80 | + <?php ( count( $achievement_types ) === 0 ? esc_html_e( 'Achievement Types', 'gamipress' ) :printf( _n( '%d Achievement Type', '%d Achievement Types', count( $achievement_types ) ), count( $achievement_types ) ) ); ?> | |
| 81 | + </a> | |
| 82 | + </h3> | |
| 83 | + | |
| 84 | + <?php if( count( $achievement_types ) === 0 ) : ?> | |
| 85 | + | |
| 86 | + <p><?php esc_html_e( 'Users can acquire achievements by completing the configured requirements. This reward often takes the form of "badges" or "stickers" that users can display on their profiles.', 'gamipress' ); ?></p> | |
| 87 | + | |
| 88 | + <div class="center"> | |
| 89 | + <a href="<?php echo admin_url( 'post-new.php?post_type=achievement-type' ); ?>" class="button button-primary"><?php esc_html_e( 'Create your first achievement type', 'gamipress' ); ?></a> | |
| 90 | + </div> | |
| 91 | + | |
| 92 | + <?php endif; ?> | |
| 93 | + | |
| 94 | + <div id="gamipress-registered-achievements" class="gamipress-registered-achievements"> | |
| 95 | + <ul> | |
| 96 | + <?php foreach( $achievement_types as $achievement_type_slug => $achievement_type) : ?> | |
| 97 | + <?php $achievement_type_count = wp_count_posts( $achievement_type_slug ); ?> | |
| 98 | + <li> | |
| 99 | + <a href="<?php echo admin_url( 'edit.php?post_type=' . $achievement_type_slug ); ?>"> | |
| 100 | + <?php printf( _n( '%d ' . $achievement_type['singular_name'], '%d ' . $achievement_type['plural_name'], $achievement_type_count->publish ), $achievement_type_count->publish ); ?> | |
| 101 | + </a> | |
| 102 | + </li> | |
| 103 | + <?php endforeach; ?> | |
| 104 | + </ul> | |
| 105 | + </div> | |
| 106 | + | |
| 107 | + <h3> | |
| 108 | + <a href="<?php echo admin_url( 'edit.php?post_type=rank-type' ); ?>" id="achievement-types"> | |
| 109 | + <?php echo gamipress_dashicon( 'rank' ); ?> | |
| 110 | + <?php ( count( $rank_types ) === 0 ? esc_html_e( 'Rank Types', 'gamipress' ) :printf( _n( '%d Rank Type', '%d Rank Types', count( $rank_types ) ), count( $rank_types ) ) ); ?> | |
| 111 | + </a> | |
| 112 | + </h3> | |
| 113 | + | |
| 114 | + <?php if( count( $rank_types ) === 0 ) : ?> | |
| 115 | + | |
| 116 | + <p><?php esc_html_e( 'Similar to achievements, ranks are awarded when users complete certain tasks. However, in this case, the criteria must be met in a specific order.', 'gamipress' ); ?></p> | |
| 117 | + | |
| 118 | + <div class="center"> | |
| 119 | + <a href="<?php echo admin_url( 'post-new.php?post_type=rank-type' ); ?>" class="button button-primary"><?php esc_html_e( 'Create your first rank type', 'gamipress' ); ?></a> | |
| 120 | + </div> | |
| 121 | + | |
| 122 | + <?php endif; ?> | |
| 123 | + | |
| 124 | + <div id="gamipress-registered-ranks" class="gamipress-registered-ranks"> | |
| 125 | + <ul> | |
| 126 | + <?php foreach( $rank_types as $rank_type_slug => $rank_type) : ?> | |
| 127 | + <?php $rank_type_count = wp_count_posts( $rank_type_slug ); ?> | |
| 128 | + <li> | |
| 129 | + <a href="<?php echo admin_url( 'edit.php?post_type=' . $rank_type_slug ); ?>"> | |
| 130 | + <?php printf( _n( '%d ' . $rank_type['singular_name'], '%d ' . $rank_type['plural_name'], $rank_type_count->publish ), $rank_type_count->publish ); ?> | |
| 131 | + </a> | |
| 132 | + </li> | |
| 133 | + <?php endforeach; ?> | |
| 134 | + </ul> | |
| 135 | + </div> | |
| 136 | + | |
| 137 | + <h3> | |
| 138 | + <?php echo gamipress_dashicon( 'flag' ); ?> | |
| 139 | + <?php _e( 'Latest User Earnings', 'gamipress' ); ?> | |
| 140 | + </h3> | |
| 141 | + | |
| 142 | + <?php | |
| 143 | + | |
| 144 | + // Setup table | |
| 145 | + ct_setup_table( 'gamipress_user_earnings' ); | |
| 146 | + | |
| 147 | + $query = new CT_Query( array( | |
| 148 | + 'orderby' => 'date', | |
| 149 | + 'order' => 'DESC', | |
| 150 | + 'items_per_page' => 5, | |
| 151 | + 'no_found_rows' => true, | |
| 152 | + 'cache_results' => false, | |
| 153 | + ) ); | |
| 154 | + | |
| 155 | + $user_earnings = $query->get_results(); | |
| 156 | + | |
| 157 | + if ( count( $user_earnings ) > 0 ) { | |
| 158 | + | |
| 159 | + echo '<div id="gamipress-latest-earnings" class="gamipress-latest-earnings">'; | |
| 160 | + | |
| 161 | + echo '<ul>'; | |
| 162 | + | |
| 163 | + $today = date( 'Y-m-d', current_time( 'timestamp' ) ); | |
| 164 | + $yesterday = date( 'Y-m-d', strtotime( '-1 day', current_time( 'timestamp' ) ) ); | |
| 165 | + | |
| 166 | + foreach ( $user_earnings as $user_earning ) { | |
| 167 | + | |
| 168 | + $time = strtotime( $user_earning->date ); | |
| 169 | + | |
| 170 | + if ( date( 'Y-m-d', $time ) === $today ) { | |
| 171 | + $relative = __( 'Today', 'gamipress' ); | |
| 172 | + } elseif ( date( 'Y-m-d', $time ) === $yesterday ) { | |
| 173 | + $relative = __( 'Yesterday', 'gamipress' ); | |
| 174 | + } elseif ( date( 'Y', $time ) !== date( 'Y', current_time( 'timestamp' ) ) ) { | |
| 175 | + /* translators: date and time format for recent posts on the dashboard, from a different calendar year, see https://secure.php.net/date */ | |
| 176 | + $relative = date_i18n( __( 'M jS Y' ), $time ); | |
| 177 | + } else { | |
| 178 | + /* translators: date and time format for recent posts on the dashboard, see https://secure.php.net/date */ | |
| 179 | + $relative = date_i18n( __( 'M jS' ), $time ); | |
| 180 | + } | |
| 181 | + | |
| 182 | + $user = get_userdata( $user_earning->user_id ); | |
| 183 | + | |
| 184 | + $user_display_name = $user->display_name; | |
| 185 | + | |
| 186 | + if( current_user_can( 'edit_users' ) ) { | |
| 187 | + $user_display_name = '<a href="' . get_edit_user_link( $user_earning->user_id ) . '">' . $user_display_name . '</a>'; | |
| 188 | + } | |
| 189 | + | |
| 190 | + $user_earning_title = '<strong>' . $user_earning->title . '</strong>'; | |
| 191 | + | |
| 192 | + $date = sprintf( _x( '%1$s, %2$s', 'dashboard' ), $relative, mysql2date( get_option( 'time_format' ), $user_earning->date ) ); | |
| 193 | + | |
| 194 | + // translators: %1$s: Username %2$s: Reward | |
| 195 | + $label = sprintf( __( '%1$s got %2$s', 'gamipress' ), | |
| 196 | + $user_display_name, | |
| 197 | + $user_earning_title | |
| 198 | + ); | |
| 199 | + | |
| 200 | + echo '<li>' . $label . '<span>' . $date . '</span>' . '</li>'; | |
| 201 | + } | |
| 202 | + | |
| 203 | + echo '</ul>'; | |
| 204 | + | |
| 205 | + echo '<a href="' . ct_get_list_link( 'gamipress_user_earnings' ) . '" class="gamipress-latest-earnings-view-all">' . esc_html__( 'View all user earnings', 'gamipress' ) . '</a>'; | |
| 206 | + | |
| 207 | + echo '</div>'; | |
| 208 | + | |
| 209 | + } else { | |
| 210 | + echo '<p>' . __( 'Nothing to show :)', 'gamipress' ) .'</p>'; | |
| 211 | + } | |
| 212 | + | |
| 213 | + wp_reset_postdata(); | |
| 214 | +} | |