Changeset 3351153
- Timestamp:
- 08/27/2025 11:31:20 AM (7 months ago)
- Location:
- wiser-review/trunk
- Files:
-
- 2 edited
-
readme.txt (modified) (2 diffs)
-
wiser-review.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wiser-review/trunk/readme.txt
r3349699 r3351153 6 6 Tested up to: 6.8 7 7 Requires PHP: 7.4 8 Stable tag: 1.98 Stable tag: 2.0 9 9 License: GPLv2 or later 10 10 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 118 118 = 1.8 = 119 119 Grouping sync & Google shopping review feed enabled at WiserReview account 120 121 = 1.9 = 122 Improvement-Grouping sync & Google shopping review feed enabled at WiserReview account -
wiser-review/trunk/wiser-review.php
r3349699 r3351153 4 4 * Plugin URI: https://wiserreview.com 5 5 * 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: 1.96 * Version: 2.0 7 7 * Author: Wiser Notify 8 8 * Requires Plugins: woocommerce … … 349 349 $wiserrw_order_data['oid'] = $order->get_id(); 350 350 $wiserrw_order_data['cdt'] = $order->get_date_created(); 351 $wiserrw_order_data['cdt'] = $order->get_date_created();351 352 352 353 353 /* Customer Details */ … … 754 754 $full_product_synced = get_option( 'wiserrw_all_products_synced' ); 755 755 $api_host = constant( 'WISERRW_API_HOST' ); 756 $per_page = $_REQUEST['per_page'] ?:50;756 $per_page = isset($_REQUEST['per_page']) ? intval($_REQUEST['per_page']) : 50; 757 757 $api_key = $wiserrw_api_settings['wiserrw_api_key']; 758 $api_data = wiserrw_validate_api( $api_key ); 759 $wsid = $api_data->data->wsid; 760 $arrsku = array(); 761 if ( isset( $_REQUEST['total_pages'] ) ) { 762 $total_pages = (int) sanitize_text_field( $_REQUEST['total_pages'] ); // Input var okay. 763 } else { 764 $count = wiserrw_count_products_with_reviews(); 765 $total_pages = $count / $per_page; 766 $total_pages = (int) $total_pages; 767 if ( $count % $per_page ) { 768 $total_pages ++; 769 } 770 } 771 772 $page = (int) sanitize_text_field( $_REQUEST['page'] ); 773 $finished = 0; 774 775 if ( $page <= $total_pages ) { 776 $message = "Synced $page / $total_pages pages"; 777 778 $products = wiserrw_get_products( $page, $per_page ); 779 $products_json = array(); 780 781 foreach ( $products as $prod ) { 782 $products_arr = array(); 783 $id = $prod['ID']; 784 $registered = get_post_meta( $id, '_wiserrw_product_registered' ); 758 $api_data = wiserrw_validate_api( $api_key ); 759 $wsid = $api_data->data->wsid; 760 761 if ( isset( $_REQUEST['total_pages'] ) ) { 762 $total_pages = (int) sanitize_text_field( wp_unslash($_REQUEST['total_pages']) ); 763 } else { 764 $count = wiserrw_count_products_with_reviews(); 765 $total_pages = $count / $per_page; 766 $total_pages = (int) $total_pages; 767 if ( $count % $per_page ) { 768 $total_pages++; 769 } 770 } 771 772 $page = isset($_REQUEST['page']) ? (int) sanitize_text_field( wp_unslash($_REQUEST['page']) ) : 1; 773 $finished = 0; 774 775 if ( $page <= $total_pages ) { 776 $message = "Synced $page / $total_pages pages"; 777 778 $products = wiserrw_get_products( $page, $per_page ); 779 $products_json = array(); 780 781 foreach ( $products as $prod ) { 782 // Reset SKU array for each product 783 $arrsku = array(); 784 785 $p_id = $prod['ID']; 786 $registered = get_post_meta( $p_id, '_wiserrw_product_registered' ); 785 787 if ( ! empty( $registered ) ) { 786 788 continue; 787 789 } 788 $image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'shop_single' ); 789 $small_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'thumbnail' ); 790 $medium_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'shop_catalog' ); 791 792 $terms = get_the_terms( $id, 'product_tag' ); 793 $term_array = array(); 794 if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { 795 foreach ( $terms as $term ) { 796 $term_array[] = $term->name; 797 } 798 } 799 800 801 $pr = wc_get_product( $id ); 802 $variants = array(); 803 804 if ( $pr->is_type( 'variable' ) ) { 805 $prod_variation = new WC_Product_Variable( $id ); 806 foreach ( $prod_variation->get_available_variations() as $variation ) { 807 $variant = array(); 808 809 $gtin = get_post_meta( $variation['variation_id'], 'hwp_var_gtin', 1 ) ?: get_post_meta( $id, 'hwp_product_gtin', 1 ) ?: $pr->get_attribute('GTIN') ?: $pr->get_attribute('EAN') ?: $pr->get_attribute('ISBN') ?: get_post_meta( $id, '_global_unique_id', 1 ); 810 811 if ( ! empty( $gtin ) ) { 812 $variant['barcode'] = $gtin; 813 } 814 815 if ( ! empty( $variation['sku'] ) ) { 816 $variant['sku'] = $variation['sku']; 817 $arrsku[] = $variation['sku']; 818 } 819 820 if ( ! empty( $variant) ) { 821 $variants[] = $variant; 822 } 823 } 824 } else { 825 $pr_sku = $pr->get_sku(); 826 $variant = array(); 827 $gtin = get_post_meta( $id, 'hwp_product_gtin', 1 ) ?: $pr->get_attribute('GTIN') ?: $pr->get_attribute('EAN') ?: $pr->get_attribute('ISBN') ?: get_post_meta( $id, '_global_unique_id', 1 ); 828 829 if( $pr_sku != '' ){ 790 791 $image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $p_id ), 'shop_single' ); 792 $small_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $p_id ), 'thumbnail' ); 793 $medium_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $p_id ), 'shop_catalog' ); 794 795 $terms = get_the_terms( $p_id, 'product_tag' ); 796 $term_array = array(); 797 if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { 798 foreach ( $terms as $term ) { 799 $term_array[] = $term->name; 800 } 801 } 802 803 $pr = wc_get_product( $p_id ); 804 if (!$pr) { 805 continue; // Skip if product doesn't exist 806 } 807 808 $variants = array(); 809 $gtin = ''; 810 811 if ( $pr->is_type( 'variable' ) ) { 812 $prod_variation = new WC_Product_Variable( $p_id ); 813 foreach ( $prod_variation->get_available_variations() as $variation ) { 814 $variant = array(); 815 816 $gtin = get_post_meta( $variation['variation_id'], 'hwp_var_gtin', 1 ) ?: 817 get_post_meta( $p_id, 'hwp_product_gtin', 1 ) ?: 818 $pr->get_attribute('GTIN') ?: 819 $pr->get_attribute('EAN') ?: 820 $pr->get_attribute('ISBN') ?: 821 get_post_meta( $p_id, '_global_unique_id', 1 ); 822 823 if ( ! empty( $gtin ) ) { 824 $variant['barcode'] = $gtin; 825 } 826 827 if ( ! empty( $variation['sku'] ) ) { 828 $variant['sku'] = $variation['sku']; 829 $arrsku[] = $variation['sku']; 830 } 831 832 if ( ! empty( $variant) ) { 833 $variants[] = $variant; 834 } 835 } 836 } else { 837 $pr_sku = $pr->get_sku(); 838 $variant = array(); 839 $gtin = get_post_meta( $p_id, 'hwp_product_gtin', 1 ) ?: 840 $pr->get_attribute('GTIN') ?: 841 $pr->get_attribute('EAN') ?: 842 $pr->get_attribute('ISBN') ?: 843 get_post_meta( $p_id, '_global_unique_id', 1 ); 844 845 if ( $pr_sku != '' ) { 830 846 $arrsku[] = $pr_sku; 831 847 } 832 if ( ! empty( $gtin ) ) { 833 $variant['barcode'] = $gtin; 834 } 835 836 if ( ! empty( $pr_sku ) ) { 837 $variant['sku'] = $pr_sku; 838 } 839 840 if ( ! empty($variant) ) { 841 $variants[] = $variant; 842 } 843 } 844 845 $vendor = get_bloginfo( 'name' ); 846 if ( empty( $vendor ) ) { 847 $vendor = $domain; 848 } 849 850 $cats = get_the_terms( $id, 'product_cat' ); 851 $cat = $vendor; 852 if ( ! empty( $cats ) && ! is_wp_error( $cats ) ) { 853 $cat = $cats[0]->name; 854 } 855 856 $title = $prod['title']; 857 if ( empty($title) ) { 858 $title = $cat; 859 } 860 861 $product_json = array( 862 'pid' => $prod['ID'], 863 'arrsku' => $arrsku, 864 'shp' => get_site_url(), 865 'wsid' => $wsid, 866 'pn' => $title, 867 'piu' => $image_url[0], 868 'pu' => get_permalink($prod['ID']), 869 'barcode' => $gtin, 870 ); 871 872 $products_json[] = $product_json; 873 update_post_meta( $id, '_wiserrw_product_registered', '1' ); 874 } 875 876 $data = array( 877 'method' => 'POST', 878 'blocking' => true, 879 'headers' => array( 'Content-Type' => 'application/json' ), 880 'body' => json_encode( array( 881 'products' => $products_json 882 ) ) 883 ); 884 $url = $api_host . 'productWebhook?wsid='.$wsid.'&key='.$api_key; 885 $response = wp_safe_remote_post( $url, $data ); 886 887 } else { 888 $message = 'Finished'; 889 $finished = 1; 890 } 891 892 if ( $page == $total_pages ) { 893 update_option( 'wiserrw_all_products_synced', 1 ); 894 } 895 896 $page ++; 897 $result = array( 898 'next_page' => $page, 899 'total_pages' => $total_pages, 900 'message' => $message, 901 'url' => $url, 902 'response' => $response, 903 'products' => $products_json, 904 'finished' => $finished, 905 ); 906 907 echo wp_json_encode( $result ); 908 wp_die(); 848 849 if ( ! empty( $gtin ) ) { 850 $variant['barcode'] = $gtin; 851 } 852 853 if ( ! empty( $pr_sku ) ) { 854 $variant['sku'] = $pr_sku; 855 } 856 857 if ( ! empty($variant) ) { 858 $variants[] = $variant; 859 } 860 } 861 862 $vendor = get_bloginfo( 'name' ); 863 $domain = isset($domain) ? $domain : parse_url(home_url(), PHP_URL_HOST); 864 if ( empty( $vendor ) ) { 865 $vendor = $domain; 866 } 867 868 $cats = get_the_terms( $p_id, 'product_cat' ); 869 $cat = $vendor; 870 if ( ! empty( $cats ) && ! is_wp_error( $cats ) ) { 871 $cat = $cats[0]->name; 872 } 873 874 $title = $prod['title']; 875 if ( empty($title) ) { 876 $title = $cat; 877 } 878 879 $product_json = array( 880 'pid' => $prod['ID'], 881 'arrsku' => $arrsku, 882 'shp' => get_site_url(), 883 'wsid' => $wsid, 884 'pn' => $title, 885 'piu' => isset($image_url[0]) ? $image_url[0] : '', 886 'pu' => get_permalink($prod['ID']), 887 'barcode'=> $gtin, 888 'variants' => $variants, // Include variants data 889 ); 890 891 $products_json[] = $product_json; 892 update_post_meta( $p_id, '_wiserrw_product_registered', '1' ); 893 } 894 895 $url = ''; 896 $response = ''; 897 898 if (!empty($products_json)) { 899 $data = array( 900 'method' => 'POST', 901 'blocking' => true, 902 'headers' => array( 'Content-Type' => 'application/json' ), 903 'body' => json_encode( array( 904 'products' => $products_json 905 ) ) 906 ); 907 $url = $api_host . 'productWebhook?wsid=' . $wsid . '&key=' . $api_key; 908 $response = wp_safe_remote_post( $url, $data ); 909 } 910 911 } else { 912 $message = 'Finished'; 913 $finished = 1; 914 } 915 916 if ( $page == $total_pages ) { 917 update_option( 'wiserrw_all_products_synced', 1 ); 918 } 919 920 $page++; 921 $result = array( 922 'next_page' => $page, 923 'total_pages' => $total_pages, 924 'message' => $message, 925 'url' => isset($url) ? $url : '', 926 'response' => isset($response) ? $response : '', 927 'products' => isset($products_json) ? $products_json : array(), 928 'finished' => $finished, 929 ); 930 931 echo wp_json_encode( $result ); 932 wp_die(); 909 933 } 910 934 add_action( 'wp_ajax_wiserrw_sync_products', 'wiserrw_sync_products' ); 911 add_action( 'wp_ajax_ wiserrw_sync_products', 'wiserrw_sync_products' );935 add_action( 'wp_ajax_nopriv_wiserrw_sync_products', 'wiserrw_sync_products' ); 912 936 913 937 function wiserrw_reset_products() { 914 938 global $wpdb; 915 $sql = "DELETE FROM wp_postmeta WHERE meta_key = '_wiserrw_product_registered'"; 916 $message = 'All Products Sync Status Cleard'; 917 $deleted = $wpdb->query($sql); 918 $deleted_count = intval($wpdb->rows_affected); 939 $sql = $wpdb->prepare( "DELETE FROM {$wpdb->prefix}postmeta WHERE meta_key = %s", '_wiserrw_product_registered' ); 940 $wpdb->query( $sql ); 941 $message = 'All Products Sync Status Cleared'; 919 942 $result = array( 920 943 'message' => $message, 921 944 ); 922 923 945 echo wp_json_encode( $result ); 924 946 wp_die(); 925 947 } 926 add_action( 'wp_ajax_wiserrw_reset_products', 'wiserrw_reset_products' ); 927 add_action( 'wp_ajax_wiserrw_reset_products', 'wiserrw_reset_products' ); 948 // Register the AJAX action only once 949 add_action('wp_ajax_wiserrw_reset_products', 'wiserrw_reset_products'); 950 add_action( 'wp_ajax_nopriv_wiserrw_reset_products', 'wiserrw_reset_products' ); 928 951 929 952 function wiserrw_product_delete_hook( $p_id ) { … … 947 970 function wiserrw_product_update_hook( $product ) { 948 971 $p_id = $product->get_id(); 949 delete_post_meta( $ id, '_wiserrw_product_registered' );972 delete_post_meta( $p_id, '_wiserrw_product_registered' ); 950 973 if ( wp_is_post_autosave( $p_id ) || wp_is_post_revision( $p_id ) ) { 951 974 return; … … 967 990 968 991 if ( $pr->is_type( 'variable' ) ) { 969 $prod_variation = new WC_Product_Variable( $ id );992 $prod_variation = new WC_Product_Variable( $p_id ); 970 993 foreach ( $prod_variation->get_available_variations() as $variation ) { 971 994 $variant = array(); 972 973 $gtin = get_post_meta( $variation['variation_id'], 'hwp_var_gtin', 1 ) ?: get_post_meta( $ id, 'hwp_product_gtin', 1 ) ?: $pr->get_attribute('GTIN') ?: $pr->get_attribute('EAN') ?: $pr->get_attribute('ISBN') ?: get_post_meta( $p_id, '_global_unique_id', 1 );974 995 996 $gtin = get_post_meta( $variation['variation_id'], 'hwp_var_gtin', 1 ) ?: get_post_meta( $p_id, 'hwp_product_gtin', 1 ) ?: $pr->get_attribute('GTIN') ?: $pr->get_attribute('EAN') ?: $pr->get_attribute('ISBN') ?: get_post_meta( $p_id, '_global_unique_id', 1 ); 997 975 998 if ( ! empty( $gtin ) ) { 976 999 $variant['barcode'] = $gtin;
Note: See TracChangeset
for help on using the changeset viewer.