PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.4
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.4
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 / add-achievement-requirements.php

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

104 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 * Add Achievement Requirements Ability
4 *
5 * @package GamiPress\AI_Assistant\Ability\Add_Achievement_Requirements
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_Add_Achievement_Requirements extends GamiPress_AI_Assistant_Ability {
13
14 public $ability = 'add-achievement-requirements';
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 requirements to an achievement in GamiPress', 'gamipress' ),
25 'description' => esc_html__( 'Design and insert new achievement requirements.', 'gamipress' ),
26 'input_schema' => array(
27 'type' => 'object',
28 'properties' => array(
29 'achievement' => array(
30 'description' => esc_html__( 'The achievement ID, title or slug.', 'gamipress' ),
31 'type' => 'string',
32 ),
33 'requirements' => array(
34 'description' => esc_html__( 'List of achievement steps, requirements or triggers. Define the criteria to meet to earn that achievement.', 'gamipress' ),
35 'type' => 'array',
36 'items' => array(
37 'type' => 'object',
38 'properties' => gamipress_ai_assistant_get_requirements_properties( 'step' ),
39 ),
40 ),
41 ),
42 'required' => array( 'achievement', 'requirements' ),
43 'additionalProperties' => false,
44 ),
45 );
46
47 }
48
49 /**
50 * Ability execute callback
51 *
52 * @since 1.0.0
53 *
54 * @param array $args
55 *
56 * @return array array( 'success' => true|false, 'message' => '' )
57 */
58 public function execute( $args ) {
59
60 $args = wp_parse_args( (array) $args, array(
61 'achievement' => '',
62 'requirements' => array(),
63 ) );
64
65 $achievement = sanitize_text_field( $args['achievement'] );
66 $requirements = $args['requirements'];
67
68 // Check the achievement type
69 $post = $this->get_post( $achievement );
70 if( $this->is_response_error( $post ) )
71 return $post;
72
73 if( ! gamipress_get_achievement_type( $post->post_type ) )
74 return $this->response_error( sprintf( __( 'The post %s is not an achievement.', 'gamipress' ), '#' . $post->ID . ' - ' . $post->post_title ) );
75
76 // Requirements
77 $updated_steps = gamipress_ai_assistant_process_requirements(
78 $requirements,
79 $post->ID,
80 'step'
81 );
82
83 $steps_count = count( $updated_steps );
84 $steps_label = _n( 'step', 'steps', $steps_count, 'gamipress' );
85 $steps_answer = gamipress_ai_assistant_get_requirements_answer( $updated_steps );
86
87 // Final response
88 $post_link = get_edit_post_link( $post );
89 $post_display = empty( $post->post_title ) ? esc_html__( '(no title)' ) : $post->post_title;
90
91 if( $post_link !== null ) {
92 $post_display = '[' . $post_display . '](' . $post_link . ')';
93 }
94
95 // translators: %1$s: Step label. %2$s: Achievement name
96 $message = sprintf( __( 'I added the following %1$s to %2$s:', 'gamipress' ), $steps_label, $post_display );
97 $message .= $steps_answer;
98
99 return $this->response_success( $message );
100
101 }
102
103 }
104 new GamiPress_AI_Assistant_Ability_Add_Achievement_Requirements();