PluginProbe
ElasticPress / 5.3.5
ElasticPress v5.3.5
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
← All changes | includes/classes/Indexables.php +75 -16 4.4.15.3.5 View file →
@@ -25,15 +25,27 @@
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 /**
39 51 * Unregister an indexable instance
@@ -41,8 +53,9 @@
41 53 * @param string $slug Indexable type slug.
42 54 * @since 4.4.1
43 55 */
44 56 public function unregister( $slug ) {
57 + unset( $this->active_indexables[ $slug ] );
45 58 unset( $this->registered_indexables[ $slug ] );
46 59 }
47 60
48 61 /**
@@ -56,32 +69,78 @@
56 69 return ( ! empty( $this->registered_indexables[ $slug ] ) ) ? $this->registered_indexables[ $slug ] : false;
57 70 }
58 71
59 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 + /**
60 119 * Get all indexable instances
61 120 *
62 - * @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.
63 122 * @param boolean $slug_only True returns an array of only string slugs.
64 - * @since 3.0
123 + * @param string $status Whether to return active indexables or all registered.
124 + * @since 3.0, 4.5.0 Added $status
65 125 * @return array
66 126 */
67 - public function get_all( $global = null, $slug_only = false ) {
127 + public function get_all( $global_property = null, $slug_only = false, $status = 'active' ) {
68 128 $indexables = [];
129 + $list = ( 'active' === $status ) ? $this->active_indexables : $this->registered_indexables;
69 130
70 - foreach ( $this->registered_indexables as $slug => $indexable ) {
71 - if ( null === $global ) {
131 + foreach ( $list as $slug => $indexable ) {
132 + if ( null === $global_property ) {
72 133 if ( $slug_only ) {
73 134 $indexables[] = $slug;
74 135 } else {
75 136 $indexables[] = $indexable;
76 137 }
77 - } else {
78 - if ( $global === $indexable->global ) {
79 - if ( $slug_only ) {
80 - $indexables[] = $slug;
81 - } else {
82 - $indexables[] = $indexable;
83 - }
138 + } elseif ( $global_property === $indexable->global ) {
139 + if ( $slug_only ) {
140 + $indexables[] = $slug;
141 + } else {
142 + $indexables[] = $indexable;
84 143 }
85 144 }
86 145 }
87 146
@@ -90,9 +149,9 @@
90 149
91 150 /**
92 151 * Return singleton instance of class
93 152 *
94 - * @return object
153 + * @return self
95 154 */
96 155 public static function factory() {
97 156 static $instance = false;
98 157