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

function lt_core_meta_schema() {
    return array(
        'lawyer'=>array(
            'lt_role'=>array('label'=>__('Professional title','law-temple-core'),'type'=>'text'),
            'lt_position'=>array('label'=>__('Position','law-temple-core'),'type'=>'text'),
            'lt_classification'=>array('label'=>__('Classification','law-temple-core'),'type'=>'select','options'=>array(''=>__('Not specified','law-temple-core'),'partner'=>__('Partner','law-temple-core'),'associate'=>__('Associate','law-temple-core'),'counsel'=>__('Counsel','law-temple-core'),'professional'=>__('Professional','law-temple-core'))),
            'lt_alt_image_id'=>array('label'=>__('Alternative image','law-temple-core'),'type'=>'media'),
            'lt_short_bio'=>array('label'=>__('Short biography','law-temple-core'),'type'=>'textarea'),
            'lt_email'=>array('label'=>__('Email','law-temple-core'),'type'=>'email'),
            'lt_phone'=>array('label'=>__('Telephone','law-temple-core'),'type'=>'text'),
            'lt_mobile'=>array('label'=>__('Mobile number','law-temple-core'),'type'=>'text'),
            'lt_linkedin'=>array('label'=>__('LinkedIn URL','law-temple-core'),'type'=>'url'),
            'lt_social_urls'=>array('label'=>__('Other approved social links (one per line)','law-temple-core'),'type'=>'textarea'),
            'lt_qualifications'=>array('label'=>__('Qualifications','law-temple-core'),'type'=>'textarea'),
            'lt_education'=>array('label'=>__('Education','law-temple-core'),'type'=>'textarea'),
            'lt_bar_admission'=>array('label'=>__('Bar admissions','law-temple-core'),'type'=>'textarea'),
            'lt_memberships'=>array('label'=>__('Professional memberships','law-temple-core'),'type'=>'textarea'),
            'lt_languages'=>array('label'=>__('Languages','law-temple-core'),'type'=>'textarea'),
            'lt_awards_text'=>array('label'=>__('Awards','law-temple-core'),'type'=>'textarea'),
            'lt_publications_text'=>array('label'=>__('Publications','law-temple-core'),'type'=>'textarea'),
            'lt_experience'=>array('label'=>__('Experience','law-temple-core'),'type'=>'textarea'),
            'lt_representative_matters'=>array('label'=>__('Representative matters','law-temple-core'),'type'=>'textarea'),
            'lt_practice_ids'=>array('label'=>__('Practice Areas','law-temple-core'),'type'=>'relation','post_type'=>'practice'),
            'lt_office_ids'=>array('label'=>__('Offices','law-temple-core'),'type'=>'relation','post_type'=>'office'),
            'lt_cv_id'=>array('label'=>__('Downloadable CV','law-temple-core'),'type'=>'media'),
            'lt_pa_contact'=>array('label'=>__('Personal assistant / contact','law-temple-core'),'type'=>'textarea'),
            'lt_featured'=>array('label'=>__('Featured member','law-temple-core'),'type'=>'checkbox'),
            'lt_profile_visibility'=>array('label'=>__('Profile visibility','law-temple-core'),'type'=>'select','options'=>array('public'=>__('Public','law-temple-core'),'hidden'=>__('Hidden from directory','law-temple-core'))),
        ),
        'practice'=>array(
            'lt_short_title'=>array('label'=>__('Short title','law-temple-core'),'type'=>'text'),
            'lt_icon'=>array('label'=>__('Icon / short symbol','law-temple-core'),'type'=>'text'),
            'lt_intro'=>array('label'=>__('Introductory statement','law-temple-core'),'type'=>'textarea'),
            'lt_services'=>array('label'=>__('Services (one per line)','law-temple-core'),'type'=>'textarea'),
            'lt_capabilities'=>array('label'=>__('Capabilities','law-temple-core'),'type'=>'textarea'),
            'lt_lead_lawyer_id'=>array('label'=>__('Lead lawyer','law-temple-core'),'type'=>'single_relation','post_type'=>'lawyer'),
            'lt_lawyer_ids'=>array('label'=>__('Related lawyers','law-temple-core'),'type'=>'relation','post_type'=>'lawyer'),
            'lt_insight_ids'=>array('label'=>__('Featured related insights','law-temple-core'),'type'=>'relation','post_type'=>'insight'),
            'lt_matter_ids'=>array('label'=>__('Related matters','law-temple-core'),'type'=>'relation','post_type'=>'matter'),
            'lt_faq_ids'=>array('label'=>__('Related FAQs','law-temple-core'),'type'=>'relation','post_type'=>'lt_faq'),
            'lt_cta_heading'=>array('label'=>__('CTA heading','law-temple-core'),'type'=>'text'),
            'lt_cta_description'=>array('label'=>__('CTA description','law-temple-core'),'type'=>'textarea'),
            'lt_cta_label'=>array('label'=>__('CTA button label','law-temple-core'),'type'=>'text'),
            'lt_cta_url'=>array('label'=>__('CTA URL','law-temple-core'),'type'=>'url'),
            'lt_seo_description'=>array('label'=>__('SEO description','law-temple-core'),'type'=>'textarea'),
            'lt_featured'=>array('label'=>__('Featured practice','law-temple-core'),'type'=>'checkbox'),
            'lt_archive_visibility'=>array('label'=>__('Archive visibility','law-temple-core'),'type'=>'select','options'=>array('visible'=>__('Visible','law-temple-core'),'hidden'=>__('Hidden','law-temple-core'))),
        ),
        'insight'=>array(
            'lt_subtitle'=>array('label'=>__('Subtitle','law-temple-core'),'type'=>'text'),
            'lt_external_author'=>array('label'=>__('External author','law-temple-core'),'type'=>'text'),
            'lt_lawyer_ids'=>array('label'=>__('Lawyer authors','law-temple-core'),'type'=>'relation','post_type'=>'lawyer'),
            'lt_practice_ids'=>array('label'=>__('Practice Areas','law-temple-core'),'type'=>'relation','post_type'=>'practice'),
            'lt_pdf_id'=>array('label'=>__('Downloadable PDF','law-temple-core'),'type'=>'media'),
            'lt_external_url'=>array('label'=>__('External URL','law-temple-core'),'type'=>'url'),
            'lt_related_ids'=>array('label'=>__('Related insights','law-temple-core'),'type'=>'relation','post_type'=>'insight'),
            'lt_featured'=>array('label'=>__('Featured insight','law-temple-core'),'type'=>'checkbox'),
            'lt_reading_time'=>array('label'=>__('Reading time (minutes, 0 = automatic)','law-temple-core'),'type'=>'number'),
        ),
        'matter'=>array(
            'lt_client_name'=>array('label'=>__('Client name','law-temple-core'),'type'=>'text'),
            'lt_confidential'=>array('label'=>__('Client information is confidential','law-temple-core'),'type'=>'checkbox'),
            'lt_anonymous_client'=>array('label'=>__('Anonymous client description','law-temple-core'),'type'=>'text'),
            'lt_summary'=>array('label'=>__('Summary','law-temple-core'),'type'=>'textarea'),
            'lt_challenge'=>array('label'=>__('Challenge','law-temple-core'),'type'=>'textarea'),
            'lt_legal_work'=>array('label'=>__('Legal work performed','law-temple-core'),'type'=>'textarea'),
            'lt_outcome'=>array('label'=>__('Outcome','law-temple-core'),'type'=>'textarea'),
            'lt_practice_ids'=>array('label'=>__('Practice Areas','law-temple-core'),'type'=>'relation','post_type'=>'practice'),
            'lt_lawyer_ids'=>array('label'=>__('Lawyers involved','law-temple-core'),'type'=>'relation','post_type'=>'lawyer'),
            'lt_jurisdiction'=>array('label'=>__('Jurisdiction','law-temple-core'),'type'=>'text'),
            'lt_year'=>array('label'=>__('Date / year','law-temple-core'),'type'=>'text'),
            'lt_featured'=>array('label'=>__('Featured matter','law-temple-core'),'type'=>'checkbox'),
        ),
        'testimonial'=>array(
            'lt_client_name'=>array('label'=>__('Client name','law-temple-core'),'type'=>'text'),
            'lt_anonymous'=>array('label'=>__('Display anonymously','law-temple-core'),'type'=>'checkbox'),
            'lt_client_position'=>array('label'=>__('Position','law-temple-core'),'type'=>'text'),
            'lt_organisation'=>array('label'=>__('Organisation','law-temple-core'),'type'=>'text'),
            'lt_logo_id'=>array('label'=>__('Organisation logo','law-temple-core'),'type'=>'media'),
            'lt_practice_ids'=>array('label'=>__('Practice Areas','law-temple-core'),'type'=>'relation','post_type'=>'practice'),
            'lt_featured'=>array('label'=>__('Featured testimonial','law-temple-core'),'type'=>'checkbox'),
        ),
        'award'=>array(
            'lt_awarding_org'=>array('label'=>__('Awarding organisation','law-temple-core'),'type'=>'text'),
            'lt_year'=>array('label'=>__('Year','law-temple-core'),'type'=>'text'),
            'lt_category'=>array('label'=>__('Category','law-temple-core'),'type'=>'text'),
            'lt_lawyer_ids'=>array('label'=>__('Lawyers','law-temple-core'),'type'=>'relation','post_type'=>'lawyer'),
            'lt_practice_ids'=>array('label'=>__('Practice Areas','law-temple-core'),'type'=>'relation','post_type'=>'practice'),
            'lt_logo_id'=>array('label'=>__('Logo','law-temple-core'),'type'=>'media'),
            'lt_external_url'=>array('label'=>__('External URL','law-temple-core'),'type'=>'url'),
            'lt_featured'=>array('label'=>__('Featured award','law-temple-core'),'type'=>'checkbox'),
        ),
        'office'=>array(
            'lt_address'=>array('label'=>__('Address','law-temple-core'),'type'=>'textarea'),
            'lt_postal_address'=>array('label'=>__('Postal address','law-temple-core'),'type'=>'textarea'),
            'lt_digital_address'=>array('label'=>__('Digital / GPS address','law-temple-core'),'type'=>'text'),
            'lt_country'=>array('label'=>__('Country','law-temple-core'),'type'=>'text'),
            'lt_city'=>array('label'=>__('City','law-temple-core'),'type'=>'text'),
            'lt_phone'=>array('label'=>__('Telephone','law-temple-core'),'type'=>'text'),
            'lt_email'=>array('label'=>__('Email','law-temple-core'),'type'=>'email'),
            'lt_hours'=>array('label'=>__('Opening hours','law-temple-core'),'type'=>'textarea'),
            'lt_latitude'=>array('label'=>__('Latitude','law-temple-core'),'type'=>'text'),
            'lt_longitude'=>array('label'=>__('Longitude','law-temple-core'),'type'=>'text'),
            'lt_maps_url'=>array('label'=>__('Google Maps URL','law-temple-core'),'type'=>'url'),
            'lt_lawyer_ids'=>array('label'=>__('Lawyers based here','law-temple-core'),'type'=>'relation','post_type'=>'lawyer'),
            'lt_cta_label'=>array('label'=>__('CTA label','law-temple-core'),'type'=>'text'),
            'lt_cta_url'=>array('label'=>__('CTA URL','law-temple-core'),'type'=>'url'),
            'lt_featured'=>array('label'=>__('Main / featured office','law-temple-core'),'type'=>'checkbox'),
        ),
        'career'=>array(
            'lt_location'=>array('label'=>__('Location','law-temple-core'),'type'=>'text'),
            'lt_department'=>array('label'=>__('Department','law-temple-core'),'type'=>'text'),
            'lt_employment_type'=>array('label'=>__('Employment type','law-temple-core'),'type'=>'text'),
            'lt_requirements'=>array('label'=>__('Requirements','law-temple-core'),'type'=>'textarea'),
            'lt_closing_date'=>array('label'=>__('Closing date','law-temple-core'),'type'=>'date'),
            'lt_application_url'=>array('label'=>__('Application URL','law-temple-core'),'type'=>'url'),
            'lt_application_email'=>array('label'=>__('Application email','law-temple-core'),'type'=>'email'),
            'lt_vacancy_status'=>array('label'=>__('Vacancy status','law-temple-core'),'type'=>'select','options'=>array('open'=>__('Open','law-temple-core'),'closed'=>__('Closed','law-temple-core'),'draft'=>__('Not accepting applications','law-temple-core'))),
        ),
        'lt_faq'=>array(
            'lt_practice_ids'=>array('label'=>__('Related Practice Areas','law-temple-core'),'type'=>'relation','post_type'=>'practice'),
            'lt_page_ids'=>array('label'=>__('Related Pages','law-temple-core'),'type'=>'relation','post_type'=>'page'),
        ),
        'lt_cta'=>array(
            'lt_cta_eyebrow'=>array('label'=>__('Eyebrow / kicker','law-temple-core'),'type'=>'text'),
            'lt_cta_button_label'=>array('label'=>__('Button label','law-temple-core'),'type'=>'text'),
            'lt_cta_button_url'=>array('label'=>__('Button URL','law-temple-core'),'type'=>'url'),
            'lt_cta_secondary_label'=>array('label'=>__('Secondary button label','law-temple-core'),'type'=>'text'),
            'lt_cta_secondary_url'=>array('label'=>__('Secondary button URL','law-temple-core'),'type'=>'url'),
            'lt_cta_style'=>array('label'=>__('CTA style','law-temple-core'),'type'=>'select','options'=>array('primary'=>__('Primary','law-temple-core'),'dark'=>__('Dark','law-temple-core'),'light'=>__('Light','law-temple-core'),'minimal'=>__('Minimal','law-temple-core'))),
        ),
        'hero_slide'=>array(
            'lt_hero_layout'=>array('label'=>__('Hero layout','law-temple-core'),'type'=>'select','options'=>array('split'=>__('Text + image','law-temple-core'),'full-image'=>__('Full image','law-temple-core'),'text'=>__('Text only','law-temple-core'),'profile'=>__('Lawyer spotlight','law-temple-core'),'practice'=>__('Practice spotlight','law-temple-core'),'video'=>__('Video background','law-temple-core'),'minimal'=>__('Minimal corporate','law-temple-core'),'insight'=>__('Featured insight','law-temple-core'))),
            'lt_eyebrow'=>array('label'=>__('Eyebrow / kicker','law-temple-core'),'type'=>'text'),
            'lt_highlight'=>array('label'=>__('Highlighted words','law-temple-core'),'type'=>'text'),
            'lt_primary_label'=>array('label'=>__('Primary CTA label','law-temple-core'),'type'=>'text'),
            'lt_primary_url'=>array('label'=>__('Primary CTA URL','law-temple-core'),'type'=>'url'),
            'lt_secondary_label'=>array('label'=>__('Secondary CTA label','law-temple-core'),'type'=>'text'),
            'lt_secondary_url'=>array('label'=>__('Secondary CTA URL','law-temple-core'),'type'=>'url'),
            'lt_primary_style'=>array('label'=>__('Primary CTA style','law-temple-core'),'type'=>'select','options'=>array('primary'=>__('Primary','law-temple-core'),'secondary'=>__('Secondary','law-temple-core'),'outline'=>__('Outline','law-temple-core'),'gold'=>__('Gold / accent','law-temple-core'),'dark'=>__('Dark','law-temple-core'),'light'=>__('Light','law-temple-core'),'text'=>__('Text link','law-temple-core'))),
            'lt_secondary_style'=>array('label'=>__('Secondary CTA style','law-temple-core'),'type'=>'select','options'=>array('primary'=>__('Primary','law-temple-core'),'secondary'=>__('Secondary','law-temple-core'),'outline'=>__('Outline','law-temple-core'),'gold'=>__('Gold / accent','law-temple-core'),'dark'=>__('Dark','law-temple-core'),'light'=>__('Light','law-temple-core'),'text'=>__('Text link','law-temple-core'))),
            'lt_image_focal'=>array('label'=>__('Image focal point','law-temple-core'),'type'=>'select','options'=>array('center'=>__('Centre','law-temple-core'),'top'=>__('Top','law-temple-core'),'bottom'=>__('Bottom','law-temple-core'),'left'=>__('Left','law-temple-core'),'right'=>__('Right','law-temple-core'))),
            'lt_mobile_image_id'=>array('label'=>__('Mobile image','law-temple-core'),'type'=>'media'),
            'lt_video_url'=>array('label'=>__('Video URL (MP4/WebM)','law-temple-core'),'type'=>'url'),
            'lt_overlay'=>array('label'=>__('Overlay opacity (0-100)','law-temple-core'),'type'=>'number'),
            'lt_background_color'=>array('label'=>__('Background colour','law-temple-core'),'type'=>'color'),
            'lt_text_color'=>array('label'=>__('Text colour','law-temple-core'),'type'=>'color'),
            'lt_alignment'=>array('label'=>__('Text alignment','law-temple-core'),'type'=>'select','options'=>array('left'=>__('Left','law-temple-core'),'center'=>__('Centre','law-temple-core'),'right'=>__('Right','law-temple-core'))),
            'lt_content_position'=>array('label'=>__('Content position','law-temple-core'),'type'=>'select','options'=>array('left'=>__('Left','law-temple-core'),'center'=>__('Centre','law-temple-core'),'right'=>__('Right','law-temple-core'))),
            'lt_vertical_align'=>array('label'=>__('Vertical alignment','law-temple-core'),'type'=>'select','options'=>array('top'=>__('Top','law-temple-core'),'center'=>__('Centre','law-temple-core'),'bottom'=>__('Bottom','law-temple-core'))),
            'lt_height'=>array('label'=>__('Height','law-temple-core'),'type'=>'select','options'=>array('compact'=>__('Compact','law-temple-core'),'standard'=>__('Standard','law-temple-core'),'large'=>__('Large','law-temple-core'),'screen'=>__('Full screen','law-temple-core'))),
            'lt_content_width'=>array('label'=>__('Content width (px)','law-temple-core'),'type'=>'number'),
            'lt_lawyer_id'=>array('label'=>__('Linked lawyer','law-temple-core'),'type'=>'single_relation','post_type'=>'lawyer'),
            'lt_practice_id'=>array('label'=>__('Linked practice','law-temple-core'),'type'=>'single_relation','post_type'=>'practice'),
            'lt_insight_id'=>array('label'=>__('Linked insight','law-temple-core'),'type'=>'single_relation','post_type'=>'insight'),
            'lt_start_date'=>array('label'=>__('Start date','law-temple-core'),'type'=>'date'),
            'lt_end_date'=>array('label'=>__('End date','law-temple-core'),'type'=>'date'),
            'lt_enabled'=>array('label'=>__('Enable this slide','law-temple-core'),'type'=>'checkbox'),
        ),
    );
}

