Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Basic VIP Enterprise Search Adapter #8

Merged
merged 13 commits into from
Apr 28, 2022
Prev Previous commit
Next Next commit
Add autoloader and basic adapter files
  • Loading branch information
kevinfodness committed Mar 29, 2022
commit 0ac8e3da2de5396d46224f7b29b59586030b0093
97 changes: 53 additions & 44 deletions .phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,46 +1,55 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance" name="Elasticsearch Extensions" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<description>PHP_CodeSniffer standard for Elasticsearch Extensions.</description>

<!-- Include Alley Rules -->
<rule ref="Alley-Interactive" />

<!-- Set the text domain for i18n. -->
<rule ref="WordPress.WP.I18n">
<properties>
<property name="text_domain" type="array" value="elasticsearch-extensions" />
</properties>
</rule>

<!-- Set the prefixes for functions etc. -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<property name="prefixes" type="array" value="elasticsearch_extensions" />
</properties>
</rule>

<!--
Pass some flags to PHPCS:
p flag: Show progress of the run.
s flag: Show sniff codes in all reports.
-->
<arg value="ps" />

<!-- Whenever possible, cache the scan results and re-use those for unchanged files on the next scan. -->
<arg name="cache" value=".phpcs.json" />

<!-- Strip the filepaths down to the relevant bit. -->
<arg name="basepath" value="./" />

<!-- Check up to 20 files simultaneously. -->
<arg name="parallel" value="20" />

<!-- Set severity to 1 to see everything that isn't effectively turned off. -->
<arg name="severity" value="1" />

<!-- Exclude a few directories and autogenerated files. -->
<exclude-pattern>vendor/</exclude-pattern>

<!-- The version set here matches the minimum version tested in buddy.yml. -->
<config name="minimum_supported_wp_version" value="5.9" />
<ruleset xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance"
name="Elasticsearch Extensions"
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<description>PHP_CodeSniffer standard for Elasticsearch Extensions.
</description>

<!-- Include Alley Rules -->
<rule ref="Alley-Interactive"/>

<!-- Set the text domain for i18n. -->
<rule ref="WordPress.WP.I18n">
<properties>
<property name="text_domain" type="array"
value="elasticsearch-extensions"/>
</properties>
</rule>

<!-- Set the prefixes for functions etc. -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<property name="prefixes" type="array" value="elasticsearch_extensions"/>
</properties>
</rule>

<!--
Pass some flags to PHPCS:
p flag: Show progress of the run.
s flag: Show sniff codes in all reports.
-->
<arg value="ps"/>

<!-- Whenever possible, cache the scan results and re-use those for unchanged files on the next scan. -->
<arg name="cache" value=".phpcs.json"/>

<!-- Strip the filepaths down to the relevant bit. -->
<arg name="basepath" value="./"/>

<!-- Check up to 20 files simultaneously. -->
<arg name="parallel" value="20"/>

<!-- Set severity to 1 to see everything that isn't effectively turned off. -->
<arg name="severity" value="1"/>

<!-- Exclude a few directories and autogenerated files. -->
<exclude-pattern>vendor/</exclude-pattern>

<!-- The adapter pattern relies on several files exporting the same class name. -->
<rule ref="WordPress.Files.FileName.InvalidClassFileName">
<exclude-pattern>adapters/</exclude-pattern>
</rule>

<!-- The version set here matches the minimum version tested in buddy.yml. -->
<config name="minimum_supported_wp_version" value="5.9"/>
</ruleset>
54 changes: 54 additions & 0 deletions adapters/class-adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Elasticsearch Extensions Adapters: Adapter Abstract Class
*
* @package Elasticsearch_Extensions
*/

namespace Elasticsearch_Extensions\Adapters;

/**
* An abstract class that establishes base functionality and sets requirements
* for implementing classes.
*
* @package Elasticsearch_Extensions
*/
abstract class Adapter {

/**
* Stores aggregation data from the Elasticsearch response.
*
* @var array
*/
private static array $aggregations = [];

/**
* Enables an aggregation based on post type.
*/
abstract public static function enable_post_type_aggregation(): void;

/**
* A function to enable an aggregation for a specific taxonomy.
*
* @param string $taxonomy The taxonomy slug for which to enable an aggregation.
*/
abstract public static function enable_taxonomy_aggregation( string $taxonomy ): void;

/**
* Gets aggregation results.
*
* @return array The aggregation results.
*/
public static function get_aggregations(): array {
return self::$aggregations;
}

/**
* Sets aggregation results.
*
* @param array $aggregations An array of aggregation results to be stored.
*/
protected static function set_aggregations( array $aggregations ): void {
self::$aggregations = $aggregations;
}
}
32 changes: 32 additions & 0 deletions adapters/class-vip-enterprise-search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Elasticsearch Extensions Adapters: VIP Enterprise Search Adapter
*
* @package Elasticsearch_Extensions
*/

use Elasticsearch_Extensions\Adapters\Adapter;

/**
* An adapter for WordPress VIP Enterprise Search.
*
* @package Elasticsearch_Extensions
*/
class Elasticsearch_Extensions extends Adapter {

/**
* Enables an aggregation based on post type.
*/
public static function enable_post_type_aggregation(): void {
// TODO.
}

/**
* A function to enable an aggregation for a specific taxonomy.
*
* @param string $taxonomy The taxonomy slug for which to enable an aggregation.
*/
public static function enable_taxonomy_aggregation( string $taxonomy ): void {
// TODO.
}
}
7 changes: 7 additions & 0 deletions elasticsearch-extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@
*/

namespace Elasticsearch_Extensions;

require_once __DIR__ . '/lib/autoload.php';

// Load adapter automatically based on environment settings.
if ( defined( 'VIP_ENABLE_VIP_SEARCH' ) && VIP_ENABLE_VIP_SEARCH ) {
require_once __DIR__ . '/adapters/class-vip-enterprise-search.php';
}
29 changes: 29 additions & 0 deletions lib/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Elasticsearch Extensions Library: Autoloader.
*
* @package Elasticsearch_Extensions
*/

namespace Elasticsearch_Extensions;

/**
* Autoload classes.
*
* @param string $class Class name.
*/
function autoload( string $class ) {
// Only autoload classes for this namespace.
$class = ltrim( $class, '\\' );
if ( strpos( $class, __NAMESPACE__ . '\\' ) !== 0 ) {
return;
}

$class = strtolower( str_replace( [ __NAMESPACE__ . '\\', '_' ], [ '', '-' ], $class ) );
$dirs = explode( '\\', $class );
$class = array_pop( $dirs );

require_once dirname( __DIR__ ) . '/' . implode( '/', $dirs ) . '/class-' . $class . '.php';
}

spl_autoload_register( '\Elasticsearch_Extensions\autoload' );