'Lesson: focus (course player, no site header or footer)', 'single-rooster_quiz-focus' => 'Quiz: focus (course player, no site header or footer)', 'single-rooster_course-landing' => 'Course: landing page', 'single-product-longform' => 'Product: long form (description and specifications in full)', 'page-app' => 'Application shell: a slim header, the account navigation beside the page\'s content, a slim footer', 'single-docs' => 'Manual page: the chapters beside the text, the breadcrumb, the screenshots in the lightbox, previous and next', 'archive-docs' => 'Manual chapter: the chapters beside the chapter\'s pages', 'route' => 'Route: a plugin screen between the site\'s header and footer', 'route-app' => 'Route in the application shell (a slim header, the account navigation beside the screen)', ]; private static ?Slots $instance = null; private array $slots = []; private bool $registered = false; private ?array $pins = null; private ?array $current = null; private bool $resolved = false; private ?string $rendered = null; private array $route_rules = []; public static function instance(): self { return self::$instance ??= new self(); } public static function boot(): void { $self = self::instance(); if ( ! is_admin() ) { foreach ( [ 'single', 'singular', 'page', 'archive', 'taxonomy', 'category', 'tag', 'author', 'date', 'home', 'frontpage', 'index', 'search', 'attachment', 'privacypolicy' ] as $type ) { add_filter( "{$type}_template_hierarchy", [ $self, 'hierarchy' ], 5 ); } add_filter( 'body_class', [ $self, 'body_class' ] ); add_action( 'pre_get_posts', [ $self, 'route_query' ] ); add_filter( 'pre_handle_404', [ $self, 'route_not_404' ], 10, 2 ); add_filter( 'document_title_parts', [ $self, 'route_title' ] ); add_filter( 'wp_robots', [ $self, 'route_robots' ] ); } add_action( 'init', [ $self, 'register_routes' ], 100 ); add_action( 'update_option_' . self::OPTION, [ self::class, 'pins_changed' ] ); add_action( 'add_option_' . self::OPTION, [ self::class, 'pins_changed' ] ); add_action( 'delete_option_' . self::OPTION, [ self::class, 'pins_changed' ] ); } public static function pins_changed(): void { self::instance()->pins = null; do_action( 'rooster/templates/pins_changed' ); } public function register( string $slot, array $args ): void { $slot = sanitize_key( str_replace( '/', '-', $slot ) ) === '' ? '' : trim( $slot ); if ( '' === $slot ) { return; } $def = array_merge( [ 'label' => $slot, 'kind' => 'single', 'post_types' => [], 'taxonomies' => [], 'default' => '', 'matches' => [ 'object', 'term', 'role', 'lang', 'always' ], 'page_option' => null, 'page_id' => null, 'is_current' => null, 'resolve' => null, 'capability' => 'edit_theme_options', 'priority' => 10, 'source' => 'plugin', 'icon' => '', 'rewrite' => null, 'query_var' => '', 'render' => null, 'seo' => 'theme', 'app' => false, 'title' => '', ], $args ); if ( 'route' === $def['kind'] ) { $def['query_var'] = sanitize_key( (string) $def['query_var'] ); $def['seo'] = in_array( $def['seo'], self::SEO, true ) ? $def['seo'] : 'theme'; $def['app'] = (bool) $def['app']; if ( '' === (string) $def['default'] ) { $def['default'] = $def['app'] ? 'route-app' : 'route'; } if ( '' !== $def['query_var'] ) { $var = $def['query_var']; add_filter( 'query_vars', static fn ( $vars ): array => in_array( $var, (array) $vars, true ) ? (array) $vars : array_merge( (array) $vars, [ $var ] ) ); $rules = is_string( $def['rewrite'] ) && '' !== $def['rewrite'] ? [ $def['rewrite'] => 'index.php?' . $var . '=1' ] : ( is_array( $def['rewrite'] ) ? $def['rewrite'] : [] ); foreach ( $rules as $regex => $query ) { if ( is_string( $regex ) && '' !== $regex && is_string( $query ) && '' !== $query ) { $this->route_rules[ $regex ] = $query; add_rewrite_rule( $regex, $query, 'top' ); } } } } $this->slots[ $slot ] = $def; } public function register_routes(): void { $this->all(); $rules = $this->route_rules; ksort( $rules ); $stamp = $rules ? md5( (string) wp_json_encode( $rules ) ) : ''; if ( $stamp !== (string) get_option( self::ROUTES_OPTION, '' ) ) { if ( '' !== $stamp ) { flush_rewrite_rules( false ); } update_option( self::ROUTES_OPTION, $stamp, false ); } } public function route_rules(): array { return $this->route_rules; } public function route_of( \WP_Query $query ): string { foreach ( $this->all() as $slot => $def ) { if ( 'route' === $def['kind'] && '' !== (string) $def['query_var'] && '' !== (string) $query->get( (string) $def['query_var'], '' ) ) { return $slot; } } return ''; } public function route_query( $query ): void { if ( ! $query instanceof \WP_Query || ! $query->is_main_query() || '' === $this->route_of( $query ) ) { return; } $query->is_home = false; $query->set( 'post__in', [ 0 ] ); $query->set( 'posts_per_page', 1 ); $query->set( 'no_found_rows', true ); $query->set( 'ignore_sticky_posts', true ); } public function route_not_404( $preempt, $query ) { if ( $query instanceof \WP_Query && $query->is_main_query() && '' !== $this->route_of( $query ) ) { status_header( 200 ); return true; } return $preempt; } private function current_route(): ?array { $current = $this->current(); $def = $current ? $this->get( (string) $current['slot'] ) : null; return $def && 'route' === $def['kind'] ? $def : null; } public function route_title( $parts ) { $def = $this->current_route(); if ( ! $def || 'theme' !== $def['seo'] ) { return $parts; } $title = is_callable( $def['title'] ) ? (string) call_user_func( $def['title'] ) : (string) $def['title']; $parts = (array) $parts; $parts['title'] = '' !== $title ? $title : (string) $def['label']; return $parts; } public function route_robots( $robots ) { $def = $this->current_route(); if ( $def && $def['app'] ) { $robots = (array) $robots; $robots['noindex'] = true; $robots['nofollow'] = true; } return $robots; } public function is_app(): bool { $current = $this->current(); $def = $current ? $this->get( (string) $current['slot'] ) : null; $template = $current ? ( '' !== $current['template'] ? $current['template'] : $current['default'] ) : ''; $app = ( $def && ! empty( $def['app'] ) ) || in_array( $template, [ 'page-app', 'route-app' ], true ); return (bool) apply_filters( 'rooster/app_area', $app, $current ); } public function all(): array { if ( ! $this->registered ) { $this->registered = true; $this->builtin(); do_action( 'rooster/templates/register_slots', $this ); uasort( $this->slots, static fn ( $a, $b ) => ( $a['priority'] <=> $b['priority'] ) ); } return $this->slots; } public function get( string $slot ): ?array { return $this->all()[ $slot ] ?? null; } private function builtin(): void { $this->register( 'rooster/post', [ 'label' => __( 'Post', 'ai-eshop-optimizer' ), 'kind' => 'single', 'post_types' => [ 'post' ], 'default' => 'single', 'matches' => [ 'object', 'term', 'role', 'lang', 'meta', 'auth', 'always' ], 'source' => 'theme', 'priority' => 20 ] ); $this->register( 'rooster/category', [ 'label' => __( 'Category archive', 'ai-eshop-optimizer' ), 'kind' => 'taxonomy', 'taxonomies' => [ 'category' ], 'default' => 'archive', 'matches' => [ 'term', 'role', 'lang', 'auth', 'always' ], 'source' => 'theme', 'priority' => 20 ] ); $woo = class_exists( 'WooCommerce' ); if ( $woo ) { $this->register( 'woocommerce/shop', [ 'label' => __( 'Product catalogue', 'ai-eshop-optimizer' ), 'kind' => 'archive', 'post_types' => [ 'product' ], 'default' => 'archive-product', 'matches' => [ 'role', 'lang', 'always' ], 'capability' => 'manage_woocommerce', 'source' => 'theme' ] ); $this->register( 'woocommerce/product', [ 'label' => __( 'Product', 'ai-eshop-optimizer' ), 'kind' => 'single', 'post_types' => [ 'product' ], 'default' => 'single-product', 'matches' => [ 'object', 'term', 'role', 'lang', 'meta', 'always' ], 'capability' => 'manage_woocommerce', 'source' => 'theme' ] ); $this->register( 'woocommerce/product-category', [ 'label' => __( 'Product category', 'ai-eshop-optimizer' ), 'kind' => 'taxonomy', 'taxonomies' => [ 'product_cat' ], 'default' => 'taxonomy-product_cat', 'matches' => [ 'term', 'role', 'lang', 'always' ], 'capability' => 'manage_woocommerce', 'source' => 'theme' ] ); $this->register( 'woocommerce/product-tag', [ 'label' => __( 'Product tag', 'ai-eshop-optimizer' ), 'kind' => 'taxonomy', 'taxonomies' => [ 'product_tag' ], 'default' => 'taxonomy-product_tag', 'matches' => [ 'term', 'always' ], 'capability' => 'manage_woocommerce', 'source' => 'theme' ] ); $this->register( 'woocommerce/cart', [ 'label' => __( 'Cart', 'ai-eshop-optimizer' ), 'kind' => 'page', 'page_option' => 'woocommerce_cart_page_id', 'default' => 'page-cart', 'matches' => [ 'role', 'lang', 'always' ], 'capability' => 'manage_woocommerce', 'source' => 'theme' ] ); $this->register( 'woocommerce/checkout', [ 'label' => __( 'Checkout', 'ai-eshop-optimizer' ), 'kind' => 'page', 'page_option' => 'woocommerce_checkout_page_id', 'default' => 'page-checkout', 'matches' => [ 'role', 'lang', 'always' ], 'capability' => 'manage_woocommerce', 'source' => 'theme' ] ); $this->register( 'woocommerce/my-account', [ 'label' => __( 'My account', 'ai-eshop-optimizer' ), 'kind' => 'page', 'page_option' => 'woocommerce_myaccount_page_id', 'default' => 'page', 'matches' => [ 'role', 'lang', 'always' ], 'capability' => 'manage_woocommerce', 'source' => 'theme' ] ); } if ( post_type_exists( 'rooster_course' ) || class_exists( 'RoosterLMS\\Content\\PostTypes' ) ) { $lms_page = static fn ( string $key ): ?callable => static function () use ( $key ): int { return class_exists( 'RoosterLMS\\Core\\Settings' ) ? (int) \RoosterLMS\Core\Settings::get( $key ) : 0; }; $this->register( 'rooster-lms/course', [ 'label' => __( 'Course', 'ai-eshop-optimizer' ), 'kind' => 'single', 'post_types' => [ 'rooster_course' ], 'default' => 'single-rooster_course', 'matches' => [ 'object', 'term', 'org', 'role', 'lang', 'meta', 'always' ], 'resolve' => [ self::class, 'resolve_lms_course' ], 'source' => 'theme', ] ); $this->register( 'rooster-lms/course-archive', [ 'label' => __( 'Course catalogue', 'ai-eshop-optimizer' ), 'kind' => 'archive', 'post_types' => [ 'rooster_course' ], 'default' => 'archive-rooster_course', 'matches' => [ 'org', 'role', 'lang', 'always' ], 'source' => 'theme' ] ); $this->register( 'rooster-lms/course-category', [ 'label' => __( 'Course category', 'ai-eshop-optimizer' ), 'kind' => 'taxonomy', 'taxonomies' => [ 'rooster_course_category' ], 'default' => 'taxonomy-rooster_course_category', 'matches' => [ 'term', 'org', 'role', 'lang', 'always' ], 'source' => 'theme' ] ); $this->register( 'rooster-lms/lesson', [ 'label' => __( 'Lesson', 'ai-eshop-optimizer' ), 'kind' => 'single', 'post_types' => [ 'rooster_lesson' ], 'default' => 'single-rooster_lesson', 'matches' => [ 'object', 'term', 'org', 'role', 'lang', 'always' ], 'source' => 'theme' ] ); $this->register( 'rooster-lms/lesson-category', [ 'label' => __( 'Lesson category', 'ai-eshop-optimizer' ), 'kind' => 'taxonomy', 'taxonomies' => [ 'rooster_lesson_category' ], 'default' => 'taxonomy-rooster_lesson_category', 'matches' => [ 'term', 'always' ], 'source' => 'theme' ] ); $this->register( 'rooster-lms/quiz', [ 'label' => __( 'Quiz', 'ai-eshop-optimizer' ), 'kind' => 'single', 'post_types' => [ 'rooster_quiz' ], 'default' => 'single-rooster_quiz', 'matches' => [ 'object', 'org', 'role', 'lang', 'always' ], 'source' => 'theme' ] ); $this->register( 'rooster-lms/person', [ 'label' => __( 'Person (instructor)', 'ai-eshop-optimizer' ), 'kind' => 'single', 'post_types' => [ 'rooster_person' ], 'default' => 'single-rooster_person', 'matches' => [ 'object', 'role', 'lang', 'always' ], 'source' => 'theme' ] ); $this->register( 'rooster-lms/account', [ 'label' => __( 'Learner account', 'ai-eshop-optimizer' ), 'kind' => 'page', 'page_id' => $lms_page( 'account_page_id' ), 'default' => 'page', 'matches' => [ 'org', 'role', 'lang', 'always' ], 'source' => 'theme' ] ); $this->register( 'rooster-lms/org-portal', [ 'label' => __( 'Organisation portal', 'ai-eshop-optimizer' ), 'kind' => 'page', 'page_id' => $lms_page( 'portal_page_id' ), 'default' => 'page', 'matches' => [ 'org', 'role', 'lang', 'always' ], 'source' => 'theme' ] ); $this->register( 'rooster-lms/register', [ 'label' => __( 'Registration', 'ai-eshop-optimizer' ), 'kind' => 'page', 'page_id' => $lms_page( 'registration_page_id' ), 'default' => 'page', 'matches' => [ 'org', 'lang', 'always' ], 'source' => 'theme' ] ); $this->register( 'rooster-lms/certificate-verify', [ 'label' => __( 'Certificate verification', 'ai-eshop-optimizer' ), 'kind' => 'page', 'page_id' => $lms_page( 'certificate_verify_page_id' ), 'default' => 'page', 'matches' => [ 'lang', 'always' ], 'source' => 'theme' ] ); } } public static function resolve_lms_course( string $slot, array $context ): string { $post_id = (int) ( $context['object_id'] ?? 0 ); if ( $post_id <= 0 || ! class_exists( 'RoosterLMS\\CourseInfo\\PageTemplates' ) || ! class_exists( 'RoosterLMS\\Domain\\Services' ) ) { return ''; } try { $course_id = (int) \RoosterLMS\Domain\Services::courses()->resolve_post( $post_id ); return $course_id > 0 ? (string) \RoosterLMS\CourseInfo\PageTemplates::resolve( $course_id ) : ''; } catch ( \Throwable $e ) { return ''; } } public function candidates( string $slot ): array { $def = $this->get( $slot ); if ( ! $def ) { return []; } $default = (string) $def['default']; $candidates = []; $add = static function ( string $slug, string $label, string $description, string $source ) use ( &$candidates ): void { if ( '' !== $slug && ! isset( $candidates[ $slug ] ) ) { $candidates[ $slug ] = [ 'label' => $label, 'description' => $description, 'source' => $source, 'preview' => '' ]; } }; if ( '' !== $default ) { $add( $default, sprintf( __( 'Default (%s)', 'ai-eshop-optimizer' ), $default ), '', 'theme' ); } foreach ( get_block_templates( [], 'wp_template' ) as $template ) { $slug = (string) $template->slug; if ( str_ends_with( $slug, '-elementor' ) ) { continue; } if ( '' !== $default && ( $slug === $default || str_starts_with( $slug, $default . '-' ) ) ) { $add( $slug, (string) $template->title, (string) $template->description, 'custom' === $template->source ? 'user' : 'theme' ); } } $post_types = (array) $def['post_types']; if ( $post_types && class_exists( 'WP_Theme_JSON_Resolver' ) ) { foreach ( (array) \WP_Theme_JSON_Resolver::get_theme_data()->get_custom_templates() as $slug => $tpl ) { if ( array_intersect( $post_types, (array) ( $tpl['postTypes'] ?? [] ) ) ) { $add( (string) $slug, (string) ( $tpl['title'] ?? $slug ), '', 'theme' ); } } } $labels = (array) apply_filters( 'rooster/templates/candidate_labels', self::LABELS ); foreach ( $candidates as $slug => &$c ) { if ( isset( $labels[ $slug ] ) ) { $c['label'] = (string) $labels[ $slug ]; } } unset( $c ); $candidates = (array) apply_filters( 'rooster/templates/candidates', $candidates, $slot, $def ); foreach ( $candidates as $slug => &$c ) { $file = ROOSTER_DIR . '/screenshots/' . $slug . '.png'; if ( '' === ( $c['preview'] ?? '' ) && is_file( $file ) ) { $c['preview'] = ROOSTER_URI . '/screenshots/' . $slug . '.png'; } } return $candidates; } public function pins(): array { if ( null === $this->pins ) { $pins = get_option( self::OPTION, [] ); $this->pins = is_array( $pins ) ? $pins : []; } return (array) apply_filters( 'rooster/templates/pins', $this->pins ); } public function set_pins( array $pins ): bool { $clean = []; foreach ( $pins as $slot => $entry ) { $slot = (string) $slot; if ( ! is_array( $entry ) ) { continue; } $rules = []; foreach ( (array) ( $entry['rules'] ?? [] ) as $rule ) { $rule = self::sanitize_rule( (array) $rule ); if ( $rule ) { $rules[] = $rule; } } usort( $rules, static fn ( $a, $b ) => $a['priority'] <=> $b['priority'] ); $default = self::clean_slug( (string) ( $entry['default'] ?? '' ) ); if ( '' === $default && ! $rules ) { continue; } $clean[ $slot ] = [ 'default' => $default, 'rules' => $rules ]; } $this->pins = $clean; if ( $clean === ( get_option( self::OPTION, [] ) ?: [] ) ) { return true; } $ok = update_option( self::OPTION, $clean, false ); if ( ! $ok && ! get_option( self::OPTION ) ) { $ok = add_option( self::OPTION, $clean, '', false ); } return (bool) $ok; } public function set_default( string $slot, string $template ): bool { $pins = $this->pins(); $pins[ $slot ] = [ 'default' => self::clean_slug( $template ), 'rules' => $pins[ $slot ]['rules'] ?? [] ]; return $this->set_pins( $pins ); } public function add_rule( string $slot, array $rule ): bool { $pins = $this->pins(); $pins[ $slot ]['default'] ??= ''; $pins[ $slot ]['rules'][] = $rule; return $this->set_pins( $pins ); } public function remove_rule( string $slot, int $index ): bool { $pins = $this->pins(); if ( isset( $pins[ $slot ]['rules'][ $index ] ) ) { unset( $pins[ $slot ]['rules'][ $index ] ); $pins[ $slot ]['rules'] = array_values( $pins[ $slot ]['rules'] ); } return $this->set_pins( $pins ); } public static function sanitize_rule( array $rule ): ?array { $match = (array) ( $rule['match'] ?? [] ); $kind = (string) ( $match['kind'] ?? '' ); $template = self::clean_slug( (string) ( $rule['template'] ?? '' ) ); if ( '' === $template || ! in_array( $kind, self::MATCH_KINDS, true ) ) { return null; } $clean = [ 'kind' => $kind ]; switch ( $kind ) { case 'object': case 'org': case 'page': $clean['id'] = (int) ( $match['id'] ?? 0 ); if ( $clean['id'] <= 0 ) { return null; } break; case 'term': $clean['taxonomy'] = sanitize_key( (string) ( $match['taxonomy'] ?? '' ) ); $clean['id'] = (int) ( $match['id'] ?? 0 ); if ( '' === $clean['taxonomy'] || $clean['id'] <= 0 ) { return null; } break; case 'role': $clean['role'] = sanitize_key( (string) ( $match['role'] ?? '' ) ); if ( '' === $clean['role'] ) { return null; } break; case 'lang': $clean['lang'] = sanitize_key( (string) ( $match['lang'] ?? '' ) ); if ( '' === $clean['lang'] ) { return null; } break; case 'meta': $clean['key'] = sanitize_key( (string) ( $match['key'] ?? '' ) ); $clean['value'] = sanitize_text_field( (string) ( $match['value'] ?? '' ) ); if ( '' === $clean['key'] ) { return null; } break; case 'auth': $clean['auth'] = (string) ( $match['auth'] ?? '' ); if ( ! in_array( $clean['auth'], [ 'signed-in', 'guest' ], true ) ) { return null; } break; } return [ 'match' => $clean, 'template' => $template, 'priority' => (int) ( $rule['priority'] ?? 10 ) ]; } public static function clean_slug( string $slug ): string { $slug = strtolower( trim( $slug ) ); return preg_match( '/^[a-z0-9][a-z0-9_-]{0,127}$/', $slug ) ? $slug : ''; } public function current_slot(): string { return (string) ( $this->current()['slot'] ?? '' ); } public function current_template(): string { return (string) ( $this->current()['template'] ?? '' ); } public function current(): ?array { if ( $this->resolved ) { return $this->current; } $this->resolved = true; $query = $GLOBALS['wp_query'] ?? null; if ( is_admin() || ! did_action( 'wp' ) && ! ( $query instanceof \WP_Query && $query->is_main_query() && null !== $query->query ) ) { $this->resolved = false; return null; } $slots = $this->all(); uasort( $slots, static fn ( $a, $b ) => ( (int) $a['priority'] ) <=> ( (int) $b['priority'] ) ); foreach ( $slots as $slot => $def ) { if ( $this->matches_query( $def ) ) { $context = $this->context( $def ); $template = $this->resolve( $slot, $context ); $this->current = [ 'slot' => $slot, 'template' => $template, 'default' => (string) $def['default'] ]; return $this->current; } } $this->current = null; return null; } public function render_current(): string { if ( null !== $this->rendered ) { return $this->rendered; } $this->rendered = ''; $current = $this->current(); $def = $current ? $this->get( (string) $current['slot'] ) : null; if ( $def && 'route' === ( $def['kind'] ?? '' ) && is_callable( $def['render'] ?? null ) ) { $this->rendered = (string) call_user_func( $def['render'], (string) $current['slot'], $this->context( $def ) ); } return $this->rendered; } private function matches_query( array $def ): bool { if ( is_callable( $def['is_current'] ) ) { return (bool) call_user_func( $def['is_current'] ); } switch ( $def['kind'] ) { case 'single': return $def['post_types'] && is_singular( $def['post_types'] ); case 'archive': return $def['post_types'] && is_post_type_archive( $def['post_types'] ); case 'taxonomy': return $def['taxonomies'] && ( is_tax( $def['taxonomies'] ) || ( in_array( 'category', $def['taxonomies'], true ) && is_category() ) || ( in_array( 'post_tag', $def['taxonomies'], true ) && is_tag() ) ); case 'page': $id = $this->bound_page( $def ); return $id > 0 && is_page( $id ); case 'route': return '' !== (string) $def['query_var'] && '' !== (string) get_query_var( (string) $def['query_var'], '' ); } return false; } public function bound_page( array $def ): int { $id = 0; if ( is_callable( $def['page_id'] ) ) { $id = (int) call_user_func( $def['page_id'] ); } elseif ( is_string( $def['page_option'] ) && '' !== $def['page_option'] ) { $id = (int) get_option( $def['page_option'], 0 ); } if ( $id > 0 ) { $id = (int) apply_filters( 'wpml_object_id', $id, 'page', true ); if ( function_exists( 'pll_get_post' ) ) { $id = (int) ( pll_get_post( $id ) ?: $id ); } } return $id; } public function context( array $def ): array { $object = get_queried_object(); $object_id = (int) get_queried_object_id(); $post_type = $object instanceof \WP_Post ? $object->post_type : ''; $terms = []; $with_ancestors = static function ( string $tax, array $ids ): array { $ids = array_map( 'intval', $ids ); if ( is_taxonomy_hierarchical( $tax ) ) { foreach ( $ids as $id ) { $ids = array_merge( $ids, array_map( 'intval', get_ancestors( $id, $tax, 'taxonomy' ) ) ); } } return array_values( array_unique( $ids ) ); }; if ( $object instanceof \WP_Term ) { $terms[ $object->taxonomy ] = $with_ancestors( $object->taxonomy, [ (int) $object->term_id ] ); } elseif ( $object instanceof \WP_Post ) { update_object_term_cache( [ $object->ID ], $object->post_type ); foreach ( get_object_taxonomies( $object->post_type ) as $tax ) { $found = get_the_terms( $object, $tax ); if ( is_array( $found ) && $found ) { $terms[ $tax ] = $with_ancestors( $tax, array_map( 'intval', wp_list_pluck( $found, 'term_id' ) ) ); } } } $lang = ''; if ( defined( 'ICL_LANGUAGE_CODE' ) ) { $lang = (string) ICL_LANGUAGE_CODE; } elseif ( function_exists( 'pll_current_language' ) ) { $lang = (string) pll_current_language( 'slug' ); } $context = [ 'object_id' => $object_id, 'post_type' => $post_type, 'terms' => $terms, 'roles' => rooster_current_user_roles(), 'auth' => is_user_logged_in() ? 'signed-in' : 'guest', 'lang' => $lang, 'org' => 0, 'meta' => static fn ( string $key ): string => $object instanceof \WP_Post ? (string) get_post_meta( $object->ID, $key, true ) : '', ]; return (array) apply_filters( 'rooster/templates/context', $context, $def ); } public function resolve( string $slot, array $context ): string { $def = $this->get( $slot ); if ( ! $def ) { return ''; } if ( is_callable( $def['resolve'] ) ) { $slug = self::clean_slug( (string) call_user_func( $def['resolve'], $slot, $context ) ); if ( '' !== $slug ) { return $slug; } } $pins = $this->pins()[ $slot ] ?? []; $rules = (array) ( $pins['rules'] ?? [] ); usort( $rules, static fn ( $a, $b ) => ( (int) ( $a['priority'] ?? 10 ) ) <=> ( (int) ( $b['priority'] ?? 10 ) ) ); foreach ( $rules as $rule ) { if ( $this->rule_matches( (array) ( $rule['match'] ?? [] ), $context ) ) { return self::clean_slug( (string) ( $rule['template'] ?? '' ) ); } } return self::clean_slug( (string) ( $pins['default'] ?? '' ) ); } public function rule_matches( array $match, array $context ): bool { switch ( $match['kind'] ?? '' ) { case 'always': return true; case 'object': case 'page': return (int) ( $match['id'] ?? 0 ) === (int) ( $context['object_id'] ?? 0 ); case 'term': $ids = (array) ( $context['terms'][ $match['taxonomy'] ?? '' ] ?? [] ); return in_array( (int) ( $match['id'] ?? 0 ), $ids, true ); case 'org': return (int) ( $match['id'] ?? 0 ) === (int) ( $context['org'] ?? 0 ); case 'role': return in_array( (string) ( $match['role'] ?? '' ), (array) ( $context['roles'] ?? [] ), true ); case 'lang': return (string) ( $match['lang'] ?? '' ) === (string) ( $context['lang'] ?? '' ); case 'meta': $read = $context['meta'] ?? null; return is_callable( $read ) && (string) $read( (string) ( $match['key'] ?? '' ) ) === (string) ( $match['value'] ?? '' ); case 'auth': $auth = (string) ( $context['auth'] ?? ( in_array( 'guest', (array) ( $context['roles'] ?? [] ), true ) ? 'guest' : 'signed-in' ) ); return (string) ( $match['auth'] ?? '' ) === $auth; } return false; } public function hierarchy( $templates ) { $templates = (array) $templates; $current = $this->current(); $slug = (string) ( $current['template'] ?? '' ); if ( '' === $slug && $current && 'route' === ( $this->get( (string) $current['slot'] )['kind'] ?? '' ) ) { $slug = (string) $current['default']; } if ( '' === $slug ) { return $templates; } if ( str_starts_with( $slug, 'elementor-' ) ) { $slug = (string) apply_filters( 'rooster/templates/elementor_template', '', $slug, $current ); if ( '' === $slug ) { return $templates; } } return array_values( array_unique( array_merge( [ $slug ], $templates ) ) ); } public function body_class( $classes ): array { $classes = (array) $classes; $current = $this->current(); if ( $current ) { $classes[] = 'rooster-slot-' . sanitize_html_class( str_replace( '/', '-', $current['slot'] ) ); $template = '' !== $current['template'] ? $current['template'] : $current['default']; if ( '' !== $template ) { $classes[] = 'rooster-template-' . sanitize_html_class( $template ); } if ( 'route' === ( $this->get( (string) $current['slot'] )['kind'] ?? '' ) ) { $classes[] = 'rooster-route'; } if ( $this->is_app() ) { $classes[] = 'rooster-app'; } } return $classes; } public static function picker( string $slot, array $match = [] ): string { $self = self::instance(); $def = $self->get( $slot ); if ( ! $def || ! current_user_can( (string) $def['capability'] ) ) { return ''; } if ( is_admin() ) { $relative = 'assets/js/admin/template-picker.js'; wp_enqueue_script( 'rooster-template-picker', AIEO_ENGINE_URL . '/' . $relative, [ 'wp-api-fetch', 'wp-i18n' ], (string) filemtime( AIEO_ENGINE_DIR . '/' . $relative ), true ); wp_set_script_translations( 'rooster-template-picker', 'ai-eshop-optimizer', AIEO_ENGINE_LANGUAGES ); } $candidates = $self->candidates( $slot ); $pins = $self->pins()[ $slot ] ?? []; $current = ''; if ( $match ) { foreach ( (array) ( $pins['rules'] ?? [] ) as $rule ) { if ( ( $rule['match'] ?? null ) == $match ) { $current = (string) $rule['template']; } } } else { $current = (string) ( $pins['default'] ?? '' ); } $id = 'rooster-picker-' . sanitize_html_class( str_replace( '/', '-', $slot ) ) . ( $match ? '-' . substr( md5( wp_json_encode( $match ) ), 0, 6 ) : '' ); $out = sprintf( '', esc_attr( $slot ), esc_attr( (string) wp_json_encode( (object) $match ) ) ); $out .= sprintf( '', esc_attr( $id ), esc_html( sprintf( __( 'Template for %s', 'ai-eshop-optimizer' ), (string) $def['label'] ) ) ); $out .= ' '; return $out; } }