PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.0
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.0
8.0.5 8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 All 47 releases
gamipress / modules / ai-assistant / includes / abilities / award-rank.php

award-rank.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 8.0.0, at modules/ai-assistant/includes/abilities/award-rank.php

108 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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();