| 1 |
<?php |
| 2 |
/** |
| 3 |
* Rank Types Functions |
| 4 |
* |
| 5 |
* @package GamiPress\Rank_Types_Functions |
| 6 |
* @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.4.6 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Get registered rank types |
| 14 |
* |
| 15 |
* Returns a multidimensional array of slug, single name and plural name for all rank types. |
| 16 |
* |
| 17 |
* @since 1.3.1 |
| 18 |
* |
| 19 |
* @return array An array of our registered rank types |
| 20 |
*/ |
| 21 |
function gamipress_get_rank_types() { |
| 22 |
|
| 23 |
return GamiPress()->rank_types; |
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get registered rank type slugs |
| 29 |
* |
| 30 |
* @since 1.3.1 |
| 31 |
* |
| 32 |
* @return array An array of all our registered rank type slugs (empty array if none) |
| 33 |
*/ |
| 34 |
function gamipress_get_rank_types_slugs() { |
| 35 |
|
| 36 |
// Assume we have no registered rank types |
| 37 |
$rank_type_slugs = array(); |
| 38 |
|
| 39 |
// If we do have any rank types, loop through each and add their slug to our array |
| 40 |
foreach ( GamiPress()->rank_types as $slug => $data ) { |
| 41 |
$rank_type_slugs[] = $slug; |
| 42 |
} |
| 43 |
|
| 44 |
// Finally, return our data |
| 45 |
return $rank_type_slugs; |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get the desired rank type |
| 51 |
* |
| 52 |
* @since 1.4.6 |
| 53 |
* |
| 54 |
* @param string|int|WP_Post $rank_type |
| 55 |
* |
| 56 |
* @return array|false The rank type object if is registered, if not return false |
| 57 |
*/ |
| 58 |
function gamipress_get_rank_type( $rank_type ) { |
| 59 |
|
| 60 |
// Check if is an WP_Post |
| 61 |
if( $rank_type instanceof WP_Post ) { |
| 62 |
|
| 63 |
if( $rank_type->post_type === 'rank-type' ) { |
| 64 |
// If WP_Post given is an rank type post, try to find it by ID |
| 65 |
$rank_type = $rank_type->ID; |
| 66 |
} else { |
| 67 |
// Else the WP_Post given is an rank post, so the rank type is on post_type field |
| 68 |
$rank_type = $rank_type->post_type; |
| 69 |
} |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
if( gettype( $rank_type ) === 'string' && isset( GamiPress()->rank_types[$rank_type] ) ) { |
| 74 |
return GamiPress()->rank_types[$rank_type]; |
| 75 |
} |
| 76 |
|
| 77 |
if( is_numeric( $rank_type ) ) { |
| 78 |
return gamipress_get_rank_type_by_id( $rank_type ); |
| 79 |
} |
| 80 |
|
| 81 |
// Rank type can not be found |
| 82 |
return false; |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get the desired rank type by ID |
| 88 |
* |
| 89 |
* @since 1.4.6 |
| 90 |
* |
| 91 |
* @param int $rank_type_id |
| 92 |
* |
| 93 |
* @return array|false The rank type object if is registered, if not return false |
| 94 |
*/ |
| 95 |
function gamipress_get_rank_type_by_id( $rank_type_id ) { |
| 96 |
|
| 97 |
$rank_type_id = absint( $rank_type_id ); |
| 98 |
|
| 99 |
// Bail if wrong ID given |
| 100 |
if( $rank_type_id === 0 ) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
// Loop all registered rank types to find what matches the given ID |
| 105 |
foreach ( GamiPress()->rank_types as $slug => $data ) { |
| 106 |
if( absint( $data['ID'] ) === $rank_type_id ) { |
| 107 |
return $data; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// Rank type can not be found |
| 112 |
return false; |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get the desired rank type ID |
| 118 |
* |
| 119 |
* @since 1.4.6 |
| 120 |
* |
| 121 |
* @param string|int|WP_Post $rank_type |
| 122 |
* |
| 123 |
* @return int|false The rank type ID if is registered, if not return false |
| 124 |
*/ |
| 125 |
function gamipress_get_rank_type_id( $rank_type ) { |
| 126 |
|
| 127 |
$rank_type = gamipress_get_rank_type( $rank_type ); |
| 128 |
|
| 129 |
if( $rank_type ) { |
| 130 |
return $rank_type['ID']; |
| 131 |
} |
| 132 |
|
| 133 |
return false; |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get the desired rank type singular name |
| 139 |
* |
| 140 |
* @since 1.3.1 |
| 141 |
* @updated 1.4.6 Full rewrite of the function and added the parameter $force return |
| 142 |
* |
| 143 |
* @param string|int|WP_Post $rank_type |
| 144 |
* @param bool $force_return If set to true, will return "Rank" if rank type is not registered or singular name is empty |
| 145 |
* |
| 146 |
* @return array|false The rank type singular name if is registered, if not return false |
| 147 |
*/ |
| 148 |
function gamipress_get_rank_type_singular( $rank_type, $force_return = false ) { |
| 149 |
|
| 150 |
$rank_type = gamipress_get_rank_type( $rank_type ); |
| 151 |
|
| 152 |
if( $rank_type ) { |
| 153 |
|
| 154 |
// If force return and rank type singular name is empty, return "Rank" as singular name |
| 155 |
if( $force_return && empty( $rank_type['singular_name'] ) ) { |
| 156 |
return __( 'Rank', 'gamipress' ); |
| 157 |
} |
| 158 |
|
| 159 |
// Return the rank type singular name |
| 160 |
return $rank_type['singular_name']; |
| 161 |
} |
| 162 |
|
| 163 |
return $force_return ? __( 'Rank', 'gamipress' ) : false; |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Get the desired rank type plural name |
| 169 |
* |
| 170 |
* @since 1.3.1 |
| 171 |
* @updated 1.4.6 Full rewrite of the function and added the parameter $force return |
| 172 |
* |
| 173 |
* @param string|int|WP_Post $rank_type |
| 174 |
* @param bool $force_return If set to true, will return "Ranks" if rank type is not registered or plural name is empty |
| 175 |
* |
| 176 |
* @return array|false The rank type plural name if is registered, if not return false |
| 177 |
*/ |
| 178 |
function gamipress_get_rank_type_plural( $rank_type, $force_return = false ) { |
| 179 |
|
| 180 |
$rank_type = gamipress_get_rank_type( $rank_type ); |
| 181 |
|
| 182 |
if( $rank_type ) { |
| 183 |
|
| 184 |
// If force return and rank type plural name is empty, return "Ranks" as plural name |
| 185 |
if( $force_return && empty( $rank_type['plural_name'] ) ) { |
| 186 |
return __( 'Ranks', 'gamipress' ); |
| 187 |
} |
| 188 |
|
| 189 |
// Return the rank type singular name |
| 190 |
return $rank_type['plural_name']; |
| 191 |
} |
| 192 |
|
| 193 |
return $force_return ? __( 'Ranks', 'gamipress' ) : false; |
| 194 |
|
| 195 |
} |