table.php
3 years ago
table_campaigns.php
3 years ago
table_geo.php
3 years ago
table_referrers.php
3 years ago
table_views.php
3 years ago
table.php
290 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | abstract class Table |
| 6 | { |
| 7 | abstract protected function columns(); |
| 8 | |
| 9 | abstract protected function default_columns(); |
| 10 | |
| 11 | abstract protected function rows(array $dates); |
| 12 | |
| 13 | abstract protected function table_name(); |
| 14 | |
| 15 | private $filters; |
| 16 | private $visible_columns; |
| 17 | private $views; |
| 18 | |
| 19 | public function __construct($visible_columns = null) |
| 20 | { |
| 21 | $this->filters = new Filters(); |
| 22 | $this->set_visible_columns($visible_columns); |
| 23 | } |
| 24 | |
| 25 | public function visible_columns() |
| 26 | { |
| 27 | return $this->visible_columns; |
| 28 | } |
| 29 | |
| 30 | private function set_visible_columns($visible_columns = null) |
| 31 | { |
| 32 | $valid_columns = array_keys($this->columns()); |
| 33 | |
| 34 | if ($visible_columns === null) { |
| 35 | $this->visible_columns = $this->default_columns(); |
| 36 | |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | foreach ($visible_columns as $column) { |
| 41 | if (in_array($column, $valid_columns)) { |
| 42 | if ($this->visible_columns === null) { |
| 43 | $this->visible_columns = []; |
| 44 | } |
| 45 | array_push($this->visible_columns, $column); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if ($this->visible_columns === null) { |
| 50 | $this->visible_columns = $this->default_columns(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public function get_table_markup() |
| 55 | { |
| 56 | $opts = new Dashboard_Options(); |
| 57 | $templates = IAWP()->templates(); |
| 58 | |
| 59 | return $templates->render('table/index', [ |
| 60 | 'just_rows' => false, |
| 61 | 'table_name' => $this->table_name(), |
| 62 | 'columns' => $this->visible_columns, |
| 63 | 'all_columns' => $this->columns(), |
| 64 | 'row_count' => 0, |
| 65 | 'rows' => [], |
| 66 | 'render_skeleton' => true, |
| 67 | 'page_size' => IAWP()->pagination_page_size(), |
| 68 | 'opts' => $opts, |
| 69 | ]); |
| 70 | } |
| 71 | |
| 72 | public function set_views($views) |
| 73 | { |
| 74 | $this->views = $views; |
| 75 | } |
| 76 | |
| 77 | public function get_row_markup($rows) |
| 78 | { |
| 79 | return $this->get_rendered_template($rows, true); |
| 80 | } |
| 81 | |
| 82 | private function get_rendered_template($rows, $just_rows = false) |
| 83 | { |
| 84 | $opts = new Dashboard_Options(); |
| 85 | $templates = IAWP()->templates(); |
| 86 | |
| 87 | $templates->registerFunction('row_data_attributes', [$this, 'get_row_data_attributes']); |
| 88 | $templates->registerFunction('cell_content', [$this, 'get_cell_content']); |
| 89 | |
| 90 | return $templates->render('table/index', [ |
| 91 | 'just_rows' => $just_rows, |
| 92 | 'table_name' => $this->table_name(), |
| 93 | 'columns' => $this->visible_columns, |
| 94 | 'all_columns' => $this->columns(), |
| 95 | 'row_count' => count($rows), |
| 96 | 'rows' => $rows, |
| 97 | 'render_skeleton' => false, |
| 98 | 'page_size' => IAWP()->pagination_page_size(), |
| 99 | 'opts' => $opts, |
| 100 | ]); |
| 101 | } |
| 102 | |
| 103 | public function get_row_data_attributes($row) |
| 104 | { |
| 105 | $html = ''; |
| 106 | |
| 107 | foreach ($this->columns() as $key => $value) { |
| 108 | $data_val = $row->$key(); |
| 109 | $html .= ' data-' . esc_attr($key) . '="' . esc_attr($data_val) . '"'; |
| 110 | } |
| 111 | |
| 112 | return $html; |
| 113 | } |
| 114 | |
| 115 | public function get_cell_content($row, $column) |
| 116 | { |
| 117 | if (is_null($row->$column())) { |
| 118 | return null; |
| 119 | } |
| 120 | if ($column == 'title' && $row->is_deleted()) { |
| 121 | return Security::string($row->$column()) . ' <span class="deleted-label">' . esc_html__('(deleted)', 'iawp') . '</span>'; |
| 122 | } elseif ($column == 'views') { |
| 123 | $views = number_format($row->views(), 0); |
| 124 | $views_percentage = Number_Formatter::format(($row->views() / $this->views->views()) * 100, 'percent', 2); |
| 125 | |
| 126 | return Security::string($views) . ' <span class="percentage">(' . Security::string($views_percentage) . ')</span>'; |
| 127 | } elseif ($column == 'visitors') { |
| 128 | $visitors = number_format($row->visitors(), 0); |
| 129 | $visitors_percentage = Number_Formatter::format(($row->visitors() / $this->views->visitors()) * 100, 'percent', 2); |
| 130 | |
| 131 | return Security::string($visitors) . ' <span class="percentage">(' . Security::string($visitors_percentage) . ')</span>'; |
| 132 | } elseif ($column === 'views_growth' || $column === 'visitors_growth') { |
| 133 | return Number_Formatter::format($row->$column(), 'percent', 2); |
| 134 | } elseif ($column == 'url') { |
| 135 | if ($row->is_deleted()) { |
| 136 | return Security::string(esc_url($row->url())); |
| 137 | } else { |
| 138 | return '<a href="' . Security::string(esc_url($row->url(true))) . '" target="_blank" class="external-link">' . Security::string(esc_url($row->url())) . '<span class="dashicons dashicons-external"></span></a>'; |
| 139 | } |
| 140 | } elseif ($column == 'author') { |
| 141 | return Security::html($row->avatar()) . ' ' . Security::string($row->author()); |
| 142 | } elseif ($column == 'date') { |
| 143 | return Security::string(date(Date_Format::php(), $row->date())); |
| 144 | } elseif ($column == 'type' && method_exists($row, 'icon')) { |
| 145 | return Security::html($row->icon(0)) . ' ' . Security::string($row->type()); |
| 146 | } elseif ($column == 'referrer' && !$row->is_direct()) { |
| 147 | return '<a href="' . esc_url($row->referrer_url()) . '" target="_blank" class="external-link">' . Security::string($row->referrer()) . '<span class="dashicons dashicons-external"></span></a>'; |
| 148 | } elseif ($column === 'country') { |
| 149 | return '<img class="flag" alt="Country flag" src="' . Security::string($row->flag()) . '"/>' . Security::string($row->country()); |
| 150 | } else { |
| 151 | return Security::string($row->$column()); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private function filters() |
| 156 | { |
| 157 | return $this->filters; |
| 158 | } |
| 159 | |
| 160 | public function output_toolbar() |
| 161 | { |
| 162 | $opts = new Dashboard_Options(); ?> |
| 163 | <div class="toolbar"> |
| 164 | <div class="buttons"> |
| 165 | <div class="modal-parent" |
| 166 | data-controller="dates" |
| 167 | data-dates-relative-range-id-value="<?php echo $opts->relative_range() ?>" |
| 168 | data-dates-exact-start-value="<?php echo $opts->start() ?>" |
| 169 | data-dates-exact-end-value="<?php echo $opts->end() ?>" |
| 170 | data-dates-first-day-of-week-value="<?php echo absint(IAWP()->get_option('iawp_dow', 0)) ?>" |
| 171 | data-dates-css-url-value="<?php echo esc_url(url_to('dist/styles/easepick/datepicker.css')) ?>" |
| 172 | data-dates-format-value="<?php echo esc_attr(Date_Format::js()) ?>" |
| 173 | > |
| 174 | <button id="dates-button" |
| 175 | class="iawp-button ghost-white" |
| 176 | data-action="dates#toggleModal" |
| 177 | data-dates-target="modalButton" |
| 178 | > |
| 179 | <span class="dashicons dashicons-calendar-alt"></span> |
| 180 | <span><?php echo $opts->date_label() ?></span> |
| 181 | </button> |
| 182 | <div id="modal-dates" |
| 183 | class="modal large flex" |
| 184 | data-dates-target="modal" |
| 185 | > |
| 186 | <div class="modal-inner"> |
| 187 | <div id="easepick-picker" |
| 188 | data-dates-target="easepick" |
| 189 | style="display: none;" |
| 190 | > |
| 191 | </div> |
| 192 | <div class="relative-dates"> |
| 193 | <?php foreach (Relative_Range::ranges() as $range): ?> |
| 194 | <button class="iawp-button ghost-purple" |
| 195 | data-dates-target="relativeRange" |
| 196 | data-action="dates#relativeRangeSelected" |
| 197 | data-relative-range-id="<?php echo $range->id ?>" |
| 198 | data-relative-range-label="<?php echo $range->label ?>" |
| 199 | data-relative-range-start="<?php echo $range->start->format('Y-m-d') ?>" |
| 200 | data-relative-range-end="<?php echo $range->end->format('Y-m-d') ?>" |
| 201 | > |
| 202 | <?php echo $range->label ?> |
| 203 | </button> |
| 204 | <?php endforeach ?> |
| 205 | </div> |
| 206 | <div> |
| 207 | <hr/> |
| 208 | </div> |
| 209 | <div> |
| 210 | <button class="iawp-button purple" |
| 211 | data-dates-target="apply" |
| 212 | data-action="dates#apply" |
| 213 | > |
| 214 | <?php esc_html_e('Apply', 'iawp'); ?> |
| 215 | </button> |
| 216 | <button class="iawp-button ghost-purple" |
| 217 | data-action="dates#closeModal" |
| 218 | > |
| 219 | <?php esc_html_e('Cancel', 'iawp'); ?> |
| 220 | </button> |
| 221 | </div> |
| 222 | </div> |
| 223 | </div> |
| 224 | </div> |
| 225 | <?php $this->filters()->output_filters($this->columns()) ?> |
| 226 | <?php $this->column_picker() ?> |
| 227 | </div> |
| 228 | <div class="buttons"> |
| 229 | <button class="iawp-button ghost-white" |
| 230 | data-controller="clipboard" |
| 231 | data-action="clipboard#copy" |
| 232 | > |
| 233 | <span class="dashicons dashicons-admin-page"></span> |
| 234 | <span data-clipboard-target="statusTextElement"> |
| 235 | <?php esc_html_e('Copy Dashboard URL', 'iawp'); ?> |
| 236 | </span> |
| 237 | </button> |
| 238 | <a class="learn-more" |
| 239 | href="https://independentwp.com/knowledgebase/dashboard/save-reports-revisit-later/" |
| 240 | target="_blank"><span class="dashicons dashicons-info-outline"></span></a> |
| 241 | </div> |
| 242 | </div><?php |
| 243 | } |
| 244 | |
| 245 | private function column_picker() |
| 246 | { |
| 247 | $visible_columns = $this->visible_columns; ?> |
| 248 | |
| 249 | <div class="customize-columns modal-parent small" |
| 250 | data-controller="columns" |
| 251 | > |
| 252 | <button id="customize-columns" |
| 253 | class="iawp-button ghost-white" |
| 254 | data-action="columns#toggleModal" |
| 255 | data-columns-target="modalButton" |
| 256 | > |
| 257 | <span class="dashicons dashicons-columns"></span> |
| 258 | <span><?php esc_html_e('Edit Columns', 'iawp'); ?></span> |
| 259 | </button> |
| 260 | <div id="modal-columns" |
| 261 | class="modal small" |
| 262 | data-columns-target="modal" |
| 263 | > |
| 264 | <div class="modal-inner"> |
| 265 | <div class="title-small"> |
| 266 | <?php esc_html_e('Columns', 'iawp'); ?> |
| 267 | </div> |
| 268 | <?php foreach ($this->columns() as $key => $label): ?> |
| 269 | <?php $selected = in_array($key, $visible_columns); ?> |
| 270 | <label class="column-label" |
| 271 | for="iawp-column-<?php echo esc_attr($key); ?>" |
| 272 | > |
| 273 | <input id="iawp-column-<?php echo esc_attr($key); ?>" |
| 274 | <?php checked(true, $selected, true); ?> |
| 275 | type="checkbox" |
| 276 | name="<?php esc_attr_e($key) ?>" |
| 277 | data-columns-target="checkbox" |
| 278 | data-action="columns#check" |
| 279 | data-test-visibility="<?php $selected ? 'visible' : 'hidden'; ?>" |
| 280 | > |
| 281 | <span><?php echo esc_html($label); ?></span> |
| 282 | </label> |
| 283 | <?php endforeach ?> |
| 284 | </div> |
| 285 | </div> |
| 286 | </div> |
| 287 | <?php |
| 288 | } |
| 289 | } |
| 290 |