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
elasticpress / includes / classes / Indexables.php

Indexables.php in ElasticPress 5.3.5, at includes/classes/Indexables.php

165 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles indexable registration and storage
4 *
5 * @since 3.0
6 * @package elasticpress
7 */
8
9 namespace ElasticPress;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly.
13 }
14
15 /**
16 * Class for handling all Indexable instances
17 */
18 class Indexables {
19
20 /**
21 * Array of registered indexables
22 *
23 * @var array
24 * @since 3.0
25 */
26 private $registered_indexables = [];
27
28 /**
29 * Array of active indexables
30 *
31 * @var array
32 * @since 4.5.0
33 */
34 private $active_indexables = [];
35
36 /**
37 * Register an indexable instance
38 *
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
42 */
43 public function register( Indexable $indexable, bool $activate = true ) {
44 $this->registered_indexables[ $indexable->slug ] = $indexable;
45 if ( $activate ) {
46 $this->active_indexables[ $indexable->slug ] = $indexable;
47 }
48 }
49
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 /**
62 * Get an indexable instance given a slug
63 *
64 * @param string $slug Indexable type slug.
65 * @since 3.0
66 * @return Indexable|boolean
67 */
68 public function get( $slug ) {
69 return ( ! empty( $this->registered_indexables[ $slug ] ) ) ? $this->registered_indexables[ $slug ] : false;
70 }
71
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 /**
119 * Get all indexable instances
120 *
121 * @param boolean $global_property If true or false, will only get Indexables with that global property.
122 * @param boolean $slug_only True returns an array of only string slugs.
123 * @param string $status Whether to return active indexables or all registered.
124 * @since 3.0, 4.5.0 Added $status
125 * @return array
126 */
127 public function get_all( $global_property = null, $slug_only = false, $status = 'active' ) {
128 $indexables = [];
129 $list = ( 'active' === $status ) ? $this->active_indexables : $this->registered_indexables;
130
131 foreach ( $list as $slug => $indexable ) {
132 if ( null === $global_property ) {
133 if ( $slug_only ) {
134 $indexables[] = $slug;
135 } else {
136 $indexables[] = $indexable;
137 }
138 } elseif ( $global_property === $indexable->global ) {
139 if ( $slug_only ) {
140 $indexables[] = $slug;
141 } else {
142 $indexables[] = $indexable;
143 }
144 }
145 }
146
147 return $indexables;
148 }
149
150 /**
151 * Return singleton instance of class
152 *
153 * @return self
154 */
155 public static function factory() {
156 static $instance = false;
157
158 if ( ! $instance ) {
159 $instance = new self();
160 }
161
162 return $instance;
163 }
164 }
165