PageRenderTime 34ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-webhooks.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 349 lines | 199 code | 59 blank | 91 comment | 35 complexity | 30f02c3c8ffba9ddca860eaec59a6085 MD5 | raw file
  1. <?php
  2. /**
  3. * WooCommerce Admin Webhooks Class
  4. *
  5. * @package WooCommerce\Admin
  6. * @version 3.3.0
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * WC_Admin_Webhooks.
  11. */
  12. class WC_Admin_Webhooks {
  13. /**
  14. * Initialize the webhooks admin actions.
  15. */
  16. public function __construct() {
  17. add_action( 'admin_init', array( $this, 'actions' ) );
  18. add_action( 'woocommerce_settings_page_init', array( $this, 'screen_option' ) );
  19. add_filter( 'woocommerce_save_settings_advanced_webhooks', array( $this, 'allow_save_settings' ) );
  20. }
  21. /**
  22. * Check if should allow save settings.
  23. * This prevents "Your settings have been saved." notices on the table list.
  24. *
  25. * @param bool $allow If allow save settings.
  26. * @return bool
  27. */
  28. public function allow_save_settings( $allow ) {
  29. if ( ! isset( $_GET['edit-webhook'] ) ) { // WPCS: input var okay, CSRF ok.
  30. return false;
  31. }
  32. return $allow;
  33. }
  34. /**
  35. * Check if is webhook settings page.
  36. *
  37. * @return bool
  38. */
  39. private function is_webhook_settings_page() {
  40. return isset( $_GET['page'], $_GET['tab'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 'advanced' === $_GET['tab'] && 'webhooks' === $_GET['section']; // WPCS: input var okay, CSRF ok.
  41. }
  42. /**
  43. * Save method.
  44. */
  45. private function save() {
  46. check_admin_referer( 'woocommerce-settings' );
  47. if ( ! current_user_can( 'manage_woocommerce' ) ) {
  48. wp_die( esc_html__( 'You do not have permission to update Webhooks', 'woocommerce' ) );
  49. }
  50. $errors = array();
  51. $webhook_id = isset( $_POST['webhook_id'] ) ? absint( $_POST['webhook_id'] ) : 0; // WPCS: input var okay, CSRF ok.
  52. $webhook = new WC_Webhook( $webhook_id );
  53. // Name.
  54. if ( ! empty( $_POST['webhook_name'] ) ) { // WPCS: input var okay, CSRF ok.
  55. $name = sanitize_text_field( wp_unslash( $_POST['webhook_name'] ) ); // WPCS: input var okay, CSRF ok.
  56. } else {
  57. $name = sprintf(
  58. /* translators: %s: date */
  59. __( 'Webhook created on %s', 'woocommerce' ),
  60. // @codingStandardsIgnoreStart
  61. strftime( _x( '%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce' ) )
  62. // @codingStandardsIgnoreEnd
  63. );
  64. }
  65. $webhook->set_name( $name );
  66. if ( ! $webhook->get_user_id() ) {
  67. $webhook->set_user_id( get_current_user_id() );
  68. }
  69. // Status.
  70. $webhook->set_status( ! empty( $_POST['webhook_status'] ) ? sanitize_text_field( wp_unslash( $_POST['webhook_status'] ) ) : 'disabled' ); // WPCS: input var okay, CSRF ok.
  71. // Delivery URL.
  72. $delivery_url = ! empty( $_POST['webhook_delivery_url'] ) ? esc_url_raw( wp_unslash( $_POST['webhook_delivery_url'] ) ) : ''; // WPCS: input var okay, CSRF ok.
  73. if ( wc_is_valid_url( $delivery_url ) ) {
  74. $webhook->set_delivery_url( $delivery_url );
  75. }
  76. // Secret.
  77. $secret = ! empty( $_POST['webhook_secret'] ) ? sanitize_text_field( wp_unslash( $_POST['webhook_secret'] ) ) : wp_generate_password( 50, true, true ); // WPCS: input var okay, CSRF ok.
  78. $webhook->set_secret( $secret );
  79. // Topic.
  80. if ( ! empty( $_POST['webhook_topic'] ) ) { // WPCS: input var okay, CSRF ok.
  81. $resource = '';
  82. $event = '';
  83. switch ( $_POST['webhook_topic'] ) { // WPCS: input var okay, CSRF ok.
  84. case 'action':
  85. $resource = 'action';
  86. $event = ! empty( $_POST['webhook_action_event'] ) ? sanitize_text_field( wp_unslash( $_POST['webhook_action_event'] ) ) : ''; // WPCS: input var okay, CSRF ok.
  87. break;
  88. default:
  89. list( $resource, $event ) = explode( '.', sanitize_text_field( wp_unslash( $_POST['webhook_topic'] ) ) ); // WPCS: input var okay, CSRF ok.
  90. break;
  91. }
  92. $topic = $resource . '.' . $event;
  93. if ( wc_is_webhook_valid_topic( $topic ) ) {
  94. $webhook->set_topic( $topic );
  95. } else {
  96. $errors[] = __( 'Webhook topic unknown. Please select a valid topic.', 'woocommerce' );
  97. }
  98. }
  99. // API version.
  100. $rest_api_versions = wc_get_webhook_rest_api_versions();
  101. $webhook->set_api_version( ! empty( $_POST['webhook_api_version'] ) ? sanitize_text_field( wp_unslash( $_POST['webhook_api_version'] ) ) : end( $rest_api_versions ) ); // WPCS: input var okay, CSRF ok.
  102. $webhook->save();
  103. // Run actions.
  104. do_action( 'woocommerce_webhook_options_save', $webhook->get_id() );
  105. if ( $errors ) {
  106. // Redirect to webhook edit page to avoid settings save actions.
  107. wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=advanced&section=webhooks&edit-webhook=' . $webhook->get_id() . '&error=' . rawurlencode( implode( '|', $errors ) ) ) );
  108. exit();
  109. } elseif ( isset( $_POST['webhook_status'] ) && 'active' === $_POST['webhook_status'] && $webhook->get_pending_delivery() ) { // WPCS: input var okay, CSRF ok.
  110. // Ping the webhook at the first time that is activated.
  111. $result = $webhook->deliver_ping();
  112. if ( is_wp_error( $result ) ) {
  113. // Redirect to webhook edit page to avoid settings save actions.
  114. wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=advanced&section=webhooks&edit-webhook=' . $webhook->get_id() . '&error=' . rawurlencode( $result->get_error_message() ) ) );
  115. exit();
  116. }
  117. }
  118. // Redirect to webhook edit page to avoid settings save actions.
  119. wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=advanced&section=webhooks&edit-webhook=' . $webhook->get_id() . '&updated=1' ) );
  120. exit();
  121. }
  122. /**
  123. * Bulk delete.
  124. *
  125. * @param array $webhooks List of webhooks IDs.
  126. */
  127. public static function bulk_delete( $webhooks ) {
  128. foreach ( $webhooks as $webhook_id ) {
  129. $webhook = new WC_Webhook( (int) $webhook_id );
  130. $webhook->delete( true );
  131. }
  132. $qty = count( $webhooks );
  133. $status = isset( $_GET['status'] ) ? '&status=' . sanitize_text_field( wp_unslash( $_GET['status'] ) ) : ''; // WPCS: input var okay, CSRF ok.
  134. // Redirect to webhooks page.
  135. wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=advanced&section=webhooks' . $status . '&deleted=' . $qty ) );
  136. exit();
  137. }
  138. /**
  139. * Delete webhook.
  140. */
  141. private function delete() {
  142. check_admin_referer( 'delete-webhook' );
  143. if ( isset( $_GET['delete'] ) ) { // WPCS: input var okay, CSRF ok.
  144. $webhook_id = absint( $_GET['delete'] ); // WPCS: input var okay, CSRF ok.
  145. if ( $webhook_id ) {
  146. $this->bulk_delete( array( $webhook_id ) );
  147. }
  148. }
  149. }
  150. /**
  151. * Webhooks admin actions.
  152. */
  153. public function actions() {
  154. if ( $this->is_webhook_settings_page() ) {
  155. // Save.
  156. if ( isset( $_POST['save'] ) && isset( $_POST['webhook_id'] ) ) { // WPCS: input var okay, CSRF ok.
  157. $this->save();
  158. }
  159. // Delete webhook.
  160. if ( isset( $_GET['delete'] ) ) { // WPCS: input var okay, CSRF ok.
  161. $this->delete();
  162. }
  163. }
  164. }
  165. /**
  166. * Page output.
  167. */
  168. public static function page_output() {
  169. // Hide the save button.
  170. $GLOBALS['hide_save_button'] = true;
  171. if ( isset( $_GET['edit-webhook'] ) ) { // WPCS: input var okay, CSRF ok.
  172. $webhook_id = absint( $_GET['edit-webhook'] ); // WPCS: input var okay, CSRF ok.
  173. $webhook = new WC_Webhook( $webhook_id );
  174. include __DIR__ . '/settings/views/html-webhooks-edit.php';
  175. return;
  176. }
  177. self::table_list_output();
  178. }
  179. /**
  180. * Notices.
  181. */
  182. public static function notices() {
  183. if ( isset( $_GET['deleted'] ) ) { // WPCS: input var okay, CSRF ok.
  184. $deleted = absint( $_GET['deleted'] ); // WPCS: input var okay, CSRF ok.
  185. /* translators: %d: count */
  186. WC_Admin_Settings::add_message( sprintf( _n( '%d webhook permanently deleted.', '%d webhooks permanently deleted.', $deleted, 'woocommerce' ), $deleted ) );
  187. }
  188. if ( isset( $_GET['updated'] ) ) { // WPCS: input var okay, CSRF ok.
  189. WC_Admin_Settings::add_message( __( 'Webhook updated successfully.', 'woocommerce' ) );
  190. }
  191. if ( isset( $_GET['created'] ) ) { // WPCS: input var okay, CSRF ok.
  192. WC_Admin_Settings::add_message( __( 'Webhook created successfully.', 'woocommerce' ) );
  193. }
  194. if ( isset( $_GET['error'] ) ) { // WPCS: input var okay, CSRF ok.
  195. foreach ( explode( '|', sanitize_text_field( wp_unslash( $_GET['error'] ) ) ) as $message ) { // WPCS: input var okay, CSRF ok.
  196. WC_Admin_Settings::add_error( trim( $message ) );
  197. }
  198. }
  199. }
  200. /**
  201. * Add screen option.
  202. */
  203. public function screen_option() {
  204. global $webhooks_table_list;
  205. if ( ! isset( $_GET['edit-webhook'] ) && $this->is_webhook_settings_page() ) { // WPCS: input var okay, CSRF ok.
  206. $webhooks_table_list = new WC_Admin_Webhooks_Table_List();
  207. // Add screen option.
  208. add_screen_option(
  209. 'per_page',
  210. array(
  211. 'default' => 10,
  212. 'option' => 'woocommerce_webhooks_per_page',
  213. )
  214. );
  215. }
  216. }
  217. /**
  218. * Table list output.
  219. */
  220. private static function table_list_output() {
  221. global $webhooks_table_list;
  222. echo '<h2 class="wc-table-list-header">' . esc_html__( 'Webhooks', 'woocommerce' ) . ' <a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=advanced&section=webhooks&edit-webhook=0' ) ) . '" class="add-new-h2">' . esc_html__( 'Add webhook', 'woocommerce' ) . '</a></h2>';
  223. // Get the webhooks count.
  224. $data_store = WC_Data_Store::load( 'webhook' );
  225. $num_webhooks = $data_store->get_count_webhooks_by_status();
  226. $count = array_sum( $num_webhooks );
  227. if ( 0 < $count ) {
  228. $webhooks_table_list->process_bulk_action();
  229. $webhooks_table_list->prepare_items();
  230. echo '<input type="hidden" name="page" value="wc-settings" />';
  231. echo '<input type="hidden" name="tab" value="advanced" />';
  232. echo '<input type="hidden" name="section" value="webhooks" />';
  233. $webhooks_table_list->views();
  234. $webhooks_table_list->search_box( __( 'Search webhooks', 'woocommerce' ), 'webhook' );
  235. $webhooks_table_list->display();
  236. } else {
  237. echo '<div class="woocommerce-BlankState woocommerce-BlankState--webhooks">';
  238. ?>
  239. <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>
  240. <a class="woocommerce-BlankState-cta button-primary button" href="<?php echo esc_url( admin_url( 'admin.php?page=wc-settings&tab=advanced&section=webhooks&edit-webhook=0' ) ); ?>"><?php esc_html_e( 'Create a new webhook', 'woocommerce' ); ?></a>
  241. <style type="text/css">#posts-filter .wp-list-table, #posts-filter .tablenav.top, .tablenav.bottom .actions { display: none; }</style>
  242. <?php
  243. }
  244. }
  245. /**
  246. * Logs output.
  247. *
  248. * @deprecated 3.3.0
  249. * @param WC_Webhook $webhook Deprecated.
  250. */
  251. public static function logs_output( $webhook = 'deprecated' ) {
  252. wc_deprecated_function( 'WC_Admin_Webhooks::logs_output', '3.3' );
  253. }
  254. /**
  255. * Get the webhook topic data.
  256. *
  257. * @param WC_Webhook $webhook Webhook instance.
  258. *
  259. * @return array
  260. */
  261. public static function get_topic_data( $webhook ) {
  262. $topic = $webhook->get_topic();
  263. $event = '';
  264. $resource = '';
  265. if ( $topic ) {
  266. list( $resource, $event ) = explode( '.', $topic );
  267. if ( 'action' === $resource ) {
  268. $topic = 'action';
  269. } elseif ( ! in_array( $resource, array( 'coupon', 'customer', 'order', 'product' ), true ) ) {
  270. $topic = 'custom';
  271. }
  272. }
  273. return array(
  274. 'topic' => $topic,
  275. 'event' => $event,
  276. 'resource' => $resource,
  277. );
  278. }
  279. /**
  280. * Get the logs navigation.
  281. *
  282. * @deprecated 3.3.0
  283. * @param int $total Deprecated.
  284. * @param WC_Webhook $webhook Deprecated.
  285. */
  286. public static function get_logs_navigation( $total, $webhook ) {
  287. wc_deprecated_function( 'WC_Admin_Webhooks::get_logs_navigation', '3.3' );
  288. }
  289. }
  290. new WC_Admin_Webhooks();