2015-01-06 19:24:32 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* WooCommerce API Settings
|
2015-01-06 19:24:32 +00:00
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category Admin
|
|
|
|
* @package WooCommerce/Admin
|
2015-05-15 19:12:11 +00:00
|
|
|
* @version 2.4.0
|
2015-01-06 19:24:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
|
|
|
|
2017-02-16 11:46:01 +00:00
|
|
|
if ( ! class_exists( 'WC_Settings_Rest_API', false ) ) :
|
2015-01-06 19:24:32 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* WC_Settings_Rest_API.
|
2015-01-06 19:24:32 +00:00
|
|
|
*/
|
2015-05-15 19:12:11 +00:00
|
|
|
class WC_Settings_Rest_API extends WC_Settings_Page {
|
2015-01-06 19:24:32 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Constructor.
|
2015-01-06 19:24:32 +00:00
|
|
|
*/
|
|
|
|
public function __construct() {
|
2015-05-15 19:12:11 +00:00
|
|
|
$this->id = 'api';
|
|
|
|
$this->label = __( 'API', 'woocommerce' );
|
2015-01-06 19:24:32 +00:00
|
|
|
|
|
|
|
add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
|
|
|
|
add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
|
2015-05-15 19:12:11 +00:00
|
|
|
add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) );
|
2015-01-06 19:24:32 +00:00
|
|
|
add_action( 'woocommerce_settings_form_method_tab_' . $this->id, array( $this, 'form_method' ) );
|
2015-05-15 19:35:50 +00:00
|
|
|
add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );
|
2015-01-06 19:24:32 +00:00
|
|
|
|
|
|
|
$this->notices();
|
|
|
|
}
|
|
|
|
|
2015-05-15 19:12:11 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Get sections.
|
2015-05-15 19:12:11 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_sections() {
|
|
|
|
$sections = array(
|
2015-05-18 18:23:05 +00:00
|
|
|
'' => __( 'Settings', 'woocommerce' ),
|
|
|
|
'keys' => __( 'Keys/Apps', 'woocommerce' ),
|
2016-08-27 01:46:45 +00:00
|
|
|
'webhooks' => __( 'Webhooks', 'woocommerce' ),
|
2015-05-15 19:12:11 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections );
|
|
|
|
}
|
|
|
|
|
2015-05-15 19:35:50 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Get settings array.
|
2015-05-15 19:35:50 +00:00
|
|
|
*
|
2017-06-27 13:58:33 +00:00
|
|
|
* @param string $current_section
|
2015-05-15 19:35:50 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-06-27 13:58:33 +00:00
|
|
|
public function get_settings( $current_section = '' ) {
|
|
|
|
$settings = array();
|
|
|
|
|
|
|
|
if ( '' === $current_section ) {
|
|
|
|
$settings = apply_filters( 'woocommerce_settings_rest_api', array(
|
|
|
|
array(
|
|
|
|
'title' => __( 'General options', 'woocommerce' ),
|
|
|
|
'type' => 'title',
|
|
|
|
'desc' => '',
|
|
|
|
'id' => 'general_options',
|
|
|
|
),
|
|
|
|
|
|
|
|
array(
|
|
|
|
'title' => __( 'API', 'woocommerce' ),
|
|
|
|
'desc' => __( 'Enable the REST API', 'woocommerce' ),
|
|
|
|
'id' => 'woocommerce_api_enabled',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'default' => 'yes',
|
|
|
|
),
|
|
|
|
|
|
|
|
array(
|
|
|
|
'type' => 'sectionend',
|
|
|
|
'id' => 'general_options',
|
|
|
|
),
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings, $current_section );
|
2015-05-15 19:35:50 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 19:24:32 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Form method.
|
2015-01-06 19:24:32 +00:00
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function form_method( $method ) {
|
2015-05-15 19:12:11 +00:00
|
|
|
global $current_section;
|
|
|
|
|
2015-05-15 19:35:50 +00:00
|
|
|
if ( 'webhooks' == $current_section ) {
|
|
|
|
if ( isset( $_GET['edit-webhook'] ) ) {
|
|
|
|
$webhook_id = absint( $_GET['edit-webhook'] );
|
|
|
|
$webhook = new WC_Webhook( $webhook_id );
|
2015-01-09 17:42:01 +00:00
|
|
|
|
2015-05-15 19:35:50 +00:00
|
|
|
if ( 'trash' != $webhook->post_data->post_status ) {
|
|
|
|
return 'post';
|
|
|
|
}
|
2015-01-09 17:42:01 +00:00
|
|
|
}
|
2015-05-15 19:35:50 +00:00
|
|
|
|
|
|
|
return 'get';
|
2015-01-08 18:34:30 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 02:03:24 +00:00
|
|
|
if ( 'keys' == $current_section ) {
|
|
|
|
if ( isset( $_GET['create-key'] ) || isset( $_GET['edit-key'] ) ) {
|
|
|
|
return 'post';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'get';
|
|
|
|
}
|
|
|
|
|
2015-05-15 19:35:50 +00:00
|
|
|
return 'post';
|
2015-01-06 19:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notices.
|
|
|
|
*/
|
2015-01-08 18:34:30 +00:00
|
|
|
private function notices() {
|
2015-05-15 19:12:11 +00:00
|
|
|
if ( isset( $_GET['section'] ) && 'webhooks' == $_GET['section'] ) {
|
2015-05-16 02:03:24 +00:00
|
|
|
WC_Admin_Webhooks::notices();
|
2015-05-15 19:12:11 +00:00
|
|
|
}
|
2015-05-18 18:23:05 +00:00
|
|
|
if ( isset( $_GET['section'] ) && 'keys' == $_GET['section'] ) {
|
|
|
|
WC_Admin_API_Keys::notices();
|
|
|
|
}
|
2015-05-15 19:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Output the settings.
|
2015-05-15 19:12:11 +00:00
|
|
|
*/
|
|
|
|
public function output() {
|
|
|
|
global $current_section;
|
|
|
|
|
|
|
|
if ( 'webhooks' == $current_section ) {
|
2015-05-16 02:03:24 +00:00
|
|
|
WC_Admin_Webhooks::page_output();
|
2016-09-09 00:14:28 +00:00
|
|
|
} elseif ( 'keys' === $current_section ) {
|
2015-05-16 02:03:24 +00:00
|
|
|
WC_Admin_API_Keys::page_output();
|
2015-05-15 19:35:50 +00:00
|
|
|
} else {
|
|
|
|
$settings = $this->get_settings( $current_section );
|
|
|
|
WC_Admin_Settings::output_fields( $settings );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Save settings.
|
2015-05-15 19:35:50 +00:00
|
|
|
*/
|
|
|
|
public function save() {
|
|
|
|
global $current_section;
|
|
|
|
|
|
|
|
if ( apply_filters( 'woocommerce_rest_api_valid_to_save', ! in_array( $current_section, array( 'keys', 'webhooks' ) ) ) ) {
|
|
|
|
$settings = $this->get_settings();
|
|
|
|
WC_Admin_Settings::save_fields( $settings );
|
2015-05-15 19:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-06 19:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
endif;
|
|
|
|
|
2015-05-15 19:12:11 +00:00
|
|
|
return new WC_Settings_Rest_API();
|