Created webhooks bulk actions

This commit is contained in:
Claudio Sanches 2015-01-09 22:08:45 -02:00
parent c71869cd6e
commit 572d451aa6
2 changed files with 188 additions and 70 deletions

View File

@ -79,27 +79,35 @@ class WC_Admin_Webhooks_Table_List extends WP_List_Table {
$edit_link = admin_url( 'admin.php?page=wc-settings&tab=webhooks&edit-webhook=' . $the_webhook->id );
$title = _draft_or_post_title( $the_webhook->post_data );
$post_type_object = get_post_type_object( $the_webhook->post_data->post_type );
$post_status = $the_webhook->post_data->post_status;
$output = '<strong><a href="' . esc_attr( $edit_link ) . '">' . esc_html( $title ) . '</a></strong>';
// Title
$output = '<strong>';
if ( 'trash' == $post_status ) {
$output .= esc_html( $title );
} else {
$output .= '<a href="' . esc_attr( $edit_link ) . '">' . esc_html( $title ) . '</a>';
}
$output .= '</strong>';
// Get actions
$actions = array();
$actions['id'] = sprintf( __( 'ID: %d', 'woocommerce' ), $the_webhook->id );
if ( current_user_can( $post_type_object->cap->edit_post, $the_webhook->id ) && 'trash' !== $the_webhook->post_data->post_status ) {
if ( current_user_can( $post_type_object->cap->edit_post, $the_webhook->id ) && 'trash' !== $post_status ) {
$actions['edit'] = '<a href="' . esc_attr( $edit_link ) . '">' . __( 'Edit', 'woocommerce' ) . '</a>';
}
if ( current_user_can( $post_type_object->cap->delete_post, $the_webhook->id ) ) {
if ( 'trash' == $the_webhook->post_data->post_status ) {
if ( 'trash' == $post_status ) {
$actions['untrash'] = '<a title="' . esc_attr( __( 'Restore this item from the Trash', 'woocommerce' ) ) . '" href="' . wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&amp;action=untrash', $the_webhook->id ) ), 'untrash-post_' . $the_webhook->id ) . '">' . __( 'Restore', 'woocommerce' ) . '</a>';
} elseif ( EMPTY_TRASH_DAYS ) {
$actions['trash'] = '<a class="submitdelete" title="' . esc_attr( __( 'Move this item to the Trash', 'woocommerce' ) ) . '" href="' . get_delete_post_link( $the_webhook->id ) . '">' . __( 'Trash', 'woocommerce' ) . '</a>';
}
if ( 'trash' == $the_webhook->post_data->post_status || ! EMPTY_TRASH_DAYS ) {
if ( 'trash' == $post_status || ! EMPTY_TRASH_DAYS ) {
$actions['delete'] = '<a class="submitdelete" title="' . esc_attr( __( 'Delete this item permanently', 'woocommerce' ) ) . '" href="' . get_delete_post_link( $the_webhook->id, '', true ) . '">' . __( 'Delete Permanently', 'woocommerce' ) . '</a>';
}
}
@ -225,11 +233,29 @@ class WC_Admin_Webhooks_Table_List extends WP_List_Table {
return $status_links;
}
/**
* Get bulk actions
*
* @return array
*/
protected function get_bulk_actions() {
if ( isset( $_GET['status'] ) && 'trash' == $_GET['status'] ) {
return array(
'untrash' => __( 'Restore', 'woocommerce' ),
'delete' => __( 'Delete Permanently', 'woocommerce' )
);
}
return array(
'trash' => __( 'Move to Trash', 'woocommerce' )
);
}
/**
* Prepare table list items.
*/
public function prepare_items() {
$per_page = 10;
$per_page = apply_filters( 'woocommerce_webhooks_settings_posts_per_page', 10 );
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();

View File

@ -21,9 +21,16 @@ class WC_Admin_Webhooks {
* Initialize the webhooks admin actions
*/
public function __construct() {
// Save webhooks
add_action( 'admin_init', array( $this, 'save' ) );
add_action( 'admin_init', array( $this, 'create' ) );
add_action( 'admin_init', array( $this, 'actions' ) );
}
/**
* Check if is webhook settings page
*
* @return bool
*/
private function is_webhook_settigs_page() {
return isset( $_GET['page'] ) && 'wc-settings' == $_GET['page'] && isset( $_GET['tab'] ) && 'webhooks' == $_GET['tab'];
}
/**
@ -99,78 +106,163 @@ class WC_Admin_Webhooks {
/**
* Save method
*/
public function save() {
if ( isset( $_GET['page'] ) && 'wc-settings' == $_GET['page'] && isset( $_GET['tab'] ) && 'webhooks' == $_GET['tab'] && isset( $_POST['save'] ) && isset( $_POST['webhook_id'] ) ) {
if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) {
die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
}
$webhook_id = absint( $_POST['webhook_id'] );
if ( ! current_user_can( 'edit_shop_webhook', $webhook_id ) ) {
return;
}
$webhook = new WC_Webhook( $webhook_id );
// Name
$this->update_name( $webhook->id );
// Status
$this->update_status( $webhook );
// Delivery URL
$this->update_delivery_url( $webhook );
// Secret
$this->update_secret( $webhook );
// Topic
$this->update_topic( $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();
}
do_action( 'woocommerce_webhook_options_save', $webhook->id );
// 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();
private function save() {
if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) {
die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
}
$webhook_id = absint( $_POST['webhook_id'] );
if ( ! current_user_can( 'edit_shop_webhook', $webhook_id ) ) {
return;
}
$webhook = new WC_Webhook( $webhook_id );
// Name
$this->update_name( $webhook->id );
// Status
$this->update_status( $webhook );
// Delivery URL
$this->update_delivery_url( $webhook );
// Secret
$this->update_secret( $webhook );
// Topic
$this->update_topic( $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();
}
do_action( 'woocommerce_webhook_options_save', $webhook->id );
// 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();
}
/**
* 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' ) );
private function create() {
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();
}
/**
* Bulk trash/delete
*
* @param array $webhooks
* @param bool $delete
*/
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 );
}
}
$type = ! EMPTY_TRASH_DAYS || $delete ? 'deleted' : 'trashed';
$qty = count( $webhooks );
$status = isset( $_GET['status'] ) ? '&status=' . sanitize_text_field( $_GET['status'] ) : '';
// 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() {
if ( $this->is_webhook_settigs_page() ) {
// Save
if ( isset( $_POST['save'] ) && isset( $_POST['webhook_id'] ) ) {
$this->save();
}
$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() );
// Create
if ( isset( $_GET['create-webhook'] ) ) {
$this->create();
}
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();
// Create
if ( isset( $_GET['action'] ) && isset( $_GET['webhook'] ) ) {
$this->bulk_actions();
}
}
}
}