| 1 |
<?php |
| 2 |
/** |
| 3 |
* Add Rank Requirements Ability |
| 4 |
* |
| 5 |
* @package GamiPress\AI_Assistant\Ability\Add_Rank_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_Rank_Requirements extends GamiPress_AI_Assistant_Ability { |
| 13 |
|
| 14 |
public $ability = 'add-rank-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 a rank in GamiPress', 'gamipress' ), |
| 25 |
'description' => esc_html__( 'Design and insert new rank requirements.', 'gamipress' ), |
| 26 |
'input_schema' => array( |
| 27 |
'type' => 'object', |
| 28 |
'properties' => array( |
| 29 |
'rank' => array( |
| 30 |
'description' => esc_html__( 'The rank 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 reach that rank.', 'gamipress' ), |
| 35 |
'type' => 'array', |
| 36 |
'items' => array( |
| 37 |
'type' => 'object', |
| 38 |
'properties' => gamipress_ai_assistant_get_requirements_properties( 'rank-requirement' ), |
| 39 |
), |
| 40 |
), |
| 41 |
), |
| 42 |
'required' => array( 'rank', '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 |
'rank' => '', |
| 62 |
'requirements' => array(), |
| 63 |
) ); |
| 64 |
|
| 65 |
$rank = sanitize_text_field( $args['rank'] ); |
| 66 |
$requirements = $args['requirements']; |
| 67 |
|
| 68 |
// Check the rank type |
| 69 |
$post = $this->get_post( $rank ); |
| 70 |
if( $this->is_response_error( $post ) ) |
| 71 |
return $post; |
| 72 |
|
| 73 |
if( ! gamipress_get_rank_type( $post->post_type ) ) |
| 74 |
return $this->response_error( sprintf( __( 'The post %s is not a rank.', 'gamipress' ), '#' . $post->ID . ' - ' . $post->post_title ) ); |
| 75 |
|
| 76 |
// Requirements |
| 77 |
$updated_requirements = gamipress_ai_assistant_process_requirements( |
| 78 |
$requirements, |
| 79 |
$post->ID, |
| 80 |
'rank-requirement' |
| 81 |
); |
| 82 |
|
| 83 |
$requirements_count = count( $updated_requirements ); |
| 84 |
$requirements_label = _n( 'requirement', 'requirements', $requirements_count, 'gamipress' ); |
| 85 |
$requirements_answer = gamipress_ai_assistant_get_requirements_answer( $updated_requirements ); |
| 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: Requirement label. %2$s: Rank name |
| 96 |
$message = sprintf( __( 'I added the following %1$s to %2$s:', 'gamipress' ), $requirements_label, $post_display ); |
| 97 |
$message .= $requirements_answer; |
| 98 |
|
| 99 |
return $this->response_success( $message ); |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
new GamiPress_AI_Assistant_Ability_Add_Rank_Requirements(); |