models
4 years ago
queries
4 years ago
utils
4 years ago
chart.php
4 years ago
db.php
4 years ago
filters-ajax.php
4 years ago
filters.php
4 years ago
rest-api.php
4 years ago
settings-ajax.php
4 years ago
settings.php
4 years ago
super-secret-content-generator.php
4 years ago
table-referrers.php
4 years ago
table-views.php
4 years ago
table.php
4 years ago
track-resource-changes.php
4 years ago
table.php
207 lines
| 1 | <?php |
| 2 | if (!class_exists('IAWP_Table')) { |
| 3 | abstract class IAWP_Table |
| 4 | { |
| 5 | abstract protected function table_name(); |
| 6 | |
| 7 | private $filters; |
| 8 | |
| 9 | public function __construct() |
| 10 | { |
| 11 | add_action('admin_init', [$this, 'register_settings']); |
| 12 | $this->filters = new IAWP_Filters(); |
| 13 | } |
| 14 | |
| 15 | public function filters() |
| 16 | { |
| 17 | return $this->filters; |
| 18 | } |
| 19 | |
| 20 | public function output_data_table(array $dates = null) |
| 21 | { |
| 22 | if ($dates == null) { |
| 23 | $dates = IAWP()->default_date_range(); |
| 24 | } |
| 25 | |
| 26 | $rows = $this->rows($dates); |
| 27 | $columns = $this->default_columns(); |
| 28 | |
| 29 | // Todo - Add a way for the UI to figure out what type of data it's working with... |
| 30 | $html = "<div id='data-table' class='data-table' data-table-name='" . esc_attr($this->table_name()) . "' |
| 31 | data-columns='" . IAWP_Security::json_encode($columns) . "' data-column-count='" . count($columns) . "'>"; |
| 32 | $html .= '<div id="columns" class="columns">'; |
| 33 | $html .= '<div class="row">'; |
| 34 | $html .= $this->cells('heading', $columns, null, count($rows)); |
| 35 | $html .= '</div>'; |
| 36 | $html .= '</div>'; |
| 37 | $html .= $this->data_table_rows($rows, $columns); |
| 38 | $html .= '</div>'; |
| 39 | |
| 40 | return $html; |
| 41 | } |
| 42 | |
| 43 | public function data_table_rows(array $rows, array $columns) |
| 44 | { |
| 45 | $html = '<div id="rows" class="rows">'; |
| 46 | if (count($rows) == 0) { |
| 47 | $html .= '<p id="data-error" class="data-error">' . esc_html__('No ' . $this->display_name() . ' found.', 'iawp') . '</p>'; |
| 48 | } else { |
| 49 | $count = 1; |
| 50 | foreach ($rows as $row) { |
| 51 | $classes = 'row'; |
| 52 | if ($this->table_name() == 'views') { |
| 53 | $classes = $row->is_deleted() ? 'row deleted' : 'row'; |
| 54 | } |
| 55 | $html .= '<div class="' . esc_attr($classes) . '" ' . $this->row_data_attrs($row) . '>'; |
| 56 | $html .= $this->cells('rows', $columns, $row, $count); |
| 57 | $html .= '</div>'; |
| 58 | $count++; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | $html .= '</div>'; |
| 63 | |
| 64 | return $html; |
| 65 | } |
| 66 | |
| 67 | public function cells($context, $columns, $data = null, $count = null) |
| 68 | { |
| 69 | $html = ''; |
| 70 | foreach ($this->columns() as $key => $value) { |
| 71 | $classes = 'cell'; |
| 72 | $classes .= !in_array($key, $columns) ? ' hide' : ''; |
| 73 | $html .= '<div class="' . esc_attr($classes) . '" data-column="' . esc_attr($key) . '">'; |
| 74 | if ($context == 'heading') { |
| 75 | $html .= $this->heading_cell_content($key, $value, $count); |
| 76 | } else { |
| 77 | $html .= $this->row_cell_content($key, $data, $count); |
| 78 | } |
| 79 | $html .= '</div>'; |
| 80 | } |
| 81 | |
| 82 | return $html; |
| 83 | } |
| 84 | |
| 85 | public function heading_cell_content($key, $value, $count) |
| 86 | { |
| 87 | $classes = $key == 'views' ? 'sort-button desc' : 'sort-button'; |
| 88 | $html = '<button class="' . esc_attr($classes) . '" data-sort="' . esc_attr($key) . '">'; |
| 89 | $html .= '<div class="row-number">' . absint($count) . '</div>'; |
| 90 | $html .= '<span class="name">' . esc_html($value) . '</span>'; |
| 91 | $html .= '<span class="dashicons dashicons-arrow-right"></span><span class="dashicons dashicons-arrow-up"></span><span class="dashicons dashicons-arrow-down"></span>'; |
| 92 | $html .= '<div class="animator"></div>'; |
| 93 | $html .= '</button>'; |
| 94 | |
| 95 | return $html; |
| 96 | } |
| 97 | |
| 98 | public function row_cell_content($key, $data, $count) |
| 99 | { |
| 100 | return '<div class="row-number">' . absint($count) . '</div><span class="cell-content">' . $this->get_cell_content($data, $key) . '</span><div class="animator"></div>'; |
| 101 | } |
| 102 | |
| 103 | public function row_data_attrs($data) |
| 104 | { |
| 105 | $html = ''; |
| 106 | |
| 107 | foreach ($this->columns() as $key => $value) { |
| 108 | $data_val = $data->$key(); |
| 109 | // Need to make an exception for labels so that sorting matches what people see e.g. attachment -> Media |
| 110 | if ($key == 'type') { |
| 111 | $data_val = $data->type_label(); |
| 112 | } |
| 113 | |
| 114 | $html .= ' data-' . esc_attr($key) . '="' . esc_attr($data_val) . '"'; |
| 115 | } |
| 116 | |
| 117 | return $html; |
| 118 | } |
| 119 | |
| 120 | public function get_cell_content($data, string $column) |
| 121 | { |
| 122 | if ($data->$column() == null) { |
| 123 | return null; |
| 124 | } |
| 125 | if ($column == 'title' && $data->is_deleted()) { |
| 126 | return IAWP_Security::string($data->$column()) . ' <span class="deleted-label">' . esc_html__('(Deleted)', 'iawp') . '</span>'; |
| 127 | } elseif ($column == 'views') { |
| 128 | return number_format($data->views(), 0); |
| 129 | } elseif ($column == 'url') { |
| 130 | if ($data->is_deleted()) { |
| 131 | return esc_url($data->url()); |
| 132 | } else { |
| 133 | return '<a href="' . esc_url($data->url()) . '" target="_blank" class="external-link">' . esc_url($data->url()) . '<span class="dashicons dashicons-external"></span></a>'; |
| 134 | } |
| 135 | } elseif ($column == 'author') { |
| 136 | return IAWP_Security::html($data->avatar()) . ' ' . IAWP_Security::string($data->author()); |
| 137 | } elseif ($column == 'date') { |
| 138 | return IAWP_Security::string(date(get_option('date_format'), $data->date())); |
| 139 | } elseif ($column == 'type' && method_exists($data, 'icon')) { |
| 140 | return IAWP_Security::html($data->icon(0)) . ' ' . IAWP_Security::string($data->type_label()); |
| 141 | } elseif ($column == 'referrer' && !$data->is_direct()) { |
| 142 | return '<a href="' . esc_url($data->referrer()) . '" target="_blank" class="external-link">' . IAWP_Security::string($data->referrer_name()) . '<span class="dashicons dashicons-external"></span></a>'; |
| 143 | } else { |
| 144 | return $data->$column(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | public function output_toolbar(array $columns, $quick_stats = '') |
| 149 | { |
| 150 | list($start, $end) = IAWP()->default_date_range(); ?> |
| 151 | <div class="toolbar"> |
| 152 | <div class="buttons"> |
| 153 | <div class="date-picker-container"> |
| 154 | <button id="date-picker" class="date-picker iawp-button white" type="text" |
| 155 | data-format="<?php echo esc_attr(IAWP_Date_Format::js()); ?>" |
| 156 | data-start="<?php echo esc_attr($start->format('Y-m-d')); ?>" |
| 157 | data-end="<?php echo esc_attr($end->format('Y-m-d')); ?>" |
| 158 | data-css="<?php echo esc_url(IAWP_URL) . 'dist/styles/easepick/datepicker.css' ?>" |
| 159 | data-dow="<?php echo absint(IAWP()->get_option('iawp_dow', 1)); ?>"> |
| 160 | <?php echo IAWP_Security::string($start->format(IAWP_Date_Format::php())); ?> - <?php echo IAWP_Security::string($end->format(IAWP_Date_Format::php())); ?> |
| 161 | </button> |
| 162 | <span class="dashicons dashicons-calendar-alt"></span> |
| 163 | </div> |
| 164 | <div class="customize-filters modal-parent"> |
| 165 | <button id="customize-filters" class="iawp-button ghost-white"> |
| 166 | <span class="dashicons dashicons-filter"></span> <?php esc_html_e('Filter Rows', 'iawp'); ?> <span class="count"></span> |
| 167 | </button> |
| 168 | <?php IAWP_Security::html($this->filters()->output_filters($columns)); ?> |
| 169 | </div> |
| 170 | <div class="customize-columns modal-parent small"> |
| 171 | <button id="customize-columns" class="iawp-button ghost-white"><span class="dashicons dashicons-columns"></span> <?php esc_html_e('Edit Columns', 'iawp'); ?></button> |
| 172 | <?php IAWP_Security::html($this->column_picker()); ?> |
| 173 | </div> |
| 174 | </div> |
| 175 | <?php echo $quick_stats; ?> |
| 176 | </div><?php |
| 177 | } |
| 178 | |
| 179 | public function column_picker() { ?> |
| 180 | <div id="modal-columns" class="modal small"> |
| 181 | <div class="modal-inner"> |
| 182 | <div class="title-small"><?php esc_html_e('Choose your columns', 'iawp'); ?> |
| 183 | </div> |
| 184 | <?php IAWP_Security::html($this->columns_callback()); ?> |
| 185 | </div> |
| 186 | </div> |
| 187 | <?php |
| 188 | } |
| 189 | |
| 190 | public function columns_callback() |
| 191 | { |
| 192 | $defaults = $this->default_columns(); |
| 193 | |
| 194 | foreach ($this->columns() as $key => $label) { |
| 195 | $selected = in_array($key, $defaults) ? true : false; ?> |
| 196 | <label class="column-label" |
| 197 | for="iawp-column-<?php echo esc_attr($key); ?>"> |
| 198 | <input type="checkbox" name="iawp_columns[]" |
| 199 | id="iawp-column-<?php echo esc_attr($key); ?>" <?php checked(true, $selected, true); ?> value="<?php echo esc_attr($key); ?>" |
| 200 | data-column="<?php echo esc_attr($key); ?>"> |
| 201 | <span><?php echo esc_html($label); ?></span> |
| 202 | </label><?php |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 |