match-column.php
6 years ago
match-context.php
6 years ago
match.php
6 years ago
preset.php
5 years ago
replace.php
6 years ago
result.php
6 years ago
search-flags.php
5 years ago
search.php
5 years ago
source-flags.php
5 years ago
source-manager.php
5 years ago
source.php
5 years ago
totals.php
6 years ago
source-manager.php
288 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 the source for the post table |
| 13 | * |
| 14 | * @return Array |
| 15 | */ |
| 16 | private static function get_post_source() { |
| 17 | return [ |
| 18 | 'name' => 'posts', |
| 19 | 'class' => 'SearchRegex\Source_Post', |
| 20 | 'label' => __( 'All Post Types', 'search-regex' ), |
| 21 | 'description' => __( 'Search all posts, pages, and custom post types.', 'search-regex' ), |
| 22 | 'type' => 'core', |
| 23 | ]; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Return all the core source types |
| 28 | * |
| 29 | * @return Array |
| 30 | */ |
| 31 | private static function get_core_sources() { |
| 32 | $sources = [ |
| 33 | self::get_post_source(), |
| 34 | [ |
| 35 | 'name' => 'comment', |
| 36 | 'class' => 'SearchRegex\Source_Comment', |
| 37 | 'label' => __( 'Comments', 'search-regex' ), |
| 38 | 'description' => __( 'Search comment content, URL, and author, with optional support for spam comments.', 'search-regex' ), |
| 39 | 'type' => 'core', |
| 40 | ], |
| 41 | [ |
| 42 | 'name' => 'user', |
| 43 | 'class' => 'SearchRegex\Source_User', |
| 44 | 'label' => __( 'Users', 'search-regex' ), |
| 45 | 'description' => __( 'Search user email, URL, and name.', 'search-regex' ), |
| 46 | 'type' => 'core', |
| 47 | ], |
| 48 | [ |
| 49 | 'name' => 'options', |
| 50 | 'class' => 'SearchRegex\Source_Options', |
| 51 | 'label' => __( 'WordPress Options', 'search-regex' ), |
| 52 | 'description' => __( 'Search all WordPress options.', 'search-regex' ), |
| 53 | 'type' => 'core', |
| 54 | ], |
| 55 | ]; |
| 56 | |
| 57 | return apply_filters( 'searchregex_sources_core', $sources ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Return all the advanced source types |
| 62 | * |
| 63 | * @return Array |
| 64 | */ |
| 65 | private static function get_advanced_sources() { |
| 66 | $sources = [ |
| 67 | [ |
| 68 | 'name' => 'post-meta', |
| 69 | 'class' => 'SearchRegex\Source_Post_Meta', |
| 70 | 'label' => __( 'Post Meta', 'search-regex' ), |
| 71 | 'description' => __( 'Search post meta names and values.', 'search-regex' ), |
| 72 | 'type' => 'advanced', |
| 73 | ], |
| 74 | [ |
| 75 | 'name' => 'comment-meta', |
| 76 | 'class' => 'SearchRegex\Source_Comment_Meta', |
| 77 | 'label' => __( 'Comment Meta', 'search-regex' ), |
| 78 | 'description' => __( 'Search comment meta names and values.', 'search-regex' ), |
| 79 | 'type' => 'advanced', |
| 80 | ], |
| 81 | [ |
| 82 | 'name' => 'user-meta', |
| 83 | 'class' => 'SearchRegex\Source_User_Meta', |
| 84 | 'label' => __( 'User Meta', 'search-regex' ), |
| 85 | 'description' => __( 'Search user meta name and values.', 'search-regex' ), |
| 86 | 'type' => 'advanced', |
| 87 | ], |
| 88 | ]; |
| 89 | |
| 90 | return apply_filters( 'searchregex_sources_advanced', $sources ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Return an array of all the database sources. Note this is filtered with `searchregex_sources` |
| 95 | * |
| 96 | * @return Array The array of database sources as name => class |
| 97 | */ |
| 98 | public static function get_all_sources() { |
| 99 | $core_sources = self::get_core_sources(); |
| 100 | $advanced_sources = self::get_advanced_sources(); |
| 101 | $post_sources = self::get_all_custom_post_types(); |
| 102 | |
| 103 | // Load custom stuff here |
| 104 | $plugin_sources = glob( dirname( SEARCHREGEX_FILE ) . '/source/plugin/*.php' ); |
| 105 | foreach ( $plugin_sources as $plugin ) { |
| 106 | /** |
| 107 | * @psalm-suppress UnresolvableInclude |
| 108 | */ |
| 109 | require_once $plugin; |
| 110 | } |
| 111 | |
| 112 | $plugin_sources = apply_filters( 'searchregex_sources_plugin', [] ); |
| 113 | $plugin_sources = array_map( function( $source ) { |
| 114 | $source['type'] = 'plugin'; |
| 115 | return $source; |
| 116 | }, $plugin_sources ); |
| 117 | |
| 118 | return array_values( |
| 119 | array_merge( |
| 120 | array_values( $core_sources ), |
| 121 | array_values( $advanced_sources ), |
| 122 | array_values( $post_sources ), |
| 123 | array_values( $plugin_sources ) |
| 124 | ) |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Return an array of all the custom sources. Note this is filtered with `searchregex_sources_posttype` |
| 130 | * |
| 131 | * @return Array{name: string, class: string, label: string, type: string} The array of database sources as name => class |
| 132 | */ |
| 133 | private static function get_all_custom_post_types() { |
| 134 | /** @var Array */ |
| 135 | $post_types = get_post_types( [], 'objects' ); |
| 136 | $post_sources = []; |
| 137 | |
| 138 | foreach ( $post_types as $type ) { |
| 139 | if ( strlen( $type->label ) > 0 ) { |
| 140 | $post_sources[] = [ |
| 141 | 'name' => $type->name, |
| 142 | 'class' => 'SearchRegex\Source_Post', |
| 143 | 'label' => $type->label, |
| 144 | 'type' => 'posttype', |
| 145 | ]; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return apply_filters( 'searchregex_sources_posttype', $post_sources ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Get all the sources grouped into 'core', 'posttype', and 'plugin' groups. |
| 154 | * |
| 155 | * @return Array Associative array of sources, grouped by type |
| 156 | */ |
| 157 | public static function get_all_grouped() { |
| 158 | $sources = self::get_all_sources(); |
| 159 | |
| 160 | $groups = [ |
| 161 | [ |
| 162 | 'name' => 'core', |
| 163 | 'label' => __( 'Standard', 'search-regex' ), |
| 164 | 'sources' => array_values( array_filter( $sources, function( $source ) { |
| 165 | return $source['type'] === 'core'; |
| 166 | } ) ), |
| 167 | ], |
| 168 | [ |
| 169 | 'name' => 'posttype', |
| 170 | 'label' => __( 'Specific Post Types', 'search-regex' ), |
| 171 | 'sources' => array_values( array_filter( $sources, function( $source ) { |
| 172 | return $source['type'] === 'posttype'; |
| 173 | } ) ), |
| 174 | ], |
| 175 | [ |
| 176 | 'name' => 'advanced', |
| 177 | 'label' => __( 'Advanced', 'search-regex' ), |
| 178 | 'sources' => array_values( array_filter( $sources, function( $source ) { |
| 179 | return $source['type'] === 'advanced'; |
| 180 | } ) ), |
| 181 | ], |
| 182 | [ |
| 183 | 'name' => 'plugin', |
| 184 | 'label' => __( 'Plugins', 'search-regex' ), |
| 185 | 'sources' => array_values( array_filter( $sources, function( $source ) { |
| 186 | return $source['type'] === 'plugin'; |
| 187 | } ) ), |
| 188 | ], |
| 189 | ]; |
| 190 | |
| 191 | return array_values( array_filter( apply_filters( 'searchregex_source_groups', $groups ), function( $group ) { |
| 192 | return count( $group['sources'] ) > 0; |
| 193 | } ) ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Return a particular Source object for the given name |
| 198 | * |
| 199 | * @param String $source Source name. |
| 200 | * @param Search_Flags $search_flags Search_Flags. |
| 201 | * @param Source_Flags $source_flags Source_Flags. |
| 202 | * @return object|null New Source_Flags object |
| 203 | */ |
| 204 | private static function get_handler_for_source( $source, Search_Flags $search_flags, Source_Flags $source_flags ) { |
| 205 | $sources = self::get_all_sources(); |
| 206 | |
| 207 | foreach ( $sources as $handler ) { |
| 208 | if ( $handler['name'] === $source ) { |
| 209 | $new_source = new $handler['class']( $handler, $search_flags, $source_flags ); |
| 210 | $source_flags->set_allowed_flags( array_keys( $new_source->get_supported_flags() ) ); |
| 211 | |
| 212 | return $new_source; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return null; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Return a list of all source names only. This can be used for checking a name is allowed. |
| 221 | * |
| 222 | * @return Array Array of source names |
| 223 | */ |
| 224 | public static function get_all_source_names() { |
| 225 | $sources = self::get_all_sources(); |
| 226 | |
| 227 | return array_map( function( $source ) { |
| 228 | return $source['name']; |
| 229 | }, $sources ); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Get all the specified sources as source objects |
| 234 | * |
| 235 | * @param Array $sources Array of source names. |
| 236 | * @param Search_Flags $search_flags The search flags object. |
| 237 | * @param Source_Flags $source_flags The source flags object. |
| 238 | * @return Array The array of source objects |
| 239 | */ |
| 240 | public static function get( $sources, Search_Flags $search_flags, Source_Flags $source_flags ) { |
| 241 | $handlers = []; |
| 242 | $cpts = []; |
| 243 | |
| 244 | /** |
| 245 | * @psalm-suppress InvalidArgument |
| 246 | */ |
| 247 | $all_cpts = array_map( function( array $source ) { |
| 248 | return $source['name']; |
| 249 | }, self::get_all_custom_post_types() ); |
| 250 | |
| 251 | // Create handlers for everything else |
| 252 | foreach ( $sources as $source ) { |
| 253 | if ( in_array( $source, $all_cpts, true ) ) { |
| 254 | $cpts[] = $source; |
| 255 | } elseif ( $source === 'posts' ) { |
| 256 | $cpts = $all_cpts; |
| 257 | } else { |
| 258 | $handler = self::get_handler_for_source( $source, $search_flags, $source_flags ); |
| 259 | |
| 260 | if ( $handler ) { |
| 261 | $handlers[] = $handler; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // Merge all CPTs together |
| 267 | if ( count( $cpts ) > 0 ) { |
| 268 | $handler = self::get_handler_for_source( 'post', $search_flags, $source_flags ); |
| 269 | |
| 270 | if ( $handler instanceof Source_Post ) { |
| 271 | $handler->set_custom_post_types( $cpts ); |
| 272 | array_unshift( $handlers, $handler ); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return $handlers; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | require_once dirname( __DIR__ ) . '/source/core/meta.php'; |
| 281 | require_once dirname( __DIR__ ) . '/source/core/post.php'; |
| 282 | require_once dirname( __DIR__ ) . '/source/core/post-meta.php'; |
| 283 | require_once dirname( __DIR__ ) . '/source/core/user.php'; |
| 284 | require_once dirname( __DIR__ ) . '/source/core/user-meta.php'; |
| 285 | require_once dirname( __DIR__ ) . '/source/core/comment.php'; |
| 286 | require_once dirname( __DIR__ ) . '/source/core/comment-meta.php'; |
| 287 | require_once dirname( __DIR__ ) . '/source/core/options.php'; |
| 288 |