load_dependencies(); $this->init_hooks(); } /** * Load required files */ private function load_dependencies() { require_once UMZUGSLISTE_PLUGIN_DIR . 'includes/class-cpt.php'; require_once UMZUGSLISTE_PLUGIN_DIR . 'includes/class-admin-menu.php'; require_once UMZUGSLISTE_PLUGIN_DIR . 'includes/class-settings.php'; require_once UMZUGSLISTE_PLUGIN_DIR . 'includes/class-furniture-data.php'; require_once UMZUGSLISTE_PLUGIN_DIR . 'includes/class-date-helpers.php'; require_once UMZUGSLISTE_PLUGIN_DIR . 'includes/class-captcha.php'; require_once UMZUGSLISTE_PLUGIN_DIR . 'includes/class-form-renderer.php'; require_once UMZUGSLISTE_PLUGIN_DIR . 'includes/class-shortcode.php'; require_once UMZUGSLISTE_PLUGIN_DIR . 'includes/class-email-generator.php'; require_once UMZUGSLISTE_PLUGIN_DIR . 'includes/class-test-email.php'; require_once UMZUGSLISTE_PLUGIN_DIR . 'includes/class-form-handler.php'; } /** * Initialize WordPress hooks */ private function init_hooks() { add_action( 'init', array( $this, 'init' ) ); add_filter( 'template_include', array( $this, 'maybe_load_form_template' ) ); // Form handler must register its init hook before init fires Umzugsliste_Form_Handler::get_instance(); } /** * Load standalone form template if current page is the configured form page * * @param string $template Current template path * @return string Template path */ public function maybe_load_form_template( $template ) { if ( is_admin() || ! is_page() ) { return $template; } $use_standalone = false; // Check configured form page ID $form_page_id = (int) get_option( 'umzugsliste_form_page_id', 0 ); if ( $form_page_id > 0 && is_page( $form_page_id ) ) { $use_standalone = true; } // Fallback: check if current page contains the [umzugsliste] shortcode if ( ! $use_standalone ) { $post = get_queried_object(); if ( $post && isset( $post->post_content ) && has_shortcode( $post->post_content, 'umzugsliste' ) ) { $use_standalone = true; } } if ( $use_standalone ) { // Extract lang from shortcode if present and switch locale before template loads $post = get_queried_object(); if ( $post && isset( $post->post_content ) && preg_match( '/\[umzugsliste[^\]]*lang=["\'](\w+)["\']/', $post->post_content, $m ) ) { $locale_map = array( 'de' => 'de_DE', 'en' => 'en_US' ); if ( isset( $locale_map[ $m[1] ] ) && $locale_map[ $m[1] ] !== get_locale() ) { switch_to_locale( $locale_map[ $m[1] ] ); } } $custom_template = UMZUGSLISTE_PLUGIN_DIR . 'templates/form-page.php'; if ( file_exists( $custom_template ) ) { return $custom_template; } } return $template; } /** * Initialize plugin components */ public function init() { // Initialize and register CPT $cpt = Umzugsliste_CPT::get_instance(); $cpt->register_post_type(); // Initialize admin menu and test email if ( is_admin() ) { Umzugsliste_Admin_Menu::get_instance(); Umzugsliste_Test_Email::get_instance(); } // Initialize settings Umzugsliste_Settings::get_instance(); // Initialize shortcode Umzugsliste_Shortcode::get_instance(); } } /** * Activation hook */ function umzugsliste_activate() { // Initialize CPT to register post type require_once UMZUGSLISTE_PLUGIN_DIR . 'includes/class-cpt.php'; Umzugsliste_CPT::get_instance(); // Auto-create form page if none exists $form_page_id = (int) get_option( 'umzugsliste_form_page_id', 0 ); if ( $form_page_id <= 0 || ! get_post( $form_page_id ) ) { $page_id = wp_insert_post( array( 'post_title' => 'Umzugsliste', 'post_content' => '', 'post_status' => 'publish', 'post_type' => 'page', ) ); if ( $page_id && ! is_wp_error( $page_id ) ) { update_option( 'umzugsliste_form_page_id', $page_id ); } } // Flush rewrite rules flush_rewrite_rules(); } register_activation_hook( __FILE__, 'umzugsliste_activate' ); /** * Deactivation hook */ function umzugsliste_deactivate() { // Flush rewrite rules flush_rewrite_rules(); } register_deactivation_hook( __FILE__, 'umzugsliste_deactivate' ); /** * Initialize plugin */ function umzugsliste_init() { return Umzugsliste::get_instance(); } add_action( 'plugins_loaded', 'umzugsliste_init' );