<?php

namespace muv\HidePreview\Frontend;


defined( 'ABSPATH' ) OR exit;


class Freischalten {

	
	public static function init() {
		add_action( 'init', array( self::class, 'addEndpoints' ) );
	}

	
	public static function addEndPoints() {
		
		add_action( 'template_redirect', array( self::class, 'endpointDoFreischalten' ), 5 );
	}

	
	public static function endpointDoFreischalten() {

		$requestUri = muv_get_request_uri();

		
		if ( strpos( $requestUri, '/muv-hide-preview/freischalten/' ) !== 0 ) {
			return;
		}

		
		$tmp    = explode( '/', $requestUri );
		$keyIst = ( count( $tmp ) < 4 ) ? '' : $tmp[3]; 

		$keySoll = get_option( 'muv-hp-freischalten-key', '' );
		
		if ( empty( $keySoll ) ) {
			wp_redirect( '/' );
			exit;
		}

		
		if ( $keyIst !== $keySoll ) {
			wp_redirect( '/' );
			exit;
		}

		
        if ( ! session_id() ) {
            session_start();
        }
		$_SESSION['muv-hp-freischalten'] = true;

		
		wp_redirect( '/' );
		exit;
	}
}
