monorepo/native/wordpress/maple-calc/maple-calc.php
2025-12-12 23:29:08 -05:00

230 lines
7.6 KiB
PHP

<?php
/**
* Plugin Name: Maple Calc
* Description: A WordPress plugin for inserting various calculators as Gutenberg blocks and shortcodes.
* Version: 1.0.1
* Author: SSP Media
* Author URI: https://sspmedia.ca/wordpress/
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
// Define plugin constants
define('MAPLE_CALC_VERSION', '1.0.1');
define('MAPLE_CALC_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('MAPLE_CALC_PLUGIN_URL', plugin_dir_url(__FILE__));
// Enqueue frontend scripts and styles
function maple_calc_frontend_enqueue_scripts() {
wp_enqueue_style('maple-calc-frontend', MAPLE_CALC_PLUGIN_URL . 'assets/css/frontend.css', [], MAPLE_CALC_VERSION);
wp_enqueue_script('maple-calc-frontend', MAPLE_CALC_PLUGIN_URL . 'assets/js/frontend.js', ['jquery', 'wp-element'], MAPLE_CALC_VERSION, true);
wp_enqueue_script('chart-js', 'https://cdn.jsdelivr.net/npm/chart.js', [], '3.7.0', true);
}
add_action('wp_enqueue_scripts', 'maple_calc_frontend_enqueue_scripts');
// Enqueue block editor assets
function maple_calc_enqueue_block_editor_assets() {
// Mortgage Calculator Block
wp_enqueue_script(
'maple-calc-mortgage-block',
MAPLE_CALC_PLUGIN_URL . 'assets/js/mortgage-block.js',
['wp-blocks', 'wp-element', 'wp-components', 'wp-editor', 'wp-block-editor'],
MAPLE_CALC_VERSION,
true
);
// Data Center Calculator Block
wp_enqueue_script(
'maple-calc-datacenter-block',
MAPLE_CALC_PLUGIN_URL . 'assets/js/datacenter-block.js',
['wp-blocks', 'wp-element', 'wp-components', 'wp-editor', 'wp-block-editor'],
MAPLE_CALC_VERSION,
true
);
// ROI Calculator Block
wp_enqueue_script(
'maple-calc-roi-block',
MAPLE_CALC_PLUGIN_URL . 'assets/js/roi-block.js',
['wp-blocks', 'wp-element', 'wp-components', 'wp-editor', 'wp-block-editor'],
MAPLE_CALC_VERSION,
true
);
}
add_action('enqueue_block_editor_assets', 'maple_calc_enqueue_block_editor_assets');
// Register Gutenberg blocks
function maple_calc_register_blocks() {
// Register Mortgage Calculator Block
register_block_type('maple/mortgage-calculator', [
'editor_script' => 'maple-calc-mortgage-block',
'render_callback' => 'maple_calc_render_mortgage_block',
'attributes' => [
'align' => [
'type' => 'string',
],
],
'supports' => [
'align' => true,
],
]);
// Register Data Center Calculator Block
register_block_type('maple/datacenter-calculator', [
'editor_script' => 'maple-calc-datacenter-block',
'render_callback' => 'maple_calc_render_datacenter_block',
'attributes' => [
'align' => [
'type' => 'string',
],
],
'supports' => [
'align' => true,
],
]);
// Register ROI Calculator Block
register_block_type('maple/roi-calculator', [
'editor_script' => 'maple-calc-roi-block',
'render_callback' => 'maple_calc_render_roi_block',
'attributes' => [
'align' => [
'type' => 'string',
],
],
'supports' => [
'align' => true,
],
]);
}
add_action('init', 'maple_calc_register_blocks');
// Register shortcodes
function maple_calc_register_shortcodes() {
add_shortcode('maple_mortgage_calculator', 'maple_calc_mortgage_shortcode');
add_shortcode('maple_datacenter_calculator', 'maple_calc_datacenter_shortcode');
add_shortcode('maple_roi_calculator', 'maple_calc_roi_shortcode');
}
add_action('init', 'maple_calc_register_shortcodes');
// Add admin menu
function maple_calc_admin_menu() {
add_menu_page(
'Maple Calc',
'Maple Calc',
'manage_options',
'maple-calc',
'maple_calc_admin_page',
'dashicons-calculator',
6
);
}
add_action('admin_menu', 'maple_calc_admin_menu');
// Admin page content
function maple_calc_admin_page() {
?>
<div class="wrap">
<h1>Maple Calc</h1>
<p>Welcome to the Maple Calc admin page. Use the shortcodes below to insert calculators into your posts or pages:</p>
<ul>
<li><code>[maple_mortgage_calculator]</code></li>
<li><code>[maple_datacenter_calculator]</code></li>
<li><code>[maple_roi_calculator]</code></li>
</ul>
</div>
<?php
}
// Render callback for Mortgage Calculator Block
function maple_calc_render_mortgage_block($attributes) {
ob_start();
include MAPLE_CALC_PLUGIN_DIR . 'templates/mortgage-calculator.php';
return ob_get_clean();
}
// Render callback for Data Center Calculator Block
function maple_calc_render_datacenter_block($attributes) {
ob_start();
include MAPLE_CALC_PLUGIN_DIR . 'templates/datacenter-calculator.php';
return ob_get_clean();
}
// Render callback for ROI Calculator Block
function maple_calc_render_roi_block($attributes) {
ob_start();
include MAPLE_CALC_PLUGIN_DIR . 'templates/roi-calculator.php';
return ob_get_clean();
}
// Shortcode callback for Mortgage Calculator
function maple_calc_mortgage_shortcode($atts) {
ob_start();
include MAPLE_CALC_PLUGIN_DIR . 'templates/mortgage-calculator.php';
return ob_get_clean();
}
// Shortcode callback for Data Center Calculator
function maple_calc_datacenter_shortcode($atts) {
ob_start();
include MAPLE_CALC_PLUGIN_DIR . 'templates/datacenter-calculator.php';
return ob_get_clean();
}
// Shortcode callback for ROI Calculator
function maple_calc_roi_shortcode($atts) {
ob_start();
include MAPLE_CALC_PLUGIN_DIR . 'templates/roi-calculator.php';
return ob_get_clean();
}
// Mortgage Calculator Logic
function maple_calculate_mortgage($principal, $interest_rate, $term_years) {
$monthly_interest_rate = $interest_rate / 100 / 12;
$term_months = $term_years * 12;
$monthly_payment = $principal * ($monthly_interest_rate * pow(1 + $monthly_interest_rate, $term_months)) / (pow(1 + $monthly_interest_rate, $term_months) - 1);
$total_payment = $monthly_payment * $term_months;
$total_interest = $total_payment - $principal;
return [
'monthly_payment' => round($monthly_payment, 2),
'total_payment' => round($total_payment, 2),
'total_interest' => round($total_interest, 2),
];
}
// Data Center Costs Calculator Logic
function maple_calculate_datacenter($power_kw, $cost_per_kwh, $rack_space, $cooling_factor = 1.2) {
$power_cost_per_hour = $power_kw * $cost_per_kwh;
$power_cost_per_month = $power_cost_per_hour * 24 * 30;
$cooling_cost_per_month = $power_cost_per_month * $cooling_factor;
$rack_cost_per_month = $rack_space * 100;
$total_monthly_cost = $power_cost_per_month + $cooling_cost_per_month + $rack_cost_per_month;
$total_annual_cost = $total_monthly_cost * 12;
return [
'power_cost_per_month' => round($power_cost_per_month, 2),
'cooling_cost_per_month' => round($cooling_cost_per_month, 2),
'rack_cost_per_month' => round($rack_cost_per_month, 2),
'total_monthly_cost' => round($total_monthly_cost, 2),
'total_annual_cost' => round($total_annual_cost, 2),
];
}
// ROI Calculator Logic
function maple_calculate_roi($initial_investment, $annual_return_rate, $time_years) {
$final_value = $initial_investment * pow(1 + $annual_return_rate / 100, $time_years);
$net_profit = $final_value - $initial_investment;
$roi_percentage = ($net_profit / $initial_investment) * 100;
return [
'final_value' => round($final_value, 2),
'net_profit' => round($net_profit, 2),
'roi_percentage' => round($roi_percentage, 2),
];
}