Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
WIP: Refactosrs partly Customizer & Kirki usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ouun committed May 20, 2020
1 parent c83b721 commit cc2db4c
Show file tree
Hide file tree
Showing 28 changed files with 719 additions and 374 deletions.
109 changes: 104 additions & 5 deletions app/Customizer/Customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

namespace Stage\Customizer;

use Kirki\Compatibility\Kirki;
use Stage\Customizer\Panels\ArchivesPanel;
use Stage\Customizer\Panels\ColorsPanel;
use Stage\Customizer\Panels\FooterPanel;
use Stage\Customizer\Panels\GlobalPanel;
use Stage\Customizer\Panels\HeaderPanel;
use Stage\Customizer\Panels\SettingsPanel;
use Stage\Customizer\Panels\WebsitePanel;
use Stage\Customizer\Panels\WebsiteFeatures;
use Roots\Acorn\ServiceProvider;
use Stage\View\Composers\Partials\Archive;
use WP_Customize_Manager;

use function Stage\stage_get_default;
Expand All @@ -27,7 +30,7 @@ public function boot()
add_filter('kirki_use_local_fonts', '__return_true');

// Do not inline CSS, build with action
add_filter('kirki_output_inline_styles', '__return_false');
add_filter('kirki_output_inline_styles', '__return_true');

