Use new code style

This commit is contained in:
David Stone
2025-02-07 19:02:33 -07:00
parent 0181024ae1
commit 8433379d90
672 changed files with 37107 additions and 45249 deletions

View File

@ -9,8 +9,8 @@
// Exit if accessed directly
defined('ABSPATH') || exit;
use \WP_Ultimo\Database\Payments\Payment_Status;
use \WP_Ultimo\Database\Memberships\Membership_Status;
use WP_Ultimo\Database\Payments\Payment_Status;
use WP_Ultimo\Database\Memberships\Membership_Status;
/**
* Calculates the Monthly Recurring Revenue of the network.
@ -22,22 +22,21 @@ function wu_calculate_mrr() {
$total_mrr = 0;
$memberships = wu_get_memberships(array(
'recurring' => true,
'status__in' => array(
Membership_Status::ACTIVE,
),
));
$memberships = wu_get_memberships(
array(
'recurring' => true,
'status__in' => array(
Membership_Status::ACTIVE,
),
)
);
foreach ($memberships as $membership) {
$recurring_amount = $membership->get_amount();
if (!$membership->is_recurring()) {
if ( ! $membership->is_recurring()) {
continue;
} // end if;
}
$duration = $membership->get_duration() ? $membership->get_duration() : 1;
@ -48,12 +47,10 @@ function wu_calculate_mrr() {
$mrr = $recurring_amount / ($duration * $normalized_duration_unit);
$total_mrr += $mrr;
} // end foreach;
}
return $total_mrr;
} // end wu_calculate_mrr;
}
/**
* Converts the duration unit strings such as 'day', 'year' and such into
@ -69,26 +66,25 @@ function wu_convert_duration_unit_to_month($duration_unit) {
$months = 1;
switch ($duration_unit) {
case 'day':
$months = 1 / 30;
break;
case 'week':
$months = 1 / 4;
break;
case 'month':
$months = 1;
break;
case 'year':
$months = 12;
break;
default:
$months = $months;
break;
} // end switch;
case 'day':
$months = 1 / 30;
break;
case 'week':
$months = 1 / 4;
break;
case 'month':
$months = 1;
break;
case 'year':
$months = 12;
break;
default:
$months = $months;
break;
}
return $months;
} // end wu_convert_duration_unit_to_month;
}
/**
* Calculates the Annual Recurring Revenue.
@ -101,8 +97,7 @@ function wu_convert_duration_unit_to_month($duration_unit) {
function wu_calculate_arr() {
return wu_calculate_mrr() * 12;
} // end wu_calculate_arr;
}
/**
* Calculates the total revenue.
@ -128,32 +123,25 @@ function wu_calculate_revenue($start_date = false, $end_date = false, $inclusive
);
if ($start_date) {
$query_args['date_query']['column'] = 'date_created';
$query_args['date_query']['after'] = $start_date;
$query_args['date_query']['inclusive'] = $inclusive;
} // end if;
}
if ($end_date) {
$query_args['date_query']['column'] = 'date_created';
$query_args['date_query']['before'] = $end_date;
$query_args['date_query']['inclusive'] = $inclusive;
} // end if;
}
$payments = wu_get_payments($query_args);
foreach ($payments as $payment) {
$total_revenue += (float) $payment->total;
} // end foreach;
}
return $total_revenue;
} // end wu_calculate_revenue;
}
/**
* Calculates the total refunds.
@ -179,32 +167,25 @@ function wu_calculate_refunds($start_date = false, $end_date = false, $inclusive
);
if ($start_date) {
$query_args['date_query']['column'] = 'date_created';
$query_args['date_query']['after'] = $start_date;
$query_args['date_query']['inclusive'] = $inclusive;
} // end if;
}
if ($end_date) {
$query_args['date_query']['column'] = 'date_created';
$query_args['date_query']['before'] = $end_date;
$query_args['date_query']['inclusive'] = $inclusive;
} // end if;
}
$payments = wu_get_payments($query_args);
foreach ($payments as $payment) {
$total_revenue += -(float) $payment->refund_total;
} // end foreach;
}
return $total_revenue;
} // end wu_calculate_refunds;
}
/**
* Calculates the taxes collected grouped by the rate.
@ -223,18 +204,14 @@ function wu_calculate_taxes_by_rate($start_date = false, $end_date = false, $inc
);
if ($start_date) {
$query_args['date_query']['after'] = $start_date;
$query_args['date_query']['inclusive'] = $inclusive;
} // end if;
}
if ($end_date) {
$query_args['date_query']['before'] = $end_date;
$query_args['date_query']['inclusive'] = $inclusive;
} // end if;
}
$order = 0;
@ -243,22 +220,17 @@ function wu_calculate_taxes_by_rate($start_date = false, $end_date = false, $inc
$line_items_groups = \WP_Ultimo\Checkout\Line_Item::get_line_items($query_args);
foreach ($line_items_groups as $line_items_group) {
$order++;
++$order;
foreach ($line_items_group as $line_item) {
$tax_name = $line_item->get_tax_label();
if ($line_item->get_tax_rate() <= 0) {
continue;
}
} // end if;
if (!wu_get_isset($taxes_paid_list, $tax_name)) {
$taxes_paid_list[$tax_name] = array(
if ( ! wu_get_isset($taxes_paid_list, $tax_name)) {
$taxes_paid_list[ $tax_name ] = array(
'title' => $tax_name,
'country' => '',
'state' => '',
@ -266,21 +238,15 @@ function wu_calculate_taxes_by_rate($start_date = false, $end_date = false, $inc
'tax_rate' => $line_item->get_tax_rate(),
'tax_total' => $line_item->get_tax_total(),
);
} else {
$taxes_paid_list[$tax_name]['tax_total'] += $line_item->get_tax_total();
$taxes_paid_list[$tax_name]['order_count'] += $order;
} // end if;
} // end foreach;
} // end foreach;
$taxes_paid_list[ $tax_name ]['tax_total'] += $line_item->get_tax_total();
$taxes_paid_list[ $tax_name ]['order_count'] += $order;
}
}
}
return $taxes_paid_list;
} // end wu_calculate_taxes_by_rate;
}
/**
* Aggregate financial data on a per product basis.
@ -300,18 +266,14 @@ function wu_calculate_financial_data_by_product($start_date = false, $end_date =
);
if ($start_date) {
$query_args['date_query']['after'] = $start_date;
$query_args['date_query']['inclusive'] = $inclusive;
} // end if;
}
if ($end_date) {
$query_args['date_query']['before'] = $end_date;
$query_args['date_query']['inclusive'] = $inclusive;
} // end if;
}
$line_items_groups = \WP_Ultimo\Checkout\Line_Item::get_line_items($query_args);
@ -320,43 +282,32 @@ function wu_calculate_financial_data_by_product($start_date = false, $end_date =
$products = wu_get_products();
foreach ($products as $product) {
$data[$product->get_id()] = array(
$data[ $product->get_id() ] = array(
'label' => $product->get_name(),
'revenue' => 0,
);
} // end foreach;
}
foreach ($line_items_groups as $line_items_group) {
foreach ($line_items_group as $line_item) {
$product_id = $line_item->get_product_id();
if (empty($product_id)) {
continue;
}
} // end if;
if (!wu_get_isset($data, $product_id)) {
if ( ! wu_get_isset($data, $product_id)) {
continue;
}
} // end if;
$data[$product_id]['revenue'] += $line_item->get_total();
} // end foreach;
} // end foreach;
$data[ $product_id ]['revenue'] += $line_item->get_total();
}
}
uasort($data, fn($a, $b) => wu_sort_by_column($b, $a, 'revenue'));
return $data;
} // end wu_calculate_financial_data_by_product;
}
/**
* Calculates the taxes collected grouped by date.
@ -375,18 +326,14 @@ function wu_calculate_taxes_by_day($start_date = false, $end_date = false, $incl
);
if ($start_date) {
$query_args['date_query']['after'] = $start_date;
$query_args['date_query']['inclusive'] = $inclusive;
} // end if;
}
if ($end_date) {
$query_args['date_query']['before'] = $end_date;
$query_args['date_query']['inclusive'] = $inclusive;
} // end if;
}
$line_items_groups = \WP_Ultimo\Checkout\Line_Item::get_line_items($query_args);
@ -401,47 +348,36 @@ function wu_calculate_taxes_by_day($start_date = false, $end_date = false, $incl
$days = array_reverse(iterator_to_array($period));
foreach ($days as $day_datetime) {
$data[$day_datetime->format('Y-m-d')] = array(
$data[ $day_datetime->format('Y-m-d') ] = array(
'order_count' => 0,
'total' => 0,
'tax_total' => 0,
'net_profit' => 0,
);
} // end foreach;
}
foreach ($line_items_groups as $line_items_group) {
foreach ($line_items_group as $line_item) {
$date = gmdate('Y-m-d', strtotime((string) $line_item->date_created));
if (!wu_get_isset($data, $date)) {
$data[$date] = array(
if ( ! wu_get_isset($data, $date)) {
$data[ $date ] = array(
'order_count' => 0,
'total' => $line_item->get_total(),
'tax_total' => $line_item->get_tax_total(),
'net_profit' => $line_item->get_total() - $line_item->get_tax_total(),
);
} else {
$data[$date]['order_count'] += 1;
$data[$date]['total'] += $line_item->get_total();
$data[$date]['tax_total'] += $line_item->get_tax_total();
$data[$date]['net_profit'] += $line_item->get_total() - $line_item->get_tax_total();
} // end if;
} // end foreach;
} // end foreach;
$data[ $date ]['order_count'] += 1;
$data[ $date ]['total'] += $line_item->get_total();
$data[ $date ]['tax_total'] += $line_item->get_tax_total();
$data[ $date ]['net_profit'] += $line_item->get_total() - $line_item->get_tax_total();
}
}
}
return $data;
} // end wu_calculate_taxes_by_day;
}
/**
* Calculates the taxes collected this year, segregated by month.
@ -454,10 +390,8 @@ function wu_calculate_taxes_by_month() {
$cache = get_site_transient('wu_tax_monthly_stats');
if (is_array($cache)) {
return $cache;
} // end if;
}
$query_args = array(
'date_query' => array(
@ -480,49 +414,38 @@ function wu_calculate_taxes_by_month() {
$months = iterator_to_array($period);
foreach ($months as $month_datetime) {
$data[$month_datetime->format('n')] = array(
$data[ $month_datetime->format('n') ] = array(
'order_count' => 0,
'total' => 0,
'tax_total' => 0,
'net_profit' => 0,
);
} // end foreach;
}
foreach ($line_items_groups as $line_items_group) {
foreach ($line_items_group as $line_item) {
$date = gmdate('n', strtotime((string) $line_item->date_created));
if (!wu_get_isset($data, $date)) {
$data[$date] = array(
if ( ! wu_get_isset($data, $date)) {
$data[ $date ] = array(
'order_count' => 0,
'total' => $line_item->get_total(),
'tax_total' => $line_item->get_tax_total(),
'net_profit' => $line_item->get_total() - $line_item->get_tax_total(),
);
} else {
$data[$date]['order_count'] += 1;
$data[$date]['total'] += $line_item->get_total();
$data[$date]['tax_total'] += $line_item->get_tax_total();
$data[$date]['net_profit'] += $line_item->get_total() - $line_item->get_tax_total();
} // end if;
} // end foreach;
} // end foreach;
$data[ $date ]['order_count'] += 1;
$data[ $date ]['total'] += $line_item->get_total();
$data[ $date ]['tax_total'] += $line_item->get_tax_total();
$data[ $date ]['net_profit'] += $line_item->get_total() - $line_item->get_tax_total();
}
}
}
set_site_transient('wu_tax_monthly_stats', $data);
return $data;
} // end wu_calculate_taxes_by_month;
}
/**
* Returns the number of sign-ups by form slug.
@ -543,18 +466,14 @@ function wu_calculate_signups_by_form($start_date = false, $end_date = false, $i
);
if ($start_date) {
$query['date_query']['after'] = $start_date;
$query['date_query']['inclusive'] = $inclusive;
} // end if;
}
if ($end_date) {
$query['date_query']['before'] = $end_date;
$query['date_query']['inclusive'] = $inclusive;
} // end if;
}
$date_query = new \WP_Date_Query($query['date_query']);
@ -577,4 +496,4 @@ function wu_calculate_signups_by_form($start_date = false, $end_date = false, $i
return $results;
} // end wu_calculate_signups_by_form;
}