| Total Complexity | 54 |
| Total Lines | 595 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Complex classes like WPInv_Subscriptions_Widget often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WPInv_Subscriptions_Widget, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class WPInv_Subscriptions_Widget extends WP_Super_Duper { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Register the widget with WordPress. |
||
| 19 | * |
||
| 20 | */ |
||
| 21 | public function __construct() { |
||
| 22 | |||
| 23 | $options = array( |
||
| 24 | 'textdomain' => 'invoicing', |
||
| 25 | 'block-icon' => 'controls-repeat', |
||
| 26 | 'block-category'=> 'widgets', |
||
| 27 | 'block-keywords'=> "['invoicing','subscriptions', 'getpaid']", |
||
| 28 | 'class_name' => __CLASS__, |
||
| 29 | 'base_id' => 'wpinv_subscriptions', |
||
| 30 | 'name' => __( 'GetPaid > Subscriptions', 'invoicing' ), |
||
| 31 | 'widget_ops' => array( |
||
| 32 | 'classname' => 'getpaid-subscriptions bsui', |
||
| 33 | 'description' => esc_html__( "Displays the current user's subscriptions.", 'invoicing' ), |
||
| 34 | ), |
||
| 35 | 'arguments' => array( |
||
| 36 | 'title' => array( |
||
| 37 | 'title' => __( 'Widget title', 'invoicing' ), |
||
| 38 | 'desc' => __( 'Enter widget title.', 'invoicing' ), |
||
| 39 | 'type' => 'text', |
||
| 40 | 'desc_tip' => true, |
||
| 41 | 'default' => '', |
||
| 42 | 'advanced' => false |
||
| 43 | ), |
||
| 44 | ) |
||
| 45 | |||
| 46 | ); |
||
| 47 | |||
| 48 | |||
| 49 | parent::__construct( $options ); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Retrieves current user's subscriptions. |
||
| 54 | * |
||
| 55 | * @return GetPaid_Subscriptions_Query |
||
| 56 | */ |
||
| 57 | public function get_subscriptions() { |
||
| 58 | |||
| 59 | // Prepare license args. |
||
| 60 | $args = array( |
||
| 61 | 'customer_in' => get_current_user_id(), |
||
| 62 | 'paged' => ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1, |
||
| 63 | ); |
||
| 64 | |||
| 65 | return new GetPaid_Subscriptions_Query( $args ); |
||
| 66 | |||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The Super block output function. |
||
| 71 | * |
||
| 72 | * @param array $args |
||
| 73 | * @param array $widget_args |
||
| 74 | * @param string $content |
||
| 75 | * |
||
| 76 | * @return mixed|string|bool |
||
| 77 | */ |
||
| 78 | public function output( $args = array(), $widget_args = array(), $content = '' ) { |
||
| 128 | |||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Retrieves the subscription columns. |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | public function get_subscriptions_table_columns() { |
||
| 137 | |||
| 138 | $columns = array( |
||
| 139 | 'subscription' => __( 'Subscription', 'invoicing' ), |
||
| 140 | 'amount' => __( 'Amount', 'invoicing' ), |
||
| 141 | 'renewal-date' => __( 'Next payment', 'invoicing' ), |
||
| 142 | 'status' => __( 'Status', 'invoicing' ), |
||
| 143 | ); |
||
| 144 | |||
| 145 | return apply_filters( 'getpaid_frontend_subscriptions_table_columns', $columns ); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Displays the table header. |
||
| 150 | * |
||
| 151 | */ |
||
| 152 | public function print_table_header() { |
||
| 153 | |||
| 154 | ?> |
||
| 155 | |||
| 156 | <style> |
||
| 157 | .getpaid-subscriptions-table-column-subscription { |
||
| 158 | width: 35%; |
||
| 159 | font-weight: 500; |
||
| 160 | } |
||
| 161 | |||
| 162 | .getpaid-subscriptions-table-row span.label { |
||
| 163 | font-weight: 500; |
||
| 164 | } |
||
| 165 | |||
| 166 | .getpaid-subscriptions.bsui .table-bordered thead th { |
||
| 167 | border-bottom-width: 1px; |
||
| 168 | } |
||
| 169 | |||
| 170 | .getpaid-subscriptions.bsui .table-striped tbody tr:nth-of-type(odd) { |
||
| 171 | background-color: rgb(0 0 0 / 0.01); |
||
| 172 | } |
||
| 173 | </style> |
||
| 174 | |||
| 175 | <table class="table table-bordered"> |
||
| 176 | |||
| 177 | <thead> |
||
| 178 | <tr> |
||
| 179 | <?php foreach ( $this->get_subscriptions_table_columns() as $key => $label ) : ?> |
||
| 180 | <th scope="col" class="getpaid-subscriptions-table-<?php echo sanitize_html_class( $key ); ?>"> |
||
| 181 | <?php echo sanitize_text_field( $label ); ?> |
||
| 182 | </th> |
||
| 183 | <?php endforeach; ?> |
||
| 184 | </tr> |
||
| 185 | </thead> |
||
| 186 | |||
| 187 | <?php |
||
| 188 | |||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Displays the table body. |
||
| 193 | * |
||
| 194 | * @param WPInv_Subscription[] $subscriptions |
||
| 195 | */ |
||
| 196 | public function print_table_body( $subscriptions ) { |
||
| 202 | } |
||
| 203 | |||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Displays the table body if no subscriptions were found. |
||
| 208 | * |
||
| 209 | */ |
||
| 210 | public function print_table_body_no_subscriptions() { |
||
| 211 | |||
| 212 | ?> |
||
| 213 | <tbody> |
||
| 214 | |||
| 215 | <tr> |
||
| 216 | <td colspan="<?php echo count( $this->get_subscriptions_table_columns() ); ?>"> |
||
| 217 | |||
| 218 | <?php |
||
| 219 | echo aui()->alert( |
||
| 220 | array( |
||
| 221 | 'content' => wp_kses_post( __( 'No subscriptions found.', 'invoicing' ) ), |
||
| 222 | 'type' => 'warning', |
||
| 223 | ) |
||
| 224 | ); |
||
| 225 | ?> |
||
| 226 | |||
| 227 | </td> |
||
| 228 | </tr> |
||
| 229 | |||
| 230 | </tbody> |
||
| 231 | <?php |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Displays the table body if subscriptions were found. |
||
| 236 | * |
||
| 237 | * @param WPInv_Subscription[] $subscriptions |
||
| 238 | */ |
||
| 239 | public function print_table_body_subscriptions( $subscriptions ) { |
||
| 283 | </tbody> |
||
| 284 | <?php |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Formats a date field. |
||
| 289 | * |
||
| 290 | * @param string $date |
||
| 291 | */ |
||
| 292 | public function format_date_field( $date, $default = "—" ) { |
||
| 293 | |||
| 294 | if ( empty( $date ) || '0000-00-00 00:00:00' == $date ) { |
||
| 295 | return $default; |
||
| 296 | } else { |
||
| 297 | return date_i18n( get_option( 'date_format' ), strtotime( $date ) ); |
||
|
|
|||
| 298 | } |
||
| 299 | |||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Subscription column |
||
| 304 | * |
||
| 305 | * @param WPInv_Subscription $subscription |
||
| 306 | * @since 1.0.0 |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function column_subscription( $subscription ) { |
||
| 310 | $subscription_id = (int) $subscription->get_id(); |
||
| 311 | $url = esc_url( add_query_arg( 'subscription', $subscription_id, get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ) ); |
||
| 312 | return $this->add_row_actions( "<a href='$url' class='text-decoration-none'>#$subscription_id</a>", $subscription ); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Adds row actions to a column |
||
| 317 | * |
||
| 318 | * @param string $content column content |
||
| 319 | * @param WPInv_Subscription $subscription |
||
| 320 | * @since 1.0.0 |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | public function add_row_actions( $content, $subscription ) { |
||
| 324 | |||
| 325 | // Prepare row actions. |
||
| 326 | $actions = array(); |
||
| 327 | |||
| 328 | // View subscription action. |
||
| 329 | $view_url = esc_url( add_query_arg( 'subscription', (int) $subscription->get_id(), get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ) ); |
||
| 330 | $actions['view'] = "<a href='$view_url' class='text-decoration-none'>" . __( 'Manage Subscription', 'invoicing' ) . '</a>'; |
||
| 331 | |||
| 332 | // View invoice action. |
||
| 333 | $invoice = $subscription->get_parent_payment(); |
||
| 334 | |||
| 335 | if ( $invoice->get_id() ) { |
||
| 336 | $view_url = esc_url( $invoice->get_view_url() ); |
||
| 337 | $actions['invoice'] = "<a href='$view_url' class='text-decoration-none'>" . __( 'View Invoice', 'invoicing' ) . '</a>'; |
||
| 338 | } |
||
| 339 | |||
| 340 | // Filter the actions. |
||
| 341 | $actions = apply_filters( 'getpaid_subscriptions_table_subscription_actions', $actions, $subscription ); |
||
| 342 | |||
| 343 | if ( ! empty( $actions ) ) { |
||
| 344 | |||
| 345 | $sanitized = array(); |
||
| 346 | foreach ( $actions as $key => $action ) { |
||
| 347 | $key = sanitize_html_class( $key ); |
||
| 348 | $action = wp_kses_post( $action ); |
||
| 349 | $sanitized[] = "<span class='$key'>$action</span>"; |
||
| 350 | } |
||
| 351 | |||
| 352 | $row_actions = "<small class='form-text getpaid-subscription-item-actions'>"; |
||
| 353 | $row_actions .= implode( ' | ', $sanitized ); |
||
| 354 | $row_actions .= '</small>'; |
||
| 355 | |||
| 356 | } |
||
| 357 | |||
| 358 | return $content . $row_actions; |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Displays the table footer. |
||
| 363 | * |
||
| 364 | */ |
||
| 365 | public function print_table_footer() { |
||
| 366 | |||
| 367 | ?> |
||
| 368 | |||
| 369 | <tfoot> |
||
| 370 | <tr> |
||
| 371 | <?php foreach ( $this->get_subscriptions_table_columns() as $key => $label ) : ?> |
||
| 372 | <th class="getpaid-subscriptions-<?php echo sanitize_html_class( $key ); ?>"> |
||
| 373 | <?php echo sanitize_text_field( $label ); ?> |
||
| 374 | </th> |
||
| 375 | <?php endforeach; ?> |
||
| 376 | </tr> |
||
| 377 | </tfoot> |
||
| 378 | |||
| 379 | </table> |
||
| 380 | <?php |
||
| 381 | |||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Displays the navigation. |
||
| 386 | * |
||
| 387 | * @param int $total |
||
| 388 | */ |
||
| 389 | public function print_navigation( $total ) { |
||
| 390 | |||
| 391 | if ( $total < 1 ) { |
||
| 392 | |||
| 393 | // Out-of-bounds, run the query again without LIMIT for total count. |
||
| 394 | $args = array( |
||
| 395 | 'customer_in' => get_current_user_id(), |
||
| 396 | 'fields' => 'id', |
||
| 397 | ); |
||
| 398 | |||
| 399 | $count_query = new GetPaid_Subscriptions_Query( $args ); |
||
| 400 | $total = $count_query->get_total(); |
||
| 401 | } |
||
| 402 | |||
| 403 | // Abort if we do not have pages. |
||
| 404 | if ( 2 > $total ) { |
||
| 405 | return; |
||
| 406 | } |
||
| 407 | |||
| 408 | ?> |
||
| 409 | |||
| 410 | <div class="getpaid-subscriptions-pagination"> |
||
| 411 | <?php |
||
| 412 | $big = 999999; |
||
| 413 | |||
| 414 | echo paginate_links( |
||
| 415 | array( |
||
| 416 | 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), |
||
| 417 | 'format' => '?paged=%#%', |
||
| 418 | 'total' => (int) ceil( $total / 10 ), |
||
| 419 | ) |
||
| 420 | ); |
||
| 421 | ?> |
||
| 422 | </div> |
||
| 423 | |||
| 424 | <?php |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Displays a single subscription. |
||
| 429 | * |
||
| 430 | * @param string $subscription |
||
| 431 | * |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | public function display_single_subscription( $subscription ) { |
||
| 610 | |||
| 611 | } |
||
| 612 | |||
| 613 | } |
||
| 614 |