| 1 |
<?php |
| 2 |
/** |
| 3 |
* Create Rank Ability |
| 4 |
* |
| 5 |
* @package GamiPress\AI_Assistant\Ability\Create_Rank |
| 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_Rank extends GamiPress_AI_Assistant_Ability { |
| 13 |
|
| 14 |
public $ability = 'create-rank'; |
| 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 a rank in GamiPress', 'gamipress' ), |
| 25 |
'description' => esc_html__( 'Design and insert a new rank 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 rank.', 'gamipress' ), |
| 31 |
'type' => 'string', |
| 32 |
), |
| 33 |
'description' => array( |
| 34 |
'description' => esc_html__( 'A clean, professional, and descriptive description for the rank.', 'gamipress' ), |
| 35 |
'type' => 'string', |
| 36 |
), |
| 37 |
'rank_type' => array( |
| 38 |
'description' => esc_html__( 'The GamiPress rank type.', 'gamipress' ), |
| 39 |
'type' => 'string', |
| 40 |
//'enum' => gamipress_get_rank_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 rank.', 'gamipress' ), |
| 44 |
'type' => 'array', |
| 45 |
'items' => array( |
| 46 |
'type' => 'object', |
| 47 |
'properties' => gamipress_ai_assistant_get_requirements_properties( 'rank-requirement' ), |
| 48 |
), |
| 49 |
), |
| 50 |
), |
| 51 |
'required' => array( 'title', 'rank_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 |
global $wpdb; |
| 70 |
|
| 71 |
$args = wp_parse_args( (array) $args, array( |
| 72 |
'title' => '', |
| 73 |
'description' => '', |
| 74 |
'rank_type' => '', |
| 75 |
'requirements' => array(), |
| 76 |
) ); |
| 77 |
|
| 78 |
$title = sanitize_text_field( $args['title'] ); |
| 79 |
$description = sanitize_text_field( $args['description'] ); |
| 80 |
$rank_type = sanitize_text_field( $args['rank_type'] ); |
| 81 |
$requirements = $args['requirements']; |
| 82 |
|
| 83 |
// Check the rank type |
| 84 |
$type = gamipress_get_rank_type( $rank_type ); |
| 85 |
if( ! $type ) |
| 86 |
return $this->response_error( sprintf( __( 'I couldn\'t find the rank type "%s".', 'gamipress' ), $rank_type ) ); |
| 87 |
|
| 88 |
// Rank position |
| 89 |
$posts = GamiPress()->db->posts; |
| 90 |
$position = absint( $wpdb->get_var( $wpdb->prepare( |
| 91 |
"SELECT p.menu_order |
| 92 |
FROM {$posts} AS p |
| 93 |
WHERE p.post_type = %s |
| 94 |
ORDER BY menu_order DESC |
| 95 |
LIMIT 1", |
| 96 |
$rank_type, |
| 97 |
) ) ); |
| 98 |
|
| 99 |
$position += 1; |
| 100 |
|
| 101 |
// Make description block compatible |
| 102 |
$description = '<!-- wp:paragraph -->' . $description . '<!-- /wp:paragraph -->'; |
| 103 |
|
| 104 |
// Insert post |
| 105 |
$post_data = array( |
| 106 |
'post_title' => $title, |
| 107 |
'post_content' => $description, |
| 108 |
'post_type' => $rank_type, |
| 109 |
'menu_order' => $position, |
| 110 |
'post_status' => 'draft', // TODO: FUTURE: Add optional input schema to manage status, for the moment, draft to prevent published unwanted results |
| 111 |
); |
| 112 |
|
| 113 |
$post_id = wp_insert_post( $post_data, true ); |
| 114 |
|
| 115 |
if( is_wp_error( $post_id ) ) { |
| 116 |
// translators: %1$s: Rank type singular %2$s: Error messages |
| 117 |
return $this->response_error( sprintf( __( 'I couldn\'t create the %1$s. Reason: %2$s.', 'gamipress' ), $type['singular_name'], $post_id->get_error_message() ) ); |
| 118 |
} |
| 119 |
|
| 120 |
// Requirements |
| 121 |
$updated_requirements = gamipress_ai_assistant_process_requirements( |
| 122 |
$requirements, |
| 123 |
$post_id, |
| 124 |
'rank-requirement' |
| 125 |
); |
| 126 |
|
| 127 |
$requirements_count = count( $updated_requirements ); |
| 128 |
|
| 129 |
$requirements_answer = gamipress_ai_assistant_get_requirements_answer( $updated_requirements ); |
| 130 |
|
| 131 |
// Final response |
| 132 |
$post = get_post( $post_id ); |
| 133 |
$post_link = get_edit_post_link( $post ); |
| 134 |
$post_display = empty( $post->post_title ) ? esc_html__( '(no title)' ) : $post->post_title; |
| 135 |
|
| 136 |
if( $post_link !== null ) { |
| 137 |
$post_display = '[' . $post_display . '](' . $post_link . ')'; |
| 138 |
} |
| 139 |
|
| 140 |
if ( $requirements_count > 0 ) { |
| 141 |
$message = sprintf( __( 'I created the %1$s %2$s that requires:', 'gamipress' ), $type['singular_name'], $post_display ); |
| 142 |
$message .= $requirements_answer; |
| 143 |
} else{ |
| 144 |
// translators: %1$s: Rank type singular %2$s: Rank name |
| 145 |
$message = sprintf( __( 'I created the %1$s %2$s.', 'gamipress' ), $type['singular_name'], $post_display ); |
| 146 |
} |
| 147 |
|
| 148 |
return $this->response_success( $message ); |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
} |
| 153 |
new GamiPress_AI_Assistant_Ability_Create_Rank(); |