Created new Settings API tab and moved the old webhooks tab to API tab

This commit is contained in:
Claudio Sanches 2015-05-15 16:12:11 -03:00
parent d53f78ed56
commit 0edbfeaa35
7 changed files with 110 additions and 68 deletions

View File

@ -2,10 +2,10 @@
/**
* WooCommerce Admin Settings Class.
*
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin
* @version 2.2.0
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin
* @version 2.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
@ -40,7 +40,7 @@ class WC_Admin_Settings {
$settings[] = include( 'settings/class-wc-settings-accounts.php' );
$settings[] = include( 'settings/class-wc-settings-emails.php' );
$settings[] = include( 'settings/class-wc-settings-integrations.php' );
$settings[] = include( 'settings/class-wc-settings-webhooks.php' );
$settings[] = include( 'settings/class-wc-settings-api.php' );
self::$settings = apply_filters( 'woocommerce_get_settings_pages', $settings );
}

View File

@ -5,7 +5,7 @@
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin
* @version 2.3.0
* @version 2.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
@ -76,7 +76,7 @@ class WC_Admin_Webhooks_Table_List extends WP_List_Table {
*/
public function column_title( $webhook ) {
$the_webhook = $this->get_webbook_object( $webhook );
$edit_link = admin_url( 'admin.php?page=wc-settings&tab=webhooks&edit-webhook=' . $the_webhook->id );
$edit_link = admin_url( 'admin.php?page=wc-settings&tab=api&section=webhooks&edit-webhook=' . $the_webhook->id );
$title = _draft_or_post_title( $the_webhook->get_post_data() );
$post_type_object = get_post_type_object( $the_webhook->get_post_data()->post_type );
$post_status = $the_webhook->get_post_data()->post_status;
@ -209,7 +209,7 @@ class WC_Admin_Webhooks_Table_List extends WP_List_Table {
}
$class = empty( $class ) && empty( $_REQUEST['status'] ) ? ' class="current"' : '';
$status_links['all'] = "<a href='admin.php?page=wc-settings&amp;tab=webhooks'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_posts, 'posts', 'woocommerce' ), number_format_i18n( $total_posts ) ) . '</a>';
$status_links['all'] = "<a href='admin.php?page=wc-settings&amp;tab=api&amp;section=webhooks'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_posts, 'posts', 'woocommerce' ), number_format_i18n( $total_posts ) ) . '</a>';
foreach ( get_post_stati( array( 'show_in_admin_status_list' => true ), 'objects' ) as $status ) {
$class = '';
@ -229,7 +229,7 @@ class WC_Admin_Webhooks_Table_List extends WP_List_Table {
$label = $this->get_status_label( $status_name, $status );
$status_links[ $status_name ] = "<a href='admin.php?page=wc-settings&amp;tab=webhooks&amp;status=$status_name'$class>" . sprintf( translate_nooped_plural( $label, $num_posts->$status_name ), number_format_i18n( $num_posts->$status_name ) ) . '</a>';
$status_links[ $status_name ] = "<a href='admin.php?page=wc-settings&amp;tab=api&amp;section=webhooks&amp;status=$status_name'$class>" . sprintf( translate_nooped_plural( $label, $num_posts->$status_name ), number_format_i18n( $num_posts->$status_name ) ) . '</a>';
}
return $status_links;
@ -260,7 +260,7 @@ class WC_Admin_Webhooks_Table_List extends WP_List_Table {
*/
protected function extra_tablenav( $which ) {
if ( 'top' == $which && isset( $_GET['status'] ) && 'trash' == $_GET['status'] && current_user_can( 'delete_shop_webhooks' ) ) {
echo '<div class="alignleft actions"><a class="button apply" href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=webhooks&status=trash&empty_trash=1' ) ) . '">' . __( 'Empty Trash', 'woocommerce' ) . '</a></div>';
echo '<div class="alignleft actions"><a class="button apply" href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=api&section=webhooks&status=trash&empty_trash=1' ) ) . '">' . __( 'Empty Trash', 'woocommerce' ) . '</a></div>';
}
}

View File

