PluginProbe
ElasticPress / 4.4.1
ElasticPress v4.4.1
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 4.4.1, at includes/classes/Indexables.php

106 lines 2.1 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 * Register an indexable instance
30 *
31 * @param Indexable $indexable Instance of Indexable.
32 * @since 3.0
33 */
34 public function register( Indexable $indexable ) {
35 $this->registered_indexables[ $indexable->slug ] = $indexable;
36 }
37
38 /**
39 * Unregister an indexable instance
40 *
41 * @param string $slug Indexable type slug.
42 * @since 4.4.1
43 */
44 public function unregister( $slug ) {
45 unset( $this->registered_indexables[ $slug ] );
46 }
47
48 /**
49 * Get an indexable instance given a slug
50 *
51 * @param string $slug Indexable type slug.
52 * @since 3.0
53 * @return Indexable|boolean
54 */
55 public function get( $slug ) {
56 return ( ! empty( $this->registered_indexables[ $slug ] ) ) ? $this->registered_indexables[ $slug ] : false;
57 }
58
59 /**
60 * Get all indexable instances
61 *
62 * @param boolean $global If true or false, will only get Indexables with that global property.
63 * @param boolean $slug_only True returns an array of only string slugs.
64 * @since 3.0
65 * @return array
66 */
67 public function get_all( $global = null, $slug_only = false ) {
68 $indexables = [];
69
70 foreach ( $this->registered_indexables as $slug => $indexable ) {
71 if ( null === $global ) {
72 if ( $slug_only ) {
73 $indexables[] = $slug;
74 } else {
75 $indexables[] = $indexable;
76 }
77 } else {
78 if ( $global === $indexable->global ) {
79 if ( $slug_only ) {
80 $indexables[] = $slug;
81 } else {
82 $indexables[] = $indexable;
83 }
84 }
85 }
86 }
87
88 return $indexables;
89 }
90
91 /**
92 * Return singleton instance of class
93 *
94 * @return object
95 */
96 public static function factory() {
97 static $instance = false;
98
99 if ( ! $instance ) {
100 $instance = new self();
101 }
102
103 return $instance;
104 }
105 }
106