PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.2
6.2.2 6.2.1 6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 All 155 releases
mainwp / modules / cost-tracker / classes / class-cost-tracker-hooks.php

class-cost-tracker-hooks.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.2.2, at modules/cost-tracker/classes/class-cost-tracker-hooks.php

409 lines 15.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Module Cost Tracker Hooks class.
4 *
5 * @package MainWP\Dashboard
6 * @version 4.6
7 */
8
9 namespace MainWP\Dashboard\Module\CostTracker;
10
11 use MainWP\Dashboard\MainWP_DB_Client;
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * Class Cost_Tracker_Hooks
20 */
21 class Cost_Tracker_Hooks {
22
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;
31
32 /**
33 * Public static variable to hold the clients costs.
34 *
35 * @var mixed Default null
36 */
37 public $clients_costs = null;
38
39 /**
40 * Public static variable to hold the clients costs.
41 *
42 * @var mixed Default null
43 */
44 public $clients_sites = null;
45
46 /**
47 * Public static variable to hold the clients costs.
48 *
49 * @var mixed Default null
50 */
51 public $sites_costs = null;
52
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 }
68
69 /**
70 * Constructor
71 *
72 * Runs each time the class is called.
73 */
74 public function __construct() {
75 }
76
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 );
91
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 );
94
95 add_filter( 'mainwp_getmetaboxes', array( &$this, 'hook_get_site_overview_page_metaboxes' ), 10, 2 );
96 }
97
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 }
109
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 }
125
126
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 }
140
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 }
166
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'] ) ) {
182
183 $current_time = time();
184 $upcoming1 = strtotime( gmdate( 'Y-m-d 00:00:00', $current_time ) );
185 $upcoming2 = strtotime( '+1 month', $current_time );
186
187 $series_products_price = array();
188 $total_prices = 0;
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 ] ) ) {
193
194 foreach ( $client_costs as $cost ) {
195 if ( 'active' !== $cost->cost_status || 'subscription' !== $cost->type ) {
196 continue;
197 }
198
199 $next_rl = $cost->next_renewal;
200
201 if ( $next_rl <= $upcoming1 ) {
202 $next_rl = Cost_Tracker_Admin::get_next_renewal( $upcoming1, $cost->renewal_type, false );
203 }
204
205 $next_price = 0;
206 while ( $next_rl <= $upcoming2 ) {
207
208 if ( $next_rl > $upcoming1 && $next_rl <= $upcoming2 ) {
209 $next_price = $cost->price;
210 }
211
212 $cost_val = 0;
213
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 }
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 }
234
235 $next_rl = Cost_Tracker_Admin::get_next_renewal( $next_rl, $cost->renewal_type, false );
236 $next_price = 0;
237 }
238 }
239 }
240
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>';
247
248 }
249 return $item;
250 }
251
252
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 }
267
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 }
408 }
409