PluginProbe ʕ •ᴥ•ʔ
Search Regex / 2.0
Search Regex v2.0
trunk 1.4.12 1.4.13 1.4.14 1.4.15 1.4.16 2.0 2.0.1 2.1 2.2 2.2.1 2.3 2.3.1 2.3.2 2.3.3 2.4 2.4.1 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.1 3.1.1 3.1.2 3.2 3.3 3.3.0 3.3.1 3.4 3.4.1 3.4.2
search-regex / models / source-manager.php
search-regex / models Last commit date
match-column.php 6 years ago match-context.php 6 years ago match.php 6 years ago replace.php 6 years ago result.php 6 years ago search-flags.php 6 years ago search.php 6 years ago source-flags.php 6 years ago source-manager.php 6 years ago source.php 6 years ago
source-manager.php
210 lines
1 <?php
2
3 namespace SearchRegex;
4
5 use SearchRegex\Search_Source;
6
7 /**
8 * Create Source objects
9 */
10 class Source_Manager {
11 /**
12 * Return an array of all the database sources. Note this is filtered with `searchregex_sources`
13 *
14 * @return Array The array of database sources as name => class
15 */
16 public static function get_all_sources() {
17 $core_sources = [
18 [
19 'name' => 'posts',
20 'class' => 'SearchRegex\Source_Post',
21 'label' => __( 'All Post Types', 'search-regex' ),
22 'description' => __( 'Search all posts, pages, and custom post types.', 'search-regex' ),
23 'type' => 'core',
24 ],
25 [
26 'name' => 'post-meta',
27 'class' => 'SearchRegex\Source_Post_Meta',
28 'label' => __( 'Post Meta', 'search-regex' ),
29 'description' => __( 'Search post meta names and values.', 'search-regex' ),
30 'type' => 'core',
31 ],
32 [
33 'name' => 'comment',
34 'class' => 'SearchRegex\Source_Comment',
35 'label' => __( 'Comments', 'search-regex' ),
36 'description' => __( 'Search comment content, URL, and author, with optional support for spam comments.', 'search-regex' ),
37 'type' => 'core',
38 ],
39 [
40 'name' => 'comment-meta',
41 'class' => 'SearchRegex\Source_Comment_Meta',
42 'label' => __( 'Comment Meta', 'search-regex' ),
43 'description' => __( 'Search comment meta names and values.', 'search-regex' ),
44 'type' => 'core',
45 ],
46 [
47 'name' => 'user',
48 'class' => 'SearchRegex\Source_User',
49 'label' => __( 'Users', 'search-regex' ),
50 'description' => __( 'Search user email, URL, and name.', 'search-regex' ),
51 'type' => 'core',
52 ],
53 [
54 'name' => 'user-meta',
55 'class' => 'SearchRegex\Source_User_Meta',
56 'label' => __( 'User Meta', 'search-regex' ),
57 'description' => __( 'Search user meta name and values.', 'search-regex' ),
58 'type' => 'core',
59 ],
60 [
61 'name' => 'options',
62 'class' => 'SearchRegex\Source_Options',
63 'label' => __( 'WordPress Options', 'search-regex' ),
64 'description' => __( 'Search all WordPress options.', 'search-regex' ),
65 'type' => 'core',
66 ],
67 ];
68
69 // Add on post types
70 /** @var Array */
71 $post_types = get_post_types( [], 'objects' );
72 $post_sources = [];
73
74 foreach ( $post_types as $type ) {
75 if ( strlen( $type->label ) > 0 ) {
76 $post_sources[] = [
77 'name' => $type->name,
78 'class' => 'SearchRegex\Source_Post',
79 'label' => $type->label,
80 'type' => 'posttype',
81 ];
82 }
83 }
84
85 // Load custom stuff here
86 $plugin_sources = glob( dirname( SEARCHREGEX_FILE ) . '/source/plugin/*.php' );
87 foreach ( $plugin_sources as $plugin ) {
88 /**
89 * @psalm-suppress UnresolvableInclude
90 */
91 require_once $plugin;
92 }
93
94 $plugin_sources = apply_filters( 'searchregex_sources_plugin', [] );
95 $plugin_sources = array_map( function( $source ) {
96 $source['type'] = 'plugin';
97 return $source;
98 }, $plugin_sources );
99
100 $post_sources = apply_filters( 'searchregex_sources_posttype', $post_sources );
101 $core_sources = apply_filters( 'searchregex_sources_core', $core_sources );
102
103 return array_merge( array_values( $core_sources ), array_values( $post_sources ), array_values( $plugin_sources ) );
104 }
105
106 /**
107 * Get all the sources grouped into 'core', 'posttype', and 'plugin' groups.
108 *
109 * @return Array Associative array of sources, grouped by type
110 */
111 public static function get_all_grouped() {
112 $sources = self::get_all_sources();
113
114 $groups = [
115 [
116 'name' => 'core',
117 'label' => __( 'Standard Types', 'search-regex' ),
118 'sources' => array_values( array_filter( $sources, function( $source ) {
119 return $source['type'] === 'core';
120 } ) ),
121 ],
122 [
123 'name' => 'posttype',
124 'label' => __( 'Specific Post Types', 'search-regex' ),
125 'sources' => array_values( array_filter( $sources, function( $source ) {
126 return $source['type'] === 'posttype';
127 } ) ),
128 ],
129 [
130 'name' => 'plugin',
131 'label' => __( 'Plugins', 'search-regex' ),
132 'sources' => array_values( array_filter( $sources, function( $source ) {
133 return $source['type'] === 'plugin';
134 } ) ),
135 ],
136 ];
137
138 return array_filter( apply_filters( 'searchregex_source_groups', $groups ), function( $group ) {
139 return count( $group['sources'] ) > 0;
140 } );
141 }
142
143 /**
144 * Return a particular Source object for the given name
145 *
146 * @param String $source Source name.
147 * @param Search_Flags $search_flags Search_Flags.
148 * @param Source_Flags $source_flags Source_Flags.
149 * @return object|null New Source_Flags object
150 */
151 private static function get_handler_for_source( $source, Search_Flags $search_flags, Source_Flags $source_flags ) {
152 $sources = self::get_all_sources();
153
154 foreach ( $sources as $handler ) {
155 if ( $handler['name'] === $source ) {
156 $new_source = new $handler['class']( $handler, $search_flags, $source_flags );
157 $source_flags->set_allowed_flags( array_keys( $new_source->get_supported_flags() ) );
158
159 return $new_source;
160 }
161 }
162
163 return null;
164 }
165
166 /**
167 * Return a list of all source names only. This can be used for checking a name is allowed.
168 *
169 * @return Array Array of source names
170 */
171 public static function get_all_source_names() {
172 $sources = self::get_all_sources();
173
174 return array_map( function( $source ) {
175 return $source['name'];
176 }, $sources );
177 }
178
179 /**
180 * Get all the specified sources as source objects
181 *
182 * @param Array $sources Array of source names.
183 * @param Search_Flags $search_flags The search flags object.
184 * @param Source_Flags $source_flags The source flags object.
185 * @return Array The array of source objects
186 */
187 public static function get( $sources, Search_Flags $search_flags, Source_Flags $source_flags ) {
188 $handlers = [];
189
190 foreach ( $sources as $source ) {
191 $handler = self::get_handler_for_source( $source, $search_flags, $source_flags );
192
193 if ( $handler ) {
194 $handlers[] = $handler;
195 }
196 }
197
198 return $handlers;
199 }
200 }
201
202 require_once dirname( __DIR__ ) . '/source/core/meta.php';
203 require_once dirname( __DIR__ ) . '/source/core/post.php';
204 require_once dirname( __DIR__ ) . '/source/core/post-meta.php';
205 require_once dirname( __DIR__ ) . '/source/core/user.php';
206 require_once dirname( __DIR__ ) . '/source/core/user-meta.php';
207 require_once dirname( __DIR__ ) . '/source/core/comment.php';
208 require_once dirname( __DIR__ ) . '/source/core/comment-meta.php';
209 require_once dirname( __DIR__ ) . '/source/core/options.php';
210