9f92958651
This commit introduces a complete internal refactoring of the theme architecture, transitioning from a monolithic functions.php structure to a modular, scalable system. ### Key Changes #### 1. Modular Architecture - Extracted core logic into dedicated modules under `/inc/` - Introduced clear separation of concerns: - `helpers/` → shared utility functions - `performance/` → optimization and media processing logic - `customizer/` → fully modularized Customizer structure - `template-*` → presentation and template-related logic #### 2. Customizer Refactor - Replaced legacy monolithic customizer with modular system: - `core.php` → base setup - `general.php`, `layout.php`, `toc.php`, `social.php` → feature-based modules - Improved maintainability and extensibility for future settings #### 3. Performance Layer Separation - Moved performance-related logic into `/inc/performance/` - Clearly separated: - frontend performance tweaks (scripts, CSS, cleanup) - media optimization tools (batch processing, cleanup UI) - Reduced coupling between UI, logic, and processing #### 4. Media Optimization Pipeline Stabilization - Re-aligned upload pipeline with WordPress native flow - Restored proper use of `wp_generate_attachment_metadata` - Ensured compatibility with: - format conversion (AVIF/WebP) - responsive image generation - WordPress core image handling #### 5. Cleaner Hook Management - Removed duplicated hooks and redundant filters - Standardized hook priorities and responsibilities - Ensured consistent use of feature toggles (`get_theme_mod`) #### 6. Improved Code Quality - Reduced function duplication - Standardized naming conventions - Improved readability and inline documentation - Removed legacy patterns and implicit dependencies #### 7. Asset Handling Improvements - Introduced centralized asset versioning via `filemtime` - Improved script loading strategy (defer non-critical JS) - Cleaner enqueue structure for styles and scripts #### 8. Foundation for Future Development This refactor lays the groundwork for: - easier feature expansion - better testability - improved debugging capabilities - long-term maintainability ### Breaking Changes - Internal file structure has changed significantly - Direct modifications to old functions.php logic may no longer apply - Customizer extensions must now hook into modular structure --- This is a purely structural and architectural release. No intentional changes to frontend behavior were introduced, but the internal system is now significantly more robust and maintainable.
245 lines
5.7 KiB
PHP
245 lines
5.7 KiB
PHP
<?php
|
|
/**
|
|
* Theme Customizer Core
|
|
*
|
|
* @package zeitfresser
|
|
*/
|
|
|
|
function zeitfresser_customize_register( $wp_customize ) {
|
|
|
|
// Live Preview support
|
|
$wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
|
|
$wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
|
|
$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
|
|
|
|
if ( isset( $wp_customize->selective_refresh ) ) {
|
|
$wp_customize->selective_refresh->add_partial(
|
|
'blogname',
|
|
array(
|
|
'selector' => '.site-title a',
|
|
'render_callback' => 'zeitfresser_customize_partial_blogname',
|
|
)
|
|
);
|
|
|
|
$wp_customize->selective_refresh->add_partial(
|
|
'blogdescription',
|
|
array(
|
|
'selector' => '.site-description',
|
|
'render_callback' => 'zeitfresser_customize_partial_blogdescription',
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Performance Tools Section
|
|
*/
|
|
$wp_customize->add_section(
|
|
'ztfr_performance_tools',
|
|
array(
|
|
'title' => 'Performance Tools Settings',
|
|
'priority' => 160,
|
|
)
|
|
);
|
|
|
|
/**
|
|
* Auto Optimize
|
|
*/
|
|
$wp_customize->add_setting(
|
|
'ztfr_auto_optimize',
|
|
array(
|
|
'default' => true,
|
|
'sanitize_callback' => 'wp_validate_boolean',
|
|
)
|
|
);
|
|
|
|
$wp_customize->add_control(
|
|
'ztfr_auto_optimize',
|
|
array(
|
|
'type' => 'checkbox',
|
|
'section' => 'ztfr_performance_tools',
|
|
'label' => 'Auto Optimize Pictures on Upload',
|
|
'description' => 'Automatically converts images to AVIF/WebP.',
|
|
)
|
|
);
|
|
|
|
/**
|
|
* Auto Delete
|
|
*/
|
|
$wp_customize->add_setting(
|
|
'ztfr_auto_delete',
|
|
array(
|
|
'default' => false,
|
|
'sanitize_callback' => 'wp_validate_boolean',
|
|
)
|
|
);
|
|
|
|
$wp_customize->add_control(
|
|
'ztfr_auto_delete',
|
|
array(
|
|
'type' => 'checkbox',
|
|
'section' => 'ztfr_performance_tools',
|
|
'label' => 'Auto Delete Original Pictures',
|
|
'description' => 'Deletes originals after optimization.',
|
|
)
|
|
);
|
|
}
|
|
add_action( 'customize_register', 'zeitfresser_customize_register' );
|
|
|
|
/**
|
|
* Partial refresh helpers
|
|
*/
|
|
function zeitfresser_customize_partial_blogname() {
|
|
bloginfo( 'name' );
|
|
}
|
|
|
|
function zeitfresser_customize_partial_blogdescription() {
|
|
bloginfo( 'description' );
|
|
}
|
|
|
|
/**
|
|
* Live preview JS
|
|
*/
|
|
function zeitfresser_customize_preview_js() {
|
|
wp_enqueue_script(
|
|
'zeitfresser-customizer',
|
|
get_template_directory_uri() . '/js/customizer.js',
|
|
array( 'customize-preview' ),
|
|
ZEITFRESSER_VERSION,
|
|
true
|
|
);
|
|
}
|
|
add_action( 'customize_preview_init', 'zeitfresser_customize_preview_js' );
|
|
|
|
/**
|
|
* Dependency UI logic
|
|
*/
|
|
function zeitfresser_customize_controls_dependency_js() {
|
|
?>
|
|
<script>
|
|
(function() {
|
|
|
|
function getOptimizeInput() {
|
|
return document.querySelector('#customize-control-ztfr_auto_optimize input');
|
|
}
|
|
|
|
function getDeleteInput() {
|
|
return document.querySelector('#customize-control-ztfr_auto_delete input');
|
|
}
|
|
|
|
function getDeleteControl() {
|
|
return document.getElementById('customize-control-ztfr_auto_delete');
|
|
}
|
|
|
|
function ensureStatusBox() {
|
|
|
|
let box = document.getElementById('ztfr-auto-status-box');
|
|
|
|
if (box) return box;
|
|
|
|
const optimizeControl = document.getElementById('customize-control-ztfr_auto_optimize');
|
|
|
|
if (!optimizeControl || !optimizeControl.parentNode) return null;
|
|
|
|
box = document.createElement('li');
|
|
box.id = 'ztfr-auto-status-box';
|
|
box.className = 'customize-control';
|
|
box.innerHTML =
|
|
'<span style="display:block;font-weight:600;margin-bottom:6px;">Current Mode</span>' +
|
|
'<span id="ztfr-auto-status-text">Checking...</span>';
|
|
|
|
optimizeControl.parentNode.insertBefore(box, optimizeControl);
|
|
|
|
return box;
|
|
}
|
|
|
|
function updateState() {
|
|
const optimizeInput = getOptimizeInput();
|
|
const deleteInput = getDeleteInput();
|
|
const deleteControl = getDeleteControl();
|
|
const statusBox = ensureStatusBox();
|
|
const statusText = document.getElementById('ztfr-auto-status-text');
|
|
|
|
if (!optimizeInput || !deleteInput || !deleteControl || !statusBox || !statusText) {
|
|
return;
|
|
}
|
|
|
|
if (!optimizeInput.checked) {
|
|
deleteInput.checked = false;
|
|
deleteInput.disabled = true;
|
|
deleteControl.style.opacity = '0.5';
|
|
statusText.textContent = '⚪ Manual Mode (no automation)';
|
|
} else {
|
|
deleteInput.disabled = false;
|
|
deleteControl.style.opacity = '1';
|
|
|
|
if (deleteInput.checked) {
|
|
statusText.textContent = '🟢 Full Auto Mode (optimize + delete)';
|
|
} else {
|
|
statusText.textContent = '🟡 Auto Optimize enabled (originals kept)';
|
|
}
|
|
}
|
|
}
|
|
|
|
function init() {
|
|
|
|
let attempts = 0;
|
|
|
|
function tryInit() {
|
|
const optimize = getOptimizeInput();
|
|
const del = getDeleteInput();
|
|
|
|
if (optimize && del) {
|
|
updateState();
|
|
return;
|
|
}
|
|
|
|
// Retry max 10x
|
|
if (attempts < 10) {
|
|
attempts++;
|
|
setTimeout(tryInit, 200);
|
|
}
|
|
}
|
|
|
|
tryInit();
|
|
|
|
document.addEventListener('change', function(e) {
|
|
if (
|
|
e.target &&
|
|
(
|
|
e.target.matches('#customize-control-ztfr_auto_optimize input') ||
|
|
e.target.matches('#customize-control-ztfr_auto_delete input')
|
|
)
|
|
) {
|
|
updateState();
|
|
}
|
|
});
|
|
}
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', init);
|
|
} else {
|
|
init();
|
|
}
|
|
|
|
})();
|
|
</script>
|
|
<?php
|
|
}
|
|
add_action( 'customize_controls_enqueue_scripts', 'zeitfresser_customize_controls_dependency_js' );
|
|
|
|
/**
|
|
* Small UI polish
|
|
*/
|
|
add_action( 'customize_controls_enqueue_scripts', function() {
|
|
?>
|
|
<style>
|
|
#customize-control-ztfr_auto_optimize > label,
|
|
#customize-control-ztfr_auto_delete > label {
|
|
display:flex;
|
|
align-items:flex-start;
|
|
gap:6px;
|
|
}
|
|
</style>
|
|
<?php
|
|
});
|