function lt_core_register_meta() {
    foreach ( lt_core_meta_schema() as $post_type => $fields ) {
        foreach ( $fields as $key => $field ) {
            $type = in_array( $field['type'], array('checkbox','media','single_relation','number'), true ) ? 'integer' : 'string';
            register_post_meta( $post_type, $key, array(
                'type'=>$type, 'single'=>true, 'show_in_rest'=>true,
                'sanitize_callback'=>function( $value ) use ( $field ) { return lt_core_sanitize_meta_value( $value, $field ); },
                'auth_callback'=>function( $allowed, $meta_key, $post_id ){ return current_user_can( 'edit_post', $post_id ); },
            ) );
        }
    }
}
function lt_core_sanitize_meta_value( $value, $field ) {
    switch ( $field['type'] ) {
        case 'checkbox': return empty($value)?0:1;
        case 'number': case 'media': case 'single_relation': return absint($value);
        case 'email': return sanitize_email($value);
        case 'url': return esc_url_raw($value);
        case 'color': return sanitize_hex_color($value) ?: '';
        case 'relation': return lt_core_sanitize_csv_ids($value);
        case 'textarea': return sanitize_textarea_field($value);
        case 'date': return preg_match('/^\d{4}-\d{2}-\d{2}$/',(string)$value) ? $value : '';
        default: return sanitize_text_field($value);
    }
}
add_action( 'add_meta_boxes', 'lt_core_add_meta_boxes' );
add_action( 'save_post', 'lt_core_save_meta_boxes', 10, 2 );

