<?php
/**
 * Sanitization callback function for the Sifency Addons settings.
 *
 * This function is registered with `register_setting()` and is called by WordPress
 * before saving the options to the database. It receives the raw input from the form
 * and must return a clean, safe array of data to be stored.
 *
 * @param array $input The raw array of settings submitted from the form.
 * @return array The sanitized array of settings to be saved.
 */
function sifency_addons_sanitize_options( $input ) {

    $sanitized_input = array();

    $existing_options = get_option( 'sifency_addons_settings', array() );
    $sanitized_input = $existing_options;

    if ( isset( $input['api_key'] ) ) {
        $sanitized_input['api_key'] = sanitize_text_field( $input['api_key'] );
    }

    if ( isset( $input['enable_awesome_feature'] ) && '1' === $input['enable_awesome_feature'] ) {
        $sanitized_input['enable_awesome_feature'] = '1';
    } else {
        $sanitized_input['enable_awesome_feature'] = '0';
    }

    if ( isset( $input['default_layout'] ) ) {
        $allowed_layouts = array( 'grid', 'list', 'carousel' );
        $submitted_layout = $input['default_layout'];

        if ( in_array( $submitted_layout, $allowed_layouts, true ) ) {
            $sanitized_input['default_layout'] = $submitted_layout;
        } else {
            $sanitized_input['default_layout'] = 'grid';
        }
    }
    
    if ( isset( $input['notification_email'] ) ) {
        $sanitized_input['notification_email'] = sanitize_email( $input['notification_email'] );
    }
    
    if ( isset( $input['items_per_page'] ) ) {
        $sanitized_input['items_per_page'] = absint( $input['items_per_page'] );
    }

    return $sanitized_input;
}
function sifency_is_pro() {
    if (class_exists('Sifency_Addons_Theme_Builder_Master') 
        && method_exists('Sifency_Addons_Theme_Builder_Master', 'sifency_addons_is_legal')) {
        return Sifency_Addons_Theme_Builder_Master::sifency_addons_is_legal();
    }
    return false;
}


if ( ! function_exists( 'sifency_get_cf7_forms' ) ) {
	function sifency_get_cf7_forms() {
		if ( ! function_exists( 'wpcf7_contact_form_shortcode' ) ) {
			return [ 'none' => esc_html__( 'Contact Form 7 is not active', 'sifency-addons' ) ];
		}
		$forms = get_posts(
			[
				'post_type'      => 'wpcf7_contact_form',
				'posts_per_page' => -1,
				'post_status'    => 'publish',
			]
		);

		$options = [ 'none' => esc_html__( '— Select a Form —', 'sifency-addons' ) ];

		if ( ! empty( $forms ) ) {
			foreach ( $forms as $form ) {
				$options[ $form->ID ] = esc_html( $form->post_title );
			}
		} else {
			$options['none'] = esc_html__( 'No contact forms found', 'sifency-addons' );
		}

		return $options;
	}
}