| 1 |
<?php |
| 2 |
/** |
| 3 |
* A helper class designed to output the content of a table. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Table_Helper |
| 6 |
* @version 1.5.0 |
| 7 |
* @author David Bisset |
| 8 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Table_Helper' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Table_Helper |
| 21 |
* |
| 22 |
* @since 1.5.0 |
| 23 |
*/ |
| 24 |
class Charitable_Table_Helper { |
| 25 |
|
| 26 |
/** |
| 27 |
* The table columns. |
| 28 |
* |
| 29 |
* @since 1.5.0 |
| 30 |
* |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
public $columns; |
| 34 |
|
| 35 |
/** |
| 36 |
* The table data. |
| 37 |
* |
| 38 |
* @since 1.5.0 |
| 39 |
* |
| 40 |
* @var array |
| 41 |
*/ |
| 42 |
public $data; |
| 43 |
|
| 44 |
/** |
| 45 |
* Other table args. |
| 46 |
* |
| 47 |
* @since 1.5.0 |
| 48 |
* |
| 49 |
* @var array |
| 50 |
*/ |
| 51 |
public $args; |
| 52 |
|
| 53 |
/** |
| 54 |
* Table attribute tags. |
| 55 |
* |
| 56 |
* @since 1.5.0 |
| 57 |
* |
| 58 |
* @var array |
| 59 |
*/ |
| 60 |
public $table_attributes = array( 'class', 'id' ); |
| 61 |
|
| 62 |
/** |
| 63 |
* The current row. |
| 64 |
* |
| 65 |
* @since 1.5.0 |
| 66 |
* |
| 67 |
* @var array |
| 68 |
*/ |
| 69 |
public $current_row; |
| 70 |
|
| 71 |
/** |
| 72 |
* The current row number. |
| 73 |
* |
| 74 |
* @since 1.5.0 |
| 75 |
* |
| 76 |
* @var int |
| 77 |
*/ |
| 78 |
public $current_row_idx; |
| 79 |
|
| 80 |
/** |
| 81 |
* Create class object. |
| 82 |
* |
| 83 |
* @since 1.5.0 |
| 84 |
* |
| 85 |
* @param array $columns A set of columns. |
| 86 |
* @param array $data Data. |
| 87 |
* @param array $args Optional set of extra arguments. |
| 88 |
*/ |
| 89 |
public function __construct( array $columns, array $data, $args = array() ) { |
| 90 |
$this->columns = $columns; |
| 91 |
$this->data = $data; |
| 92 |
$this->args = $args; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Display the table. |
| 97 |
* |
| 98 |
* @since 1.5.0 |
| 99 |
* |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
public function render() { |
| 103 |
if ( empty( $this->columns ) ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
charitable_template( 'tables/table.php', array( 'helper' => $this ) ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Render a table row. |
| 112 |
* |
| 113 |
* @since 1.5.0 |
| 114 |
* |
| 115 |
* @param array $rowdata The data to be displayed on this row. |
| 116 |
* @return void |
| 117 |
*/ |
| 118 |
public function render_row( $row_data ) { |
| 119 |
charitable_template( 'tables/row.php', array( 'data' => $row_data, 'columns' => $this->columns ) ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Return a string containing the table attributes. |
| 124 |
* |
| 125 |
* @since 1.5.0 |
| 126 |
* |
| 127 |
* @return string |
| 128 |
*/ |
| 129 |
public function table_attributes() { |
| 130 |
$attrs = ''; |
| 131 |
|
| 132 |
foreach ( $this->table_attributes as $attr ) { |
| 133 |
if ( ! array_key_exists( $attr, $this->args ) ) { |
| 134 |
continue; |
| 135 |
} |
| 136 |
|
| 137 |
$attrs .= sprintf( ' %s="%s"', $attr, esc_attr( $this->args[ $attr ] ) ); |
| 138 |
} |
| 139 |
|
| 140 |
return ltrim( $attrs ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
endif; |
| 145 |
|