column.php
3 years ago
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
421 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | abstract class Table |
| 6 | { |
| 7 | /** |
| 8 | * @return array<Column> |
| 9 | */ |
| 10 | protected abstract function local_columns() : array; |
| 11 | |
| 12 | protected abstract function table_name() : string; |
| 13 | |
| 14 | private $filters ; |
| 15 | private $visible_columns ; |
| 16 | private $views ; |
| 17 | public function __construct( $visible_columns = null ) |
| 18 | { |
| 19 | $this->filters = new Filters(); |
| 20 | $this->visible_columns = $visible_columns; |
| 21 | } |
| 22 | |
| 23 | public function get_columns() : array |
| 24 | { |
| 25 | if ( $this->visible_columns === null ) { |
| 26 | return $this->local_columns(); |
| 27 | } |
| 28 | return array_map( function ( $column ) { |
| 29 | if ( in_array( $column->id(), $this->visible_columns ) ) { |
| 30 | $column->set_visible( true ); |
| 31 | } |
| 32 | return $column; |
| 33 | }, $this->local_columns() ); |
| 34 | } |
| 35 | |
| 36 | public function visible_column_ids() |
| 37 | { |
| 38 | $visible_columns = []; |
| 39 | foreach ( $this->get_columns() as $column ) { |
| 40 | if ( $column->visible() ) { |
| 41 | $visible_columns[] = $column->id(); |
| 42 | } |
| 43 | } |
| 44 | return $visible_columns; |
| 45 | } |
| 46 | |
| 47 | public function get_table_markup() |
| 48 | { |
| 49 | $opts = new Dashboard_Options(); |
| 50 | $templates = IAWP()->templates(); |
| 51 | return $templates->render( 'table/index', [ |
| 52 | 'just_rows' => false, |
| 53 | 'table_name' => $this->table_name(), |
| 54 | 'all_columns' => $this->get_columns(), |
| 55 | 'visible_columns' => $this->get_visible_columns(), |
| 56 | 'row_count' => 0, |
| 57 | 'rows' => [], |
| 58 | 'render_skeleton' => true, |
| 59 | 'page_size' => IAWP()->pagination_page_size(), |
| 60 | 'opts' => $opts, |
| 61 | ] ); |
| 62 | } |
| 63 | |
| 64 | public function set_views( $views ) |
| 65 | { |
| 66 | $this->views = $views; |
| 67 | } |
| 68 | |
| 69 | public function get_row_markup( $rows ) |
| 70 | { |
| 71 | return $this->get_rendered_template( $rows, true ); |
| 72 | } |
| 73 | |
| 74 | private function get_visible_columns() : int |
| 75 | { |
| 76 | $visible_columns = 0; |
| 77 | foreach ( $this->get_columns() as $column ) { |
| 78 | if ( $column->visible() ) { |
| 79 | $visible_columns++; |
| 80 | } |
| 81 | } |
| 82 | return $visible_columns; |
| 83 | } |
| 84 | |
| 85 | private function get_rendered_template( $rows, $just_rows = false ) |
| 86 | { |
| 87 | $opts = new Dashboard_Options(); |
| 88 | $templates = IAWP()->templates(); |
| 89 | $templates->registerFunction( 'row_data_attributes', [ $this, 'get_row_data_attributes' ] ); |
| 90 | $templates->registerFunction( 'cell_content', [ $this, 'get_cell_content' ] ); |
| 91 | return $templates->render( 'table/index', [ |
| 92 | 'just_rows' => $just_rows, |
| 93 | 'table_name' => $this->table_name(), |
| 94 | 'all_columns' => $this->get_columns(), |
| 95 | 'visible_columns' => $this->get_visible_columns(), |
| 96 | 'row_count' => count( $rows ), |
| 97 | 'rows' => $rows, |
| 98 | 'render_skeleton' => false, |
| 99 | 'page_size' => IAWP()->pagination_page_size(), |
| 100 | 'opts' => $opts, |
| 101 | ] ); |
| 102 | } |
| 103 | |
| 104 | public function get_row_data_attributes( $row ) |
| 105 | { |
| 106 | $html = ''; |
| 107 | foreach ( $this->get_columns() as $column ) { |
| 108 | $id = $column->id(); |
| 109 | $data_val = $row->{$id}(); |
| 110 | $html .= ' data-' . esc_attr( $column->id() ) . '="' . esc_attr( $data_val ) . '"'; |
| 111 | } |
| 112 | return $html; |
| 113 | } |
| 114 | |
| 115 | public function get_cell_content( $row, $column_id ) |
| 116 | { |
| 117 | if ( is_null( $row->{$column_id}() ) ) { |
| 118 | return null; |
| 119 | } |
| 120 | |
| 121 | if ( $column_id == 'title' && $row->is_deleted() ) { |
| 122 | return Security::string( $row->{$column_id}() ) . ' <span class="deleted-label">' . esc_html__( '(deleted)', 'iawp' ) . '</span>'; |
| 123 | } elseif ( $column_id == 'views' ) { |
| 124 | $views = number_format( $row->views(), 0 ); |
| 125 | $views_percentage = Number_Formatter::format( $row->views() / $this->views->views() * 100, 'percent', 2 ); |
| 126 | return Security::string( $views ) . ' <span class="percentage">(' . Security::string( $views_percentage ) . ')</span>'; |
| 127 | } elseif ( $column_id == 'visitors' ) { |
| 128 | $visitors = number_format( $row->visitors(), 0 ); |
| 129 | $visitors_percentage = Number_Formatter::format( $row->visitors() / $this->views->visitors() * 100, 'percent', 2 ); |
| 130 | return Security::string( $visitors ) . ' <span class="percentage">(' . Security::string( $visitors_percentage ) . ')</span>'; |
| 131 | } elseif ( $column_id === 'views_growth' || $column_id === 'visitors_growth' ) { |
| 132 | return Number_Formatter::format( $row->{$column_id}(), 'percent', 2 ); |
| 133 | } elseif ( $column_id == 'url' ) { |
| 134 | |
| 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 | |
| 141 | } elseif ( $column_id == 'author' ) { |
| 142 | return Security::html( $row->avatar() ) . ' ' . Security::string( $row->author() ); |
| 143 | } elseif ( $column_id == 'date' ) { |
| 144 | return Security::string( date( Date_Format::php(), $row->date() ) ); |
| 145 | } elseif ( $column_id == 'type' && method_exists( $row, 'icon' ) ) { |
| 146 | return $row->icon( 0 ) . ' ' . Security::string( $row->type() ); |
| 147 | } elseif ( $column_id == 'referrer' && !$row->is_direct() ) { |
| 148 | return '<a href="' . esc_url( $row->referrer_url() ) . '" target="_blank" class="external-link">' . Security::string( $row->referrer() ) . '<span class="dashicons dashicons-external"></span></a>'; |
| 149 | } elseif ( $column_id === 'country' ) { |
| 150 | return '<img class="flag" alt="Country flag" src="' . Security::string( $row->flag() ) . '"/>' . Security::string( $row->country() ); |
| 151 | } elseif ( function_exists( 'wc_price' ) && ($column_id === 'wc_gross_sales' || $column_id === 'wc_refunded_amount' || $column_id === 'wc_net_sales') ) { |
| 152 | return Security::string( wc_price( $row->{$column_id}() ) ); |
| 153 | } else { |
| 154 | return Security::string( $row->{$column_id}() ); |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |
| 159 | private function filters() |
| 160 | { |
| 161 | return $this->filters; |
| 162 | } |
| 163 | |
| 164 | public function output_toolbar() |
| 165 | { |
| 166 | $opts = new Dashboard_Options(); |
| 167 | ?> |
| 168 | <div class="toolbar"> |
| 169 | <div class="buttons"> |
| 170 | <div class="modal-parent" |
| 171 | data-controller="dates" |
| 172 | data-dates-relative-range-id-value="<?php |
| 173 | echo $opts->relative_range() ; |
| 174 | ?>" |
| 175 | data-dates-exact-start-value="<?php |
| 176 | echo $opts->start() ; |
| 177 | ?>" |
| 178 | data-dates-exact-end-value="<?php |
| 179 | echo $opts->end() ; |
| 180 | ?>" |
| 181 | data-dates-first-day-of-week-value="<?php |
| 182 | echo absint( IAWP()->get_option( 'iawp_dow', 0 ) ) ; |
| 183 | ?>" |
| 184 | data-dates-css-url-value="<?php |
| 185 | echo esc_url( url_to( 'dist/styles/easepick/datepicker.css' ) ) ; |
| 186 | ?>" |
| 187 | data-dates-format-value="<?php |
| 188 | echo esc_attr( Date_Format::js() ) ; |
| 189 | ?>" |
| 190 | > |
| 191 | <button id="dates-button" |
| 192 | class="iawp-button ghost-white" |
| 193 | data-action="dates#toggleModal" |
| 194 | data-dates-target="modalButton" |
| 195 | > |
| 196 | <span class="dashicons dashicons-calendar-alt"></span> |
| 197 | <span><?php |
| 198 | echo $opts->date_label() ; |
| 199 | ?></span> |
| 200 | </button> |
| 201 | <div id="modal-dates" |
| 202 | class="modal large flex" |
| 203 | data-dates-target="modal" |
| 204 | > |
| 205 | <div class="modal-inner"> |
| 206 | <div id="easepick-picker" |
| 207 | data-dates-target="easepick" |
| 208 | style="display: none;" |
| 209 | > |
| 210 | </div> |
| 211 | <div class="relative-dates"> |
| 212 | <?php |
| 213 | foreach ( Relative_Range::ranges() as $range ) { |
| 214 | ?> |
| 215 | <button class="iawp-button ghost-purple" |
| 216 | data-dates-target="relativeRange" |
| 217 | data-action="dates#relativeRangeSelected" |
| 218 | data-relative-range-id="<?php |
| 219 | echo $range->id ; |
| 220 | ?>" |
| 221 | data-relative-range-label="<?php |
| 222 | echo $range->label ; |
| 223 | ?>" |
| 224 | data-relative-range-start="<?php |
| 225 | echo $range->start->format( 'Y-m-d' ) ; |
| 226 | ?>" |
| 227 | data-relative-range-end="<?php |
| 228 | echo $range->end->format( 'Y-m-d' ) ; |
| 229 | ?>" |
| 230 | > |
| 231 | <?php |
| 232 | echo $range->label ; |
| 233 | ?> |
| 234 | </button> |
| 235 | <?php |
| 236 | } |
| 237 | ?> |
| 238 | </div> |
| 239 | <div> |
| 240 | <hr/> |
| 241 | </div> |
| 242 | <div> |
| 243 | <button class="iawp-button purple" |
| 244 | data-dates-target="apply" |
| 245 | data-action="dates#apply" |
| 246 | > |
| 247 | <?php |
| 248 | esc_html_e( 'Apply', 'iawp' ); |
| 249 | ?> |
| 250 | </button> |
| 251 | <button class="iawp-button ghost-purple" |
| 252 | data-action="dates#closeModal" |
| 253 | > |
| 254 | <?php |
| 255 | esc_html_e( 'Cancel', 'iawp' ); |
| 256 | ?> |
| 257 | </button> |
| 258 | </div> |
| 259 | </div> |
| 260 | </div> |
| 261 | </div> |
| 262 | <?php |
| 263 | $this->filters()->output_filters( $this->get_columns() ); |
| 264 | ?> |
| 265 | <?php |
| 266 | $this->column_picker(); |
| 267 | ?> |
| 268 | </div> |
| 269 | <div class="buttons"> |
| 270 | <button class="iawp-button ghost-white" |
| 271 | data-controller="clipboard" |
| 272 | data-action="clipboard#copy" |
| 273 | > |
| 274 | <span class="dashicons dashicons-admin-page"></span> |
| 275 | <span data-clipboard-target="statusTextElement"> |
| 276 | <?php |
| 277 | esc_html_e( 'Copy Dashboard URL', 'iawp' ); |
| 278 | ?> |
| 279 | </span> |
| 280 | </button> |
| 281 | <a class="learn-more" |
| 282 | href="https://independentwp.com/knowledgebase/dashboard/save-reports-revisit-later/" |
| 283 | target="_blank"><span class="dashicons dashicons-info-outline"></span></a> |
| 284 | </div> |
| 285 | </div><?php |
| 286 | } |
| 287 | |
| 288 | private function column_picker() |
| 289 | { |
| 290 | ?> |
| 291 | |
| 292 | <div class="customize-columns modal-parent small" |
| 293 | data-controller="columns" |
| 294 | > |
| 295 | <button id="customize-columns" |
| 296 | class="iawp-button ghost-white" |
| 297 | data-action="columns#toggleModal" |
| 298 | data-columns-target="modalButton" |
| 299 | > |
| 300 | <span class="dashicons dashicons-columns"></span> |
| 301 | <span><?php |
| 302 | esc_html_e( 'Edit Columns', 'iawp' ); |
| 303 | ?></span> |
| 304 | </button> |
| 305 | <div id="modal-columns" |
| 306 | class="modal small" |
| 307 | data-columns-target="modal" |
| 308 | > |
| 309 | <div class="modal-inner"> |
| 310 | <div class="title-small"> |
| 311 | <?php |
| 312 | esc_html_e( 'Columns', 'iawp' ); |
| 313 | ?> |
| 314 | </div> |
| 315 | <?php |
| 316 | foreach ( $this->get_columns() as $column ) { |
| 317 | ?> |
| 318 | <?php |
| 319 | |
| 320 | if ( $column->id() === 'wc_orders' ) { |
| 321 | ?> |
| 322 | <p class="title-small wc-title"> |
| 323 | <?php |
| 324 | esc_html_e( 'WooCommerce', 'iawp' ); |
| 325 | ?> |
| 326 | <?php |
| 327 | ?> |
| 328 | <span class="pro-label"><?php |
| 329 | esc_html_e( 'PRO', 'iawp' ); |
| 330 | ?></span> |
| 331 | <?php |
| 332 | ?> |
| 333 | </p> |
| 334 | <?php |
| 335 | } |
| 336 | |
| 337 | ?> |
| 338 | |
| 339 | <label class="column-label" |
| 340 | for="iawp-column-<?php |
| 341 | echo esc_attr( $column->id() ) ; |
| 342 | ?>" |
| 343 | > |
| 344 | <input id="iawp-column-<?php |
| 345 | echo esc_attr( $column->id() ) ; |
| 346 | ?>" |
| 347 | <?php |
| 348 | |
| 349 | if ( $column->requires_pro() && !IAWP_FS()->can_use_premium_code__premium_only() ) { |
| 350 | ?> |
| 351 | <?php |
| 352 | checked( true, false, true ); |
| 353 | ?> |
| 354 | disabled="disabled" |
| 355 | data-locked-behind-pro="true" |
| 356 | <?php |
| 357 | } else { |
| 358 | ?> |
| 359 | <?php |
| 360 | checked( true, $column->visible(), true ); |
| 361 | ?> |
| 362 | data-columns-target="checkbox" |
| 363 | data-action="columns#check" |
| 364 | <?php |
| 365 | } |
| 366 | |
| 367 | ?> |
| 368 | type="checkbox" |
| 369 | name="<?php |
| 370 | esc_attr_e( $column->id() ); |
| 371 | ?>" |
| 372 | data-test-visibility="<?php |
| 373 | echo ( $column->visible() ? 'visible' : 'hidden' ) ; |
| 374 | ?>" |
| 375 | > |
| 376 | <span><?php |
| 377 | echo esc_html( $column->label() ) ; |
| 378 | ?></span> |
| 379 | </label> |
| 380 | <?php |
| 381 | } |
| 382 | ?> |
| 383 | </div> |
| 384 | </div> |
| 385 | </div> |
| 386 | <?php |
| 387 | } |
| 388 | |
| 389 | public final function csv( $rows ) |
| 390 | { |
| 391 | $columns = $this->get_columns(); |
| 392 | $csv_header = []; |
| 393 | $csv_rows = []; |
| 394 | foreach ( $columns as $column ) { |
| 395 | if ( !$column->exportable() || $column->requires_pro() && !IAWP_FS()->can_use_premium_code__premium_only() ) { |
| 396 | continue; |
| 397 | } |
| 398 | $csv_header[] = $column->label(); |
| 399 | } |
| 400 | foreach ( $rows as $row ) { |
| 401 | $csv_row = []; |
| 402 | foreach ( $columns as $column ) { |
| 403 | if ( !$column->exportable() || $column->requires_pro() && !IAWP_FS()->can_use_premium_code__premium_only() ) { |
| 404 | continue; |
| 405 | } |
| 406 | $column_id = $column->id(); |
| 407 | |
| 408 | if ( $column_id === 'date' ) { |
| 409 | $csv_row[] = date( Date_Format::php(), $row->{$column_id}() ); |
| 410 | } else { |
| 411 | $csv_row[] = $row->{$column_id}(); |
| 412 | } |
| 413 | |
| 414 | } |
| 415 | $csv_rows[] = $csv_row; |
| 416 | } |
| 417 | $csv = array_merge( [ $csv_header ], $csv_rows ); |
| 418 | return Array_To_CSV::array2csv( $csv ); |
| 419 | } |
| 420 | |
| 421 | } |