woocommerce/includes/admin/settings/class-wc-settings-api.php

164 lines
3.4 KiB
PHP
Raw Normal View History

<?php
/**
2015-11-03 13:53:50 +00:00
* WooCommerce API Settings
*
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin
* @version 2.4.0
*/
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-11-03 13:31:20 +00:00
* WC_Settings_Rest_API.
*/
class WC_Settings_Rest_API extends WC_Settings_Page {
/**
2015-11-03 13:31:20 +00:00
* Constructor.
*/
public function __construct() {
$this->id = 'api';
$this->label = __( 'API', 'woocommerce' );
add_action( 'woocommerce_settings_form_method_tab_' . $this->id, array( $this, 'form_method' ) );
parent::__construct();
$this->notices();
}
/**
2015-11-03 13:31:20 +00:00
* Get sections.
*
* @return array
*/
public function get_sections() {
$sections = array(
'' => __( 'Settings', 'woocommerce' ),
'keys' => __( 'Keys/Apps', 'woocommerce' ),
'webhooks' => __( 'Webhooks', 'woocommerce' ),
);
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
*
* @param string $current_section
2015-05-15 19:35:50 +00:00
* @return array
*/
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-11-03 13:31:20 +00:00
* Form method.
*
* @param string $method
*
* @return string
*/
public function form_method( $method ) {
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';
}
/**
* Notices.
*/
2015-01-08 18:34:30 +00:00
private function notices() {
if ( isset( $_GET['section'] ) && 'webhooks' == $_GET['section'] ) {
2015-05-16 02:03:24 +00:00
WC_Admin_Webhooks::notices();
}
if ( isset( $_GET['section'] ) && 'keys' == $_GET['section'] ) {
WC_Admin_API_Keys::notices();
}
}
/**
2015-11-03 13:31:20 +00:00
* Output the settings.
*/
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 );
}
}
}
endif;
return new WC_Settings_Rest_API();