get_topic(); $event = ''; $resource = ''; if ( $topic ) { list( $resource, $event ) = explode( '.', $topic ); if ( 'action' === $resource ) { $topic = 'action'; } else if ( ! in_array( $resource, array( 'coupon', 'customer', 'order', 'product' ) ) ) { $topic = 'custom'; } } return array( 'topic' => $topic, 'event' => $event, 'resource' => $resource ); } /** * Output the metabox */ public static function output( $post ) { wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' ); $webhook = new WC_Webhook( $post->ID ); ?>
update( $wpdb->posts, array( 'post_title' => $name ), array( 'ID' => $post_id ) ); } /** * Updated the Webhook status * * @param WC_Webhook $webhook */ private static function update_status( $webhook ) { $status = ! empty( $_POST['status'] ) ? wc_clean( $_POST['status'] ) : ''; $webhook->update_status( $status ); } /** * Updated the Webhook delivery URL * * @param WC_Webhook $webhook */ private static function update_delivery_url( $webhook ) { $delivery_url = ! empty( $_POST['delivery_url'] ) ? $_POST['delivery_url'] : ''; if ( wc_is_valid_url( $delivery_url ) ) { $webhook->set_delivery_url( $delivery_url ); } } /** * Updated the Webhook secret * * @param WC_Webhook $webhook */ private static function update_secret( $webhook ) { $secret = ! empty( $_POST['secret'] ) ? $_POST['secret'] : get_user_meta( get_current_user_id(), 'woocommerce_api_consumer_secret', true ); $webhook->set_secret( $secret ); } /** * Updated the Webhook topic * * @param WC_Webhook $webhook */ private static function update_topic( $webhook ) { if ( ! empty( $_POST['topic'] ) ) { list( $resource, $event ) = explode( '.', wc_clean( $_POST['topic'] ) ); if ( 'action' === $resource ) { $event = ! empty( $_POST['action_event'] ) ? wc_clean( $_POST['action_event'] ) : ''; } else if ( ! in_array( $resource, array( 'coupon', 'customer', 'order', 'product' ) ) && ! empty( $_POST['custom_topic'] ) ) { list( $resource, $event ) = explode( '.', wc_clean( $_POST['custom_topic'] ) ); } $topic = $resource . '.' . $event; if ( wc_is_webhook_valid_topic( $topic ) ) { $webhook->set_topic( $topic ); } } } /** * Set Webhook post data. * * @param int $post_id */ private static function set_post_data( $post_id ) { global $wpdb; $password = uniqid( 'webhook_' ); $password = strlen( $password ) > 20 ? substr( $password, 0, 20 ) : $password; $wpdb->update( $wpdb->posts, array( 'post_password' => $password, 'ping_status' => 'closed', 'comment_status' => 'open' ), array( 'ID' => $post_id ) ); } /** * Save meta box data */ public static function save( $post_id ) { $webhook = new WC_Webhook( $post_id ); // Name self::update_name( $post_id ); // Status self::update_status( $webhook ); // Delivery URL self::update_delivery_url( $webhook ); // Secret self::update_secret( $webhook ); // Topic self::update_topic( $webhook ); // Webhook Created if ( isset( $_POST['original_post_status'] ) && 'auto-draft' === $_POST['original_post_status'] ) { // Set Post data like ping status and password self::set_post_data( $post_id ); // Ping webhook $webhook->deliver_ping(); } do_action( 'woocommerce_webhook_options_save', $post_id ); } }