-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3499 from 10up/feature/facet-abstract-classes
Implement abstract Block facet feature class
- Loading branch information
Showing
13 changed files
with
309 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/** | ||
* Abstract Facet Block class. | ||
* | ||
* @since 4.7.0 | ||
* @package elasticpress | ||
*/ | ||
|
||
namespace ElasticPress\Feature\Facets; | ||
|
||
/** | ||
* Abstract Facet Block class. | ||
*/ | ||
abstract class Block { | ||
|
||
/** | ||
* Setup hooks and filters for facet block. | ||
*/ | ||
abstract public function setup(); | ||
|
||
/** | ||
* Register facet block. | ||
*/ | ||
abstract public function register_block(); | ||
|
||
/** | ||
* Setup REST endpoints for facet feature. | ||
*/ | ||
abstract public function setup_endpoints(); | ||
|
||
/** | ||
* Render the block. | ||
* | ||
* @param array $attributes Block attributes. | ||
* @return string | ||
*/ | ||
abstract public function render_block( $attributes ); | ||
|
||
/** | ||
* Outputs the block preview | ||
* | ||
* @param \WP_REST_Request $request REST request | ||
* @return string | ||
*/ | ||
abstract public function render_block_preview( $request ); | ||
|
||
/** | ||
* Check if the current user has permission to view the facets REST endpoint. | ||
* | ||
* @return true|\WP_Error | ||
*/ | ||
public function check_facets_rest_permission() { | ||
if ( ! current_user_can( 'edit_theme_options' ) ) { | ||
return new \WP_Error( 'ep_rest_forbidden', esc_html__( 'Sorry, you cannot view this resource.', 'elasticpress' ), array( 'status' => 401 ) ); | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/** | ||
* Abstract Facet Renderer class. | ||
* | ||
* @since 4.7.0 | ||
* @package elasticpress | ||
*/ | ||
|
||
namespace ElasticPress\Feature\Facets; | ||
|
||
/** | ||
* Abstract Facet Renderer class. | ||
*/ | ||
abstract class Renderer { | ||
/** | ||
* Whether the term count should be displayed or not. | ||
* | ||
* @var bool | ||
*/ | ||
protected $display_count; | ||
|
||
/** | ||
* Method to render the facet. | ||
* | ||
* @param array $args Widget args | ||
* @param array $instance Instance settings | ||
*/ | ||
abstract public function render( $args, $instance ); | ||
|
||
/** | ||
* Whether the facet should be rendered or not. | ||
* | ||
* @return bool | ||
*/ | ||
protected function should_render() : bool { | ||
return true; | ||
} | ||
|
||
/** | ||
* Given an array of values, reorder them. | ||
* | ||
* @param array $values Multidimensional array of values. Each value should have (string) `name`, (int) `count`, and (bool) `is_selected`. | ||
* @param string $orderby Key to be used to order. | ||
* @param string $order ASC or DESC. | ||
* @return array | ||
*/ | ||
protected function order_values( array $values, string $orderby = 'count', $order = 'desc' ) : array { | ||
$orderby = strtolower( $orderby ); | ||
$orderby = in_array( $orderby, [ 'name', 'count' ], true ) ? $orderby : 'count'; | ||
|
||
$order = strtoupper( $order ); | ||
$order = in_array( $order, [ 'ASC', 'DESC' ], true ) ? $order : 'DESC'; | ||
|
||
$values = wp_list_sort( $values, $orderby, $order, true ); | ||
|
||
$selected = []; | ||
foreach ( $values as $key => $value ) { | ||
if ( $value['is_selected'] ) { | ||
$selected[ $key ] = $value; | ||
unset( $values[ $key ] ); | ||
} | ||
} | ||
$values = $selected + $values; | ||
|
||
return $values; | ||
} | ||
|
||
/** | ||
* Get the markup for an individual facet item. | ||
* | ||
* @param array|object $item Facet item. | ||
* @param string $url URL for the facet item. | ||
* @return string|null | ||
*/ | ||
public function get_facet_item_value_html( $item, string $url ) { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.