-
Notifications
You must be signed in to change notification settings - Fork 475
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 #6339 from easydigitaldownloads/issue/6286
Issue/6286 – Table endpoint support
- Loading branch information
Showing
11 changed files
with
627 additions
and
105 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
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
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
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,230 @@ | ||
<?php | ||
/** | ||
* Reports API - Table Endpoint Handler | ||
* | ||
* @package EDD | ||
* @subpackage Reports | ||
* @copyright Copyright (c) 2018, Pippin Williamson | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License | ||
* @since 3.0 | ||
*/ | ||
namespace EDD\Reports\Data; | ||
|
||
/** | ||
* Handler for building a table endpoint in the Reports API. | ||
* | ||
* @since 3.0 | ||
*/ | ||
final class Table_Endpoint extends Endpoint { | ||
|
||
/** | ||
* Endpoint view (type). | ||
* | ||
* @since 3.0 | ||
* @var string | ||
*/ | ||
protected $view = 'table'; | ||
|
||
/** | ||
* List table instance. | ||
* | ||
* @since 3.0 | ||
* @var \WP_List_Table | ||
*/ | ||
private $list_table; | ||
|
||
/** | ||
* Represents the full path to the list table class file. | ||
* | ||
* @since 3.0 | ||
* @var string | ||
*/ | ||
private $class_file; | ||
|
||
/** | ||
* Sets up the table endpoint. | ||
* | ||
* @since 3.0 | ||
* | ||
* @param array $args Table endpoint attributes. | ||
*/ | ||
public function __construct( array $args ) { | ||
$this->errors = new \WP_Error(); | ||
|
||
// ID and Label. | ||
$this->set_props( $args ); | ||
|
||
// List table set up and dumping display args. | ||
$this->setup_list_table( $args ); | ||
|
||
// Parse display attributes from defaults. | ||
$args = $this->parse_display_props( $args ); | ||
|
||
parent::__construct( $args ); | ||
} | ||
|
||
/** | ||
* Sets display-related properties for the Endpoint. | ||
* | ||
* @since 3.0 | ||
* | ||
* @param array $endpoint Endpoint record from the registry. | ||
*/ | ||
private function parse_display_props( $endpoint ) { | ||
|
||
$view_type = $this->get_view(); | ||
|
||
if ( ! empty( $endpoint['views'][ $this->view ] ) ) { | ||
|
||
$view_atts = $endpoint['views'][ $this->view ]; | ||
|
||
$list_table = $this->get_list_table(); | ||
|
||
if ( null === $list_table ) { | ||
$endpoint; | ||
} | ||
|
||
// Inject the default list table display callback if 'display'. | ||
if ( ! empty( $view_atts['display_callback'] ) && 'display' === $view_atts['display_callback'] ) { | ||
$view_atts['display_callback'] = array( $list_table, 'display' ); | ||
} | ||
|
||
// Inject the default list table data callback if 'prepare_items'. | ||
if ( ! empty( $view_atts['data_callback'] ) && 'prepare_items' === $view_atts['data_callback'] ) { | ||
$view_atts['data_callback'] = array( $list_table, 'prepare_items' ); | ||
} | ||
|
||
$endpoint['views'][ $view_type ] = $view_atts; | ||
} | ||
|
||
return $endpoint; | ||
} | ||
|
||
/** | ||
* Sets attributes related to the list table. | ||
* | ||
* @since 3.0 | ||
* | ||
* @param array $endpoint Table endpoint arguments. | ||
*/ | ||
private function setup_list_table( $endpoint ) { | ||
|
||
if ( ! empty( $endpoint['views'][ $this->view ]['display_args'] ) ) { | ||
|
||
$display_args = $endpoint['views'][ $this->view ]['display_args']; | ||
|
||
if ( ! empty( $display_args['class_name'] ) ) { | ||
|
||
if ( ! empty( $display_args['class_file'] ) ) { | ||
|
||
$this->set_class_file( $display_args['class_file'] ); | ||
|
||
$this->set_list_table( $display_args['class_name'] ); | ||
|
||
} else { | ||
|
||
$this->errors->add( | ||
'missing_table_class_file', | ||
sprintf( 'The list table class file for the \'%1$s\' endpoint is missing.', $this->get_id() ) | ||
); | ||
|
||
} | ||
|
||
} else { | ||
|
||
$this->errors->add( | ||
'missing_table_class_name', | ||
sprintf( 'The list table class name for the \'%1$s\' endpoint is missing.', | ||
$this->get_id() | ||
) | ||
); | ||
|
||
} | ||
|
||
// Dump the display args as they're no longer needed. | ||
$endpoint['views'][ $this->view ]['display_args'] = array(); | ||
|
||
} | ||
|
||
} | ||
|
||
/** | ||
* Retrieves the list table class file. | ||
* | ||
* @since 3.0 | ||
* | ||
* @return string|null Class file if set, otherwise null. | ||
*/ | ||
public function get_class_file() { | ||
return $this->class_file; | ||
} | ||
|
||
/** | ||
* Sets the list table class file. | ||
* | ||
* @since 3.0 | ||
* | ||
* @param string $file Class file. | ||
*/ | ||
private function set_class_file( $file ) { | ||
if ( 0 === validate_file( $file ) ) { | ||
$this->class_file = $file; | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves the list table instance. | ||
* | ||
* @since 3.0 | ||
* | ||
* @return \WP_List_Table|null List table instance if set, otherwise null. | ||
*/ | ||
public function get_list_table() { | ||
return $this->list_table; | ||
} | ||
|
||
/** | ||
* Sets the list table instance. | ||
* | ||
* @since 3.0 | ||
* | ||
* @see get_class_file() | ||
* | ||
* @param string $class List table class name. | ||
*/ | ||
private function set_list_table( $class ) { | ||
if ( ! class_exists( $class ) ) { | ||
$path_to_file = $this->get_class_file(); | ||
|
||
if ( file_exists( $path_to_file ) ) { | ||
require_once $path_to_file; | ||
} | ||
} | ||
$this->list_table = new $class; | ||
} | ||
|
||
/** | ||
* Display logic for the current table endpoint. | ||
* | ||
* @since 3.0 | ||
*/ | ||
public function display() { | ||
$callback = $this->get_display_callback(); | ||
|
||
if ( is_callable( $callback ) ) { | ||
$table = $this->get_list_table(); | ||
|
||
if ( null !== $table ) { | ||
// Prep the table data for display (prepare_items). | ||
$this->get_data(); | ||
|
||
call_user_func_array( $callback, array( | ||
'endpoint' => $this, | ||
'table' => $table, | ||
'args' => $this->get_display_args(), | ||
) ); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.