From 31b3398c2f967ca299307d5de2a66e62d24fcf22 Mon Sep 17 00:00:00 2001 From: dwindown Date: Sun, 7 Dec 2025 12:58:49 +0700 Subject: [PATCH] Fix bugs --- .gitignore | 1 + assets/public.js | 26 +++++-- dw-sheet-data-checker-pro.php | 40 +++++++--- includes/class-Sheet-Data-Checker-Pro.php | 8 +- includes/class-Shortcode.php | 91 +++++++++++++++-------- 5 files changed, 120 insertions(+), 46 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..05eb778 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +compile.sh \ No newline at end of file diff --git a/assets/public.js b/assets/public.js index 29a0615..b38f884 100644 --- a/assets/public.js +++ b/assets/public.js @@ -227,6 +227,22 @@ jQuery(document).ready(function($){ } } + // Helper function to get output setting by key + function getOutputSetting(output, key) { + // Convert output object to array if needed + if (Array.isArray(output)) { + return output.find(function(o) { return o.key === key; }); + } else { + // output is an object, find by matching key property + for (var prop in output) { + if (output[prop].key === key) { + return output[prop]; + } + } + } + return null; + } + // Render vertical table display function renderVerticalTable(checkerId, res) { var thisChecker = $('#checker-' + checkerId); @@ -240,7 +256,7 @@ jQuery(document).ready(function($){ $.each(row, function(q, r) { var id = q.replace(/\s/g, '_').replace(/\./g, '_').toLowerCase(); - var outputSetting = res.output.find(function(o) { return o.key === q; }); + var outputSetting = getOutputSetting(res.output, q); if (!outputSetting || outputSetting.hide === 'yes') return; @@ -283,7 +299,7 @@ jQuery(document).ready(function($){ // Headers if (res.rows.length > 0) { $.each(res.rows[0], function(q, r) { - var outputSetting = res.output.find(function(o) { return o.key === q; }); + var outputSetting = getOutputSetting(res.output, q); if (!outputSetting || outputSetting.hide === 'yes') return; resultDiv += ''+q+''; }); @@ -295,7 +311,7 @@ jQuery(document).ready(function($){ resultDiv += ''; $.each(row, function(q, r) { var id = q.replace(/\s/g, '_').replace(/\./g, '_').toLowerCase(); - var outputSetting = res.output.find(function(o) { return o.key === q; }); + var outputSetting = getOutputSetting(res.output, q); if (!outputSetting || outputSetting.hide === 'yes') return; @@ -343,7 +359,7 @@ jQuery(document).ready(function($){ $.each(row, function(q, r) { var id = q.replace(/\s/g, '_').replace(/\./g, '_').toLowerCase(); - var outputSetting = res.output.find(function(o) { return o.key === q; }); + var outputSetting = getOutputSetting(res.output, q); if (!outputSetting || outputSetting.hide === 'yes') return; @@ -391,7 +407,7 @@ jQuery(document).ready(function($){ $.each(row, function(q, r) { var id = q.replace(/\s/g, '_').replace(/\./g, '_').toLowerCase(); - var outputSetting = res.output.find(function(o) { return o.key === q; }); + var outputSetting = getOutputSetting(res.output, q); if (!outputSetting || outputSetting.hide === 'yes') return; diff --git a/dw-sheet-data-checker-pro.php b/dw-sheet-data-checker-pro.php index 752dc7e..34a599a 100644 --- a/dw-sheet-data-checker-pro.php +++ b/dw-sheet-data-checker-pro.php @@ -2,7 +2,7 @@ /** * Plugin Name: Sheet Data Checker Pro * Description: Check data from Google Sheet with customizable filter form - * Version: 1.4.0 + * Version: 1.4.2 * Plugin URI: https://dwindi.com/sheet-data-checker * Author: Dwindi Ramadhana * Author URI: https://facebook.com/dwindi.ramadhana @@ -22,13 +22,35 @@ // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) exit; -define( 'SHEET_CHECKER_PRO_NAME', 'Sheet Data Checker Pro' ); -define( 'SHEET_CHECKER_PRO_MEMBER', 'https://member.dwindi.com' ); -define( 'SHEET_CHECKER_PRO_BASENAME', plugin_basename(__FILE__)); -define( 'SHEET_CHECKER_PRO_VERSION', '1.4.0' ); -define( 'SHEET_CHECKER_PRO_URL', plugin_dir_url( __FILE__ ) ); -define( 'SHEET_CHECKER_PRO_PATH', plugin_dir_path( __FILE__ ) ); -define( 'SHEET_CHECKER_PRO_DOMAIN', 'sheet-data-checker-pro' ); +if ( ! defined( 'SHEET_CHECKER_PRO_NAME' ) ) { + define( 'SHEET_CHECKER_PRO_NAME', 'Sheet Data Checker Pro' ); +} -require_once SHEET_CHECKER_PRO_PATH . 'includes/class-Sheet-Data-Checker-Pro.php'; +if ( ! defined( 'SHEET_CHECKER_PRO_MEMBER' ) ) { + define( 'SHEET_CHECKER_PRO_MEMBER', 'https://member.dwindi.com' ); +} + +if ( ! defined( 'SHEET_CHECKER_PRO_BASENAME' ) ) { + define( 'SHEET_CHECKER_PRO_BASENAME', plugin_basename( __FILE__ ) ); +} + +if ( ! defined( 'SHEET_CHECKER_PRO_VERSION' ) ) { + define( 'SHEET_CHECKER_PRO_VERSION', '1.4.2' ); +} + +if ( ! defined( 'SHEET_CHECKER_PRO_URL' ) ) { + define( 'SHEET_CHECKER_PRO_URL', plugin_dir_url( __FILE__ ) ); +} + +if ( ! defined( 'SHEET_CHECKER_PRO_PATH' ) ) { + define( 'SHEET_CHECKER_PRO_PATH', plugin_dir_path( __FILE__ ) ); +} + +if ( ! defined( 'SHEET_CHECKER_PRO_DOMAIN' ) ) { + define( 'SHEET_CHECKER_PRO_DOMAIN', 'sheet-data-checker-pro' ); +} + +if (!class_exists('SHEET_DATA_CHECKER_PRO')) { + require_once SHEET_CHECKER_PRO_PATH . 'includes/class-Sheet-Data-Checker-Pro.php'; +} new SHEET_DATA_CHECKER_PRO(); \ No newline at end of file diff --git a/includes/class-Sheet-Data-Checker-Pro.php b/includes/class-Sheet-Data-Checker-Pro.php index b1fccf2..14fdd2d 100644 --- a/includes/class-Sheet-Data-Checker-Pro.php +++ b/includes/class-Sheet-Data-Checker-Pro.php @@ -24,7 +24,9 @@ class SHEET_DATA_CHECKER_PRO { add_action( 'init', [$this, 'create_custom_post_type'] ); add_action( 'admin_enqueue_scripts', [$this, 'enqueue_bootstrap_admin'] ); - include SHEET_CHECKER_PRO_PATH . 'includes/class-License.php'; + if (!class_exists('CHECKER_LICENSE')) { + include SHEET_CHECKER_PRO_PATH . 'includes/class-License.php'; + } $lis = new CHECKER_LICENSE(); if(true == $lis->the_lis()){ @@ -38,7 +40,9 @@ class SHEET_DATA_CHECKER_PRO { add_action( 'wp_ajax_load_repeater_field_card', [$this, 'load_repeater_field_card'] ); add_action( 'wp_ajax_load_output_setting', [$this, 'load_output_setting'] ); - require 'class-Shortcode.php'; + if (!class_exists('CHECKER_SHORTCODE')) { + require 'class-Shortcode.php'; + } new CHECKER_SHORTCODE(); } diff --git a/includes/class-Shortcode.php b/includes/class-Shortcode.php index 474defa..c53cd90 100644 --- a/includes/class-Shortcode.php +++ b/includes/class-Shortcode.php @@ -22,7 +22,9 @@ class CHECKER_SHORTCODE extends SHEET_DATA_CHECKER_PRO { public function __construct() { // Load security class - require_once SHEET_CHECKER_PRO_PATH . 'includes/class-Security.php'; + if (!class_exists('CHECKER_SECURITY')) { + require_once SHEET_CHECKER_PRO_PATH . 'includes/class-Security.php'; + } add_shortcode('checker', [$this, 'content'] ); add_action( 'wp_enqueue_scripts', [$this, 'enqueue'] ); @@ -101,13 +103,8 @@ class CHECKER_SHORTCODE extends SHEET_DATA_CHECKER_PRO { // Set the delimiter based on the format $delimiter = $link_format == 'tsv' ? "\t" : ","; // Use tab for TSV, comma for CSV - if (($handle = fopen($url, "r")) !== false) { - $keys = fgetcsv($handle, 0, $delimiter); // Read the first row as keys - while (($row = fgetcsv($handle, 0, $delimiter)) !== false) { - $data[] = array_combine($keys, $row); // Combine keys with row values and add to the data array - } - fclose($handle); - } + // Use WordPress HTTP API instead of fopen for better server compatibility + $data = $this->fetch_remote_csv_data($url, $delimiter); $background_color = $checker['card']['background']; if($checker['card']['bg_opacity'] < 100){ $background_color = $checker['card']['background'].''.$checker['card']['bg_opacity']; @@ -246,6 +243,58 @@ class CHECKER_SHORTCODE extends SHEET_DATA_CHECKER_PRO { } + /** + * Fetch remote CSV/TSV data using WordPress HTTP API + * Replaces fopen() for better server compatibility + */ + private function fetch_remote_csv_data($url, $delimiter, $limit = null) { + $data = []; + + // Use WordPress HTTP API to fetch remote file + $response = wp_remote_get($url); + + if (is_wp_error($response)) { + error_log('Failed to fetch remote file: ' . $response->get_error_message()); + return $data; + } + + $body = wp_remote_retrieve_body($response); + if (empty($body)) { + error_log('Empty response from remote file: ' . $url); + return $data; + } + + // Parse CSV/TSV data + $lines = explode("\n", $body); + if (empty($lines)) { + return $data; + } + + // Get headers from first line + $keys = str_getcsv($lines[0], $delimiter); + + // Process data rows + $count = 0; + for ($i = 1; $i < count($lines); $i++) { + if (empty(trim($lines[$i]))) { + continue; // Skip empty lines + } + + $row = str_getcsv($lines[$i], $delimiter); + if (count($keys) === count($row)) { + $data[] = array_combine($keys, $row); + $count++; + + // Apply limit if specified + if ($limit && $count >= $limit) { + break; + } + } + } + + return $data; + } + public function checker_public_validation() { $post_id = $_REQUEST['checker_id']; @@ -295,13 +344,8 @@ class CHECKER_SHORTCODE extends SHEET_DATA_CHECKER_PRO { // Set the delimiter based on the format $delimiter = $link_format == 'tsv' ? "\t" : ","; // Use tab for TSV, comma for CSV - if (($handle = fopen($url, "r")) !== false) { - $keys = fgetcsv($handle, 0, $delimiter); // Read the first row as keys - while (($row = fgetcsv($handle, 0, $delimiter)) !== false) { - $data[] = array_combine($keys, $row); // Combine keys with row values and add to the data array - } - fclose($handle); - } + // Use WordPress HTTP API instead of fopen for better server compatibility + $data = $this->fetch_remote_csv_data($url, $delimiter); $validator = $_REQUEST['validate']; $validation = []; @@ -380,21 +424,8 @@ class CHECKER_SHORTCODE extends SHEET_DATA_CHECKER_PRO { $link_format = substr($url, -3); $delimiter = $link_format == 'tsv' ? "\t" : ","; - $data = []; - $handle = fopen($url, "r"); - - if ($handle !== false) { - $keys = fgetcsv($handle, 0, $delimiter); - $count = 0; - - while (($row = fgetcsv($handle, 0, $delimiter)) !== false && $count < $limit) { - if (count($keys) === count($row)) { - $data[] = array_combine($keys, $row); - $count++; - } - } - fclose($handle); - } + // Use WordPress HTTP API instead of fopen for better server compatibility + $data = $this->fetch_remote_csv_data($url, $delimiter, $limit); wp_send_json([ 'count' => count($data),