RegisteredWebhook.php
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models; |
| 4 | |
| 5 | use SureCart\Support\URL; |
| 6 | |
| 7 | /** |
| 8 | * Registered Webhook Model. |
| 9 | */ |
| 10 | class RegisteredWebhook extends Webhook { |
| 11 | /** |
| 12 | * Create the registered webhook. |
| 13 | * Creates a webhook for the current site and stores it in the WP options table. |
| 14 | * |
| 15 | * @param array $args Unused. |
| 16 | * |
| 17 | * @return $this|\WP_Error |
| 18 | */ |
| 19 | protected function create( $args = [] ) { |
| 20 | $webhook = parent::create( |
| 21 | [ |
| 22 | 'description' => 'WordPress Webhook SureCart', |
| 23 | 'enabled' => true, |
| 24 | 'destination' => 'wordpress', |
| 25 | 'url' => $this->getListenerUrl(), |
| 26 | 'webhook_events' => \SureCart::config()->webhook_events, |
| 27 | ] |
| 28 | ); |
| 29 | |
| 30 | // handle error. |
| 31 | if ( is_wp_error( $webhook ) ) { |
| 32 | return $webhook; |
| 33 | } |
| 34 | |
| 35 | // save with signing secret. |
| 36 | $this->registration()->save( $webhook ); |
| 37 | |
| 38 | // return this. |
| 39 | return $this; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get the registered webhook. |
| 44 | * |
| 45 | * @return \SureCart\Models\Webhook|\WP_Error; |
| 46 | */ |
| 47 | protected function get() { |
| 48 | $id = $this->registration()->get()['id'] ?? null; |
| 49 | if ( ! $id ) { |
| 50 | return false; |
| 51 | } |
| 52 | return parent::find( $id ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Alias for get. |
| 57 | * |
| 58 | * @param string $id Not used. |
| 59 | * |
| 60 | * @return \SureCart\Models\Webhook|\WP_Error; |
| 61 | */ |
| 62 | protected function find( $id = '' ) { |
| 63 | return $this->get(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Update the model. |
| 68 | * |
| 69 | * @param array $attributes Attributes to update. |
| 70 | * @return $this|false |
| 71 | */ |
| 72 | protected function update( $attributes = [] ) { |
| 73 | $id = $this->registration()->get()['id'] ?? null; |
| 74 | $webhook = parent::update( |
| 75 | array_merge( |
| 76 | [ |
| 77 | 'id' => $id, |
| 78 | 'url' => $this->getListenerUrl(), |
| 79 | 'webhook_events' => \SureCart::config()->webhook_events, |
| 80 | ], |
| 81 | $attributes |
| 82 | ) |
| 83 | ); |
| 84 | |
| 85 | if ( is_wp_error( $webhook ) ) { |
| 86 | return $webhook; |
| 87 | } |
| 88 | |
| 89 | $this->registration()->save( $webhook ); |
| 90 | |
| 91 | return $this; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Delete the model. |
| 96 | * |
| 97 | * @return $this|false |
| 98 | */ |
| 99 | protected function delete( $id = 0 ) { |
| 100 | $id = $this->registration()->get()['id'] ?? null; |
| 101 | |
| 102 | if ( ! $id ) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | // delete webhook. |
| 107 | $webhook = parent::delete( $id ); |
| 108 | |
| 109 | if ( is_wp_error( $webhook ) ) { |
| 110 | return $webhook; |
| 111 | } |
| 112 | |
| 113 | // delete registration. |
| 114 | $this->registration()->delete(); |
| 115 | |
| 116 | return $this; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Stores the registrationd webhook data in the WP options table. |
| 121 | * |
| 122 | * @return \SureCart\Models\WebhookRegistration; |
| 123 | */ |
| 124 | protected function registration() { |
| 125 | return new WebhookRegistration(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get the listener url. |
| 130 | * |
| 131 | * @return string |
| 132 | */ |
| 133 | protected function getListenerUrl(): string { |
| 134 | return get_home_url( null, '/surecart/webhooks', is_ssl() ? 'https' : 'http' ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Send test webhook. |
| 139 | * |
| 140 | * @return \SureCart\Models\Webhook |
| 141 | */ |
| 142 | protected function test() { |
| 143 | $id = $this->registration()->get()['id'] ?? null; |
| 144 | return $this->makeRequest( |
| 145 | [ |
| 146 | 'method' => 'POST', |
| 147 | 'query' => [], |
| 148 | ], |
| 149 | $this->endpoint . '/' . $id . '/test', |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Does the current domain match the registered webhook domain? |
| 155 | * |
| 156 | * @return boolean |
| 157 | */ |
| 158 | protected function currentDomainMatches() { |
| 159 | $webhook = $this->registration()->get(); |
| 160 | if ( empty( $webhook['url'] ) ) { |
| 161 | return false; |
| 162 | } |
| 163 | return untrailingslashit( URL::getSchemeAndHttpHost( $webhook['url'] ) ) === untrailingslashit( URL::getSchemeAndHttpHost( $this->getListenerUrl() ) ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Get the signing secret. |
| 168 | * |
| 169 | * Returns an empty string when no webhook is registered — avoids null |
| 170 | * dereference on PHP 8+ and ensures verification fails closed (CVE-2026-7655). |
| 171 | * |
| 172 | * @return string |
| 173 | */ |
| 174 | protected function getSigningSecret() { |
| 175 | $webhook = $this->registration()->get(); |
| 176 | return ! empty( $webhook->signing_secret ) ? (string) $webhook->signing_secret : ''; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Has the webhook a signing secret? |
| 181 | */ |
| 182 | protected function hasSigningSecret() { |
| 183 | return (bool) $this->getSigningSecret(); |
| 184 | } |
| 185 | } |
| 186 |