'GET', 'callback' => array($this, 'get_fields'), 'permission_callback' => array($this, 'check_permission'), )); register_rest_route('cfep/v1', '/fields', array( 'methods' => 'POST', 'callback' => array($this, 'save_field'), 'permission_callback' => array($this, 'check_permission'), )); register_rest_route('cfep/v1', '/fields/(?P[a-zA-Z0-9_-]+)', array( 'methods' => 'DELETE', 'callback' => array($this, 'delete_field'), 'permission_callback' => array($this, 'check_permission'), )); register_rest_route('cfep/v1', '/fields/reorder', array( 'methods' => 'POST', 'callback' => array($this, 'reorder_fields'), 'permission_callback' => array($this, 'check_permission'), )); // Reset classic fields to default register_rest_route('cfep/v1', '/fields/reset', array( 'methods' => 'POST', 'callback' => array($this, 'reset_classic_fields'), 'permission_callback' => array($this, 'check_permission'), )); register_rest_route('cfep/v1', '/templates', array( 'methods' => 'GET', 'callback' => array($this, 'get_templates'), 'permission_callback' => array($this, 'check_permission'), )); register_rest_route('cfep/v1', '/templates/(?P\d+)/apply', array( 'methods' => 'POST', 'callback' => array($this, 'apply_template'), 'permission_callback' => array($this, 'check_permission'), )); // Languages (XML) register_rest_route('cfep/v1', '/languages', array( 'methods' => 'GET', 'callback' => array($this, 'get_languages'), 'permission_callback' => array($this, 'permissions_check'), )); register_rest_route('cfep/v1', '/language', array( 'methods' => 'POST', 'callback' => array($this, 'set_language'), 'permission_callback' => array($this, 'permissions_check'), )); register_rest_route('cfep/v1', '/i18n', array( 'methods' => 'GET', 'callback' => array($this, 'get_i18n'), 'permission_callback' => array($this, 'permissions_check'), )); // Settings routes register_rest_route('cfep/v1', '/settings', array( 'methods' => 'GET', 'callback' => array($this, 'get_settings'), 'permission_callback' => array($this, 'check_permission'), )); register_rest_route('cfep/v1', '/settings', array( 'methods' => 'POST', 'callback' => array($this, 'save_settings'), 'permission_callback' => array($this, 'check_permission'), )); // NEW: Block Checkout routes $this->register_block_routes(); } public function check_permission() { return current_user_can('manage_woocommerce'); } public function permissions_check() { return current_user_can('manage_woocommerce'); } public function get_fields($request) { $field_manager = CFEP_Field_Manager::instance(); return rest_ensure_response($field_manager->get_all_fields()); } public function save_field($request) { $field_manager = CFEP_Field_Manager::instance(); $data = $request->get_json_params(); $field_key = $field_manager->save_field($data); return rest_ensure_response(array('success' => true, 'field_key' => $field_key)); } public function delete_field($request) { global $wpdb; $field_manager = CFEP_Field_Manager::instance(); $field_key = $request->get_param('key'); // Debug: Check if field exists $table = $wpdb->prefix . 'cfep_fields'; $cache_key = 'cfep_field_exists_' . $field_key; $exists = wp_cache_get($cache_key, 'cfep'); if ($exists === false) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $exists = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $table WHERE field_key = %s", $field_key ) ); wp_cache_set($cache_key, $exists, 'cfep', 300); } // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log('CFEP Delete - Field: ' . $field_key . ', Exists: ' . $exists); if (!$exists) { return rest_ensure_response(array( 'success' => false, 'message' => 'Field not found', 'field_key' => $field_key )); } $result = $field_manager->delete_field($field_key); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log('CFEP Delete - Result: ' . ($result ? 'true' : 'false')); return rest_ensure_response(array( 'success' => $result, 'field_key' => $field_key, 'message' => $result ? 'Field deleted' : 'Delete failed' )); } public function get_templates($request) { $templates = CFEP_Templates::instance(); return rest_ensure_response($templates->get_all_templates()); } public function apply_template($request) { $templates = CFEP_Templates::instance(); $template_id = $request->get_param('id'); $count = $templates->apply_template($template_id); return rest_ensure_response(array('success' => $count > 0, 'fields_added' => $count)); } public function reorder_fields($request) { $field_manager = CFEP_Field_Manager::instance(); $positions = $request->get_json_params(); if (!isset($positions['positions']) || !is_array($positions['positions'])) { return new WP_Error('invalid_data', __('Invalid positions data', 'akz-checkout-fields-manager-for-woocommerce'), array('status' => 400)); } $result = $field_manager->update_field_positions($positions['positions']); return rest_ensure_response(array('success' => $result)); } private function cfep_get_available_languages() { $langs = array(); $lang_dir = plugin_dir_path(dirname(dirname(__FILE__))) . 'languages/'; if (is_dir($lang_dir)) { foreach (glob($lang_dir . '*.xml') as $file) { $code = basename($file, '.xml'); $langs[] = array('code' => $code, 'label' => strtoupper($code)); } } return $langs; } private function cfep_load_language_strings($code) { $strings = array(); $lang_dir = plugin_dir_path(dirname(dirname(__FILE__))) . 'languages/'; $path = $lang_dir . $code . '.xml'; if (!file_exists($path)) return $strings; libxml_use_internal_errors(true); $xml = simplexml_load_file($path); if (!$xml) return $strings; foreach ($xml->string as $node) { $key = (string)$node['key']; $val = (string)$node; if ($key !== '') $strings[$key] = $val; } return $strings; } public function get_languages($request) { return rest_ensure_response($this->cfep_get_available_languages()); } public function set_language($request) { $code = sanitize_text_field($request->get_param('code')); if (empty($code)) $code = 'en'; update_option('cfep_language', $code); return rest_ensure_response(array('success' => true, 'code' => $code)); } public function get_i18n($request) { $code = get_option('cfep_language', 'en'); $strings = $this->cfep_load_language_strings($code); return rest_ensure_response(array('code' => $code, 'strings' => $strings)); } /** * Register Block Checkout routes */ public function register_block_routes() { // Get all block fields register_rest_route('cfep/v1', '/block-fields', array( 'methods' => 'GET', 'callback' => array($this, 'get_block_fields'), 'permission_callback' => array($this, 'check_permission') )); // Save block field register_rest_route('cfep/v1', '/block-fields', array( 'methods' => 'POST', 'callback' => array($this, 'save_block_field'), 'permission_callback' => array($this, 'check_permission') )); // Delete block field register_rest_route('cfep/v1', '/block-fields/(?P[a-zA-Z0-9_-]+)', array( 'methods' => 'DELETE', 'callback' => array($this, 'delete_block_field'), 'permission_callback' => array($this, 'check_permission') )); // Reorder block fields register_rest_route('cfep/v1', '/block-fields/reorder', array( 'methods' => 'POST', 'callback' => array($this, 'reorder_block_fields'), 'permission_callback' => array($this, 'check_permission') )); // Reset block fields to default register_rest_route('cfep/v1', '/block-fields/reset', array( 'methods' => 'POST', 'callback' => array($this, 'reset_block_fields'), 'permission_callback' => array($this, 'check_permission') )); } /** * Get block fields */ public function get_block_fields($request) { $block_manager = CFEP_Block_Field_Manager::instance(); $fields = $block_manager->get_all_fields(); return rest_ensure_response($fields); } /** * Save block field */ public function save_block_field($request) { $block_manager = CFEP_Block_Field_Manager::instance(); $data = $request->get_json_params(); $field_key = $block_manager->save_field($data); if ($field_key) { return rest_ensure_response(array( 'success' => true, 'field_key' => $field_key )); } return new WP_Error('save_failed', 'Failed to save block field', array('status' => 500)); } /** * Delete block field */ public function delete_block_field($request) { $block_manager = CFEP_Block_Field_Manager::instance(); $field_key = $request->get_param('key'); $result = $block_manager->delete_field($field_key); return rest_ensure_response(array( 'success' => $result )); } /** * Reorder block fields */ public function reorder_block_fields($request) { $block_manager = CFEP_Block_Field_Manager::instance(); $data = $request->get_json_params(); if (!isset($data['positions']) || !is_array($data['positions'])) { return new WP_Error('invalid_data', 'Invalid positions data', array('status' => 400)); } $result = $block_manager->update_field_positions($data['positions']); return rest_ensure_response(array( 'success' => $result )); } /** * Reset block fields to WooCommerce defaults */ public function reset_block_fields($request) { global $wpdb; $table = $wpdb->prefix . 'cfep_block_fields'; // SECURITY: Validate table name to prevent SQL injection if (!CFEP_Security::validate_table($table)) { return new WP_Error('invalid_table', 'Invalid table name', array('status' => 403)); } // ONLY reset BLOCK fields table (not Classic!) // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $wpdb->query("TRUNCATE TABLE $table"); // Force reload default WooCommerce fields if (class_exists('CFEP_Block_WC_Fields_Loader')) { $loader = CFEP_Block_WC_Fields_Loader::instance(); // Use reflection to call the private method $reflection = new ReflectionClass($loader); $method = $reflection->getMethod('insert_default_woocommerce_fields'); $method->setAccessible(true); $method->invoke($loader); } return rest_ensure_response(array( 'success' => true, 'message' => 'Block fields reset to default successfully' )); } /** * Reset classic fields to WooCommerce defaults */ public function reset_classic_fields($request) { global $wpdb; $table = $wpdb->prefix . 'cfep_fields'; // SECURITY: Validate table name to prevent SQL injection if (!CFEP_Security::validate_table($table)) { return new WP_Error('invalid_table', 'Invalid table name', array('status' => 403)); } // Delete ALL fields from table (custom AND modified defaults) // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $deleted = $wpdb->query("DELETE FROM $table"); // CRITICAL FIX: Re-insert WooCommerce default fields with proper positions require_once CFEP_PLUGIN_DIR . 'includes/class-cfep-wc-fields-loader.php'; $loader = CFEP_WC_Fields_Loader::instance(); $inserted = $loader->insert_default_woocommerce_fields(true); // Force re-insert // Clear any cached field data delete_transient('cfep_classic_fields'); delete_transient('cfep_billing_fields'); delete_transient('cfep_shipping_fields'); wp_cache_flush(); return rest_ensure_response(array( 'success' => true, 'message' => "Classic fields reset successfully! Deleted $deleted fields, re-inserted $inserted default fields with proper order.", 'deleted' => $deleted, 'inserted' => $inserted )); } /** * Get plugin settings */ public function get_settings($request) { return rest_ensure_response(array( 'show_in_emails' => get_option('cfep_show_in_emails', 'yes'), 'show_in_order_details' => get_option('cfep_show_in_order_details', 'yes') )); } /** * Save plugin settings */ public function save_settings($request) { $params = $request->get_json_params(); if (isset($params['show_in_emails'])) { update_option('cfep_show_in_emails', $params['show_in_emails']); } if (isset($params['show_in_order_details'])) { update_option('cfep_show_in_order_details', $params['show_in_order_details']); } return rest_ensure_response(array( 'success' => true, 'message' => 'Settings saved successfully' )); } }