|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Ability: stream/create-alert — create a Stream alert rule. |
| 4 | + * |
| 5 | + * @package WP_Stream |
| 6 | + */ |
| 7 | + |
| 8 | +namespace WP_Stream; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class - Ability_Create_Alert |
| 12 | + */ |
| 13 | +class Ability_Create_Alert extends Ability { |
| 14 | + |
| 15 | + /** |
| 16 | + * {@inheritDoc} |
| 17 | + */ |
| 18 | + public function get_name() { |
| 19 | + return 'stream/create-alert'; |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * {@inheritDoc} |
| 24 | + */ |
| 25 | + public function get_label() { |
| 26 | + return __( 'Create Stream Alert', 'stream' ); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * {@inheritDoc} |
| 31 | + */ |
| 32 | + public function get_description() { |
| 33 | + return __( 'Create a new Stream alert rule. Alerts notify configured channels when records matching the trigger filters are logged.', 'stream' ); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritDoc} |
| 38 | + */ |
| 39 | + public function get_annotations() { |
| 40 | + return array( |
| 41 | + 'readonly' => false, |
| 42 | + 'instructions' => __( 'Create an alert that fires whenever a record matches the supplied filters. Validate the connector/context/action with stream/get-connectors first, and confirm with the user before creating the alert because it changes site behavior.', 'stream' ), |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * {@inheritDoc} |
| 48 | + */ |
| 49 | + public function get_input_schema() { |
| 50 | + return array( |
| 51 | + 'type' => 'object', |
| 52 | + 'additionalProperties' => false, |
| 53 | + 'required' => array( 'alert_type', 'trigger_author', 'trigger_context', 'trigger_action' ), |
| 54 | + 'properties' => array( |
| 55 | + 'alert_type' => array( |
| 56 | + 'type' => 'string', |
| 57 | + 'description' => 'Notifier slug. Built-in types are none, highlight, email, ifttt, slack. Other slugs may be registered by extensions.', |
| 58 | + ), |
| 59 | + 'trigger_author' => array( |
| 60 | + 'type' => 'string', |
| 61 | + 'description' => 'User ID or role slug to match. Use "any" to match all authors.', |
| 62 | + ), |
| 63 | + 'trigger_context' => array( |
| 64 | + 'type' => 'string', |
| 65 | + 'description' => 'Connector or "connector-context" slug. Use "any" to match all contexts.', |
| 66 | + ), |
| 67 | + 'trigger_action' => array( |
| 68 | + 'type' => 'string', |
| 69 | + 'description' => 'Action slug to match (e.g. "updated"). Use "any" to match all actions.', |
| 70 | + ), |
| 71 | + 'alert_meta' => array( |
| 72 | + 'type' => 'object', |
| 73 | + 'description' => 'Additional notifier-specific configuration (e.g. email recipients, slack webhook).', |
| 74 | + 'additionalProperties' => true, |
| 75 | + ), |
| 76 | + 'status' => array( |
| 77 | + 'type' => 'string', |
| 78 | + 'description' => 'Initial alert status.', |
| 79 | + 'enum' => array( Alerts::STATUS_ENABLED, Alerts::STATUS_DISABLED ), |
| 80 | + 'default' => Alerts::STATUS_ENABLED, |
| 81 | + ), |
| 82 | + ), |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * {@inheritDoc} |
| 88 | + */ |
| 89 | + public function get_output_schema() { |
| 90 | + return array( |
| 91 | + 'type' => 'object', |
| 92 | + 'additionalProperties' => false, |
| 93 | + 'properties' => array( |
| 94 | + 'id' => array( 'type' => 'integer' ), |
| 95 | + 'status' => array( |
| 96 | + 'type' => 'string', |
| 97 | + 'enum' => array( Alerts::STATUS_ENABLED, Alerts::STATUS_DISABLED ), |
| 98 | + ), |
| 99 | + 'title' => array( 'type' => 'string' ), |
| 100 | + 'alert_type' => array( 'type' => array( 'string', 'null' ) ), |
| 101 | + 'alert_meta' => array( |
| 102 | + 'type' => 'object', |
| 103 | + 'additionalProperties' => true, |
| 104 | + ), |
| 105 | + ), |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * {@inheritDoc} |
| 111 | + * |
| 112 | + * @param mixed $input Validated input matching get_input_schema(), or null. |
| 113 | + */ |
| 114 | + public function execute( $input = null ) { |
| 115 | + $status = isset( $input['status'] ) ? $input['status'] : Alerts::STATUS_ENABLED; |
| 116 | + |
| 117 | + // Validate alert_type against the registered notifier slugs. The schema |
| 118 | + // can't enum these because alert types are extensible via the |
| 119 | + // wp_stream_alert_types filter -- a hardcoded enum would lock out |
| 120 | + // 3rd-party notifiers. Validate at execute() time instead. |
| 121 | + $registered_types = isset( $this->plugin->alerts->alert_types ) |
| 122 | + ? array_keys( (array) $this->plugin->alerts->alert_types ) |
| 123 | + : array(); |
| 124 | + if ( ! empty( $registered_types ) && ! in_array( $input['alert_type'], $registered_types, true ) ) { |
| 125 | + return new \WP_Error( |
| 126 | + 'stream_unknown_alert_type', |
| 127 | + sprintf( |
| 128 | + /* translators: 1: alert_type slug supplied by caller, 2: comma-separated list of registered alert type slugs */ |
| 129 | + __( 'Unknown alert_type "%1$s". Registered types: %2$s.', 'stream' ), |
| 130 | + (string) $input['alert_type'], |
| 131 | + implode( ', ', $registered_types ) |
| 132 | + ), |
| 133 | + array( 'status' => 400 ) |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + // Mirror the admin form's connector-context split so Alert::get_title() |
| 138 | + // and Alert_Trigger_Context::check_record() see the same data shape |
| 139 | + // they see when an admin creates the alert through the UI. |
| 140 | + $trigger_context_raw = (string) $input['trigger_context']; |
| 141 | + if ( false !== strpos( $trigger_context_raw, '-' ) ) { |
| 142 | + list( $trigger_connector, $trigger_context ) = explode( '-', $trigger_context_raw, 2 ); |
| 143 | + } else { |
| 144 | + $trigger_connector = $trigger_context_raw; |
| 145 | + $trigger_context = ''; |
| 146 | + } |
| 147 | + |
| 148 | + $extra_meta = isset( $input['alert_meta'] ) ? (array) $input['alert_meta'] : array(); |
| 149 | + $alert_meta = array_merge( |
| 150 | + $extra_meta, |
| 151 | + array( |
| 152 | + 'trigger_author' => $input['trigger_author'], |
| 153 | + 'trigger_connector' => $trigger_connector, |
| 154 | + 'trigger_context' => $trigger_context, |
| 155 | + 'trigger_action' => $input['trigger_action'], |
| 156 | + ) |
| 157 | + ); |
| 158 | + |
| 159 | + // Delegate the actual insert + meta save to Alert::save() so the |
| 160 | + // admin UI and the Abilities API stay on a single code path. |
| 161 | + $alert = new Alert( |
| 162 | + (object) array( |
| 163 | + 'alert_type' => $input['alert_type'], |
| 164 | + 'alert_meta' => $alert_meta, |
| 165 | + 'status' => $status, |
| 166 | + ), |
| 167 | + $this->plugin |
| 168 | + ); |
| 169 | + |
| 170 | + $post_id = $alert->save(); |
| 171 | + |
| 172 | + if ( is_wp_error( $post_id ) ) { |
| 173 | + return $post_id; |
| 174 | + } |
| 175 | + |
| 176 | + if ( empty( $post_id ) ) { |
| 177 | + return new \WP_Error( |
| 178 | + 'stream_alert_create_failed', |
| 179 | + __( 'Failed to create alert.', 'stream' ), |
| 180 | + array( 'status' => 500 ) |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + // Normalize alert_meta to stdClass when empty so the response satisfies |
| 185 | + // the declared object output schema. Reachable in theory if a future |
| 186 | + // Alert::save() change ever leaves alert_meta unwritten -- today the |
| 187 | + // call always persists the merged trigger keys, but the coerce is cheap |
| 188 | + // defense and keeps the JSON output consistent with get-alerts. |
| 189 | + $persisted_meta = get_post_meta( $post_id, 'alert_meta', true ); |
| 190 | + $alert_meta = is_array( $persisted_meta ) && ! empty( $persisted_meta ) |
| 191 | + ? $persisted_meta |
| 192 | + : new \stdClass(); |
| 193 | + |
| 194 | + return array( |
| 195 | + 'id' => (int) $post_id, |
| 196 | + 'status' => (string) get_post_status( $post_id ), |
| 197 | + 'title' => (string) get_the_title( $post_id ), |
| 198 | + 'alert_type' => get_post_meta( $post_id, 'alert_type', true ), |
| 199 | + 'alert_meta' => $alert_meta, |
| 200 | + ); |
| 201 | + } |
| 202 | +} |
0 commit comments