|null */ private static $types = null; /** @var array|null */ private static $taxonomies = null; public static function init( Settings $settings ): void { self::$settings = $settings; add_action( 'registered_post_type', array( __CLASS__, 'reset' ) ); add_action( 'registered_taxonomy', array( __CLASS__, 'reset' ) ); } public static function reset(): void { self::$types = null; self::$taxonomies = null; } /** * Candidate post types that can be enabled in settings. * * @return array name => label */ public static function available(): array { $out = array(); foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $name => $obj ) { if ( ! in_array( $name, self::EXCLUDED_TYPES, true ) ) { $out[ $name ] = $obj->labels->name; } } return $out; } /** @return array */ public static function available_taxonomies(): array { $out = array(); foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $name => $obj ) { if ( ! in_array( $name, self::EXCLUDED_TAXONOMIES, true ) ) { $out[ $name ] = $obj->labels->name; } } return $out; } /** @return string[] */ public static function enabled(): array { if ( null === self::$types ) { $chosen = (array) self::$settings->get( 'post_types', array() ); $available = array_keys( self::available() ); $list = $chosen ? array_values( array_intersect( $chosen, $available ) ) : $available; self::$types = array_fill_keys( (array) apply_filters( 'translateplus_post_types', $list ), true ); } return array_keys( self::$types ); } /** @return string[] */ public static function enabled_taxonomies(): array { if ( null === self::$taxonomies ) { $chosen = (array) self::$settings->get( 'taxonomies', array() ); $available = array_keys( self::available_taxonomies() ); $list = $chosen ? array_values( array_intersect( $chosen, $available ) ) : $available; self::$taxonomies = array_fill_keys( (array) apply_filters( 'translateplus_taxonomies', $list ), true ); } return array_keys( self::$taxonomies ); } public static function is_translatable( string $type ): bool { if ( 'product_variation' === $type ) { return true; // Follows its parent product. } self::enabled(); return isset( self::$types[ $type ] ); } public static function is_translatable_taxonomy( string $taxonomy ): bool { self::enabled_taxonomies(); return isset( self::$taxonomies[ $taxonomy ] ); } }