Plugin Directory

Changeset 3352566


Ignore:
Timestamp:
08/29/2025 11:31:08 AM (6 months ago)
Author:
wisernotify
Message:

Updating trunk to version 2.1

Location:
wiser-review/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • wiser-review/trunk/assets/js/wiserw-js.js

    r3349699 r3352566  
    8080        var to_date = $("#wiserrw_to_date").val();
    8181        let offset = 0;
    82         const limit = 1;
     82        const limit = 50;
     83        var fetch_type = $("#wiserrw_fetch_type").val();
     84        let progressUpdateTimer = null;
     85       
     86        function updateProgress(percent) {
     87            document.getElementById('progress-bar').value = percent;
     88            document.getElementById('progress-text').innerText = percent + '%';
     89        }
     90       
    8391        function wiserrw_bulk_export() {
    84        
    8592            const data = new FormData();
    8693            data.append('action', 'wiserrw_bulk_orders_send');
     
    9097            data.append('offset', offset);
    9198            data.append('limit', limit);
     99            if(fetch_type == 'duration') {
     100                var duration = $("#wiserrw_duration").val();
     101                data.append('duration', duration);
     102            }
     103
     104            // Update progress every 500ms while waiting for response
     105            progressUpdateTimer = setInterval(() => {
     106                const currentProgress = parseInt(document.getElementById('progress-bar').value);
     107                if (currentProgress < 90) { // Don't go above 90% until we're done
     108                    updateProgress(currentProgress + 1);
     109                }
     110            }, 500);
    92111
    93112            fetch(ajaxurl, {
     
    97116            .then(res => res.json())
    98117            .then(res => {
     118                clearInterval(progressUpdateTimer); // Stop the progress animation
     119               
    99120                if (res.success) {
    100121                    const percent = res.data.percent;
    101                     offset += limit;
    102 
    103                     document.getElementById('progress-bar').value = percent;
    104                     document.getElementById('progress-text').innerText = percent + '%';
     122                    offset += limit; // Increment offset for next chunk
     123                    updateProgress(percent); // Update to actual progress
    105124
    106125                    if (!res.data.done) {
    107                         wiserrw_bulk_export(); // next chunk
     126                        setTimeout(() => wiserrw_bulk_export(), 100); // Small delay between chunks
    108127                    } else {
    109                         $("#export-progress").css( 'opacity', '0' );
     128                        $("#export-progress").css('opacity', '0');
     129                        updateProgress(100); // Ensure we show 100% at the end
     130                       
     131                        // Show success message with total orders processed
     132                        if (res.data.total_processed) {
     133                            setTimeout(() => {
     134                                alert('Success! Review requests sent to ' + res.data.total_processed + ' orders.');
     135                            }, 500);
     136                        }
     137                        // Show API errors if any
     138                        if (res.data.api_error_messages && res.data.api_error_messages.length > 0) {
     139                            setTimeout(() => {
     140                                alert('Some orders had errors:\n' + res.data.api_error_messages.join('\n'));
     141                            }, 1000);
     142                        }
    110143                    }
    111144                } else {
    112                     alert('Export failed.');
     145                    updateProgress(0); // Reset progress on error
     146                    alert('Export failed: ' + (res.data ? res.data.message : 'Unknown error'));
    113147                }
    114148            });
  • wiser-review/trunk/readme.txt

    r3351153 r3352566  
    66Tested up to: 6.8
    77Requires PHP: 7.4
    8 Stable tag: 2.0
     8Stable tag: 2.1
    99License: GPLv2 or later
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    121121= 1.9 =
    122122Improvement-Grouping sync & Google shopping review feed enabled at WiserReview account
     123
     124= 2.1 =
     125Bulk Send - Past customer data - Feature improved
  • wiser-review/trunk/wiser-review.php

    r3351153 r3352566  
    44 * Plugin URI: https://wiserreview.com
    55 * Description: Wiser Review module helps you collect and display product reviews, star ratings, and nudges. It also automates review requests via email to boost custom engagement and conversions.
    6  * Version: 2.0
     6 * Version: 2.1
    77 * Author: Wiser Notify
    88 * Requires Plugins: woocommerce
     
    111111function wiserrw_show_product_review() {
    112112    $wiserrw_api_settings = get_option( 'wiserrw_api_settings' );
    113     if ( '1' === $wiserrw_api_settings['wiserrw_product_review_enabled'] ) {
    114         echo do_shortcode( '[wiserrw_product_review]' );
    115     }
     113    if ( isset($wiserrw_api_settings['wiserrw_product_review_enabled']) && '1' === $wiserrw_api_settings['wiserrw_product_review_enabled'] ) {
     114    echo do_shortcode( '[wiserrw_product_review]' );
     115}
    116116}
    117117add_action( 'woocommerce_after_single_product_summary', 'wiserrw_show_product_review' );
     
    124124 */
    125125function wiserrw_show_product_rating_on_collection() {
    126     $product_id = get_the_ID();
    127     $wiserrw_api_settings = get_option( 'wiserrw_api_settings' );
    128     $wiserrw_api_data = get_option( 'wiserrw_api_data' );
    129     if ( '1' === $wiserrw_api_settings['wiserrw_product_card_enabled'] ) {
    130         echo do_shortcode( '[wiserrw_rating_count]' );
    131     }
     126        $product_id = get_the_ID();
     127        $wiserrw_api_settings = get_option( 'wiserrw_api_settings' );
     128        $wiserrw_api_data = get_option( 'wiserrw_api_data' );
     129        if ( isset($wiserrw_api_settings['wiserrw_product_card_enabled']) && '1' === $wiserrw_api_settings['wiserrw_product_card_enabled'] ) {
     130        echo do_shortcode( '[wiserrw_rating_count]' );
     131    }
    132132}
    133133add_action( 'woocommerce_after_shop_loop_item', 'wiserrw_show_product_rating_on_collection' );
     
    138138function wiserr_review_script() {
    139139    $wiserrw_api_settings = get_option( 'wiserrw_api_settings' );
    140     if ( $wiserrw_api_settings && '1' === $wiserrw_api_settings['wiserrw_live_mode_checkbox'] ) {
     140if ( isset($wiserrw_api_settings['wiserrw_live_mode_checkbox']) && '1' === $wiserrw_api_settings['wiserrw_live_mode_checkbox'] ) {
    141141        $wiserrw_api_data = get_option( 'wiserrw_api_data' );
    142142        $header_pixel = $wiserrw_api_data->header_pixel;
     
    161161    $wiserrw_api_settings = get_option( 'wiserrw_api_settings' );
    162162    $wiserrw_api_data = get_option( 'wiserrw_api_data' );
    163     if ( '1' === $wiserrw_api_settings['wiserrw_star_rating_enabled'] ) {
    164         echo do_shortcode( '[wiserrw_rating_count]' );
    165     }
     163    if ( isset($wiserrw_api_settings['wiserrw_star_rating_enabled']) && '1' === $wiserrw_api_settings['wiserrw_star_rating_enabled'] ) {
     164    echo do_shortcode( '[wiserrw_rating_count]' );
     165    }   
    166166}
    167167add_action( 'woocommerce_single_product_summary', 'wiserrw_show_rating_count', 5 );
     
    334334 */
    335335function wiserrw_woocommerce_order_status_completed( $order_id ) {
    336     $wiserrw_api_settings = get_option( 'wiserrw_api_settings' );
     336    $wiserrw_api_settings = get_option('wiserrw_api_settings', array());
     337    if (empty($wiserrw_api_settings) || empty($wiserrw_api_settings['wiserrw_api_key'])) {
     338        return; // Exit if no API settings
     339    }
     340
    337341    $api_key = $wiserrw_api_settings['wiserrw_api_key'];
    338     $api_data = wiserrw_validate_api( $api_key );
    339     if ( $api_data ) {
    340         $wiserrw_order_data = array();
    341         $wiserrw_line_items = array();
    342         $customer_data = array();
    343         $wsid = $api_data->data->wsid;
    344         $automation_id = $api_data->data->automation_id;
    345         $api_endpoint = 'https://api.wiserreview.com/api/woocommerce/webhook?wsid=' . $wsid . '&atmid=' . $automation_id;
    346         $order    = wc_get_order( $order_id );
    347 
    348         /* Order Details */
    349         $wiserrw_order_data['oid'] = $order->get_id();
    350         $wiserrw_order_data['cdt'] = $order->get_date_created();
     342    $api_data = wiserrw_validate_api($api_key);
     343    if (!$api_data || !isset($api_data->data->wsid) || !isset($api_data->data->automation_id)) {
     344        return; // Exit if invalid API data
     345    }
     346
     347    $order = wc_get_order($order_id);
     348    if (!$order) {
     349        return; // Exit if invalid order
     350    }
     351
     352    $wsid = $api_data->data->wsid;
     353    $automation_id = $api_data->data->automation_id;
     354    $api_endpoint = 'https://api.wiserreview.com/api/woocommerce/webhook?wsid=' . $wsid . '&atmid=' . $automation_id;
     355
     356    // Initialize arrays
     357    $wiserrw_order_data = array();
     358    $customer_data = array();
     359
     360    /* Order Details */
     361    $wiserrw_order_data['oid'] = $order->get_id();
     362    $date_created = $order->get_date_created();
     363    $wiserrw_order_data['cdt'] = $date_created ? $date_created->format('Y-m-d H:i:s') : current_time('mysql');
     364
     365    /* Customer Details */
     366    $customer_data['email'] = $order->get_billing_email();
     367    $customer_data['phone'] = $order->get_billing_phone();
     368    $customer_data['first_name'] = $order->get_billing_first_name();
     369    $customer_data['last_name'] = $order->get_billing_last_name();
     370    $wiserrw_order_data['customer'] = $customer_data;
     371
     372    /* Product Details */
     373    $wiserrw_order_data['line_items'] = array(); // Initialize line items array
     374    $items = $order->get_items();
     375   
     376    foreach ( $items as $item ) {
     377        $product = $item->get_product();
     378        if (!$product || !$product instanceof WC_Product) {
     379            continue; // Skip if product doesn't exist anymore
     380        }
    351381       
    352 
    353         /* Customer Details */
    354         $customer_data['email'] = $order->get_billing_email();
    355         $customer_data['phone'] = $order->get_billing_phone();
    356         $customer_data['first_name'] = $order->get_billing_first_name();
    357         $customer_data['last_name'] = $order->get_billing_last_name();
    358         $wiserrw_order_data['customer'] = $customer_data;
    359 
    360         /* Product Details */
    361         $items = $order->get_items();
    362         foreach ( $items as $item ) {
    363             $product       = $item->get_product();
    364             $product_id    = $item->get_product_id();
    365             $product_price = $product->get_price();
    366             $url           = get_permalink( $product_id );
    367             $product_name  = $item->get_name();
    368             $image = wp_get_attachment_image_src( get_post_thumbnail_id( $product_id ), 'single-post-thumbnail' );
    369             $image_url = $image[0];
    370             $product_data['id'] = $product_id;
    371             $product_data['pn'] = $product_name;
    372             $product_data['name'] = $product_name;
    373             $product_data['prc'] = $product_price;
    374             $product_data['pu'] = $url;
    375             $product_data['piu'] = $image_url;
    376             $wiserrw_order_data['line_items'][] = $product_data;
     382        $product_id = $item->get_product_id();
     383        $product_price = $product->get_price();
     384        $url = get_permalink($product_id);
     385        $product_name = $item->get_name();
     386       
     387        // Safely get product image
     388        $image = wp_get_attachment_image_src(get_post_thumbnail_id($product_id), 'single-post-thumbnail');
     389        $image_url = is_array($image) ? $image[0] : '';
     390       
     391        // Create new product data array for each item
     392        $product_data = array(
     393            'id' => $product_id,
     394            'pn' => $product_name,
     395            'name' => $product_name,
     396            'prc' => $product_price,
     397            'pu' => $url,
     398            'piu' => $image_url
     399        );
     400       
     401        $wiserrw_order_data['line_items'][] = $product_data;
     402    }
     403   
     404    // Send data to API
     405    $response = wp_remote_post(
     406        $api_endpoint,
     407        array(
     408            'method' => 'POST',
     409            'headers' => array(
     410                'Content-Type' => 'application/json',
     411            ),
     412            'timeout' => 45,
     413            'redirection' => 5,
     414            'httpversion' => '1.0',
     415            'blocking' => true,
     416            'body' => wp_json_encode($wiserrw_order_data), // Use wp_json_encode for better error handling
     417        )
     418    );
     419
     420    if (is_wp_error($response)) {
     421        $error_message = $response->get_error_message();
     422        error_log(sprintf(
     423            'WiserReview API Error (Order #%d): %s',
     424            $order_id,
     425            $error_message
     426        ));
     427    } else {
     428        $response_code = wp_remote_retrieve_response_code($response);
     429        if ($response_code !== 200) {
     430            $response_body = wp_remote_retrieve_body($response);
     431            error_log(sprintf(
     432                'WiserReview API Non-200 Response (Order #%d): Code %d - %s',
     433                $order_id,
     434                $response_code,
     435                $response_body
     436            ));
    377437        }
    378         $response = wp_remote_post(
    379             $api_endpoint,
    380             array(
    381                 'method' => 'POST',
    382                 'headers'   => array(
    383                     'Content-Type' => 'application/json',
    384                 ),
    385                 'timeout' => 45,
    386                 'redirection' => 5,
    387                 'httpversion' => '1.0',
    388                 'blocking' => true,
    389                 'body' => json_encode( $wiserrw_order_data ),
    390             )
    391         );
    392         if ( is_wp_error( $response ) ) {
    393             $error_message = $response->get_error_message();
    394             echo esc_html( $error_message );
    395         }
    396     }
     438    }
     439
    397440}
    398441add_action( 'woocommerce_order_status_completed', 'wiserrw_woocommerce_order_status_completed' );
     
    430473                break;
    431474        }
     475        // Get orders between date_from and now
     476        $date_to = current_time('timestamp');
    432477        $all_orders = wc_get_orders(
    433478            array(
    434479                'status'        => array( 'completed', 'processing' ),
    435                 'date_created'  => '>' . gmdate( 'Y-m-d H:i:s', $date_from ),
     480                'date_created'  => $date_from . '...' . $date_to,  // Using WooCommerce's date range format
    436481                'limit'         => -1,
    437482            )
    438483        );
    439484    } else {
    440         $start_date = new WC_DateTime($from_date);
    441         $end_date   = new WC_DateTime($to_date);
     485        // For custom date range, ensure we get full days
     486        $start_date = strtotime($from_date . ' 00:00:00');
     487        $end_date   = strtotime($to_date . ' 23:59:59');
     488       
     489        if (!$start_date || !$end_date) {
     490            wp_send_json_error(array('message' => 'Invalid date range provided'));
     491            die();
     492        }
     493       
    442494        $all_orders = wc_get_orders(
    443495            array(
    444496                'status'        => array( 'completed', 'processing' ),
    445                 'date_created'  => gmdate( $start_date . '...' . $end_date ),
     497                'date_created'  => $start_date . '...' . $end_date,  // Using WooCommerce's date range format
    446498                'limit'         => -1,
    447499            )
     
    553605                break;
    554606            case '7_days':
    555                 $date_from = strtotime( '-1 days' );
     607                $date_from = strtotime( '-7 days' ); // Fixed: Now correctly gets 7 days of orders
    556608                break;
    557609            default:
     
    559611                break;
    560612        }
     613       
     614        // Get orders between date_from and now
     615        $date_to = current_time('timestamp');
    561616        $all_orders = wc_get_orders(
    562617            array(
    563618                'status'        => array( 'completed', 'processing' ),
    564                 'date_created'  => '>' . gmdate( 'Y-m-d H:i:s', $date_from ),
     619                'date_created'  => $date_from . '...' . $date_to,  // Using WooCommerce's date range format
    565620                'limit'         => -1,
    566621            )
     
    579634
    580635    $total = count( $all_orders );
     636    if ( $total === 0 ) {
     637        wp_send_json_success([
     638            'percent'  => 100,
     639            'done'     => true,
     640            'message'  => 'No orders found for selected range.',
     641        ]);
     642        wp_die();
     643    }
     644
    581645    if ( $total > 0 ) {
    582646        $chunk_ids = array_slice( $all_orders, $offset, $limit );
     
    608672
    609673            /* Product Details */
     674            $wiserrw_order_data['line_items'] = array(); // Clear line items for new order
    610675            $items = $order->get_items();
    611676            foreach ( $items as $item ) {
    612                 $product       = $item->get_product();
    613                 $product_id    = $item->get_product_id();
     677                $product = $item->get_product();
     678                if (!$product || !$product instanceof WC_Product) {
     679                    continue; // Skip if product doesn't exist anymore
     680                }
     681                $product_id = $item->get_product_id();
    614682                $product_price = $product->get_price();
    615                 $url           = get_permalink( $product_id );
    616                 $product_name  = $item->get_name();
    617                 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $product_id ), 'single-post-thumbnail' );
    618                 $image_url = $image[0];
    619                 $product_data['id'] = $product_id;
    620                 $product_data['pn'] = $product_name;
    621                 $product_data['name'] = $product_name;
    622                 $product_data['prc'] = $product_price;
    623                 $product_data['pu'] = $url;
    624                 $product_data['piu'] = $image_url;
     683                $url = get_permalink($product_id);
     684                $product_name = $item->get_name();
     685                $image = wp_get_attachment_image_src(get_post_thumbnail_id($product_id), 'single-post-thumbnail');
     686                $image_url = is_array($image) ? $image[0] : '';
     687               
     688                $product_data = array( // Initialize new array for each product
     689                    'id' => $product_id,
     690                    'pn' => $product_name,
     691                    'name' => $product_name,
     692                    'prc' => $product_price,
     693                    'pu' => $url,
     694                    'piu' => $image_url
     695                );
    625696                $wiserrw_order_data['line_items'][] = $product_data;
    626697            }
     
    640711                )
    641712            );
    642             $response_arr[] = $response;
    643713            if ( is_wp_error( $response ) ) {
    644714                $error_message = $response->get_error_message();
     715                error_log(sprintf(
     716                    'WiserReview Bulk Send API Error (Order #%d): %s',
     717                    $order_id,
     718                    $error_message
     719                ));
    645720                $api_error_messages[] = $error_message;
     721            } else {
     722                $response_code = wp_remote_retrieve_response_code($response);
     723                if ($response_code !== 200) {
     724                    $error_message = wp_remote_retrieve_body($response);
     725                    error_log(sprintf(
     726                        'WiserReview Bulk Send API Non-200 Response (Order #%d): Code %d - %s',
     727                        $order_id,
     728                        $response_code,
     729                        $error_message
     730                    ));
     731                    $api_error_messages[] = $error_message;
     732                }
    646733            }
    647734        }
    648735
    649736        $processed = $offset + count( $chunk_ids );
    650         $percent = round( ( $processed / $total ) * 100 );
    651 
    652         wp_send_json_success(
    653             array(
    654                 'percent'  => $percent,
    655                 'done'     => $processed >= $total,
    656                 'wiserrw_order_data' => $wiserrw_order_data,
    657                 'api_error_messages' => $api_error_messages,
    658                 'api_response' => $response_arr,
    659                 'api_url' => $api_endpoint,
    660             )
    661         );
     737        $percent = min(round( ( $processed / $total ) * 100 ), 100); // Ensure we don't exceed 100%
     738        $is_done = $processed >= $total;
     739
     740        $response_data = array(
     741            'percent'  => $percent,
     742            'done'     => $is_done,
     743            'total_processed' => $is_done ? $total : $processed,
     744            'total_orders' => $total,
     745            'processed_chunk' => count($chunk_ids),
     746            'api_error_messages' => $api_error_messages
     747        );
     748
     749        wp_send_json_success($response_data);
    662750    }
    663751    die();
     
    893981        }
    894982
    895         $url = '';
    896         $response = '';
    897        
    898983        if (!empty($products_json)) {
    899984            $data = array(
     
    907992            $url = $api_host . 'productWebhook?wsid=' . $wsid . '&key=' . $api_key;   
    908993            $response = wp_safe_remote_post( $url, $data );
     994           
     995            if (is_wp_error($response)) {
     996                $error_message = $response->get_error_message();
     997                error_log(sprintf(
     998                    'WiserReview Product Sync API Error: %s',
     999                    $error_message
     1000                ));
     1001            } else {
     1002                $response_code = wp_remote_retrieve_response_code($response);
     1003                if ($response_code !== 200) {
     1004                    $response_body = wp_remote_retrieve_body($response);
     1005                    error_log(sprintf(
     1006                        'WiserReview Product Sync API Non-200 Response: Code %d - %s',
     1007                        $response_code,
     1008                        $response_body
     1009                    ));
     1010                }
     1011            }
    9091012        }
    9101013       
Note: See TracChangeset for help on using the changeset viewer.