array_merge( (array) $classes, [ 'rooster-elementor-mode' ] ) );
}
public static function register_locations( $manager ): void {
foreach ( [ 'header', 'footer' ] as $location ) {
if ( method_exists( $manager, 'register_core_location' ) ) {
$manager->register_core_location( $location );
} elseif ( method_exists( $manager, 'register_location' ) ) {
$manager->register_location( $location );
}
}
}
public static function place_part( $html, $block ): string {
$html = (string) $html;
$location = self::part_location( (string) ( $block['attrs']['slug'] ?? '' ) );
if ( '' === $location || ! preg_match( '/^(\s*<([a-z][a-z0-9_:]*)\b[^>]*>)(.*)(<\/\2>\s*)$/s', $html, $m ) ) {
return $html;
}
if ( str_contains( $m[3], 'rooster-builder-location' ) ) {
return $html;
}
return $m[1] . self::location( $location, $m[3] ) . $m[4];
}
private static function part_location( string $slug ): string {
return preg_match( '/^(header|footer)(-stacked|-accordion)?(-mobile)?$/', $slug, $m ) ? $m[1] : '';
}
public static function location( string $location, string $fallback ): string {
if ( ! self::active() || ! in_array( $location, [ 'header', 'footer' ], true ) ) {
return $fallback;
}
$class = 'rooster-builder-location rooster-builder-location-' . $location;
if ( function_exists( 'elementor_theme_do_location' ) ) {
ob_start();
$did = (bool) elementor_theme_do_location( $location );
$out = trim( (string) ob_get_clean() );
if ( $did && '' !== $out ) {
return '
' . $out . '
';
}
}
if ( ! class_exists( 'Header_Footer_Elementor' ) ) {
return $fallback;
}
$out = '';
if ( 'header' === $location && function_exists( 'hfe_header_enabled' ) && hfe_header_enabled() ) {
ob_start();
\Header_Footer_Elementor::get_header_content();
$out = trim( (string) ob_get_clean() );
return '' !== $out ? '' . $out . '
' : $fallback;
}
if ( 'footer' === $location ) {
if ( function_exists( 'hfe_is_before_footer_enabled' ) && hfe_is_before_footer_enabled() && method_exists( 'Header_Footer_Elementor', 'get_before_footer_content' ) ) {
ob_start();
\Header_Footer_Elementor::get_before_footer_content();
$out .= (string) ob_get_clean();
}
if ( function_exists( 'hfe_footer_enabled' ) && hfe_footer_enabled() ) {
ob_start();
\Header_Footer_Elementor::get_footer_content();
$out .= (string) ob_get_clean();
}
$out = trim( $out );
return '' !== $out ? '' . $out . '
' : $fallback;
}
return $fallback;
}
public static function content( int $template_id ): string {
if ( ! self::active() || $template_id <= 0 || ! class_exists( '\Elementor\Plugin' ) ) {
return '';
}
try {
return trim( (string) \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $template_id, true ) );
} catch ( \Throwable $e ) {
return '';
}
}
public static function is_built_with_elementor( int $post_id ): bool {
if ( $post_id <= 0 ) {
return false;
}
try {
if ( class_exists( '\Elementor\Plugin' ) && isset( \Elementor\Plugin::$instance->documents ) ) {
$document = \Elementor\Plugin::$instance->documents->get( $post_id );
if ( $document && method_exists( $document, 'is_built_with_elementor' ) ) {
return (bool) $document->is_built_with_elementor();
}
}
} catch ( \Throwable $e ) {
}
return 'builder' === get_post_meta( $post_id, '_elementor_edit_mode', true );
}
public static function full_width_hierarchy( $templates ): array {
$templates = (array) $templates;
if ( ! is_singular( [ 'page', 'post' ] ) ) {
return $templates;
}
$post = get_queried_object();
if ( ! $post instanceof \WP_Post || ! self::is_built_with_elementor( (int) $post->ID ) ) {
return $templates;
}
if ( '' !== (string) get_page_template_slug( $post ) ) {
return $templates;
}
if ( ! apply_filters( 'rooster/elementor/auto_full_width', true, $post ) ) {
return $templates;
}
return array_values( array_unique( array_merge( [ self::FULL_WIDTH ], $templates ) ) );
}
public static function template_exists( string $slug ): bool {
return '' !== $slug && ( is_file( get_stylesheet_directory() . '/templates/' . $slug . '.html' ) || is_file( get_template_directory() . '/templates/' . $slug . '.html' ) );
}
public static function course_template( $template, string $resolved, array $current ): string {
$default = (string) ( $current['default'] ?? '' );
if ( '' !== (string) $template || '' === $default || ! preg_match( '/^elementor-\d+$/', $resolved ) ) {
return (string) $template;
}
return self::template_exists( $default . '-elementor' ) ? $default . '-elementor' : '';
}
public static function candidates( $candidates, string $slot, array $def ): array {
$candidates = (array) $candidates;
$default = (string) ( $def['default'] ?? '' );
if ( '' === $default || ! self::template_exists( $default . '-elementor' ) ) {
return $candidates;
}
foreach ( self::library() as $id => $title ) {
$candidates[ 'elementor-' . $id ] ??= [
'label' => sprintf( __( 'Elementor: %s', 'ai-eshop-optimizer' ), $title ),
'description' => __( 'An Elementor library template rendered between the header and footer parts.', 'ai-eshop-optimizer' ),
'source' => 'plugin',
'preview' => '',
];
}
return $candidates;
}
public static function library(): array {
if ( null !== self::$library ) {
return self::$library;
}
self::$library = [];
if ( ! post_type_exists( 'elementor_library' ) ) {
return self::$library;
}
$posts = get_posts( [
'post_type' => 'elementor_library',
'post_status' => 'publish',
'posts_per_page' => 100,
'orderby' => 'title',
'order' => 'ASC',
'no_found_rows' => true,
'update_post_term_cache' => false,
'meta_query' => [ [ 'key' => '_elementor_template_type', 'value' => [ 'page', 'section', 'container', 'e-flexbox' ], 'compare' => 'IN' ] ],
] );
foreach ( $posts as $post ) {
self::$library[ (int) $post->ID ] = (string) $post->post_title;
}
return self::$library;
}
public static function render_page_template( $html, string $key, int $course_id = 0 ): string {
if ( '' !== (string) $html || ! preg_match( '/^elementor-(\d+)$/', $key, $m ) ) {
return (string) $html;
}
$content = self::content( (int) $m[1] );
if ( '' === $content ) {
return (string) $html;
}
return do_blocks( '' )
. '' . $content . ''
. do_blocks( '' );
}
public static function sync_kit_width(): void {
if ( self::$syncing || ! self::active() || ! class_exists( '\Elementor\Plugin' ) ) {
return;
}
try {
$kit = \Elementor\Plugin::$instance->kits_manager->get_active_kit();
if ( ! $kit || ! $kit->get_main_id() ) {
return;
}
$wide = (int) preg_replace( '/\D+/', '', (string) wp_get_global_settings( [ 'layout', 'wideSize' ] ) );
if ( $wide <= 0 ) {
return;
}
$current = (array) $kit->get_settings( 'container_width' );
if ( (int) ( $current['size'] ?? 0 ) === $wide && 'px' === (string) ( $current['unit'] ?? 'px' ) ) {
return;
}
self::$syncing = true;
$kit->update_settings( [ 'container_width' => [ 'unit' => 'px', 'size' => $wide, 'sizes' => [] ] ] );
\Elementor\Plugin::$instance->files_manager->clear_cache();
} catch ( \Throwable $e ) {
} finally {
self::$syncing = false;
}
}
public static function after_document_save( $document ): void {
if ( class_exists( '\Elementor\Core\Kits\Documents\Kit' ) && $document instanceof \Elementor\Core\Kits\Documents\Kit ) {
self::sync_kit_width();
}
}
}