';
}
return $input;
}
/**
* Method hook_manage_clients_column().
*
* Handle hook to manage sites columns.
*
* @param array $columns sites table columns.
*
* @return array $columns columns.
*/
public function hook_manage_clients_column( $columns ) {
$columns['client-cost-tracker'] = '' . __( 'Costs', 'mainwp' ) . '';
return $columns;
}
/**
* Method hook_clientstable_prepared_items().
*
* Handle hook to prepared manage sites items.
*
* @param array $clients sites prepared.
* @param array $clients_ids sites ids prepared.
*/
public function hook_clientstable_prepared_items( $clients, $clients_ids ) {
unset( $clients );
if ( null === $this->clients_costs ) {
$this->clients_sites = array();
// get all sites of the $clients_ids.
$cls_sites = MainWP_DB_Client::instance()->get_websites_by_client_ids( $clients_ids );
if ( is_array( $cls_sites ) ) {
foreach ( $cls_sites as $cls_site ) {
if ( ! empty( $cls_site->client_id ) && ! isset( $this->clients_sites[ $cls_site->client_id ] ) ) {
$this->clients_sites[ $cls_site->client_id ] = array();
}
$this->clients_sites[ $cls_site->client_id ][] = $cls_site->id;
}
}
$this->clients_costs = Cost_Tracker_DB::get_instance()->get_all_cost_trackers_by_clients( $clients_ids, array( 'get_cost_sites' => true ) );
}
}
/**
* Manage Sites Item
*
* Adds the custom column data in the Manage Sites and Monitoring tables.
*
* @param array $item Site comlumn data.
*
* @return array $item Site comlumn data.
*/
public function hook_manage_clients_display_item( $item ) { //phpcs:ignore -- NOSONAR - complex.
if ( ! is_array( $item ) || ! isset( $item['client_id'] ) ) {
return $item;
}
$client_id = $item['client_id'];
if ( ! isset( $item['client-cost-tracker'] ) ) {
$current_time = time();
$upcoming1 = strtotime( gmdate( 'Y-m-d 00:00:00', $current_time ) );
$upcoming2 = strtotime( '+1 month', $current_time );
$series_products_price = array();
$total_prices = 0;
// get all cost trackers of current client.
$client_costs = is_array( $this->clients_costs ) && isset( $this->clients_costs[ $client_id ] ) ? $this->clients_costs[ $client_id ] : array();
if ( is_array( $client_costs ) && ! empty( $this->clients_sites[ $client_id ] ) ) {
foreach ( $client_costs as $cost ) {
if ( 'active' !== $cost->cost_status || 'subscription' !== $cost->type ) {
continue;
}
$next_rl = $cost->next_renewal;
if ( $next_rl <= $upcoming1 ) {
$next_rl = Cost_Tracker_Admin::get_next_renewal( $upcoming1, $cost->renewal_type, false );
}
$next_price = 0;
while ( $next_rl <= $upcoming2 ) {
if ( $next_rl > $upcoming1 && $next_rl <= $upcoming2 ) {
$next_price = $cost->price;
}
$cost_val = 0;
if ( ! empty( $next_price ) && is_array( $cost->cost_sites_ids ) ) {
foreach ( $cost->cost_sites_ids as $ct_siteid ) {
// if site of cost tracker in the client's sites then calculate the site's cost/price.
if ( is_array( $this->clients_sites[ $client_id ] ) && in_array( $ct_siteid, $this->clients_sites[ $client_id ] ) ) {
if ( 'single_site' === $cost->license_type ) {
$cost_val += $cost->price;
} elseif ( 'multi_site' === $cost->license_type && ! empty( $cost->count_sites ) ) {
$cost_val += $cost->price / $cost->count_sites;
}
}
}
}
if ( ! empty( $cost_val ) ) {
if ( ! isset( $series_products_price[ $cost->id ] ) ) {
$series_products_price[ $cost->id ] = 0;
}
$series_products_price[ $cost->id ] += $cost_val;
$total_prices += $cost_val;
}
$next_rl = Cost_Tracker_Admin::get_next_renewal( $next_rl, $cost->renewal_type, false );
$next_price = 0;
}
}
}
$params = '';
$selected_ids = array_keys( $series_products_price );
if ( ! empty( $selected_ids ) ) {
$params = '&selected_ids=' . implode( ',', $selected_ids ) . '&client_id=' . intval( $client_id );
}
$item['client-cost-tracker'] = '' . Cost_Tracker_Utility::cost_tracker_format_price( $total_prices, true ) . '';
}
return $item;
}
/**
* Method hook_manage_sites_column().
*
* Handle hook to manage sites columns.
*
* @param array $columns sites table columns.
*
* @return array $columns columns.
*/
public function hook_manage_sites_column( $columns ) {
$columns['site-cost-tracker'] = '' . __( 'Costs', 'mainwp' ) . '';
return $columns;
}
/**
* Method hook_sitestable_prepared_items().
*
* Handle hook to prepared manage sites items.
*
* @param array $websites sites prepared.
* @param array $site_ids sites ids prepared.
*/
public function hook_sitestable_prepared_items( $websites, $site_ids ) {
unset( $websites );
if ( null === $this->sites_costs ) {
$this->sites_costs = Cost_Tracker_DB::get_instance()->get_all_cost_trackers_by_sites( $site_ids );
}
}
/**
* Manage Sites Item
*
* Adds the custom column data in the Manage Sites and Monitoring tables.
*
* @param array $item Site comlumn data.
*
* @return array $item Site comlumn data.
*/
public function hook_manage_sites_display_item( $item ) { //phpcs:ignore -- NOSONAR - complex.
if ( ! is_array( $item ) || ! isset( $item['id'] ) ) {
return $item;
}
$item_id = $item['id'];
if ( ! isset( $item['site-cost-tracker'] ) ) {
$current_time = time();
$upcoming1 = strtotime( gmdate( 'Y-m-d 00:00:00', $current_time ) );
$upcoming2 = strtotime( '+1 month', $current_time );
$series_products_price = array();
$total_prices = 0;
$site_costs = is_array( $this->sites_costs ) && isset( $this->sites_costs[ $item_id ] ) ? $this->sites_costs[ $item_id ] : array();
if ( is_array( $site_costs ) ) {
foreach ( $site_costs as $cost ) {
if ( 'active' !== $cost->cost_status || 'subscription' !== $cost->type ) {
continue;
}
$next_rl = $cost->next_renewal;
if ( $next_rl <= $upcoming1 ) {
$next_rl = Cost_Tracker_Admin::get_next_renewal( $upcoming1, $cost->renewal_type, false );
}
$next_price = 0;
while ( $next_rl <= $upcoming2 ) {
if ( $next_rl > $upcoming1 && $next_rl <= $upcoming2 ) {
$next_price = $cost->price;
}
$price = 0;
if ( ! empty( $next_price ) ) {
if ( 'single_site' === $cost->license_type ) {
$price = $cost->price;
} elseif ( 'multi_site' === $cost->license_type && ! empty( $cost->count_sites ) ) {
$price = $cost->price / $cost->count_sites;
}
}
if ( ! empty( $price ) ) {
if ( ! isset( $series_products_price[ $cost->id ] ) ) {
$series_products_price[ $cost->id ] = 0;
}
$series_products_price[ $cost->id ] += $price;
$total_prices += $price;
}
$next_rl = Cost_Tracker_Admin::get_next_renewal( $next_rl, $cost->renewal_type, false );
$next_price = 0;
}
}
}
$params = '';
$selected_ids = array_keys( $series_products_price );
if ( ! empty( $selected_ids ) ) {
$params = '&selected_ids=' . implode( ',', $selected_ids ) . '&site_id=' . intval( $item_id );
}
$item['site-cost-tracker'] = '' . Cost_Tracker_Utility::cost_tracker_format_price( $total_prices, true ) . '';
}
return $item;
}
/**
* Widgets screen options.
*
* @param array $input Input.
*
* @return array $input Input.
*/
public function hook_get_widgets_screen_options( $input ) {
$input['advanced-cost-tracker-costs'] = __( 'Cost Tracker', 'mainwp' );
return $input;
}
/**
* Method hook_get_client_page_metaboxes().
*
* Hook Clients get metaboxes.
*
* @param array $widgets Client widgets.
*/
public function hook_get_client_page_metaboxes( $widgets ) {
$widgets[] = array(
'id' => 'cost-tracker-costs',
'callback' => array( Cost_Tracker_Clients_Widget::instance(), 'callback_render_costs_widget' ),
);
return $widgets;
}
/**
* Method hook_get_client_page_metaboxes().
*
* Hook Clients get metaboxes.
*
* @param array $widgets Client widgets.
* @param int $dashboard_siteid Site Id.
*
* @return array $widgets Widgets data.
*/
public function hook_get_site_overview_page_metaboxes( $widgets, $dashboard_siteid = 0 ) {
if ( ! empty( $dashboard_siteid ) ) {
$widgets[] = array(
'id' => 'cost-tracker-widget',
'callback' => array( Cost_Tracker_Sites_Widget::instance(), 'callback_render_costs_widget' ),
);
}
return $widgets;
}
}