sitecrafting / conifer

:evergreen_tree: A powerful WordPress library plugin for OO development
https://www.coniferplug.in
MIT License
18 stars 2 forks source link

Metafield search API improvements #4

Closed acobster closed 6 years ago

acobster commented 6 years ago

There are a number of issues with the current implementation of meta-field search.

Generalize and rename AcfField

WPB-69 introduced the ability to hook into WordPress's search functionality to query meta fields and taxonomies from within Groot. Its current implementation, the Conifer\AcfSearch class (formerly Project\AcfSearch), limits the use-case to configuring this behavior for a single configuration across all post types.

Making AcfSearch a trait and allowing the calling code to specify which fields to search for which post types would address this and would improve performance.

We should also rename it to SupportsMetaSearch or something, since it searches meta fields regardless of whether they are declared by ACF.

Possible usage might look like:

use Conifer\Post\Post;
use Conifer\Post\SupportsMetaSearch;

class MyPost extends Post {
  use SupportsMetaSearch;

  // method to get called on site init
  public static function config() {
    include_meta_fields_in_search([
      'my_field',
      'my_other_field',
      ['meta_key' => 'wildcard_field_%', 'meta_compare' => 'LIKE']
    ]);
  }
}

Make taxonomy searches optional/configurable

Currently AcfSearch searches taxonomy field values as well as ACF meta fields. This can result in expensive sub-queries that may not be necessary. Revise the structure to make this more configurable so that we can specify via advanced_custom_search (which should probably also be named more descriptively) or an intermediate add_filter callback whether to search taxonomies or not.

Syntax cleanup

While we're at it, we should also clean up uses of the local $table_prefix variable in buildWhereClause(), e.g in SELECT * FROM ".$table_prefix."comments. We don't need to do this concatenation explicitly, as we can get the prefixed table name directly from $wpdb. For example, $wpdb->posts is equivalent to $wpdb->prefix . "posts". More info here.

Convert method names to snake_case for consistency with Timber/Conifer codebase.

Let's also stop YELLING IN THE COMMENTS. RELAX, PEOPLE.

acobster commented 6 years ago

Implemented, documented, and tested. We'll add more features soon.