register( 'my-product', __( 'My Product', 'my-product' ), $blocks, $args ); * * Cost: none. The loader adds no hook, no option, no cookie and no handle, on any request: registering a copy notes it * in one global (`rooster_blocks_candidates`), and nothing else happens until a product asks for the package. The * factory hooks Elementor only when Elementor is active; the token sheet loads only with a block that renders. * * @package RoosterBlocks */ defined( 'ABSPATH' ) || exit; if ( ! function_exists( 'rooster_blocks_register' ) ) { /** * Notes a copy of the package, on any request: a passive list, no hook. A copy without includes/Library.php or a * version is ignored. * * @param string $dir The copy's folder (holds rooster-blocks.php, version.php, includes/). * @param string $version The copy's version; read from the folder's version.php when ''. * @param string $url The copy's URL (a plugin: plugins_url( 'lib/rooster-blocks', __FILE__ )); '' = none yet * (the package has no asset in Phase 0). */ function rooster_blocks_register( string $dir, string $version = '', string $url = '' ): void { $dir = rtrim( str_replace( '\\', '/', $dir ), '/' ); if ( ! is_file( $dir . '/includes/Library.php' ) ) { return; } if ( '' === $version && is_file( $dir . '/version.php' ) ) { $read = include $dir . '/version.php'; $version = is_string( $read ) ? $read : ''; } if ( 1 !== preg_match( '/^\d+\.\d+\.\d+/', $version ) ) { return; } $candidates = is_array( $GLOBALS['rooster_blocks_candidates'] ?? null ) ? $GLOBALS['rooster_blocks_candidates'] : array(); $candidates[ $dir ] = $version; $GLOBALS['rooster_blocks_candidates'] = $candidates; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- the loader's own global. if ( '' !== $url ) { $urls = is_array( $GLOBALS['rooster_blocks_urls'] ?? null ) ? $GLOBALS['rooster_blocks_urls'] : array(); $urls[ $dir ] = rtrim( $url, '/' ); $GLOBALS['rooster_blocks_urls'] = $urls; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- the loader's own global. } } } if ( ! function_exists( 'rooster_blocks_newest' ) ) { /** * The folder of the newest registered copy ('' without any): the highest version; of equal versions, the first * registered. * * @param array $candidates Folder => version. */ function rooster_blocks_newest( array $candidates ): string { $best = ''; $version = ''; foreach ( $candidates as $dir => $candidate ) { if ( '' === $best || version_compare( (string) $candidate, $version, '>' ) ) { $best = (string) $dir; $version = (string) $candidate; } } return $best; } } if ( ! function_exists( 'rooster_blocks_loaded' ) ) { /** * The folder of the copy whose classes this process already holds ('' before any is loaded), as it was registered. * Once a copy's Library.php is in, that copy serves every later call of the request, whatever copy registers after * it: a process never holds two copies (the admin package's lesson, 0.75.2). */ function rooster_blocks_loaded(): string { if ( ! class_exists( '\Rooster\Blocks\Library', false ) ) { return ''; } $file = (string) ( new \ReflectionClass( '\Rooster\Blocks\Library' ) )->getFileName(); // Library.php sits in the copy's includes/ folder: the copy's folder is cut from that path as a string (the // wordpress.org review gate rejects a dirname() that climbs levels). $dir = rtrim( str_replace( '\\', '/', dirname( $file ) ), '/' ); if ( str_ends_with( $dir, '/includes' ) ) { $dir = substr( $dir, 0, -9 ); } $real = rtrim( str_replace( '\\', '/', (string) realpath( $dir ) ), '/' ); // The registered folder may reach the copy through a junction or a symbolic link while PHP names the included // file by its real path: the two are compared as real paths, and the registered spelling is what is returned. $known = is_array( $GLOBALS['rooster_blocks_candidates'] ?? null ) ? array_keys( $GLOBALS['rooster_blocks_candidates'] ) : array(); foreach ( $known as $registered ) { $spelled = rtrim( str_replace( '\\', '/', (string) $registered ), '/' ); if ( $spelled === $dir || ( '' !== $real && rtrim( str_replace( '\\', '/', (string) realpath( $spelled ) ), '/' ) === $real ) ) { return (string) $registered; } } return $dir; } } if ( ! function_exists( 'rooster_blocks_load' ) ) { /** * The package, on demand: the newest noted copy's classes, once per process; returns that copy's folder ('' when * no copy is noted, or before `init`: every product notes its copy on `plugins_loaded`, so a call before `init` * could load an older copy than the newest). Loading adds no hook: the copy's classes load on first use. */ function rooster_blocks_load(): string { $dir = rooster_blocks_loaded(); if ( '' !== $dir ) { return $dir; } if ( ! did_action( 'init' ) ) { return ''; } $candidates = is_array( $GLOBALS['rooster_blocks_candidates'] ?? null ) ? $GLOBALS['rooster_blocks_candidates'] : array(); $dir = rooster_blocks_newest( $candidates ); if ( '' === $dir ) { return ''; } require_once $dir . '/includes/Library.php'; $urls = is_array( $GLOBALS['rooster_blocks_urls'] ?? null ) ? $GLOBALS['rooster_blocks_urls'] : array(); \Rooster\Blocks\Library::load( $dir, is_string( $urls[ $dir ] ?? null ) ? $urls[ $dir ] : '' ); return $dir; } } if ( ! function_exists( 'rooster_blocks_elementor' ) ) { /** * The Elementor widget factory of the serving copy (blocks 1.0; docs/CONTRACT.md section 11), or null when the * package cannot load yet (before `init`, or no copy noted). A product registers its category and blocks with it * once, on `init`; without Elementor the factory notes them and adds no hook. * * rooster_blocks_elementor()?->register( 'rooster-one', __( 'Rooster One', 'ai-eshop-optimizer' ), $blocks, $args ); */ function rooster_blocks_elementor(): ?\Rooster\Blocks\Elementor\Factory { if ( '' === rooster_blocks_load() ) { return null; } return \Rooster\Blocks\Elementor\Factory::instance(); } }