|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DotOrg\TryWordPress; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use InvalidArgumentException; |
| 7 | + |
| 8 | +class TransformersRegistry { |
| 9 | + private static string $user_choice_meta_key_prefix = '_data_liberation_chosen_handler_'; |
| 10 | + private static array $handlers = array(); |
| 11 | + |
| 12 | + /** |
| 13 | + * Add a handler for a specific subject type |
| 14 | + * |
| 15 | + * @param SubjectType $type The subject type for which handler should be registered. |
| 16 | + * @param array $identifier Array containing unique slug and description. |
| 17 | + * @param callable $handler The handler function. |
| 18 | + * @return void |
| 19 | + * @throws InvalidArgumentException If handler is not callable. |
| 20 | + */ |
| 21 | + public static function add( SubjectType $type, array $identifier, callable $handler ): void { |
| 22 | + if ( ! is_callable( $handler ) ) { |
| 23 | + throw new InvalidArgumentException( 'Handler must be callable' ); |
| 24 | + } |
| 25 | + |
| 26 | + if ( ! isset( $identifier['slug'] ) ) { |
| 27 | + throw new InvalidArgumentException( 'Identifier slug must be defined' ); |
| 28 | + } |
| 29 | + |
| 30 | + if ( ! isset( self::$handlers[ $type->value ] ) ) { |
| 31 | + self::$handlers[ $type->value ] = array(); |
| 32 | + } |
| 33 | + |
| 34 | + self::$handlers[ $type->value ][ $identifier['slug'] ] = array( |
| 35 | + 'slug' => $identifier['slug'], |
| 36 | + 'description' => $identifier['desc'], |
| 37 | + 'handler' => $handler, |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Check if handlers exist for a type |
| 43 | + * |
| 44 | + * @param SubjectType $type The subject type to check for. |
| 45 | + * @return bool True if handlers exist |
| 46 | + */ |
| 47 | + public static function has( SubjectType $type ): bool { |
| 48 | + return isset( self::$handlers[ $type->value ] ) && ! empty( self::$handlers[ $type->value ] ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Check if there is a "compete" i.e., multiple handlers for a type |
| 53 | + * |
| 54 | + * @param SubjectType $type The subject type to check for. |
| 55 | + * @return bool True if handlers exist |
| 56 | + */ |
| 57 | + public static function is_compete( SubjectType $type ): bool { |
| 58 | + return isset( self::$handlers[ $type->value ] ) && count( self::$handlers[ $type->value ] ) > 1; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Execute all handlers for a type |
| 63 | + * |
| 64 | + * @param SubjectType $type The subject type to handle. |
| 65 | + * @param Subject $subject Data to pass to handlers. |
| 66 | + * @return void |
| 67 | + * @throws Exception If no handler has been registered or user choice hasn't been set when multiples are registered. |
| 68 | + */ |
| 69 | + public static function handle( SubjectType $type, Subject $subject ): void { |
| 70 | + if ( ! self::has( $type ) ) { |
| 71 | + throw new Exception( sprintf( 'no handler registered for type: %s', esc_html( $type->value ) ) ); |
| 72 | + } |
| 73 | + |
| 74 | + if ( self::is_compete( $type ) ) { |
| 75 | + $choice = self::get_user_choice( $type ); |
| 76 | + if ( ! empty( $choice ) ) { |
| 77 | + $chosen = self::$handlers[ $type->value ][ $choice ]; |
| 78 | + } else { |
| 79 | + throw new Exception( 'handle() invoked without user choice on compete' ); |
| 80 | + } |
| 81 | + } else { |
| 82 | + $chosen = current( self::$handlers[ $type->value ] ); |
| 83 | + } |
| 84 | + |
| 85 | + $transformed_post_id = $chosen['handler']( $subject ); |
| 86 | + |
| 87 | + if ( $transformed_post_id ) { |
| 88 | + update_post_meta( $subject->id(), Transformer::META_KEY_LIBERATED_OUTPUT, $transformed_post_id ); |
| 89 | + update_post_meta( $transformed_post_id, Transformer::META_KEY_LIBERATED_SOURCE, $subject->id() ); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Remove all handlers for a type |
| 95 | + * |
| 96 | + * @param SubjectType $type The type to clear handlers for. |
| 97 | + * @return void |
| 98 | + */ |
| 99 | + public static function clear( SubjectType $type ): void { |
| 100 | + if ( isset( self::$handlers[ $type->value ] ) ) { |
| 101 | + unset( self::$handlers[ $type->value ] ); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Set user choice for what transformer to run for a subject type when multiples are registered |
| 107 | + * |
| 108 | + * @param SubjectType $type The subject type for which choice is to be saved. |
| 109 | + * @param string $transformer_slug Identifying slug of the chosen transformer. |
| 110 | + * @return void |
| 111 | + */ |
| 112 | + public static function set_user_choice( SubjectType $type, string $transformer_slug ): void { |
| 113 | + update_user_meta( get_current_user_id(), self::$user_choice_meta_key_prefix . $type->value, $transformer_slug ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Retrieves the user choice for what transformer to run for a subject type when multiples are registered |
| 118 | + * |
| 119 | + * @param SubjectType $type The subject type for which choice is to be retrieved. |
| 120 | + * @return string $transformer_slug Identifying slug of the chosen transformer. |
| 121 | + */ |
| 122 | + public static function get_user_choice( SubjectType $type ): string { |
| 123 | + return get_user_meta( get_current_user_id(), self::$user_choice_meta_key_prefix . $type->value, true ); |
| 124 | + } |
| 125 | +} |
0 commit comments