woocommerce/includes/admin/class-wc-admin-webhooks.php

271 lines
6.9 KiB
PHP
Raw Normal View History

2015-01-09 17:42:01 +00:00
<?php
/**
* WooCommerce Admin Webhooks Class.
*
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin
* @version 2.3.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* WC_Admin_Webhooks
*/
class WC_Admin_Webhooks {
/**
* Initialize the webhooks admin actions
*/
public function __construct() {
2015-01-10 00:08:45 +00:00
add_action( 'admin_init', array( $this, 'actions' ) );
}
/**
* Check if is webhook settings page
*
* @return bool
*/
2015-01-10 00:25:04 +00:00
private function is_webhook_settings_page() {
2015-01-10 00:08:45 +00:00
return isset( $_GET['page'] ) && 'wc-settings' == $_GET['page'] && isset( $_GET['tab'] ) && 'webhooks' == $_GET['tab'];
2015-01-09 17:42:01 +00:00
}
/**
* Updated the Webhook name
*
* @param int $webhook_id
*/
private function update_name( $webhook_id ) {
global $wpdb;
$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' ) ) );
$wpdb->update( $wpdb->posts, array( 'post_title' => $name ), array( 'ID' => $webhook_id ) );
}
/**
* Updated the Webhook status
*
* @param WC_Webhook $webhook
*/
private function update_status( $webhook ) {
$status = ! empty( $_POST['webhook_status'] ) ? wc_clean( $_POST['webhook_status'] ) : '';
$webhook->update_status( $status );
}
/**
* Updated the Webhook delivery URL
*
* @param WC_Webhook $webhook
*/
private function update_delivery_url( $webhook ) {
$delivery_url = ! empty( $_POST['webhook_delivery_url'] ) ? $_POST['webhook_delivery_url'] : '';
if ( wc_is_valid_url( $delivery_url ) ) {
$webhook->set_delivery_url( $delivery_url );
}
}
/**
* Updated the Webhook secret
*
* @param WC_Webhook $webhook
*/
private function update_secret( $webhook ) {
$secret = ! empty( $_POST['webhook_secret'] ) ? $_POST['webhook_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 function update_topic( $webhook ) {
if ( ! empty( $_POST['webhook_topic'] ) ) {
list( $resource, $event ) = explode( '.', wc_clean( $_POST['webhook_topic'] ) );
if ( 'action' === $resource ) {
$event = ! empty( $_POST['webhook_action_event'] ) ? wc_clean( $_POST['webhook_action_event'] ) : '';
} else if ( ! in_array( $resource, array( 'coupon', 'customer', 'order', 'product' ) ) && ! empty( $_POST['webhook_custom_topic'] ) ) {
list( $resource, $event ) = explode( '.', wc_clean( $_POST['webhook_custom_topic'] ) );
}
$topic = $resource . '.' . $event;
if ( wc_is_webhook_valid_topic( $topic ) ) {
$webhook->set_topic( $topic );
}
}
}
/**
* Save method
*/
2015-01-10 00:08:45 +00:00
private function save() {
if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) {
die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
}
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
$webhook_id = absint( $_POST['webhook_id'] );
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
if ( ! current_user_can( 'edit_shop_webhook', $webhook_id ) ) {
return;
}
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
$webhook = new WC_Webhook( $webhook_id );
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
// Name
$this->update_name( $webhook->id );
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
// Status
$this->update_status( $webhook );
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
// Delivery URL
$this->update_delivery_url( $webhook );
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
// Secret
$this->update_secret( $webhook );
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
// Topic
$this->update_topic( $webhook );
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
// 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();
}
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
do_action( 'woocommerce_webhook_options_save', $webhook->id );
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
// Redirect to webhook edit page to avoid settings save actions
wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks&edit-webhook=' . $webhook->id . '&updated=1' ) );
exit();
}
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
/**
* Create Webhook
*/
private function create() {
if ( ! current_user_can( 'publish_shop_webhooks' ) ) {
wp_die( __( 'You don\'t have permissions to create Webhooks!', 'woocommerce' ) );
2015-01-09 17:42:01 +00:00
}
2015-01-10 00:08:45 +00:00
$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();
2015-01-09 17:42:01 +00:00
}
/**
2015-01-10 00:08:45 +00:00
* Bulk trash/delete
*
* @param array $webhooks
* @param bool $delete
*/
2015-01-10 00:08:45 +00:00
private function bulk_trash( $webhooks, $delete = false ) {
foreach ( $webhooks as $webhook_id ) {
if ( $delete ) {
wp_delete_post( $webhook_id, true );
} else {
wp_trash_post( $webhook_id );
}
2015-01-10 00:08:45 +00:00
}
$type = ! EMPTY_TRASH_DAYS || $delete ? 'deleted' : 'trashed';
$qty = count( $webhooks );
$status = isset( $_GET['status'] ) ? '&status=' . sanitize_text_field( $_GET['status'] ) : '';
2015-01-10 00:08:45 +00:00
// Redirect to webhooks page
wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks' . $status . '&' . $type . '=' . $qty ) );
exit();
}
/**
* Bulk untrash
*
* @param array $webhooks
*/
private function bulk_untrash( $webhooks ) {
foreach ( $webhooks as $webhook_id ) {
wp_untrash_post( $webhook_id );
}
$qty = count( $webhooks );
// Redirect to webhooks page
wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks&status=trash&untrashed=' . $qty ) );
exit();
}
/**
* Webhook bulk actions
*/
private function bulk_actions() {
if ( ! current_user_can( 'edit_shop_webhooks' ) ) {
wp_die( __( 'You don\'t have permissions to edit Webhooks!', 'woocommerce' ) );
}
$webhooks = array_map( 'absint', (array) $_GET['webhook'] );
switch ( $_GET['action'] ) {
case 'trash' :
$this->bulk_trash( $webhooks );
break;
case 'untrash' :
$this->bulk_untrash( $webhooks );
break;
case 'delete' :
$this->bulk_trash( $webhooks, true );
break;
default :
break;
}
}
/**
* Webhooks admin actions
*/
public function actions() {
2015-01-10 00:25:04 +00:00
if ( $this->is_webhook_settings_page() ) {
2015-01-10 00:08:45 +00:00
// Save
if ( isset( $_POST['save'] ) && isset( $_POST['webhook_id'] ) ) {
$this->save();
}
2015-01-10 00:08:45 +00:00
// Create
if ( isset( $_GET['create-webhook'] ) ) {
$this->create();
}
2015-01-10 00:08:45 +00:00
// Create
if ( isset( $_GET['action'] ) && isset( $_GET['webhook'] ) ) {
$this->bulk_actions();
}
}
}
2015-01-09 17:42:01 +00:00
}
new WC_Admin_Webhooks();