| @@ -1,0 +1,95 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Admin Requirements | |
| 4 | + * | |
| 5 | + * @package GamiPress\Admin\Requirements | |
| 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 our custom requirements columns | |
| 14 | + * | |
| 15 | + * @since 1.0.6 | |
| 16 | + * | |
| 17 | + * @param $posts_columns | |
| 18 | + * @param $post_type | |
| 19 | + * | |
| 20 | + * @return mixed | |
| 21 | + */ | |
| 22 | +function gamipress_requirements_posts_columns( $posts_columns, $post_type ) { | |
| 23 | + | |
| 24 | + if( ! in_array( $post_type, gamipress_get_requirement_types_slugs() ) ) { | |
| 25 | + return $posts_columns; | |
| 26 | + } | |
| 27 | + | |
| 28 | + // Try to place our column before date column | |
| 29 | + $pos = array_search( 'date', array_keys( $posts_columns ) ); | |
| 30 | + | |
| 31 | + if ( ! is_int( $pos ) ) { | |
| 32 | + $pos = 1; | |
| 33 | + } | |
| 34 | + | |
| 35 | + // Place our column in our desired position | |
| 36 | + $chunks = array_chunk( $posts_columns, $pos, true ); | |
| 37 | + | |
| 38 | + if( ( $post_type === 'points-award' || $post_type === 'points-deduct' ) ) { | |
| 39 | + $chunks[0]['connected_to'] = __( 'Points Type', 'gamipress' ); | |
| 40 | + } else if( $post_type === 'step' ) { | |
| 41 | + $chunks[0]['connected_to'] = __( 'Achievement', 'gamipress' ); | |
| 42 | + } else if( $post_type === 'rank-requirement' ) { | |
| 43 | + $chunks[0]['connected_to'] = __( 'Rank', 'gamipress' ); | |
| 44 | + } | |
| 45 | + | |
| 46 | + return call_user_func_array( 'array_merge', $chunks ); | |
| 47 | +} | |
| 48 | +add_filter( 'manage_posts_columns', 'gamipress_requirements_posts_columns', 10, 2 ); | |
| 49 | + | |
| 50 | +/** | |
| 51 | + * Output for our custom requirements columns | |
| 52 | + * | |
| 53 | + * @since 1.0.6 | |
| 54 | + * | |
| 55 | + * @param $column_name | |
| 56 | + * @param $post_id | |
| 57 | + */ | |
| 58 | +function gamipress_requirements_posts_custom_columns( $column_name, $post_id ) { | |
| 59 | + | |
| 60 | + $post_type = gamipress_get_post_type( $post_id ); | |
| 61 | + | |
| 62 | + if( ! in_array( $post_type, gamipress_get_requirement_types_slugs() ) ) { | |
| 63 | + return; | |
| 64 | + } | |
| 65 | + | |
| 66 | + if( $column_name !== 'connected_to' ) { | |
| 67 | + return; | |
| 68 | + } | |
| 69 | + | |
| 70 | + $label = ''; | |
| 71 | + $object = null; | |
| 72 | + | |
| 73 | + if( ( $post_type === 'points-award' ) ) { | |
| 74 | + $label = __( 'Points Type', 'gamipress' ); | |
| 75 | + $object = gamipress_get_points_award_points_type( $post_id ); | |
| 76 | + } else if( ( $post_type === 'points-deduct' ) ) { | |
| 77 | + $label = __( 'Points Type', 'gamipress' ); | |
| 78 | + $object = gamipress_get_points_deduct_points_type( $post_id ); | |
| 79 | + } else if( $post_type === 'step' ) { | |
| 80 | + $label = __( 'Achievement', 'gamipress' ); | |
| 81 | + $object = gamipress_get_step_achievement( $post_id ); | |
| 82 | + } else if( $post_type === 'rank-requirement' ) { | |
| 83 | + $label = __( 'Rank', 'gamipress' ); | |
| 84 | + $object = gamipress_get_rank_requirement_rank( $post_id ); | |
| 85 | + } | |
| 86 | + | |
| 87 | + if( $object ) : ?> | |
| 88 | + <a href="<?php echo get_edit_post_link( $object->ID ); ?>"><?php echo $object->post_title; ?></a> | |
| 89 | + <?php else : ?> | |
| 90 | + <span style="color: #a00;"><?php printf( __( 'Missed %s', 'gamipress' ), $label ); ?></span> | |
| 91 | + <?php endif; | |
| 92 | + | |
| 93 | +} | |
| 94 | +add_action( 'manage_posts_custom_column', 'gamipress_requirements_posts_custom_columns', 10, 2 ); | |
| 95 | +add_action( 'manage_pages_custom_column', 'gamipress_requirements_posts_custom_columns', 10, 2 ); | |