Skip to content

Commit

Permalink
Added method to create webhooks on settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiosanches committed Jan 9, 2015
1 parent 2626275 commit c71869c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 30 deletions.
63 changes: 35 additions & 28 deletions includes/admin/class-wc-admin-webhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class WC_Admin_Webhooks {
public function __construct() {
// Save webhooks
add_action( 'admin_init', array( $this, 'save' ) );
add_action( 'admin_init', array( $this, 'create' ) );
}

/**
Expand Down Expand Up @@ -95,28 +96,6 @@ private function update_topic( $webhook ) {
}
}

/**
* Set Webhook post data.
*
* @param int $webhook_id
*/
private function set_post_data( $webhook_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' => $webhook_id )
);
}

/**
* Save method
*/
Expand Down Expand Up @@ -150,12 +129,9 @@ public function save() {
// Topic
$this->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
$this->set_post_data( $webhook->id );

// Ping webhook
// Ping the webhook at the first time that is activated
$peding_delivery = get_post_meta( $webhook->id, '_webhook_pending_delivery', true );
if ( isset( $_POST['webhook_status'] ) && 'active' === $_POST['webhook_status'] && $peding_delivery ) {
$webhook->deliver_ping();
}

Expand All @@ -166,6 +142,37 @@ public function save() {
exit();
}
}

/**
* Create Webhook
*/
public function create() {
if ( isset( $_GET['page'] ) && 'wc-settings' == $_GET['page'] && isset( $_GET['tab'] ) && 'webhooks' == $_GET['tab'] && isset( $_GET['create-webhook'] ) ) {
if ( ! current_user_can( 'publish_shop_webhooks' ) ) {
wp_die( __( 'You don\'t have permissions to create Webhooks!', 'woocommerce' ) );
}

$webhook_id = wp_insert_post( array(
'post_type' => 'shop_webhook',
'post_status' => 'pending',
'ping_status' => 'closed',
'post_author' => get_current_user_id(),
'post_password' => strlen( ( $password = uniqid( 'webhook_' ) ) ) > 20 ? substr( $password, 0, 20 ) : $password,
'post_title' => sprintf( __( 'Webhook created on %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce' ) ) ),
'comment_status' => 'open'
) );

if ( is_wp_error( $webhook_id ) ) {
wp_die( $webhook_id->get_error_messages() );
}

update_post_meta( $webhook_id, '_webhook_pending_delivery', true );

// Redirect to edit page
wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks&edit-webhook=' . $webhook_id . '&created=1' ) );
exit();
}
}
}

new WC_Admin_Webhooks();
8 changes: 6 additions & 2 deletions includes/admin/settings/class-wc-settings-webhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,20 @@ private function notices() {
}

if ( isset( $_GET['updated'] ) ) {
$updated = absint( $_GET['updated'] );
WC_Admin_Settings::add_message( __( 'Webhook updated successfully.', 'woocommerce' ) );
}

WC_Admin_Settings::add_message( __( 'Webhook saved successfully.', 'woocommerce' ) );
if ( isset( $_GET['created'] ) ) {
WC_Admin_Settings::add_message( __( 'Webhook created successfully.', 'woocommerce' ) );
}
}

/**
* Table list output
*/
private function table_list_output() {
echo '<h3>' . __( 'Webhooks', 'woocommerce' ) . ' <a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=webhooks&create-webhook=1' ) ) . '" class="add-new-h2">' . __( 'Add Webhook', 'woocommerce' ) . '</a></h3>';

$webhooks_table_list = new WC_Admin_Webhooks_Table_List();
$webhooks_table_list->prepare_items();

Expand Down

0 comments on commit c71869c

Please sign in to comment.