PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / trunk
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar vtrunk
3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 All 156 releases
notificationx / includes / Abilities / Manage / ToggleNotification.php

ToggleNotification.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar trunk, at includes/Abilities/Manage/ToggleNotification.php

113 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Ability: enable or disable a 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 * Toggles a notification active/inactive. Honours the free-plan limit of one
19 * active notification at a time (enforced by PostType::can_enable()).
20 */
21 class ToggleNotification extends AbilityBase {
22
23 protected $id = 'notificationx/toggle-notification';
24 protected $label = 'Enable or disable a notification';
25 protected $description = 'Enable (activate) or disable (deactivate) a notification by its id. On the free plan only one notification can be active at a time, so enabling one may require disabling another first.';
26 protected $is_write = true;
27 protected $is_idempotent = true;
28
29 public function input_schema() {
30 return array(
31 'type' => 'object',
32 'required' => array( 'nx_id', 'enabled' ),
33 'properties' => array(
34 'nx_id' => array(
35 'type' => 'integer',
36 'description' => 'The notification id to toggle.',
37 ),
38 'enabled' => array(
39 'type' => 'boolean',
40 'description' => 'true to activate, false to deactivate.',
41 ),
42 ),
43 );
44 }
45
46 public function output_schema() {
47 return array(
48 'type' => 'object',
49 'properties' => array(
50 'nx_id' => array( 'type' => 'integer' ),
51 'enabled' => array( 'type' => 'boolean' ),
52 ),
53 );
54 }
55
56 public function execute( $input ) {
57 $nx_id = (int) $input['nx_id'];
58 $enabled = (bool) $input['enabled'];
59
60 $post_type = PostType::get_instance();
61 $post = $post_type->get_post( $nx_id );
62
63 if ( empty( $post ) ) {
64 return new \WP_Error(
65 'nx_mcp_not_found',
66 /* translators: %d: notification id. */
67 sprintf( __( 'No notification found with id %d.', 'notificationx' ), $nx_id ),
68 array( 'status' => 404 )
69 );
70 }
71
72 $source = isset( $post['source'] ) ? $post['source'] : '';
73 $currently_enabled = $post_type->is_enabled( $nx_id );
74
75 // No change requested.
76 if ( $currently_enabled === $enabled ) {
77 return array(
78 'nx_id' => $nx_id,
79 'enabled' => $enabled,
80 'message' => __( 'No change; notification already in the requested state.', 'notificationx' ),
81 );
82 }
83
84 // Enabling: respect the free-plan single-active-notification cap.
85 if ( $enabled && ! $currently_enabled && ! $post_type->can_enable( $source ) ) {
86 return new \WP_Error(
87 'nx_mcp_cap_reached',
88 __( 'Another notification is already active. On the free plan only one notification can be active at a time — disable the active one first, or upgrade to Pro.', 'notificationx' ),
89 array( 'status' => 409 )
90 );
91 }
92
93 $post_type->save_post(
94 array(
95 'update_status' => true,
96 'nx_id' => $nx_id,
97 'enabled' => $enabled,
98 'source' => $source,
99 )
100 );
101
102 // Re-read the stored record instead of PostType::is_enabled(), whose
103 // enabled-source list is memoized before the toggle and would report
104 // the pre-save state to the caller.
105 $fresh = $post_type->get_post( $nx_id );
106
107 return array(
108 'nx_id' => $nx_id,
109 'enabled' => ! empty( $fresh['enabled'] ),
110 );
111 }
112 }
113