| 1 |
<?php |
| 2 |
/** |
| 3 |
* AI Ability |
| 4 |
* |
| 5 |
* @package GamiPress\Classes\AI_Ability |
| 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 { |
| 13 |
|
| 14 |
public $integration = 'gamipress'; |
| 15 |
public $ability = ''; |
| 16 |
public $name = ''; // integration/ability |
| 17 |
public $label = ''; // Set on register |
| 18 |
public $description = ''; // Set on register |
| 19 |
public $category = 'gamipress'; |
| 20 |
public $args = array(); |
| 21 |
|
| 22 |
// Meta args |
| 23 |
public $readonly = false; |
| 24 |
public $destructive = false; |
| 25 |
public $idempotent = true; |
| 26 |
public $show_in_rest = true; |
| 27 |
|
| 28 |
public function __construct() { |
| 29 |
|
| 30 |
$this->hooks(); |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Register the required hooks |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
*/ |
| 39 |
public function hooks() { |
| 40 |
|
| 41 |
add_action( 'wp_abilities_api_init', array( $this, 'register_ability' ) ); |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Register |
| 47 |
* |
| 48 |
* @since 1.0.0 |
| 49 |
*/ |
| 50 |
public function register() { |
| 51 |
// Override |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Register the ability |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
*/ |
| 59 |
public function register_ability() { |
| 60 |
|
| 61 |
// Init the subclass vars |
| 62 |
$this->register(); |
| 63 |
|
| 64 |
$this->name = $this->integration . '/' . $this->ability; |
| 65 |
|
| 66 |
$this->args = wp_parse_args( $this->args, array( |
| 67 |
'label' => $this->label, // Override |
| 68 |
'description' => $this->description, // Override |
| 69 |
'category' => $this->category, |
| 70 |
'input_schema' => $this->input_schema(), // Override |
| 71 |
'output_schema' => $this->output_schema(), |
| 72 |
'execute_callback' => array( $this, 'execute' ), |
| 73 |
'permission_callback' => array( $this, 'permission' ), |
| 74 |
'meta' => $this->meta(), |
| 75 |
) ); |
| 76 |
|
| 77 |
wp_register_ability( $this->name, $this->args ); |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Input schema |
| 83 |
* |
| 84 |
* @since 1.0.0 |
| 85 |
* |
| 86 |
* @return array |
| 87 |
*/ |
| 88 |
public function input_schema() { |
| 89 |
// Override |
| 90 |
return array(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Output schema |
| 95 |
* |
| 96 |
* @since 1.0.0 |
| 97 |
* |
| 98 |
* @return array |
| 99 |
*/ |
| 100 |
public function output_schema() { |
| 101 |
return array( |
| 102 |
'type' => 'object', |
| 103 |
'properties' => array( |
| 104 |
// Default to match with expected in execute |
| 105 |
'success' => array( 'type' => 'boolean' ), |
| 106 |
'message' => array( 'type' => 'string' ), |
| 107 |
), |
| 108 |
'required' => array( 'success', 'message' ), |
| 109 |
'additionalProperties' => true, |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Meta |
| 115 |
* |
| 116 |
* @since 1.0.0 |
| 117 |
* |
| 118 |
* @return array |
| 119 |
*/ |
| 120 |
public function meta() { |
| 121 |
return array( |
| 122 |
'annotations' => array( |
| 123 |
'readonly' => $this->readonly, |
| 124 |
'destructive' => $this->destructive, |
| 125 |
'idempotent' => $this->idempotent, |
| 126 |
), |
| 127 |
'show_in_rest' => $this->show_in_rest, |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Ability permission callback |
| 133 |
* |
| 134 |
* @since 1.0.0 |
| 135 |
*/ |
| 136 |
public function permission() { |
| 137 |
return current_user_can( 'manage_options' ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Ability execute callback |
| 142 |
* |
| 143 |
* @since 1.0.0 |
| 144 |
* |
| 145 |
* @param array $args |
| 146 |
* |
| 147 |
* @return array array( 'success' => true|false, 'message' => '' ) |
| 148 |
*/ |
| 149 |
public function execute( $args ) { |
| 150 |
// Override |
| 151 |
return array(); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Returns a response following the ability output schema |
| 156 |
* |
| 157 |
* @since 1.0.0 |
| 158 |
* |
| 159 |
* @param string $message |
| 160 |
* @param bool $success |
| 161 |
* |
| 162 |
* @return array |
| 163 |
*/ |
| 164 |
public function response( $message, $success = true ) { |
| 165 |
if( $success ) |
| 166 |
return $this->response_success( $message ); |
| 167 |
else |
| 168 |
return $this->response_error( $message ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Returns a success response |
| 173 |
* |
| 174 |
* @since 1.0.0 |
| 175 |
* |
| 176 |
* @param string $message |
| 177 |
* |
| 178 |
* @return array |
| 179 |
*/ |
| 180 |
public function response_success( $message ) { |
| 181 |
|
| 182 |
return array( |
| 183 |
'success' => true, |
| 184 |
'message' => $this->get_response_prefix( true ) . ' ' . $message, |
| 185 |
); |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Returns an error response |
| 191 |
* |
| 192 |
* @since 1.0.0 |
| 193 |
* |
| 194 |
* @param string $message |
| 195 |
* |
| 196 |
* @return array |
| 197 |
*/ |
| 198 |
public function response_error( $message ) { |
| 199 |
|
| 200 |
return array( |
| 201 |
'success' => false, |
| 202 |
'message' => $this->get_response_prefix( false ) . ' ' . $message, |
| 203 |
); |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
public function is_response_error( $thing ) { |
| 208 |
|
| 209 |
if( is_array( $thing ) |
| 210 |
&& isset( $thing['message'] ) |
| 211 |
&& isset( $thing['success'] ) |
| 212 |
&& $thing['success'] === false |
| 213 |
) { |
| 214 |
return true; |
| 215 |
} |
| 216 |
|
| 217 |
return false; |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Gets a random prefix |
| 223 |
* |
| 224 |
* @since 1.0.0 |
| 225 |
* |
| 226 |
* @param bool $success |
| 227 |
* |
| 228 |
* @return string |
| 229 |
*/ |
| 230 |
public function get_response_prefix( $success = true ) { |
| 231 |
|
| 232 |
$prefixes = array( |
| 233 |
__( 'Success!', 'gamipress' ), |
| 234 |
__( 'Done!', 'gamipress' ), |
| 235 |
__( 'Ready!', 'gamipress' ), |
| 236 |
__( 'Complete!', 'gamipress' ), |
| 237 |
__( 'Completed!', 'gamipress' ), |
| 238 |
__( 'Finish!', 'gamipress' ), |
| 239 |
__( 'Finished!', 'gamipress' ), |
| 240 |
__( 'It\'s done!', 'gamipress' ), |
| 241 |
__( 'I\'ve finished.', 'gamipress' ), |
| 242 |
); |
| 243 |
|
| 244 |
if( ! $success ) { |
| 245 |
$prefixes = array( |
| 246 |
__( 'Oops!', 'gamipress' ), |
| 247 |
__( 'Failed!', 'gamipress' ), |
| 248 |
__( 'Couldn\'t complete.', 'gamipress' ), |
| 249 |
__( 'Something went wrong.', 'gamipress' ), |
| 250 |
); |
| 251 |
} |
| 252 |
|
| 253 |
$i = array_rand( $prefixes, 1 ); |
| 254 |
|
| 255 |
return isset( $prefixes[$i] ) ? $prefixes[$i] : ''; |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
public function parse_user_query_args( $args = array() ) { |
| 260 |
$args = wp_parse_args( $args, array( |
| 261 |
'limit' => 3, |
| 262 |
) ); |
| 263 |
|
| 264 |
return $args; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Search for users from a search term. |
| 269 |
* |
| 270 |
* @since 1.0.0 |
| 271 |
* |
| 272 |
* @param string|int $search Search term to find the user(s) |
| 273 |
* @param array $args Extra search args |
| 274 |
* |
| 275 |
* @return array |
| 276 |
*/ |
| 277 |
public function resolve_user_ids( $search = '', $args = array() ) { |
| 278 |
|
| 279 |
global $wpdb; |
| 280 |
|
| 281 |
if ( empty( $search ) ) return array(); |
| 282 |
|
| 283 |
$args = $this->parse_user_query_args( $args ); |
| 284 |
|
| 285 |
// Pull back the search string |
| 286 |
$search = esc_sql( $wpdb->esc_like( $search ) ); |
| 287 |
$where = ''; |
| 288 |
$from = "FROM {$wpdb->users} as u "; |
| 289 |
|
| 290 |
$where = "WHERE ( "; |
| 291 |
|
| 292 |
$where .= "u.user_login LIKE '%{$search}%' "; |
| 293 |
$where .= "OR u.display_name LIKE '%{$search}%' "; |
| 294 |
|
| 295 |
if( is_numeric( $search ) ) |
| 296 |
$where .= "OR u.ID = '{$search}' "; |
| 297 |
|
| 298 |
if( strpos( $search, '@' ) !== false ) |
| 299 |
$where .= "OR u.user_email LIKE '%{$search}%' "; |
| 300 |
|
| 301 |
$where .= " ) "; |
| 302 |
|
| 303 |
$limit = isset( $args['limit'] ) ? absint( $args['limit'] ) : 3; |
| 304 |
|
| 305 |
$user_ids = $wpdb->get_col( |
| 306 |
"SELECT u.ID |
| 307 |
{$from} |
| 308 |
{$where} |
| 309 |
LIMIT {$limit}", |
| 310 |
); |
| 311 |
|
| 312 |
return $user_ids; |
| 313 |
|
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Get user from a search term. |
| 318 |
* |
| 319 |
* @since 1.0.0 |
| 320 |
* |
| 321 |
* @param string|int $search Search term to find the user |
| 322 |
* |
| 323 |
* @return WP_User|array WP_User object or an error response array |
| 324 |
*/ |
| 325 |
public function get_user( $search = '' ) { |
| 326 |
|
| 327 |
// If search is a number, check if there is a user with that ID |
| 328 |
if( is_numeric( $search ) && absint( $search ) > 0 ) { |
| 329 |
$user = get_user_by( 'id', absint( $search ) ); |
| 330 |
|
| 331 |
if( $user ) return $user; |
| 332 |
} |
| 333 |
|
| 334 |
// Ssearch the user by term |
| 335 |
$user_ids = $this->resolve_user_ids( $search ); |
| 336 |
|
| 337 |
// Bail if can not find the user |
| 338 |
if( count( $user_ids ) === 0 ) { |
| 339 |
return $this->response_error( sprintf( |
| 340 |
__( 'I couldn\'t find the user "%s".', 'gamipress' ) |
| 341 |
. ' ' . __( 'Try providing a different name, email or another identifying detail.', 'gamipress' ), |
| 342 |
$search |
| 343 |
) ); |
| 344 |
} else if( count( $user_ids ) > 1 ) { |
| 345 |
// Bail if more than 1 user found |
| 346 |
// IMPORTANT: use $this->get_users() if you need multiple users |
| 347 |
return $this->response_error( sprintf( |
| 348 |
__( 'I found several users matching with "%s".', 'gamipress' ) |
| 349 |
. ' ' . __( 'Try providing a different name, email or another identifying detail.', 'gamipress' ), |
| 350 |
$search |
| 351 |
) ); |
| 352 |
} |
| 353 |
|
| 354 |
$user = get_user_by( 'id', absint( $user_ids[0] ) ); |
| 355 |
|
| 356 |
if( ! $user ) { |
| 357 |
return $this->response_error( sprintf( |
| 358 |
__( 'I couldn\'t find the user "%s".', 'gamipress' ) |
| 359 |
. ' ' . __( 'Try providing a different name, email or another identifying detail.', 'gamipress' ), |
| 360 |
$search |
| 361 |
) ); |
| 362 |
} |
| 363 |
|
| 364 |
return $user; |
| 365 |
|
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Get users from a search term. |
| 370 |
* |
| 371 |
* @since 1.0.0 |
| 372 |
* |
| 373 |
* @param string|int $search Search term to find the users |
| 374 |
* |
| 375 |
* @return array WP_User array or an error response array |
| 376 |
*/ |
| 377 |
public function get_users( $search = '' ) { |
| 378 |
|
| 379 |
$user_ids = $this->resolve_user_ids( $search ); |
| 380 |
|
| 381 |
// Bail if can not find the user |
| 382 |
if( count( $user_ids ) === 0 ) { |
| 383 |
return $this->response_error( sprintf( |
| 384 |
__( 'I couldn\'t find any user by "%s".', 'gamipress' ) |
| 385 |
. ' ' . __( 'Try providing a different name, email or another identifying detail.', 'gamipress' ), |
| 386 |
$search |
| 387 |
) ); |
| 388 |
} |
| 389 |
|
| 390 |
$users = array(); |
| 391 |
|
| 392 |
foreach( $user_ids as $user_id ) { |
| 393 |
$user = get_user_by( 'id', absint( $user_id ) ); |
| 394 |
|
| 395 |
if( $user ) $users[] = $user; |
| 396 |
} |
| 397 |
|
| 398 |
if( count( $users ) === 0 ) { |
| 399 |
return $this->response_error( sprintf( |
| 400 |
__( 'I couldn\'t find any user by "%s".', 'gamipress' ) |
| 401 |
. ' ' . __( 'Try providing a different name, email or another identifying detail.', 'gamipress' ), |
| 402 |
$search |
| 403 |
) ); |
| 404 |
} |
| 405 |
|
| 406 |
return $users; |
| 407 |
|
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
* Search for posts from a search term. |
| 412 |
* |
| 413 |
* @since 1.0.0 |
| 414 |
* |
| 415 |
* @param string|int $search Search term to find the post(s) |
| 416 |
* @param array $args Extra search args |
| 417 |
* |
| 418 |
* @return array |
| 419 |
*/ |
| 420 |
public function resolve_post_ids( $search = '', $args = array() ) { |
| 421 |
|
| 422 |
global $wpdb; |
| 423 |
|
| 424 |
if ( empty( $search ) ) return array(); |
| 425 |
|
| 426 |
$args = $this->parse_post_query_args( $args ); |
| 427 |
|
| 428 |
// Pull back the search string |
| 429 |
$search = esc_sql( $wpdb->esc_like( $search ) ); |
| 430 |
$from = "FROM {$wpdb->posts} as p "; |
| 431 |
|
| 432 |
$where = "WHERE ( "; |
| 433 |
|
| 434 |
// Search for title or slug |
| 435 |
$where .= "p.post_title LIKE '%{$search}%' "; |
| 436 |
$where .= "OR p.post_name LIKE '%{$search}%' "; |
| 437 |
|
| 438 |
// Search by ID |
| 439 |
if( is_numeric( $search ) ) |
| 440 |
$where .= "OR p.ID = '{$search}' "; |
| 441 |
|
| 442 |
$where .= " ) "; |
| 443 |
|
| 444 |
// Status publish, private or draft (preventing in trash or pending) |
| 445 |
$where .= "AND p.post_status IN ( 'publish', 'private', 'draft' ) "; |
| 446 |
|
| 447 |
// Post type |
| 448 |
if( ! empty( $args['post_type'] ) ) { |
| 449 |
$post_type = esc_sql( $args['post_type'] ); |
| 450 |
$where .= "AND p.post_type = '{$post_type}' "; |
| 451 |
} |
| 452 |
|
| 453 |
$limit = isset( $args['limit'] ) ? absint( $args['limit'] ) : 3; |
| 454 |
|
| 455 |
$post_ids = $wpdb->get_col( |
| 456 |
"SELECT p.ID |
| 457 |
{$from} |
| 458 |
{$where} |
| 459 |
LIMIT {$limit}", |
| 460 |
); |
| 461 |
|
| 462 |
return $post_ids; |
| 463 |
|
| 464 |
} |
| 465 |
|
| 466 |
public function parse_post_query_args( $args = array() ) { |
| 467 |
$args = wp_parse_args( $args, array( |
| 468 |
'post_type' => '', |
| 469 |
'limit' => 3, |
| 470 |
) ); |
| 471 |
|
| 472 |
return $args; |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* Get post from a search term. |
| 477 |
* |
| 478 |
* @since 1.0.0 |
| 479 |
* |
| 480 |
* @param string|int $search Search term to find the user |
| 481 |
* @param array $args Extra search args |
| 482 |
* |
| 483 |
* @return WP_User|array WP_User object or an error response array |
| 484 |
*/ |
| 485 |
public function get_post( $search = '', $args = array() ) { |
| 486 |
|
| 487 |
$args = $this->parse_post_query_args( $args ); |
| 488 |
|
| 489 |
$post_ids = $this->resolve_post_ids( $search, $args ); |
| 490 |
$post_type = false; |
| 491 |
|
| 492 |
if( isset( $args['post_type'] ) ) |
| 493 |
$post_type = get_post_type_object( $args['post_type'] ); |
| 494 |
|
| 495 |
if( ! $post_type ) |
| 496 |
$post_type = get_post_type_object( 'post' ); |
| 497 |
|
| 498 |
// Bail if can not find the post |
| 499 |
if( count( $post_ids ) === 0 ) { |
| 500 |
return $this->response_error( sprintf( |
| 501 |
// translators: %1$s: Post type singular %2$s: Search term |
| 502 |
__( 'I couldn\'t find the %1$s "%2$s".', 'gamipress' ) |
| 503 |
. ' ' . __( 'Try providing a different title, slug or another identifying detail.', 'gamipress' ), |
| 504 |
$post_type->labels->singular_name, |
| 505 |
$search |
| 506 |
) ); |
| 507 |
} else if( count( $post_ids ) > 1 ) { |
| 508 |
// Bail if more than 1 post found |
| 509 |
return $this->response_error( sprintf( |
| 510 |
// translators: %1$s: Post type plural %2$s: Search term |
| 511 |
__( 'I found several %1$s matching with "%2$s".', 'gamipress' ) |
| 512 |
. ' ' . __( 'Try providing a different title, slug or another identifying detail.', 'gamipress' ), |
| 513 |
$post_type->labels->name, |
| 514 |
$search |
| 515 |
) ); |
| 516 |
} |
| 517 |
|
| 518 |
$post = get_post( absint( $post_ids[0] ) ); |
| 519 |
|
| 520 |
if( ! $post ) { |
| 521 |
return $this->response_error( sprintf( |
| 522 |
// translators: %1$s: Post type singular %2$s: Search term |
| 523 |
__( 'I couldn\'t find the %1$s "%2$s".', 'gamipress' ) |
| 524 |
. ' ' . __( 'Try providing a different title, slug or another identifying detail.', 'gamipress' ), |
| 525 |
$post_type->labels->singular_name, |
| 526 |
$search |
| 527 |
) ); |
| 528 |
} |
| 529 |
|
| 530 |
return $post; |
| 531 |
|
| 532 |
} |
| 533 |
|
| 534 |
/** |
| 535 |
* Get post from a search term. |
| 536 |
* |
| 537 |
* @since 1.0.0 |
| 538 |
* |
| 539 |
* @param string|int $search Search term to find the user |
| 540 |
* @param array $args Extra search args |
| 541 |
* |
| 542 |
* @return array WP_Post array or an error response array |
| 543 |
*/ |
| 544 |
public function get_posts( $search = '', $args = array() ) { |
| 545 |
|
| 546 |
$args = $this->parse_post_query_args( $args ); |
| 547 |
|
| 548 |
$post_ids = $this->resolve_post_ids( $search, $args ); |
| 549 |
$post_type = false; |
| 550 |
|
| 551 |
if( isset( $args['post_type'] ) ) { |
| 552 |
$post_type = get_post_type_object( $args['post_type'] ); |
| 553 |
} |
| 554 |
|
| 555 |
if( ! $post_type ) { |
| 556 |
$post_type = get_post_type_object( 'post' ); |
| 557 |
} |
| 558 |
|
| 559 |
// Bail if can not find the post |
| 560 |
if( count( $post_ids ) === 0 ) { |
| 561 |
return $this->response_error( sprintf( |
| 562 |
// translators: %1$s: Post type plural %2$s: Search term |
| 563 |
__( 'I couldn\'t find any %s "%s".', 'gamipress' ) |
| 564 |
. ' ' . __( 'Try providing a different title, slug or another identifying detail.', 'gamipress' ), |
| 565 |
$post_type->labels->name, |
| 566 |
$search |
| 567 |
) ); |
| 568 |
} |
| 569 |
|
| 570 |
$posts = array(); |
| 571 |
|
| 572 |
foreach( $post_ids as $post_id ) { |
| 573 |
$post = get_post( absint( $post_id ) ); |
| 574 |
|
| 575 |
if( $post ) $posts[] = $post; |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
if( count( $posts ) === 0 ) { |
| 580 |
return $this->response_error( sprintf( |
| 581 |
// translators: %1$s: Post type plural %2$s: Search term |
| 582 |
__( 'I couldn\'t find any %s "%s".', 'gamipress' ) |
| 583 |
. ' ' . __( 'Try providing a different title, slug or another identifying detail.', 'gamipress' ), |
| 584 |
$post_type->labels->name, |
| 585 |
$search |
| 586 |
) ); |
| 587 |
} |
| 588 |
|
| 589 |
return $posts; |
| 590 |
|
| 591 |
} |
| 592 |
|
| 593 |
} |