Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/cost-calculator-builder/cost-calculator-builder.php
+++ b/cost-calculator-builder/cost-calculator-builder.php
@@ -8,7 +8,7 @@
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: cost-calculator-builder
- * Version: 3.6.17
+ * Version: 4.0.3
*/
if ( ! defined( 'ABSPATH' ) ) {
@@ -17,9 +17,9 @@
define( 'CALC_DIR', __DIR__ );
define( 'CALC_FILE', __FILE__ );
-define( 'CALC_VERSION', '3.6.17' );
+define( 'CALC_VERSION', '4.0.3' );
define( 'CALC_WP_TESTED_UP', '6.9' );
-define( 'CALC_DB_VERSION', '3.6.15' );
+define( 'CALC_DB_VERSION', '4.0.0' );
define( 'CALC_PATH', dirname( CALC_FILE ) );
define( 'CALC_URL', plugins_url( '', CALC_FILE ) );
@@ -87,6 +87,7 @@
require_once CALC_PATH . '/includes/classes/pdf-manager/CCBPdfManagerTemplates.php';
require_once CALC_PATH . '/includes/classes/pdf-manager/CCBPdfManager.php';
require_once CALC_PATH . '/includes/classes/CCBCalculators.php';
+require_once CALC_PATH . '/includes/classes/CCBCalculatorsHandler.php';
require_once CALC_PATH . '/includes/classes/CCBCalculatorTemplates.php';
require_once CALC_PATH . '/includes/classes/CCBCategory.php';
require_once CALC_PATH . '/includes/classes/CCBExportImport.php';
--- a/cost-calculator-builder/includes/admin/enqueue.php
+++ b/cost-calculator-builder/includes/admin/enqueue.php
@@ -10,9 +10,9 @@
wp_enqueue_style( 'ccb-global-styles', CALC_URL . '/frontend/dist/css/global.css', array(), CALC_VERSION );
wp_enqueue_style( 'ccb-icons-list', CALC_URL . '/frontend/dist/css/icon/style.css', array(), CALC_VERSION );
wp_enqueue_style( 'ccb-pdf-template', CALC_URL . '/frontend/dist/css/pdf-template.css', array(), CALC_VERSION );
+ wp_enqueue_style( 'ccb-admin-welcome-css', CALC_URL . '/frontend/dist/css/welcome.css', array(), CALC_VERSION );
- /** Loading wp media libraries **/
- if ( isset( $_GET['page'] ) && ( $_GET['page'] === 'cost_calculator_builder' ) ) { //phpcs:ignore
+ if ( isset( $_GET['page'] ) && ( $_GET['page'] === 'cost_calculator_builder_settings' ) ) { //phpcs:ignore
wp_enqueue_media();
wp_enqueue_script( 'cbb-resize-sensor-js', CALC_URL . '/frontend/dist/sticky/ResizeSensor.js', array(), CALC_VERSION, true );
wp_enqueue_script( 'cbb-sticky-sidebar-js', CALC_URL . '/frontend/dist/sticky/sticky-sidebar.js', array( 'cbb-resize-sensor-js' ), CALC_VERSION, true );
@@ -30,7 +30,51 @@
wp_enqueue_style( 'ccb-admin-welcome-css', CALC_URL . '/frontend/dist/css/welcome.css', array(), CALC_VERSION );
wp_enqueue_script( 'cbb-admin-welcome-js', CALC_URL . '/frontend/dist/welcome.js', array( 'jquery' ), CALC_VERSION ); // phpcs:ignore
wp_enqueue_script( 'cbb-phone-js', CALC_URL . '/frontend/dist/libs/vue/phone/vue-phone-number-input.umd.js', array(), CALC_VERSION, true );
+ wp_enqueue_style( 'ccb-pdf-template', CALC_URL . '/frontend/dist/css/pdf-template.css', array(), CALC_VERSION );
+ }
+ /** Loading wp media libraries **/
+ if ( isset( $_GET['page'] ) && ( $_GET['page'] === 'cost_calculator_builder' || $_GET['page'] === 'cost_calculator_templates' || $_GET['page'] === 'cost_calculator_builder_whats_new' ) ) { //phpcs:ignore
+ wp_enqueue_media();
+
+ Viteenqueue_asset(
+ CALC_PATH . '/frontend/vue3/dist',
+ 'src/admin/main.ts',
+ array(
+ 'handle' => 'ccb-admin-bundle',
+ 'in-footer' => true,
+ )
+ );
+
+ $translations = array_merge(
+ cBuilderClassesCCBTranslations::get_frontend_translations(),
+ cBuilderClassesCCBTranslations::get_full_admin_translations()
+ );
+ $active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins', array() ) );
+ $is_woocommerce_active = class_exists( 'WooCommerce' ) || in_array( 'woocommerce/woocommerce.php', $active_plugins, true );
+ $woo_install_url = esc_url( admin_url( 'plugin-install.php?s=woocommerce&tab=search&type=term' ) );
+ wp_localize_script(
+ 'ccb-admin-bundle',
+ 'ccb_ajax_window',
+ array(
+ 'ajax_url' => admin_url( 'admin-ajax.php' ),
+ 'dateFormat' => get_option( 'date_format' ),
+ 'language' => substr( get_bloginfo( 'language' ), 0, 2 ),
+ 'plugin_url' => CALC_URL,
+ 'translations' => $translations,
+ 'pro_active' => ccb_pro_active(),
+ 'plugin_version' => CALC_VERSION,
+ 'site_url' => site_url(),
+ 'woo_active' => $is_woocommerce_active,
+ 'woo_install_url' => $woo_install_url,
+ 'menu_items' => cBuilderClassesCCBSettingsData::get_menu_items(),
+ 'current_page' => isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : '', // phpcs:ignore WordPress.Security.NonceVerification
+ 'export_link' => esc_url( get_site_url() ) . '/wp-admin/admin-ajax.php?action=cost-calculator-custom-export-run&ccb_nonce=' . esc_attr( wp_create_nonce( 'ccb_export_nonce' ) ),
+ )
+ );
+
+ wp_enqueue_style( 'ccb-calc-font', CALC_URL . '/frontend/dist/css/font/font.css', array(), CALC_VERSION );
+ wp_enqueue_style( 'ccb-red-hat-display', CALC_URL . '/frontend/dist/css/font/RedHutDisplay/font.css', array(), CALC_VERSION );
} elseif ( isset( $_GET['page'] ) && ( $_GET['page'] === 'cost_calculator_analytics' ) ) { //phpcs:ignore
Viteenqueue_asset(
CALC_PATH . '/frontend/vue3/dist',
@@ -43,12 +87,12 @@
// phpcs:disable
$handle_data = array(
- 'ajax_url' => admin_url( 'admin-ajax.php' ),
- 'dateFormat' => get_option( 'date_format' ),
- 'language' => substr( get_bloginfo( 'language' ), 0, 2 ),
- 'plugin_url' => CALC_URL,
- 'translations' => cBuilderClassesCCBTranslations::get_full_admin_translations(),
- 'pro_active' => ccb_pro_active(),
+ 'ajax_url' => admin_url( 'admin-ajax.php' ),
+ 'dateFormat' => get_option( 'date_format' ),
+ 'language' => substr( get_bloginfo( 'language' ), 0, 2 ),
+ 'plugin_url' => CALC_URL,
+ 'translations' => cBuilderClassesCCBTranslations::get_full_admin_translations(),
+ 'pro_active' => ccb_pro_active(),
'plugin_version' => CALC_VERSION,
'menu_items' => cBuilderClassesCCBSettingsData::get_menu_items(),
);
--- a/cost-calculator-builder/includes/classes/CCBAjaxAction.php
+++ b/cost-calculator-builder/includes/classes/CCBAjaxAction.php
@@ -28,20 +28,48 @@
}
public static function init() {
- self::addAction( 'calc_create_id', array( CCBCalculators::class, 'create_calc_id' ) );
+ /**
+ * NEW ACTIONS
+ * ------------------------------------------------------------- **/
+ self::addAction( 'ccb_fetch_calculators', array( CCBCalculatorsHandler::class, 'ccb_fetch_calculators' ) );
+ self::addAction( 'ccb_fetch_templates', array( CCBCalculatorsHandler::class, 'ccb_fetch_templates' ) );
+ self::addAction( 'calc_create_id', array( CCBCalculatorsHandler::class, 'create_calc_id' ) );
+ self::addAction( 'calc_edit_calc', array( CCBCalculatorsHandler::class, 'edit_calc' ) );
+ self::addAction( 'calc_use_template', array( CCBCalculatorsHandler::class, 'use_template' ) );
+ self::addAction( 'calc_run_updates', array( CCBCalculatorsHandler::class, 'run_calc_updates' ) );
+ self::addAction( 'ccb_delete_calc', array( CCBCalculatorsHandler::class, 'delete_calc' ) );
+ self::addAction( 'ccb_duplicate_calc', array( CCBCalculatorsHandler::class, 'duplicate_calc' ) );
+
+ self::addAction( 'calc_toggle_favorite', array( CCBCalculatorTemplates::class, 'calc_toggle_favorite' ) );
+
+ /** import/export */
+ self::addAction( 'cost-calculator-custom-import-total', array( CCBExportImport::class, 'custom_import_calculators_total' ) );
+ self::addAction( 'cost-calculator-demo-calculators-total', array( CCBExportImport::class, 'demo_calculators_total' ) );
+ self::addAction( 'cost-calculator-import-run', array( CCBExportImport::class, 'import_run' ) );
+ self::addAction( 'cost-calculator-custom-export-run', array( CCBExportImport::class, 'export_calculators' ) );
+
+ /** Admin actions */
+ self::addAction( 'ccb_get_calculator_admin_data', array( CCBCalculatorsHandler::class, 'get_calculator_admin_data' ) );
+ self::addAction( 'calc_save_settings', array( CCBCalculatorsHandler::class, 'save_settings' ) );
+
+ /**
+ * OLD ACTIONS
+ * ------------------------------------------------------------- **/
self::addAction( 'calc_get_products', array( CCBCalculators::class, 'getProducts' ) );
+ self::addAction( 'calc_get_woo_categories', array( CCBCalculators::class, 'getWooCategories' ) );
self::addAction( 'calc_get_wp_pages', array( CCBCalculators::class, 'get_wp_pages' ) );
- self::addAction( 'calc_edit_calc', array( CCBCalculators::class, 'edit_calc' ) );
- self::addAction( 'calc_delete_calc', array( CCBCalculators::class, 'delete_calc' ) );
self::addAction( 'calc_save_custom', array( CCBCalculators::class, 'save_custom' ) );
self::addAction( 'calc_skip_quick_tour', array( CCBCalculators::class, 'calc_skip_quick_tour' ) );
self::addAction( 'calc_skip_hint', array( CCBCalculators::class, 'calc_skip_hint' ) );
self::addAction( 'calc_get_existing', array( CCBCalculators::class, 'get_existing' ) );
- self::addAction( 'calc_save_settings', array( CCBCalculators::class, 'save_settings' ) );
self::addAction( 'calc_save_ai_api_key', array( CCBCalculators::class, 'save_api_key' ) );
+ self::addAction( 'ccb_get_preset', array( CCBCalculators::class, 'ccb_get_preset' ) );
+ self::addAction( 'ccb_select_preset', array( CCBCalculators::class, 'ccb_select_preset' ) );
self::addAction( 'ccb_update_preset', array( CCBCalculators::class, 'ccb_update_preset' ) );
self::addAction( 'ccb_update_preset_title', array( CCBCalculators::class, 'ccb_update_preset_title' ) );
self::addAction( 'ccb_add_preset', array( CCBCalculators::class, 'ccb_add_preset' ) );
+ self::addAction( 'ccb_extend_preset', array( CCBCalculators::class, 'ccb_extend_preset' ) );
+ self::addAction( 'ccb_duplicate_preset', array( CCBCalculators::class, 'ccb_duplicate_preset' ) );
self::addAction( 'ccb_delete_preset', array( CCBCalculators::class, 'ccb_delete_preset' ) );
self::addAction( 'ccb_reset_type', array( CCBCalculators::class, 'ccb_reset_type' ) );
self::addAction( 'ccb_preset_hide_notice', array( CCBCalculators::class, 'ccb_preset_hide_notice' ) );
@@ -51,9 +79,6 @@
self::addAction( 'ccb_restore_pdf_template', array( CCBCalculators::class, 'ccb_restore_pdf_template' ) );
self::addAction( 'calc_save_general_settings', array( CCBCalculators::class, 'save_general_settings' ) );
self::addAction( 'calc_get_general_settings', array( CCBCalculators::class, 'calc_get_general_settings' ) );
- self::addAction( 'calc_duplicate_calc', array( CCBCalculators::class, 'duplicate_calc' ) );
- self::addAction( 'calc-run-calc-updates', array( CCBUpdates::class, 'run_calc_updates' ) );
- self::addAction( 'calc_use_template', array( CCBCalculators::class, 'calc_use_template' ) );
self::addAction( 'calc_config_settings', array( CCBCalculators::class, 'calc_config_settings' ) );
self::addAction( 'calc_delete_payment', array( CCBCalculators::class, 'calc_delete_payment' ) );
self::addAction( 'ccb_update_banner', array( CCBCalculators::class, 'ccb_update_banner' ) );
@@ -63,7 +88,6 @@
self::addAction( 'calc_save_as_template', array( CCBCalculatorTemplates::class, 'calc_save_as_template' ) );
self::addAction( 'calc_get_templates_list', array( CCBCalculatorTemplates::class, 'calc_get_all_templates' ) );
self::addAction( 'calc_delete_templates', array( CCBCalculatorTemplates::class, 'calc_delete_template' ) );
- self::addAction( 'calc_toggle_favorite', array( CCBCalculatorTemplates::class, 'calc_toggle_favorite' ) );
self::addAction( 'calc_rollback', array( CCBCalculators::class, 'ccb_rollback_handler' ) );
@@ -79,19 +103,13 @@
self::addAction( 'embed-create-page', array( CCBEmbedCalculator::class, 'create_page' ) );
self::addAction( 'embed-get-pages', array( CCBEmbedCalculator::class, 'get_all_pages' ) );
self::addAction( 'embed-insert-pages', array( CCBEmbedCalculator::class, 'insert_pages' ) );
-
- /** import/export */
- self::addAction( 'cost-calculator-custom-import-total', array( CCBExportImport::class, 'custom_import_calculators_total' ) );
- self::addAction( 'cost-calculator-demo-calculators-total', array( CCBExportImport::class, 'demo_import_calculators_total' ) );
- self::addAction( 'cost-calculator-import-run', array( CCBExportImport::class, 'import_run' ) );
- self::addAction( 'cost-calculator-custom-export-run', array( CCBExportImport::class, 'export_calculators' ) );
+ self::addAction( 'calc_discount_list', array( CCBDiscountController::class, 'discount_list' ) );
if ( defined( 'CCB_PRO_VERSION' ) ) {
self::addAction( 'calc_create_discount', array( CCBDiscountController::class, 'create' ) );
self::addAction( 'calc_update_discount', array( CCBDiscountController::class, 'update' ) );
self::addAction( 'calc_delete_discount', array( CCBDiscountController::class, 'delete' ) );
self::addAction( 'calc_duplicate_discount', array( CCBDiscountController::class, 'duplicate' ) );
- self::addAction( 'calc_discount_list', array( CCBDiscountController::class, 'discount_list' ) );
self::addAction( 'calc_preview_discount_list', array( CCBDiscountController::class, 'discount_preview_list' ) );
self::addAction( 'create_cc_order', array( CCBOrderController::class, 'create' ), true );
--- a/cost-calculator-builder/includes/classes/CCBBuilderAdminMenu.php
+++ b/cost-calculator-builder/includes/classes/CCBBuilderAdminMenu.php
@@ -52,6 +52,14 @@
);
add_submenu_page(
'cost_calculator_builder',
+ esc_html__( "What's New", 'cost-calculator-builder' ),
+ esc_html__( "What's New", 'cost-calculator-builder' ),
+ 'manage_options',
+ 'cost_calculator_builder_whats_new',
+ array( $this, 'render_whats_new' )
+ );
+ add_submenu_page(
+ 'cost_calculator_builder',
esc_html__( 'Templates', 'cost-calculator-builder' ),
esc_html__( 'Templates', 'cost-calculator-builder' ),
'manage_options',
@@ -75,8 +83,8 @@
esc_html__( 'Global Settings', 'cost-calculator-builder' ),
esc_html__( 'Global Settings', 'cost-calculator-builder' ),
'manage_options',
- 'cost_calculator_builder&tab=settings',
- array( $this, 'render_page' )
+ 'cost_calculator_builder_settings',
+ array( $this, 'render_settings_page' )
);
if ( defined( 'CCB_PRO_VERSION' ) ) {
@@ -182,6 +190,10 @@
echo CCBTemplate::load( 'admin/pages/templates' ); //phpcs:ignore
}
+ public function render_whats_new() {
+ echo CCBTemplate::load( 'admin/pages/whats-new' ); //phpcs:ignore
+ }
+
public function render_preview() {
echo CCBTemplate::load( 'admin/pages/preview' ); //phpcs:ignore
}
@@ -198,6 +210,10 @@
echo CCBTemplate::load( 'admin/index' ); //phpcs:ignore
}
+ public function render_settings_page() {
+ echo CCBTemplate::load( 'admin/pages/settings' ); //phpcs:ignore
+ }
+
public function calc_orders_page() {
echo CCBTemplate::load( 'admin/pages/new-orders' ); //phpcs:ignore
}
--- a/cost-calculator-builder/includes/classes/CCBCalculatorTemplates.php
+++ b/cost-calculator-builder/includes/classes/CCBCalculatorTemplates.php
@@ -136,9 +136,9 @@
}
$result = array(
- 'success' => false,
- 'templates' => array(),
- 'message' => __( 'Could not delete template, please try again!', 'cost-calculator-builder' ),
+ 'success' => false,
+ 'message' => __( 'Could not delete template, please try again!', 'cost-calculator-builder' ),
+ 'data' => array(),
);
if ( isset( $_GET['template_id'] ) ) {
@@ -163,16 +163,17 @@
public static function calc_toggle_favorite() {
check_ajax_referer( 'ccb_toggle_favorite', 'nonce' );
- if ( ! current_user_can( 'manage_options' ) ) {
- wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
- }
-
$result = array(
- 'success' => true,
- 'favorites' => array(),
- 'message' => __( 'Could not toggle favorite, please try again!', 'cost-calculator-builder' ),
+ 'success' => false,
+ 'data' => array(),
+ 'message' => __( 'Could not toggle favorite, please try again!', 'cost-calculator-builder' ),
);
+ if ( ! current_user_can( 'manage_options' ) ) {
+ $result['message'] = __( 'You are not allowed to run this action', 'cost-calculator-builder' );
+ wp_send_json( $result );
+ }
+
$request_body = file_get_contents( 'php://input' );
$request_data = json_decode( $request_body, true );
$data = apply_filters( 'stm_ccb_sanitize_array', $request_data );
@@ -191,10 +192,11 @@
} else {
$favorites[] = $template_id;
$result['message'] = 'Added to favorites';
+ $result['success'] = array_values( $favorites );
}
update_option( 'calc_templates_favorites', $favorites );
- $result['favorites'] = $favorites;
+ $result['data']['favorites'] = array_values( $favorites );
}
wp_send_json( $result );
@@ -294,14 +296,14 @@
$resources_json[] = array(
'type' => $type,
'title' => $title,
- 'calc_id' => $calc_id,
+ 'id' => $id,
'category' => $category,
'template_id' => $id,
'icon' => $icon,
'link' => $link,
'info' => $info,
'description' => $description,
- 'action' => 'use_template',
+ 'image' => CALC_URL . '/frontend/vue3/assets/images/calc-preview.png',
);
}
}
@@ -655,7 +657,7 @@
return array();
}
- $key = array_search( $name, array_column( $contents['calculators'], 'ccb_name' ) );
+ $key = array_search( $name, array_column( $contents['calculators'], 'ccb_name' ), true );
if ( false === $key ) {
return array();
}
--- a/cost-calculator-builder/includes/classes/CCBCalculators.php
+++ b/cost-calculator-builder/includes/classes/CCBCalculators.php
@@ -58,26 +58,34 @@
/**
* Get Default Data
*/
- private static function get_default_calculator_data() {
+ public static function get_default_calculator_data() {
return array(
- 'id' => '',
- 'title' => '',
- 'forms' => array(),
- 'pages' => array(),
- 'fields' => array(),
- 'builder' => array(),
- 'formula' => array(),
- 'settings' => array(),
- 'products' => array(),
- 'categories' => array(),
- 'conditions' => array(),
- 'success' => false,
- 'appearance' => array(),
- 'desc_options' => array(
+ 'pages' => array(),
+ 'calculator_builder' => array(
+ 'id' => '',
+ 'title' => '',
+ 'formula' => array(),
+ 'fields' => array(),
+ 'builder' => array(),
+ ),
+ 'settings' => array(),
+ 'total_summary' => array(),
+ 'general_settings' => array(),
+ 'products' => array(),
+ 'categories' => array(),
+ 'conditions' => array(),
+ 'appearance' => array(),
+ 'desc_options' => array(
self::DESC_POSITION_BEFORE => __( 'Show before field', 'cost-calculator-builder' ),
self::DESC_POSITION_AFTER => __( 'Show after field', 'cost-calculator-builder' ),
),
- 'message' => __( 'There is no calculator with this id', 'cost-calculator-builder' ),
+ 'order_form_manager' => array(
+ 'order_form_fields' => array(),
+ 'active_form_id' => null,
+ 'order_forms' => array(),
+ 'calc_active_form_fields' => array(),
+ 'order_active_form_fields' => array(),
+ ),
);
}
@@ -116,6 +124,35 @@
);
}
+ public static function getWooCategories() {
+ check_ajax_referer( 'calc_load_woo_categories', 'nonce' );
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
+ }
+
+ $category_ids = array();
+
+ $page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
+ $search = ! empty( $_GET['search'] ) ? sanitize_text_field( $_GET['search'] ) : '';
+ $per_page = ! empty( $_GET['per_page'] ) ? (int) $_GET['per_page'] : 5;
+
+ if ( ! empty( $_GET['category_ids'] ) ) {
+ $category_ids = explode( ',', sanitize_text_field( $_GET['category_ids'] ) );
+ }
+
+ $data = ccb_woo_categories_paginated( $page, $per_page, $search, $category_ids );
+
+ wp_send_json(
+ array(
+ 'categories' => $data['items'],
+ 'has_more' => $data['has_more'],
+ 'page' => $page,
+ 'success' => true,
+ )
+ );
+ }
+
public static function get_wp_pages() {
check_ajax_referer( 'calc_load_pages', 'nonce' );
@@ -151,7 +188,6 @@
public static function edit_calc() {
check_ajax_referer( 'ccb_edit_calc', 'nonce' );
-
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
}
@@ -173,19 +209,13 @@
public static function edit_calc_body( $params ) {
$result = self::get_default_calculator_data();
-
if ( ! empty( $params['calc_id'] ) ) {
$calc_id = (int) sanitize_text_field( $params['calc_id'] );
- $result['id'] = $calc_id;
- $result['title'] = get_post_meta( $calc_id, 'stm-name', true );
- $result['fields'] = CCBFieldsHelper::fields();
$result['order_form_fields'] = CCBOrderFormFieldsHelper::order_form_fields();
$result['activeFormId'] = CCBOrderFormFieldsHelper::get_active_form_id( $calc_id );
$result['order_forms'] = Forms::get_all_forms();
$result['order_active_form_fields'] = FormFields::get_active_fields( $result['activeFormId'] );
- $result['formula'] = get_post_meta( $calc_id, 'stm-formula', true );
- $result['conditions'] = get_post_meta( $calc_id, 'stm-conditions', true );
$result['saved'] = get_post_meta( $calc_id, 'calc_saved', true );
$general_settings = CCBSettingsData::get_calc_global_settings();
@@ -231,19 +261,12 @@
$result['general_settings'] = $general_settings;
- $stm_fields = get_post_meta( $calc_id, 'stm-fields', true );
- $result['builder'] = ! empty( $stm_fields ) ? $stm_fields : array();
-
$discount_params = array();
if ( isset( $params['discount'] ) ) {
$discount_params = self::get_filter_data( $params['discount'] );
}
$discount_params['calc_id'] = $calc_id;
- $result['discounts'] = array(
- 'discounts' => Discounts::get_all_discounts( $discount_params ),
- 'discounts_count' => Discounts::get_total_discounts( $discount_params ),
- );
$preset_key = get_post_meta( $calc_id, 'ccb_calc_preset_idx', true );
$preset_key = empty( $preset_key ) ? 'default' : $preset_key;
@@ -272,14 +295,14 @@
}
$settings = CCBSettingsData::get_calc_single_settings( $calc_id );
- /* pro-features */
- $result['pages'] = self::fetch_page_ids( $settings );
- $result['forms'] = ccb_contact_forms();
- $result['products'] = self::fetch_product_ids( $settings );
- $result['categories'] = ccb_woo_categories();
if ( ccb_pro_active() ) {
- $result['payments'] = cBuilderClassesCCBProSettings::get_payments();
+ /* pro-features */
+ $result['payments'] = cBuilderClassesCCBProSettings::get_payments();
+ $result['pages'] = self::fetch_page_ids( $settings );
+ $result['forms'] = ccb_contact_forms();
+ $result['products'] = self::fetch_product_ids( $settings );
+ $result['categories'] = ccb_woo_categories();
}
$result['sp_list'] = array();
@@ -329,9 +352,6 @@
$general_settings = CCBSettingsData::get_calc_global_settings();
$ccb_sync = ccb_sync_settings_from_general_settings( $result['settings'], $general_settings );
$result['settings'] = $ccb_sync['settings'];
-
- $page_params = self::get_filter_data( $params['page_params'] );
- $result['calculators'] = self::get_calculator_list( $page_params );
}
return $result;
@@ -349,85 +369,12 @@
}
/**
- * Duplicate Calculator
- */
- public static function duplicate_calc() {
-
- check_ajax_referer( 'ccb_duplicate_calc', 'nonce' );
-
- if ( ! current_user_can( 'manage_options' ) ) {
- wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
- }
-
- $result = array(
- 'calculators' => array(),
- 'success' => false,
- 'message' => __( "Couldn't duplicate calculator, please try again!', 'cost-calculator-builder" ),
- );
-
- $params = self::get_filter_data( $_GET );
-
- if ( isset( $_GET['calculator_ids'] ) ) {
-
- $ids = array_map(
- function ( $item ) {
- return (int) sanitize_text_field( $item );
- },
- explode( ',', $_GET['calculator_ids'] )
- );
-
- $result_ids = array();
- foreach ( $ids as $id ) {
- $new_calculator = array(
- 'post_parent' => $id,
- 'post_status' => 'publish',
- 'post_type' => 'cost-calc',
- );
-
- $duplicated_post_id = wp_insert_post( $new_calculator );
-
- $data = array(
- 'id' => $duplicated_post_id,
- 'title' => self::get_calculator_name_for_duplicate( $id ),
- 'formula' => get_post_meta( $id, 'stm-formula', true ),
- 'settings' => CCBSettingsData::get_calc_single_settings( $id ),
- 'builder' => get_post_meta( $id, 'stm-fields', true ),
- 'conditions' => get_post_meta( $id, 'stm-conditions', true ),
- 'appearance' => get_post_meta( $id, 'ccb-appearance', true ),
- 'preset_idx' => get_post_meta( $id, 'ccb_calc_preset_idx', true ),
- );
-
- if ( ccb_update_calc_values( $data ) ) {
- array_push( $result_ids, $duplicated_post_id );
- }
- }
-
- $result['success'] = true;
- $result['calculators'] = self::get_calculator_list( $params );
- $result['duplicated_ids'] = $result_ids;
- $result['message'] = __( 'Calculators duplicated successfully', 'cost-calculator-builder' );
- }
-
- if ( ! empty( $_GET['calc_id'] ) ) {
- $data = self::duplicate_target_calc( $_GET['calc_id'] );
- if ( ccb_update_calc_values( $data ) ) {
- $result['success'] = true;
- $result['calculators'] = self::get_calculator_list( $params );
- $result['message'] = __( 'Calculator duplicated successfully', 'cost-calculator-builder' );
- $result['duplicated_id'] = $_GET['calc_id'];
- }
- }
-
- wp_send_json( $result );
- }
-
- /**
* Duplicate target calc
*
* @param $calc_id
* @return array
*/
- private static function duplicate_target_calc( $calc_id, $copy_title = true ) {
+ public static function duplicate_target_calc( $calc_id, $copy_title = true ) {
$calc_id = (int) sanitize_text_field( $calc_id );
$my_post = array(
@@ -536,59 +483,6 @@
}
/**
- * Delete calc by id
- */
- public static function delete_calc() {
- check_ajax_referer( 'ccb_delete_calc', 'nonce' );
-
- if ( ! current_user_can( 'manage_options' ) ) {
- wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
- }
-
- $result = array(
- 'success' => false,
- 'calculators' => array(),
- 'message' => __( 'Could not delete calculator, please try again!', 'cost-calculator-builder' ),
- );
-
- $params = self::get_filter_data( $_GET );
-
- if ( isset( $_GET['calculator_ids'] ) ) {
- $ids = array_map(
- function ( $item ) {
- return (int) sanitize_text_field( $item );
- },
- explode( ',', $_GET['calculator_ids'] )
- );
-
- foreach ( $ids as $id ) {
- wp_delete_post( $id );
- clearMetaData( $id );
- ccb_update_woocommerce_calcs( $id, true );
- }
-
- $result['success'] = true;
- $result['calculators'] = self::get_calculator_list( $params );
- $result['message'] = __( 'Calculators deleted successfully', 'cost-calculator-builder' );
- }
-
- if ( isset( $_GET['calc_id'] ) ) {
-
- $calc_id = (int) sanitize_text_field( $_GET['calc_id'] );
-
- wp_delete_post( $calc_id );
- clearMetaData( $calc_id );
- ccb_update_woocommerce_calcs( $calc_id, true );
-
- $result['success'] = true;
- $result['calculators'] = self::get_calculator_list( $params );
- $result['message'] = __( 'Calculator deleted successfully', 'cost-calculator-builder' );
- }
-
- wp_send_json( $result );
- }
-
- /**
* Save Custom Styles
*/
public static function save_custom() {
@@ -616,38 +510,105 @@
$appearance = $content['appearance'];
}
- $preset_key = ! isset( $data['selectedIdx'] ) ? 'default' : $data['selectedIdx'];
- if ( 'custom' !== $preset_key ) {
- if ( 'saved' === $preset_key ) {
- $presets = get_option( 'ccb_appearance_presets', array() );
- $count = count( $presets ) + 1;
- $new_preset = CCBPresetGenerator::generate_new_preset( $count, $appearance );
- $preset_key = $new_preset['key'];
- $presets[ $preset_key ] = $new_preset;
-
- CCBPresetGenerator::update_presets( $presets );
+ $preset_key = ! isset( $data['selectedIdx'] ) ? 'default' : sanitize_text_field( $data['selectedIdx'] );
+ if ( 'saved' === $preset_key ) {
+ $presets = CCBPresetGenerator::get_static_preset_from_db( true );
+ $max_idx = 0;
+ foreach ( array_keys( $presets ) as $k ) {
+ if ( preg_match( '/^saved_(d+)$/', $k, $m ) ) {
+ $max_idx = max( $max_idx, (int) $m[1] );
+ }
}
+ $new_preset = CCBPresetGenerator::generate_new_preset( $max_idx + 1, $appearance );
+ $preset_key = $new_preset['key'];
+ $presets[ $preset_key ] = $new_preset;
- CCBPresetGenerator::save_custom( $preset_key, $appearance );
- CCBPresetGenerator::update_preset_key( $data['id'], sanitize_text_field( $preset_key ) );
- } else {
+ CCBPresetGenerator::update_presets( $presets );
+ }
+
+ if ( ! CCBPresetGenerator::preset_exist( $preset_key ) ) {
$preset_key = 'default';
}
+ CCBPresetGenerator::save_custom( $preset_key, $appearance );
+ CCBPresetGenerator::update_preset_key( $data['id'], sanitize_text_field( $preset_key ) );
+
$presets_data = CCBAppearanceHelper::get_appearance_data( $preset_key );
+ $appearance = self::get_save_custom_appearance_response( $preset_key, $presets_data );
$result['success'] = true;
$result['message'] = 'Changes Saved successfully';
$result['data'] = array(
- 'presetIdx' => $preset_key,
- 'appearance' => ! isset( $presets_data['data'] ) ? array() : $presets_data['data'],
- 'presets' => ! isset( $presets_data['list'] ) ? array() : $presets_data['list'],
+ 'appearance' => $appearance,
+ 'presetIdx' => $appearance['selected_preset_key'],
+ 'presets' => $appearance['presets'],
);
}
wp_send_json( $result );
}
+ private static function get_save_custom_appearance_response( $preset_key, $presets_data ) {
+ $active_preset = isset( $presets_data['data'] ) && is_array( $presets_data['data'] ) ? $presets_data['data'] : array();
+ if ( ! isset( $active_preset['preset_key'] ) ) {
+ $active_preset['preset_key'] = $preset_key;
+ }
+
+ return array(
+ 'presets' => self::normalize_save_custom_presets( $presets_data['list'] ?? array() ),
+ 'active_preset' => $active_preset,
+ 'selected_preset_key' => $preset_key,
+ );
+ }
+
+ private static function normalize_save_custom_presets( $presets ) {
+ if ( ! is_array( $presets ) ) {
+ return array();
+ }
+
+ $result = array();
+ foreach ( $presets as $preset ) {
+ $preset_id = isset( $preset['key'] ) ? $preset['key'] : '';
+ if ( empty( $preset_id ) ) {
+ continue;
+ }
+
+ $preset_appearance = CCBAppearanceHelper::get_appearance_data( $preset_id );
+ $appearance_data = isset( $preset_appearance['data'] ) && is_array( $preset_appearance['data'] ) ? $preset_appearance['data'] : array();
+
+ $result[] = array(
+ 'id' => $preset_id,
+ 'name' => isset( $preset['title'] ) ? $preset['title'] : '',
+ 'colors' => self::extract_save_custom_preset_colors( $appearance_data ),
+ );
+ }
+
+ return $result;
+ }
+
+ private static function extract_save_custom_preset_colors( $appearance_data ) {
+ $color_keys = array( 'container', 'primary_color', 'accent_color', 'secondary_color', 'error_color' );
+ $result = array();
+
+ foreach ( $color_keys as $key ) {
+ $value = '';
+
+ if ( isset( $appearance_data['desktop']['colors']['data'][ $key ]['value'] ) ) {
+ $value = $appearance_data['desktop']['colors']['data'][ $key ]['value'];
+ } elseif ( isset( $appearance_data['desktop']['colors'][ $key ] ) ) {
+ $value = $appearance_data['desktop']['colors'][ $key ];
+ }
+
+ if ( is_array( $value ) && isset( $value['color'] ) ) {
+ $value = $value['color'];
+ }
+
+ $result[] = $value;
+ }
+
+ return $result;
+ }
+
public static function calc_skip_hint() {
check_ajax_referer( 'calc_skip_hint', 'nonce' );
@@ -753,10 +714,10 @@
if ( empty( $global_settings['ai']['gpt_api_key'] ) ) {
$global_settings['ai']['gpt_api_key'] = $key;
- update_option( 'ccb_general_settings', $global_settings );
+ CCBSettingsData::update_calc_global_settings( $global_settings );
$result['success'] = true;
- $result['message'] = 'Key updated successfully';
+ $result['message'] = __( 'Key updated successfully', 'cost-calculator-builder' );
}
}
@@ -764,57 +725,6 @@
}
/**
- * Save all calculator settings via calc id
- */
- public static function save_settings() {
- check_ajax_referer( 'ccb_save_settings', 'nonce' );
-
- if ( ! current_user_can( 'manage_options' ) ) {
- wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
- }
-
- $result = array(
- 'success' => false,
- 'message' => 'Something went wrong',
- );
-
- if ( 'done' !== get_option( 'ccb_quick_tour_type', 'quick_tour_start' ) ) {
- update_option( 'ccb_quick_tour_type', 'done' );
- }
-
- $request_body = file_get_contents( 'php://input' );
- $request_data = json_decode( $request_body, true );
- $data = apply_filters( 'stm_ccb_sanitize_array', $request_data );
-
- if ( isset( $data['settings']['formFields']['body'] ) ) {
- $content = $data['settings']['formFields']['body'];
- $content = str_replace( '\n', '<br>', $content );
- $data['settings']['formFields']['body'] = str_replace( '\', '', $content );
- }
-
- if ( ! empty( $data ) && ccb_update_calc_values( $data ) ) {
- $general_settings = CCBSettingsData::get_calc_global_settings();
- if ( isset( $general_settings['backup_settings'] ) && true === filter_var( $general_settings['backup_settings']['auto_backup'], FILTER_VALIDATE_BOOLEAN ) ) {
- self::savepoint( $data );
- }
-
- $params = array(
- 'discount' => $data['discount'] ?? '',
- 'page_params' => self::get_filter_data( $data['params'] ?? array() ),
- );
-
- $params['calc_id'] = ! empty( $data['id'] ) ? sanitize_text_field( $data['id'] ) : '';
- $result['data'] = self::edit_calc_body( $params );
-
- $result['success'] = true;
- $result['message'] = 'Calculator updated successfully';
- $result['calculators'] = self::get_calculator_list();
- }
-
- wp_send_json( $result );
- }
-
- /**
* Get general settings
*/
public static function calc_get_general_settings() {
@@ -867,7 +777,7 @@
self::clear_all_history();
}
- update_option( 'ccb_general_settings', $data['settings'] );
+ CCBSettingsData::update_calc_global_settings( $data['settings'] );
$result['success'] = true;
$result['message'] = __( 'Settings updated successfully', 'cost-calculator-builder' );
}
@@ -1078,6 +988,149 @@
wp_send_json( $result );
}
+ public static function ccb_duplicate_preset() {
+ check_ajax_referer( 'ccb_duplicate_preset', 'nonce' );
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
+ }
+
+ $result = array(
+ 'success' => false,
+ 'message' => 'Something went wrong',
+ );
+
+ if ( isset( $_GET['selectedIdx'] ) ) {
+ $preset_key = sanitize_text_field( $_GET['selectedIdx'] );
+ $presets_from_db = CCBPresetGenerator::get_static_preset_from_db( true );
+
+ $source_data = null;
+ if ( isset( $presets_from_db[ $preset_key ]['data'] ) ) {
+ $source_data = $presets_from_db[ $preset_key ]['data'];
+ } else {
+ $default_presets = CCBPresetGenerator::default_presets();
+ if ( isset( $default_presets[ $preset_key ]['data'] ) ) {
+ $source_data = $default_presets[ $preset_key ]['data'];
+ }
+ }
+
+ if ( ! empty( $source_data ) ) {
+ $max_idx = 0;
+ foreach ( array_keys( $presets_from_db ) as $k ) {
+ if ( preg_match( '/^saved_(d+)$/', $k, $m ) ) {
+ $max_idx = max( $max_idx, (int) $m[1] );
+ }
+ }
+ $new_preset = CCBPresetGenerator::extend_preset( $max_idx + 1, $source_data );
+ $new_key = $new_preset['key'];
+ $presets_from_db[ $new_key ] = $new_preset;
+
+ CCBPresetGenerator::update_presets( apply_filters( 'ccb_appearance_data_update', $presets_from_db ) );
+
+ $presets_data = CCBAppearanceHelper::get_appearance_data( $new_key );
+ $result = array(
+ 'success' => true,
+ 'message' => 'Preset duplicated successfully',
+ 'list' => self::normalize_save_custom_presets( $presets_data['list'] ?? array() ),
+ 'data' => $presets_data['data'] ?? array(),
+ 'new_preset_key' => $new_key,
+ );
+ }
+ }
+
+ wp_send_json( $result );
+ }
+
+ public static function ccb_extend_preset() {
+ check_ajax_referer( 'ccb_extend_preset', 'nonce' );
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
+ }
+
+ $result = array(
+ 'success' => false,
+ 'message' => 'Something went wrong',
+ );
+
+ if ( isset( $_GET['selectedIdx'] ) ) {
+ $preset_key = sanitize_text_field( $_GET['selectedIdx'] );
+ $presets = CCBAppearanceHelper::get_appearance_data( $preset_key );
+
+ if ( isset( $presets['data'] ) ) {
+ $result = array(
+ 'success' => true,
+ 'message' => 'Preset extended successfully',
+ 'data' => $presets['data'],
+ );
+ }
+ }
+
+ wp_send_json( $result );
+ }
+
+ public static function ccb_get_preset() {
+ check_ajax_referer( 'ccb_get_preset', 'nonce' );
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
+ }
+
+ $result = array(
+ 'success' => false,
+ 'message' => 'Something went wrong',
+ );
+
+ if ( isset( $_GET['selectedIdx'] ) ) {
+ $preset_key = sanitize_text_field( $_GET['selectedIdx'] );
+ $presets = CCBAppearanceHelper::get_appearance_data( $preset_key );
+
+ if ( isset( $presets['data'] ) ) {
+ $result = array(
+ 'success' => true,
+ 'message' => 'Theme loaded',
+ 'data' => $presets['data'],
+ );
+ }
+ }
+
+ wp_send_json( $result );
+ }
+
+ public static function ccb_select_preset() {
+ check_ajax_referer( 'ccb_select_preset', 'nonce' );
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
+ }
+
+ $result = array(
+ 'success' => false,
+ 'message' => 'Something went wrong',
+ );
+
+ if ( isset( $_GET['calc_id'] ) && isset( $_GET['selectedIdx'] ) ) {
+ $calc_id = intval( $_GET['calc_id'] );
+ $preset_key = sanitize_text_field( $_GET['selectedIdx'] );
+
+ if ( ! CCBPresetGenerator::preset_exist( $preset_key ) ) {
+ $preset_key = 'default';
+ }
+
+ CCBPresetGenerator::update_preset_key( $calc_id, $preset_key );
+
+ $result = array(
+ 'success' => true,
+ 'message' => 'Theme selected',
+ 'data' => array(
+ 'selected_preset_key' => $preset_key,
+ ),
+ );
+ }
+
+ wp_send_json( $result );
+ }
+
public static function ccb_update_preset() {
check_ajax_referer( 'ccb_update_preset', 'nonce' );
@@ -1092,8 +1145,7 @@
if ( isset( $_GET['calc_id'] ) && isset( $_GET['selectedIdx'] ) ) {
$preset_key = sanitize_text_field( $_GET['selectedIdx'] );
- $temp_key = ! empty( $_GET['editCustom'] ) ? sanitize_text_field( $_GET['editCustom'] ) : $preset_key;
- $presets = CCBAppearanceHelper::get_appearance_data( $temp_key );
+ $presets = CCBAppearanceHelper::get_appearance_data( $preset_key );
if ( isset( $presets['data'] ) ) {
$result = array(
@@ -1103,9 +1155,7 @@
'list' => $presets['list'],
);
- if ( 'custom' !== $preset_key ) {
- CCBPresetGenerator::update_preset_key( $_GET['calc_id'], sanitize_text_field( $preset_key ) );
- }
+ CCBPresetGenerator::update_preset_key( $_GET['calc_id'], sanitize_text_field( $preset_key ) );
}
}
@@ -1280,7 +1330,7 @@
wp_send_json( $result );
}
- private static function savepoint( $data = array() ) {
+ public static function savepoint( $data = array() ) {
if ( empty( $data ) ) {
return false;
}
@@ -1416,12 +1466,12 @@
$calc_settings['sticky_calc']['display_type'] = $type;
}
- update_option( 'stm_ccb_form_settings_' . sanitize_text_field( $calculator->ID ), apply_filters( 'stm_ccb_sanitize_array', $calc_settings ) );
+ CCBSettingsData::update_calc_single_settings( $calculator->ID, $calc_settings );
}
} elseif ( 'btn' === $type ) {
$calc_settings = CCBSettingsData::get_calc_single_settings( $calc_id );
$calc_settings['sticky_calc']['display_type'] = 'btn';
- update_option( 'stm_ccb_form_settings_' . sanitize_text_field( $calc_id ), apply_filters( 'stm_ccb_sanitize_array', $calc_settings ) );
+ CCBSettingsData::update_calc_single_settings( $calc_id, $calc_settings );
}
$result['success'] = true;
--- a/cost-calculator-builder/includes/classes/CCBCalculatorsHandler.php
+++ b/cost-calculator-builder/includes/classes/CCBCalculatorsHandler.php
@@ -0,0 +1,787 @@
+<?php
+
+namespace cBuilderClasses;
+
+use cBuilderHelpersCCBFieldsHelper;
+use cBuilderHelpersCCBOrderFormFieldsHelper;
+use cBuilderClassesDatabaseFormFields;
+use cBuilderClassesDatabaseForms;
+use cBuilderClassesAppearanceCCBAppearanceHelper;
+use cBuilderClassesAppearancePresetsCCBPresetGenerator;
+
+class CCBCalculatorsHandler {
+ public static function ccb_fetch_calculators() {
+ check_ajax_referer( 'ccb_fetch_calculators', 'nonce' );
+
+ $result = array(
+ 'success' => false,
+ 'message' => __( 'You are not allowed to run this action', 'cost-calculator-builder' ),
+ 'data' => array(),
+ );
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json( $result );
+ }
+
+ $result['success'] = true;
+ $result['message'] = __( 'Calculators fetched successfully', 'cost-calculator-builder' );
+ $result['data'] = array(
+ 'calculators' => self::get_calculators(),
+ 'total' => self::get_total(),
+ );
+
+ wp_send_json( $result );
+ }
+
+ public static function ccb_fetch_templates() {
+ check_ajax_referer( 'ccb_fetch_templates', 'nonce' );
+
+ $result = array(
+ 'success' => false,
+ 'message' => __( 'You are not allowed to run this action', 'cost-calculator-builder' ),
+ 'data' => array(),
+ );
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json( $result );
+ }
+
+ $result['success'] = true;
+ $result['message'] = __( 'Templates fetched successfully', 'cost-calculator-builder' );
+ $result['data'] = array(
+ 'templates' => self::get_templates(),
+ 'categories' => self::get_categories(),
+ 'favorites' => self::get_favorites(),
+ 'admin_email' => self::get_admin_email(),
+ 'pro_active' => self::get_pro_active(),
+ 'unlock' => self::get_unlock(),
+ );
+
+ wp_send_json( $result );
+ }
+
+
+ public static function create_calc_id() {
+ check_ajax_referer( 'ccb_create_id', 'nonce' );
+
+ $result = array(
+ 'success' => false,
+ 'message' => __( 'You are not allowed to run this action', 'cost-calculator-builder' ),
+ 'data' => array(),
+ );
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json( $result );
+ }
+
+ // create cost-calc post and get id
+ $id = wp_insert_post(
+ array(
+ 'post_type' => 'cost-calc',
+ 'post_status' => 'draft',
+ )
+ );
+
+ $data = array(
+ 'id' => $id,
+ 'url' => 'page=cost_calculator_builder&action=edit&id=' . $id,
+ );
+
+ $result['success'] = true;
+ $result['message'] = __( 'Calculator created successfully', 'cost-calculator-builder' );
+ $result['data'] = $data;
+
+ wp_send_json( $result );
+ }
+
+ public static function edit_calc() {
+ check_ajax_referer( 'ccb_edit_calc', 'nonce' );
+
+ $result = array(
+ 'success' => false,
+ 'message' => __( 'You are not allowed to run this action', 'cost-calculator-builder' ),
+ 'data' => array(),
+ );
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json( $result );
+ }
+
+ $id = isset( $_GET['id'] ) ? $_GET['id'] : 0;
+
+ if ( empty( $id ) ) {
+ $result['message'] = __( 'Invalid input', 'cost-calculator-builder' );
+ wp_send_json( $result );
+ }
+
+ $result['success'] = true;
+ $result['message'] = __( 'Calculator edited successfully', 'cost-calculator-builder' );
+ $result['data'] = array(
+ 'url' => 'page=cost_calculator_builder&action=edit&id=' . $id,
+ );
+
+ wp_send_json( $result );
+ }
+
+ public static function use_template() {
+ check_ajax_referer( 'ccb_use_template', 'nonce' );
+
+ $result = array(
+ 'success' => false,
+ 'message' => __( 'You are not allowed to run this action', 'cost-calculator-builder' ),
+ 'data' => array(),
+ );
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json( $result );
+ }
+
+ $request_body = file_get_contents( 'php://input' );
+ $request_data = json_decode( $request_body, true );
+ $data = apply_filters( 'stm_ccb_sanitize_array', $request_data );
+ $template_id = ! isset( $data['template_id'] ) ? null : $data['template_id'];
+
+ $result = array(
+ 'success' => false,
+ 'message' => __( 'Could not duplicate calculator, please try again!', 'cost-calculator-builder' ),
+ 'data' => array(),
+ );
+
+ if ( ! is_null( $template_id ) ) {
+ $calc_id = get_post_meta( $template_id, 'calc_id', true );
+ $category = get_post_meta( $template_id, 'category', true );
+ $data = CCBCalculators::duplicate_target_calc( $calc_id, false );
+
+ if ( 'custom_templates' !== $category ) {
+ $data['preset_idx'] = 'default';
+ }
+
+ if ( ccb_update_calc_values( $data ) ) {
+ $result['success'] = true;
+ $result['message'] = __( 'Calculator created successfully', 'cost-calculator-builder' );
+ $result['data'] = array(
+ 'id' => $data['id'],
+ );
+ }
+ }
+
+ wp_send_json( $result );
+ }
+
+ public static function run_calc_updates() {
+ check_ajax_referer( 'ccb_run_calc_updates', 'nonce' );
+
+ $result = array(
+ 'success' => false,
+ 'message' => __( 'You are not allowed to run this action', 'cost-calculator-builder' ),
+ 'data' => array(),
+ );
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json( $result );
+ }
+
+ $updates = CCBUpdates::get_updates();
+
+ $data = $_POST;
+ if ( empty( $_POST ) ) {
+ $request_body = file_get_contents( 'php://input' );
+ $request_data = json_decode( $request_body, true );
+ $data = apply_filters( 'stm_ccb_sanitize_array', $request_data );
+ }
+
+ if ( current_user_can( 'manage_options' ) && 'calc_run_updates' === $data['action'] && ! empty( $data['access'] ) ) {
+ foreach ( $updates as $callback_arr ) {
+ foreach ( $callback_arr as $callback ) {
+ call_user_func( array( '\cBuilder\Classes\CCBUpdatesCallbacks', $callback ) );
+ }
+ }
+ }
+
+ $result['success'] = true;
+ $result['message'] = __( 'Calculators updated successfully', 'cost-calculator-builder' );
+ $result['data'] = array();
+
+ wp_send_json( $result );
+ }
+
+ public static function delete_calc() {
+ check_ajax_referer( 'ccb_delete_calc', 'nonce' );
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
+ }
+
+ $result = array(
+ 'success' => false,
+ 'message' => __( 'Could not delete calculator, please try again!', 'cost-calculator-builder' ),
+ );
+
+ if ( isset( $_GET['ids'] ) ) {
+ $ids = array_map(
+ function ( $item ) {
+ return (int) sanitize_text_field( $item );
+ },
+ explode( ',', $_GET['ids'] )
+ );
+
+ foreach ( $ids as $id ) {
+ wp_delete_post( $id );
+ clearMetaData( $id );
+ ccb_update_woocommerce_calcs( $id, true );
+ }
+
+ $result['success'] = true;
+ $result['message'] = __( 'Calculators deleted successfully', 'cost-calculator-builder' );
+ }
+
+ wp_send_json( $result );
+ }
+
+ public static function duplicate_calc() {
+
+ check_ajax_referer( 'ccb_duplicate_calc', 'nonce' );
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
+ }
+
+ $result = array(
+ 'success' => false,
+ 'message' => __( "Couldn't duplicate calculator, please try again!', 'cost-calculator-builder" ),
+ );
+
+ if ( isset( $_GET['ids'] ) ) {
+
+ $ids = array_map(
+ function ( $item ) {
+ return (int) sanitize_text_field( $item );
+ },
+ explode( ',', $_GET['ids'] )
+ );
+
+ foreach ( $ids as $id ) {
+ $new_calculator = array(
+ 'post_parent' => $id,
+ 'post_status' => 'publish',
+ 'post_type' => 'cost-calc',
+ );
+
+ $duplicated_post_id = wp_insert_post( $new_calculator );
+
+ $data = array(
+ 'id' => $duplicated_post_id,
+ 'title' => CCBCalculators::get_calculator_name_for_duplicate( $id ),
+ 'formula' => get_post_meta( $id, 'stm-formula', true ),
+ 'settings' => CCBSettingsData::get_calc_single_settings( $id ),
+ 'builder' => get_post_meta( $id, 'stm-fields', true ),
+ 'conditions' => get_post_meta( $id, 'stm-conditions', true ),
+ 'appearance' => get_post_meta( $id, 'ccb-appearance', true ),
+ 'preset_idx' => get_post_meta( $id, 'ccb_calc_preset_idx', true ),
+ );
+
+ if ( ccb_update_calc_values( $data ) ) {
+ $result['success'] = true;
+ $result['message'] = __( 'Calculator duplicated successfully', 'cost-calculator-builder' );
+ } else {
+ $result['success'] = false;
+ $result['message'] = __( 'Could not duplicate calculator, please try again!', 'cost-calculator-builder' );
+ }
+ }
+ }
+
+ wp_send_json( $result );
+ }
+
+ public static function get_calculator_admin_data() {
+ check_ajax_referer( 'ccb_get_calculator_admin_data', 'nonce' );
+
+ $result = array(
+ 'success' => false,
+ 'message' => __( 'You are not allowed to run this action', 'cost-calculator-builder' ),
+ 'data' => array(),
+ );
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json( $result );
+ }
+
+ $calc_id = isset( $_GET['calc_id'] ) ? $_GET['calc_id'] : 0;
+
+ if ( empty( $calc_id ) ) {
+ $result['message'] = __( 'Invalid input', 'cost-calculator-builder' );
+ wp_send_json( $result );
+ }
+
+ $result['success'] = true;
+ $result['message'] = __( 'Calculator admin data fetched successfully', 'cost-calculator-builder' );
+ $result['data'] = self::parse_calc_data( $calc_id );
+
+ wp_send_json( $result );
+ }
+
+ public static function save_settings() {
+ check_ajax_referer( 'ccb_save_settings', 'nonce' );
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json_error( __( 'You are not allowed to run this action', 'cost-calculator-builder' ) );
+ }
+
+ $result = array(
+ 'success' => false,
+ 'message' => 'Something went wrong',
+ );
+
+ $request_body = file_get_contents( 'php://input' );
+ $request_data = json_decode( $request_body, true );
+ $data = apply_filters( 'stm_ccb_sanitize_array', $request_data );
+
+ if ( isset( $data['settings']['formFields']['body'] ) ) {
+ $content = $data['settings']['formFields']['body'];
+ $content = str_replace( '\n', '<br>', $content );
+ $data['settings']['formFields']['body'] = str_replace( '\', '', $content );
+ }
+
+ if ( ! empty( $data ) && ccb_update_calc_values( $data ) ) {
+ $general_settings = CCBSettingsData::get_calc_global_settings();
+ if ( isset( $general_settings['backup_settings'] ) && true === filter_var( $general_settings['backup_settings']['auto_backup'], FILTER_VALIDATE_BOOLEAN ) ) {
+ CCBCalculators::savepoint( $data );
+ }
+
+ $params['calc_id'] = ! empty( $data['id'] ) ? sanitize_text_field( $data['id'] ) : '';
+ $calc_data = CCBCalculators::edit_calc_body( $params );
+ $total_summary = get_post_meta( intval( $data['id'] ), 'stm-total-summary', true );
+
+ $result['success'] = true;
+ $result['message'] = 'Calculator updated successfully';
+ $result['data'] = array(
+ 'settings' => $calc_data['settings'],
+ 'total_summary' => ! empty( $total_summary ) ? $total_summary : CCBSettingsData::total_summary_settings_data( $calc_data['settings']['general'] ?? array() ),
+ );
+ }
+
+ wp_send_json( $result );
+ }
+
+ private static function get_calculators() {
+ global $wpdb;
+
+ $limit = isset( $_GET['limit'] ) ? $_GET['limit'] : 10;
+ $page = isset( $_GET['page'] ) ? $_GET['page'] : 1;
+ $sortBy = isset( $_GET['sort_by'] ) ? $_GET['sort_by'] : 'id';
+ $direction = isset( $_GET['direction'] ) ? $_GET['direction'] : 'desc';
+ $offset = 1 === $page ? 0 : ( $page - 1 ) * $limit;
+
+ // phpcs:disable
+ $query = $wpdb->prepare(
+ "SELECT ID as id, post_date as created_at, post_title as title, post_title as title FROM {$wpdb->posts} WHERE post_type = 'cost-calc' AND post_status = 'publish' ORDER BY {$sortBy} {$direction} LIMIT %d OFFSET %d",
+ $limit,
+ $offset
+ );
+ // phpcs:enable
+
+ $calculators = $wpdb->get_results( $query, ARRAY_A ); // phpcs:ignore
+
+ foreach ( $calculators as $key => $calculator ) {
+ $calculators[ $key ]['icon'] = self::get_calculator_icon( $calculator['id'] );
+ $calculators[ $key ]['image'] = self::get_calculator_image( $calculator['id'] );
+ }
+
+ return $calculators;
+ }
+
+ private static function get_calculator_icon( $id ) {
+ $source = get_post_meta( $id, 'ccb_icon', true );
+ if ( ! empty( $source ) ) {
+ return $source;
+ }
+
+ $avatar_index = random_int( 0, 9 );
+ $avatar_name = 0 === $avatar_index ? 'avatar.png' : 'avatar_' . $avatar_index . '.png';
+
+ return CALC_URL . '/frontend/vue3/assets/images/avatars/' . $avatar_name;
+ }
+
+ private static function get_calculator_image( $id ) {
+ $source = get_post_meta( $id, 'ccb_image', true );
+ if ( ! empty( $source ) ) {
+ return $source;
+ }
+
+ return CALC_URL . '/frontend/vue3/assets/images/calc-preview.png';
+ }
+
+ private static function get_templates() {
+ $templates = CCBCalculatorTemplates::calc_templates_list();
+ if ( is_array( $templates ) ) {
+ return $templates;
+ }
+
+ return array();
+ }
+
+ private static function get_categories() {
+ $categories = CCBCategory::calc_categories_list();
+ if ( is_array( $categories ) ) {
+
+ $data = array();
+ $templates = self::get_templates();
+
+ foreach ( $templates as $template ) {
+ if ( ! isset( $data[ $template['category'] ] ) ) {
+ $data[ $template['category'] ] = 0;
+ }
+
+ $data[ $template['category'] ]++;
+ }
+
+ foreach ( $categories as $key => $category ) {
+ $categories[ $key ]['count'] = isset( $data[ $category['slug'] ] ) ? $data[ $category['slug'] ] : 0;
+ }
+
+ return $categories;
+ }
+
+ return array();
+ }
+
+ private static function get_favorites() {
+ return array_values( get_option( 'calc_templates_favorites', array() ) );
+ }
+
+ private static function get_admin_email() {
+ return get_option( 'ccb_lock_templates_email' );
+ }
+
+ private static function get_pro_active() {
+ return defined( 'CCB_PRO_VERSION' );
+ }
+
+ private static function get_unlock() {
+ return get_option( 'ccb_lock_templates', false );
+ }
+
+ private static function get_total() {
+ global $wpdb;
+ return $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'cost-calc' AND post_status = 'publish'" );
+ }
+
+ private static function get_filter_data( $data ) {
+ $sort_by = ! empty( $data['sortBy'] ) ? sanitize_text_field( $data['sortBy'] ) : 'id';
+ $direction = ! empty( $data['direction'] ) ? sanitize_text_field( $data['direction'] ) : 'desc';
+ $page = ! empty( $data['page'] ) ? (int) sanitize_text_field( $data['page'] ) : 1;
+ $limit = ! empty( $data['limit'] ) ? sanitize_text_field( $data['limit'] ) : 5;
+ $search = ! empty( $data['search'] ) ? sanitize_text_field( $data['search'] ) : '';
+ $offset = 1 === $page ? 0 : ( $page - 1 ) * $limit;
+ $direction = 'desc' === strtolower( $direction ) ? $direction : 'asc';
+
+ return array(
+ 'page' => $page,
+ 'limit' => $limit,
+ 'offset' => $offset,
+ 'sort_by' => $sort_by,
+ 'direction' => $direction,
+ 'search' => $search,
+ );
+ }
+
+ private static function parse_calc_data( $calc_id ) {
+ $result = CCBCalculators::get_default_calculator_data();
+
+ if ( ! empty( $calc_id ) ) {
+ $stm_fields = get_post_meta( $calc_id, 'stm-fields', true );
+
+ $result['calculator_builder'] = array(
+ 'id' => $calc_id,
+ 'title' => get_post_meta( $calc_id, 'stm-name', true ),
+ 'formula' => get_post_meta( $calc_id, 'stm-formula', true ),
+ 'builder' => ! empty( $stm_fields ) ? $stm_fields : array(),
+ 'fields' => CCBFieldsHelper::fields(),
+ );
+ $result['saved'] = (bool) get_post_meta( $calc_id, 'calc_saved', true );
+
+ $all_forms = Forms::get_all_forms();
+ if ( empty( $all_forms ) ) {
+ Forms::create_default_form_for_calculator( $calc_id );
+ $all_forms = Forms::get_all_forms();
+ }
+
+ $active_form_id = CCBOrderFormFieldsHelper::get_active_form_id( $calc_id );
+ if ( empty( $active_form_id ) && ! empty( $all_forms ) ) {
+ $active_form_id = $all_forms[0]['id'];
+ update_post_meta( $calc_id, 'form_id', $active_form_id );
+ }
+
+ if ( ccb_pro_active() ) {
+ $result['order_form_manager'] = array(
+ 'order_form_fields' => CCBOrderFormFieldsHelper::order_form_fields(),
+ 'active_form_id' => $active_form_id,
+ 'order_forms' => $all_forms,
+ 'order_active_form_fields' => FormFields::get_active_fields( intval( $active_form_id ) ),
+ );
+
+ $result['payments'] = cBuilderClassesCCBProSettings::get_payments();
+
+ $conditions = get_post_meta( $calc_id, 'stm-conditions', true );
+ $result['conditions'] = array(
+ 'nodes' => $conditions['nodes_v4'] ?? array(),
+ 'links' => $conditions['links_v4'] ?? array(),
+ 'spaces' => $conditions['spaces'] ?? array(),
+ );
+ } else {
+ $result['conditions'] = array(
+ 'nodes' => array(),
+ 'links' => array(),
+ 'spaces' => array(),
+ );
+
+ $result['order_form_manager'] = array(
+ 'order_form_fields' => array(),
+ 'active_form_id' => null,
+ 'order_forms' => array(),
+ 'order_active_form_fields' => array(),
+ );
+ }
+
+ $settings = self::get_calc_settings_and_general_settings( $calc_id );
+ if ( ! empty( $settings['settings'] ) ) {
+ $result['settings'] = $settings['settings'];
+ }
+
+ if ( ! empty( $settings['general_settings'] ) ) {
+ $result['general_settings'] =