function lt_core_add_meta_boxes() {
    foreach ( array_keys( lt_core_meta_schema() ) as $post_type ) {
        add_meta_box( 'lt-core-details', __( 'Law Firm Details', 'law-temple-core' ), 'lt_core_render_meta_box', $post_type, 'normal', 'high' );
    }
}
function lt_core_render_meta_box( $post ) {
    $schema = lt_core_meta_schema(); $fields = $schema[$post->post_type] ?? array();
    wp_nonce_field( 'lt_core_save_meta', 'lt_core_meta_nonce' );
    echo '<div class="lt-admin-fields">';
    foreach ( $fields as $key => $field ) {
        $value = get_post_meta( $post->ID, $key, true );
        if ( 'checkbox' === $field['type'] && '' === $value && 'lt_enabled' === $key ) { $value = 1; }
        echo '<div class="lt-admin-field"><label for="'.esc_attr($key).'"><strong>'.esc_html($field['label']).'</strong></label>';
        lt_core_render_field( $key, $field, $value, $post->ID );
        echo '</div>';
    }
    echo '</div>';
}
function lt_core_render_field( $key, $field, $value, $post_id = 0 ) {
    $name = 'lt_meta['.$key.']';
    switch ( $field['type'] ) {
        case 'textarea': echo '<textarea class="widefat" rows="5" id="'.esc_attr($key).'" name="'.esc_attr($name).'">'.esc_textarea($value).'</textarea>'; break;
        case 'select': echo '<select class="widefat" id="'.esc_attr($key).'" name="'.esc_attr($name).'">'; foreach($field['options'] as $v=>$l){echo '<option value="'.esc_attr($v).'" '.selected($value,$v,false).'>'.esc_html($l).'</option>';} echo '</select>'; break;
        case 'checkbox': echo '<label><input type="checkbox" id="'.esc_attr($key).'" name="'.esc_attr($name).'" value="1" '.checked($value,1,false).'> '.esc_html__('Yes','law-temple-core').'</label>'; break;
        case 'relation':
            $selected=lt_core_csv_ids($value);
            $counts=wp_count_posts($field['post_type']);
            $total=0; if($counts){foreach((array)$counts as $status=>$count){if(in_array($status,array('publish','draft','private'),true))$total+=(int)$count;}}
            $query_args=array('post_type'=>$field['post_type'],'post_status'=>array('publish','draft','private'),'orderby'=>'title','order'=>'ASC','exclude'=>$post_id?array($post_id):array());
            if($total>60 && $selected){$query_args['post__in']=$selected;$query_args['posts_per_page']=count($selected);}else{$query_args['posts_per_page']=-1;}
            $items=get_posts($query_args);
            echo '<div class="lt-ajax-relation" data-post-type="'.esc_attr($field['post_type']).'" data-exclude="'.esc_attr($post_id).'">';
            echo '<input type="search" class="widefat lt-relation-search" placeholder="'.esc_attr__('Search relationships…','law-temple-core').'" autocomplete="off" style="margin-bottom:6px">';
            echo '<div class="lt-relation-results" hidden></div><select multiple size="8" class="widefat lt-relation-select" id="'.esc_attr($key).'" name="'.esc_attr($name).'[]">';
            foreach($items as $item){echo '<option value="'.esc_attr($item->ID).'" '.selected(in_array($item->ID,$selected,true),true,false).'>'.esc_html($item->post_title).'</option>';}
            echo '</select><p class="description">'.esc_html($total>60?__('Search above to add relationships. Selected items remain in the list.','law-temple-core'):__('Use Ctrl/Cmd to select multiple items. Search can also find additional records.','law-temple-core')).'</p></div>'; break;
        case 'single_relation':
            $selected_id=absint($value);$items=array();if($selected_id){$item=get_post($selected_id);if($item)$items[]=$item;}
            $counts=wp_count_posts($field['post_type']);$total=0;if($counts){foreach((array)$counts as $status=>$count){if(in_array($status,array('publish','draft','private'),true))$total+=(int)$count;}}
            if($total<=60)$items=get_posts(array('post_type'=>$field['post_type'],'post_status'=>array('publish','draft','private'),'posts_per_page'=>-1,'orderby'=>'title','order'=>'ASC'));
            echo '<div class="lt-ajax-relation lt-ajax-relation--single" data-post-type="'.esc_attr($field['post_type']).'" data-exclude="0"><input type="search" class="widefat lt-relation-search" placeholder="'.esc_attr__('Search relationships…','law-temple-core').'" autocomplete="off" style="margin-bottom:6px"><div class="lt-relation-results" hidden></div><select class="widefat lt-relation-select" id="'.esc_attr($key).'" name="'.esc_attr($name).'"><option value="0">'.esc_html__('None','law-temple-core').'</option>'; foreach($items as $item){echo '<option value="'.esc_attr($item->ID).'" '.selected($selected_id,$item->ID,false).'>'.esc_html($item->post_title).'</option>';} echo '</select></div>'; break;
        case 'media':
            $thumb=$value?wp_get_attachment_image(absint($value),'thumbnail'):''; echo '<div class="lt-media-field" data-target="'.esc_attr($key).'"><div class="lt-media-preview">'.$thumb.'</div><input type="hidden" id="'.esc_attr($key).'" name="'.esc_attr($name).'" value="'.esc_attr(absint($value)).'"><button type="button" class="button lt-media-select">'.esc_html__('Choose media','law-temple-core').'</button> <button type="button" class="button-link-delete lt-media-remove">'.esc_html__('Remove','law-temple-core').'</button></div>'; break;
        default:
            $type=in_array($field['type'],array('email','url','number','date','color'),true)?$field['type']:'text'; echo '<input class="widefat" type="'.esc_attr($type).'" id="'.esc_attr($key).'" name="'.esc_attr($name).'" value="'.esc_attr($value).'">';
    }
}
function lt_core_save_meta_boxes( $post_id, $post ) {
    if ( ! isset($_POST['lt_core_meta_nonce']) || ! wp_verify_nonce( sanitize_text_field(wp_unslash($_POST['lt_core_meta_nonce'])), 'lt_core_save_meta' ) ) return;
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
    if ( ! current_user_can('edit_post',$post_id) ) return;
    $schema=lt_core_meta_schema(); if(!isset($schema[$post->post_type])) return;
    $raw = isset($_POST['lt_meta']) && is_array($_POST['lt_meta']) ? wp_unslash($_POST['lt_meta']) : array();
    foreach($schema[$post->post_type] as $key=>$field){
        $value = $raw[$key] ?? ( 'checkbox'===$field['type'] ? 0 : '' );
        $clean=lt_core_sanitize_meta_value($value,$field); update_post_meta($post_id,$key,$clean);
    }
}