// Fix Kirki URL handling for local development, if in symlink folder
add_filter(
Expand Down Expand Up @@ -90,13 +93,13 @@ function ($standard_fonts) {
'after_setup_theme',
function () {
$modules = array(
'core' => '\Kirki\Compatibility\Kirki',
// 'core' => '\Kirki\Compatibility\Kirki',
'postMessage' => '\Kirki\Module\Postmessage',
'css' => '\Kirki\Module\CSS',
'selective-refresh' => '\Kirki\Module\Selective_Refresh',
'field-dependencies' => '\Kirki\Module\Field_Dependencies',
// 'field-dependencies' => '\Kirki\Module\Field_Dependencies',
'webfonts' => '\Kirki\Module\Webfonts',
'tooltips' => '\Kirki\Module\Tooltips',
// 'tooltips' => '\Kirki\Module\Tooltips',
'sync' => '\Kirki\Module\Sync',
'font-uploads' => '\Kirki\Module\FontUploads',
);
Expand Down Expand Up @@ -159,9 +162,21 @@ function (WP_Customize_Manager $wp_customize) {
add_action(
'after_setup_theme',
function () {

/**
* Set default config is none is defined for field
*/
self::addConfig('stage', [
'capability' => 'edit_theme_options',
'option_type' => 'theme_mod',
'gutenberg_support' => true,
'disable_output' => false,
]);

new WebsitePanel(); // HIDES AND MOVES CONTROLS!
new WebsiteFeatures(); // HIDES AND MOVES CONTROLS!
new SettingsPanel();
new WebsiteFeatures();
new ColorsPanel();
new GlobalPanel();
new HeaderPanel();
new ArchivesPanel();
Expand All @@ -186,6 +201,11 @@ function () {
null,
true
);

// Collect data for 'stage' JS object via 'stage_localize_script' filter
wp_localize_script('stage/customize-controls.js', 'stage', [
'archives' => Archive::registeredArchives(),
]);
}
);

Expand Down Expand Up @@ -224,4 +244,83 @@ function () {
20
);
}


/**
* Proxy function for Kirki Config.
*
* @static
* @access public
* @since 1.0
* @param string $id The config ID.
* @param array $args The config arguments.
* @return void
*/
public static function addConfig($id, $args = [])
{

$args = wp_parse_args($args, [
'capability' => 'edit_theme_options',
'option_type' => 'theme_mod',
'gutenberg_support' => true,
'disable_output' => false,
]);

Kirki::add_config($id, apply_filters('stage_customizer_config_args', $args));
}


/**
* Proxy function for Kirki Panel.
*
* @static
* @access public
* @since 1.0
* @param string $id The section ID.
* @param array $args The field arguments.
* @return void
*/
public static function addPanel($id, $args)
{
Kirki::add_panel($id, apply_filters('stage_customizer_panel_args', $args));
}


/**
* Proxy function for Kirki Section.
*
* @static
* @access public
* @since 1.0
* @param string $id The section ID.
* @param array $args The field arguments.
* @return void
*/
public static function addSection($id, $args)
{
Kirki::add_section($id, apply_filters('stage_customizer_section_args', $args));
}


/**
* Proxy function for Kirki Field.
*
* @static
* @access public
* @param string|array $config
* @param array $args The field arguments.
* @return void
* @since 1.0
*/
public static function addField($config, $args = [])
{

// Use global config if none defined
if (is_array($config)) {
$args = $config;
$config = 'stage';
}

Kirki::add_field($config, apply_filters('stage_customizer_field_args', $args));
}
}
66 changes: 24 additions & 42 deletions app/Customizer/Panels/ArchivesPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Stage\Customizer\Panels;

use Kirki\Control\Checkbox_Switch;
use Kirki\Panel;
use Kirki\Section;
use WP_Customize_Manager;
use Stage\View\Composers\Partials\Archive;
use Stage\Customizer\Controls\LayoutControl;
use Stage\Customizer\Controls\ToggleControl;
use Kirki\Compatibility\Kirki;
use Kirki\Section;

use function Stage\stage_get_default;
use function Stage\stage_get_fallback_template;
Expand All @@ -17,33 +17,17 @@ class ArchivesPanel

// Set panel ID
private static $panel = 'archives';
private static $config = 'archives_conf';

public function __construct()
{
/**
* Register Global Config
*/
Kirki::add_config(
self::$config,
array(
'capability' => 'edit_theme_options',
'option_type' => 'theme_mod',
'disable_output' => false,
)
);

/**
* Register Panel
* Set up the panel
*/
// Layout Panel
Kirki::add_panel(
self::$panel,
array(
'priority' => 20,
'title' => esc_attr__('Archives', 'stage'),
)
);
new Panel(self::$panel, array(
'priority' => 20,
'title' => esc_html__('Archives', 'stage'),
));

foreach (Archive::archivesToRegister() as $cpt_name => $cpt_label) {
// Set section & settings ID
Expand Down Expand Up @@ -71,7 +55,7 @@ public function __construct()
'customize_register',
function (WP_Customize_Manager $wp_customize) use ($section, $cpt_name, $cpt_label) {
// Archive layout settings
$id = 'archive.' . $cpt_name . '.layout';
$id = "archive.$cpt_name.layout";
$config = Archive::getPostTypeArchiveConfigKey($cpt_name) . '.layout';
$display_configs['layout'] = $id;

Expand Down Expand Up @@ -116,34 +100,32 @@ function (WP_Customize_Manager $wp_customize) use ($section, $cpt_name, $cpt_lab
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'default' => $display_config,
'transport' => 'postMessage', // Or postMessage.
'sanitize_callback' => array(
'Stage\Customizer\Controls\ToggleControl',
'sanitize_toggle'
),
'transport' => $key == 'display_sidebar' ?
'refresh' :
'postMessage',
'sanitize_callback' => 'Stage\stage_bool_to_string',
'sanitize_js_callback' => 'Stage\stage_string_to_bool',
)
);

$wp_customize->add_control(
new ToggleControl(
$wp_customize,
$id,
array(
'label' => __(ucwords(str_replace('_', ' ', $key)), 'stage'),
'section' => $section,
)
)
);
// Add control.
$wp_customize->add_control(new Checkbox_Switch($wp_customize, $id, [
'section' => $section,
'choices' => [
'on' => __(ucwords(str_replace('_', ' ', $key)), 'stage'),
'off' => __(ucwords(str_replace('_', ' ', $key)), 'stage'),
],
]));
}

// Add common refresh partial
if (! empty($display_configs)) {
$wp_customize->selective_refresh->add_partial(
$id,
array(
'selector' => 'body.' . $cpt_name . '-archive .archive-wrap',
'selector' => 'body.' . $cpt_name . '-archive .archive-wrap .wrap-inner',
'settings' => array_values($display_configs),
'container_inclusive' => false,
'container_inclusive' => true,
'render_callback' => function () use ($cpt_name) {
// Get $layout based on available setting
$chosen_key = 'archive.' . $cpt_name . '.layout'; // theme_mod() or option() key
Expand Down
Loading

0 comments on commit cc2db4c

Please sign in to comment.