212 lines
6.1 KiB
PHP
212 lines
6.1 KiB
PHP
<?php
|
|
/**
|
|
* Theme Helper Functions
|
|
*
|
|
* Customizer output helpers, custom logo, pagination, auth gates,
|
|
* honeypot, and CSV utility shared across the theme.
|
|
*
|
|
* Navigation walker and primary menu fallback have been extracted to
|
|
* inc/nav-walker.php (v2.3.0) to keep this file under 300 lines.
|
|
*
|
|
* @see functions.php — includes this file (after nav-walker.php)
|
|
* @see inc/nav-walker.php — Peptide_Starter_Nav_Walker + menu fallback
|
|
* @see header.php — uses peptide_starter_the_custom_logo()
|
|
* @see page-subject-log.php, page-tracker.php, page-protocol-builder.php —
|
|
* use peptide_starter_require_login()
|
|
*
|
|
* What: Utility functions for template output, theme mods, auth gating.
|
|
* Who calls it: Templates and handler files.
|
|
* Dependencies: WordPress Customizer API, email-verification.
|
|
*
|
|
* @package peptide-starter
|
|
*/
|
|
|
|
// Exit if accessed directly.
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Display custom logo with fallback to site name link.
|
|
*
|
|
* @return void
|
|
*/
|
|
function peptide_starter_the_custom_logo() {
|
|
if ( function_exists( 'the_custom_logo' ) && has_custom_logo() ) {
|
|
the_custom_logo();
|
|
return;
|
|
}
|
|
// Use SVG logo if available
|
|
$logo_path = get_template_directory() . '/assets/brand/logo-horizontal.svg';
|
|
if ( file_exists( $logo_path ) ) {
|
|
echo '<a class="site-logo" href="' . esc_url( home_url( '/' ) ) . '" rel="home" aria-label="' . esc_attr( get_bloginfo( 'name' ) ) . '">';
|
|
include $logo_path;
|
|
echo '</a>';
|
|
return;
|
|
}
|
|
// Fallback to text
|
|
$site_name = get_bloginfo( 'name' );
|
|
echo '<a class="site-logo" href="' . esc_url( home_url( '/' ) ) . '" rel="home">' . esc_html( $site_name ) . '</a>';
|
|
}
|
|
|
|
/**
|
|
* Get hero title from Customizer.
|
|
*
|
|
* @return string Hero title text.
|
|
*/
|
|
function peptide_starter_get_hero_title() {
|
|
return get_theme_mod( 'hero_title', esc_html__( 'Is this peptide right for you?', 'peptide-starter' ) );
|
|
}
|
|
|
|
/**
|
|
* Get hero subtitle from Customizer.
|
|
*
|
|
* @return string Hero subtitle text.
|
|
*/
|
|
function peptide_starter_get_hero_subtitle() {
|
|
return get_theme_mod( 'hero_subtitle', esc_html__( 'What the research really says', 'peptide-starter' ) );
|
|
}
|
|
|
|
/**
|
|
* Get search placeholder from Customizer.
|
|
*
|
|
* @return string Search placeholder text.
|
|
*/
|
|
function peptide_starter_get_search_placeholder() {
|
|
return get_theme_mod( 'hero_search_placeholder', esc_html__( 'Search peptides, sequences, or research...', 'peptide-starter' ) );
|
|
}
|
|
|
|
/**
|
|
* Get footer copyright text from Customizer.
|
|
*
|
|
* @return string Copyright text (may contain safe HTML).
|
|
*/
|
|
function peptide_starter_get_footer_copyright() {
|
|
return get_theme_mod( 'footer_copyright', esc_html__( 'Copyright \u00a9 2026 Peptide Repo. All rights reserved.', 'peptide-starter' ) );
|
|
}
|
|
|
|
/**
|
|
* Whether to render the newsletter signup section. Filterable.
|
|
*
|
|
* @return bool
|
|
*/
|
|
function peptide_starter_show_newsletter_form() {
|
|
return apply_filters( 'peptide_starter_show_newsletter', true );
|
|
}
|
|
|
|
/**
|
|
* Render pagination for archive pages.
|
|
*
|
|
* @return void
|
|
*/
|
|
function peptide_starter_pagination() {
|
|
global $wp_query;
|
|
|
|
if ( $wp_query->max_num_pages <= 1 ) {
|
|
return;
|
|
}
|
|
|
|
$current = max( 1, get_query_var( 'paged' ) );
|
|
$total = $wp_query->max_num_pages;
|
|
|
|
echo '<nav class="ps-pagination">';
|
|
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- paginate_links returns safe HTML.
|
|
echo paginate_links(
|
|
array(
|
|
'base' => get_pagenum_link( 1 ) . '%_%',
|
|
'format' => 'page/%#%/',
|
|
'current' => $current,
|
|
'total' => $total,
|
|
'prev_text' => esc_html__( '\u2190 Previous', 'peptide-starter' ),
|
|
'next_text' => esc_html__( 'Next \u2192', 'peptide-starter' ),
|
|
'type' => 'plain',
|
|
)
|
|
);
|
|
echo '</nav>';
|
|
}
|
|
|
|
/**
|
|
* Whether a plugin is active by its main file path.
|
|
*
|
|
* @param string $plugin Plugin file path relative to plugins/.
|
|
* @return bool
|
|
*/
|
|
function peptide_starter_is_plugin_active( $plugin ) {
|
|
return in_array( $plugin, (array) get_option( 'active_plugins', array() ), true );
|
|
}
|
|
|
|
/**
|
|
* Safe wp_get_referer fallback — never returns empty.
|
|
*
|
|
* @return string Referer or home_url('/').
|
|
*/
|
|
function peptide_starter_safe_referer() {
|
|
$ref = wp_get_referer();
|
|
return $ref ? $ref : home_url( '/' );
|
|
}
|
|
|
|
/**
|
|
* Gate a template behind login + verified email.
|
|
*
|
|
* Unauthenticated \u2192 redirect to /auth?redirect_to=current-uri.
|
|
* Logged in but unverified \u2192 redirect to /profile?verify_required=1.
|
|
*
|
|
* Side effects: emits 302 and exits the request.
|
|
*
|
|
* @param string|null $redirect_to Optional override for post-login target.
|
|
* @return void
|
|
*/
|
|
function peptide_starter_require_login( $redirect_to = null ) {
|
|
if ( ! is_user_logged_in() ) {
|
|
$target = $redirect_to ? $redirect_to : ( isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '/' );
|
|
wp_safe_redirect( home_url( '/auth?redirect_to=' . rawurlencode( $target ) ) );
|
|
exit;
|
|
}
|
|
|
|
if ( function_exists( 'peptide_starter_user_is_verified' ) && ! peptide_starter_user_is_verified( get_current_user_id() ) ) {
|
|
wp_safe_redirect( home_url( '/profile?verify_required=1' ) );
|
|
exit;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Render a honeypot input block for a given form.
|
|
*
|
|
* Pair with peptide_starter_honeypot_triggered() in the handler.
|
|
*
|
|
* @param string $form_name Short slug embedded in the field name.
|
|
* @return void Echoes HTML.
|
|
*/
|
|
function peptide_starter_render_honeypot( $form_name ) {
|
|
$field = 'ps_hp_' . preg_replace( '/[^a-z0-9_]/', '', $form_name );
|
|
?>
|
|
<div class="ps-hp-wrap" aria-hidden="true">
|
|
<label>
|
|
<?php esc_html_e( 'Leave this field empty', 'peptide-starter' ); ?>
|
|
<input type="text" name="<?php echo esc_attr( $field ); ?>" tabindex="-1" autocomplete="off" value="">
|
|
</label>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
/**
|
|
* CSV-injection-safe value for fputcsv output.
|
|
*
|
|
* Prefixes dangerous leading characters (=, +, -, @, tab, CR) with a
|
|
* single quote so Excel/Sheets won't interpret the cell as a formula.
|
|
*
|
|
* @param string $value Raw cell value.
|
|
* @return string Safe cell value.
|
|
*/
|
|
function peptide_starter_csv_safe( $value ) {
|
|
$value = (string) $value;
|
|
if ( '' === $value ) {
|
|
return $value;
|
|
}
|
|
$dangerous = array( '=', '+', '-', '@', "\t", "\r" );
|
|
if ( in_array( $value[0], $dangerous, true ) ) {
|
|
return "'" . $value;
|
|
}
|
|
return $value;
|
|
}
|