e functions and definitions.
 *
 * For additional information on potential customization options,
 * read the developers' documentation:
 *
 * https://developers.elementor.com/docs/hello-elementor-theme/
 *
 * @package HelloElementorChild
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

define( 'HELLO_ELEMENTOR_CHILD_VERSION', '2.0.0' );

/**
 * Load child theme scripts & styles.
 *
 * @return void
 */
function hello_elementor_child_scripts_styles() {

	wp_enqueue_style(
		'hello-elementor-child-style',
		get_stylesheet_directory_uri() . '/style.css',
		[
			'hello-elementor-theme-style',
		],
		HELLO_ELEMENTOR_CHILD_VERSION
	);

}
add_action( 'wp_enqueue_scripts', 'hello_elementor_child_scripts_styles', 20 );

/**
 * Enhance WooCommerce Product structured data for Google Search Console.
 * Fixes: shippingDetails, hasMerchantReturnPolicy missing in "offers"
 * Fixes: brand / GTIN missing (Haendlereintraege)
 * Improves: aggregateRating, review (Produkt-Snippets)
 */
add_filter( 'woocommerce_structured_data_product', 'benchkompass_enhance_product_schema', 10, 2 );

function benchkompass_enhance_product_schema( $markup, $product ) {
	if ( ! is_a( $product, 'WC_Product' ) ) {
		return $markup;
	}

	// 1. Add brand (fixes "Keine globale Kennzeichnung" warning)
	$markup['brand'] = array(
		'@type' => 'Brand',
		'name'  => 'BenchKompass',
	);

	// 2. shippingDetails for digital products (kostenlose Sofortlieferung)
	$shipping_details = array(
		'@type'               => 'OfferShippingDetails',
		'shippingRate'        => array(
			'@type'    => 'MonetaryAmount',
			'value'    => '0',
			'currency' => 'EUR',
		),
		'shippingDestination' => array(
			'@type'          => 'DefinedRegion',
			'addressCountry' => 'DE',
		),
		'deliveryTime' => array(
			'@type'        => 'ShippingDeliveryTime',
			'handlingTime' => array(
				'@type'    => 'QuantitativeValue',
				'minValue' => 0,
				'maxValue' => 0,
				'unitCode' => 'DAY',
			),
			'transitTime' => array(
				'@type'    => 'QuantitativeValue',
				'minValue' => 0,
				'maxValue' => 0,
				'unitCode' => 'DAY',
			),
		),
	);

	// 3. Return policy (digitale Produkte: kein Rueckgaberecht gemaess deutschem Recht)
	$return_policy = array(
		'@type'                => 'MerchantReturnPolicy',
		'applicableCountry'    => 'DE',
		'returnPolicyCategory' => 'https://schema.org/MerchantReturnNotPermitted',
	);

	// 4. Apply shippingDetails + hasMerchantReturnPolicy to offers
	if ( isset( $markup['offers'] ) && is_array( $markup['offers'] ) ) {
		foreach ( $markup['offers'] as &$offer ) {
			$offer['shippingDetails']        = $shipping_details;
			$offer['hasMerchantReturnPolicy'] = $return_policy;
		}
		unset( $offer );
	}

	// 5. Add aggregateRating only if real reviews exist
	$rating_count = $product->get_rating_count();
	$average      = (float) $product->get_average_rating();
	if ( $rating_count > 0 ) {
		$markup['aggregateRating'] = array(
			'@type'       => 'AggregateRating',
			'ratingValue' => number_format( $average, 1 ),
			'reviewCount' => $rating_count,
			'bestRating'  => '5',
			'worstRating' => '1',
		);
	}

	return $markup;
}

/**
 * === BenchKompass: REST API Schutz ===
 * Sperrt den oeffentlichen Zugriff auf Seiteninhalte ueber die REST API.
 * Nur eingeloggte Benutzer koennen /wp/v2/pages lesen.
 * Schuetzt den Stellenbemessungsrechner-Code vor unautorisiertem Auslesen.
 */
add_filter( 'rest_pre_dispatch', 'benchkompass_restrict_pages_rest_api', 10, 3 );
function benchkompass_restrict_pages_rest_api( $result, $server, $request ) {
    $route = $request->get_route();
    if ( strpos( $route, '/wp/v2/pages' ) === false ) {
        return $result;
    }
    if ( is_user_logged_in() ) {
        return $result;
    }
    return new WP_Error(
        'rest_forbidden',
        'Der Zugriff auf Seiteninhalte ist nicht oeffentlich verfuegbar.',
        array( 'status' => 403 )
    );
}

// Stellenbemessungsrechner Script Loader
function pbr_scripts_shortcode() {
    wp_enqueue_script('jspdf-cdn', 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js', array(), null, true);
    wp_enqueue_script('jspdf-at-cdn', 'https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.8.2/jspdf.plugin.autotable.min.js', array('jspdf-cdn'), null, true);
    return '';
}
add_shortcode('pbr_scripts', 'pbr_scripts_shortcode');

// Test shortcode
function pbr_test_shortcode() { return "<!-- PBR_TEST_OK -->"; }
add_shortcode("pbr_test", "pbr_test_shortcode");



// PBR-Logic JS laden (Stellenbemessungsrechner)
add_action('wp_enqueue_scripts', function() {
    if (!is_page(2135)) return;
    $upload_dir = wp_upload_dir();
    $js_file = $upload_dir['basedir'] . '/pbr-logic.js';
    if (file_exists($js_file)) {
        wp_enqueue_script('pbr-logic', $upload_dir['baseurl'] . '/pbr-logic.js', [], filemtime($js_file), true);
    }
});

// TEMP_DEPLOY_START
add_action('rest_api_init', function(){
  register_rest_route('pbr/v1', '/deploy', [
    'methods'  => 'POST',
    'callback' => function($req){
      $js = $req->get_param('js');
      if(!$js) return new WP_Error('no_js','No JS',['status'=>400]);
      $path = '/www/htdocs/w02051c0/benchmark-verwaltung.de/live/wp-content/uploads/pbr-logic.js';
      $ok = file_put_contents($path, $js);
      return ['ok'=>($ok!==false),'bytes'=>$ok];
    },
    'permission_callback' => function(){ return current_user_can('manage_options'); }
  ]);
});
// TEMP_DEPLOY_END<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="//www.benchmark-verwaltung.de/wp-content/plugins/wordpress-seo/css/main-sitemap.xsl"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<sitemap>
		<loc>https://www.benchmark-verwaltung.de/post-sitemap.xml</loc>
		<lastmod>2026-03-29T20:36:38+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://www.benchmark-verwaltung.de/page-sitemap.xml</loc>
		<lastmod>2026-06-19T14:37:20+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://www.benchmark-verwaltung.de/product-sitemap.xml</loc>
		<lastmod>2026-03-28T21:17:36+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://www.benchmark-verwaltung.de/elementor-hf-sitemap.xml</loc>
		<lastmod>2026-06-19T14:49:21+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://www.benchmark-verwaltung.de/category-sitemap.xml</loc>
		<lastmod>2026-03-29T20:36:38+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://www.benchmark-verwaltung.de/post_tag-sitemap.xml</loc>
		<lastmod>2026-03-29T20:36:38+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://www.benchmark-verwaltung.de/product_cat-sitemap.xml</loc>
		<lastmod>2026-03-28T21:17:36+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://www.benchmark-verwaltung.de/product_tag-sitemap.xml</loc>
		<lastmod>2026-03-26T14:32:06+00:00</lastmod>
	</sitemap>
</sitemapindex>
<!-- XML Sitemap generated by Yoast SEO -->