integration class. * * @var array */ const KNOWN_THEMES = [ 'astra' => Themes\Astra::class, 'generatepress' => Themes\GeneratePress::class, 'oceanwp' => Themes\OceanWP::class, 'hello-elementor' => Themes\Hello_Elementor::class, ]; /** * The method that the loader picked for this request. * * @var string */ private static $method = ''; /** * Adds the hooks. */ public function __construct() { add_action( 'after_setup_theme', [ $this, 'setup' ], 20 ); } /** * Resolves the integration method and starts it. * * @return void */ public function setup() { $mode = Settings::get( 'theme_mode' ); if ( Plugin::instance()->is_uae_active() ) { $mode = 'off'; } /** * Filters the theme integration mode. * * @param string $mode auto, theme_support, generic, or off. */ $mode = apply_filters( 'headfoot_theme_mode', $mode ); switch ( $mode ) { case 'off': self::$method = 'off'; return; case 'theme_support': self::$method = 'theme_support'; break; case 'generic': self::$method = 'generic'; break; default: self::$method = self::detect_auto_method(); } $this->start( self::$method ); // Canvas pages print no theme header, so every method except off needs this. new Canvas(); } /** * Starts the integration for a method. * * @param string $method Method name. * @return void */ private function start( $method ) { if ( 'generic' === $method ) { new Generic(); return; } if ( 'block_theme' === $method ) { new Block_Theme(); return; } if ( isset( self::KNOWN_THEMES[ $method ] ) ) { $class = self::KNOWN_THEMES[ $method ]; new $class(); } } /** * Returns the method that the automatic mode picks for the active theme. * * The order is theme support, known theme, block theme, generic fallback. * * @return string */ public static function detect_auto_method() { if ( current_theme_supports( 'headfoot' ) || current_theme_supports( 'header-footer-elementor' ) ) { return 'theme_support'; } $known = self::get_known_theme(); if ( $known ) { return $known; } if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) { return 'block_theme'; } return 'generic'; } /** * Returns the slug of the known theme that is active, or an empty string. * * The parent theme counts, so child themes work. * * @return string */ public static function get_known_theme() { foreach ( [ get_template(), get_stylesheet() ] as $slug ) { if ( isset( self::KNOWN_THEMES[ $slug ] ) ) { return $slug; } } return ''; } /** * Returns the method that the loader picked. * * @return string off, theme_support, generic, block_theme, or a known theme slug. */ public static function get_method() { return self::$method; } /** * Returns a method name in plain words. * * @param string $method Method name. * @return string */ public static function describe_method( $method ) { switch ( $method ) { case 'off': return esc_html__( 'Off. The plugin renders nothing.', 'headfoot' ); case 'theme_support': return esc_html__( 'Theme support. The theme calls the plugin functions.', 'headfoot' ); case 'generic': return esc_html__( 'Generic fallback. The plugin replaces header.php and footer.php.', 'headfoot' ); case 'block_theme': return esc_html__( 'Block theme. The plugin replaces the header and footer template parts.', 'headfoot' ); } if ( isset( self::KNOWN_THEMES[ $method ] ) ) { $theme = wp_get_theme( $method ); $name = $theme->exists() ? $theme->get( 'Name' ) : $method; /* translators: %s: Theme name. */ return sprintf( esc_html__( 'Dedicated integration for %s.', 'headfoot' ), $name ); } return esc_html__( 'Not resolved yet.', 'headfoot' ); } }