@archive-vault) if ( is_post_type_archive() ) { $post_type = get_post_type(); // Gets the queried post type. // Check if it's an Addifect-generated custom post type and strip the prefix. if ( strpos( $post_type, 'addifect_cp_' ) === 0 ) { $clean_slug = str_replace( 'addifect_cp_', '', $post_type ); return '@archive-' . $clean_slug; } // Fallback for third-party CPT archives (e.g., WooCommerce products). return '@archive-' . $post_type; } // 5. Generic Archives Catch-All (Categories, Tags, Author pages) if ( is_archive() ) { return '@archive'; } // 6. Single Blog Post if ( is_singular( 'post' ) ) { return '@post'; } // 7. Single Page if ( is_singular( 'page' ) ) { global $post; // If a specific custom template exists for this slug (e.g., 'vault'), use it. if ( ! empty( $post->post_name ) && addifect_template_exists( $post->post_name ) ) { return $post->post_name; } // Otherwise, fall back to the standard page template. return '@page'; } // 8. Custom Post Types Singles (e.g., addifect_cp_vault -> @vault) if ( is_singular() ) { $post_type = get_post_type(); // Check if it's an Addifect-generated custom post type and strip the prefix. if ( strpos( $post_type, 'addifect_cp_' ) === 0 ) { $clean_slug = str_replace( 'addifect_cp_', '', $post_type ); return '@' . $clean_slug; } // Fallback for any third-party custom post types. return '@' . $post_type; } return null; } /** * Check if a given template ID exists in transient cache. * * @param string $template_id Template identifier. * @return bool True if template transient exists and is not empty. */ function addifect_template_exists( $template_id ) { $data = addifect_get_template( $template_id ); return ! empty( $data ); } /** * Register internal rewrite rules. * Hooked to 'init'. */ function addifect_register_template_rewrite() { add_rewrite_rule( '^__addifect/templates/([a-zA-Z0-9]+)/?$', 'index.php?addifect_templates=1&addifect_hash=$matches[1]', 'top' ); add_rewrite_rule( '^__addifect/studio/?$', 'index.php?addifect_studio=1', 'top' ); add_rewrite_rule( '^__addifect/studio/site-canvas/?$', 'index.php?addifect_studio=1', 'top' ); } add_action( 'init', 'addifect_register_template_rewrite' ); /** * Main template router. */ function addifect_template_router() { // 1. Handle template payload. if ( get_query_var( 'addifect_templates' ) ) { $payload_path = plugin_dir_path( __FILE__ ) . 'template-payload.php'; if ( file_exists( $payload_path ) ) { include $payload_path; if ( function_exists( 'addifect_render_template_payload' ) ) { addifect_render_template_payload(); exit; } } } // 2. Handle editor endpoint. if ( get_query_var( 'addifect_studio' ) ) { // 2.1. If not logged in, redirect to Login page. if ( ! is_user_logged_in() ) { auth_redirect(); exit; } /** * 2.2. Security Guard: * Instead of die, we redirect unauthorized users (like Subscribers) * back to the home page. */ // phpcs:ignore WordPress.WP.Capabilities.Unknown if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'addifect_edit_design_files' ) ) { wp_safe_redirect( home_url() ); exit; } // 2.3. Load the Studio Canvas. $studio_path = plugin_dir_path( __FILE__ ) . 'studio/studio.php'; if ( file_exists( $studio_path ) ) { include $studio_path; exit; } else { // Fallback if the file is missing. wp_safe_redirect( home_url() ); exit; } } // 3. Normal rendering. $template_id = addifect_resolve_template_id(); if ( $template_id && addifect_template_exists( $template_id ) ) { $render_path = plugin_dir_path( __FILE__ ) . 'render/render.php'; if ( file_exists( $render_path ) ) { include $render_path; exit; } } } add_action( 'template_redirect', 'addifect_template_router' );