@@ -41,7 +41,7 @@ |
||
| 41 | 41 | // $errors = []; |
| 42 | 42 | |
| 43 | 43 | try { |
| 44 | - if (empty($input['uniqueInputs']) || !is_array($input['uniqueInputs'])) { |
|
| 44 | + if (empty($input['uniqueInputs']) || ! is_array($input['uniqueInputs'])) { |
|
| 45 | 45 | throw new OutOfBoundsException( |
| 46 | 46 | esc_html__('A valid input was not provided.', 'event_espresso') |
| 47 | 47 | ); |
@@ -10,69 +10,69 @@ |
||
| 10 | 10 | class BulkEntityMutator extends EntityMutator |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @var callable $entity_mutator . |
|
| 15 | - */ |
|
| 16 | - protected $entity_mutator; |
|
| 13 | + /** |
|
| 14 | + * @var callable $entity_mutator . |
|
| 15 | + */ |
|
| 16 | + protected $entity_mutator; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * BulkEntityMutator constructor. |
|
| 20 | - * |
|
| 21 | - * @param callable $entity_mutator The mutator for an entity. |
|
| 22 | - */ |
|
| 23 | - public function __construct(callable $entity_mutator) |
|
| 24 | - { |
|
| 25 | - $this->entity_mutator = $entity_mutator; |
|
| 26 | - } |
|
| 27 | - /** |
|
| 28 | - * Bulk updates entities. |
|
| 29 | - * |
|
| 30 | - * @param array $input The input for the mutation |
|
| 31 | - * @param AppContext $context The AppContext passed down to all resolvers |
|
| 32 | - * @param ResolveInfo $info The ResolveInfo passed down to all resolvers |
|
| 33 | - * @return array |
|
| 34 | - */ |
|
| 35 | - public function updateEntities(array $input, AppContext $context, ResolveInfo $info): array |
|
| 36 | - { |
|
| 18 | + /** |
|
| 19 | + * BulkEntityMutator constructor. |
|
| 20 | + * |
|
| 21 | + * @param callable $entity_mutator The mutator for an entity. |
|
| 22 | + */ |
|
| 23 | + public function __construct(callable $entity_mutator) |
|
| 24 | + { |
|
| 25 | + $this->entity_mutator = $entity_mutator; |
|
| 26 | + } |
|
| 27 | + /** |
|
| 28 | + * Bulk updates entities. |
|
| 29 | + * |
|
| 30 | + * @param array $input The input for the mutation |
|
| 31 | + * @param AppContext $context The AppContext passed down to all resolvers |
|
| 32 | + * @param ResolveInfo $info The ResolveInfo passed down to all resolvers |
|
| 33 | + * @return array |
|
| 34 | + */ |
|
| 35 | + public function updateEntities(array $input, AppContext $context, ResolveInfo $info): array |
|
| 36 | + { |
|
| 37 | 37 | |
| 38 | - $updated = []; |
|
| 39 | - $failed = []; |
|
| 40 | - // TODO Add meaningful error messages for every failure. |
|
| 41 | - // $errors = []; |
|
| 38 | + $updated = []; |
|
| 39 | + $failed = []; |
|
| 40 | + // TODO Add meaningful error messages for every failure. |
|
| 41 | + // $errors = []; |
|
| 42 | 42 | |
| 43 | - try { |
|
| 44 | - if (empty($input['uniqueInputs']) || !is_array($input['uniqueInputs'])) { |
|
| 45 | - throw new OutOfBoundsException( |
|
| 46 | - esc_html__('A valid input was not provided.', 'event_espresso') |
|
| 47 | - ); |
|
| 48 | - } |
|
| 43 | + try { |
|
| 44 | + if (empty($input['uniqueInputs']) || !is_array($input['uniqueInputs'])) { |
|
| 45 | + throw new OutOfBoundsException( |
|
| 46 | + esc_html__('A valid input was not provided.', 'event_espresso') |
|
| 47 | + ); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - $sharedInput = ! empty($input['sharedInput']) ? $input['sharedInput'] : []; |
|
| 50 | + $sharedInput = ! empty($input['sharedInput']) ? $input['sharedInput'] : []; |
|
| 51 | 51 | |
| 52 | - foreach ($input['uniqueInputs'] as $uniqueInput) { |
|
| 53 | - try { |
|
| 54 | - // values in $uniqueInput will override those in $sharedInput |
|
| 55 | - $finalInput = array_merge($sharedInput, $uniqueInput); |
|
| 56 | - // mutate the individual entity. |
|
| 57 | - $mutator = $this->entity_mutator; |
|
| 58 | - $mutator($finalInput, $context, $info); |
|
| 59 | - // we are here it means the update was successful. |
|
| 60 | - $updated[] = $uniqueInput['id']; |
|
| 61 | - } catch (Exception $e) { |
|
| 62 | - // sorry mate, couldn't help you :( |
|
| 63 | - $failed[] = $uniqueInput['id']; |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - } catch (Exception $exception) { |
|
| 67 | - EntityMutator::handleExceptions( |
|
| 68 | - $exception, |
|
| 69 | - esc_html__( |
|
| 70 | - 'Could not perform the update because of the following error(s)', |
|
| 71 | - 'event_espresso' |
|
| 72 | - ) |
|
| 73 | - ); |
|
| 74 | - } |
|
| 52 | + foreach ($input['uniqueInputs'] as $uniqueInput) { |
|
| 53 | + try { |
|
| 54 | + // values in $uniqueInput will override those in $sharedInput |
|
| 55 | + $finalInput = array_merge($sharedInput, $uniqueInput); |
|
| 56 | + // mutate the individual entity. |
|
| 57 | + $mutator = $this->entity_mutator; |
|
| 58 | + $mutator($finalInput, $context, $info); |
|
| 59 | + // we are here it means the update was successful. |
|
| 60 | + $updated[] = $uniqueInput['id']; |
|
| 61 | + } catch (Exception $e) { |
|
| 62 | + // sorry mate, couldn't help you :( |
|
| 63 | + $failed[] = $uniqueInput['id']; |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + } catch (Exception $exception) { |
|
| 67 | + EntityMutator::handleExceptions( |
|
| 68 | + $exception, |
|
| 69 | + esc_html__( |
|
| 70 | + 'Could not perform the update because of the following error(s)', |
|
| 71 | + 'event_espresso' |
|
| 72 | + ) |
|
| 73 | + ); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - return compact('updated', 'failed'); |
|
| 77 | - } |
|
| 76 | + return compact('updated', 'failed'); |
|
| 77 | + } |
|
| 78 | 78 | } |
@@ -8,17 +8,17 @@ |
||
| 8 | 8 | class TicketBulkUpdate extends EntityMutator |
| 9 | 9 | { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * Defines the mutation data modification closure. |
|
| 13 | - * |
|
| 14 | - * @param EEM_Ticket $model |
|
| 15 | - * @param Ticket $type |
|
| 16 | - * @return callable |
|
| 17 | - */ |
|
| 18 | - public static function mutateAndGetPayload(EEM_Ticket $model, Ticket $type) |
|
| 19 | - { |
|
| 20 | - $entityMutator = TicketUpdate::mutateAndGetPayload($model, $type); |
|
| 21 | - $bulkMutator = new BulkEntityMutator($entityMutator); |
|
| 22 | - return array($bulkMutator, 'updateEntities'); |
|
| 23 | - } |
|
| 11 | + /** |
|
| 12 | + * Defines the mutation data modification closure. |
|
| 13 | + * |
|
| 14 | + * @param EEM_Ticket $model |
|
| 15 | + * @param Ticket $type |
|
| 16 | + * @return callable |
|
| 17 | + */ |
|
| 18 | + public static function mutateAndGetPayload(EEM_Ticket $model, Ticket $type) |
|
| 19 | + { |
|
| 20 | + $entityMutator = TicketUpdate::mutateAndGetPayload($model, $type); |
|
| 21 | + $bulkMutator = new BulkEntityMutator($entityMutator); |
|
| 22 | + return array($bulkMutator, 'updateEntities'); |
|
| 23 | + } |
|
| 24 | 24 | } |
@@ -7,19 +7,19 @@ |
||
| 7 | 7 | abstract class Factory implements FactoryInterface |
| 8 | 8 | { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * @var LoaderInterface $loader |
|
| 12 | - */ |
|
| 13 | - protected $loader; |
|
| 10 | + /** |
|
| 11 | + * @var LoaderInterface $loader |
|
| 12 | + */ |
|
| 13 | + protected $loader; |
|
| 14 | 14 | |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * RouteFactory constructor. |
|
| 18 | - * |
|
| 19 | - * @param LoaderInterface $loader |
|
| 20 | - */ |
|
| 21 | - public function __construct(LoaderInterface $loader) |
|
| 22 | - { |
|
| 23 | - $this->loader = $loader; |
|
| 24 | - } |
|
| 16 | + /** |
|
| 17 | + * RouteFactory constructor. |
|
| 18 | + * |
|
| 19 | + * @param LoaderInterface $loader |
|
| 20 | + */ |
|
| 21 | + public function __construct(LoaderInterface $loader) |
|
| 22 | + { |
|
| 23 | + $this->loader = $loader; |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | */ |
| 54 | 54 | public function create($fqcn, array $arguments = []) |
| 55 | 55 | { |
| 56 | - if (! isset($arguments[0]) || ! $arguments[0] instanceof DomainInterface) { |
|
| 56 | + if ( ! isset($arguments[0]) || ! $arguments[0] instanceof DomainInterface) { |
|
| 57 | 57 | throw new InvalidArgumentException( |
| 58 | 58 | esc_html__( |
| 59 | 59 | 'In order to generate an AssetManifest class you need to supply an array where the first argument is an instance of DomainInterface.', |
@@ -73,9 +73,9 @@ discard block |
||
| 73 | 73 | private function getAssetManifestForDomain($manifest_fqcn, DomainInterface $domain) |
| 74 | 74 | { |
| 75 | 75 | $domain_fqcn = get_class($domain); |
| 76 | - if (! isset(AssetManifestFactory::$manifests[ $domain_fqcn ])) { |
|
| 76 | + if ( ! isset(AssetManifestFactory::$manifests[$domain_fqcn])) { |
|
| 77 | 77 | $asset_manifest = new $manifest_fqcn($domain); |
| 78 | - if (! $asset_manifest instanceof AssetManifestInterface || ! $asset_manifest instanceof $manifest_fqcn) { |
|
| 78 | + if ( ! $asset_manifest instanceof AssetManifestInterface || ! $asset_manifest instanceof $manifest_fqcn) { |
|
| 79 | 79 | throw new DomainException( |
| 80 | 80 | sprintf( |
| 81 | 81 | esc_html__( |
@@ -88,8 +88,8 @@ discard block |
||
| 88 | 88 | } |
| 89 | 89 | // we still need to share this with the core loader to facilitate automatic dependency injection |
| 90 | 90 | $this->loader->share(AssetManifest::class, $asset_manifest, [$domain]); |
| 91 | - AssetManifestFactory::$manifests[ $domain_fqcn ] = $asset_manifest; |
|
| 91 | + AssetManifestFactory::$manifests[$domain_fqcn] = $asset_manifest; |
|
| 92 | 92 | } |
| 93 | - return AssetManifestFactory::$manifests[ $domain_fqcn ]; |
|
| 93 | + return AssetManifestFactory::$manifests[$domain_fqcn]; |
|
| 94 | 94 | } |
| 95 | 95 | } |
@@ -16,86 +16,86 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class AssetManifestFactory implements FactoryInterface |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * @var AssetManifestInterface[] |
|
| 21 | - */ |
|
| 22 | - private static $manifests = []; |
|
| 19 | + /** |
|
| 20 | + * @var AssetManifestInterface[] |
|
| 21 | + */ |
|
| 22 | + private static $manifests = []; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @var LoaderInterface $loader |
|
| 26 | - */ |
|
| 27 | - protected $loader; |
|
| 24 | + /** |
|
| 25 | + * @var LoaderInterface $loader |
|
| 26 | + */ |
|
| 27 | + protected $loader; |
|
| 28 | 28 | |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * AssetManifestFactory constructor. |
|
| 32 | - * |
|
| 33 | - * @param LoaderInterface $loader |
|
| 34 | - */ |
|
| 35 | - public function __construct(LoaderInterface $loader) |
|
| 36 | - { |
|
| 37 | - $this->loader = $loader; |
|
| 38 | - } |
|
| 30 | + /** |
|
| 31 | + * AssetManifestFactory constructor. |
|
| 32 | + * |
|
| 33 | + * @param LoaderInterface $loader |
|
| 34 | + */ |
|
| 35 | + public function __construct(LoaderInterface $loader) |
|
| 36 | + { |
|
| 37 | + $this->loader = $loader; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * returns the applicable AssetManifest for the provided Domain |
|
| 43 | - * |
|
| 44 | - * @param DomainInterface $domain |
|
| 45 | - * @return AssetManifestInterface |
|
| 46 | - */ |
|
| 47 | - public function createFromDomainObject(DomainInterface $domain) |
|
| 48 | - { |
|
| 49 | - return $this->getAssetManifestForDomain(AssetManifest::class, $domain); |
|
| 50 | - } |
|
| 41 | + /** |
|
| 42 | + * returns the applicable AssetManifest for the provided Domain |
|
| 43 | + * |
|
| 44 | + * @param DomainInterface $domain |
|
| 45 | + * @return AssetManifestInterface |
|
| 46 | + */ |
|
| 47 | + public function createFromDomainObject(DomainInterface $domain) |
|
| 48 | + { |
|
| 49 | + return $this->getAssetManifestForDomain(AssetManifest::class, $domain); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * for creating an atypical AssetManifest for the Domain provided in the $arguments array |
|
| 55 | - * |
|
| 56 | - * @param string $fqcn Fully Qualified Class Name |
|
| 57 | - * @param array $arguments [optional] array of data required for construction |
|
| 58 | - * @return AssetManifestInterface |
|
| 59 | - */ |
|
| 60 | - public function create($fqcn, array $arguments = []) |
|
| 61 | - { |
|
| 62 | - if (! isset($arguments[0]) || ! $arguments[0] instanceof DomainInterface) { |
|
| 63 | - throw new InvalidArgumentException( |
|
| 64 | - esc_html__( |
|
| 65 | - 'In order to generate an AssetManifest class you need to supply an array where the first argument is an instance of DomainInterface.', |
|
| 66 | - 'event_espresso' |
|
| 67 | - ) |
|
| 68 | - ); |
|
| 69 | - } |
|
| 70 | - return $this->getAssetManifestForDomain($fqcn, $arguments[0]); |
|
| 71 | - } |
|
| 53 | + /** |
|
| 54 | + * for creating an atypical AssetManifest for the Domain provided in the $arguments array |
|
| 55 | + * |
|
| 56 | + * @param string $fqcn Fully Qualified Class Name |
|
| 57 | + * @param array $arguments [optional] array of data required for construction |
|
| 58 | + * @return AssetManifestInterface |
|
| 59 | + */ |
|
| 60 | + public function create($fqcn, array $arguments = []) |
|
| 61 | + { |
|
| 62 | + if (! isset($arguments[0]) || ! $arguments[0] instanceof DomainInterface) { |
|
| 63 | + throw new InvalidArgumentException( |
|
| 64 | + esc_html__( |
|
| 65 | + 'In order to generate an AssetManifest class you need to supply an array where the first argument is an instance of DomainInterface.', |
|
| 66 | + 'event_espresso' |
|
| 67 | + ) |
|
| 68 | + ); |
|
| 69 | + } |
|
| 70 | + return $this->getAssetManifestForDomain($fqcn, $arguments[0]); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * @param string $manifest_fqcn |
|
| 76 | - * @param DomainInterface $domain |
|
| 77 | - * @return AssetManifestInterface |
|
| 78 | - */ |
|
| 79 | - private function getAssetManifestForDomain($manifest_fqcn, DomainInterface $domain) |
|
| 80 | - { |
|
| 81 | - $domain_fqcn = get_class($domain); |
|
| 82 | - if (! isset(AssetManifestFactory::$manifests[ $domain_fqcn ])) { |
|
| 83 | - $asset_manifest = new $manifest_fqcn($domain); |
|
| 84 | - if (! $asset_manifest instanceof AssetManifestInterface || ! $asset_manifest instanceof $manifest_fqcn) { |
|
| 85 | - throw new DomainException( |
|
| 86 | - sprintf( |
|
| 87 | - esc_html__( |
|
| 88 | - 'The requested AssetManifest class "%1$s" could not be loaded.', |
|
| 89 | - 'event_espresso' |
|
| 90 | - ), |
|
| 91 | - $manifest_fqcn |
|
| 92 | - ) |
|
| 93 | - ); |
|
| 94 | - } |
|
| 95 | - // we still need to share this with the core loader to facilitate automatic dependency injection |
|
| 96 | - $this->loader->share(AssetManifest::class, $asset_manifest, [$domain]); |
|
| 97 | - AssetManifestFactory::$manifests[ $domain_fqcn ] = $asset_manifest; |
|
| 98 | - } |
|
| 99 | - return AssetManifestFactory::$manifests[ $domain_fqcn ]; |
|
| 100 | - } |
|
| 74 | + /** |
|
| 75 | + * @param string $manifest_fqcn |
|
| 76 | + * @param DomainInterface $domain |
|
| 77 | + * @return AssetManifestInterface |
|
| 78 | + */ |
|
| 79 | + private function getAssetManifestForDomain($manifest_fqcn, DomainInterface $domain) |
|
| 80 | + { |
|
| 81 | + $domain_fqcn = get_class($domain); |
|
| 82 | + if (! isset(AssetManifestFactory::$manifests[ $domain_fqcn ])) { |
|
| 83 | + $asset_manifest = new $manifest_fqcn($domain); |
|
| 84 | + if (! $asset_manifest instanceof AssetManifestInterface || ! $asset_manifest instanceof $manifest_fqcn) { |
|
| 85 | + throw new DomainException( |
|
| 86 | + sprintf( |
|
| 87 | + esc_html__( |
|
| 88 | + 'The requested AssetManifest class "%1$s" could not be loaded.', |
|
| 89 | + 'event_espresso' |
|
| 90 | + ), |
|
| 91 | + $manifest_fqcn |
|
| 92 | + ) |
|
| 93 | + ); |
|
| 94 | + } |
|
| 95 | + // we still need to share this with the core loader to facilitate automatic dependency injection |
|
| 96 | + $this->loader->share(AssetManifest::class, $asset_manifest, [$domain]); |
|
| 97 | + AssetManifestFactory::$manifests[ $domain_fqcn ] = $asset_manifest; |
|
| 98 | + } |
|
| 99 | + return AssetManifestFactory::$manifests[ $domain_fqcn ]; |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -13,94 +13,94 @@ |
||
| 13 | 13 | class Barista implements BaristaInterface |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - const DEPENDENCY_LIST_REGISTERED = 'registered'; |
|
| 16 | + const DEPENDENCY_LIST_REGISTERED = 'registered'; |
|
| 17 | 17 | |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var AssetManifestInterface |
|
| 21 | - */ |
|
| 22 | - private $asset_manifest; |
|
| 19 | + /** |
|
| 20 | + * @var AssetManifestInterface |
|
| 21 | + */ |
|
| 22 | + private $asset_manifest; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @var bool |
|
| 26 | - */ |
|
| 27 | - protected $initialized = false; |
|
| 24 | + /** |
|
| 25 | + * @var bool |
|
| 26 | + */ |
|
| 27 | + protected $initialized = false; |
|
| 28 | 28 | |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Barista constructor. |
|
| 32 | - * |
|
| 33 | - * @param AssetManifestInterface $asset_manifest |
|
| 34 | - */ |
|
| 35 | - public function __construct(AssetManifestInterface $asset_manifest) |
|
| 36 | - { |
|
| 37 | - $this->asset_manifest = $asset_manifest; |
|
| 38 | - } |
|
| 30 | + /** |
|
| 31 | + * Barista constructor. |
|
| 32 | + * |
|
| 33 | + * @param AssetManifestInterface $asset_manifest |
|
| 34 | + */ |
|
| 35 | + public function __construct(AssetManifestInterface $asset_manifest) |
|
| 36 | + { |
|
| 37 | + $this->asset_manifest = $asset_manifest; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @return void |
|
| 43 | - */ |
|
| 44 | - public function initialize() |
|
| 45 | - { |
|
| 46 | - if (! $this->initialized) { |
|
| 47 | - $this->asset_manifest->initialize(); |
|
| 48 | - add_action('wp_enqueue_scripts', [$this, 'registerScripts'], 0); |
|
| 49 | - add_action('admin_enqueue_scripts', [$this, 'registerScripts'], 0); |
|
| 50 | - add_action('wp_enqueue_scripts', [$this, 'registerStyles'], 0); |
|
| 51 | - add_action('admin_enqueue_scripts', [$this, 'registerStyles'], 0); |
|
| 52 | - $this->initialized = true; |
|
| 53 | - } |
|
| 54 | - } |
|
| 41 | + /** |
|
| 42 | + * @return void |
|
| 43 | + */ |
|
| 44 | + public function initialize() |
|
| 45 | + { |
|
| 46 | + if (! $this->initialized) { |
|
| 47 | + $this->asset_manifest->initialize(); |
|
| 48 | + add_action('wp_enqueue_scripts', [$this, 'registerScripts'], 0); |
|
| 49 | + add_action('admin_enqueue_scripts', [$this, 'registerScripts'], 0); |
|
| 50 | + add_action('wp_enqueue_scripts', [$this, 'registerStyles'], 0); |
|
| 51 | + add_action('admin_enqueue_scripts', [$this, 'registerStyles'], 0); |
|
| 52 | + $this->initialized = true; |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Registers all the WordPress packages scripts that are in the standardized |
|
| 59 | - * `build/` location. |
|
| 60 | - * |
|
| 61 | - * @return void |
|
| 62 | - * @throws Exception |
|
| 63 | - */ |
|
| 64 | - public function registerScripts() |
|
| 65 | - { |
|
| 66 | - try { |
|
| 67 | - $entry_points = $this->asset_manifest->getEntryPoints(); |
|
| 68 | - foreach ($entry_points as $entry_point) { |
|
| 69 | - if ($this->asset_manifest->hasAsset($entry_point)) { |
|
| 70 | - $handle = $this->asset_manifest->getAssetHandle($entry_point); |
|
| 71 | - $source = $this->asset_manifest->getAssetUrl($entry_point); |
|
| 72 | - $dependencies = $this->asset_manifest->getAssetDependencies($entry_point); |
|
| 73 | - $version = $this->asset_manifest->getAssetVersion($entry_point); |
|
| 74 | - wp_register_script($handle, $source, $dependencies, $version, true); |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - } catch (Exception $exception) { |
|
| 78 | - wp_die(new ExceptionStackTraceDisplay($exception)); |
|
| 79 | - } |
|
| 80 | - } |
|
| 57 | + /** |
|
| 58 | + * Registers all the WordPress packages scripts that are in the standardized |
|
| 59 | + * `build/` location. |
|
| 60 | + * |
|
| 61 | + * @return void |
|
| 62 | + * @throws Exception |
|
| 63 | + */ |
|
| 64 | + public function registerScripts() |
|
| 65 | + { |
|
| 66 | + try { |
|
| 67 | + $entry_points = $this->asset_manifest->getEntryPoints(); |
|
| 68 | + foreach ($entry_points as $entry_point) { |
|
| 69 | + if ($this->asset_manifest->hasAsset($entry_point)) { |
|
| 70 | + $handle = $this->asset_manifest->getAssetHandle($entry_point); |
|
| 71 | + $source = $this->asset_manifest->getAssetUrl($entry_point); |
|
| 72 | + $dependencies = $this->asset_manifest->getAssetDependencies($entry_point); |
|
| 73 | + $version = $this->asset_manifest->getAssetVersion($entry_point); |
|
| 74 | + wp_register_script($handle, $source, $dependencies, $version, true); |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + } catch (Exception $exception) { |
|
| 78 | + wp_die(new ExceptionStackTraceDisplay($exception)); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Registers all the packages and domain styles that are in the build folder. |
|
| 85 | - * |
|
| 86 | - * @return void |
|
| 87 | - * @throws Exception |
|
| 88 | - */ |
|
| 89 | - public function registerStyles() |
|
| 90 | - { |
|
| 91 | - try { |
|
| 92 | - $entry_points = $this->asset_manifest->getEntryPoints(); |
|
| 93 | - foreach ($entry_points as $entry_point) { |
|
| 94 | - if ($this->asset_manifest->hasAsset($entry_point, AssetManifest::ASSET_EXT_CSS)) { |
|
| 95 | - $handle = $this->asset_manifest->getAssetHandle($entry_point); |
|
| 96 | - $source = $this->asset_manifest->getAssetUrl($entry_point, AssetManifest::ASSET_EXT_CSS); |
|
| 97 | - $dependencies = $this->asset_manifest->getAssetDependencies($entry_point, AssetManifest::ASSET_EXT_CSS); |
|
| 98 | - $version = $this->asset_manifest->getAssetVersion($entry_point, AssetManifest::ASSET_EXT_CSS); |
|
| 99 | - wp_register_style($handle, $source, $dependencies, $version, 'all'); |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - } catch (Exception $exception) { |
|
| 103 | - wp_die(new ExceptionStackTraceDisplay($exception)); |
|
| 104 | - } |
|
| 105 | - } |
|
| 83 | + /** |
|
| 84 | + * Registers all the packages and domain styles that are in the build folder. |
|
| 85 | + * |
|
| 86 | + * @return void |
|
| 87 | + * @throws Exception |
|
| 88 | + */ |
|
| 89 | + public function registerStyles() |
|
| 90 | + { |
|
| 91 | + try { |
|
| 92 | + $entry_points = $this->asset_manifest->getEntryPoints(); |
|
| 93 | + foreach ($entry_points as $entry_point) { |
|
| 94 | + if ($this->asset_manifest->hasAsset($entry_point, AssetManifest::ASSET_EXT_CSS)) { |
|
| 95 | + $handle = $this->asset_manifest->getAssetHandle($entry_point); |
|
| 96 | + $source = $this->asset_manifest->getAssetUrl($entry_point, AssetManifest::ASSET_EXT_CSS); |
|
| 97 | + $dependencies = $this->asset_manifest->getAssetDependencies($entry_point, AssetManifest::ASSET_EXT_CSS); |
|
| 98 | + $version = $this->asset_manifest->getAssetVersion($entry_point, AssetManifest::ASSET_EXT_CSS); |
|
| 99 | + wp_register_style($handle, $source, $dependencies, $version, 'all'); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + } catch (Exception $exception) { |
|
| 103 | + wp_die(new ExceptionStackTraceDisplay($exception)); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | 106 | } |
@@ -43,7 +43,7 @@ |
||
| 43 | 43 | */ |
| 44 | 44 | public function initialize() |
| 45 | 45 | { |
| 46 | - if (! $this->initialized) { |
|
| 46 | + if ( ! $this->initialized) { |
|
| 47 | 47 | $this->asset_manifest->initialize(); |
| 48 | 48 | add_action('wp_enqueue_scripts', [$this, 'registerScripts'], 0); |
| 49 | 49 | add_action('admin_enqueue_scripts', [$this, 'registerScripts'], 0); |
@@ -13,42 +13,42 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class AssetManifestException extends DomainException |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * AssetManifestException constructor |
|
| 18 | - * |
|
| 19 | - * @param string $entry |
|
| 20 | - * @param string $path |
|
| 21 | - * @param string $message |
|
| 22 | - * @param int $code |
|
| 23 | - * @param Exception $previous |
|
| 24 | - */ |
|
| 25 | - public function __construct($entry, $path, $message = '', $code = 0, Exception $previous = null) |
|
| 26 | - { |
|
| 27 | - if (empty($message)) { |
|
| 28 | - switch($entry) { |
|
| 29 | - case AssetManifest::KEY_DEPENDENCIES: |
|
| 30 | - $key = 'file'; |
|
| 31 | - break; |
|
| 32 | - case AssetManifest::KEY_VERSION: |
|
| 33 | - $key = 'version'; |
|
| 34 | - break; |
|
| 35 | - case AssetManifest::KEY_ENTRY_POINTS: |
|
| 36 | - case AssetManifest::KEY_FILES: |
|
| 37 | - default: |
|
| 38 | - $key = 'key'; |
|
| 39 | - } |
|
| 40 | - $message = |
|
| 41 | - sprintf( |
|
| 42 | - esc_html__( |
|
| 43 | - 'The "%1$s" %2$s was not found in the assets manifest file at: %3$s', |
|
| 44 | - 'event_espresso' |
|
| 45 | - ), |
|
| 46 | - $entry, |
|
| 47 | - $key, |
|
| 48 | - $path |
|
| 49 | - ); |
|
| 50 | - } |
|
| 51 | - parent::__construct($message, $code, $previous); |
|
| 52 | - } |
|
| 16 | + /** |
|
| 17 | + * AssetManifestException constructor |
|
| 18 | + * |
|
| 19 | + * @param string $entry |
|
| 20 | + * @param string $path |
|
| 21 | + * @param string $message |
|
| 22 | + * @param int $code |
|
| 23 | + * @param Exception $previous |
|
| 24 | + */ |
|
| 25 | + public function __construct($entry, $path, $message = '', $code = 0, Exception $previous = null) |
|
| 26 | + { |
|
| 27 | + if (empty($message)) { |
|
| 28 | + switch($entry) { |
|
| 29 | + case AssetManifest::KEY_DEPENDENCIES: |
|
| 30 | + $key = 'file'; |
|
| 31 | + break; |
|
| 32 | + case AssetManifest::KEY_VERSION: |
|
| 33 | + $key = 'version'; |
|
| 34 | + break; |
|
| 35 | + case AssetManifest::KEY_ENTRY_POINTS: |
|
| 36 | + case AssetManifest::KEY_FILES: |
|
| 37 | + default: |
|
| 38 | + $key = 'key'; |
|
| 39 | + } |
|
| 40 | + $message = |
|
| 41 | + sprintf( |
|
| 42 | + esc_html__( |
|
| 43 | + 'The "%1$s" %2$s was not found in the assets manifest file at: %3$s', |
|
| 44 | + 'event_espresso' |
|
| 45 | + ), |
|
| 46 | + $entry, |
|
| 47 | + $key, |
|
| 48 | + $path |
|
| 49 | + ); |
|
| 50 | + } |
|
| 51 | + parent::__construct($message, $code, $previous); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | 54 | } |
@@ -25,7 +25,7 @@ |
||
| 25 | 25 | public function __construct($entry, $path, $message = '', $code = 0, Exception $previous = null) |
| 26 | 26 | { |
| 27 | 27 | if (empty($message)) { |
| 28 | - switch($entry) { |
|
| 28 | + switch ($entry) { |
|
| 29 | 29 | case AssetManifest::KEY_DEPENDENCIES: |
| 30 | 30 | $key = 'file'; |
| 31 | 31 | break; |
@@ -14,328 +14,328 @@ |
||
| 14 | 14 | class AssetManifest implements AssetManifestInterface |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - const ASSET_EXT_CSS = '.css'; |
|
| 18 | - |
|
| 19 | - const ASSET_EXT_JS = '.js'; |
|
| 20 | - |
|
| 21 | - const ASSET_EXT_PHP = '.php'; |
|
| 22 | - |
|
| 23 | - const FILE_NAME = 'asset-manifest.json'; |
|
| 24 | - |
|
| 25 | - const KEY_DEPENDENCIES = 'dependencies'; |
|
| 26 | - |
|
| 27 | - const KEY_ENTRY_POINTS = 'entrypoints'; |
|
| 28 | - |
|
| 29 | - const KEY_FILES = 'files'; |
|
| 30 | - |
|
| 31 | - const KEY_VERSION = 'version'; |
|
| 32 | - |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var array |
|
| 36 | - */ |
|
| 37 | - private $asset_files; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @var string |
|
| 41 | - */ |
|
| 42 | - private $assets_namespace; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - private $assets_path; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @var DomainInterface |
|
| 51 | - */ |
|
| 52 | - protected $domain; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @var array |
|
| 56 | - */ |
|
| 57 | - private $entry_points; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @var array |
|
| 61 | - */ |
|
| 62 | - private $manifest; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @var string |
|
| 66 | - */ |
|
| 67 | - private $manifest_path; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * This is a list of known handles that are used for css. |
|
| 71 | - * @var array |
|
| 72 | - */ |
|
| 73 | - private $wp_css_handle_dependencies = [ |
|
| 74 | - 'wp-components', |
|
| 75 | - 'wp-block-editor', |
|
| 76 | - 'wp-block-library', |
|
| 77 | - 'wp-edit-post', |
|
| 78 | - 'wp-edit-widgets', |
|
| 79 | - 'wp-editor', |
|
| 80 | - 'wp-format-library', |
|
| 81 | - 'wp-list-reusable-blocks', |
|
| 82 | - 'wp-nux', |
|
| 83 | - ]; |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * AssetManifest constructor. |
|
| 88 | - * |
|
| 89 | - * @param DomainInterface $domain |
|
| 90 | - */ |
|
| 91 | - public function __construct(DomainInterface $domain) |
|
| 92 | - { |
|
| 93 | - $this->domain = $domain; |
|
| 94 | - $this->initialize(); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @return void |
|
| 100 | - */ |
|
| 101 | - public function initialize() |
|
| 102 | - { |
|
| 103 | - if (! $this->manifest) { |
|
| 104 | - $this->assets_namespace = $this->domain->assetNamespace(); |
|
| 105 | - $assets_path = $this->domain->distributionAssetsPath(); |
|
| 106 | - $this->assets_path = trailingslashit($assets_path); |
|
| 107 | - $this->setManifestFilepath(); |
|
| 108 | - $this->loadManifest(); |
|
| 109 | - $this->getAssetFiles(); |
|
| 110 | - $this->getEntryPoints(); |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @param string $manifest_path |
|
| 117 | - */ |
|
| 118 | - public function setManifestFilepath($manifest_path = '') |
|
| 119 | - { |
|
| 120 | - $manifest_path = $manifest_path ? $manifest_path : $this->assets_path . AssetManifest::FILE_NAME; |
|
| 121 | - if (! is_readable($manifest_path)) { |
|
| 122 | - throw new AssetManifestException( |
|
| 123 | - $manifest_path, |
|
| 124 | - '', |
|
| 125 | - sprintf( |
|
| 126 | - esc_html__( |
|
| 127 | - 'The "%1$s" file was not found or is not readable. Please verify that the file exists and has appropriate permissions.', |
|
| 128 | - 'event_espresso' |
|
| 129 | - ), |
|
| 130 | - $manifest_path |
|
| 131 | - ) |
|
| 132 | - ); |
|
| 133 | - } |
|
| 134 | - $this->manifest_path = $manifest_path; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @return void |
|
| 140 | - */ |
|
| 141 | - private function loadManifest() |
|
| 142 | - { |
|
| 143 | - if (! $this->manifest) { |
|
| 144 | - // TODO May be use WP File system? ¯\_(ツ)_/¯ |
|
| 145 | - $manifest_json = file_get_contents($this->manifest_path); |
|
| 146 | - $this->manifest = json_decode($manifest_json, true); |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @param string $file_name |
|
| 153 | - * @return string |
|
| 154 | - */ |
|
| 155 | - private function trimAssetFilename($file_name) |
|
| 156 | - { |
|
| 157 | - return ltrim($file_name, '/'); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @return array |
|
| 163 | - */ |
|
| 164 | - public function getAssetFiles() |
|
| 165 | - { |
|
| 166 | - if (! $this->asset_files) { |
|
| 167 | - if (empty($this->manifest[ AssetManifest::KEY_FILES ])) { |
|
| 168 | - if (WP_DEBUG) { |
|
| 169 | - throw new AssetManifestException(AssetManifest::KEY_FILES, $this->manifest_path); |
|
| 170 | - } |
|
| 171 | - return []; |
|
| 172 | - } |
|
| 173 | - $this->asset_files = $this->manifest[ AssetManifest::KEY_FILES ]; |
|
| 174 | - } |
|
| 175 | - return $this->asset_files; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @return array |
|
| 181 | - */ |
|
| 182 | - public function getEntryPoints() |
|
| 183 | - { |
|
| 184 | - if (! $this->entry_points) { |
|
| 185 | - if (empty($this->manifest[ AssetManifest::KEY_ENTRY_POINTS ])) { |
|
| 186 | - if (WP_DEBUG) { |
|
| 187 | - throw new AssetManifestException(AssetManifest::KEY_ENTRY_POINTS, $this->manifest_path); |
|
| 188 | - } |
|
| 189 | - return []; |
|
| 190 | - } |
|
| 191 | - $this->entry_points = array_keys($this->manifest[ AssetManifest::KEY_ENTRY_POINTS ]); |
|
| 192 | - } |
|
| 193 | - return $this->entry_points; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @param string $entry_point |
|
| 199 | - * @param string $type |
|
| 200 | - * @return string |
|
| 201 | - */ |
|
| 202 | - private function getAsset($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
|
| 203 | - { |
|
| 204 | - return $this->hasAsset($entry_point, $type) |
|
| 205 | - ? $this->trimAssetFilename($this->asset_files[ $entry_point . $type ]) |
|
| 206 | - : ''; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * @param string $entry_point |
|
| 212 | - * @param string $type |
|
| 213 | - * @return array |
|
| 214 | - */ |
|
| 215 | - public function getAssetDependencies($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
|
| 216 | - { |
|
| 217 | - $asset = $this->getAssetDetails($entry_point); |
|
| 218 | - if (! isset($asset[AssetManifest::KEY_DEPENDENCIES])) { |
|
| 219 | - return []; |
|
| 220 | - } |
|
| 221 | - $dependencies = $asset[AssetManifest::KEY_DEPENDENCIES]; |
|
| 222 | - // remove cyclical dependencies, if any |
|
| 223 | - if (($key = array_search($this->assets_namespace . '-' . $entry_point, $dependencies, true)) !== false) { |
|
| 224 | - unset($dependencies[ $key ]); |
|
| 225 | - } |
|
| 226 | - // currently need to derive dependencies for CSS from the JS dependencies |
|
| 227 | - if ($type === AssetManifest::ASSET_EXT_CSS) { |
|
| 228 | - $css_dependencies = []; |
|
| 229 | - foreach ($dependencies as $handle) { |
|
| 230 | - $dependency_style = $this->getEntryPointFromHandle($handle) . AssetManifest::ASSET_EXT_CSS; |
|
| 231 | - if (isset($this->asset_files[ $dependency_style ]) |
|
| 232 | - || in_array($handle, $this->wp_css_handle_dependencies) |
|
| 233 | - ) { |
|
| 234 | - $css_dependencies[] = $handle; |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - return $css_dependencies; |
|
| 238 | - } |
|
| 239 | - return $dependencies; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * @param string $entry_point |
|
| 245 | - * @return array |
|
| 246 | - */ |
|
| 247 | - public function getAssetDetails($entry_point) |
|
| 248 | - { |
|
| 249 | - $file_name = $this->getAsset($entry_point, AssetManifest::ASSET_EXT_PHP); |
|
| 250 | - if (! $file_name) { |
|
| 251 | - return []; |
|
| 252 | - } |
|
| 253 | - $full_path = $this->assets_path . $file_name; |
|
| 254 | - if (! is_readable($full_path)) { |
|
| 255 | - if (WP_DEBUG) { |
|
| 256 | - throw new AssetManifestException(AssetManifest::KEY_DEPENDENCIES, $full_path); |
|
| 257 | - } |
|
| 258 | - return []; |
|
| 259 | - } |
|
| 260 | - return require($full_path); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * @param string $entry_point |
|
| 266 | - * @return string|int|false |
|
| 267 | - */ |
|
| 268 | - public function getAssetHandle($entry_point) |
|
| 269 | - { |
|
| 270 | - return $this->assets_namespace . '-' . $entry_point; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * @return string |
|
| 276 | - */ |
|
| 277 | - public function getAssetsPath() |
|
| 278 | - { |
|
| 279 | - return $this->assets_path; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * @param string $handle |
|
| 285 | - * @return string|int|false |
|
| 286 | - */ |
|
| 287 | - private function getEntryPointFromHandle($handle) |
|
| 288 | - { $find = $this->assets_namespace . '-'; |
|
| 289 | - return str_replace($find, '', $handle); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * @param string $entry_point |
|
| 295 | - * @param string $type |
|
| 296 | - * @return string |
|
| 297 | - */ |
|
| 298 | - public function getAssetPath($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
|
| 299 | - { |
|
| 300 | - $file_name = $this->getAsset($entry_point, $type); |
|
| 301 | - return $file_name ? $this->domain->distributionAssetsPath($file_name) : ''; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * @param string $entry_point |
|
| 307 | - * @param string $type |
|
| 308 | - * @return string |
|
| 309 | - */ |
|
| 310 | - public function getAssetUrl($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
|
| 311 | - { |
|
| 312 | - $file_name = $this->getAsset($entry_point, $type); |
|
| 313 | - return $file_name ? $this->domain->distributionAssetsUrl($file_name) : ''; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * @param string $entry_point |
|
| 319 | - * @param string $type |
|
| 320 | - * @return string|int|false |
|
| 321 | - */ |
|
| 322 | - public function getAssetVersion($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
|
| 323 | - { |
|
| 324 | - $asset = $this->getAssetDetails($entry_point); |
|
| 325 | - return $type === AssetManifest::ASSET_EXT_JS && isset($asset[AssetManifest::KEY_VERSION]) |
|
| 326 | - ? $asset[AssetManifest::KEY_VERSION] |
|
| 327 | - : filemtime($this->getAssetPath($entry_point, $type)); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * @param string $entry_point |
|
| 333 | - * @param string $type |
|
| 334 | - * @return string |
|
| 335 | - */ |
|
| 336 | - public function hasAsset($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
|
| 337 | - { |
|
| 338 | - $file_name = $entry_point . $type; |
|
| 339 | - return ! empty($this->asset_files[ $file_name ]); |
|
| 340 | - } |
|
| 17 | + const ASSET_EXT_CSS = '.css'; |
|
| 18 | + |
|
| 19 | + const ASSET_EXT_JS = '.js'; |
|
| 20 | + |
|
| 21 | + const ASSET_EXT_PHP = '.php'; |
|
| 22 | + |
|
| 23 | + const FILE_NAME = 'asset-manifest.json'; |
|
| 24 | + |
|
| 25 | + const KEY_DEPENDENCIES = 'dependencies'; |
|
| 26 | + |
|
| 27 | + const KEY_ENTRY_POINTS = 'entrypoints'; |
|
| 28 | + |
|
| 29 | + const KEY_FILES = 'files'; |
|
| 30 | + |
|
| 31 | + const KEY_VERSION = 'version'; |
|
| 32 | + |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var array |
|
| 36 | + */ |
|
| 37 | + private $asset_files; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @var string |
|
| 41 | + */ |
|
| 42 | + private $assets_namespace; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + private $assets_path; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @var DomainInterface |
|
| 51 | + */ |
|
| 52 | + protected $domain; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @var array |
|
| 56 | + */ |
|
| 57 | + private $entry_points; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @var array |
|
| 61 | + */ |
|
| 62 | + private $manifest; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @var string |
|
| 66 | + */ |
|
| 67 | + private $manifest_path; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * This is a list of known handles that are used for css. |
|
| 71 | + * @var array |
|
| 72 | + */ |
|
| 73 | + private $wp_css_handle_dependencies = [ |
|
| 74 | + 'wp-components', |
|
| 75 | + 'wp-block-editor', |
|
| 76 | + 'wp-block-library', |
|
| 77 | + 'wp-edit-post', |
|
| 78 | + 'wp-edit-widgets', |
|
| 79 | + 'wp-editor', |
|
| 80 | + 'wp-format-library', |
|
| 81 | + 'wp-list-reusable-blocks', |
|
| 82 | + 'wp-nux', |
|
| 83 | + ]; |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * AssetManifest constructor. |
|
| 88 | + * |
|
| 89 | + * @param DomainInterface $domain |
|
| 90 | + */ |
|
| 91 | + public function __construct(DomainInterface $domain) |
|
| 92 | + { |
|
| 93 | + $this->domain = $domain; |
|
| 94 | + $this->initialize(); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @return void |
|
| 100 | + */ |
|
| 101 | + public function initialize() |
|
| 102 | + { |
|
| 103 | + if (! $this->manifest) { |
|
| 104 | + $this->assets_namespace = $this->domain->assetNamespace(); |
|
| 105 | + $assets_path = $this->domain->distributionAssetsPath(); |
|
| 106 | + $this->assets_path = trailingslashit($assets_path); |
|
| 107 | + $this->setManifestFilepath(); |
|
| 108 | + $this->loadManifest(); |
|
| 109 | + $this->getAssetFiles(); |
|
| 110 | + $this->getEntryPoints(); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @param string $manifest_path |
|
| 117 | + */ |
|
| 118 | + public function setManifestFilepath($manifest_path = '') |
|
| 119 | + { |
|
| 120 | + $manifest_path = $manifest_path ? $manifest_path : $this->assets_path . AssetManifest::FILE_NAME; |
|
| 121 | + if (! is_readable($manifest_path)) { |
|
| 122 | + throw new AssetManifestException( |
|
| 123 | + $manifest_path, |
|
| 124 | + '', |
|
| 125 | + sprintf( |
|
| 126 | + esc_html__( |
|
| 127 | + 'The "%1$s" file was not found or is not readable. Please verify that the file exists and has appropriate permissions.', |
|
| 128 | + 'event_espresso' |
|
| 129 | + ), |
|
| 130 | + $manifest_path |
|
| 131 | + ) |
|
| 132 | + ); |
|
| 133 | + } |
|
| 134 | + $this->manifest_path = $manifest_path; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @return void |
|
| 140 | + */ |
|
| 141 | + private function loadManifest() |
|
| 142 | + { |
|
| 143 | + if (! $this->manifest) { |
|
| 144 | + // TODO May be use WP File system? ¯\_(ツ)_/¯ |
|
| 145 | + $manifest_json = file_get_contents($this->manifest_path); |
|
| 146 | + $this->manifest = json_decode($manifest_json, true); |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @param string $file_name |
|
| 153 | + * @return string |
|
| 154 | + */ |
|
| 155 | + private function trimAssetFilename($file_name) |
|
| 156 | + { |
|
| 157 | + return ltrim($file_name, '/'); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @return array |
|
| 163 | + */ |
|
| 164 | + public function getAssetFiles() |
|
| 165 | + { |
|
| 166 | + if (! $this->asset_files) { |
|
| 167 | + if (empty($this->manifest[ AssetManifest::KEY_FILES ])) { |
|
| 168 | + if (WP_DEBUG) { |
|
| 169 | + throw new AssetManifestException(AssetManifest::KEY_FILES, $this->manifest_path); |
|
| 170 | + } |
|
| 171 | + return []; |
|
| 172 | + } |
|
| 173 | + $this->asset_files = $this->manifest[ AssetManifest::KEY_FILES ]; |
|
| 174 | + } |
|
| 175 | + return $this->asset_files; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @return array |
|
| 181 | + */ |
|
| 182 | + public function getEntryPoints() |
|
| 183 | + { |
|
| 184 | + if (! $this->entry_points) { |
|
| 185 | + if (empty($this->manifest[ AssetManifest::KEY_ENTRY_POINTS ])) { |
|
| 186 | + if (WP_DEBUG) { |
|
| 187 | + throw new AssetManifestException(AssetManifest::KEY_ENTRY_POINTS, $this->manifest_path); |
|
| 188 | + } |
|
| 189 | + return []; |
|
| 190 | + } |
|
| 191 | + $this->entry_points = array_keys($this->manifest[ AssetManifest::KEY_ENTRY_POINTS ]); |
|
| 192 | + } |
|
| 193 | + return $this->entry_points; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @param string $entry_point |
|
| 199 | + * @param string $type |
|
| 200 | + * @return string |
|
| 201 | + */ |
|
| 202 | + private function getAsset($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
|
| 203 | + { |
|
| 204 | + return $this->hasAsset($entry_point, $type) |
|
| 205 | + ? $this->trimAssetFilename($this->asset_files[ $entry_point . $type ]) |
|
| 206 | + : ''; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * @param string $entry_point |
|
| 212 | + * @param string $type |
|
| 213 | + * @return array |
|
| 214 | + */ |
|
| 215 | + public function getAssetDependencies($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
|
| 216 | + { |
|
| 217 | + $asset = $this->getAssetDetails($entry_point); |
|
| 218 | + if (! isset($asset[AssetManifest::KEY_DEPENDENCIES])) { |
|
| 219 | + return []; |
|
| 220 | + } |
|
| 221 | + $dependencies = $asset[AssetManifest::KEY_DEPENDENCIES]; |
|
| 222 | + // remove cyclical dependencies, if any |
|
| 223 | + if (($key = array_search($this->assets_namespace . '-' . $entry_point, $dependencies, true)) !== false) { |
|
| 224 | + unset($dependencies[ $key ]); |
|
| 225 | + } |
|
| 226 | + // currently need to derive dependencies for CSS from the JS dependencies |
|
| 227 | + if ($type === AssetManifest::ASSET_EXT_CSS) { |
|
| 228 | + $css_dependencies = []; |
|
| 229 | + foreach ($dependencies as $handle) { |
|
| 230 | + $dependency_style = $this->getEntryPointFromHandle($handle) . AssetManifest::ASSET_EXT_CSS; |
|
| 231 | + if (isset($this->asset_files[ $dependency_style ]) |
|
| 232 | + || in_array($handle, $this->wp_css_handle_dependencies) |
|
| 233 | + ) { |
|
| 234 | + $css_dependencies[] = $handle; |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + return $css_dependencies; |
|
| 238 | + } |
|
| 239 | + return $dependencies; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * @param string $entry_point |
|
| 245 | + * @return array |
|
| 246 | + */ |
|
| 247 | + public function getAssetDetails($entry_point) |
|
| 248 | + { |
|
| 249 | + $file_name = $this->getAsset($entry_point, AssetManifest::ASSET_EXT_PHP); |
|
| 250 | + if (! $file_name) { |
|
| 251 | + return []; |
|
| 252 | + } |
|
| 253 | + $full_path = $this->assets_path . $file_name; |
|
| 254 | + if (! is_readable($full_path)) { |
|
| 255 | + if (WP_DEBUG) { |
|
| 256 | + throw new AssetManifestException(AssetManifest::KEY_DEPENDENCIES, $full_path); |
|
| 257 | + } |
|
| 258 | + return []; |
|
| 259 | + } |
|
| 260 | + return require($full_path); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * @param string $entry_point |
|
| 266 | + * @return string|int|false |
|
| 267 | + */ |
|
| 268 | + public function getAssetHandle($entry_point) |
|
| 269 | + { |
|
| 270 | + return $this->assets_namespace . '-' . $entry_point; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * @return string |
|
| 276 | + */ |
|
| 277 | + public function getAssetsPath() |
|
| 278 | + { |
|
| 279 | + return $this->assets_path; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * @param string $handle |
|
| 285 | + * @return string|int|false |
|
| 286 | + */ |
|
| 287 | + private function getEntryPointFromHandle($handle) |
|
| 288 | + { $find = $this->assets_namespace . '-'; |
|
| 289 | + return str_replace($find, '', $handle); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * @param string $entry_point |
|
| 295 | + * @param string $type |
|
| 296 | + * @return string |
|
| 297 | + */ |
|
| 298 | + public function getAssetPath($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
|
| 299 | + { |
|
| 300 | + $file_name = $this->getAsset($entry_point, $type); |
|
| 301 | + return $file_name ? $this->domain->distributionAssetsPath($file_name) : ''; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * @param string $entry_point |
|
| 307 | + * @param string $type |
|
| 308 | + * @return string |
|
| 309 | + */ |
|
| 310 | + public function getAssetUrl($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
|
| 311 | + { |
|
| 312 | + $file_name = $this->getAsset($entry_point, $type); |
|
| 313 | + return $file_name ? $this->domain->distributionAssetsUrl($file_name) : ''; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * @param string $entry_point |
|
| 319 | + * @param string $type |
|
| 320 | + * @return string|int|false |
|
| 321 | + */ |
|
| 322 | + public function getAssetVersion($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
|
| 323 | + { |
|
| 324 | + $asset = $this->getAssetDetails($entry_point); |
|
| 325 | + return $type === AssetManifest::ASSET_EXT_JS && isset($asset[AssetManifest::KEY_VERSION]) |
|
| 326 | + ? $asset[AssetManifest::KEY_VERSION] |
|
| 327 | + : filemtime($this->getAssetPath($entry_point, $type)); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * @param string $entry_point |
|
| 333 | + * @param string $type |
|
| 334 | + * @return string |
|
| 335 | + */ |
|
| 336 | + public function hasAsset($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
|
| 337 | + { |
|
| 338 | + $file_name = $entry_point . $type; |
|
| 339 | + return ! empty($this->asset_files[ $file_name ]); |
|
| 340 | + } |
|
| 341 | 341 | } |
@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | class AssetManifest implements AssetManifestInterface |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - const ASSET_EXT_CSS = '.css'; |
|
| 17 | + const ASSET_EXT_CSS = '.css'; |
|
| 18 | 18 | |
| 19 | 19 | const ASSET_EXT_JS = '.js'; |
| 20 | 20 | |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | */ |
| 101 | 101 | public function initialize() |
| 102 | 102 | { |
| 103 | - if (! $this->manifest) { |
|
| 103 | + if ( ! $this->manifest) { |
|
| 104 | 104 | $this->assets_namespace = $this->domain->assetNamespace(); |
| 105 | 105 | $assets_path = $this->domain->distributionAssetsPath(); |
| 106 | 106 | $this->assets_path = trailingslashit($assets_path); |
@@ -117,8 +117,8 @@ discard block |
||
| 117 | 117 | */ |
| 118 | 118 | public function setManifestFilepath($manifest_path = '') |
| 119 | 119 | { |
| 120 | - $manifest_path = $manifest_path ? $manifest_path : $this->assets_path . AssetManifest::FILE_NAME; |
|
| 121 | - if (! is_readable($manifest_path)) { |
|
| 120 | + $manifest_path = $manifest_path ? $manifest_path : $this->assets_path.AssetManifest::FILE_NAME; |
|
| 121 | + if ( ! is_readable($manifest_path)) { |
|
| 122 | 122 | throw new AssetManifestException( |
| 123 | 123 | $manifest_path, |
| 124 | 124 | '', |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | */ |
| 141 | 141 | private function loadManifest() |
| 142 | 142 | { |
| 143 | - if (! $this->manifest) { |
|
| 143 | + if ( ! $this->manifest) { |
|
| 144 | 144 | // TODO May be use WP File system? ¯\_(ツ)_/¯ |
| 145 | 145 | $manifest_json = file_get_contents($this->manifest_path); |
| 146 | 146 | $this->manifest = json_decode($manifest_json, true); |
@@ -163,14 +163,14 @@ discard block |
||
| 163 | 163 | */ |
| 164 | 164 | public function getAssetFiles() |
| 165 | 165 | { |
| 166 | - if (! $this->asset_files) { |
|
| 167 | - if (empty($this->manifest[ AssetManifest::KEY_FILES ])) { |
|
| 166 | + if ( ! $this->asset_files) { |
|
| 167 | + if (empty($this->manifest[AssetManifest::KEY_FILES])) { |
|
| 168 | 168 | if (WP_DEBUG) { |
| 169 | 169 | throw new AssetManifestException(AssetManifest::KEY_FILES, $this->manifest_path); |
| 170 | 170 | } |
| 171 | 171 | return []; |
| 172 | 172 | } |
| 173 | - $this->asset_files = $this->manifest[ AssetManifest::KEY_FILES ]; |
|
| 173 | + $this->asset_files = $this->manifest[AssetManifest::KEY_FILES]; |
|
| 174 | 174 | } |
| 175 | 175 | return $this->asset_files; |
| 176 | 176 | } |
@@ -181,14 +181,14 @@ discard block |
||
| 181 | 181 | */ |
| 182 | 182 | public function getEntryPoints() |
| 183 | 183 | { |
| 184 | - if (! $this->entry_points) { |
|
| 185 | - if (empty($this->manifest[ AssetManifest::KEY_ENTRY_POINTS ])) { |
|
| 184 | + if ( ! $this->entry_points) { |
|
| 185 | + if (empty($this->manifest[AssetManifest::KEY_ENTRY_POINTS])) { |
|
| 186 | 186 | if (WP_DEBUG) { |
| 187 | 187 | throw new AssetManifestException(AssetManifest::KEY_ENTRY_POINTS, $this->manifest_path); |
| 188 | 188 | } |
| 189 | 189 | return []; |
| 190 | 190 | } |
| 191 | - $this->entry_points = array_keys($this->manifest[ AssetManifest::KEY_ENTRY_POINTS ]); |
|
| 191 | + $this->entry_points = array_keys($this->manifest[AssetManifest::KEY_ENTRY_POINTS]); |
|
| 192 | 192 | } |
| 193 | 193 | return $this->entry_points; |
| 194 | 194 | } |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | private function getAsset($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
| 203 | 203 | { |
| 204 | 204 | return $this->hasAsset($entry_point, $type) |
| 205 | - ? $this->trimAssetFilename($this->asset_files[ $entry_point . $type ]) |
|
| 205 | + ? $this->trimAssetFilename($this->asset_files[$entry_point.$type]) |
|
| 206 | 206 | : ''; |
| 207 | 207 | } |
| 208 | 208 | |
@@ -215,20 +215,20 @@ discard block |
||
| 215 | 215 | public function getAssetDependencies($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
| 216 | 216 | { |
| 217 | 217 | $asset = $this->getAssetDetails($entry_point); |
| 218 | - if (! isset($asset[AssetManifest::KEY_DEPENDENCIES])) { |
|
| 218 | + if ( ! isset($asset[AssetManifest::KEY_DEPENDENCIES])) { |
|
| 219 | 219 | return []; |
| 220 | 220 | } |
| 221 | 221 | $dependencies = $asset[AssetManifest::KEY_DEPENDENCIES]; |
| 222 | 222 | // remove cyclical dependencies, if any |
| 223 | - if (($key = array_search($this->assets_namespace . '-' . $entry_point, $dependencies, true)) !== false) { |
|
| 224 | - unset($dependencies[ $key ]); |
|
| 223 | + if (($key = array_search($this->assets_namespace.'-'.$entry_point, $dependencies, true)) !== false) { |
|
| 224 | + unset($dependencies[$key]); |
|
| 225 | 225 | } |
| 226 | 226 | // currently need to derive dependencies for CSS from the JS dependencies |
| 227 | 227 | if ($type === AssetManifest::ASSET_EXT_CSS) { |
| 228 | 228 | $css_dependencies = []; |
| 229 | 229 | foreach ($dependencies as $handle) { |
| 230 | - $dependency_style = $this->getEntryPointFromHandle($handle) . AssetManifest::ASSET_EXT_CSS; |
|
| 231 | - if (isset($this->asset_files[ $dependency_style ]) |
|
| 230 | + $dependency_style = $this->getEntryPointFromHandle($handle).AssetManifest::ASSET_EXT_CSS; |
|
| 231 | + if (isset($this->asset_files[$dependency_style]) |
|
| 232 | 232 | || in_array($handle, $this->wp_css_handle_dependencies) |
| 233 | 233 | ) { |
| 234 | 234 | $css_dependencies[] = $handle; |
@@ -247,11 +247,11 @@ discard block |
||
| 247 | 247 | public function getAssetDetails($entry_point) |
| 248 | 248 | { |
| 249 | 249 | $file_name = $this->getAsset($entry_point, AssetManifest::ASSET_EXT_PHP); |
| 250 | - if (! $file_name) { |
|
| 250 | + if ( ! $file_name) { |
|
| 251 | 251 | return []; |
| 252 | 252 | } |
| 253 | - $full_path = $this->assets_path . $file_name; |
|
| 254 | - if (! is_readable($full_path)) { |
|
| 253 | + $full_path = $this->assets_path.$file_name; |
|
| 254 | + if ( ! is_readable($full_path)) { |
|
| 255 | 255 | if (WP_DEBUG) { |
| 256 | 256 | throw new AssetManifestException(AssetManifest::KEY_DEPENDENCIES, $full_path); |
| 257 | 257 | } |
@@ -267,7 +267,7 @@ discard block |
||
| 267 | 267 | */ |
| 268 | 268 | public function getAssetHandle($entry_point) |
| 269 | 269 | { |
| 270 | - return $this->assets_namespace . '-' . $entry_point; |
|
| 270 | + return $this->assets_namespace.'-'.$entry_point; |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | * @return string|int|false |
| 286 | 286 | */ |
| 287 | 287 | private function getEntryPointFromHandle($handle) |
| 288 | - { $find = $this->assets_namespace . '-'; |
|
| 288 | + { $find = $this->assets_namespace.'-'; |
|
| 289 | 289 | return str_replace($find, '', $handle); |
| 290 | 290 | } |
| 291 | 291 | |
@@ -335,7 +335,7 @@ discard block |
||
| 335 | 335 | */ |
| 336 | 336 | public function hasAsset($entry_point, $type = AssetManifest::ASSET_EXT_JS) |
| 337 | 337 | { |
| 338 | - $file_name = $entry_point . $type; |
|
| 339 | - return ! empty($this->asset_files[ $file_name ]); |
|
| 338 | + $file_name = $entry_point.$type; |
|
| 339 | + return ! empty($this->asset_files[$file_name]); |
|
| 340 | 340 | } |
| 341 | 341 | } |
@@ -72,13 +72,13 @@ |
||
| 72 | 72 | private function getBaristaForDomain(AssetManifestInterface $asset_manifest, DomainInterface $domain) |
| 73 | 73 | { |
| 74 | 74 | $domain_fqcn = get_class($domain); |
| 75 | - if (! isset(BaristaFactory::$baristas[ $domain_fqcn ])) { |
|
| 75 | + if ( ! isset(BaristaFactory::$baristas[$domain_fqcn])) { |
|
| 76 | 76 | $barista = new Barista($asset_manifest); |
| 77 | 77 | // we still need to share this with the core loader to facilitate automatic dependency injection |
| 78 | 78 | $this->loader->share(Barista::class, $barista, [$asset_manifest]); |
| 79 | - BaristaFactory::$baristas[ $domain_fqcn ] = $barista; |
|
| 79 | + BaristaFactory::$baristas[$domain_fqcn] = $barista; |
|
| 80 | 80 | } |
| 81 | - return BaristaFactory::$baristas[ $domain_fqcn ]; |
|
| 81 | + return BaristaFactory::$baristas[$domain_fqcn]; |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | |
@@ -11,114 +11,114 @@ |
||
| 11 | 11 | |
| 12 | 12 | class BaristaFactory implements FactoryInterface |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var AssetManifestFactory |
|
| 16 | - */ |
|
| 17 | - private $manifest_factory; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @var BaristaInterface[] |
|
| 21 | - */ |
|
| 22 | - private static $baristas = []; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var LoaderInterface $loader |
|
| 26 | - */ |
|
| 27 | - protected $loader; |
|
| 28 | - |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * BaristaFactory constructor. |
|
| 32 | - * |
|
| 33 | - * @param AssetManifestFactory $manifest_factory |
|
| 34 | - * @param LoaderInterface $loader |
|
| 35 | - */ |
|
| 36 | - public function __construct(AssetManifestFactory $manifest_factory, LoaderInterface $loader) |
|
| 37 | - { |
|
| 38 | - $this->manifest_factory = $manifest_factory; |
|
| 39 | - $this->loader = $loader; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @param string $domain_fqcn |
|
| 45 | - * @return BaristaInterface |
|
| 46 | - */ |
|
| 47 | - public function createFromDomainClass($domain_fqcn) |
|
| 48 | - { |
|
| 49 | - /** @var DomainInterface $domain */ |
|
| 50 | - $domain = $this->loader->getShared($domain_fqcn); |
|
| 51 | - return $this->createFromDomainObject($domain); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @param DomainInterface $domain |
|
| 57 | - * @return BaristaInterface |
|
| 58 | - */ |
|
| 59 | - public function createFromDomainObject(DomainInterface $domain) |
|
| 60 | - { |
|
| 61 | - $asset_manifest = $this->manifest_factory->createFromDomainObject($domain); |
|
| 62 | - return $this->getBaristaForDomain($asset_manifest, $domain); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @param string $domain_fqcn Fully Qualified Class Name for the applicable DomainInterface class |
|
| 68 | - * @param array $domain_arguments arguments required by the applicable DomainInterface class |
|
| 69 | - * @return BaristaInterface |
|
| 70 | - */ |
|
| 71 | - public function create($domain_fqcn = '', array $domain_arguments = []) |
|
| 72 | - { |
|
| 73 | - $domain = $this->getDomain($domain_fqcn, $domain_arguments); |
|
| 74 | - $asset_manifest = $this->manifest_factory->createFromDomainObject($domain); |
|
| 75 | - return $this->getBaristaForDomain($asset_manifest, $domain); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @param AssetManifestInterface $asset_manifest |
|
| 81 | - * @param DomainInterface $domain |
|
| 82 | - * @return BaristaInterface |
|
| 83 | - */ |
|
| 84 | - private function getBaristaForDomain(AssetManifestInterface $asset_manifest, DomainInterface $domain) |
|
| 85 | - { |
|
| 86 | - $domain_fqcn = get_class($domain); |
|
| 87 | - if (! isset(BaristaFactory::$baristas[ $domain_fqcn ])) { |
|
| 88 | - $barista = new Barista($asset_manifest); |
|
| 89 | - // we still need to share this with the core loader to facilitate automatic dependency injection |
|
| 90 | - $this->loader->share(Barista::class, $barista, [$asset_manifest]); |
|
| 91 | - BaristaFactory::$baristas[ $domain_fqcn ] = $barista; |
|
| 92 | - } |
|
| 93 | - return BaristaFactory::$baristas[ $domain_fqcn ]; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @param string $domain_fqcn Fully Qualified Class Name for the applicable DomainInterface class |
|
| 99 | - * @param array $arguments |
|
| 100 | - * @return DomainInterface |
|
| 101 | - */ |
|
| 102 | - private function getDomain($domain_fqcn, array $arguments = []) |
|
| 103 | - { |
|
| 104 | - // if no FQCN is supplied for the domain, then we are loading the defaults for core |
|
| 105 | - // add-ons will always have to supply their domain FQCN and arguments to retrieve their manifest |
|
| 106 | - $domain = empty($domain_fqcn) |
|
| 107 | - ? DomainFactory::getEventEspressoCoreDomain() |
|
| 108 | - : DomainFactory::getShared(new FullyQualifiedName($domain_fqcn), $arguments); |
|
| 109 | - if ($domain instanceof DomainInterface) { |
|
| 110 | - return $domain; |
|
| 111 | - } |
|
| 112 | - throw new DomainException( |
|
| 113 | - sprintf( |
|
| 114 | - esc_html__( |
|
| 115 | - 'BaristaFactory::create() requires a fully qualified class name (FQCN) for the currently applicable Domain object. |
|
| 14 | + /** |
|
| 15 | + * @var AssetManifestFactory |
|
| 16 | + */ |
|
| 17 | + private $manifest_factory; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @var BaristaInterface[] |
|
| 21 | + */ |
|
| 22 | + private static $baristas = []; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var LoaderInterface $loader |
|
| 26 | + */ |
|
| 27 | + protected $loader; |
|
| 28 | + |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * BaristaFactory constructor. |
|
| 32 | + * |
|
| 33 | + * @param AssetManifestFactory $manifest_factory |
|
| 34 | + * @param LoaderInterface $loader |
|
| 35 | + */ |
|
| 36 | + public function __construct(AssetManifestFactory $manifest_factory, LoaderInterface $loader) |
|
| 37 | + { |
|
| 38 | + $this->manifest_factory = $manifest_factory; |
|
| 39 | + $this->loader = $loader; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @param string $domain_fqcn |
|
| 45 | + * @return BaristaInterface |
|
| 46 | + */ |
|
| 47 | + public function createFromDomainClass($domain_fqcn) |
|
| 48 | + { |
|
| 49 | + /** @var DomainInterface $domain */ |
|
| 50 | + $domain = $this->loader->getShared($domain_fqcn); |
|
| 51 | + return $this->createFromDomainObject($domain); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @param DomainInterface $domain |
|
| 57 | + * @return BaristaInterface |
|
| 58 | + */ |
|
| 59 | + public function createFromDomainObject(DomainInterface $domain) |
|
| 60 | + { |
|
| 61 | + $asset_manifest = $this->manifest_factory->createFromDomainObject($domain); |
|
| 62 | + return $this->getBaristaForDomain($asset_manifest, $domain); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @param string $domain_fqcn Fully Qualified Class Name for the applicable DomainInterface class |
|
| 68 | + * @param array $domain_arguments arguments required by the applicable DomainInterface class |
|
| 69 | + * @return BaristaInterface |
|
| 70 | + */ |
|
| 71 | + public function create($domain_fqcn = '', array $domain_arguments = []) |
|
| 72 | + { |
|
| 73 | + $domain = $this->getDomain($domain_fqcn, $domain_arguments); |
|
| 74 | + $asset_manifest = $this->manifest_factory->createFromDomainObject($domain); |
|
| 75 | + return $this->getBaristaForDomain($asset_manifest, $domain); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @param AssetManifestInterface $asset_manifest |
|
| 81 | + * @param DomainInterface $domain |
|
| 82 | + * @return BaristaInterface |
|
| 83 | + */ |
|
| 84 | + private function getBaristaForDomain(AssetManifestInterface $asset_manifest, DomainInterface $domain) |
|
| 85 | + { |
|
| 86 | + $domain_fqcn = get_class($domain); |
|
| 87 | + if (! isset(BaristaFactory::$baristas[ $domain_fqcn ])) { |
|
| 88 | + $barista = new Barista($asset_manifest); |
|
| 89 | + // we still need to share this with the core loader to facilitate automatic dependency injection |
|
| 90 | + $this->loader->share(Barista::class, $barista, [$asset_manifest]); |
|
| 91 | + BaristaFactory::$baristas[ $domain_fqcn ] = $barista; |
|
| 92 | + } |
|
| 93 | + return BaristaFactory::$baristas[ $domain_fqcn ]; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @param string $domain_fqcn Fully Qualified Class Name for the applicable DomainInterface class |
|
| 99 | + * @param array $arguments |
|
| 100 | + * @return DomainInterface |
|
| 101 | + */ |
|
| 102 | + private function getDomain($domain_fqcn, array $arguments = []) |
|
| 103 | + { |
|
| 104 | + // if no FQCN is supplied for the domain, then we are loading the defaults for core |
|
| 105 | + // add-ons will always have to supply their domain FQCN and arguments to retrieve their manifest |
|
| 106 | + $domain = empty($domain_fqcn) |
|
| 107 | + ? DomainFactory::getEventEspressoCoreDomain() |
|
| 108 | + : DomainFactory::getShared(new FullyQualifiedName($domain_fqcn), $arguments); |
|
| 109 | + if ($domain instanceof DomainInterface) { |
|
| 110 | + return $domain; |
|
| 111 | + } |
|
| 112 | + throw new DomainException( |
|
| 113 | + sprintf( |
|
| 114 | + esc_html__( |
|
| 115 | + 'BaristaFactory::create() requires a fully qualified class name (FQCN) for the currently applicable Domain object. |
|
| 116 | 116 | %1$sThe supplied FQCN ("%2$s") is either invalid or the class is missing.', |
| 117 | - 'event_espresso' |
|
| 118 | - ), |
|
| 119 | - '<br />', |
|
| 120 | - $domain_fqcn |
|
| 121 | - ) |
|
| 122 | - ); |
|
| 123 | - } |
|
| 117 | + 'event_espresso' |
|
| 118 | + ), |
|
| 119 | + '<br />', |
|
| 120 | + $domain_fqcn |
|
| 121 | + ) |
|
| 122 | + ); |
|
| 123 | + } |
|
| 124 | 124 | } |
@@ -15,35 +15,35 @@ |
||
| 15 | 15 | class JedLocaleData |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var array $locales |
|
| 20 | - */ |
|
| 21 | - protected $locales = []; |
|
| 18 | + /** |
|
| 19 | + * @var array $locales |
|
| 20 | + */ |
|
| 21 | + protected $locales = []; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Returns Jed-formatted localization data. |
|
| 25 | - * |
|
| 26 | - * @param string $domain Translation domain. |
|
| 27 | - * @return array |
|
| 28 | - */ |
|
| 29 | - public function getData($domain = Domain::TEXT_DOMAIN) |
|
| 30 | - { |
|
| 31 | - if (! isset($locales[ $domain ])) { |
|
| 32 | - $translations = get_translations_for_domain($domain); |
|
| 33 | - $locale = [ |
|
| 34 | - '' => [ |
|
| 35 | - 'domain' => $domain, |
|
| 36 | - 'lang' => is_admin() ? EEH_DTT_Helper::get_user_locale() : get_locale() |
|
| 37 | - ], |
|
| 38 | - ]; |
|
| 39 | - if (! empty($translations->headers['Plural-Forms'])) { |
|
| 40 | - $locale['']['plural_forms'] = $translations->headers['Plural-Forms']; |
|
| 41 | - } |
|
| 42 | - foreach ($translations->entries as $id => $entry) { |
|
| 43 | - $locale[ $id ] = $entry->translations; |
|
| 44 | - } |
|
| 45 | - $locales[ $domain ] = $locale; |
|
| 46 | - } |
|
| 47 | - return $locales[ $domain ]; |
|
| 48 | - } |
|
| 23 | + /** |
|
| 24 | + * Returns Jed-formatted localization data. |
|
| 25 | + * |
|
| 26 | + * @param string $domain Translation domain. |
|
| 27 | + * @return array |
|
| 28 | + */ |
|
| 29 | + public function getData($domain = Domain::TEXT_DOMAIN) |
|
| 30 | + { |
|
| 31 | + if (! isset($locales[ $domain ])) { |
|
| 32 | + $translations = get_translations_for_domain($domain); |
|
| 33 | + $locale = [ |
|
| 34 | + '' => [ |
|
| 35 | + 'domain' => $domain, |
|
| 36 | + 'lang' => is_admin() ? EEH_DTT_Helper::get_user_locale() : get_locale() |
|
| 37 | + ], |
|
| 38 | + ]; |
|
| 39 | + if (! empty($translations->headers['Plural-Forms'])) { |
|
| 40 | + $locale['']['plural_forms'] = $translations->headers['Plural-Forms']; |
|
| 41 | + } |
|
| 42 | + foreach ($translations->entries as $id => $entry) { |
|
| 43 | + $locale[ $id ] = $entry->translations; |
|
| 44 | + } |
|
| 45 | + $locales[ $domain ] = $locale; |
|
| 46 | + } |
|
| 47 | + return $locales[ $domain ]; |
|
| 48 | + } |
|
| 49 | 49 | } |
| 50 | 50 | \ No newline at end of file |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | public function getData($domain = Domain::TEXT_DOMAIN) |
| 30 | 30 | { |
| 31 | - if (! isset($locales[ $domain ])) { |
|
| 31 | + if ( ! isset($locales[$domain])) { |
|
| 32 | 32 | $translations = get_translations_for_domain($domain); |
| 33 | 33 | $locale = [ |
| 34 | 34 | '' => [ |
@@ -36,14 +36,14 @@ discard block |
||
| 36 | 36 | 'lang' => is_admin() ? EEH_DTT_Helper::get_user_locale() : get_locale() |
| 37 | 37 | ], |
| 38 | 38 | ]; |
| 39 | - if (! empty($translations->headers['Plural-Forms'])) { |
|
| 39 | + if ( ! empty($translations->headers['Plural-Forms'])) { |
|
| 40 | 40 | $locale['']['plural_forms'] = $translations->headers['Plural-Forms']; |
| 41 | 41 | } |
| 42 | 42 | foreach ($translations->entries as $id => $entry) { |
| 43 | - $locale[ $id ] = $entry->translations; |
|
| 43 | + $locale[$id] = $entry->translations; |
|
| 44 | 44 | } |
| 45 | - $locales[ $domain ] = $locale; |
|
| 45 | + $locales[$domain] = $locale; |
|
| 46 | 46 | } |
| 47 | - return $locales[ $domain ]; |
|
| 47 | + return $locales[$domain]; |
|
| 48 | 48 | } |
| 49 | 49 | } |
| 50 | 50 | \ No newline at end of file |