@ -5,7 +5,7 @@
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin
* @version 2.3.0
* @version 2.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
@ -30,7 +30,12 @@ class WC_Admin_Webhooks {
* @return bool
*/
private function is_webhook_settings_page() {
return isset( $_GET['page'] ) && 'wc-settings' == $_GET['page'] && isset( $_GET['tab'] ) && 'webhooks' == $_GET['tab'];
return isset( $_GET['page'] )
&& 'wc-settings' == $_GET['page']
&& isset( $_GET['tab'] )
&& 'api' == $_GET['tab']
&& isset( $_GET['section'] )
&& 'webhooks' == isset( $_GET['section'] );
}
/**
@ -157,7 +162,7 @@ class WC_Admin_Webhooks {
delete_transient( 'woocommerce_webhook_ids' );
// 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' ) );
wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=api&section=webhooks&edit-webhook=' . $webhook->id . '&updated=1' ) );
exit();
}
@ -188,7 +193,7 @@ class WC_Admin_Webhooks {
delete_transient( 'woocommerce_webhook_ids' );
// Redirect to edit page
wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks&edit-webhook=' . $webhook_id . '&created=1' ) );
wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=api&section=webhooks&edit-webhook=' . $webhook_id . '&created=1' ) );
exit();
}
@ -212,7 +217,7 @@ class WC_Admin_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 ) );
wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=api&section=webhooks' . $status . '&' . $type . '=' . $qty ) );
exit();
}
@ -229,7 +234,7 @@ class WC_Admin_Webhooks {
$qty = count( $webhooks );
// Redirect to webhooks page
wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks&status=trash&untrashed=' . $qty ) );
wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=api&section=webhooks&status=trash&untrashed=' . $qty ) );
exit();
}
@ -283,7 +288,7 @@ class WC_Admin_Webhooks {
$qty = count( $webhooks );
// Redirect to webhooks page
wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks&deleted=' . $qty ) );
wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=api&section=webhooks&deleted=' . $qty ) );
exit();
}

View File

@ -1,38 +1,54 @@
<?php
/**
* WooCommerce Webhooks Settings
* WooCommerce API Settings
*
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin
* @version 2.3.0
* @version 2.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'WC_Settings_Webhooks' ) ) :
if ( ! class_exists( 'WC_Settings_Rest_API' ) ) :
/**
* WC_Settings_Webhooks
* WC_Settings_Rest_API
*/
class WC_Settings_Webhooks extends WC_Settings_Page {
class WC_Settings_Rest_API extends WC_Settings_Page {
/**
* Constructor
*/
public function __construct() {
$this->id = 'webhooks';
$this->label = __( 'Webhooks', 'woocommerce' );
$this->id = 'api';
$this->label = __( 'API', 'woocommerce' );
add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) );
add_action( 'woocommerce_settings_form_method_tab_' . $this->id, array( $this, 'form_method' ) );
$this->notices();
}
/**
* Get sections
*
* @return array
*/
public function get_sections() {
$sections = array(
'' => __( 'Settings', 'woocommerce' ),
'keys' => __( 'Keys', 'woocommerce' ),
'webhooks' => __( 'Webhooks', 'woocommerce' ),
);
return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections );
}
/**
* Form method
*
@ -41,7 +57,9 @@ class WC_Settings_Webhooks extends WC_Settings_Page {
* @return string
*/
public function form_method( $method ) {
if ( isset( $_GET['edit-webhook'] ) ) {
global $current_section;
if ( 'webhooks' == $current_section && isset( $_GET['edit-webhook'] ) ) {
$webhook_id = absint( $_GET['edit-webhook'] );
$webhook = new WC_Webhook( $webhook_id );
@ -57,6 +75,46 @@ class WC_Settings_Webhooks extends WC_Settings_Page {
* Notices.
*/
private function notices() {
if ( isset( $_GET['section'] ) && 'webhooks' == $_GET['section'] ) {
$this->webhook_notices();
}
}
/**
* Output the settings
*/
public function output() {
global $current_section;
if ( 'webhooks' == $current_section ) {
$this->webhooks_output();
}
}
/**
* Webhooks output
*/
private function webhooks_output() {
// Hide the save button
$GLOBALS['hide_save_button'] = true;
if ( isset( $_GET['edit-webhook'] ) ) {
$webhook_id = absint( $_GET['edit-webhook'] );
$webhook = new WC_Webhook( $webhook_id );
if ( 'trash' != $webhook->post_data->post_status ) {
$this->webhooks_edit_output( $webhook );
return;
}
}
$this->webhooks_table_list_output();
}
/**
* Webhooks - Notices.
*/
private function webhook_notices() {
if ( isset( $_GET['trashed'] ) ) {
$trashed = absint( $_GET['trashed'] );
@ -85,16 +143,17 @@ class WC_Settings_Webhooks extends WC_Settings_Page {
}
/**
* Table list output
* Webhooks - 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>';
private function webhooks_table_list_output() {
echo '<h3>' . __( 'Webhooks', 'woocommerce' ) . ' <a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=api&section=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();
echo '<input type="hidden" name="page" value="wc-settings" />';
echo '<input type="hidden" name="tab" value="webhooks" />';
echo '<input type="hidden" name="tab" value="api" />';
echo '<input type="hidden" name="section" value="webhooks" />';
$webhooks_table_list->views();
$webhooks_table_list->search_box( __( 'Search Webhooks', 'woocommerce' ), 'webhook' );
@ -102,20 +161,20 @@ class WC_Settings_Webhooks extends WC_Settings_Page {
}
/**
* Edit webhook output
* Webhooks - Edit webhook output
*
* @param WC_Webhook $webhook
*/
private function edit_output( $webhook ) {
private function webhooks_edit_output( $webhook ) {
include_once( 'views/html-webhooks-edit.php' );
}
/**
* Logs output
* Webhooks - Logs output
*
* @param WC_Webhook $webhook
*/
private function logs_output( $webhook ) {
private function webhooks_logs_output( $webhook ) {
$current = isset( $_GET['log_page'] ) ? absint( $_GET['log_page'] ) : 1;
$args = array(
'post_id' => $webhook->id,
@ -142,33 +201,11 @@ class WC_Settings_Webhooks extends WC_Settings_Page {
}
/**
* Output the settings
*/
public function output() {
global $current_section;
// Hide the save button
$GLOBALS['hide_save_button'] = true;
if ( isset( $_GET['edit-webhook'] ) ) {
$webhook_id = absint( $_GET['edit-webhook'] );
$webhook = new WC_Webhook( $webhook_id );
if ( 'trash' != $webhook->post_data->post_status ) {
$this->edit_output( $webhook );
return;
}
}
$this->table_list_output();
}
/**
* Get the webhook topic data
* Webhooks - Get the webhook topic data
*
* @return array
*/
private function get_topic_data( $webhook ) {
private function webhooks_get_topic_data( $webhook ) {
$topic = $webhook->get_topic();
$event = '';
$resource = '';
@ -191,13 +228,13 @@ class WC_Settings_Webhooks extends WC_Settings_Page {
}
/**
* Get the logs navigation.
* Webhooks - Get the logs navigation.
*
* @param int $total
*
* @return string
*/
private function get_logs_navigation( $total, $webhook ) {
private function webhooks_get_logs_navigation( $total, $webhook ) {
$pages = ceil( $total / 10 );
$current = isset( $_GET['log_page'] ) ? absint( $_GET['log_page'] ) : 1;
@ -212,13 +249,13 @@ class WC_Settings_Webhooks extends WC_Settings_Page {
if ( 1 == $current ) {
$html .= '<button class="button-primary" disabled="disabled">' . __( '&lsaquo; Previous', 'woocommerce' ) . '</button> ';
} else {
$html .= '<a class="button-primary" href="' . admin_url( 'admin.php?page=wc-settings&tab=webhooks&edit-webhook=' . $webhook->id . '&log_page=' . ( $current - 1 ) ) . '#webhook-logs">' . __( '&lsaquo; Previous', 'woocommerce' ) . '</a> ';
$html .= '<a class="button-primary" href="' . admin_url( 'admin.php?page=wc-settings&tab=api&section=webhooks&edit-webhook=' . $webhook->id . '&log_page=' . ( $current - 1 ) ) . '#webhook-logs">' . __( '&lsaquo; Previous', 'woocommerce' ) . '</a> ';
}
if ( $pages == $current ) {
$html .= '<button class="button-primary" disabled="disabled">' . __( 'Next &rsaquo;', 'woocommerce' ) . '</button>';
} else {
$html .= '<a class="button-primary" href="' . admin_url( 'admin.php?page=wc-settings&tab=webhooks&edit-webhook=' . $webhook->id . '&log_page=' . ( $current + 1 ) ) . '#webhook-logs">' . __( 'Next &rsaquo;', 'woocommerce' ) . '</a>';
$html .= '<a class="button-primary" href="' . admin_url( 'admin.php?page=wc-settings&tab=api&section=webhooks&edit-webhook=' . $webhook->id . '&log_page=' . ( $current + 1 ) ) . '#webhook-logs">' . __( 'Next &rsaquo;', 'woocommerce' ) . '</a>';
}
$html .= '</p>';
}
@ -231,4 +268,4 @@ class WC_Settings_Webhooks extends WC_Settings_Page {
endif;
return new WC_Settings_Webhooks();
return new WC_Settings_Rest_API();

View File

@ -1,7 +1,7 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit;
}
?>

View File

@ -1,7 +1,7 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit;
}
$count_comments = wp_count_comments( $webhook->id );
@ -9,7 +9,7 @@ $total = $count_comments->approved;
?>
<?php echo $this->get_logs_navigation( $total, $webhook ); ?>
<?php echo $this->webhooks_get_logs_navigation( $total, $webhook ); ?>
<table id="webhook-logs-table" class="widefat">
<thead>
@ -39,4 +39,4 @@ $total = $count_comments->approved;
</tbody>
</table>
<?php echo $this->get_logs_navigation( $total, $webhook ); ?>
<?php echo $this->webhooks_get_logs_navigation( $total, $webhook ); ?>

View File

@ -44,7 +44,7 @@ if ( ! defined( 'ABSPATH' ) ) {
<td class="forminp">
<select name="webhook_topic" id="webhook_topic" class="wc-enhanced-select">
<?php
$topic_data = $this->get_topic_data( $webhook );
$topic_data = $this->webhooks_get_topic_data( $webhook );
$topics = array(
'' => __( 'Select an option&hellip;', 'woocommerce' ),
@ -162,7 +162,7 @@ if ( ! defined( 'ABSPATH' ) ) {
<div id="webhook-logs" class="settings-panel">
<h3><?php _e( 'Webhook Logs', 'woocommerce' ); ?></h3>
<?php $this->logs_output( $webhook ); ?>
<?php $this->webhooks_logs_output( $webhook ); ?>
</div>
<script type="text/javascript">