| 1 |
<?php |
| 2 |
/** |
| 3 |
* Terms feature |
| 4 |
* |
| 5 |
* @since 3.1 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\Feature\Terms; |
| 10 |
|
| 11 |
use ElasticPress\Feature; |
| 12 |
use ElasticPress\FeatureRequirementsStatus; |
| 13 |
use ElasticPress\Indexable; |
| 14 |
use ElasticPress\Indexables; |
| 15 |
|
| 16 |
/** |
| 17 |
* Terms feature class |
| 18 |
*/ |
| 19 |
class Terms extends Feature { |
| 20 |
/** |
| 21 |
* Whether the feature should be always visible in the dashboard |
| 22 |
* |
| 23 |
* @since 5.0.0 |
| 24 |
* @var boolean |
| 25 |
*/ |
| 26 |
protected $is_visible = false; |
| 27 |
|
| 28 |
/** |
| 29 |
* Initialize feature, setting it's config |
| 30 |
* |
| 31 |
* @since 3.1 |
| 32 |
*/ |
| 33 |
public function __construct() { |
| 34 |
$this->slug = 'terms'; |
| 35 |
|
| 36 |
$this->requires_install_reindex = true; |
| 37 |
|
| 38 |
Indexables::factory()->register( new Indexable\Term\Term(), false ); |
| 39 |
|
| 40 |
parent::__construct(); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Sets i18n strings. |
| 45 |
* |
| 46 |
* @return void |
| 47 |
* @since 5.2.0 |
| 48 |
*/ |
| 49 |
public function set_i18n_strings(): void { |
| 50 |
$this->title = esc_html__( 'Terms', 'elasticpress' ); |
| 51 |
|
| 52 |
$this->summary = '<p>' . __( 'This feature will empower your website to overcome traditional WordPress term search and query limitations that can present themselves at scale. This feature is only needed if you are using <code>WP_Term_Query</code> directly.', 'elasticpress' ) . '</p>'; |
| 53 |
|
| 54 |
$this->docs_url = __( 'https://www.elasticpress.io/resources/articles/configuring-elasticpress-via-the-plugin-dashboard/#terms', 'elasticpress' ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Setup search functionality |
| 59 |
* |
| 60 |
* @since 3.1 |
| 61 |
*/ |
| 62 |
public function setup() { |
| 63 |
Indexables::factory()->activate( 'term' ); |
| 64 |
|
| 65 |
add_action( 'init', [ $this, 'search_setup' ] ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Setup search integration |
| 70 |
* |
| 71 |
* @since 3.1 |
| 72 |
*/ |
| 73 |
public function search_setup() { |
| 74 |
add_filter( 'ep_elasticpress_enabled', [ $this, 'integrate_search_queries' ], 10, 2 ); |
| 75 |
add_filter( 'ep_term_fuzziness_arg', [ $this, 'set_admin_terms_search_fuzziness' ] ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Enable integration on search queries |
| 80 |
* |
| 81 |
* @param bool $enabled Whether EP is enabled |
| 82 |
* @param \WP_Term_Query $query Current query object. |
| 83 |
* @since 3.1 |
| 84 |
* @return bool |
| 85 |
*/ |
| 86 |
public function integrate_search_queries( $enabled, $query ) { |
| 87 |
if ( ! is_a( $query, 'WP_Term_Query' ) ) { |
| 88 |
return $enabled; |
| 89 |
} |
| 90 |
|
| 91 |
if ( isset( $query->query_vars['ep_integrate'] ) && ! filter_var( $query->query_vars['ep_integrate'], FILTER_VALIDATE_BOOLEAN ) ) { |
| 92 |
$enabled = false; |
| 93 |
} elseif ( ! empty( $query->query_vars['search'] ) ) { |
| 94 |
$enabled = true; |
| 95 |
} |
| 96 |
|
| 97 |
return $enabled; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Determine feature reqs status |
| 102 |
* |
| 103 |
* @since 3.1 |
| 104 |
* @return FeatureRequirementsStatus |
| 105 |
*/ |
| 106 |
public function requirements_status() { |
| 107 |
$status = new FeatureRequirementsStatus( 1, null, $this ); |
| 108 |
|
| 109 |
return $status; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Change fuzziness level for terms search in admin |
| 114 |
* |
| 115 |
* @param {int} $fuzziness Amount of fuzziness to factor into search |
| 116 |
* @since 3.6.4 |
| 117 |
* @return int |
| 118 |
*/ |
| 119 |
public function set_admin_terms_search_fuzziness( $fuzziness ) { |
| 120 |
if ( is_admin() ) { |
| 121 |
$fuzziness = 0; |
| 122 |
} |
| 123 |
return $fuzziness; |
| 124 |
} |
| 125 |
} |
| 126 |
|