| @@ -25,18 +25,41 @@ | ||
| 25 | 25 | */ |
| 26 | 26 | private $registered_indexables = []; |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | + * Array of active indexables | |
| 30 | + * | |
| 31 | + * @var array | |
| 32 | + * @since 4.5.0 | |
| 33 | + */ | |
| 34 | + private $active_indexables = []; | |
| 35 | + | |
| 36 | + /** | |
| 29 | 37 | * Register an indexable instance |
| 30 | 38 | * |
| 31 | - * @param Indexable $indexable Instance of Indexable. | |
| 32 | - * @since 3.0 | |
| 39 | + * @param Indexable $indexable Instance of Indexable | |
| 40 | + * @param bool $activate If the indexable should also be activated. Defaults to true. | |
| 41 | + * @since 3.0, 4.5.0 added $activate | |
| 33 | 42 | */ |
| 34 | - public function register( Indexable $indexable ) { | |
| 43 | + public function register( Indexable $indexable, bool $activate = true ) { | |
| 35 | 44 | $this->registered_indexables[ $indexable->slug ] = $indexable; |
| 45 | + if ( $activate ) { | |
| 46 | + $this->active_indexables[ $indexable->slug ] = $indexable; | |
| 47 | + } | |
| 36 | 48 | } |
| 37 | 49 | |
| 38 | 50 | /** |
| 51 | + * Unregister an indexable instance | |
| 52 | + * | |
| 53 | + * @param string $slug Indexable type slug. | |
| 54 | + * @since 4.4.1 | |
| 55 | + */ | |
| 56 | + public function unregister( $slug ) { | |
| 57 | + unset( $this->active_indexables[ $slug ] ); | |
| 58 | + unset( $this->registered_indexables[ $slug ] ); | |
| 59 | + } | |
| 60 | + | |
| 61 | + /** | |
| 39 | 62 | * Get an indexable instance given a slug |
| 40 | 63 | * |
| 41 | 64 | * @param string $slug Indexable type slug. |
| 42 | 65 | * @since 3.0 |
| @@ -46,32 +69,78 @@ | ||
| 46 | 69 | return ( ! empty( $this->registered_indexables[ $slug ] ) ) ? $this->registered_indexables[ $slug ] : false; |
| 47 | 70 | } |
| 48 | 71 | |
| 49 | 72 | /** |
| 73 | + * Activate an indexable functionality | |
| 74 | + * | |
| 75 | + * @param string $slug The indexable slug | |
| 76 | + * @since 4.5.0 | |
| 77 | + */ | |
| 78 | + public function activate( string $slug ) { | |
| 79 | + $indexable = $this->get( $slug ); | |
| 80 | + if ( $indexable ) { | |
| 81 | + $this->active_indexables[ $indexable->slug ] = $indexable; | |
| 82 | + if ( method_exists( $indexable, 'setup' ) ) { | |
| 83 | + $indexable->setup(); | |
| 84 | + } | |
| 85 | + } | |
| 86 | + } | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * Deactivate an indexable | |
| 90 | + * | |
| 91 | + * @param string $slug The indexable slug | |
| 92 | + * @since 4.5.0 | |
| 93 | + */ | |
| 94 | + public function deactivate( string $slug ) { | |
| 95 | + unset( $this->active_indexables[ $slug ] ); | |
| 96 | + } | |
| 97 | + | |
| 98 | + /** | |
| 99 | + * Deactivate all indexables | |
| 100 | + * | |
| 101 | + * @since 4.5.0 | |
| 102 | + */ | |
| 103 | + public function deactivate_all() { | |
| 104 | + $this->active_indexables = []; | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * Return whether an Indexable is active or not. | |
| 109 | + * | |
| 110 | + * @since 4.5.0 | |
| 111 | + * @param string $slug Indexable slug | |
| 112 | + * @return boolean | |
| 113 | + */ | |
| 114 | + public function is_active( string $slug ): bool { | |
| 115 | + return ! empty( $this->active_indexables[ $slug ] ); | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 50 | 119 | * Get all indexable instances |
| 51 | 120 | * |
| 52 | - * @param boolean $global If true or false, will only get Indexables with that global property. | |
| 121 | + * @param boolean $global_property If true or false, will only get Indexables with that global property. | |
| 53 | 122 | * @param boolean $slug_only True returns an array of only string slugs. |
| 54 | - * @since 3.0 | |
| 123 | + * @param string $status Whether to return active indexables or all registered. | |
| 124 | + * @since 3.0, 4.5.0 Added $status | |
| 55 | 125 | * @return array |
| 56 | 126 | */ |
| 57 | - public function get_all( $global = null, $slug_only = false ) { | |
| 127 | + public function get_all( $global_property = null, $slug_only = false, $status = 'active' ) { | |
| 58 | 128 | $indexables = []; |
| 129 | + $list = ( 'active' === $status ) ? $this->active_indexables : $this->registered_indexables; | |
| 59 | 130 | |
| 60 | - foreach ( $this->registered_indexables as $slug => $indexable ) { | |
| 61 | - if ( null === $global ) { | |
| 131 | + foreach ( $list as $slug => $indexable ) { | |
| 132 | + if ( null === $global_property ) { | |
| 62 | 133 | if ( $slug_only ) { |
| 63 | 134 | $indexables[] = $slug; |
| 64 | 135 | } else { |
| 65 | 136 | $indexables[] = $indexable; |
| 66 | 137 | } |
| 67 | - } else { | |
| 68 | - if ( $global === $indexable->global ) { | |
| 69 | - if ( $slug_only ) { | |
| 70 | - $indexables[] = $slug; | |
| 71 | - } else { | |
| 72 | - $indexables[] = $indexable; | |
| 73 | - } | |
| 138 | + } elseif ( $global_property === $indexable->global ) { | |
| 139 | + if ( $slug_only ) { | |
| 140 | + $indexables[] = $slug; | |
| 141 | + } else { | |
| 142 | + $indexables[] = $indexable; | |
| 74 | 143 | } |
| 75 | 144 | } |
| 76 | 145 | } |
| 77 | 146 | |
| @@ -80,9 +149,9 @@ | ||
| 80 | 149 | |
| 81 | 150 | /** |
| 82 | 151 | * Return singleton instance of class |
| 83 | 152 | * |
| 84 | - * @return object | |
| 153 | + * @return self | |
| 85 | 154 | */ |
| 86 | 155 | public static function factory() { |
| 87 | 156 | static $instance = false; |
| 88 | 157 | |