| 1 |
<?php |
| 2 |
/** |
| 3 |
* Award Rank Ability |
| 4 |
* |
| 5 |
* @package GamiPress\AI_Assistant\Ability\Award_Rank |
| 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 |
class GamiPress_AI_Assistant_Ability_Award_Rank extends GamiPress_AI_Assistant_Ability { |
| 13 |
|
| 14 |
public $ability = 'award-rank'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Register the ability |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
*/ |
| 21 |
public function register() { |
| 22 |
|
| 23 |
$this->args = array( |
| 24 |
'label' => __( 'Award a rank in GamiPress', 'gamipress' ), |
| 25 |
'description' => __( 'Awards a specific rank to a user. Requires the user, the rank type, and the rank to award.', 'gamipress' ), |
| 26 |
'input_schema' => array( |
| 27 |
'type' => 'object', |
| 28 |
'properties' => array( |
| 29 |
'user' => array( |
| 30 |
'type' => 'string', |
| 31 |
'description' => __( 'The ID, email or name of the user who will receive the points.', 'gamipress' ), |
| 32 |
), |
| 33 |
'rank_type' => array( |
| 34 |
'description' => __( 'The GamiPress rank type.', 'gamipress' ), |
| 35 |
'type' => 'string', |
| 36 |
//'enum' => gamipress_get_rank_types_slugs(), |
| 37 |
), |
| 38 |
'rank' => array( |
| 39 |
'description' => __( 'The rank to award. Get the rank ID.', 'gamipress' ), |
| 40 |
'type' => 'string', |
| 41 |
), |
| 42 |
), |
| 43 |
'required' => array( 'user', 'rank_type', 'rank' ), |
| 44 |
'additionalProperties' => false, |
| 45 |
), |
| 46 |
); |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Ability execute callback |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
* |
| 55 |
* @param array $args |
| 56 |
* |
| 57 |
* @return array array( 'success' => true|false, 'message' => '' ) |
| 58 |
*/ |
| 59 |
public function execute( $args ) { |
| 60 |
|
| 61 |
$args = wp_parse_args( (array) $args, array( |
| 62 |
'user' => '', |
| 63 |
'rank_type' => '', |
| 64 |
'rank' => '', |
| 65 |
) ); |
| 66 |
|
| 67 |
$user_search = sanitize_text_field( $args['user'] ); |
| 68 |
$rank_type = sanitize_text_field( $args['rank_type'] ); |
| 69 |
$rank_search = sanitize_text_field( $args['rank'] ); |
| 70 |
|
| 71 |
// Check the user |
| 72 |
$user = $this->get_user( $user_search ); |
| 73 |
if( $this->is_response_error( $user ) ) return $user; |
| 74 |
|
| 75 |
// Check the ranks type |
| 76 |
$type = gamipress_get_rank_type( $rank_type ); |
| 77 |
if( ! $type ) |
| 78 |
return $this->response_error( sprintf( __( 'I couldn\'t find the rank type "%s".', 'gamipress' ), $rank_type ) ); |
| 79 |
|
| 80 |
// Check the post |
| 81 |
$rank = $this->get_post( $rank_search, array( 'post_type' => $type['slug'] ) ); |
| 82 |
if( $this->is_response_error( $rank ) ) return $rank; |
| 83 |
|
| 84 |
// Award the rank to the user |
| 85 |
gamipress_update_user_rank( $user->ID, $rank->ID ); |
| 86 |
|
| 87 |
$user_display = $user->display_name; |
| 88 |
|
| 89 |
if ( current_user_can( 'edit_users' ) ) { |
| 90 |
$user_display = '[' . $user_display . '](' . get_edit_user_link( $user->ID ) . ')'; |
| 91 |
} |
| 92 |
|
| 93 |
$post_display = $rank->post_title; |
| 94 |
|
| 95 |
if ( current_user_can( 'edit_post', $rank->ID ) ) { |
| 96 |
$post_display = '[' . $rank->post_title . '](' . get_edit_post_link( $rank->ID ) . ')'; |
| 97 |
} |
| 98 |
|
| 99 |
$url_credential = '[' . __( 'credential', 'gamipress' ) . '](' . gamipress_get_credential_url( $rank->ID, $user->ID ) . ')'; |
| 100 |
|
| 101 |
$message = sprintf( __( 'I awarded %s to %s. View the %s.', 'gamipress' ), $post_display, $user_display, $url_credential ); |
| 102 |
|
| 103 |
return $this->response_success( $message ); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
new GamiPress_AI_Assistant_Ability_Award_Rank(); |