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

110 lines
2.3 KiB
PHP
Raw Normal View History

2013-07-26 14:36:28 +00:00
<?php
/**
* WooCommerce Settings Page/Tab
*
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin
2013-07-26 14:36:28 +00:00
* @version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
2013-07-26 14:36:28 +00:00
if ( ! class_exists( 'WC_Settings_Page' ) ) :
/**
2015-11-03 13:31:20 +00:00
* WC_Settings_Page.
2013-07-26 14:36:28 +00:00
*/
abstract class WC_Settings_Page {
2013-07-26 14:36:28 +00:00
protected $id = '';
protected $label = '';
/**
2015-11-03 13:31:20 +00:00
* Constructor.
*/
public function __construct() {
add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) );
add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );
}
2013-07-26 14:36:28 +00:00
/**
2015-11-03 13:31:20 +00:00
* Add this page to settings.
2013-07-26 14:36:28 +00:00
*/
public function add_settings_page( $pages ) {
$pages[ $this->id ] = $this->label;
return $pages;
}
/**
2015-11-03 13:31:20 +00:00
* Get settings array.
2013-07-26 14:36:28 +00:00
*
* @return array
*/
public function get_settings() {
return apply_filters( 'woocommerce_get_settings_' . $this->id, array() );
2013-07-26 14:36:28 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Get sections.
2013-07-26 14:36:28 +00:00
*
* @return array
*/
public function get_sections() {
return apply_filters( 'woocommerce_get_sections_' . $this->id, array() );
2013-07-26 14:36:28 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Output sections.
2013-07-26 14:36:28 +00:00
*/
public function output_sections() {
global $current_section;
$sections = $this->get_sections();
if ( empty( $sections ) || 1 === sizeof( $sections ) ) {
2013-07-26 14:36:28 +00:00
return;
}
2013-07-26 14:36:28 +00:00
echo '<ul class="subsubsub">';
$array_keys = array_keys( $sections );
foreach ( $sections as $id => $label ) {
2013-10-22 16:26:18 +00:00
echo '<li><a href="' . admin_url( 'admin.php?page=wc-settings&tab=' . $this->id . '&section=' . sanitize_title( $id ) ) . '" class="' . ( $current_section == $id ? 'current' : '' ) . '">' . $label . '</a> ' . ( end( $array_keys ) == $id ? '' : '|' ) . ' </li>';
}
2013-07-26 14:36:28 +00:00
echo '</ul><br class="clear" />';
}
/**
2015-11-03 13:31:20 +00:00
* Output the settings.
2013-07-26 14:36:28 +00:00
*/
public function output() {
$settings = $this->get_settings();
WC_Admin_Settings::output_fields( $settings );
}
/**
2015-11-03 13:31:20 +00:00
* Save settings.
2013-07-26 14:36:28 +00:00
*/
public function save() {
global $current_section;
$settings = $this->get_settings();
WC_Admin_Settings::save_fields( $settings );
if ( $current_section ) {
do_action( 'woocommerce_update_options_' . $this->id . '_' . $current_section );
}
2013-07-26 14:36:28 +00:00
}
}
endif;