| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: get a single NotificationX notification with its full configuration. |
| 4 |
* |
| 5 |
* @package NotificationX\Abilities\Read |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace NotificationX\Abilities\Read; |
| 9 |
|
| 10 |
use NotificationX\Abilities\AbilityBase; |
| 11 |
use NotificationX\Abilities\BuilderInfo; |
| 12 |
use NotificationX\Core\PostType; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Returns the full stored configuration of one notification. |
| 20 |
*/ |
| 21 |
class GetNotification extends AbilityBase { |
| 22 |
|
| 23 |
protected $id = 'notificationx/get-notification'; |
| 24 |
protected $label = 'Get notification'; |
| 25 |
protected $description = 'Get the full configuration of a single NotificationX notification by its id (nx_id), including type, source, theme, content and display settings.'; |
| 26 |
|
| 27 |
public function input_schema() { |
| 28 |
return array( |
| 29 |
'type' => 'object', |
| 30 |
'required' => array( 'nx_id' ), |
| 31 |
'properties' => array( |
| 32 |
'nx_id' => array( |
| 33 |
'type' => 'integer', |
| 34 |
'description' => 'The notification id to fetch.', |
| 35 |
), |
| 36 |
), |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
public function output_schema() { |
| 41 |
return array( |
| 42 |
'type' => 'object', |
| 43 |
'properties' => array( |
| 44 |
'enabled' => array( 'type' => 'boolean' ), |
| 45 |
'theme_valid' => array( 'type' => 'boolean' ), |
| 46 |
'notification' => array( 'type' => 'object' ), |
| 47 |
), |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
public function execute( $input ) { |
| 52 |
$nx_id = (int) $input['nx_id']; |
| 53 |
$post_type = PostType::get_instance(); |
| 54 |
$post = $post_type->get_post( $nx_id ); |
| 55 |
|
| 56 |
if ( empty( $post ) ) { |
| 57 |
return new \WP_Error( |
| 58 |
'nx_mcp_not_found', |
| 59 |
/* translators: %d: notification id. */ |
| 60 |
sprintf( __( 'No notification found with id %d.', 'notificationx' ), $nx_id ), |
| 61 |
array( 'status' => 404 ) |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
// Authoritative active state (from the enabled-source map, not the blob). |
| 66 |
$enabled = (bool) $post_type->is_enabled( $nx_id ); |
| 67 |
// Whether the stored theme actually exists for this source (a false here |
| 68 |
// is the usual reason a notification saves but renders nothing). |
| 69 |
$source = isset( $post['source'] ) ? $post['source'] : ''; |
| 70 |
$theme = isset( $post['themes'] ) ? $post['themes'] : ( isset( $post['theme'] ) ? $post['theme'] : '' ); |
| 71 |
$theme_valid = $source && $theme ? BuilderInfo::is_valid_theme( $source, $theme ) : false; |
| 72 |
|
| 73 |
return array( |
| 74 |
'enabled' => $enabled, |
| 75 |
'theme_valid' => $theme_valid, |
| 76 |
'notification' => $post, |
| 77 |
); |
| 78 |
} |
| 79 |
} |
| 80 |
|