<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }

function lt_core_defaults() {
    return array(
        'firm_name' => '', 'legal_name' => '', 'short_name' => '', 'tagline' => '', 'description' => '', 'founded_year' => '',
        'registration_details' => '', 'regulator_details' => '', 'main_office_id' => 0,
        'general_email' => '', 'phone_primary' => '', 'phone_secondary' => '', 'emergency_line' => '', 'office_hours' => '',
        'postal_address' => '', 'digital_address' => '',
        'linkedin' => '', 'twitter' => '', 'facebook' => '', 'instagram' => '', 'youtube' => '', 'other_networks' => '',
        'privacy_page_id' => 0, 'disclaimer_page_id' => 0, 'cookie_page_id' => 0,
        'copyright' => '', 'enquiry_recipient' => get_option( 'admin_email' ), 'enquiry_cc' => '',
        'enquiry_subject_prefix' => __( 'Website enquiry', 'law-temple-core' ),
        'enquiry_confirmation' => __( 'Thank you. Your enquiry has been received.', 'law-temple-core' ),
        'external_form_url' => '', 'consent_text' => __( 'I have read the Privacy Notice and consent to the processing of the information submitted for the purpose of responding to this enquiry.', 'law-temple-core' ),
        'allow_file_upload' => 0, 'schema_enabled' => 1, 'analytics_id' => '', 'gtm_id' => '', 'tracking_requires_consent' => 1,
        'label_practice' => __( 'Practice Areas', 'law-temple-core' ), 'label_people' => __( 'Our People', 'law-temple-core' ),
        'label_insights' => __( 'Insights', 'law-temple-core' ), 'label_sectors' => __( 'Sectors', 'law-temple-core' ),
        'label_matters' => __( 'Experience', 'law-temple-core' ),
        'search_types' => 'page,practice,lawyer,insight,matter,office,career',
        'search_max_results' => 8, 'search_include_metadata' => 1, 'search_include_relationships' => 1,
    );
}
function lt_core_options() { return wp_parse_args( get_option( 'lt_core_options', array() ), lt_core_defaults() ); }
function lt_core_option( $key, $fallback = '' ) { $o = lt_core_options(); return array_key_exists( $key, $o ) ? $o[ $key ] : $fallback; }
function lt_core_csv_ids( $value ) {
    if ( is_array( $value ) ) { return array_values( array_filter( array_map( 'absint', $value ) ) ); }
    if ( ! $value ) { return array(); }
    return array_values( array_filter( array_map( 'absint', preg_split( '/\s*,\s*/', (string) $value ) ) ) );
}
function lt_core_sanitize_csv_ids( $value ) { return implode( ',', lt_core_csv_ids( $value ) ); }
function lt_core_bool( $value ) { return empty( $value ) ? 0 : 1; }
function lt_core_get_related_ids( $post_id, $key ) { return lt_core_csv_ids( get_post_meta( $post_id, $key, true ) ); }
function lt_core_get_related_posts( $post_id, $key, $post_type, $limit = -1 ) {
    $ids = lt_core_get_related_ids( $post_id, $key );
    if ( ! $ids ) { return array(); }
    return get_posts( array( 'post_type'=>$post_type, 'post__in'=>$ids, 'orderby'=>'post__in', 'posts_per_page'=>$limit, 'post_status'=>'publish' ) );
}
function lt_core_label( $which, $fallback ) { $v = lt_core_option( 'label_' . $which, $fallback ); return $v ?: $fallback; }
function lt_core_url_allowed( $url ) { return esc_url_raw( $url ); }
function lt_core_safe_email_list( $value ) {
    $emails = array_filter( array_map( 'sanitize_email', preg_split( '/[;,\s]+/', (string) $value ) ) );
    return implode( ',', $emails );
}
function lt_core_get_firm_name() { return lt_core_option( 'firm_name' ) ?: get_bloginfo( 'name' ); }
function lt_core_post_is_public( $post_id ) { return 'publish' === get_post_status( $post_id ); }
function lt_core_has_seo_plugin() {
    return defined( 'WPSEO_VERSION' ) || defined( 'RANK_MATH_VERSION' ) || defined( 'SEOPRESS_VERSION' );
}
function lt_core_meta_textarea( $post_id, $key ) { return trim( (string) get_post_meta( $post_id, $key, true ) ); }
