WC_Admin_Webhooks

WC_Admin_Webhooks.

Defined (1)

The class is defined in the following location(s).

/includes/admin/class-wc-admin-webhooks.php  
  1. class WC_Admin_Webhooks { 
  2.  
  3. /** 
  4. * Initialize the webhooks admin actions. 
  5. */ 
  6. public function __construct() { 
  7. add_action( 'admin_init', array( $this, 'actions' ) ); 
  8.  
  9. /** 
  10. * Check if is webhook settings page. 
  11. * @return bool 
  12. */ 
  13. private function is_webhook_settings_page() { 
  14. return isset( $_GET['page'] ) 
  15. && 'wc-settings' == $_GET['page'] 
  16. && isset( $_GET['tab'] ) 
  17. && 'api' == $_GET['tab'] 
  18. && isset( $_GET['section'] ) 
  19. && 'webhooks' == isset( $_GET['section'] ); 
  20.  
  21. /** 
  22. * Updated the Webhook name. 
  23. * @param int $webhook_id 
  24. */ 
  25. private function update_name( $webhook_id ) { 
  26. global $wpdb; 
  27.  
  28. $name = ! empty( $_POST['webhook_name'] ) ? $_POST['webhook_name'] : sprintf( __( 'Webhook created on %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce' ) ) ); 
  29. $wpdb->update( $wpdb->posts, array( 'post_title' => $name ), array( 'ID' => $webhook_id ) ); 
  30.  
  31. /** 
  32. * Updated the Webhook status. 
  33. * @param WC_Webhook $webhook 
  34. */ 
  35. private function update_status( $webhook ) { 
  36. $status = ! empty( $_POST['webhook_status'] ) ? wc_clean( $_POST['webhook_status'] ) : ''; 
  37.  
  38. $webhook->update_status( $status ); 
  39.  
  40. /** 
  41. * Updated the Webhook delivery URL. 
  42. * @param WC_Webhook $webhook 
  43. */ 
  44. private function update_delivery_url( $webhook ) { 
  45. $delivery_url = ! empty( $_POST['webhook_delivery_url'] ) ? $_POST['webhook_delivery_url'] : ''; 
  46.  
  47. if ( wc_is_valid_url( $delivery_url ) ) { 
  48. $webhook->set_delivery_url( $delivery_url ); 
  49.  
  50. /** 
  51. * Updated the Webhook secret. 
  52. * @param WC_Webhook $webhook 
  53. */ 
  54. private function update_secret( $webhook ) { 
  55. $secret = ! empty( $_POST['webhook_secret'] ) ? $_POST['webhook_secret'] : wp_generate_password( 50, true, true ); 
  56.  
  57. $webhook->set_secret( $secret ); 
  58.  
  59. /** 
  60. * Updated the Webhook topic. 
  61. * @param WC_Webhook $webhook 
  62. */ 
  63. private function update_topic( $webhook ) { 
  64. if ( ! empty( $_POST['webhook_topic'] ) ) { 
  65.  
  66. $resource = ''; 
  67. $event = ''; 
  68.  
  69. switch ( $_POST['webhook_topic'] ) { 
  70. case 'custom' : 
  71. if ( ! empty( $_POST['webhook_custom_topic'] ) ) { 
  72. list( $resource, $event ) = explode( '.', wc_clean( $_POST['webhook_custom_topic'] ) ); 
  73. break; 
  74. case 'action' : 
  75. $resource = 'action'; 
  76. $event = ! empty( $_POST['webhook_action_event'] ) ? wc_clean( $_POST['webhook_action_event'] ) : ''; 
  77. break; 
  78.  
  79. default : 
  80. list( $resource, $event ) = explode( '.', wc_clean( $_POST['webhook_topic'] ) ); 
  81. break; 
  82.  
  83. $topic = $resource . '.' . $event; 
  84.  
  85. if ( wc_is_webhook_valid_topic( $topic ) ) { 
  86. $webhook->set_topic( $topic ); 
  87.  
  88. /** 
  89. * Save method. 
  90. */ 
  91. private function save() { 
  92. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) { 
  93. wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); 
  94.  
  95. $webhook_id = absint( $_POST['webhook_id'] ); 
  96.  
  97. if ( ! current_user_can( 'edit_shop_webhook', $webhook_id ) ) { 
  98. return; 
  99.  
  100. $webhook = new WC_Webhook( $webhook_id ); 
  101.  
  102. // Name 
  103. $this->update_name( $webhook->id ); 
  104.  
  105. // Status 
  106. $this->update_status( $webhook ); 
  107.  
  108. // Delivery URL 
  109. $this->update_delivery_url( $webhook ); 
  110.  
  111. // Secret 
  112. $this->update_secret( $webhook ); 
  113.  
  114. // Topic 
  115. $this->update_topic( $webhook ); 
  116.  
  117. // Update date. 
  118. wp_update_post( array( 'ID' => $webhook->id, 'post_modified' => current_time( 'mysql' ) ) ); 
  119.  
  120. // Run actions 
  121. do_action( 'woocommerce_webhook_options_save', $webhook->id ); 
  122.  
  123. delete_transient( 'woocommerce_webhook_ids' ); 
  124.  
  125. // Ping the webhook at the first time that is activated 
  126. $pending_delivery = get_post_meta( $webhook->id, '_webhook_pending_delivery', true ); 
  127.  
  128. if ( isset( $_POST['webhook_status'] ) && 'active' === $_POST['webhook_status'] && $pending_delivery ) { 
  129. $result = $webhook->deliver_ping(); 
  130.  
  131. if ( is_wp_error( $result ) ) { 
  132. // Redirect to webhook edit page to avoid settings save actions 
  133. wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=api§ion=webhooks&edit-webhook=' . $webhook->id . '&error=' . urlencode( $result->get_error_message() ) ) ); 
  134. exit(); 
  135.  
  136. // Redirect to webhook edit page to avoid settings save actions 
  137. wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=api§ion=webhooks&edit-webhook=' . $webhook->id . '&updated=1' ) ); 
  138. exit(); 
  139.  
  140. /** 
  141. * Create Webhook. 
  142. */ 
  143. private function create() { 
  144. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'create-webhook' ) ) { 
  145. wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); 
  146.  
  147. if ( ! current_user_can( 'publish_shop_webhooks' ) ) { 
  148. wp_die( __( 'You don\'t have permissions to create Webhooks!', 'woocommerce' ) ); 
  149.  
  150. $webhook_id = wp_insert_post( array( 
  151. 'post_type' => 'shop_webhook',  
  152. 'post_status' => 'pending',  
  153. 'ping_status' => 'closed',  
  154. 'post_author' => get_current_user_id(),  
  155. 'post_password' => strlen( ( $password = uniqid( 'webhook_' ) ) ) > 20 ? substr( $password, 0, 20 ) : $password,  
  156. 'post_title' => sprintf( __( 'Webhook created on %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce' ) ) ),  
  157. 'comment_status' => 'open' 
  158. ) ); 
  159.  
  160. if ( is_wp_error( $webhook_id ) ) { 
  161. wp_die( $webhook_id->get_error_messages() ); 
  162.  
  163. update_post_meta( $webhook_id, '_webhook_pending_delivery', true ); 
  164.  
  165. delete_transient( 'woocommerce_webhook_ids' ); 
  166.  
  167. // Redirect to edit page 
  168. wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=api§ion=webhooks&edit-webhook=' . $webhook_id . '&created=1' ) ); 
  169. exit(); 
  170.  
  171. /** 
  172. * Bulk trash/delete. 
  173. * @param array $webhooks 
  174. * @param bool $delete 
  175. */ 
  176. private function bulk_trash( $webhooks, $delete = false ) { 
  177. foreach ( $webhooks as $webhook_id ) { 
  178. if ( $delete ) { 
  179. wp_delete_post( $webhook_id, true ); 
  180. } else { 
  181. wp_trash_post( $webhook_id ); 
  182.  
  183. $type = ! EMPTY_TRASH_DAYS || $delete ? 'deleted' : 'trashed'; 
  184. $qty = count( $webhooks ); 
  185. $status = isset( $_GET['status'] ) ? '&status=' . sanitize_text_field( $_GET['status'] ) : ''; 
  186.  
  187. delete_transient( 'woocommerce_webhook_ids' ); 
  188.  
  189. // Redirect to webhooks page 
  190. wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=api§ion=webhooks' . $status . '&' . $type . '=' . $qty ) ); 
  191. exit(); 
  192.  
  193. /** 
  194. * Bulk untrash. 
  195. * @param array $webhooks 
  196. */ 
  197. private function bulk_untrash( $webhooks ) { 
  198. foreach ( $webhooks as $webhook_id ) { 
  199. wp_untrash_post( $webhook_id ); 
  200.  
  201. $qty = count( $webhooks ); 
  202.  
  203. delete_transient( 'woocommerce_webhook_ids' ); 
  204.  
  205. // Redirect to webhooks page 
  206. wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=api§ion=webhooks&status=trash&untrashed=' . $qty ) ); 
  207. exit(); 
  208.  
  209. /** 
  210. * Bulk actions. 
  211. */ 
  212. private function bulk_actions() { 
  213. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) { 
  214. wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); 
  215.  
  216. if ( ! current_user_can( 'edit_shop_webhooks' ) ) { 
  217. wp_die( __( 'You don\'t have permissions to edit Webhooks!', 'woocommerce' ) ); 
  218.  
  219. $webhooks = array_map( 'absint', (array) $_GET['webhook'] ); 
  220.  
  221. switch ( $_GET['action'] ) { 
  222. case 'trash' : 
  223. $this->bulk_trash( $webhooks ); 
  224. break; 
  225. case 'untrash' : 
  226. $this->bulk_untrash( $webhooks ); 
  227. break; 
  228. case 'delete' : 
  229. $this->bulk_trash( $webhooks, true ); 
  230. break; 
  231. default : 
  232. break; 
  233.  
  234. /** 
  235. * Empty Trash. 
  236. */ 
  237. private function empty_trash() { 
  238. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'empty_trash' ) ) { 
  239. wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); 
  240.  
  241. if ( ! current_user_can( 'delete_shop_webhooks' ) ) { 
  242. wp_die( __( 'You don\'t have permissions to delete Webhooks!', 'woocommerce' ) ); 
  243.  
  244. $webhooks = get_posts( array( 
  245. 'post_type' => 'shop_webhook',  
  246. 'ignore_sticky_posts' => true,  
  247. 'nopaging' => true,  
  248. 'post_status' => 'trash',  
  249. 'fields' => 'ids' 
  250. ) ); 
  251.  
  252. foreach ( $webhooks as $webhook_id ) { 
  253. wp_delete_post( $webhook_id, true ); 
  254.  
  255. $qty = count( $webhooks ); 
  256.  
  257. // Redirect to webhooks page 
  258. wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=api§ion=webhooks&deleted=' . $qty ) ); 
  259. exit(); 
  260.  
  261. /** 
  262. * Webhooks admin actions. 
  263. */ 
  264. public function actions() { 
  265. if ( $this->is_webhook_settings_page() ) { 
  266. // Save 
  267. if ( isset( $_POST['save'] ) && isset( $_POST['webhook_id'] ) ) { 
  268. $this->save(); 
  269.  
  270. // Create 
  271. if ( isset( $_GET['create-webhook'] ) ) { 
  272. $this->create(); 
  273.  
  274. // Bulk actions 
  275. if ( isset( $_GET['action'] ) && isset( $_GET['webhook'] ) ) { 
  276. $this->bulk_actions(); 
  277.  
  278. // Empty trash 
  279. if ( isset( $_GET['empty_trash'] ) ) { 
  280. $this->empty_trash(); 
  281.  
  282. /** 
  283. * Page output. 
  284. */ 
  285. public static function page_output() { 
  286. // Hide the save button 
  287. $GLOBALS['hide_save_button'] = true; 
  288.  
  289. if ( isset( $_GET['edit-webhook'] ) ) { 
  290. $webhook_id = absint( $_GET['edit-webhook'] ); 
  291. $webhook = new WC_Webhook( $webhook_id ); 
  292.  
  293. if ( 'trash' != $webhook->post_data->post_status ) { 
  294. include( 'settings/views/html-webhooks-edit.php' ); 
  295. return; 
  296.  
  297. self::table_list_output(); 
  298.  
  299. /** 
  300. * Notices. 
  301. */ 
  302. public static function notices() { 
  303. if ( isset( $_GET['trashed'] ) ) { 
  304. $trashed = absint( $_GET['trashed'] ); 
  305.  
  306. WC_Admin_Settings::add_message( sprintf( _n( '1 webhook moved to the Trash.', '%d webhooks moved to the Trash.', $trashed, 'woocommerce' ), $trashed ) ); 
  307.  
  308. if ( isset( $_GET['untrashed'] ) ) { 
  309. $untrashed = absint( $_GET['untrashed'] ); 
  310.  
  311. WC_Admin_Settings::add_message( sprintf( _n( '1 webhook restored from the Trash.', '%d webhooks restored from the Trash.', $untrashed, 'woocommerce' ), $untrashed ) ); 
  312.  
  313. if ( isset( $_GET['deleted'] ) ) { 
  314. $deleted = absint( $_GET['deleted'] ); 
  315.  
  316. WC_Admin_Settings::add_message( sprintf( _n( '1 webhook permanently deleted.', '%d webhooks permanently deleted.', $deleted, 'woocommerce' ), $deleted ) ); 
  317.  
  318. if ( isset( $_GET['updated'] ) ) { 
  319. WC_Admin_Settings::add_message( __( 'Webhook updated successfully.', 'woocommerce' ) ); 
  320.  
  321. if ( isset( $_GET['created'] ) ) { 
  322. WC_Admin_Settings::add_message( __( 'Webhook created successfully.', 'woocommerce' ) ); 
  323.  
  324. if ( isset( $_GET['error'] ) ) { 
  325. WC_Admin_Settings::add_error( wc_clean( $_GET['error'] ) ); 
  326.  
  327. /** 
  328. * Table list output. 
  329. */ 
  330. private static function table_list_output() { 
  331. echo '<h2>' . __( 'Webhooks', 'woocommerce' ) . ' <a href="' . esc_url( wp_nonce_url( admin_url( 'admin.php?page=wc-settings&tab=api§ion=webhooks&create-webhook=1' ), 'create-webhook' ) ) . '" class="add-new-h2">' . __( 'Add Webhook', 'woocommerce' ) . '</a></h2>'; 
  332.  
  333. $webhooks_table_list = new WC_Admin_Webhooks_Table_List(); 
  334. $webhooks_table_list->prepare_items(); 
  335.  
  336. echo '<input type="hidden" name="page" value="wc-settings" />'; 
  337. echo '<input type="hidden" name="tab" value="api" />'; 
  338. echo '<input type="hidden" name="section" value="webhooks" />'; 
  339.  
  340. $webhooks_table_list->views(); 
  341. $webhooks_table_list->search_box( __( 'Search Webhooks', 'woocommerce' ), 'webhook' ); 
  342. $webhooks_table_list->display(); 
  343.  
  344. /** 
  345. * Logs output. 
  346. * @param WC_Webhook $webhook 
  347. */ 
  348. public static function logs_output( $webhook ) { 
  349. $current = isset( $_GET['log_page'] ) ? absint( $_GET['log_page'] ) : 1; 
  350. $args = array( 
  351. 'post_id' => $webhook->id,  
  352. 'status' => 'approve',  
  353. 'type' => 'webhook_delivery',  
  354. 'number' => 10 
  355. ); 
  356.  
  357. if ( 1 < $current ) { 
  358. $args['offset'] = ( $current - 1 ) * 10; 
  359.  
  360. remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_webhook_comments' ), 10, 1 ); 
  361.  
  362. $logs = get_comments( $args ); 
  363.  
  364. add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_webhook_comments' ), 10, 1 ); 
  365.  
  366. if ( $logs ) { 
  367. include_once( 'settings/views/html-webhook-logs.php' ); 
  368. } else { 
  369. echo '<p>' . __( 'This Webhook has no log yet.', 'woocommerce' ) . '</p>'; 
  370.  
  371. /** 
  372. * Get the webhook topic data. 
  373. * @return array 
  374. */ 
  375. public static function get_topic_data( $webhook ) { 
  376. $topic = $webhook->get_topic(); 
  377. $event = ''; 
  378. $resource = ''; 
  379.  
  380. if ( $topic ) { 
  381. list( $resource, $event ) = explode( '.', $topic ); 
  382.  
  383. if ( 'action' === $resource ) { 
  384. $topic = 'action'; 
  385. } else if ( ! in_array( $resource, array( 'coupon', 'customer', 'order', 'product' ) ) ) { 
  386. $topic = 'custom'; 
  387.  
  388. return array( 
  389. 'topic' => $topic,  
  390. 'event' => $event,  
  391. 'resource' => $resource 
  392. ); 
  393.  
  394. /** 
  395. * Get the logs navigation. 
  396. * @param int $total 
  397. * @return string 
  398. */ 
  399. public static function get_logs_navigation( $total, $webhook ) { 
  400. $pages = ceil( $total / 10 ); 
  401. $current = isset( $_GET['log_page'] ) ? absint( $_GET['log_page'] ) : 1; 
  402.  
  403. $html = '<div class="webhook-logs-navigation">'; 
  404.  
  405. $html .= '<p class="info" style="float: left;"><strong>'; 
  406. $html .= sprintf( '%s – Page %d of %d', _n( '1 item', sprintf( '%d items', $total ), $total, 'woocommerce' ), $current, $pages ); 
  407. $html .= '</strong></p>'; 
  408.  
  409. if ( 1 < $pages ) { 
  410. $html .= '<p class="tools" style="float: right;">'; 
  411. if ( 1 == $current ) { 
  412. $html .= '<button class="button-primary" disabled="disabled">' . __( '‹ Previous', 'woocommerce' ) . '</button> '; 
  413. } else { 
  414. $html .= '<a class="button-primary" href="' . admin_url( 'admin.php?page=wc-settings&tab=api§ion=webhooks&edit-webhook=' . $webhook->id . '&log_page=' . ( $current - 1 ) ) . '#webhook-logs">' . __( '‹ Previous', 'woocommerce' ) . '</a> '; 
  415.  
  416. if ( $pages == $current ) { 
  417. $html .= '<button class="button-primary" disabled="disabled">' . __( 'Next ›', 'woocommerce' ) . '</button>'; 
  418. } else { 
  419. $html .= '<a class="button-primary" href="' . admin_url( 'admin.php?page=wc-settings&tab=api§ion=webhooks&edit-webhook=' . $webhook->id . '&log_page=' . ( $current + 1 ) ) . '#webhook-logs">' . __( 'Next ›', 'woocommerce' ) . '</a>'; 
  420. $html .= '</p>'; 
  421.  
  422. $html .= '<div class="clear"></div></div>'; 
  423.  
  424. return $html;