Skip to content

Commit

Permalink
Merge pull request #6339 from easydigitaldownloads/issue/6286
Browse files Browse the repository at this point in the history
Issue/6286 – Table endpoint support
  • Loading branch information
DrewAPicture committed Jan 30, 2018
2 parents 6862c05 + da7d036 commit 833105d
Show file tree
Hide file tree
Showing 11 changed files with 627 additions and 105 deletions.
41 changes: 17 additions & 24 deletions includes/admin/reporting/reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ function edd_reports_page() {
.edd-item-has-tabs #edd-item-card-wrapper {
width: 85%;
}
#edd-item-card-wrapper h2 {
font-size: 1.5em;
text-align: center;
}
#edd-item-card-wrapper h3 {
margin-top: 0;
margin-top; 0;
margin-bottom: 0;
}
</style>
<div class="wrap">
<h2><?php _e( 'Easy Digital Downloads Reports', 'easy-digital-downloads' ); ?></h2>
<h1><?php _e( 'Easy Digital Downloads Reports', 'easy-digital-downloads' ); ?></h1>

<div id="edd-item-wrapper" class="edd-item-has-tabs edd-clearfix">
<div id="edd-item-tab-wrapper" class="report-tab-wrapper">
Expand Down Expand Up @@ -93,41 +98,29 @@ function edd_reports_page() {
</div>

<div id="edd-item-card-wrapper" class="edd-report-card-wrapper" style="float: left">
<?php do_action( 'edd_reports_tabs' ); ?>
<?php $report = Reports\get_report( $active_tab ); ?>

<div id="edd-reports-card-header">
<h2><?php echo esc_html( $report->get_label() ); ?></h2>
</div>

<?php
$report = Reports\get_report( $active_tab );
do_action( 'edd_reports_tabs' );

if ( ! is_wp_error( $report ) ) :

do_action( 'edd_reports_page_top' );

if ( $report->has_endpoints( 'tiles' ) ) : ?>

<div id="edd-reports-tiles-wrap">
<div id="dashboard-widgets" class="metabox-holder">

<div class="postbox-container">
<?php do_meta_boxes( 'download_page_edd-reports', 'primary', null ); ?>
</div>

<div class="postbox-container">
<?php do_meta_boxes( 'download_page_edd-reports', 'secondary', null ); ?>
</div>
$report->display_endpoint_group( 'tiles' );

<div class="postbox-container">
<?php do_meta_boxes( 'download_page_edd-reports', 'tertiary', null ); ?>
</div>
$report->display_endpoint_group( 'tables' );

</div>
</div>
<?php endif; // Has endpoints.
endif; // WP_Error.

if ( has_action( "edd_reports_tab_{$active_tab}" ) ) {
do_action( "edd_reports_tab_{$active_tab}" );
do_action( "edd_reports_tab_{$active_tab}", $report );
} elseif ( has_action( "edd_reports_view_{$active_tab}" ) ) {
do_action( "edd_reports_view_{$active_tab}" );
do_action( "edd_reports_view_{$active_tab}", $report );
}

do_action( 'edd_reports_page_bottom' );
Expand Down
1 change: 1 addition & 0 deletions includes/reports/class-init.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private function includes() {
// Endpoints.
require_once $reports_dir . '/data/class-endpoint.php';
require_once $reports_dir . '/data/class-tile-endpoint.php';
require_once $reports_dir . '/data/class-table-endpoint.php';
require_once $reports_dir . '/data/class-endpoint-registry.php';
}

Expand Down
4 changes: 3 additions & 1 deletion includes/reports/data/class-base-object.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ abstract class Base_Object {
* @param array $args Arguments for instantiating the object.
*/
public function __construct( $args ) {
$this->errors = new \WP_Error();
if ( ! isset( $this->errors ) ) {
$this->errors = new \WP_Error();
}

$this->set_props( $args );
}
Expand Down
2 changes: 1 addition & 1 deletion includes/reports/data/class-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function get_view() {
*
* @param string $view_type Endpoint type.
*/
private function check_view() {
protected function check_view() {
$views = Reports\get_endpoint_views();

if ( ! array_key_exists( $this->get_view(), $views ) ) {
Expand Down
51 changes: 51 additions & 0 deletions includes/reports/data/class-report.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,37 @@ public function has_endpoints( $view_group ) {
return ! empty( $endpoints );
}

/**
* Retrieves a given endpoint by view group.
*
* @since 3.0
*
* @param string $endpoint_id Endpoint ID.
* @param string $view_group Endpoint view group.
* @return Endpoint|\WP_Error Endpoint object if it exists, otherwise a WP_Error object.
*/
public function get_endpoint( $endpoint_id, $view_group ) {
$endpoints = $this->get_endpoints( $view_group );

if ( isset( $endpoints[ $endpoint_id ] ) ) {

$endpoint = $endpoints[ $endpoint_id ];

} else {

$message = sprintf( 'The \'%1$s\' endpoint does not exist for the \'%2$s\' view group in the \'%3$s\' report.',
$endpoint_id,
$view_group,
$this->get_id()
);

$endpoint = new \WP_Error( 'invalid_report_endpoint', $message );

}

return $endpoint;
}

/**
* Retrieves the capability needed to view the rendered report.
*
Expand All @@ -271,4 +302,24 @@ private function set_capability( $capability ) {
$this->capability = sanitize_key( $capability );
}

/**
* Displays an entire group of an endpoints view.
*
* @since 3.0
*
* @param string $view_group Endpoints view group.
* @return void
*/
public function display_endpoint_group( $view_group ) {
$groups = $this->parse_view_groups();

if ( array_key_exists( $view_group, $groups ) ) {
$callback = Reports\get_endpoint_group_callback( $groups[ $view_group ] );

if ( is_callable( $callback ) ) {
call_user_func( $callback, $this );
}
}
}

}
230 changes: 230 additions & 0 deletions includes/reports/data/class-table-endpoint.php
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(),
) );
}
}
}

}
Loading

0 comments on commit 833105d

Please sign in to comment.