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 / create-achievement.php

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

145 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Create Achievement Ability
4 *
5 * @package GamiPress\AI_Assistant\Ability\Create_Achievement
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_Create_Achievement extends GamiPress_AI_Assistant_Ability {
13
14 public $ability = 'create-achievement';
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__( 'Create an achievement in GamiPress', 'gamipress' ),
25 'description' => esc_html__( 'Design and insert a new achievement of a type into the database.', 'gamipress' ),
26 'input_schema' => array(
27 'type' => 'object',
28 'properties' => array(
29 'title' => array(
30 'description' => esc_html__( 'A clean, professional, and descriptive title for the achievement.', 'gamipress' ),
31 'type' => 'string',
32 ),
33 'description' => array(
34 'description' => esc_html__( 'A clean, professional, and descriptive description for the achievement.', 'gamipress' ),
35 'type' => 'string',
36 ),
37 'achievement_type' => array(
38 'description' => esc_html__( 'The GamiPress achievement type.', 'gamipress' ),
39 'type' => 'string',
40 //'enum' => gamipress_get_achievement_types_slugs(),
41 ),
42 'requirements' => array(
43 'description' => esc_html__( 'List of achievement steps, requirements or triggers. Define the criteria to meet to earn that achievement.', 'gamipress' ),
44 'type' => 'array',
45 'items' => array(
46 'type' => 'object',
47 'properties' => gamipress_ai_assistant_get_requirements_properties( 'step' ),
48 ),
49 ),
50 ),
51 'required' => array( 'title', 'achievement_type' ),
52 'additionalProperties' => false,
53 ),
54 );
55
56 }
57
58 /**
59 * Ability execute callback
60 *
61 * @since 1.0.0
62 *
63 * @param array $args
64 *
65 * @return array array( 'success' => true|false, 'message' => '' )
66 */
67 public function execute( $args ) {
68
69 $args = wp_parse_args( (array) $args, array(
70 'title' => '',
71 'description' => '',
72 'achievement_type' => '',
73 'requirements' => array(),
74 ) );
75
76 $title = sanitize_text_field( $args['title'] );
77 $description = sanitize_text_field( $args['description'] );
78 $achievement_type = sanitize_text_field( $args['achievement_type'] );
79 $requirements = $args['requirements'];
80
81 // Check the achievement type
82 $type = gamipress_get_achievement_type( $achievement_type );
83 if( ! $type )
84 return $this->response_error( sprintf( __( 'I couldn\'t find the achievement type "%s".', 'gamipress' ), $achievement_type ) );
85
86 // Make description block compatible
87 $description = '<!-- wp:paragraph -->' . $description . '<!-- /wp:paragraph -->';
88
89 // Insert post
90 $post_data = array(
91 'post_title' => $title,
92 'post_content' => $description,
93 'post_type' => $achievement_type,
94 'post_status' => 'draft', // TODO: FUTURE: Add optional input schema to manage status, for the moment, draft to prevent published unwanted results
95 );
96
97 $post_id = wp_insert_post( $post_data, true );
98
99 if( is_wp_error( $post_id ) ) {
100 // translators: %1$s: Achievement type singular %2$s: Error messages
101 return $this->response_error( sprintf( __( 'I couldn\'t create the %1$s. Reason: %2$s.', 'gamipress' ), $type['singular_name'], $post_id->get_error_message() ) );
102 }
103
104 // Post metas
105 $prefix = '_gamipress_';
106
107 update_post_meta( $post_id, $prefix . 'earned_by', 'triggers' );
108 update_post_meta( $post_id, $prefix . 'maximum_earnings', '1' );
109 update_post_meta( $post_id, $prefix . 'global_maximum_earnings', '0' );
110 update_post_meta( $post_id, $prefix . 'hidden', 'show' );
111
112 // Requirements
113 $updated_steps = gamipress_ai_assistant_process_requirements(
114 $requirements,
115 $post_id,
116 'step'
117 );
118
119 $steps_count = count( $updated_steps );
120
121 $steps_answer = gamipress_ai_assistant_get_requirements_answer( $updated_steps );
122
123 // Final response
124 $post = get_post( $post_id );
125 $post_link = get_edit_post_link( $post );
126 $post_display = empty( $post->post_title ) ? esc_html__( '(no title)' ) : $post->post_title;
127
128 if( $post_link !== null ) {
129 $post_display = '[' . $post_display . '](' . $post_link . ')';
130 }
131
132 if ( $steps_count > 0 ) {
133 $message = sprintf( __( 'I created the %1$s %2$s that requires:', 'gamipress' ), $type['singular_name'], $post_display );
134 $message .= $steps_answer;
135 } else{
136 // translators: %1$s: Achievement type singular %2$s: Achievement name
137 $message = sprintf( __( 'I created the %1$s %2$s.', 'gamipress' ), $type['singular_name'], $post_display );
138 }
139
140 return $this->response_success( $message );
141
142 }
143
144 }
145 new GamiPress_AI_Assistant_Ability_Create_Achievement();