| 1 |
<?php |
| 2 |
/** |
| 3 |
* Add Points Deducts Ability |
| 4 |
* |
| 5 |
* @package GamiPress\AI_Assistant\Ability\Add_Points_Deducts |
| 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_Add_Points_Deducts extends GamiPress_AI_Assistant_Ability { |
| 13 |
|
| 14 |
public $ability = 'add-points-deducts'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Register the ability |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
*/ |
| 21 |
public function register() { |
| 22 |
|
| 23 |
$this->args = array( |
| 24 |
'label' => esc_html__( 'Add points deducts to a points type in GamiPress', 'gamipress' ), |
| 25 |
'description' => esc_html__( 'Design and insert a new points deduct into the database.', 'gamipress' ), |
| 26 |
'input_schema' => array( |
| 27 |
'type' => 'object', |
| 28 |
'properties' => array( |
| 29 |
'points_type' => array( |
| 30 |
'description' => __( 'The GamiPress points type.', 'gamipress' ), |
| 31 |
'type' => 'string', |
| 32 |
//'enum' => gamipress_get_points_types_slugs(), |
| 33 |
), |
| 34 |
'points_deducts' => array( |
| 35 |
'description' => esc_html__( 'List of points deducts or rules for the points type. Define the criteria to meet to earn points of this type.', 'gamipress' ), |
| 36 |
'type' => 'array', |
| 37 |
'items' => array( |
| 38 |
'type' => 'object', |
| 39 |
'properties' => gamipress_ai_assistant_get_requirements_properties( 'points-deduct' ), |
| 40 |
), |
| 41 |
), |
| 42 |
), |
| 43 |
'required' => array( 'points_type' ), |
| 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 |
'points_type' => '', |
| 63 |
'points_deducts' => array(), |
| 64 |
) ); |
| 65 |
|
| 66 |
$points_type = sanitize_text_field( $args['points_type'] ); |
| 67 |
$points_deducts = $args['points_deducts']; |
| 68 |
|
| 69 |
// Check the points type |
| 70 |
$type = gamipress_get_points_type( $points_type ); |
| 71 |
|
| 72 |
if( ! $type ) |
| 73 |
return $this->response_error( sprintf( __( 'I couldn\'t find the points type "%s".', 'gamipress' ), $points_type ) ); |
| 74 |
|
| 75 |
// Requirements |
| 76 |
$updated_deducts = gamipress_ai_assistant_process_requirements( |
| 77 |
$points_deducts, |
| 78 |
$type['ID'], |
| 79 |
'points-deduct' |
| 80 |
); |
| 81 |
|
| 82 |
$deducts_count = count( $updated_deducts ); |
| 83 |
$deducts_label = _n( 'deduct', 'deducts', $deducts_count, 'gamipress' ); |
| 84 |
$deducts_answer = gamipress_ai_assistant_get_requirements_answer( $updated_deducts ); |
| 85 |
|
| 86 |
// Final response |
| 87 |
$post_link = get_edit_post_link( $type['ID'] ); |
| 88 |
$post_display = $type['plural_name']; |
| 89 |
|
| 90 |
if( $post_link !== null ) { |
| 91 |
$post_display = '[' . $post_display . '](' . $post_link . ')'; |
| 92 |
} |
| 93 |
|
| 94 |
// translators: %1$s: Deduct label. %2$s: Point type name |
| 95 |
$message = sprintf( __( 'I added the following points %1$s to %2$s:', 'gamipress' ), $deducts_label, $post_display ); |
| 96 |
$message .= $deducts_answer; |
| 97 |
|
| 98 |
return $this->response_success( $message ); |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
new GamiPress_AI_Assistant_Ability_Add_Points_Deducts(); |