| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Yearly Renewwals Widget |
| 4 |
* |
| 5 |
* Displays the Yearly Renewwals. |
| 6 |
* |
| 7 |
* @package MainWP/Dashboard |
| 8 |
* @version 5.0.1 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace MainWP\Dashboard\Module\CostTracker; |
| 12 |
|
| 13 |
use MainWP\Dashboard\MainWP_UI; |
| 14 |
use MainWP\Dashboard\MainWP_Utility; |
| 15 |
|
| 16 |
// Exit if accessed directly. |
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
/** |
| 23 |
* Class Cost_Tracker_Yearly_Renewals |
| 24 |
* |
| 25 |
* Displays the Yearly Renewwals. |
| 26 |
*/ |
| 27 |
class Cost_Tracker_Yearly_Renewals { // phpcs:ignore -- NOSONAR - multi methods. |
| 28 |
|
| 29 |
/** |
| 30 |
* Protected static variable to hold the single instance of the class. |
| 31 |
* |
| 32 |
* @var mixed Default null |
| 33 |
*/ |
| 34 |
protected static $instance = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* Return the single instance of the class. |
| 38 |
* |
| 39 |
* @return mixed $instance The single instance of the class. |
| 40 |
*/ |
| 41 |
public static function instance() { |
| 42 |
if ( is_null( static::$instance ) ) { |
| 43 |
static::$instance = new self(); |
| 44 |
} |
| 45 |
return static::$instance; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
/** |
| 50 |
* Renders the Upocmming renewals widget data |
| 51 |
*/ |
| 52 |
public static function render_top_grid() { |
| 53 |
?> |
| 54 |
<div class="ui grid mainwp-widget-header"> |
| 55 |
<div class="twelve wide column"> |
| 56 |
<h2 class="ui header handle-drag"> |
| 57 |
<?php |
| 58 |
/** |
| 59 |
* Filter: mainwp_module_cost_tracker_yearly_renewals_widget_title |
| 60 |
* |
| 61 |
* Filters the widget title text. |
| 62 |
* |
| 63 |
* @since 5.0.1 |
| 64 |
*/ |
| 65 |
echo esc_html( apply_filters( 'mainwp_module_cost_tracker_yearly_renewals_widget_title', esc_html__( 'Upcoming Yearly Renewals', 'mainwp' ) ) ); |
| 66 |
?> |
| 67 |
<div class="sub header"><?php esc_html_e( 'Monitor your expenses - this widget highlights your upcoming yearly renewals.', 'mainwp' ); ?></div> |
| 68 |
</h2> |
| 69 |
</div> |
| 70 |
|
| 71 |
<div class="four wide column right aligned"> |
| 72 |
<div class="ui dropdown right pointing mainwp-dropdown-tab not-auto-init" id="cost-tracker-widget-yearly-renewals-top-select"> |
| 73 |
<input type="hidden" value="yearly-renewals-month"> |
| 74 |
<i class="vertical ellipsis icon"></i> |
| 75 |
<div class="menu"> |
| 76 |
<a class="item yearly_renewals_today_lnk" data-tab="yearly-renewals-today" data-value="yearly-renewals-today" title="<?php esc_attr_e( 'Today', 'mainwp' ); ?>" href="#"><?php esc_html_e( 'Today', 'mainwp' ); ?></a> |
| 77 |
<a class="item yearly_renewals_tomorrow_lnk" data-tab="yearly-renewals-tomorrow" data-value="yearly-renewals-tomorrow" title="<?php esc_attr_e( 'Tomorrow', 'mainwp' ); ?>" href="#"><?php esc_html_e( 'Tomorrow', 'mainwp' ); ?></a> |
| 78 |
<a class="item yearly_renewals_week_lnk" data-tab="yearly-renewals-week" data-value="yearly-renewals-week" title="<?php esc_attr_e( 'This Week', 'mainwp' ); ?>" href="#"><?php esc_html_e( 'This Week', 'mainwp' ); ?></a> |
| 79 |
<a class="item yearly_renewals_next_week_lnk" data-tab="yearly-renewals-next_week" data-value="yearly-renewals-next_week" title="<?php esc_attr_e( 'Next Week', 'mainwp' ); ?>" href="#"><?php esc_html_e( 'Next Week', 'mainwp' ); ?></a> |
| 80 |
<a class="item yearly_renewals_month_lnk" data-tab="yearly-renewals-month" data-value="yearly-renewals-month" title="<?php esc_attr_e( 'This Month', 'mainwp' ); ?>" href="#"><?php esc_html_e( 'This Month', 'mainwp' ); ?></a> |
| 81 |
<a class="item yearly_renewals_next_month_lnk" data-tab="yearly-renewals-next_month" data-value="yearly-renewals-next_month" title="<?php esc_attr_e( 'Next Month', 'mainwp' ); ?>" href="#"><?php esc_html_e( 'Next Month', 'mainwp' ); ?></a> |
| 82 |
<a class="item yearly_renewals_year_lnk" data-tab="yearly-renewals-year" data-value="yearly-renewals-year" title="<?php esc_attr_e( 'This Year', 'mainwp' ); ?>" href="#"><?php esc_html_e( 'This Year', 'mainwp' ); ?></a> |
| 83 |
<a class="item yearly_renewals_next_year_lnk" data-tab="yearly-renewals-next_year" data-value="yearly-renewals-next_year" title="<?php esc_attr_e( 'Next Year', 'mainwp' ); ?>" href="#"><?php esc_html_e( 'Next Year', 'mainwp' ); ?></a> |
| 84 |
</div> |
| 85 |
</div> |
| 86 |
</div> |
| 87 |
</div> |
| 88 |
|
| 89 |
<script type="text/javascript"> |
| 90 |
jQuery( document ).ready( function () { |
| 91 |
var $topSelect = jQuery( '#cost-tracker-widget-yearly-renewals-top-select' ).dropdown( { |
| 92 |
onChange: function( value ) { |
| 93 |
mainwp_ui_state_save('cost-widget-yearly-renewals', value); |
| 94 |
} |
| 95 |
} ); |
| 96 |
|
| 97 |
var curTab = mainwp_ui_state_load('cost-widget-yearly-renewals'); |
| 98 |
if( curTab != '' && curTab != null ){ |
| 99 |
$topSelect.dropdown( 'set selected', curTab ); |
| 100 |
jQuery( '.cost_tracker_yearly_renewals').removeClass('active'); //to fix preset. |
| 101 |
jQuery( '.cost_tracker_yearly_renewals[data-tab="' + curTab + '"]' ).addClass( 'active' ); |
| 102 |
} |
| 103 |
|
| 104 |
} ); |
| 105 |
</script> |
| 106 |
<?php |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Handle render costs widget. |
| 111 |
*/ |
| 112 |
public function render() { |
| 113 |
$args = func_get_args(); |
| 114 |
$data = is_array( $args ) && ! empty( $args[1][0] ) && is_array( $args[1][0] ) ? $args[1][0] : array(); |
| 115 |
$costs_data = is_array( $data ) && ! empty( $data['costs_data'] ) ? $data['costs_data'] : array(); |
| 116 |
|
| 117 |
if ( ! is_array( $costs_data ) ) { |
| 118 |
$costs_data = array(); |
| 119 |
} |
| 120 |
static::render_top_grid(); |
| 121 |
?> |
| 122 |
<div class="mainwp-scrolly-overflow"> |
| 123 |
<?php |
| 124 |
$tabs = array( 'today', 'tomorrow', 'week', 'next_week', 'month', 'next_month', 'year', 'next_year' ); |
| 125 |
foreach ( $tabs as $tab ) { |
| 126 |
$this->render_renewals_tab( $tab, $costs_data ); |
| 127 |
} |
| 128 |
?> |
| 129 |
</div> |
| 130 |
<div class="mainwp-widget-footer"></div> |
| 131 |
<?php |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Render upcoming renewals tabs. |
| 136 |
* |
| 137 |
* @param string $tab Tab. |
| 138 |
* @param array $cost_data Cost data. |
| 139 |
*/ |
| 140 |
public static function get_costs_widgets_data( $tab, $cost_data ) { //phpcs:ignore -- NOSONAR - complex. |
| 141 |
$list = array(); |
| 142 |
if ( is_array( $cost_data ) ) { |
| 143 |
$time = time(); |
| 144 |
$upcoming1 = 0; |
| 145 |
$upcoming2 = 0; |
| 146 |
if ( 'today' === $tab ) { |
| 147 |
$upcoming1 = strtotime( gmdate( 'Y-m-d 00:00:00', $time ) ); |
| 148 |
$upcoming2 = strtotime( gmdate( 'Y-m-d 23:59:59', $time ) ); |
| 149 |
} elseif ( 'tomorrow' === $tab ) { |
| 150 |
$upcoming1 = strtotime( gmdate( 'Y-m-d 00:00:00', strtotime( '+1 day', $time ) ) ); |
| 151 |
$upcoming2 = strtotime( gmdate( 'Y-m-d 23:59:59', strtotime( '+1 day', $time ) ) ); |
| 152 |
} elseif ( 'week' === $tab ) { |
| 153 |
$upcoming1 = strtotime( gmdate( 'Y-m-d 00:00:00', strtotime( 'monday this week' ) ) ); |
| 154 |
$upcoming2 = strtotime( gmdate( 'Y-m-d 23:59:59', strtotime( 'sunday this week' ) ) ); |
| 155 |
} elseif ( 'next_week' === $tab ) { |
| 156 |
$upcoming1 = strtotime( gmdate( 'Y-m-d 00:00:00', strtotime( 'first day of next week' ) ) ); |
| 157 |
$upcoming2 = strtotime( gmdate( 'Y-m-d 23:59:59', strtotime( 'last day of next week' ) ) ); |
| 158 |
} elseif ( 'month' === $tab ) { |
| 159 |
$upcoming1 = strtotime( gmdate( 'Y-m-01 00:00:00', $time ) ); |
| 160 |
$upcoming2 = strtotime( gmdate( 'Y-m-t 23:59:59', $time ) ); |
| 161 |
} elseif ( 'next_month' === $tab ) { |
| 162 |
$upcoming1 = strtotime( gmdate( 'Y-m-d 23:59:59', strtotime( 'last day of this month' ) ) ); |
| 163 |
$upcoming2 = strtotime( gmdate( 'Y-m-d 23:59:59', strtotime( 'last day of next month' ) ) ); |
| 164 |
} elseif ( 'year' === $tab ) { |
| 165 |
$upcoming1 = strtotime( gmdate( 'Y-01-01 00:00:00', $time ) ); |
| 166 |
$upcoming2 = strtotime( gmdate( 'Y-12-t 23:59:59', $time ) ); |
| 167 |
} elseif ( 'next_year' === $tab ) { |
| 168 |
$upcoming1 = strtotime( gmdate( 'Y-m-d 00:00:00', strtotime( 'first day of January ' . gmdate( 'Y' ) . '+1 year' ) ) ); |
| 169 |
$upcoming2 = strtotime( gmdate( 'Y-m-d 23:59:59', strtotime( 'last day of December ' . gmdate( 'Y' ) . '+1 year' ) ) ); |
| 170 |
} |
| 171 |
if ( $upcoming1 ) { |
| 172 |
foreach ( $cost_data as $cost ) { |
| 173 |
if ( 'yearly' !== $cost->renewal_type ) { |
| 174 |
continue; |
| 175 |
} |
| 176 |
$next_renewal = Cost_Tracker_Admin::get_next_renewal( $cost->last_renewal, $cost->renewal_type ); |
| 177 |
if ( $next_renewal > $upcoming1 && $next_renewal <= $upcoming2 ) { |
| 178 |
$list[] = $cost; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
return $list; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Render upcoming renewals tabs. |
| 188 |
* |
| 189 |
* @param string $tab Tab. |
| 190 |
* @param array $cost_data Cost data. |
| 191 |
*/ |
| 192 |
public static function render_renewals_tab( $tab, $cost_data ) { |
| 193 |
$lists = static::get_costs_widgets_data( $tab, $cost_data ); |
| 194 |
?> |
| 195 |
<div class="cost_tracker_yearly_renewals ui middle aligned tab <?php echo 'month' === $tab ? 'active' : ''; ?>" data-tab="yearly-renewals-<?php echo esc_attr( $tab ); ?>"> |
| 196 |
<?php |
| 197 |
/** |
| 198 |
* Action: mainwp_module_yearly_renewals_before_costs_list |
| 199 |
* |
| 200 |
* Fires before the list of costs. |
| 201 |
* |
| 202 |
* @param string $tab Tab. |
| 203 |
* @param array $cost_data Cost data. |
| 204 |
* |
| 205 |
* @since 5.0.2 |
| 206 |
*/ |
| 207 |
do_action( 'mainwp_module_yearly_renewals_before_costs_list', $tab, $cost_data ); |
| 208 |
if ( empty( $lists ) ) { |
| 209 |
MainWP_UI::render_empty_element_placeholder( __( 'No Upcoming Renewals', 'mainwp' ), '<a href="admin.php?page=CostTrackerAdd">' . __( 'Add yearly renewals to see your upcoming annual costs.', 'mainwp' ) . '</a>', '<em data-emoji=":calendar_spiral:" class="medium"></em>' ); |
| 210 |
} else { |
| 211 |
?> |
| 212 |
<table class="ui stacking table" id="mainwp-yearly-renewals-table-<?php echo esc_attr( $tab ); ?>"> |
| 213 |
<thead> |
| 214 |
<tr> |
| 215 |
<th scope="col" ><?php echo esc_html__( 'Name', 'mainwp' ); ?></th> |
| 216 |
<th scope="col" ><?php echo esc_html__( 'Renews at', 'mainwp' ); ?></th> |
| 217 |
<th scope="col" class="collapsing right aligned"><?php echo esc_html__( 'Price', 'mainwp' ); ?></th> |
| 218 |
</tr> |
| 219 |
</thead> |
| 220 |
<?php |
| 221 |
foreach ( $lists as $item ) { |
| 222 |
$next_renewal = Cost_Tracker_Admin::get_next_renewal( $item->last_renewal, $item->renewal_type ); |
| 223 |
?> |
| 224 |
<tr> |
| 225 |
<td><a href="admin.php?page=CostTrackerAdd&id=<?php echo intval( $item->id ); ?>"><?php echo esc_html( $item->name ); ?></a></td> |
| 226 |
<td data-order="<?php echo esc_attr( $next_renewal ); ?>"><?php echo MainWP_Utility::format_date( $next_renewal ); //phpcs:ignore -- ok. ?></td> |
| 227 |
<td class="right aligned"><?php Cost_Tracker_Utility::cost_tracker_format_price( $item->price ); ?></td> |
| 228 |
</tr> |
| 229 |
<?php |
| 230 |
} |
| 231 |
?> |
| 232 |
</table> |
| 233 |
<script type="text/javascript"> |
| 234 |
jQuery( document ).ready( function() { |
| 235 |
jQuery( '#mainwp-yearly-renewals-table-today' ).DataTable(); |
| 236 |
jQuery( '#mainwp-yearly-renewals-table-tomorrow' ).DataTable(); |
| 237 |
jQuery( '#mainwp-yearly-renewals-table-week' ).DataTable(); |
| 238 |
jQuery( '#mainwp-yearly-renewals-table-next_week' ).DataTable(); |
| 239 |
jQuery( '#mainwp-yearly-renewals-table-month' ).DataTable(); |
| 240 |
jQuery( '#mainwp-yearly-renewals-table-next_month' ).DataTable(); |
| 241 |
jQuery( '#mainwp-yearly-renewals-table-year' ).DataTable(); |
| 242 |
jQuery( '#mainwp-yearly-renewals-table-next_year' ).DataTable(); |
| 243 |
} ); |
| 244 |
</script> |
| 245 |
<?php |
| 246 |
} |
| 247 |
/** |
| 248 |
* Action: mainwp_module_yearly_renewals_after_costs_list |
| 249 |
* |
| 250 |
* Fires after the list of costs. |
| 251 |
* |
| 252 |
* @param string $tab Tab. |
| 253 |
* @param array $cost_data Cost data. |
| 254 |
* |
| 255 |
* @since 5.0.1 |
| 256 |
*/ |
| 257 |
do_action( 'mainwp_module_yearly_renewals_after_costs_list', $tab, $cost_data ); |
| 258 |
?> |
| 259 |
</div> |
| 260 |
<?php |
| 261 |
} |
| 262 |
} |
| 263 |
|