Skip to content

Commit

Permalink
Add panel option.
Browse files Browse the repository at this point in the history
  • Loading branch information
fumikito committed May 26, 2020
1 parent 0d6fe71 commit 51b16c1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 9 deletions.
20 changes: 20 additions & 0 deletions src/Kunoichi/ThemeCustomizer/CustomizerSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ abstract class CustomizerSetting extends Singleton {

use Utilities;

protected $panel_id = '';

protected $section_id = '';

/**
Expand Down Expand Up @@ -44,9 +46,17 @@ protected function after_init() {
* @param \WP_Customize_Manager $wp_customizer
*/
public function customize_register( $wp_customizer ) {
// Register panel.
$panel_setting = $this->panel_settings();
if ( $panel_setting ) {
$wp_customizer->add_panel( $this->panel_id, $panel_setting );
}
// Register setting if required.
$section_setting = $this->section_setting();
if ( $section_setting ) {
if ( $this->panel_id ) {
$section_setting[ 'panel' ] = $this->panel_id;
}
$wp_customizer->add_section( $this->get_section(), $section_setting );
}
// Register all fields.
Expand Down Expand Up @@ -76,6 +86,16 @@ protected function register_field( &$wp_customizer, $id, $args ) {
$wp_customizer->add_control( new $control_class( $wp_customizer, $id, $args ) );
}

/**
* If proper array returned, register panel.
*
* @see \WP_Customize_Manager::add_panel
* @return array
*/
protected function panel_settings() {
return [];
}

/**
* If array returned, register section.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/src/Kunoichi/ThemeCustomizerTest/NestedCustomizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Kunoichi\ThemeCustomizerTest;


use Kunoichi\ThemeCustomizer\CustomizerSetting;

class NestedCustomizer extends CustomizerSetting {

protected $panel_id = 'nested';

protected $section_id = 'nested_child';

protected function section_setting() {
return [
'title' => 'Nested Section',
];
}

protected function panel_settings() {
return [
'title' => 'Nested Panel',
'description' => 'This panel is added by Kunoichi Theme Customizer.',
'priority' => 200,
];
}

protected function get_fields() {
return [
'nested_section_1' => [
'label' => 'Nested Field 1',
],
'nested_section_2' => [
'label' => 'Nested Field 2',
],
];
}


}
6 changes: 3 additions & 3 deletions tests/src/Kunoichi/ThemeCustomizerTest/SimpleCustomizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class SimpleCustomizer extends CustomizerSetting {

protected $section_id = 'site-setting';
protected $section_id = 'title_tagline';


/**
Expand All @@ -21,8 +21,8 @@ class SimpleCustomizer extends CustomizerSetting {
protected function get_fields() {
return [
'site_setting_title' => [
'label' => __( 'Title' ),
'description' => '',
'label' => 'Added Title',
'description' => 'This section is added by Kunoichi Theme Customizer.',
],
];
}
Expand Down
1 change: 0 additions & 1 deletion tests/test-utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ public function test_translation() {
switch_to_locale( 'ja' );
$this->assertEquals( 'メタ情報とSEO', __( 'Meta and SEO', 'theme-customizer' ) );
}

}
12 changes: 7 additions & 5 deletions theme-customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
if ( ! defined( 'ABSPATH' ) ) {
die( 'Invalid request.' );
}

require __DIR__ . '/vendor/autoload.php';

\Kunoichi\ThemeCustomizer::load_locale( get_locale() );

$auto_loaded = [ 'Patterns' ];
foreach ( $auto_loaded as $dir ) {
$path = __DIR__ . '/src/Kunoichi/ThemeCustomizer/' . $dir;
$auto_loaded = [
[ __DIR__ . '/src', 'Kunoichi/ThemeCustomizer/Patterns' ],
[ __DIR__ . '/tests/src/', 'Kunoichi/ThemeCustomizerTest' ],
];
foreach ( $auto_loaded as list( $base, $dir ) ) {
$path = $base . '/' . $dir;
if ( ! is_dir( $path ) ) {
continue;
}
Expand All @@ -32,7 +34,7 @@
continue;
}
list( $file, $class_name ) = $match;
$class_name = "Kunoichi\\ThemeCustomizer\\{$dir}\\{$class_name}";
$class_name = str_replace( '/', "\\", $dir ) . "\\" . $class_name;
if ( ! class_exists( $class_name ) ) {
continue;
}
Expand Down

0 comments on commit 51b16c1

Please sign in to comment.