| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: duplicate an existing notification. |
| 4 |
* |
| 5 |
* @package NotificationX\Abilities\Manage |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace NotificationX\Abilities\Manage; |
| 9 |
|
| 10 |
use NotificationX\Abilities\AbilityBase; |
| 11 |
use NotificationX\Core\PostType; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Clones a notification into a new, inactive copy. |
| 19 |
*/ |
| 20 |
class DuplicateNotification extends AbilityBase { |
| 21 |
|
| 22 |
protected $id = 'notificationx/duplicate-notification'; |
| 23 |
protected $label = 'Duplicate a notification'; |
| 24 |
protected $description = 'Create a copy of an existing notification. The copy is created inactive (disabled) so it never conflicts with the free-plan single-active limit; enable it explicitly when ready.'; |
| 25 |
protected $is_write = true; |
| 26 |
protected $is_idempotent = false; |
| 27 |
|
| 28 |
public function input_schema() { |
| 29 |
return array( |
| 30 |
'type' => 'object', |
| 31 |
'required' => array( 'nx_id' ), |
| 32 |
'properties' => array( |
| 33 |
'nx_id' => array( |
| 34 |
'type' => 'integer', |
| 35 |
'description' => 'The notification id to duplicate.', |
| 36 |
), |
| 37 |
'title' => array( |
| 38 |
'type' => 'string', |
| 39 |
'description' => 'Optional title for the copy. Defaults to the original title with " (copy)" appended.', |
| 40 |
), |
| 41 |
), |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
public function output_schema() { |
| 46 |
return array( |
| 47 |
'type' => 'object', |
| 48 |
'properties' => array( |
| 49 |
'nx_id' => array( 'type' => 'integer' ), |
| 50 |
), |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
public function execute( $input ) { |
| 55 |
$nx_id = (int) $input['nx_id']; |
| 56 |
$post_type = PostType::get_instance(); |
| 57 |
$existing = $post_type->get_post( $nx_id ); |
| 58 |
|
| 59 |
if ( empty( $existing ) ) { |
| 60 |
return new \WP_Error( |
| 61 |
'nx_mcp_not_found', |
| 62 |
/* translators: %d: notification id. */ |
| 63 |
sprintf( __( 'No notification found with id %d.', 'notificationx' ), $nx_id ), |
| 64 |
array( 'status' => 404 ) |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
$data = $existing; |
| 69 |
unset( $data['nx_id'], $data['created_at'], $data['updated_at'] ); |
| 70 |
|
| 71 |
$original_title = isset( $existing['title'] ) ? $existing['title'] : ''; |
| 72 |
if ( ! empty( $input['title'] ) ) { |
| 73 |
$data['title'] = sanitize_text_field( $input['title'] ); |
| 74 |
} else { |
| 75 |
/* translators: %s: original notification title. */ |
| 76 |
$data['title'] = trim( sprintf( __( '%s (copy)', 'notificationx' ), $original_title ) ); |
| 77 |
} |
| 78 |
|
| 79 |
// Always create the copy disabled. |
| 80 |
$data['enabled'] = false; |
| 81 |
|
| 82 |
$saved = $post_type->save_post( $data ); |
| 83 |
$new_id = isset( $saved['nx_id'] ) ? (int) $saved['nx_id'] : 0; |
| 84 |
|
| 85 |
if ( empty( $new_id ) ) { |
| 86 |
return new \WP_Error( |
| 87 |
'nx_mcp_duplicate_failed', |
| 88 |
__( 'Could not duplicate the notification.', 'notificationx' ), |
| 89 |
array( 'status' => 500 ) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
return array( |
| 94 |
'nx_id' => $new_id, |
| 95 |
'enabled' => false, |
| 96 |
); |
| 97 |
} |
| 98 |
} |
| 99 |
|