← All changes
|
modules/cost-tracker/classes/class-cost-tracker-hooks.php
+359
-302
5.0
→
6.2.3
View file →
| @@ -8,344 +8,401 @@ | ||
| 8 | 8 | |
| 9 | 9 | namespace MainWP\Dashboard\Module\CostTracker; |
| 10 | 10 | |
| 11 | 11 | use MainWP\Dashboard\MainWP_DB_Client; |
| 12 | + | |
| 13 | +// Exit if accessed directly. | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit; | |
| 16 | +} | |
| 17 | + | |
| 12 | 18 | /** |
| 13 | 19 | * Class Cost_Tracker_Hooks |
| 14 | 20 | */ |
| 15 | 21 | class Cost_Tracker_Hooks { |
| 16 | 22 | |
| 17 | - /** | |
| 18 | - * Public static variable to hold the single instance of the class. | |
| 19 | - * | |
| 20 | - * @static | |
| 21 | - * | |
| 22 | - * @var mixed Default null | |
| 23 | - */ | |
| 24 | - public static $instance = null; | |
| 23 | + /** | |
| 24 | + * Public static variable to hold the single instance of the class. | |
| 25 | + * | |
| 26 | + * @static | |
| 27 | + * | |
| 28 | + * @var mixed Default null | |
| 29 | + */ | |
| 30 | + public static $instance = null; | |
| 25 | 31 | |
| 26 | - /** | |
| 27 | - * Public static variable to hold the clients costs. | |
| 28 | - * | |
| 29 | - * @var mixed Default null | |
| 30 | - */ | |
| 31 | - public $clients_costs = null; | |
| 32 | + /** | |
| 33 | + * Public static variable to hold the clients costs. | |
| 34 | + * | |
| 35 | + * @var mixed Default null | |
| 36 | + */ | |
| 37 | + public $clients_costs = null; | |
| 32 | 38 | |
| 33 | - /** | |
| 34 | - * Public static variable to hold the clients costs. | |
| 35 | - * | |
| 36 | - * @var mixed Default null | |
| 37 | - */ | |
| 38 | - public $clients_sites = null; | |
| 39 | + /** | |
| 40 | + * Public static variable to hold the clients costs. | |
| 41 | + * | |
| 42 | + * @var mixed Default null | |
| 43 | + */ | |
| 44 | + public $clients_sites = null; | |
| 39 | 45 | |
| 40 | - /** | |
| 41 | - * Public static variable to hold the clients costs. | |
| 42 | - * | |
| 43 | - * @var mixed Default null | |
| 44 | - */ | |
| 45 | - public $sites_costs = null; | |
| 46 | + /** | |
| 47 | + * Public static variable to hold the clients costs. | |
| 48 | + * | |
| 49 | + * @var mixed Default null | |
| 50 | + */ | |
| 51 | + public $sites_costs = null; | |
| 46 | 52 | |
| 47 | - /** | |
| 48 | - * Get Instance | |
| 49 | - * | |
| 50 | - * Creates public static instance. | |
| 51 | - * | |
| 52 | - * @static | |
| 53 | - * | |
| 54 | - * @return Cost_Tracker_Hooks | |
| 55 | - */ | |
| 56 | - public static function get_instance() { | |
| 57 | - if ( null === self::$instance ) { | |
| 58 | - self::$instance = new self(); | |
| 59 | - } | |
| 60 | - return self::$instance; | |
| 61 | - } | |
| 53 | + /** | |
| 54 | + * Get Instance | |
| 55 | + * | |
| 56 | + * Creates public static instance. | |
| 57 | + * | |
| 58 | + * @static | |
| 59 | + * | |
| 60 | + * @return Cost_Tracker_Hooks | |
| 61 | + */ | |
| 62 | + public static function get_instance() { | |
| 63 | + if ( null === static::$instance ) { | |
| 64 | + static::$instance = new self(); | |
| 65 | + } | |
| 66 | + return static::$instance; | |
| 67 | + } | |
| 62 | 68 | |
| 63 | - /** | |
| 64 | - * Constructor | |
| 65 | - * | |
| 66 | - * Runs each time the class is called. | |
| 67 | - */ | |
| 68 | - public function __construct() { | |
| 69 | - } | |
| 69 | + /** | |
| 70 | + * Constructor | |
| 71 | + * | |
| 72 | + * Runs each time the class is called. | |
| 73 | + */ | |
| 74 | + public function __construct() { | |
| 75 | + } | |
| 70 | 76 | |
| 71 | - /** | |
| 72 | - * Initiate Hooks | |
| 73 | - * | |
| 74 | - * Initiates hooks for the extension. | |
| 75 | - */ | |
| 76 | - public function init() { | |
| 77 | - add_filter( 'mainwp_widgets_screen_options', array( &$this, 'hook_widgets_screen_options' ), 10, 1 ); // for both client, site overview widgets. | |
| 78 | - add_filter( 'mainwp_header_actions_right', array( $this, 'hook_screen_options' ), 10, 2 ); | |
| 79 | - add_action( 'mainwp_clientstable_prepared_items', array( &$this, 'hook_clientstable_prepared_items' ), 10, 2 ); | |
| 80 | - add_filter( 'mainwp_clients_sitestable_getcolumns', array( &$this, 'hook_manage_clients_column' ), 10 ); | |
| 81 | - add_filter( 'mainwp_clients_sitestable_item', array( &$this, 'hook_manage_clients_display_item' ), 10 ); | |
| 82 | - add_filter( 'mainwp_sitestable_getcolumns', array( &$this, 'hook_manage_sites_column' ), 10 ); | |
| 83 | - add_action( 'mainwp_sitestable_prepared_items', array( &$this, 'hook_sitestable_prepared_items' ), 10, 2 ); | |
| 84 | - add_filter( 'mainwp_sitestable_item', array( &$this, 'hook_manage_sites_display_item' ), 10 ); | |
| 77 | + /** | |
| 78 | + * Initiate Hooks | |
| 79 | + * | |
| 80 | + * Initiates hooks for the extension. | |
| 81 | + */ | |
| 82 | + public function init() { | |
| 83 | + add_filter( 'mainwp_widgets_screen_options', array( &$this, 'hook_widgets_screen_options' ), 10, 1 ); // for both client, site overview widgets. | |
| 84 | + add_filter( 'mainwp_header_actions_right', array( $this, 'hook_screen_options' ), 10, 2 ); | |
| 85 | + add_action( 'mainwp_clientstable_prepared_items', array( &$this, 'hook_clientstable_prepared_items' ), 10, 2 ); | |
| 86 | + add_filter( 'mainwp_clients_sitestable_getcolumns', array( &$this, 'hook_manage_clients_column' ), 10 ); | |
| 87 | + add_filter( 'mainwp_clients_sitestable_item', array( &$this, 'hook_manage_clients_display_item' ), 10 ); | |
| 88 | + add_filter( 'mainwp_sitestable_getcolumns', array( &$this, 'hook_manage_sites_column' ), 10 ); | |
| 89 | + add_action( 'mainwp_sitestable_prepared_items', array( &$this, 'hook_sitestable_prepared_items' ), 10, 2 ); | |
| 90 | + add_filter( 'mainwp_sitestable_item', array( &$this, 'hook_manage_sites_display_item' ), 10 ); | |
| 85 | 91 | |
| 86 | - add_filter( 'mainwp_clients_getmetaboxes', array( &$this, 'hook_get_client_page_metaboxes' ), 10, 1 ); | |
| 87 | - add_filter( 'mainwp_clients_widgets_screen_options', array( &$this, 'hook_get_widgets_screen_options' ), 10, 1 ); | |
| 92 | + add_filter( 'mainwp_clients_getmetaboxes', array( &$this, 'hook_get_client_page_metaboxes' ), 10, 1 ); | |
| 93 | + add_filter( 'mainwp_clients_widgets_screen_options', array( &$this, 'hook_get_widgets_screen_options' ), 10, 1 ); | |
| 88 | 94 | |
| 89 | - add_filter( 'mainwp_getmetaboxes', array( &$this, 'hook_get_site_overview_page_metaboxes' ), 10, 2 ); | |
| 90 | - } | |
| 95 | + add_filter( 'mainwp_getmetaboxes', array( &$this, 'hook_get_site_overview_page_metaboxes' ), 10, 2 ); | |
| 96 | + } | |
| 91 | 97 | |
| 92 | - /** | |
| 93 | - * Widgets screen options. | |
| 94 | - * | |
| 95 | - * @param array $input Input. | |
| 96 | - * | |
| 97 | - * @return array $input Input. | |
| 98 | - */ | |
| 99 | - public function hook_widgets_screen_options( $input ) { | |
| 100 | - $input['advanced-cost-tracker-widget'] = esc_html__( 'Cost Tracker', 'mainwp' ); | |
| 101 | - return $input; | |
| 102 | - } | |
| 98 | + /** | |
| 99 | + * Widgets screen options. | |
| 100 | + * | |
| 101 | + * @param array $input Input. | |
| 102 | + * | |
| 103 | + * @return array $input Input. | |
| 104 | + */ | |
| 105 | + public function hook_widgets_screen_options( $input ) { | |
| 106 | + $input['advanced-cost-tracker-widget'] = esc_html__( 'Cost Tracker (Individual Site Operations page)', 'mainwp' ); | |
| 107 | + return $input; | |
| 108 | + } | |
| 103 | 109 | |
| 104 | - /** | |
| 105 | - * Method screen_options() | |
| 106 | - * | |
| 107 | - * Create Screen Options button. | |
| 108 | - * | |
| 109 | - * @param mixed $input Screen options button HTML. | |
| 110 | - * | |
| 111 | - * @return mixed Screen sptions button. | |
| 112 | - */ | |
| 113 | - public function hook_screen_options( $input ) { | |
| 114 | - if ( isset( $_GET['page'] ) && 'ManageCostTracker' === $_GET['page'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 115 | - $input .= '<a class="ui button basic icon" onclick="mainwp_module_cost_tracker_sites_screen_options(); return false;" data-inverted="" data-position="bottom right" href="#" target="_blank" data-tooltip="' . esc_html__( 'Page Settings', 'mainwp' ) . '"><i class="cog icon"></i></a>'; | |
| 116 | - } | |
| 117 | - return $input; | |
| 118 | - } | |
| 110 | + /** | |
| 111 | + * Method hook_screen_options() | |
| 112 | + * | |
| 113 | + * Create Screen Options button. | |
| 114 | + * | |
| 115 | + * @param mixed $input Screen options button HTML. | |
| 116 | + * | |
| 117 | + * @return mixed Screen sptions button. | |
| 118 | + */ | |
| 119 | + public function hook_screen_options( $input ) { | |
| 120 | + if ( isset( $_GET['page'] ) && 'ManageCostTracker' === $_GET['page'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 121 | + $input .= '<a class="ui button basic icon" onclick="mainwp_module_cost_tracker_sites_screen_options(); return false;" data-inverted="" data-position="bottom right" href="#" target="_blank" data-tooltip="' . esc_html__( 'Page Settings', 'mainwp' ) . '"><i class="cog icon"></i></a>'; | |
| 122 | + } | |
| 123 | + return $input; | |
| 124 | + } | |
| 119 | 125 | |
| 120 | 126 | |
| 121 | - /** | |
| 122 | - * Method hook_manage_clients_column(). | |
| 123 | - * | |
| 124 | - * Handle hook to manage sites columns. | |
| 125 | - * | |
| 126 | - * @param array $columns sites table columns. | |
| 127 | - * | |
| 128 | - * @return array $columns columns. | |
| 129 | - */ | |
| 130 | - public function hook_manage_clients_column( $columns ) { | |
| 131 | - $columns['client-cost-tracker'] = __( 'Cost', 'mainwp' ); | |
| 132 | - return $columns; | |
| 133 | - } | |
| 127 | + /** | |
| 128 | + * Method hook_manage_clients_column(). | |
| 129 | + * | |
| 130 | + * Handle hook to manage sites columns. | |
| 131 | + * | |
| 132 | + * @param array $columns sites table columns. | |
| 133 | + * | |
| 134 | + * @return array $columns columns. | |
| 135 | + */ | |
| 136 | + public function hook_manage_clients_column( $columns ) { | |
| 137 | + $columns['client-cost-tracker'] = '<span data-tooltip="30-Day cost forecast" data-position="left center" data-inverted="">' . __( 'Costs', 'mainwp' ) . '</span>'; | |
| 138 | + return $columns; | |
| 139 | + } | |
| 134 | 140 | |
| 135 | - /** | |
| 136 | - * Method hook_clientstable_prepared_items(). | |
| 137 | - * | |
| 138 | - * Handle hook to prepared manage sites items. | |
| 139 | - * | |
| 140 | - * @param array $clients sites prepared. | |
| 141 | - * @param array $clients_ids sites ids prepared. | |
| 142 | - */ | |
| 143 | - public function hook_clientstable_prepared_items( $clients, $clients_ids ) { | |
| 144 | - if ( null === $this->clients_costs ) { | |
| 145 | - $this->clients_sites = array(); | |
| 146 | - // get all sites of the $clients_ids. | |
| 147 | - $cls_sites = MainWP_DB_Client::instance()->get_websites_by_client_ids( $clients_ids ); | |
| 148 | - if ( is_array( $cls_sites ) ) { | |
| 149 | - foreach ( $cls_sites as $cls_site ) { | |
| 150 | - if ( ! empty( $cls_site->client_id ) && ! isset( $this->clients_sites[ $cls_site->client_id ] ) ) { | |
| 151 | - $this->clients_sites[ $cls_site->client_id ] = array(); | |
| 152 | - } | |
| 153 | - $this->clients_sites[ $cls_site->client_id ][] = $cls_site->id; | |
| 154 | - } | |
| 155 | - } | |
| 156 | - $this->clients_costs = Cost_Tracker_DB::get_instance()->get_cost_tracker_info_of_clients( $clients_ids, array( 'with_sites' => true ) ); | |
| 157 | - } | |
| 158 | - } | |
| 141 | + /** | |
| 142 | + * Method hook_clientstable_prepared_items(). | |
| 143 | + * | |
| 144 | + * Handle hook to prepared manage sites items. | |
| 145 | + * | |
| 146 | + * @param array $clients sites prepared. | |
| 147 | + * @param array $clients_ids sites ids prepared. | |
| 148 | + */ | |
| 149 | + public function hook_clientstable_prepared_items( $clients, $clients_ids ) { | |
| 150 | + unset( $clients ); | |
| 151 | + if ( null === $this->clients_costs ) { | |
| 152 | + $this->clients_sites = array(); | |
| 153 | + // get all sites of the $clients_ids. | |
| 154 | + $cls_sites = MainWP_DB_Client::instance()->get_websites_by_client_ids( $clients_ids ); | |
| 155 | + if ( is_array( $cls_sites ) ) { | |
| 156 | + foreach ( $cls_sites as $cls_site ) { | |
| 157 | + if ( ! empty( $cls_site->client_id ) && ! isset( $this->clients_sites[ $cls_site->client_id ] ) ) { | |
| 158 | + $this->clients_sites[ $cls_site->client_id ] = array(); | |
| 159 | + } | |
| 160 | + $this->clients_sites[ $cls_site->client_id ][] = $cls_site->id; | |
| 161 | + } | |
| 162 | + } | |
| 163 | + $this->clients_costs = Cost_Tracker_DB::get_instance()->get_all_cost_trackers_by_clients( $clients_ids, array( 'get_cost_sites' => true ) ); | |
| 164 | + } | |
| 165 | + } | |
| 159 | 166 | |
| 160 | - /** | |
| 161 | - * Manage Sites Item | |
| 162 | - * | |
| 163 | - * Adds the custom column data in the Manage Sites and Monitoring tables. | |
| 164 | - * | |
| 165 | - * @param array $item Site comlumn data. | |
| 166 | - * | |
| 167 | - * @return array $item Site comlumn data. | |
| 168 | - */ | |
| 169 | - public function hook_manage_clients_display_item( $item ) { | |
| 170 | - if ( ! is_array( $item ) || ! isset( $item['client_id'] ) ) { | |
| 171 | - return $item; | |
| 172 | - } | |
| 173 | - $client_id = $item['client_id']; | |
| 174 | - if ( ! isset( $item['client-cost-tracker'] ) ) { | |
| 175 | - $array_costs = array( | |
| 176 | - 'weekly' => 0, | |
| 177 | - 'monthly' => 0, | |
| 178 | - 'quarterly' => 0, | |
| 179 | - 'yearly' => 0, | |
| 180 | - ); | |
| 167 | + /** | |
| 168 | + * Manage Sites Item | |
| 169 | + * | |
| 170 | + * Adds the custom column data in the Manage Sites and Monitoring tables. | |
| 171 | + * | |
| 172 | + * @param array $item Site comlumn data. | |
| 173 | + * | |
| 174 | + * @return array $item Site comlumn data. | |
| 175 | + */ | |
| 176 | + public function hook_manage_clients_display_item( $item ) { //phpcs:ignore -- NOSONAR - complex. | |
| 177 | + if ( ! is_array( $item ) || ! isset( $item['client_id'] ) ) { | |
| 178 | + return $item; | |
| 179 | + } | |
| 180 | + $client_id = $item['client_id']; | |
| 181 | + if ( ! isset( $item['client-cost-tracker'] ) ) { | |
| 181 | 182 | |
| 182 | - // get all cost trackers of current client. | |
| 183 | - $client_costs = is_array( $this->clients_costs ) && isset( $this->clients_costs[ $client_id ] ) ? $this->clients_costs[ $client_id ] : array(); | |
| 184 | - if ( is_array( $client_costs ) && ! empty( $this->clients_sites[ $client_id ] ) ) { | |
| 185 | - foreach ( $client_costs as $sub ) { | |
| 186 | - if ( 'active' !== $sub->cost_status ) { | |
| 187 | - continue; | |
| 188 | - } | |
| 189 | - if ( is_array( $sub->cost_sites_ids ) ) { | |
| 190 | - $cost_val = 0; | |
| 191 | - foreach ( $sub->cost_sites_ids as $ct_siteid ) { | |
| 192 | - // if site of cost tracker in the client's sites then calculate the site's cost/price. | |
| 193 | - if ( is_array( $this->clients_sites[ $client_id ] ) && in_array( $ct_siteid, $this->clients_sites[ $client_id ] ) ) { | |
| 194 | - if ( 'single_site' === $sub->license_type ) { | |
| 195 | - $cost_val += $sub->price; | |
| 196 | - } elseif ( 'multi_site' === $sub->license_type && ! empty( $sub->count_sites ) ) { | |
| 197 | - $cost_val += $sub->price / $sub->count_sites; | |
| 198 | - } | |
| 199 | - } | |
| 200 | - } | |
| 201 | - if ( isset( $array_costs[ $sub->renewal_type ] ) ) { | |
| 202 | - $array_costs[ $sub->renewal_type ] += $cost_val; | |
| 203 | - } | |
| 204 | - } | |
| 205 | - } | |
| 206 | - } | |
| 183 | + $current_time = time(); | |
| 184 | + $upcoming1 = strtotime( gmdate( 'Y-m-d 00:00:00', $current_time ) ); | |
| 185 | + $upcoming2 = strtotime( '+1 month', $current_time ); | |
| 207 | 186 | |
| 208 | - $item['client-cost-tracker'] = Cost_Tracker_Utility::get_separated_costs_price( $array_costs ); | |
| 209 | - } | |
| 210 | - return $item; | |
| 211 | - } | |
| 187 | + $series_products_price = array(); | |
| 188 | + $total_prices = 0; | |
| 212 | 189 | |
| 190 | + // get all cost trackers of current client. | |
| 191 | + $client_costs = is_array( $this->clients_costs ) && isset( $this->clients_costs[ $client_id ] ) ? $this->clients_costs[ $client_id ] : array(); | |
| 192 | + if ( is_array( $client_costs ) && ! empty( $this->clients_sites[ $client_id ] ) ) { | |
| 213 | 193 | |
| 194 | + foreach ( $client_costs as $cost ) { | |
| 195 | + if ( 'active' !== $cost->cost_status || 'subscription' !== $cost->type ) { | |
| 196 | + continue; | |
| 197 | + } | |
| 214 | 198 | |
| 215 | - /** | |
| 216 | - * Method hook_manage_sites_column(). | |
| 217 | - * | |
| 218 | - * Handle hook to manage sites columns. | |
| 219 | - * | |
| 220 | - * @param array $columns sites table columns. | |
| 221 | - * | |
| 222 | - * @return array $columns columns. | |
| 223 | - */ | |
| 224 | - public function hook_manage_sites_column( $columns ) { | |
| 225 | - $columns['site-cost-tracker'] = esc_html__( 'Cost', 'mainwp' ); | |
| 226 | - return $columns; | |
| 227 | - } | |
| 199 | + $next_rl = $cost->next_renewal; | |
| 228 | 200 | |
| 201 | + if ( $next_rl <= $upcoming1 ) { | |
| 202 | + $next_rl = Cost_Tracker_Admin::get_next_renewal( $upcoming1, $cost->renewal_type, false ); | |
| 203 | + } | |
| 229 | 204 | |
| 230 | - /** | |
| 231 | - * Method hook_sitestable_prepared_items(). | |
| 232 | - * | |
| 233 | - * Handle hook to prepared manage sites items. | |
| 234 | - * | |
| 235 | - * @param array $websites sites prepared. | |
| 236 | - * @param array $site_ids sites ids prepared. | |
| 237 | - */ | |
| 238 | - public function hook_sitestable_prepared_items( $websites, $site_ids ) { | |
| 239 | - if ( null === $this->sites_costs ) { | |
| 240 | - $this->sites_costs = Cost_Tracker_DB::get_instance()->get_cost_trackers_info_of_sites( $site_ids ); | |
| 241 | - } | |
| 242 | - } | |
| 205 | + $next_price = 0; | |
| 206 | + while ( $next_rl <= $upcoming2 ) { | |
| 243 | 207 | |
| 244 | - /** | |
| 245 | - * Manage Sites Item | |
| 246 | - * | |
| 247 | - * Adds the custom column data in the Manage Sites and Monitoring tables. | |
| 248 | - * | |
| 249 | - * @param array $item Site comlumn data. | |
| 250 | - * | |
| 251 | - * @return array $item Site comlumn data. | |
| 252 | - */ | |
| 253 | - public function hook_manage_sites_display_item( $item ) { | |
| 254 | - if ( ! is_array( $item ) || ! isset( $item['id'] ) ) { | |
| 255 | - return $item; | |
| 256 | - } | |
| 208 | + if ( $next_rl > $upcoming1 && $next_rl <= $upcoming2 ) { | |
| 209 | + $next_price = $cost->price; | |
| 210 | + } | |
| 257 | 211 | |
| 258 | - $item_id = $item['id']; | |
| 212 | + $cost_val = 0; | |
| 259 | 213 | |
| 260 | - if ( ! isset( $item['site-cost-tracker'] ) ) { | |
| 261 | - $array_costs = array( | |
| 262 | - 'weekly' => 0, | |
| 263 | - 'monthly' => 0, | |
| 264 | - 'quarterly' => 0, | |
| 265 | - 'yearly' => 0, | |
| 266 | - ); | |
| 267 | - $site_costs = is_array( $this->sites_costs ) && isset( $this->sites_costs[ $item_id ] ) ? $this->sites_costs[ $item_id ] : array(); | |
| 268 | - if ( is_array( $site_costs ) ) { | |
| 269 | - foreach ( $site_costs as $sub ) { | |
| 270 | - if ( 'active' !== $sub->cost_status ) { | |
| 271 | - continue; | |
| 272 | - } | |
| 273 | - $price = 0; | |
| 274 | - if ( 'single_site' === $sub->license_type ) { | |
| 275 | - $price = $sub->price; | |
| 276 | - } elseif ( 'multi_site' === $sub->license_type && ! empty( $sub->count_sites ) ) { | |
| 277 | - $price += $sub->price / $sub->count_sites; | |
| 278 | - } | |
| 279 | - if ( isset( $array_costs[ $sub->renewal_type ] ) ) { | |
| 280 | - $array_costs[ $sub->renewal_type ] += $price; | |
| 281 | - } | |
| 282 | - } | |
| 283 | - } | |
| 284 | - $item['site-cost-tracker'] = Cost_Tracker_Utility::get_separated_costs_price( $array_costs ); | |
| 285 | - } | |
| 286 | - return $item; | |
| 287 | - } | |
| 214 | + if ( ! empty( $next_price ) && is_array( $cost->cost_sites_ids ) ) { | |
| 215 | + foreach ( $cost->cost_sites_ids as $ct_siteid ) { | |
| 216 | + // if site of cost tracker in the client's sites then calculate the site's cost/price. | |
| 217 | + if ( is_array( $this->clients_sites[ $client_id ] ) && in_array( $ct_siteid, $this->clients_sites[ $client_id ] ) ) { | |
| 218 | + if ( 'single_site' === $cost->license_type ) { | |
| 219 | + $cost_val += $cost->price; | |
| 220 | + } elseif ( 'multi_site' === $cost->license_type && ! empty( $cost->count_sites ) ) { | |
| 221 | + $cost_val += $cost->price / $cost->count_sites; | |
| 222 | + } | |
| 223 | + } | |
| 224 | + } | |
| 225 | + } | |
| 288 | 226 | |
| 227 | + if ( ! empty( $cost_val ) ) { | |
| 228 | + if ( ! isset( $series_products_price[ $cost->id ] ) ) { | |
| 229 | + $series_products_price[ $cost->id ] = 0; | |
| 230 | + } | |
| 231 | + $series_products_price[ $cost->id ] += $cost_val; | |
| 232 | + $total_prices += $cost_val; | |
| 233 | + } | |
| 289 | 234 | |
| 235 | + $next_rl = Cost_Tracker_Admin::get_next_renewal( $next_rl, $cost->renewal_type, false ); | |
| 236 | + $next_price = 0; | |
| 237 | + } | |
| 238 | + } | |
| 239 | + } | |
| 290 | 240 | |
| 291 | - /** | |
| 292 | - * Widgets screen options. | |
| 293 | - * | |
| 294 | - * @param array $input Input. | |
| 295 | - * | |
| 296 | - * @return array $input Input. | |
| 297 | - */ | |
| 298 | - public function hook_get_widgets_screen_options( $input ) { | |
| 299 | - $input['advanced-cost-tracker-costs'] = __( 'Cost Tracker', 'mainwp' ); | |
| 300 | - return $input; | |
| 301 | - } | |
| 241 | + $params = ''; | |
| 242 | + $selected_ids = array_keys( $series_products_price ); | |
| 243 | + if ( ! empty( $selected_ids ) ) { | |
| 244 | + $params = '&selected_ids=' . implode( ',', $selected_ids ) . '&client_id=' . intval( $client_id ); | |
| 245 | + } | |
| 246 | + $item['client-cost-tracker'] = '<a class="ui mini basic grey button" href="admin.php?page=ManageCostTracker' . $params . '" ><i class="file invoice dollar icon"></i>' . Cost_Tracker_Utility::cost_tracker_format_price( $total_prices, true ) . '</a>'; | |
| 302 | 247 | |
| 248 | + } | |
| 249 | + return $item; | |
| 250 | + } | |
| 303 | 251 | |
| 304 | - /** | |
| 305 | - * Method hook_get_client_page_metaboxes(). | |
| 306 | - * | |
| 307 | - * Hook Clients get metaboxes. | |
| 308 | - * | |
| 309 | - * @param array $widgets Client widgets. | |
| 310 | - */ | |
| 311 | - public function hook_get_client_page_metaboxes( $widgets ) { | |
| 312 | - $widgets[] = array( | |
| 313 | - 'id' => 'cost-tracker-costs', | |
| 314 | - 'callback' => array( Cost_Tracker_Clients_Widget::instance(), 'callback_render_costs_widget' ), | |
| 315 | - ); | |
| 316 | - return $widgets; | |
| 317 | - } | |
| 318 | 252 | |
| 319 | - /** | |
| 320 | - * Widgets screen options. | |
| 321 | - * | |
| 322 | - * @param array $input Input. | |
| 323 | - * | |
| 324 | - * @return array $input Input. | |
| 325 | - */ | |
| 326 | - public function hook_get_site_overview_screen_options( $input ) { | |
| 327 | - $input['advanced-cost-tracker-costs'] = __( 'Cost Tracker', 'mainwp' ); | |
| 328 | - return $input; | |
| 329 | - } | |
| 330 | 253 | |
| 254 | + /** | |
| 255 | + * Method hook_manage_sites_column(). | |
| 256 | + * | |
| 257 | + * Handle hook to manage sites columns. | |
| 258 | + * | |
| 259 | + * @param array $columns sites table columns. | |
| 260 | + * | |
| 261 | + * @return array $columns columns. | |
| 262 | + */ | |
| 263 | + public function hook_manage_sites_column( $columns ) { | |
| 264 | + $columns['site-cost-tracker'] = '<span data-tooltip="30-Day cost forecast" data-position="left center" data-inverted="">' . __( 'Costs', 'mainwp' ) . '</span>'; | |
| 265 | + return $columns; | |
| 266 | + } | |
| 331 | 267 | |
| 332 | - /** | |
| 333 | - * Method hook_get_client_page_metaboxes(). | |
| 334 | - * | |
| 335 | - * Hook Clients get metaboxes. | |
| 336 | - * | |
| 337 | - * @param array $widgets Client widgets. | |
| 338 | - * @param int $dashboard_siteid Site Id. | |
| 339 | - * | |
| 340 | - * @return array $widgets Widgets data. | |
| 341 | - */ | |
| 342 | - public function hook_get_site_overview_page_metaboxes( $widgets, $dashboard_siteid = 0 ) { | |
| 343 | - if ( ! empty( $dashboard_siteid ) ) { | |
| 344 | - $widgets[] = array( | |
| 345 | - 'id' => 'cost-tracker-widget', | |
| 346 | - 'callback' => array( Cost_Tracker_Sites_Widget::instance(), 'callback_render_costs_widget' ), | |
| 347 | - ); | |
| 348 | - } | |
| 349 | - return $widgets; | |
| 350 | - } | |
| 268 | + | |
| 269 | + /** | |
| 270 | + * Method hook_sitestable_prepared_items(). | |
| 271 | + * | |
| 272 | + * Handle hook to prepared manage sites items. | |
| 273 | + * | |
| 274 | + * @param array $websites sites prepared. | |
| 275 | + * @param array $site_ids sites ids prepared. | |
| 276 | + */ | |
| 277 | + public function hook_sitestable_prepared_items( $websites, $site_ids ) { | |
| 278 | + unset( $websites ); | |
| 279 | + if ( null === $this->sites_costs ) { | |
| 280 | + $this->sites_costs = Cost_Tracker_DB::get_instance()->get_all_cost_trackers_by_sites( $site_ids ); | |
| 281 | + } | |
| 282 | + } | |
| 283 | + | |
| 284 | + /** | |
| 285 | + * Manage Sites Item | |
| 286 | + * | |
| 287 | + * Adds the custom column data in the Manage Sites and Monitoring tables. | |
| 288 | + * | |
| 289 | + * @param array $item Site comlumn data. | |
| 290 | + * | |
| 291 | + * @return array $item Site comlumn data. | |
| 292 | + */ | |
| 293 | + public function hook_manage_sites_display_item( $item ) { //phpcs:ignore -- NOSONAR - complex. | |
| 294 | + if ( ! is_array( $item ) || ! isset( $item['id'] ) ) { | |
| 295 | + return $item; | |
| 296 | + } | |
| 297 | + | |
| 298 | + $item_id = $item['id']; | |
| 299 | + | |
| 300 | + if ( ! isset( $item['site-cost-tracker'] ) ) { | |
| 301 | + | |
| 302 | + $current_time = time(); | |
| 303 | + $upcoming1 = strtotime( gmdate( 'Y-m-d 00:00:00', $current_time ) ); | |
| 304 | + $upcoming2 = strtotime( '+1 month', $current_time ); | |
| 305 | + | |
| 306 | + $series_products_price = array(); | |
| 307 | + $total_prices = 0; | |
| 308 | + $site_costs = is_array( $this->sites_costs ) && isset( $this->sites_costs[ $item_id ] ) ? $this->sites_costs[ $item_id ] : array(); | |
| 309 | + if ( is_array( $site_costs ) ) { | |
| 310 | + foreach ( $site_costs as $cost ) { | |
| 311 | + if ( 'active' !== $cost->cost_status || 'subscription' !== $cost->type ) { | |
| 312 | + continue; | |
| 313 | + } | |
| 314 | + $next_rl = $cost->next_renewal; | |
| 315 | + if ( $next_rl <= $upcoming1 ) { | |
| 316 | + $next_rl = Cost_Tracker_Admin::get_next_renewal( $upcoming1, $cost->renewal_type, false ); | |
| 317 | + } | |
| 318 | + $next_price = 0; | |
| 319 | + while ( $next_rl <= $upcoming2 ) { | |
| 320 | + if ( $next_rl > $upcoming1 && $next_rl <= $upcoming2 ) { | |
| 321 | + $next_price = $cost->price; | |
| 322 | + } | |
| 323 | + | |
| 324 | + $price = 0; | |
| 325 | + if ( ! empty( $next_price ) ) { | |
| 326 | + if ( 'single_site' === $cost->license_type ) { | |
| 327 | + $price = $cost->price; | |
| 328 | + } elseif ( 'multi_site' === $cost->license_type && ! empty( $cost->count_sites ) ) { | |
| 329 | + $price = $cost->price / $cost->count_sites; | |
| 330 | + } | |
| 331 | + } | |
| 332 | + | |
| 333 | + if ( ! empty( $price ) ) { | |
| 334 | + | |
| 335 | + if ( ! isset( $series_products_price[ $cost->id ] ) ) { | |
| 336 | + $series_products_price[ $cost->id ] = 0; | |
| 337 | + } | |
| 338 | + | |
| 339 | + $series_products_price[ $cost->id ] += $price; | |
| 340 | + $total_prices += $price; | |
| 341 | + } | |
| 342 | + | |
| 343 | + $next_rl = Cost_Tracker_Admin::get_next_renewal( $next_rl, $cost->renewal_type, false ); | |
| 344 | + $next_price = 0; | |
| 345 | + } | |
| 346 | + } | |
| 347 | + } | |
| 348 | + $params = ''; | |
| 349 | + $selected_ids = array_keys( $series_products_price ); | |
| 350 | + if ( ! empty( $selected_ids ) ) { | |
| 351 | + $params = '&selected_ids=' . implode( ',', $selected_ids ) . '&site_id=' . intval( $item_id ); | |
| 352 | + } | |
| 353 | + $item['site-cost-tracker'] = '<a class="ui mini basic grey button" href="admin.php?page=ManageCostTracker' . $params . '" ><i class="file invoice dollar icon"></i>' . Cost_Tracker_Utility::cost_tracker_format_price( $total_prices, true ) . '</a>'; | |
| 354 | + } | |
| 355 | + | |
| 356 | + return $item; | |
| 357 | + } | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + /** | |
| 362 | + * Widgets screen options. | |
| 363 | + * | |
| 364 | + * @param array $input Input. | |
| 365 | + * | |
| 366 | + * @return array $input Input. | |
| 367 | + */ | |
| 368 | + public function hook_get_widgets_screen_options( $input ) { | |
| 369 | + $input['advanced-cost-tracker-costs'] = __( 'Cost Tracker', 'mainwp' ); | |
| 370 | + return $input; | |
| 371 | + } | |
| 372 | + | |
| 373 | + | |
| 374 | + /** | |
| 375 | + * Method hook_get_client_page_metaboxes(). | |
| 376 | + * | |
| 377 | + * Hook Clients get metaboxes. | |
| 378 | + * | |
| 379 | + * @param array $widgets Client widgets. | |
| 380 | + */ | |
| 381 | + public function hook_get_client_page_metaboxes( $widgets ) { | |
| 382 | + $widgets[] = array( | |
| 383 | + 'id' => 'cost-tracker-costs', | |
| 384 | + 'callback' => array( Cost_Tracker_Clients_Widget::instance(), 'callback_render_costs_widget' ), | |
| 385 | + ); | |
| 386 | + return $widgets; | |
| 387 | + } | |
| 388 | + | |
| 389 | + /** | |
| 390 | + * Method hook_get_client_page_metaboxes(). | |
| 391 | + * | |
| 392 | + * Hook Clients get metaboxes. | |
| 393 | + * | |
| 394 | + * @param array $widgets Client widgets. | |
| 395 | + * @param int $dashboard_siteid Site Id. | |
| 396 | + * | |
| 397 | + * @return array $widgets Widgets data. | |
| 398 | + */ | |
| 399 | + public function hook_get_site_overview_page_metaboxes( $widgets, $dashboard_siteid = 0 ) { | |
| 400 | + if ( ! empty( $dashboard_siteid ) ) { | |
| 401 | + $widgets[] = array( | |
| 402 | + 'id' => 'cost-tracker-widget', | |
| 403 | + 'callback' => array( Cost_Tracker_Sites_Widget::instance(), 'callback_render_costs_widget' ), | |
| 404 | + ); | |
| 405 | + } | |
| 406 | + return $widgets; | |
| 407 | + } | |
| 351 | 408 | } |