helper
1 year ago
importers
1 year ago
list-tables
1 year ago
marketplace-suggestions
1 year ago
meta-boxes
1 year ago
notes
1 year ago
plugin-updates
1 year ago
reports
1 year ago
settings
1 year ago
views
1 year ago
class-wc-admin-addons.php
1 year ago
class-wc-admin-api-keys-table-list.php
1 year ago
class-wc-admin-api-keys.php
1 year ago
class-wc-admin-assets.php
1 year ago
class-wc-admin-attributes.php
1 year ago
class-wc-admin-customize.php
1 year ago
class-wc-admin-dashboard-setup.php
1 year ago
class-wc-admin-dashboard.php
1 year ago
class-wc-admin-duplicate-product.php
1 year ago
class-wc-admin-exporters.php
1 year ago
class-wc-admin-help.php
1 year ago
class-wc-admin-importers.php
1 year ago
class-wc-admin-log-table-list.php
1 year ago
class-wc-admin-marketplace-promotions.php
1 year ago
class-wc-admin-menus.php
1 year ago
class-wc-admin-meta-boxes.php
1 year ago
class-wc-admin-notices.php
1 year ago
class-wc-admin-permalink-settings.php
1 year ago
class-wc-admin-pointers.php
1 year ago
class-wc-admin-post-types.php
1 year ago
class-wc-admin-profile.php
1 year ago
class-wc-admin-reports.php
1 year ago
class-wc-admin-settings.php
1 year ago
class-wc-admin-setup-wizard.php
1 year ago
class-wc-admin-status.php
1 year ago
class-wc-admin-taxonomies.php
1 year ago
class-wc-admin-upload-downloadable-product.php
1 year ago
class-wc-admin-webhooks-table-list.php
1 year ago
class-wc-admin-webhooks.php
1 year ago
class-wc-admin.php
1 year ago
wc-admin-functions.php
1 year ago
wc-meta-box-functions.php
1 year ago
class-wc-admin-webhooks.php
378 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Admin Webhooks Class |
| 4 | * |
| 5 | * @package WooCommerce\Admin |
| 6 | * @version 3.3.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * WC_Admin_Webhooks. |
| 13 | */ |
| 14 | class WC_Admin_Webhooks { |
| 15 | |
| 16 | /** |
| 17 | * Initialize the webhooks admin actions. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | add_action( 'admin_init', array( $this, 'actions' ) ); |
| 21 | add_action( 'woocommerce_settings_page_init', array( $this, 'screen_option' ) ); |
| 22 | add_filter( 'woocommerce_save_settings_advanced_webhooks', array( $this, 'allow_save_settings' ) ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Check if should allow save settings. |
| 27 | * This prevents "Your settings have been saved." notices on the table list. |
| 28 | * |
| 29 | * @param bool $allow If allow save settings. |
| 30 | * @return bool |
| 31 | */ |
| 32 | public function allow_save_settings( $allow ) { |
| 33 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 34 | if ( ! isset( $_GET['edit-webhook'] ) ) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | return $allow; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Check if is webhook settings page. |
| 43 | * |
| 44 | * @return bool |
| 45 | */ |
| 46 | private function is_webhook_settings_page() { |
| 47 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 48 | return isset( $_GET['page'], $_GET['tab'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 'advanced' === $_GET['tab'] && 'webhooks' === $_GET['section']; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Save method. |
| 53 | */ |
| 54 | private function save() { |
| 55 | check_admin_referer( 'woocommerce-settings' ); |
| 56 | |
| 57 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
| 58 | wp_die( esc_html__( 'You do not have permission to update Webhooks', 'woocommerce' ) ); |
| 59 | } |
| 60 | |
| 61 | $errors = array(); |
| 62 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 63 | $webhook_id = isset( $_POST['webhook_id'] ) ? absint( $_POST['webhook_id'] ) : 0; |
| 64 | $webhook = new WC_Webhook( $webhook_id ); |
| 65 | |
| 66 | // Name. |
| 67 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 68 | if ( ! empty( $_POST['webhook_name'] ) ) { |
| 69 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 70 | $name = sanitize_text_field( wp_unslash( $_POST['webhook_name'] ) ); |
| 71 | } else { |
| 72 | $name = sprintf( |
| 73 | /* translators: %s: date */ |
| 74 | __( 'Webhook created on %s', 'woocommerce' ), |
| 75 | // @codingStandardsIgnoreStart |
| 76 | (new DateTime('now'))->format( _x( 'M d, Y @ h:i A', 'Webhook created on date parsed by DateTime::format', 'woocommerce' ) ) |
| 77 | // @codingStandardsIgnoreEnd |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | $webhook->set_name( $name ); |
| 82 | |
| 83 | if ( ! $webhook->get_user_id() ) { |
| 84 | $webhook->set_user_id( get_current_user_id() ); |
| 85 | } |
| 86 | |
| 87 | // Status. |
| 88 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 89 | $webhook->set_status( ! empty( $_POST['webhook_status'] ) ? sanitize_text_field( wp_unslash( $_POST['webhook_status'] ) ) : 'disabled' ); |
| 90 | |
| 91 | // Delivery URL. |
| 92 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 93 | $delivery_url = ! empty( $_POST['webhook_delivery_url'] ) ? esc_url_raw( wp_unslash( $_POST['webhook_delivery_url'] ) ) : ''; |
| 94 | |
| 95 | if ( wc_is_valid_url( $delivery_url ) ) { |
| 96 | $webhook->set_delivery_url( $delivery_url ); |
| 97 | } |
| 98 | |
| 99 | // Secret. |
| 100 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 101 | $secret = ! empty( $_POST['webhook_secret'] ) ? sanitize_text_field( wp_unslash( $_POST['webhook_secret'] ) ) : wp_generate_password( 50, true, true ); |
| 102 | $webhook->set_secret( $secret ); |
| 103 | |
| 104 | // Topic. |
| 105 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 106 | if ( ! empty( $_POST['webhook_topic'] ) ) { |
| 107 | $resource = ''; |
| 108 | $event = ''; |
| 109 | |
| 110 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 111 | switch ( $_POST['webhook_topic'] ) { |
| 112 | case 'action': |
| 113 | $resource = 'action'; |
| 114 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 115 | $event = ! empty( $_POST['webhook_action_event'] ) ? sanitize_text_field( wp_unslash( $_POST['webhook_action_event'] ) ) : ''; |
| 116 | break; |
| 117 | |
| 118 | default: |
| 119 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 120 | list( $resource, $event ) = explode( '.', sanitize_text_field( wp_unslash( $_POST['webhook_topic'] ) ) ); |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | $topic = $resource . '.' . $event; |
| 125 | |
| 126 | if ( wc_is_webhook_valid_topic( $topic ) ) { |
| 127 | $webhook->set_topic( $topic ); |
| 128 | } else { |
| 129 | $errors[] = __( 'Webhook topic unknown. Please select a valid topic.', 'woocommerce' ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // API version. |
| 134 | $rest_api_versions = wc_get_webhook_rest_api_versions(); |
| 135 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 136 | $webhook->set_api_version( ! empty( $_POST['webhook_api_version'] ) ? sanitize_text_field( wp_unslash( $_POST['webhook_api_version'] ) ) : end( $rest_api_versions ) ); |
| 137 | |
| 138 | $webhook->save(); |
| 139 | |
| 140 | // Run actions. |
| 141 | do_action( 'woocommerce_webhook_options_save', $webhook->get_id() ); |
| 142 | if ( $errors ) { |
| 143 | // Redirect to webhook edit page to avoid settings save actions. |
| 144 | wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=advanced§ion=webhooks&edit-webhook=' . $webhook->get_id() . '&error=' . rawurlencode( implode( '|', $errors ) ) ) ); |
| 145 | exit(); |
| 146 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 147 | } elseif ( isset( $_POST['webhook_status'] ) && 'active' === $_POST['webhook_status'] && $webhook->get_pending_delivery() ) { |
| 148 | // Ping the webhook at the first time that is activated. |
| 149 | $result = $webhook->deliver_ping(); |
| 150 | |
| 151 | if ( is_wp_error( $result ) ) { |
| 152 | // Redirect to webhook edit page to avoid settings save actions. |
| 153 | wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=advanced§ion=webhooks&edit-webhook=' . $webhook->get_id() . '&error=' . rawurlencode( $result->get_error_message() ) ) ); |
| 154 | exit(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Redirect to webhook edit page to avoid settings save actions. |
| 159 | wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=advanced§ion=webhooks&edit-webhook=' . $webhook->get_id() . '&updated=1' ) ); |
| 160 | exit(); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Bulk delete. |
| 165 | * |
| 166 | * @param array $webhooks List of webhooks IDs. |
| 167 | */ |
| 168 | public static function bulk_delete( $webhooks ) { |
| 169 | foreach ( $webhooks as $webhook_id ) { |
| 170 | $webhook = new WC_Webhook( (int) $webhook_id ); |
| 171 | $webhook->delete( true ); |
| 172 | } |
| 173 | |
| 174 | $qty = count( $webhooks ); |
| 175 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 176 | $status = isset( $_GET['status'] ) ? '&status=' . sanitize_text_field( wp_unslash( $_GET['status'] ) ) : ''; |
| 177 | |
| 178 | // Redirect to webhooks page. |
| 179 | wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=advanced§ion=webhooks' . $status . '&deleted=' . $qty ) ); |
| 180 | exit(); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Delete webhook. |
| 185 | */ |
| 186 | private function delete() { |
| 187 | check_admin_referer( 'delete-webhook' ); |
| 188 | |
| 189 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 190 | if ( isset( $_GET['delete'] ) ) { |
| 191 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 192 | $webhook_id = absint( $_GET['delete'] ); |
| 193 | |
| 194 | if ( $webhook_id ) { |
| 195 | self::bulk_delete( array( $webhook_id ) ); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Webhooks admin actions. |
| 202 | */ |
| 203 | public function actions() { |
| 204 | if ( $this->is_webhook_settings_page() ) { |
| 205 | // Save. |
| 206 | // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 207 | if ( isset( $_POST['save'] ) && isset( $_POST['webhook_id'] ) ) { |
| 208 | $this->save(); |
| 209 | } |
| 210 | |
| 211 | // Delete webhook. |
| 212 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 213 | if ( isset( $_GET['delete'] ) ) { |
| 214 | $this->delete(); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Page output. |
| 221 | */ |
| 222 | public static function page_output() { |
| 223 | // Hide the save button. |
| 224 | $GLOBALS['hide_save_button'] = true; |
| 225 | |
| 226 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 227 | if ( isset( $_GET['edit-webhook'] ) ) { |
| 228 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 229 | $webhook_id = absint( $_GET['edit-webhook'] ); |
| 230 | $webhook = new WC_Webhook( $webhook_id ); |
| 231 | |
| 232 | include __DIR__ . '/settings/views/html-webhooks-edit.php'; |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | self::table_list_output(); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Notices. |
| 241 | */ |
| 242 | public static function notices() { |
| 243 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 244 | if ( isset( $_GET['deleted'] ) ) { |
| 245 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 246 | $deleted = absint( $_GET['deleted'] ); |
| 247 | |
| 248 | /* translators: %d: count */ |
| 249 | WC_Admin_Settings::add_message( sprintf( _n( '%d webhook permanently deleted.', '%d webhooks permanently deleted.', $deleted, 'woocommerce' ), $deleted ) ); |
| 250 | } |
| 251 | |
| 252 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 253 | if ( isset( $_GET['updated'] ) ) { |
| 254 | WC_Admin_Settings::add_message( __( 'Webhook updated successfully.', 'woocommerce' ) ); |
| 255 | } |
| 256 | |
| 257 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 258 | if ( isset( $_GET['created'] ) ) { |
| 259 | WC_Admin_Settings::add_message( __( 'Webhook created successfully.', 'woocommerce' ) ); |
| 260 | } |
| 261 | |
| 262 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 263 | if ( isset( $_GET['error'] ) ) { |
| 264 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 265 | foreach ( explode( '|', sanitize_text_field( wp_unslash( $_GET['error'] ) ) ) as $message ) { |
| 266 | WC_Admin_Settings::add_error( trim( $message ) ); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Add screen option. |
| 273 | */ |
| 274 | public function screen_option() { |
| 275 | global $webhooks_table_list; |
| 276 | |
| 277 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 278 | if ( ! isset( $_GET['edit-webhook'] ) && $this->is_webhook_settings_page() ) { |
| 279 | $webhooks_table_list = new WC_Admin_Webhooks_Table_List(); |
| 280 | |
| 281 | // Add screen option. |
| 282 | add_screen_option( |
| 283 | 'per_page', |
| 284 | array( |
| 285 | 'default' => 10, |
| 286 | 'option' => 'woocommerce_webhooks_per_page', |
| 287 | ) |
| 288 | ); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Table list output. |
| 294 | */ |
| 295 | private static function table_list_output() { |
| 296 | global $webhooks_table_list; |
| 297 | |
| 298 | echo '<h2 class="wc-table-list-header">' . esc_html__( 'Webhooks', 'woocommerce' ) . ' <a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=advanced§ion=webhooks&edit-webhook=0' ) ) . '" class="page-title-action">' . esc_html__( 'Add webhook', 'woocommerce' ) . '</a></h2>'; |
| 299 | |
| 300 | // Get the webhooks count. |
| 301 | $data_store = WC_Data_Store::load( 'webhook' ); |
| 302 | $num_webhooks = $data_store->get_count_webhooks_by_status(); |
| 303 | $count = array_sum( $num_webhooks ); |
| 304 | |
| 305 | if ( 0 < $count ) { |
| 306 | $webhooks_table_list->process_bulk_action(); |
| 307 | $webhooks_table_list->prepare_items(); |
| 308 | |
| 309 | echo '<input type="hidden" name="page" value="wc-settings" />'; |
| 310 | echo '<input type="hidden" name="tab" value="advanced" />'; |
| 311 | echo '<input type="hidden" name="section" value="webhooks" />'; |
| 312 | |
| 313 | $webhooks_table_list->views(); |
| 314 | $webhooks_table_list->search_box( __( 'Search webhooks', 'woocommerce' ), 'webhook' ); |
| 315 | $webhooks_table_list->display(); |
| 316 | } else { |
| 317 | echo '<div class="woocommerce-BlankState woocommerce-BlankState--webhooks">'; |
| 318 | ?> |
| 319 | <h2 class="woocommerce-BlankState-message"><?php esc_html_e( 'Webhooks are event notifications sent to URLs of your choice. They can be used to integrate with third-party services which support them.', 'woocommerce' ); ?></h2> |
| 320 | <a class="woocommerce-BlankState-cta button-primary button" href="<?php echo esc_url( admin_url( 'admin.php?page=wc-settings&tab=advanced§ion=webhooks&edit-webhook=0' ) ); ?>"><?php esc_html_e( 'Create a new webhook', 'woocommerce' ); ?></a> |
| 321 | <style type="text/css">#posts-filter .wp-list-table, #posts-filter .tablenav.top, .tablenav.bottom .actions { display: none; }</style> |
| 322 | <?php |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Logs output. |
| 328 | * |
| 329 | * @deprecated 3.3.0 |
| 330 | * @param WC_Webhook $webhook Deprecated. |
| 331 | */ |
| 332 | public static function logs_output( $webhook = 'deprecated' ) { |
| 333 | wc_deprecated_function( 'WC_Admin_Webhooks::logs_output', '3.3' ); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Get the webhook topic data. |
| 338 | * |
| 339 | * @param WC_Webhook $webhook Webhook instance. |
| 340 | * |
| 341 | * @return array |
| 342 | */ |
| 343 | public static function get_topic_data( $webhook ) { |
| 344 | $topic = $webhook->get_topic(); |
| 345 | $event = ''; |
| 346 | $resource = ''; |
| 347 | |
| 348 | if ( $topic ) { |
| 349 | list( $resource, $event ) = explode( '.', $topic ); |
| 350 | |
| 351 | if ( 'action' === $resource ) { |
| 352 | $topic = 'action'; |
| 353 | } elseif ( ! in_array( $resource, array( 'coupon', 'customer', 'order', 'product' ), true ) ) { |
| 354 | $topic = 'custom'; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return array( |
| 359 | 'topic' => $topic, |
| 360 | 'event' => $event, |
| 361 | 'resource' => $resource, |
| 362 | ); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Get the logs navigation. |
| 367 | * |
| 368 | * @deprecated 3.3.0 |
| 369 | * @param int $total Deprecated. |
| 370 | * @param WC_Webhook $webhook Deprecated. |
| 371 | */ |
| 372 | public static function get_logs_navigation( $total, $webhook ) { |
| 373 | wc_deprecated_function( 'WC_Admin_Webhooks::get_logs_navigation', '3.3' ); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | new WC_Admin_Webhooks(); |
| 378 |