Plugin Directory

Changeset 3425122


Ignore:
Timestamp:
12/22/2025 08:18:07 AM (3 months ago)
Author:
akashmalik
Message:

PHP version 8.4 release compatibility

Location:
social-boost/trunk
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • social-boost/trunk/includes/socialboost-api.php

    r3251063 r3425122  
    123123        $msg = '';
    124124        try {
    125             if (strpos($request->get_header('user_agent'), 'Appsmav') === false) {
     125            if (amsb_strpos($request->get_header('user_agent'), 'Appsmav') === false) {
    126126                throw new Exception('Error: ');
    127127            }
    128128
    129             if (empty($_POST['payload'])) {
     129            if (!isset($_POST['payload']) || empty($_POST['payload'])) {
    130130                throw new Exception('Error: ');
    131131            }
     
    134134            $post_payload = sanitize_text_field($_POST['payload']);
    135135
    136             // Use hash_equals for timing attack protection
    137136            if (!hash_equals($payload, $post_payload)) {
    138137                throw new Exception('Warning: ');
     
    155154        $msg = '';
    156155        try {
    157             if (strpos($request->get_header('user_agent'), 'Appsmav') === false) {
     156            if (amsb_strpos($request->get_header('user_agent'), 'Appsmav') === false) {
    158157                throw new Exception('Error: ');
    159158            }
     
    199198            }
    200199
    201             $res = json_decode($res, true);
    202             if (empty($res) || !empty($res['error'])) {
     200            $res = amsb_json_decode($res, true, array('error' => 1));
     201            if (empty($res) || (isset($res['error']) && !empty($res['error']))) {
    203202                throw new Exception('Error: ');
    204203            }
     
    218217    public function getversion($request)
    219218    {
     219        $data = array();
    220220        try {
    221221            $version = '';
     
    237237        try
    238238        {
     239            if (!isset($_POST['order_id'])) {
     240                throw new Exception("Order ID is required");
     241            }
    239242            $order_id = sanitize_text_field($_POST['order_id']);
    240243            $order = new WC_Order($order_id);
     
    245248            $data = array(
    246249                'error' => 0,
    247                 'order' => $order->get_data()
     250                'order' => method_exists($order, 'get_data') ? $order->get_data() : array()
    248251            );
    249252        }
     
    265268        try
    266269        {
    267             if (empty($_POST['id'])) {
     270            if (!isset($_POST['id']) || empty($_POST['id'])) {
    268271                throw new Exception('Invalid Page');
    269272            }
     
    310313        try
    311314        {
    312             if (empty($_POST['title'])) {
     315            if (!isset($_POST['title']) || empty($_POST['title'])) {
    313316                throw new Exception('Invalid Title');
    314317            }
    315318
    316             if (empty($_POST['content'])) {
     319            if (!isset($_POST['content']) || empty($_POST['content'])) {
    317320                throw new Exception('Invalid Content');
    318321            }
     
    358361            }
    359362
    360             if (empty($_POST['id'])) {
     363            if (!isset($_POST['id']) || empty($_POST['id'])) {
    361364                throw new Exception('Invalid Page');
    362365            }
     
    377380            else
    378381            {
    379                 $params['post_title'] = sanitize_text_field($_POST['title']);
     382                $params['post_title'] = (isset($_POST['title']) && !empty($_POST['title'])) ? sanitize_text_field($_POST['title']) : '';
    380383            }
    381384
     
    408411        try
    409412        {
    410             if (empty($_POST['id'])) {
     413            if (!isset($_POST['id']) || empty($_POST['id'])) {
    411414                throw new Exception('Invalid Page');
    412415            }
     
    436439    public function reset_installation($request)
    437440    {
     441        $data = array();
    438442        try
    439443        {
     
    459463        try
    460464        {
    461             if(empty($_POST['data']))
     465            if(!isset($_POST['data']) || empty($_POST['data']))
    462466                throw new Exception('No config to set');
    463467
    464             if(empty($_POST['data']) || !is_array($_POST['data']))
     468            if(!is_array($_POST['data']))
    465469                throw new Exception('Invalid config to set');
    466470
     
    495499        try
    496500        {
    497             if (empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
     501            if (!isset($_POST['email']) || empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    498502                throw new Exception("Invalid email address");
    499503            }
    500504
    501             if (empty($_POST['user_name'])) {
     505            if (!isset($_POST['user_name']) || empty($_POST['user_name'])) {
    502506                throw new Exception("Invalid user name");
    503507            }
    504508
    505             if (empty($_POST['first_name']) || strlen($_POST['first_name']) > 50) {
     509            if (!isset($_POST['first_name']) || empty($_POST['first_name']) || amsb_strlen($_POST['first_name']) > 50) {
    506510                throw new Exception("Invalid first name");
    507511            }
    508512
    509             if (!empty($_POST['last_name']) && strlen($_POST['last_name']) > 50) {
     513            if (isset($_POST['last_name']) && !empty($_POST['last_name']) && amsb_strlen($_POST['last_name']) > 50) {
    510514                throw new Exception("Invalid last name");
    511515            }
     
    518522            $user_name = sanitize_user($_POST['user_name']);
    519523            $first_name = sanitize_text_field($_POST['first_name']);
    520             $last_name = empty($_POST['last_name']) ? '' : sanitize_text_field($_POST['last_name']);
     524            $last_name = (isset($_POST['last_name']) && !empty($_POST['last_name'])) ? sanitize_text_field($_POST['last_name']) : '';
    521525
    522526            $user = get_user_by('email', $email);
     
    577581        $resp['msg']   = 'No User Exist';
    578582        try {
    579             if (empty($_POST['verify_user']) || !filter_var($_POST['verify_user'], FILTER_VALIDATE_EMAIL)) {
     583            if (!isset($_POST['verify_user']) || empty($_POST['verify_user']) || !filter_var($_POST['verify_user'], FILTER_VALIDATE_EMAIL)) {
    580584                throw new Exception("Invalid email address");
    581585            }
     
    613617        {
    614618            $cat_args = array(
     619                'taxonomy'   => 'product_cat',
    615620                'orderby'    => 'name',
    616621                'order'      => 'asc',
    617622                'hide_empty' => false,
    618623            );
    619             $categories = get_terms('product_cat', $cat_args);
     624            // Use WordPress 4.5+ compatible syntax
     625            $categories = get_terms($cat_args);
     626           
     627            // Validate result is not WP_Error
     628            if (is_wp_error($categories)) {
     629                $categories = array();
     630            }
    620631
    621632            $data = array(
    622633                'error' => 0,
    623                 'product_categories' => !empty($categories) ? $categories : array()
     634                'product_categories' => !empty($categories) && is_array($categories) ? $categories : array()
    624635            );
    625636        }
     
    640651    public function sb_create_coupon($request)
    641652    {
     653        $data = array();
    642654        try
    643655        {
    644656            global $wpdb;
    645657
    646             if(empty($_POST['cpn_type']) || empty($_POST['sbcpn_code']))
     658            if(!isset($_POST['cpn_type']) || empty($_POST['cpn_type']) || !isset($_POST['sbcpn_code']) || empty($_POST['sbcpn_code']))
    647659                throw new Exception('InvalidRequest1');
    648660
     
    653665                throw new Exception('WooPluginNotFound');
    654666
    655             if(!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins'))))
     667            if(!amsb_in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins'))))
    656668                throw new Exception('PluginDeactivated');
    657669
    658670            // Validate coupon types
    659             if(!in_array(wc_clean($_POST['cpn_type']), array_keys(wc_get_coupon_types())))
    660                 throw new WC_CLI_Exception('woocommerce_cli_invalid_coupon_type', sprintf(__('Invalid coupon type - the coupon type must be any of these: %s', 'woocommerce'), implode(', ', array_keys(wc_get_coupon_types()))));
     671            if(!amsb_in_array(wc_clean($_POST['cpn_type']), array_keys(wc_get_coupon_types())))
     672                throw new Exception(sprintf(__('Invalid coupon type - the coupon type must be any of these: %s', 'woocommerce'), amsb_implode(', ', array_keys(wc_get_coupon_types()))));
    661673
    662674            $assoc_args = array(
     
    680692            );
    681693
    682             $assoc_args['product_ids'] = !empty($assoc_args['product_ids']) ? json_decode($assoc_args['product_ids'], true) : [];
    683             $assoc_args['exclude_product_ids'] = !empty($assoc_args['exclude_product_ids']) ? json_decode($assoc_args['exclude_product_ids'], true) : [];
    684             $assoc_args['product_category_ids'] = !empty($assoc_args['product_category_ids']) ? json_decode($assoc_args['product_category_ids'], true) : [];
    685             $assoc_args['exclude_product_category_ids'] = !empty($assoc_args['exclude_product_category_ids']) ? json_decode($assoc_args['exclude_product_category_ids'], true) : [];
    686             $assoc_args['customer_emails'] = !empty($assoc_args['customer_emails']) ? json_decode(stripslashes($assoc_args['customer_emails']), true) : [];
    687 
    688             if(!empty($_POST['usage_limit_per_user']))
     694            // Use safe JSON decode for all array fields
     695            $assoc_args['product_ids'] = !empty($assoc_args['product_ids']) ? amsb_json_decode($assoc_args['product_ids'], true, array()) : array();
     696            $assoc_args['exclude_product_ids'] = !empty($assoc_args['exclude_product_ids']) ? amsb_json_decode($assoc_args['exclude_product_ids'], true, array()) : array();
     697            $assoc_args['product_category_ids'] = !empty($assoc_args['product_category_ids']) ? amsb_json_decode($assoc_args['product_category_ids'], true, array()) : array();
     698            $assoc_args['exclude_product_category_ids'] = !empty($assoc_args['exclude_product_category_ids']) ? amsb_json_decode($assoc_args['exclude_product_category_ids'], true, array()) : array();
     699            $assoc_args['customer_emails'] = !empty($assoc_args['customer_emails']) ? amsb_json_decode(amsb_stripslashes($assoc_args['customer_emails']), true, array()) : array();
     700
     701            if(isset($_POST['usage_limit_per_user']) && !empty($_POST['usage_limit_per_user']))
    689702                $assoc_args['usage_limit'] = '';
    690703
     
    713726            if(empty($app_id) || empty($payload))
    714727                throw new Exception('IntegrationMissing');
     728
     729            // Validate additional required fields
     730            if(!isset($_POST['id_coupon']) || !isset($_POST['hash']))
     731                throw new Exception('InvalidRequest3');
    715732
    716733            $param = array(
     
    731748            $res = $httpObj->getResponse();
    732749            if(!empty($res))
    733                 $res = json_decode($res, true);
    734 
    735             if(empty($res) || !empty($res['error']))
     750                $res = amsb_json_decode($res, true, array('error' => 1));
     751
     752            if(empty($res) || (isset($res['error']) && !empty($res['error'])))
    736753                throw new Exception('VerificationFailed');
    737754
     
    771788
    772789            if(is_wp_error($id))
    773                 throw new WC_CLI_Exception('woocommerce_cli_cannot_create_coupon', $id->get_error_message());
     790                throw new Exception('Cannot create coupon: ' . $id->get_error_message());
    774791
    775792            // Set coupon meta
     
    777794            update_post_meta($id, 'coupon_amount', wc_format_decimal($coupon_data['amount']));
    778795            update_post_meta($id, 'individual_use', (!empty($coupon_data['individual_use']) ) ? 'yes' : 'no' );
    779             update_post_meta($id, 'product_ids', implode(',', array_filter(array_map('intval', $coupon_data['product_ids']))));
    780             update_post_meta($id, 'exclude_product_ids', implode(',', array_filter(array_map('intval', $coupon_data['exclude_product_ids']))));
     796           
     797            // Validate arrays before array operations
     798            $product_ids = is_array($coupon_data['product_ids']) ? $coupon_data['product_ids'] : array();
     799            $exclude_product_ids = is_array($coupon_data['exclude_product_ids']) ? $coupon_data['exclude_product_ids'] : array();
     800           
     801            update_post_meta($id, 'product_ids', amsb_implode(',', array_filter(array_map('intval', $product_ids))));
     802            update_post_meta($id, 'exclude_product_ids', amsb_implode(',', array_filter(array_map('intval', $exclude_product_ids))));
    781803            update_post_meta($id, 'usage_limit', absint($coupon_data['usage_limit']));
    782804            update_post_meta($id, 'usage_limit_per_user', absint($coupon_data['usage_limit_per_user']));
     
    784806            update_post_meta($id, 'usage_count', absint($coupon_data['usage_count']));
    785807
    786             if('' !== wc_clean($coupon_data['expiry_date']))
    787                 $coupon_data['expiry_date'] = date('Y-m-d', strtotime($coupon_data['expiry_date']));
     808            if('' !== wc_clean($coupon_data['expiry_date'])) {
     809                $timestamp = amsb_strtotime($coupon_data['expiry_date']);
     810                // Validate strtotime didn't return FALSE
     811                $coupon_data['expiry_date'] = ($timestamp !== false) ? date('Y-m-d', $timestamp) : '';
     812            }
    788813
    789814            update_post_meta($id, 'expiry_date', wc_clean($coupon_data['expiry_date']));
    790815            update_post_meta($id, 'free_shipping', (!empty($coupon_data['enable_free_shipping']) ) ? 'yes' : 'no' );
    791             update_post_meta($id, 'product_categories', array_filter(array_map('intval', $coupon_data['product_category_ids'])));
    792             update_post_meta($id, 'exclude_product_categories', array_filter(array_map('intval', $coupon_data['exclude_product_category_ids'])));
     816           
     817            // Validate arrays before array operations
     818            $product_category_ids = is_array($coupon_data['product_category_ids']) ? $coupon_data['product_category_ids'] : array();
     819            $exclude_product_category_ids = is_array($coupon_data['exclude_product_category_ids']) ? $coupon_data['exclude_product_category_ids'] : array();
     820            $customer_emails = is_array($coupon_data['customer_emails']) ? $coupon_data['customer_emails'] : array();
     821           
     822            update_post_meta($id, 'product_categories', array_filter(array_map('intval', $product_category_ids)));
     823            update_post_meta($id, 'exclude_product_categories', array_filter(array_map('intval', $exclude_product_category_ids)));
    793824            update_post_meta($id, 'exclude_sale_items', (!empty($coupon_data['exclude_sale_items']) ) ? 'yes' : 'no' );
    794825            update_post_meta($id, 'minimum_amount', wc_format_decimal($coupon_data['minimum_amount']));
    795826            update_post_meta($id, 'maximum_amount', wc_format_decimal($coupon_data['maximum_amount']));
    796             update_post_meta($id, 'customer_email', array_filter(array_map('sanitize_email', $coupon_data['customer_emails'])));
    797 
    798             if (!empty($_POST['custom_attributes']))
     827            update_post_meta($id, 'customer_email', array_filter(array_map('sanitize_email', $customer_emails)));
     828
     829            if (isset($_POST['custom_attributes']) && !empty($_POST['custom_attributes']))
    799830            {
    800                 $custom_attributes = stripslashes(sanitize_text_field($_POST['custom_attributes']));
    801                 $custom_attributes = json_decode($custom_attributes, true);
     831                $custom_attributes = amsb_stripslashes(sanitize_text_field($_POST['custom_attributes']));
     832                $custom_attributes = amsb_json_decode($custom_attributes, true, array());
    802833                if (!empty($custom_attributes) && is_array($custom_attributes))
    803834                {
     
    827858    public function verify_coupon_code($request)
    828859    {
     860        $data = array();
    829861        try
    830862        {
    831863            $data['error'] = 0;
    832864
    833             if (empty($_POST['coupon_code'])) {
     865            if (!isset($_POST['coupon_code']) || empty($_POST['coupon_code'])) {
    834866                throw new Exception('Coupon code cannot be empty. Please check');
    835867            }
     
    838870
    839871            $coupon = new WC_Coupon($coupon_code);
    840             if (!empty($coupon->id))
     872            // Use getter method instead of direct property access (PHP 8.2+ compatible)
     873            $coupon_id = method_exists($coupon, 'get_id') ? $coupon->get_id() : 0;
     874            if (!empty($coupon_id))
    841875            {
    842876                $data['msg'] = 'Yes';
    843                 $data['coupon'] = json_decode($coupon, true);
     877                // Get coupon data array instead of trying to json_decode object
     878                $data['coupon'] = method_exists($coupon, 'get_data') ? $coupon->get_data() : array();
    844879            }
    845880            else
     
    859894    public function delete_coupon_code($request)
    860895    {
     896        $data = array();
    861897        try
    862898        {
    863899            $data['error'] = 0;
    864900
    865             if (empty($_POST['coupon_code'])) {
     901            if (!isset($_POST['coupon_code']) || empty($_POST['coupon_code'])) {
    866902                throw new Exception('Invalid coupon code');
    867903            }
     
    869905            $coupon_code = sanitize_text_field($_POST['coupon_code']);
    870906            $coupon = new WC_Coupon($coupon_code);
    871             if (!empty($coupon->id))
     907            // Use getter methods instead of direct property access (PHP 8.2+ compatible)
     908            $coupon_id = method_exists($coupon, 'get_id') ? $coupon->get_id() : 0;
     909            if (!empty($coupon_id))
    872910            {
    873                 $validate_usage = empty($_POST['validate_usage']) ? 0 : sanitize_text_field($_POST['validate_usage']);
    874                 if(!empty($validate_usage) && (!isset($coupon->usage_count) || $coupon->usage_count != 0))
     911                $validate_usage = (isset($_POST['validate_usage']) && !empty($_POST['validate_usage'])) ? sanitize_text_field($_POST['validate_usage']) : 0;
     912                $usage_count = method_exists($coupon, 'get_usage_count') ? $coupon->get_usage_count() : 0;
     913               
     914                if(!empty($validate_usage) && $usage_count != 0)
    875915                {
    876                     $data['id'] = $coupon->id;
    877                     $data['usage_count'] = $coupon->usage_count;
     916                    $data['id'] = $coupon_id;
     917                    $data['usage_count'] = $usage_count;
    878918                    throw new Exception('Coupon code already used');
    879919                }
    880920
    881                 $post_id = wp_delete_post($coupon->id, TRUE);
     921                $post_id = wp_delete_post($coupon_id, TRUE);
    882922                if ( is_wp_error( $post_id ) ) {
    883923                    throw new Exception( $post_id->get_error_message());
  • social-boost/trunk/includes/socialboost-functions.php

    r2444135 r3425122  
    3030
    3131                if(!empty($config_json))
    32                     $config = json_decode($config_json, true);
     32                    $config = amsb_json_decode($config_json, true, array());
    3333            }
    3434        }
  • social-boost/trunk/readme.txt

    r3398647 r3425122  
    55Requires at least: 3.0.1
    66Tested up to: 6.8
    7 Stable tag: 3.5.2
     7Stable tag: 3.5.3
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    437437WordPress version 6.8 release compatibility
    438438
    439 == Upgrade Notice ==
    440439= 3.5.2 =
    441440Improvements and bug fixes
    442441
     442= 3.5.3 =
     443PHP version 8.4 release compatibility
     444
    443445== Upgrade Notice ==
    444 = 3.5.2 =
    445 Improvements and bug fixes
     446= 3.5.3 =
     447PHP version 8.4 release compatibility
  • social-boost/trunk/socialboost.php

    r3398647 r3425122  
    22/**
    33 * @package Social Boost by Appsmav
    4  * @version 3.5.2
     4 * @version 3.5.3
    55 */
    66/*
     
    88 Plugin URI: http://appsmav.com
    99 Description: Get leads & customers. Boost social media followers.
    10  Version: 3.5.2
     10 Version: 3.5.3
    1111 Author: Appsmav
    1212 Author URI: http://appsmav.com
     
    3030defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    3131define('SB_PLUGIN_BASE_PATH', dirname(__FILE__));
     32// Load compatibility polyfills for PHP 5.4-8.4
     33if (file_exists(SB_PLUGIN_BASE_PATH . '/includes/compat/polyfills.php')) {
     34    require_once(SB_PLUGIN_BASE_PATH . '/includes/compat/polyfills.php');
     35}
    3236
    3337class Social_Boost
    3438{
    35     public static $_plugin_version  = '3.5.2';
     39    public static $_plugin_version  = '3.5.3';
    3640    public static $_callback_url = 'https://social.appsmav.com/';
    3741    public static $_api_version  = 'api/v1/';
    3842    protected static $_api_url      = 'https://clients.appsmav.com/api_v1.php';
    39     protected static $_c_sdk_url    = '//cdn.appsmav.com/sb/assets/js/widget-sdk.js?v=3.5.2';
     43    protected static $_c_sdk_url    = '//cdn.appsmav.com/sb/assets/js/widget-sdk.js?v=3.5.3';
    4044
    4145    /**
     
    110114        $id = isset($atts['id']) ? sanitize_text_field($atts['id']) : '';
    111115        $patternAlphaNum = '/^[a-zA-Z0-9]+$/';
    112         if (empty($id) || !preg_match($patternAlphaNum, $id) || strlen($id) > 10) {
     116        if (empty($id) || !amsb_preg_match($patternAlphaNum, $id) || amsb_strlen($id) > 10) {
    113117            return '';
    114118        }
    115119        $type = isset($atts['type']) ? sanitize_text_field($atts['type']) : '';
    116         if (!empty($type) && (!preg_match($patternAlphaNum, $type) || strlen($type) > 10)) {
     120        if (!empty($type) && (!amsb_preg_match($patternAlphaNum, $type) || amsb_strlen($type) > 10)) {
    117121            return '';
    118122        }
     
    178182        $orderConfig = '';
    179183        if (class_exists('WC_Integration')) {
    180             if (is_order_received_page()) {
     184            if (function_exists('is_order_received_page') && is_order_received_page()) {
    181185                $orderConfig = ', is_thankyou_page: "true"';
    182186                $order_id = self::_getOrderID();
     
    232236            global $wp;
    233237            $order_id = '';
    234             if (isset($wp->query_vars['order-received']) && !empty($wp->query_vars['order-received'])) {
    235                 $order_id = $wp->query_vars['order-received'];
    236             } else if(isset($_GET['view-order']) && !empty($_GET['view-order'])) {
     238
     239            // Validate $wp object exists and has query_vars
     240            if (isset($wp) && is_object($wp) && isset($wp->query_vars) && is_array($wp->query_vars)) {
     241                if (isset($wp->query_vars['order-received']) && !empty($wp->query_vars['order-received'])) {
     242                    $order_id = $wp->query_vars['order-received'];
     243                }
     244            }
     245
     246            if (empty($order_id) && isset($_GET['view-order']) && !empty($_GET['view-order'])) {
    237247                //check if on view-order page and get parameter is available
    238248                $order_id = sanitize_text_field($_GET['view-order']);
     
    240250                //check if on view order-received page and get parameter is available
    241251                $order_id = sanitize_text_field($_GET['order-received']);
    242             } elseif (isset($_GET['key']) && !empty($_GET['key']) && version_compare( WC_VERSION, '5.9', '>=' )) {
     252            } elseif (isset($_GET['key']) && !empty($_GET['key']) && amsb_check_wc_version('5.9', '>=') && function_exists('wc_get_order_id_by_order_key')) {
    243253                $order_id = wc_get_order_id_by_order_key( sanitize_text_field($_GET['key']) );
    244254            } else {
    245                 $url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    246                 $template_name = strpos($url,'/order-received/') === false ? '/view-order/' : '/order-received/';
    247                 if (strpos($url,$template_name) !== false) {
    248                     $start = strpos($url,$template_name);
    249                     $first_part = substr($url, $start+strlen($template_name));
    250                     $order_id = substr($first_part, 0, strpos($first_part, '/'));
     255                if (isset($_SERVER['SERVER_NAME']) && isset($_SERVER['REQUEST_URI'])) {
     256                    $url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
     257                    $template_name = amsb_strpos($url,'/order-received/') === false ? '/view-order/' : '/order-received/';
     258                    if (amsb_strpos($url,$template_name) !== false) {
     259                        $start = amsb_strpos($url,$template_name);
     260                        if ($start !== false) {
     261                            $first_part = amsb_substr($url, $start+amsb_strlen($template_name));
     262                            $slash_pos = amsb_strpos($first_part, '/');
     263                            if ($slash_pos !== false) {
     264                                $order_id = amsb_substr($first_part, 0, $slash_pos);
     265                            }
     266                        }
     267                    }
    251268                }
    252269            }
     
    326343        try
    327344        {
     345            // Validate $post object
     346            if (!is_object($post) || !isset($post->post_type) || !isset($post->ID)) {
     347                return;
     348            }
     349
    328350            // Only want to set if this is a old post!
    329351            if (!$update || 'page' !== $post->post_type) {
     
    356378
    357379            $res = self::_curlResp($param, $url);
    358             if(empty($res) || $res['error'] == 1) {
     380            if(empty($res) || (isset($res['error']) && $res['error'] == 1)) {
    359381                throw new Exception('VerificationFailed');
    360382            }
     
    362384        catch (Exception $ex)
    363385        {
    364             $resp['error'] = 1;
    365             $resp['msg']   = $ex->getMessage();
    366386        }
    367387    }
     
    395415
    396416            $res = self::_curlResp($param, $url);
    397             if(empty($res) || $res['error'] == 1)
     417            if(empty($res) || (isset($res['error']) && $res['error'] == 1))
    398418                throw new Exception('VerificationFailed');
    399419        }
    400420        catch (Exception $ex)
    401421        {
    402             $resp['error'] = 1;
    403             $resp['msg']   = $ex->getMessage();
    404422        }
    405423    }
     
    411429                // Check if our plugin was updated
    412430                $plugin_path = plugin_basename(__FILE__);
    413                 if (isset($options['plugins']) && is_array($options['plugins']) && in_array($plugin_path, $options['plugins'], true)) {
     431                if (isset($options['plugins']) && is_array($options['plugins']) && amsb_in_array($plugin_path, $options['plugins'], true)) {
    414432
    415433                    // Safely get plugin data
     
    420438                    $plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin_path);
    421439                    $new_version = isset($plugin_data['Version']) ? $plugin_data['Version'] : ''; // Fetch new version
    422                     if (!empty($new_version) && preg_match('/^\d+\.\d+\.\d+$/', $new_version)) {
     440                    if (!empty($new_version) && amsb_preg_match('/^\d+\.\d+\.\d+$/', $new_version)) {
    423441
    424442                        $app_id  = get_option('socialboost_appid', 0);
     
    441459
    442460                            // Validate API response
    443                             if (empty($response) || !empty($response['error'])) {
    444                                 throw new Exception('API request failed: ' . ($response['message'] ?? 'Unknown error'));
     461                            if (empty($response) || (isset($response['error']) && !empty($response['error']))) {
     462                                $error_msg = (isset($response['message']) && !empty($response['message'])) ? $response['message'] : 'Unknown error';
     463                                throw new Exception('API request failed: ' . $error_msg);
    445464                            }
    446465                        }
     
    454473    public function sb_ajax_check_grvlsw_settings()
    455474    {
     475        $res = array();
    456476        try {
    457477            // Verify nonce
     
    480500                $param['plugin_type'] = 'WOO';
    481501
    482             $res = array();
    483502            $res = self::_curlResp($param,self::$_api_url);
    484503
    485             if( !empty($res['is_shop']) && $res['is_shop'] == 1)
    486             {
     504            if( isset($res['is_shop']) && !empty($res['is_shop']) && $res['is_shop'] == 1)
     505            {
     506                // Validate required response fields
     507                if (!isset($res['id_shop']) || !isset($res['id_site']) || !isset($res['pay_load'])) {
     508                    throw new Exception('Invalid API response - missing required fields');
     509                }
    487510                update_option( 'socialboost_admin_email', $email);
    488511                update_option( 'socialboost_shop_id', $res['id_shop']);
     
    500523
    501524            }
    502             else if (!empty($res['is_shop']) && $res['is_shop'] == 2)
     525            else if (isset($res['is_shop']) && !empty($res['is_shop']) && $res['is_shop'] == 2)
    503526            {
    504527                $ip_info                = self::_getIPDetails();
     
    538561                $params['shop_name']    = get_option('blogname');
    539562                $params['campaign_name']= 'REWARDS';
    540                 $params['timezone']     = 'America/Chicago'; //Dummy $p['grappsmav_reg_timezone'];
    541                 $params['date_format']  = 'd/m/Y'; //Dummy$p['grappsmav_reg_date_format'];
     563                $params['timezone']     = 'America/Chicago';
     564                $params['date_format']  = 'd/m/Y';
    542565                $params['exclusion_period'] = 0;
    543566                $params['login_url']        = get_option('siteurl');
     
    548571                $res = self::_curlResp($params, self::$_api_url);
    549572
    550                 if ($res['error'] == 0)
     573                if (isset($res['error']) && $res['error'] == 0)
    551574                {
     575                    // Validate required response fields
    552576                    $res['sb_reg'] = 0;
    553                     update_option('socialboost_shop_id', $res['id_shop']);
    554                     update_option('socialboost_appid', $res['id_site']);
    555                     update_option('socialboost_payload', $res['pay_load']);
     577                    update_option('socialboost_shop_id', (isset($res['id_shop']) && !empty($res['id_shop'])) ? $res['id_shop'] : '');
     578                    update_option('socialboost_appid', (isset($res['id_site']) && !empty($res['id_site'])) ? $res['id_site'] : '');
     579                    update_option('socialboost_payload', (isset($res['pay_load']) && !empty($res['pay_load'])) ? $res['pay_load'] : '');
    556580                    update_option('socialboost_admin_email', $email);
    557581                    update_option('socialboost_register', 1 );
     
    561585                    $res['frame_url'] = self::$_callback_url.'autologin?id_shop='.$res['id_shop'].'&admin_email='.urlencode($params['email']).'&payload='.$res['pay_load'].'&autoredirect=auto';
    562586                }
    563                 else if ($res['error'] == 1)
     587                else if (isset($res['error']) && $res['error'] == 1)
    564588                    $res['sb_reg'] = 1;
    565                 else if ($res['error'] == 2 || $res['error'] == 3)
     589                else if (isset($res['error']) && ($res['error'] == 2 || $res['error'] == 3))
    566590                {
    567591                    update_option( 'socialboost_register', 3 );
     
    587611    public function sb_ajax_check_grvlsw_login()
    588612    {
     613        $res = array();
    589614        try
    590615        {
     
    595620            }
    596621
    597             if(empty($_POST['socialboost_login_email']) || !filter_var($_POST['socialboost_login_email'], FILTER_VALIDATE_EMAIL))
     622            if(!isset($_POST['socialboost_login_email']) || empty($_POST['socialboost_login_email']) || !filter_var($_POST['socialboost_login_email'], FILTER_VALIDATE_EMAIL))
    598623                throw new Exception("Please enter valid email");
    599624
    600             if(empty($_POST['socialboost_login_pwd']))
     625            if(!isset($_POST['socialboost_login_pwd']) || empty($_POST['socialboost_login_pwd']))
    601626                throw new Exception("Please enter password");
    602627
    603             $res = array();
    604628            $params = array();
    605629            $email                 = sanitize_email( $_POST['socialboost_login_email'] );
     
    619643                throw new Exception("Invalid Email / Password.");
    620644
    621             $resCurl = json_decode($response['body'], true);
    622 
    623             if ($resCurl['error'] == 0)
     645            $resCurl = amsb_json_decode($response['body'], true, array('error' => 1));
     646
     647            if (isset($resCurl['error']) && $resCurl['error'] == 0)
    624648            {
    625649                $plugin_type = 'WP';
     
    629653
    630654                update_option( 'socialboost_admin_email', $adminEmail);
    631                 update_option( 'socialboost_shop_id', $resCurl['id_shop']);
    632                 update_option('socialboost_appid', $resCurl['id_site']);
    633                 update_option( 'socialboost_payload', $resCurl['pay_load']);
     655                update_option( 'socialboost_shop_id', (isset($resCurl['id_shop']) && !empty($resCurl['id_shop'])) ? $resCurl['id_shop'] : '');
     656                update_option('socialboost_appid', (isset($resCurl['id_site']) && !empty($resCurl['id_site'])) ? $resCurl['id_site'] : '');
     657                update_option( 'socialboost_payload', (isset($resCurl['pay_load']) && !empty($resCurl['pay_load'])) ? $resCurl['pay_load'] : '');
    634658                update_option( 'socialboost_register', 1 );
    635659
     
    646670            {
    647671                $res['error']   = 1;
    648                 $res['message'] = (!empty($resCurl['message'])) ? $resCurl['message'] : "Invalid Email / Password";
     672                $res['message'] = (isset($resCurl['message']) && !empty($resCurl['message'])) ? $resCurl['message'] : "Invalid Email / Password";
    649673            }
    650674
     
    699723    protected static function _curlResp($param,$url)
    700724    {
     725        $resp = array();
    701726        $response = wp_remote_post($url,array('body'=> $param,'timeout' => 10));
    702727        if (is_array($response) && !empty($response['body'])) {
    703            $resp = json_decode($response['body'], true);
     728           $resp = amsb_json_decode($response['body'], true, array('error' => 1));
    704729        } else {
    705730           $resp['error']  = 1;
     
    711736    protected static function _getIPDetails()
    712737    {
     738        // Get client IP with fallback
     739        $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
     740
    713741        // Default return value for failure case of API request
    714         $ip  = $_SERVER['REMOTE_ADDR'];
    715742        $ip_details = array('ip'=>$ip, 'city'=>'', 'region_name'=>'', 'country_code'=>'US');
    716743
    717744        try {
     745            // Validate IP is not empty
     746            if (empty($ip)) {
     747                return $ip_details;
     748            }
    718749
    719750            $url = 'http://www.geoplugin.net/json.gp?ip='.$ip;
    720751            $response = wp_remote_get( $url );
    721752
    722             if (is_array($response) && !empty($response['body']))
    723             {
    724                 $ipLocArr = json_decode($response['body'], TRUE);
     753            if (is_array($response) && isset($response['body']) && !empty($response['body']))
     754            {
     755                $ipLocArr = amsb_json_decode($response['body'], TRUE, array());
    725756
    726757                /*
     
    730761                * https://stackoverflow.com/questions/28038278/getting-visitors-city-from-their-ip-not-working-geoplugin
    731762                */
    732                 if (!empty($ipLocArr['geoplugin_request']) && $ipLocArr['geoplugin_request'] == $ip && in_array($ipLocArr['geoplugin_status'], array(200, 206))) {
    733                     $ip_details['ip']            = empty($ipLocArr['geoplugin_request']) ? $ip : $ipLocArr['geoplugin_request'];
    734                     $ip_details['city']          = empty($ipLocArr['geoplugin_city']) ? null : $ipLocArr['geoplugin_city'];
    735                     $ip_details['region_name']   = empty($ipLocArr['geoplugin_regionName']) ? null : $ipLocArr['geoplugin_regionName'];
    736                     $ip_details['country_code']  = empty($ipLocArr['geoplugin_countryCode']) ? 'US' : $ipLocArr['geoplugin_countryCode'];
     763                if (is_array($ipLocArr) && isset($ipLocArr['geoplugin_request']) && !empty($ipLocArr['geoplugin_request']) &&
     764                    $ipLocArr['geoplugin_request'] == $ip &&
     765                    isset($ipLocArr['geoplugin_status']) && amsb_in_array($ipLocArr['geoplugin_status'], array(200, 206))) {
     766                    $ip_details['ip']            = isset($ipLocArr['geoplugin_request']) ? $ipLocArr['geoplugin_request'] : $ip;
     767                    $ip_details['city']          = isset($ipLocArr['geoplugin_city']) && !empty($ipLocArr['geoplugin_city']) ? $ipLocArr['geoplugin_city'] : '';
     768                    $ip_details['region_name']   = isset($ipLocArr['geoplugin_regionName']) && !empty($ipLocArr['geoplugin_regionName']) ? $ipLocArr['geoplugin_regionName'] : '';
     769                    $ip_details['country_code']  = isset($ipLocArr['geoplugin_countryCode']) && !empty($ipLocArr['geoplugin_countryCode']) ? $ipLocArr['geoplugin_countryCode'] : 'US';
    737770                }
    738771            }
     
    754787
    755788        $params['action']       = 'createaccount';
    756         $params['firstname']    = sanitize_text_field($p['socialboost_reg_firstname']);
    757         $params['lastname']     = sanitize_text_field($p['socialboost_reg_lastname']);
    758         $params["raffd"]        = sanitize_text_field($p['raffd']);
     789        $params['firstname']    = isset($p['socialboost_reg_firstname']) ? sanitize_text_field($p['socialboost_reg_firstname']) : '';
     790        $params['lastname']     = isset($p['socialboost_reg_lastname']) ? sanitize_text_field($p['socialboost_reg_lastname']) : '';
     791        $params["raffd"]        = isset($p['raffd']) ? sanitize_text_field($p['raffd']) : '';
    759792        $params['companyname']  = get_bloginfo('name');
    760793        $params['email']        = sanitize_email($p['socialboost_reg_email_user']);
     
    832865        if(isset($_REQUEST['grc']))
    833866        {
    834             if( !session_id())
    835                session_start();
    836 
    837            $_SESSION['grc']     = sanitize_text_field($_REQUEST['grc']);
    838            $_SESSION['gre']     = sanitize_text_field($_REQUEST['gre']);
    839            $_SESSION['typ']     = isset($_REQUEST['type']) ? sanitize_text_field($_REQUEST['type']) : 'gr';
    840            $_SESSION['scopeid'] = sanitize_text_field($_REQUEST['scopeid']);
    841            $_SESSION['mavtoken']= isset($_REQUEST['mavtoken']) ? sanitize_text_field($_REQUEST['mavtoken']) : '';
     867            if(amsb_session_start())
     868            {
     869               $_SESSION['grc']     = sanitize_text_field($_REQUEST['grc']);
     870               $_SESSION['gre']     = isset($_REQUEST['gre']) ? sanitize_text_field($_REQUEST['gre']) : '';
     871               $_SESSION['typ']     = isset($_REQUEST['type']) ? sanitize_text_field($_REQUEST['type']) : 'gr';
     872               $_SESSION['scopeid'] = isset($_REQUEST['scopeid']) ? sanitize_text_field($_REQUEST['scopeid']) : '';
     873               $_SESSION['mavtoken']= isset($_REQUEST['mavtoken']) ? sanitize_text_field($_REQUEST['mavtoken']) : '';
     874            }
    842875        }
    843876
     
    848881    public function send_comment_to_appsmav($comment_ID)
    849882    {
    850         if( !session_id())
    851             session_start();
     883        amsb_session_start();
    852884
    853885        if(isset($_SESSION['grc'])){
    854886
    855887            $mavtoken = '';
    856             if (!empty($_SESSION['mavtoken']))
     888            if (isset($_SESSION['mavtoken']) && !empty($_SESSION['mavtoken']))
    857889                $mavtoken = "&mavtoken=" . $_SESSION['mavtoken'];
     890
     891            $_SESSION['typ'] = isset($_SESSION['typ']) ? $_SESSION['typ'] : '';
     892            $_SESSION['grc'] = isset($_SESSION['grc']) ? $_SESSION['grc'] : '';
     893            $_SESSION['gre'] = isset($_SESSION['gre']) ? $_SESSION['gre'] : '';
     894            $_SESSION['scopeid'] = isset($_SESSION['scopeid']) ? $_SESSION['scopeid'] : '';
    858895
    859896            switch($_SESSION['typ'])
     
    900937            global $wpdb;
    901938
     939            // Initialize $_REQUEST['order_status'] to prevent PHP 8 E_WARNING
     940            if(!isset($_REQUEST['order_status']) || empty($_REQUEST['order_status']))
     941                $_REQUEST['order_status'] = '';
     942
    902943            // Check purchase campaign is enabled
    903944            $is_enabled = self::is_purchase_camp_enabled();
     
    907948            $order = new WC_Order($order_id);
    908949
     950            // Validate order exists
     951            if (!$order || !method_exists($order, 'get_id') || !$order->get_id()) {
     952                return; // Invalid order
     953            }
     954
    909955            $user_email = '';
    910956            $ordered_user = $order->get_user();
     
    913959
    914960            $status = $order->get_status();
    915             $param['order_status'] = strtolower($status);
    916 
    917             if(strtolower($status) != 'processing' && strtolower($status) != 'paid' && strtolower($status) != 'completed')
     961            $status_lower = amsb_strtolower($status);
     962            $param['order_status'] = $status_lower;
     963
     964            if($status_lower != 'processing' && $status_lower != 'paid' && $status_lower != 'completed')
    918965                $param['order_status'] = 'pending';
    919966
    920967            $param['user'] = $ordered_user;
    921             if (version_compare( WC_VERSION, '3.7', '<' ))
     968            if (amsb_check_wc_version( '3.7', '<' ))
    922969                $couponsArr = $order->get_used_coupons();
    923970            else
     
    931978            $param['tax'] = $order->get_total_tax();
    932979
    933             if(strtolower($status) == 'pending')
     980            if($status_lower == 'pending')
    934981               return;
    935982
    936             if(!empty($couponsArr))
     983            if(!empty($couponsArr) && isset($couponsArr[0]))
    937984                $param['coupon'] = $couponsArr[0];
    938985
    939             if(version_compare( WC_VERSION, '3.0', '<' )) {
     986            if(amsb_check_wc_version( '3.0', '<' )) {
    940987                $param['name'] = $order->get_billing_first_name();
    941988            }
     
    943990            {
    944991                $order_data = $order->get_data();
    945                 $param['name'] = empty($order_data['billing']['first_name']) ? '' : $order_data['billing']['first_name'];
     992                $param['name'] = (isset($order_data['billing']['first_name']) && !empty($order_data['billing']['first_name'])) ? $order_data['billing']['first_name'] : '';
    946993            }
    947994
     
    9531000            $param['comment'] = 'Order Id - ' . $order_id . ' From ' . get_option('siteurl');
    9541001            $param['status'] = 'Add';
    955             $param['created_date'] = $order->get_date_created()->format('c');
     1002            $date_created = $order->get_date_created();
     1003            $param['created_date'] = ($date_created && method_exists($date_created, 'format')) ? $date_created->format('c') : date('c');
    9561004            $param['user_ip'] = $order->get_customer_ip_address();
    9571005
    958             if(version_compare( WC_VERSION, '3.0', '<' ))
     1006            if(amsb_check_wc_version( '3.0', '<' ))
    9591007                $curOrder = $order->get_order_currency();
    9601008            else
     
    9701018
    9711019                $prodArr = $order->get_items();
     1020
     1021                // Validate items array
     1022                if (!is_array($prodArr) || empty($prodArr)) {
     1023                    $prodArr = array();
     1024                }
     1025
    9721026                $total = 0;
    9731027
    9741028                foreach($prodArr as $prod)
    9751029                {
    976                     $product = new WC_Product($prod['product_id']);
     1030                    // Validate product ID exists in array
     1031                    if (!isset($prod['product_id'])) {
     1032                        continue;
     1033                    }
     1034
     1035                    $product = wc_get_product($prod['product_id']);
     1036                    if (!$product) {
     1037                        continue; // Skip if product not found
     1038                    }
     1039
    9771040                    $get_items_sql = $wpdb->prepare("select * from {$wpdb->prefix}postmeta WHERE meta_key = %s AND post_id = %d", '_price', $prod['product_id']);
    9781041                    $line_item = $wpdb->get_row($get_items_sql);
    979                     $price = $line_item->meta_value;
    980 
    981                     if(empty($price))
    982                         $price = $product->price;
    983 
    984                     $total += $price * $prod['qty'];
    985                 }
    986 
    987                 $ratio = $param['subtotal'] / $total;
    988                 $param['total'] = $param['total'] / $ratio;
    989                 $param['subtotal'] = $param['subtotal'] / $ratio;
    990                 $param['shipping'] = $param['shipping'] / $ratio;
    991                 $param['shipping_tax'] = $param['shipping_tax'] / $ratio;
    992                 $param['tax'] = $param['tax'] / $ratio;
     1042
     1043                    // Get price from meta or product object
     1044                    $price = 0;
     1045                    if ($line_item && isset($line_item->meta_value) && !empty($line_item->meta_value)) {
     1046                        $price = $line_item->meta_value;
     1047                    } else {
     1048                        $price = $product->get_price();
     1049                    }
     1050
     1051                    $prod['qty'] = isset($prod['qty']) ? $prod['qty'] : 1;
     1052                    $total += floatval($price) * floatval($prod['qty']);
     1053                }
     1054
     1055                $ratio = 1;
     1056                if ($total > 0) {
     1057                    $ratio = ($param['subtotal'] > 0) ? ($param['subtotal'] / $total) : 1;
     1058                    $param['total'] = $param['total'] / $ratio;
     1059                    $param['subtotal'] = $param['subtotal'] / $ratio;
     1060                    $param['shipping'] = $param['shipping'] / $ratio;
     1061                    $param['shipping_tax'] = $param['shipping_tax'] / $ratio;
     1062                    $param['tax'] = $param['tax'] / $ratio;
     1063                }
    9931064
    9941065                $param['currency_conversion'] = array(
     
    10091080                {
    10101081                    if( $order->get_parent_id() === 0 && get_post_meta( $order_id, 'has_wcmp_sub_order', true ) == '1'){
    1011                         $param['comment'] = 'Main WCMp Order Id ' . str_replace('wc-', '', sanitize_text_field($_REQUEST['order_status'])) . ' - ' . $order_id . ' From ' . get_option('socialboost_shop_id', 0).' total '.$param['total'];
     1082                        $param['comment'] = 'Main WCMp Order Id ' . amsb_str_replace('wc-', '', sanitize_text_field($_REQUEST['order_status'])) . ' - ' . $order_id . ' From ' . get_option('socialboost_shop_id', 0).' total '.$param['total'];
    10121083                        $param['total'] = 0;
    10131084                        $param['subtotal'] = 0;
     
    10201091                {
    10211092                    if( $order->get_parent_id() === 0 && get_post_meta( $order_id, 'has_sub_order', true ) == '1'){
    1022                         $param['comment'] = 'Main Dokan Order Id ' . str_replace('wc-', '', sanitize_text_field($_REQUEST['order_status'])) . ' - ' . $order_id . ' From ' . get_option('socialboost_shop_id', 0).' total '.$param['total'];
     1093                        $param['comment'] = 'Main Dokan Order Id ' . amsb_str_replace('wc-', '', sanitize_text_field($_REQUEST['order_status'])) . ' - ' . $order_id . ' From ' . get_option('socialboost_shop_id', 0).' total '.$param['total'];
    10231094                        $param['total'] = 0;
    10241095                        $param['subtotal'] = 0;
     
    10531124
    10541125            $order = new WC_Order($order_id);
     1126            // Validate order exists
     1127            if (!$order || !method_exists($order, 'get_id') || !$order->get_id()) {
     1128                return; // Invalid order
     1129            }
    10551130            $status = $order->get_status();
    10561131            $arrayAdd = array('processing', 'completed');
     
    10651140                $user_email = $ordered_user->get('user_email');
    10661141
    1067             if(in_array($status, $arrayAdd))
     1142            if(amsb_in_array($status, $arrayAdd))
    10681143            {
    10691144                $urlApi = self::$_callback_url . self::$_api_version . 'addPurchaseEntry';
     
    10761151            }
    10771152
    1078             if (version_compare( WC_VERSION, '3.7', '<' ))
     1153            if (amsb_check_wc_version( '3.7', '<' ))
    10791154                $couponsArr = $order->get_used_coupons();
    10801155            else
    10811156                $couponsArr = $order->get_coupon_codes();
    10821157
    1083             if(!empty($couponsArr))
     1158            if(!empty($couponsArr) && isset($couponsArr[0]))
    10841159                $param['coupon'] = $couponsArr[0];
    10851160
     
    10971172            $param['tax'] = $order->get_total_tax();
    10981173
    1099             if(version_compare( WC_VERSION, '3.0', '<' ))
     1174            if(amsb_check_wc_version( '3.0', '<' ))
    11001175                $curOrder = $order->get_order_currency();
    11011176            else
     
    11091184
    11101185                $prodArr = $order->get_items();
     1186
     1187                // Validate items array
     1188                if (!is_array($prodArr) || empty($prodArr)) {
     1189                    $prodArr = array();
     1190                }
     1191
    11111192                $subtotal = 0;
    11121193
    11131194                foreach($prodArr as $prod)
    11141195                {
    1115                     $product = new WC_Product($prod['product_id']);
     1196                    // Validate product ID exists in array
     1197                    if (!isset($prod['product_id'])) {
     1198                        continue;
     1199                    }
     1200
     1201                    $product = wc_get_product($prod['product_id']);
     1202                    if (!$product) {
     1203                        continue; // Skip if product not found
     1204                    }
     1205
    11161206                    $get_items_sql = $wpdb->prepare("select * from {$wpdb->prefix}postmeta WHERE meta_key = %s AND post_id = %d", '_price', $prod['product_id']);
    11171207                    $line_item = $wpdb->get_row($get_items_sql);
    1118                     $price = $line_item->meta_value;
    1119 
    1120                     if(empty($price))
    1121                         $price = $product->price;
    1122 
    1123                     $subtotal += $price * $prod['qty'];
    1124                 }
    1125 
    1126                 $ratio = $order->get_subtotal() / $subtotal;
    1127                 $param['total'] = $param['total'] / $ratio;
    1128                 $param['subtotal'] = $param['subtotal'] / $ratio;
    1129                 $param['shipping'] = $param['shipping'] / $ratio;
    1130                 $param['shipping_tax'] = $param['shipping_tax'] / $ratio;
    1131                 $param['tax'] = $param['tax'] / $ratio;
    1132                 $param['refunded'] = $param['refunded'] / $ratio;
    1133                 $param['discount'] = $param['discount'] / $ratio;
     1208
     1209                    // Get price from meta or product object
     1210                    $price = 0;
     1211                    if ($line_item && isset($line_item->meta_value) && !empty($line_item->meta_value)) {
     1212                        $price = $line_item->meta_value;
     1213                    } else {
     1214                        $price = $product->get_price();
     1215                    }
     1216
     1217                    $prod['qty'] = isset($prod['qty']) ? $prod['qty'] : 1;
     1218                    $subtotal += floatval($price) * floatval($prod['qty']);
     1219                }
     1220
     1221                // CRITICAL: Prevent division by zero
     1222                $ratio = 1; // Default ratio if division cannot be performed
     1223                $order_subtotal = $order->get_subtotal();
     1224                if ($subtotal > 0 && $order_subtotal > 0) {
     1225                    $ratio = ($order_subtotal > 0) ? ($order_subtotal / $subtotal) : 1;
     1226                    $param['total'] = $param['total'] / $ratio;
     1227                    $param['subtotal'] = $param['subtotal'] / $ratio;
     1228                    $param['shipping'] = $param['shipping'] / $ratio;
     1229                    $param['shipping_tax'] = $param['shipping_tax'] / $ratio;
     1230                    $param['tax'] = $param['tax'] / $ratio;
     1231                    $param['refunded'] = $param['refunded'] / $ratio;
     1232                    $param['discount'] = $param['discount'] / $ratio;
     1233                }
    11341234
    11351235                $param['currency_conversion'] = array(
     
    11471247            }
    11481248
    1149             if(empty($_REQUEST['order_status']))
     1249            if(amsb_check_wc_version( '3.0', '<' ))
     1250            {
     1251                $param['name'] = $order->get_billing_first_name();
     1252            }
     1253            else
     1254            {
     1255                $order_data = $order->get_data();
     1256                $param['name'] = (isset($order_data['billing']['first_name']) && !empty($order_data['billing']['first_name'])) ? $order_data['billing']['first_name'] : '';
     1257            }
     1258
     1259            $date_created = $order->get_date_created();
     1260            if(!isset($_REQUEST['order_status']) || empty($_REQUEST['order_status'])) {
    11501261                $_REQUEST['order_status'] = '';
    1151 
    1152             if(version_compare( WC_VERSION, '3.0', '<' ))
    1153             {
    1154                 $param['name'] = $order->get_billing_first_name();
    1155             }
    1156             else
    1157             {
    1158                 $order_data = $order->get_data();
    1159                 $param['name'] = empty($order_data['billing']['first_name']) ? '' : $order_data['billing']['first_name'];
    1160             }
    1161 
    1162             $param['created_date'] = $order->get_date_created()->format('c');
     1262            }
     1263
     1264            $param['created_date'] = ($date_created && method_exists($date_created, 'format')) ? $date_created->format('c') : date('c');
    11631265            $param['user_ip'] = $order->get_customer_ip_address();
    11641266            $param['email'] = !empty($user_email) ? $user_email : $order->get_billing_email();
    11651267            $param['customer_id'] = $order->get_user_id();
    1166             $param['comment'] = 'Order Id ' . str_replace('wc-', '', sanitize_text_field($_REQUEST['order_status'])) . ' - ' . $order_id . ' From ' . get_option('siteurl');
     1268            $param['comment'] = 'Order Id ' . amsb_str_replace('wc-', '', sanitize_text_field($_REQUEST['order_status'])) . ' - ' . $order_id . ' From ' . get_option('siteurl');
    11671269            $param['order'] = 0;
    11681270            $param['id_order'] = $order_id;
     
    11731275                {
    11741276                    if( $order->get_parent_id() === 0 && get_post_meta( $order_id, 'has_wcmp_sub_order', true ) == '1'){
    1175                         $param['comment'] = 'Main WCMp Order Id ' . str_replace('wc-', '', sanitize_text_field($_REQUEST['order_status'])) . ' - ' . $order_id . ' From ' . get_option('socialboost_shop_id', 0).' total '.$param['total'];
     1277                        $param['comment'] = 'Main WCMp Order Id ' . amsb_str_replace('wc-', '', sanitize_text_field($_REQUEST['order_status'])) . ' - ' . $order_id . ' From ' . get_option('socialboost_shop_id', 0).' total '.$param['total'];
    11761278                        $param['total'] = 0;
    11771279                        $param['subtotal'] = 0;
     
    11841286                {
    11851287                    if( $order->get_parent_id() === 0 && get_post_meta( $order_id, 'has_sub_order', true ) == '1'){
    1186                         $param['comment'] = 'Main Dokan Order Id ' . str_replace('wc-', '', sanitize_text_field($_REQUEST['order_status'])) . ' - ' . $order_id . ' From ' . get_option('socialboost_shop_id', 0).' total '.$param['total'];
     1288                        $param['comment'] = 'Main Dokan Order Id ' . amsb_str_replace('wc-', '', sanitize_text_field($_REQUEST['order_status'])) . ' - ' . $order_id . ' From ' . get_option('socialboost_shop_id', 0).' total '.$param['total'];
    11871289                        $param['total'] = 0;
    11881290                        $param['subtotal'] = 0;
     
    12091311            global $wpdb;
    12101312            $order = new WC_Order($order_id);
     1313
     1314            // Validate order exists
     1315            if (!$order || !method_exists($order, 'get_id') || !$order->get_id()) {
     1316                return; // Invalid order
     1317            }
    12111318
    12121319            // Check purchase campaign is enabled
     
    12361343                $email = $ordered_user->get('user_email');
    12371344
    1238             if(empty($_REQUEST['refund_amount']))
     1345            if(!isset($_REQUEST['refund_amount']) || empty($_REQUEST['refund_amount']))
    12391346                return;
    12401347
     
    12481355
    12491356            $param['curShop'] = get_option('woocommerce_currency', 'USD');
    1250             if(version_compare( WC_VERSION, '3.0', '<' ))
     1357            if(amsb_check_wc_version( '3.0', '<' ))
    12511358                $param['curOrder'] = $order->get_order_currency();
    12521359            else
     
    12541361
    12551362            $refundData = array();
    1256             foreach($order->get_refunds() as $refunds) {
    1257                 $refundData['discount_total'] = $refunds->discount_total;
    1258                 $refundData['discount_tax'] = $refunds->discount_tax;
    1259                 $refundData['shipping_total'] = $refunds->shipping_total;
    1260                 $refundData['shipping_tax'] = $refunds->shipping_tax;
    1261                 $refundData['cart_tax'] = $refunds->cart_tax;
    1262                 $refundData['total'] = $refunds->total;
    1263                 $refundData['total_tax'] = $refunds->total_tax;
    1264                 $refundData['amount'] = $refunds->amount;
     1363            $order_refunds = $order->get_refunds();
     1364
     1365            // Validate refunds array
     1366            if (!is_array($order_refunds) || empty($order_refunds)) {
     1367                return; // No refunds to process
     1368            }
     1369
     1370            foreach($order_refunds as $refunds) {
     1371                $refundData['discount_total'] = method_exists($refunds, 'get_discount_total') ? $refunds->get_discount_total() : 0;
     1372                $refundData['discount_tax'] = method_exists($refunds, 'get_discount_tax') ? $refunds->get_discount_tax() : 0;
     1373                $refundData['shipping_total'] = method_exists($refunds, 'get_shipping_total') ? $refunds->get_shipping_total() : 0;
     1374                $refundData['shipping_tax'] = method_exists($refunds, 'get_shipping_tax') ? $refunds->get_shipping_tax() : 0;
     1375                $refundData['cart_tax'] = method_exists($refunds, 'get_cart_tax') ? $refunds->get_cart_tax() : 0;
     1376                $refundData['total'] = method_exists($refunds, 'get_total') ? $refunds->get_total() : 0;
     1377                $refundData['total_tax'] = method_exists($refunds, 'get_total_tax') ? $refunds->get_total_tax() : 0;
     1378                $refundData['amount'] = method_exists($refunds, 'get_amount') ? $refunds->get_amount() : 0;
    12651379
    12661380                $refundData['product_total'] = 0;
    1267                 foreach($refunds->get_items(array('line_item')) as $key => $lineItemObj) {
    1268                     $refundData['product_total'] += $lineItemObj->get_subtotal();
     1381                $refund_items = $refunds->get_items(array('line_item'));
     1382
     1383                // Validate items array before foreach
     1384                if (is_array($refund_items) && !empty($refund_items)) {
     1385                    foreach($refund_items as $key => $lineItemObj) {
     1386                        if (method_exists($lineItemObj, 'get_subtotal')) {
     1387                            $refundData['product_total'] += $lineItemObj->get_subtotal();
     1388                        }
     1389                    }
    12691390                }
    12701391
     
    12761397            {
    12771398                $prodArr = $order->get_items();
     1399
     1400                // Validate items array
     1401                if (!is_array($prodArr) || empty($prodArr)) {
     1402                    $prodArr = array();
     1403                }
     1404
    12781405                $subtotal = 0;
    12791406
    12801407                foreach($prodArr as $prod)
    12811408                {
    1282                     $product = new WC_Product($prod['product_id']);
     1409                    // Validate product ID exists in array
     1410                    if (!isset($prod['product_id'])) {
     1411                        continue;
     1412                    }
     1413
     1414                    $product = wc_get_product($prod['product_id']);
     1415                    if (!$product) {
     1416                        continue; // Skip if product not found
     1417                    }
     1418
    12831419                    $get_items_sql = $wpdb->prepare("select * from {$wpdb->prefix}postmeta WHERE meta_key = %s AND post_id = %d", '_price', $prod['product_id']);
    12841420                    $line_item = $wpdb->get_row($get_items_sql);
    1285                     $price = $line_item->meta_value;
    1286 
    1287                     if(empty($price))
    1288                         $price = $product->price;
    1289 
    1290                     $subtotal += $price * $prod['qty'];
    1291                 }
    1292 
    1293                 $ratio = $order->get_subtotal() / $subtotal;
    1294                 $refundData['discount_total'] = $refundData['discount_total'] / $ratio;
    1295                 $refundData['discount_tax']   = $refundData['discount_tax'] / $ratio;
    1296                 $refundData['shipping_total'] = $refundData['shipping_total'] / $ratio;
    1297                 $refundData['shipping_tax']   = $refundData['shipping_tax'] / $ratio;
    1298                 $refundData['cart_tax']       = $refundData['cart_tax'] / $ratio;
    1299                 $refundData['total']          = $refundData['total'] / $ratio;
    1300                 $refundData['total_tax']      = $refundData['total_tax'] / $ratio;
    1301                 $refundData['amount']         = $refundData['amount'] / $ratio;
    1302                 $refundData['product_total']  = $refundData['product_total'] / $ratio;
     1421
     1422                    // Get price from meta or product object
     1423                    $price = 0;
     1424                    if ($line_item && isset($line_item->meta_value) && !empty($line_item->meta_value)) {
     1425                        $price = $line_item->meta_value;
     1426                    } else {
     1427                        $price = $product->get_price();
     1428                    }
     1429
     1430                    $prod['qty'] = isset($prod['qty']) ? $prod['qty'] : 1;
     1431                    $subtotal += floatval($price) * floatval($prod['qty']);
     1432                }
     1433
     1434                // CRITICAL: Prevent division by zero and initialize ratio
     1435                $ratio = 1; // Default ratio
     1436                $order_subtotal = $order->get_subtotal();
     1437                if ($subtotal > 0 && $order_subtotal > 0) {
     1438                    $ratio = ($order_subtotal > 0) ? ($order_subtotal / $subtotal) : 1;
     1439                    $refundData['discount_total'] = $refundData['discount_total'] / $ratio;
     1440                    $refundData['discount_tax']   = $refundData['discount_tax'] / $ratio;
     1441                    $refundData['shipping_total'] = $refundData['shipping_total'] / $ratio;
     1442                    $refundData['shipping_tax']   = $refundData['shipping_tax'] / $ratio;
     1443                    $refundData['cart_tax']       = $refundData['cart_tax'] / $ratio;
     1444                    $refundData['total']          = $refundData['total'] / $ratio;
     1445                    $refundData['total_tax']      = $refundData['total_tax'] / $ratio;
     1446                    $refundData['amount']         = $refundData['amount'] / $ratio;
     1447                    $refundData['product_total']  = $refundData['product_total'] / $ratio;
     1448                }
    13031449
    13041450                $param['currency_conversion'] = array(
     
    13271473            $param['tax'] = $order->get_total_tax();
    13281474
    1329             $param['created_date'] = $order->get_date_created()->format('c');
     1475            $date_created = $order->get_date_created();
     1476            $param['created_date'] = ($date_created && method_exists($date_created, 'format')) ? $date_created->format('c') : date('c');
    13301477            $param['user_ip'] = $order->get_customer_ip_address();
    13311478            $param['email'] = !empty($email) ? $email : $order->get_billing_email();
     
    13361483            $urlApi = self::$_callback_url . self::$_api_version . 'removePurchaseEntry';
    13371484
    1338             if(version_compare( WC_VERSION, '3.0', '<' ))
     1485            if(amsb_check_wc_version( '3.0', '<' ))
    13391486            {
    13401487                $param['name'] = $order->get_billing_first_name();
     
    13431490            {
    13441491                $order_data = $order->get_data();
    1345                 $param['name'] = empty($order_data['billing']['first_name']) ? '' : $order_data['billing']['first_name'];
     1492                $param['name'] = (isset($order_data['billing']['first_name']) && !empty($order_data['billing']['first_name'])) ? $order_data['billing']['first_name'] : '';
    13461493            }
    13471494
     
    13651512            global $wpdb;
    13661513
    1367             if(!empty($_REQUEST['action']) && sanitize_text_field($_REQUEST['action']) == 'woocommerce_delete_refund')
     1514            if(isset($_REQUEST['action']) && !empty($_REQUEST['action']) && sanitize_text_field($_REQUEST['action']) == 'woocommerce_delete_refund')
    13681515            {
    13691516                // Check purchase campaign is enabled
     
    13731520
    13741521                $refund = new WC_Order_Refund($refund_id);
    1375                 $order = new WC_Order($refund->post->post_parent);
     1522
     1523                // Validate refund object exists
     1524                if (!$refund || !method_exists($refund, 'get_id') || !$refund->get_id()) {
     1525                    return; // Invalid refund
     1526                }
     1527
     1528                $parent_order_id = method_exists($refund, 'get_parent_id') ? $refund->get_parent_id() : 0;
     1529                if (!$parent_order_id) {
     1530                    return; // No parent order found
     1531                }
     1532                $order = new WC_Order($parent_order_id);
     1533
     1534                // Validate order exists
     1535                if (!$order || !method_exists($order, 'get_id') || !$order->get_id()) {
     1536                    return; // Invalid parent order
     1537                }
    13761538
    13771539                $param['discount'] = $order->get_total_discount();
     
    13821544                $param['tax'] = $order->get_total_tax();
    13831545
    1384                 if(version_compare( WC_VERSION, '3.0', '<' ))
     1546                if(amsb_check_wc_version( '3.0', '<' ))
    13851547                    $curOrder = $order->get_order_currency();
    13861548                else
     
    14041566                    foreach($prodArr as $prod)
    14051567                    {
    1406                         $product = new WC_Product($prod['product_id']);
     1568                        // Validate product ID exists in array
     1569                        if (!isset($prod['product_id'])) {
     1570                            continue;
     1571                        }
     1572
     1573                        $product = wc_get_product($prod['product_id']);
     1574                        if (!$product) {
     1575                            continue; // Skip if product not found
     1576                        }
     1577
    14071578                        $get_items_sql = $wpdb->prepare("select * from {$wpdb->prefix}postmeta WHERE meta_key = %s AND post_id = %d", '_price', $prod['product_id']);
    14081579                        $line_item = $wpdb->get_row($get_items_sql);
    1409                         $price = $line_item->meta_value;
    1410 
    1411                         if(empty($price))
    1412                             $price = $product->price;
    1413 
    1414                         $total += $price * $prod['qty'];
     1580
     1581                        // Get price from meta or product object
     1582                        $price = 0;
     1583                        if ($line_item && isset($line_item->meta_value) && !empty($line_item->meta_value)) {
     1584                            $price = $line_item->meta_value;
     1585                        } else {
     1586                            $price = $product->get_price();
     1587                        }
     1588
     1589                        $prod['qty'] = isset($prod['qty']) ? $prod['qty'] : 1;
     1590                        $total += floatval($price) * floatval($prod['qty']);
    14151591                    }
    14161592
    1417                     $ratio = $param['subtotal'] / $total;
    1418                     $param['total'] = $param['total'] / $ratio;
    1419                 }
    1420 
    1421                 $param['created_date'] = $order->get_date_created()->format('c');
     1593                    if ($total > 0 && $param['subtotal'] > 0) {
     1594                        $ratio = ($param['subtotal'] > 0) ? ($param['subtotal'] / $total) : 1;
     1595                        $param['total'] = $param['total'] / $ratio;
     1596                    }
     1597                }
     1598
     1599                $date_created = $order->get_date_created();
     1600                $param['created_date'] = ($date_created && method_exists($date_created, 'format')) ? $date_created->format('c') : date('c');
    14221601                $param['user_ip'] = $order->get_customer_ip_address();
    14231602                $param['email'] = !empty($email) ? $email : $order->get_billing_email();
    14241603                $param['customer_id'] = $order->get_user_id();
    14251604                $param['order'] = 0;
    1426                 $param['id_order'] = $refund->post->post_parent;
     1605                $param['id_order'] = $parent_order_id; // Already obtained from get_parent_id()
    14271606                $urlApi = self::$_callback_url . self::$_api_version . 'addPurchaseEntry';
    14281607
    1429                 if(version_compare( WC_VERSION, '3.0', '<' ))
     1608                if(amsb_check_wc_version( '3.0', '<' ))
    14301609                {
    14311610                    $param['name'] = $order->get_billing_first_name();
     
    14371616                }
    14381617
    1439                 $param['comment'] = 'Order Id Refund Restore - ' . $refund->post->post_parent . ' From ' . get_option('siteurl');
     1618                $param['comment'] = 'Order Id Refund Restore - ' . $parent_order_id . ' From ' . get_option('siteurl');
    14401619                $param['status'] = 'Add';
    14411620                $param['order_status'] = $order->get_status();
     
    14521631    {
    14531632        $msg = '';
     1633        $params = array();
    14541634        try
    14551635        {
     
    14711651            $params['name'] = isset($param['name']) ? $param['name'] : '';
    14721652            $params['comment'] = isset($param['comment']) ? $param['comment'] : '';
    1473             $params["app_lang"] = str_replace('-', '_', get_bloginfo('language'));
    1474             $allparam = implode('#'.$params['app'].'#', $paramSalt);
     1653
     1654            // Validate get_bloginfo result
     1655            $blog_lang = get_bloginfo('language');
     1656            $params["app_lang"] = !empty($blog_lang) ? amsb_str_replace('-', '_', $blog_lang) : '';
     1657
     1658            $allparam = amsb_implode('#'.$params['app'].'#', $paramSalt);
    14751659            $params['salt'] = md5($allparam);
    14761660            $params['id_shop'] = $shop_id;
     
    15031687            {
    15041688                $res = self::_curlResp($params, $urlApi);
    1505                 if(!empty($res['error']))
     1689                if(isset($res['error']) && !empty($res['error']))
    15061690                    $msg = 'Unexpected error occur. Please check with administrator.';
    15071691            }
     
    15551739                throw new Exception('Verification request failed');
    15561740
    1557             $resp_data = json_decode($response['body'], true);
     1741            $resp_data = amsb_json_decode($response['body'], true, array('error' => 1));
    15581742
    15591743            if (isset($resp_data['error']) && $resp_data['error'] == 0 && isset($resp_data['config'])) {
    1560                 $config = $resp_data['config'];
    1561 
    1562                 if (!empty($app_config) && is_array($app_config))
     1744                $config = is_array($resp_data['config']) ? $resp_data['config'] : array();
     1745
     1746                if (!empty($app_config) && is_array($app_config) && !empty($config))
    15631747                    $config = array_merge($app_config, $config);
    15641748
Note: See TracChangeset for help on using the changeset viewer.