| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin Ranks |
| 4 |
* |
| 5 |
* @package GamiPress\Admin\Ranks |
| 6 |
* @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.3.1 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Register ranks related hooks |
| 14 |
* |
| 15 |
* @since 1.3.1 |
| 16 |
*/ |
| 17 |
function gamipress_load_ranks_admin_hooks() { |
| 18 |
|
| 19 |
foreach( gamipress_get_rank_types() as $slug => $rank ) { |
| 20 |
add_filter( "manage_{$slug}_posts_columns", 'gamipress_rank_posts_columns' ); |
| 21 |
add_filter( "manage_edit-{$slug}_sortable_columns", 'gamipress_rank_sortable_columns' ); |
| 22 |
add_action( "manage_{$slug}_posts_custom_column", 'gamipress_rank_posts_custom_columns', 10, 2 ); |
| 23 |
} |
| 24 |
|
| 25 |
} |
| 26 |
add_action( 'admin_init', 'gamipress_load_ranks_admin_hooks' ); |
| 27 |
|
| 28 |
/** |
| 29 |
* Overrides the enter title here on rank edit screen |
| 30 |
* |
| 31 |
* @since 1.3.1 |
| 32 |
* |
| 33 |
* @param $placeholder |
| 34 |
* @param $post |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
function gamipress_admin_enter_title_here( $placeholder, $post ) { |
| 39 |
|
| 40 |
if( gamipress_is_rank( $post->post_type ) ) { |
| 41 |
return __( 'Rank name', 'gamipress' ); |
| 42 |
} |
| 43 |
|
| 44 |
return $placeholder; |
| 45 |
|
| 46 |
} |
| 47 |
add_filter( 'enter_title_here', 'gamipress_admin_enter_title_here', 10, 2 ); |
| 48 |
|
| 49 |
/** |
| 50 |
* Register our custom ranks columns |
| 51 |
* |
| 52 |
* @since 1.3.1 |
| 53 |
* @updated 1.3.9.5 Added the thumbnail column |
| 54 |
* |
| 55 |
* @param array $posts_columns |
| 56 |
* |
| 57 |
* @return array |
| 58 |
*/ |
| 59 |
function gamipress_rank_posts_columns( $posts_columns ) { |
| 60 |
|
| 61 |
$posts_columns['title'] = __( 'Name', 'gamipress' ); |
| 62 |
|
| 63 |
unset( $posts_columns['author'] ); |
| 64 |
|
| 65 |
// Prepend the thumbnail column |
| 66 |
$chunks = array_chunk( $posts_columns, 1, true ); |
| 67 |
$chunks[0]['thumbnail'] = __( 'Image', 'gamipress' ); |
| 68 |
|
| 69 |
$posts_columns = call_user_func_array( 'array_merge', $chunks ); |
| 70 |
|
| 71 |
// Try to place our column before date column |
| 72 |
$pos = array_search( 'date', array_keys( $posts_columns ) ); |
| 73 |
|
| 74 |
if ( ! is_int( $pos ) ) { |
| 75 |
$pos = 1; |
| 76 |
} |
| 77 |
|
| 78 |
// Place our column in our desired position |
| 79 |
$chunks = array_chunk( $posts_columns, $pos, true ); |
| 80 |
$chunks[0]['priority'] = __( 'Priority', 'gamipress' ); |
| 81 |
$chunks[0]['unlock_with_points'] = __( 'Unlock with Points', 'gamipress' ); |
| 82 |
|
| 83 |
return call_user_func_array( 'array_merge', $chunks ); |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Set ranks sortable columns |
| 89 |
* |
| 90 |
* @since 1.3.1 |
| 91 |
* |
| 92 |
* @param array $sortable_columns |
| 93 |
* |
| 94 |
* @return array |
| 95 |
*/ |
| 96 |
function gamipress_rank_sortable_columns( $sortable_columns ) { |
| 97 |
|
| 98 |
$sortable_columns['priority'] = 'priority'; |
| 99 |
|
| 100 |
return $sortable_columns; |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Output for our custom ranks columns |
| 106 |
* |
| 107 |
* @since 1.3.1 |
| 108 |
* @updated 1.3.9.5 Added the thumbnail column output |
| 109 |
* |
| 110 |
* @param string $column_name |
| 111 |
* @param integer $post_id |
| 112 |
*/ |
| 113 |
function gamipress_rank_posts_custom_columns( $column_name, $post_id ) { |
| 114 |
|
| 115 |
$prefix = '_gamipress_'; |
| 116 |
|
| 117 |
switch( $column_name ) { |
| 118 |
case 'thumbnail': |
| 119 |
|
| 120 |
$can_edit_post = current_user_can( 'edit_post', $post_id ); |
| 121 |
|
| 122 |
if( $can_edit_post ) { |
| 123 |
printf( |
| 124 |
'<a href="%s" aria-label="%s">%s</a>', |
| 125 |
get_edit_post_link( $post_id ), |
| 126 |
/* translators: %s: post title */ |
| 127 |
esc_attr( sprintf( __( '“%s” (Edit)' ), get_post_field( 'post_title', $post_id ) ) ), |
| 128 |
gamipress_get_the_post_thumbnail( $post_id, array( 32, 32 ) ) |
| 129 |
); |
| 130 |
} else { |
| 131 |
echo gamipress_get_the_post_thumbnail( $post_id, array( 32, 32 ) ); |
| 132 |
} |
| 133 |
|
| 134 |
break; |
| 135 |
case 'priority': |
| 136 |
|
| 137 |
echo absint( get_post_field( 'menu_order', $post_id ) ); |
| 138 |
|
| 139 |
break; |
| 140 |
case 'unlock_with_points': |
| 141 |
|
| 142 |
$unlock_with_points = gamipress_get_post_meta( $post_id, $prefix . 'unlock_with_points' ); |
| 143 |
|
| 144 |
if( ! (bool) $unlock_with_points ) { |
| 145 |
break; |
| 146 |
} |
| 147 |
|
| 148 |
// Setup points vars |
| 149 |
$points_type_to_unlock = gamipress_get_post_meta( $post_id, $prefix . 'points_type_to_unlock' ); |
| 150 |
$points_to_unlock = absint( gamipress_get_post_meta( $post_id, $prefix . 'points_to_unlock' ) ); |
| 151 |
|
| 152 |
if( $points_to_unlock !== 0 ) { |
| 153 |
echo gamipress_format_points( $points_to_unlock, $points_type_to_unlock ); |
| 154 |
} |
| 155 |
|
| 156 |
break; |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Filter query for ranks |
| 163 |
* |
| 164 |
* @since 1.3.1 |
| 165 |
* |
| 166 |
* @param $query |
| 167 |
*/ |
| 168 |
function gamipress_rank_pre_get_posts( $query ) { |
| 169 |
|
| 170 |
if( ! is_admin() ) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
if( ! gamipress_is_rank( $query->get( 'post_type') ) ) { |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
$orderby = $query->get( 'orderby' ); |
| 179 |
|
| 180 |
// If order by is set to custom field, make it work, also set this order by as default when no order by has specified |
| 181 |
if( $orderby === 'priority' || empty( $orderby ) ) { |
| 182 |
$query->set( 'orderby', 'menu_order' ); |
| 183 |
} |
| 184 |
|
| 185 |
// Override the default order by |
| 186 |
if( $orderby === 'menu_order title' ) { |
| 187 |
$query->set( 'orderby', 'menu_order' ); |
| 188 |
$query->set( 'order', 'DESC' ); |
| 189 |
} |
| 190 |
|
| 191 |
} |
| 192 |
add_action( 'pre_get_posts', 'gamipress_rank_pre_get_posts' ); |
| 193 |
|
| 194 |
// Function callback to render next rank on rank details box |
| 195 |
function gamipress_next_rank_content_cb( $field, $object_id, $object_type ) { |
| 196 |
|
| 197 |
// Bail if is an auto-draft rank |
| 198 |
if( get_post_field( 'post_status', $object_id ) === 'auto-draft' ) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
$rank = gamipress_get_next_rank( $object_id ); |
| 203 |
$rank_type = gamipress_get_post_type( $object_id ); |
| 204 |
|
| 205 |
if( $rank ) : ?> |
| 206 |
<div class="cmb-th"> |
| 207 |
<label><?php echo sprintf( __( 'Next %s', 'gamipress' ), gamipress_get_rank_type_singular( $rank_type, true ) ); ?></label> |
| 208 |
</div> |
| 209 |
<p> |
| 210 |
<?php echo $rank->post_title; ?> |
| 211 |
<a href="<?php echo get_edit_post_link( $rank->ID ); ?>"><?php _e( 'Edit' ); ?></a> |
| 212 |
</p> |
| 213 |
<?php endif; |
| 214 |
|
| 215 |
} |
| 216 |
|
| 217 |
// Function callback to render previous rank on rank details box |
| 218 |
function gamipress_prev_rank_content_cb( $field, $object_id, $object_type ) { |
| 219 |
|
| 220 |
// Bail if is an auto-draft rank |
| 221 |
if( get_post_field( 'post_status', $object_id ) === 'auto-draft' ) { |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
$rank = gamipress_get_prev_rank( $object_id ); |
| 226 |
$rank_type = gamipress_get_post_type( $object_id ); |
| 227 |
|
| 228 |
if( $rank ) : ?> |
| 229 |
<div class="cmb-th"> |
| 230 |
<label><?php echo sprintf( __( 'Previous %s', 'gamipress' ), gamipress_get_rank_type_singular( $rank_type, true ) ); ?></label> |
| 231 |
</div> |
| 232 |
<p> |
| 233 |
<?php echo $rank->post_title; ?> |
| 234 |
<a href="<?php echo get_edit_post_link( $rank->ID ); ?>"><?php _e( 'Edit' ); ?></a> |
| 235 |
</p> |
| 236 |
<?php endif; |
| 237 |
|
| 238 |
} |