PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.3
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.3
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 7.8.7 7.8.6 All 45 releases
gamipress / modules / ai-assistant / includes / abilities / award-points.php

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

131 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Award Points Ability
4 *
5 * @package GamiPress\AI_Assistant\Ability\Award_points
6 * @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com>
7 * @since 1.0.0
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 class GamiPress_AI_Assistant_Ability_Award_Points extends GamiPress_AI_Assistant_Ability {
13
14 public $ability = 'award-points';
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 points in GamiPress', 'gamipress' ),
25 'description' => __( 'Awards a specified number of GamiPress points to a user. Requires the user, the points type, and the number of points 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 'points_type' => array(
34 'description' => __( 'The GamiPress points type.', 'gamipress' ),
35 'type' => 'string',
36 //'enum' => gamipress_get_points_types_slugs(),
37 ),
38 'points' => array(
39 'description' => __( 'The number of points to award. Must be a positive integer.', 'gamipress' ),
40 'type' => 'integer',
41 ),
42 'user_earning' => array(
43 'description' => __( 'Decide whether to record the movement in the user earnings table. By default true unless the user specifies otherwise.', 'gamipress' ),
44 'type' => 'boolean',
45 'default' => 'true',
46 ),
47 'user_earning_title' => array(
48 'description' => __( 'Text for the user earning record.', 'gamipress' ),
49 'type' => 'string',
50 'default' => __( 'Manual balance adjustment', 'gamipress' ),
51 ),
52 ),
53 'required' => array( 'user', 'points_type', 'points' ),
54 'additionalProperties' => false,
55 ),
56 );
57
58 }
59
60 /**
61 * Ability execute callback
62 *
63 * @since 1.0.0
64 *
65 * @param array $args
66 *
67 * @return array array( 'success' => true|false, 'message' => '' )
68 */
69 public function execute( $args ) {
70
71 $args = wp_parse_args( (array) $args, array(
72 'user' => '',
73 'points_type' => '',
74 'points' => '',
75 'user_earning' => true,
76 'user_earning_title' => __( 'Manual balance adjustment', 'gamipress' ),
77 ) );
78
79 $user_search = sanitize_text_field( $args['user'] );
80 $points_type = sanitize_text_field( $args['points_type'] );
81 $points = (int) $args['points'];
82 $user_earning = (bool) $args['user_earning'];
83 $user_earning_title = sanitize_text_field( $args['user_earning_title'] );
84
85 // Check the user
86 $user = $this->get_user( $user_search );
87 if( $this->is_response_error( $user ) ) return $user;
88
89 // Check the points type
90 $points_type_id = gamipress_get_points_type_id( $points_type );
91 if( ! $points_type_id )
92 return $this->response_error( sprintf( __( 'I couldn\'t find the points type "%s".', 'gamipress' ), $points_type ) );
93
94 // Award the points to the user
95 gamipress_award_points_to_user( $user->ID, $points, $points_type );
96
97 if( $user_earning ) {
98 // Insert the custom user earning for the manual balance adjustment
99 $user_earning_id = gamipress_insert_user_earning( $user->ID, array(
100 'title' => $user_earning_title,
101 'user_id' => $user->ID,
102 'post_id' => $points_type_id,
103 'post_type' => 'points-type',
104 'points' => $points,
105 'points_type' => $points_type,
106 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
107 ) );
108 }
109
110 $points_formatted = gamipress_format_points( $points, $points_type, false );
111
112 $user_display = $user->display_name;
113
114 if ( current_user_can( 'edit_users' ) ) {
115 $user_display = '[' . $user_display . '](' . get_edit_user_link( $user->ID ) . ')';
116 }
117
118 if( $user_earning ) {
119 // translators: %1$s: Points amount & label %2$s: User %3$s: Reason
120 $message = sprintf( __( 'I awarded %1$s to %2$s and registered the earning with the reason "%3$s".', 'gamipress' ), $points_formatted, $user_display, $user_earning_title );
121 } else {
122 // translators: %1$s: Points amount & label %2$s: User
123 $message = sprintf( __( 'I awarded %1$s to %2$s without registering in user earnings.', 'gamipress' ), $points_formatted, $user_display );
124 }
125
126 return $this->response_success( $message );
127
128 }
129
130 }
131 new GamiPress_AI_Assistant_Ability_Award_Points();