| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: list NotificationX notifications. |
| 4 |
* |
| 5 |
* @package NotificationX\Abilities\Read |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace NotificationX\Abilities\Read; |
| 9 |
|
| 10 |
use NotificationX\Abilities\AbilityBase; |
| 11 |
use NotificationX\Core\PostType; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Returns a compact list of the site's notifications with their status. |
| 19 |
*/ |
| 20 |
class ListNotifications extends AbilityBase { |
| 21 |
|
| 22 |
protected $id = 'notificationx/list-notifications'; |
| 23 |
protected $label = 'List notifications'; |
| 24 |
protected $description = 'List NotificationX campaigns on this site with their type, data source, theme and active status. Supports filtering by source, type and enabled state.'; |
| 25 |
|
| 26 |
public function input_schema() { |
| 27 |
return array( |
| 28 |
'type' => 'object', |
| 29 |
'properties' => array( |
| 30 |
'enabled' => array( |
| 31 |
'type' => 'boolean', |
| 32 |
'description' => 'Only return notifications that are active (true) or inactive (false). Omit for all.', |
| 33 |
), |
| 34 |
'source' => array( |
| 35 |
'type' => 'string', |
| 36 |
'description' => 'Filter by data source id (e.g. "woocommerce", "comments"). Omit for all.', |
| 37 |
), |
| 38 |
'type' => array( |
| 39 |
'type' => 'string', |
| 40 |
'description' => 'Filter by notification type id (e.g. "conversions", "reviews"). Omit for all.', |
| 41 |
), |
| 42 |
'limit' => array( |
| 43 |
'type' => 'integer', |
| 44 |
'description' => 'Maximum number of notifications to return (default 50, max 200).', |
| 45 |
), |
| 46 |
), |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
public function output_schema() { |
| 51 |
return array( |
| 52 |
'type' => 'object', |
| 53 |
'properties' => array( |
| 54 |
'count' => array( 'type' => 'integer' ), |
| 55 |
'notifications' => array( 'type' => 'array' ), |
| 56 |
), |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
public function execute( $input ) { |
| 61 |
$wheres = array(); |
| 62 |
if ( array_key_exists( 'enabled', $input ) ) { |
| 63 |
$wheres['enabled'] = (bool) $input['enabled']; |
| 64 |
} |
| 65 |
if ( ! empty( $input['source'] ) ) { |
| 66 |
$wheres['source'] = $input['source']; |
| 67 |
} |
| 68 |
if ( ! empty( $input['type'] ) ) { |
| 69 |
$wheres['type'] = $input['type']; |
| 70 |
} |
| 71 |
|
| 72 |
$limit = isset( $input['limit'] ) ? min( 200, max( 1, (int) $input['limit'] ) ) : 50; |
| 73 |
|
| 74 |
$posts = PostType::get_instance()->get_posts( $wheres ); |
| 75 |
if ( ! is_array( $posts ) ) { |
| 76 |
$posts = array(); |
| 77 |
} |
| 78 |
|
| 79 |
$posts = array_slice( $posts, 0, $limit ); |
| 80 |
|
| 81 |
$notifications = array(); |
| 82 |
foreach ( $posts as $post ) { |
| 83 |
$notifications[] = array( |
| 84 |
'nx_id' => isset( $post['nx_id'] ) ? (int) $post['nx_id'] : 0, |
| 85 |
'title' => isset( $post['title'] ) ? $post['title'] : '', |
| 86 |
'type' => isset( $post['type'] ) ? $post['type'] : '', |
| 87 |
'type_label' => isset( $post['type_label'] ) ? $post['type_label'] : '', |
| 88 |
'source' => isset( $post['source'] ) ? $post['source'] : '', |
| 89 |
'source_label' => isset( $post['source_label'] ) ? $post['source_label'] : '', |
| 90 |
'theme' => isset( $post['themes'] ) ? $post['themes'] : ( isset( $post['theme'] ) ? $post['theme'] : '' ), |
| 91 |
'enabled' => ! empty( $post['enabled'] ), |
| 92 |
'created_at' => isset( $post['created_at'] ) ? $post['created_at'] : '', |
| 93 |
'updated_at' => isset( $post['updated_at'] ) ? $post['updated_at'] : '', |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
return array( |
| 98 |
'count' => count( $notifications ), |
| 99 |
'notifications' => $notifications, |
| 100 |
); |
| 101 |
} |
| 102 |
} |
| 103 |
|