Plugin Directory

Changeset 3445533


Ignore:
Timestamp:
01/23/2026 11:35:55 AM (4 weeks ago)
Author:
shipo
Message:

Minor fix checkout functions for PHP 7

Location:
shipo/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • shipo/trunk/includes/class-shipo-admin-order.php

    r3403157 r3445533  
    11<?php
     2if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
     3
    24/**
    35 * Gestionează afișarea informațiilor despre comandă în panoul de administrare
  • shipo/trunk/includes/class-shipo-ajax.php

    r3440612 r3445533  
    11<?php
     2
     3if ( ! defined( 'ABSPATH' ) ) exit;
     4
    25/**
    36 * Handles all AJAX requests
     
    1114     */
    1215    public function __construct() {
     16        // Fix user authentication for REST API
     17        add_filter('determine_current_user', [$this, 'authenticate_rest_user'], 20);
     18
    1319        add_action('rest_api_init', array($this, 'register_rest_routes'));
    1420     
     
    2026
    2127        add_action('wp_ajax_shipo_print_download_awb', array($this, 'print_download_awb'));
     28    }
     29
     30    /**
     31     * Authenticate user for REST API requests
     32     */
     33    public function authenticate_rest_user($user_id) {
     34        // If user is already authenticated, return the user
     35        if ($user_id) {
     36            return $user_id;
     37        }
     38       
     39        // Only proceed for our REST API namespace
     40        if (!empty(sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI']))) && strpos(sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])), '/wp-json/shipo/') === false) {
     41            return $user_id;
     42        }
     43       
     44        // Check for cookie authentication
     45        if (!empty(sanitize_text_field(wp_unslash($_COOKIE[LOGGED_IN_COOKIE])))) {
     46            // Get user from auth cookie
     47            $cookie_parts = explode('|', sanitize_text_field(wp_unslash($_COOKIE[LOGGED_IN_COOKIE])));
     48            if (count($cookie_parts) !== 3) {
     49                return $user_id;
     50            }
     51           
     52            $username = $cookie_parts[0];
     53            $expiration = $cookie_parts[1];
     54            $token = $cookie_parts[2];
     55           
     56            // Make sure the cookie is not expired
     57            if ($expiration < time()) {
     58                return $user_id;
     59            }
     60           
     61            // Get user by username
     62            $user = get_user_by('login', $username);
     63            if (!$user) {
     64                return $user_id;
     65            }
     66           
     67            // Return the user ID
     68            return $user->ID;
     69        }
     70
     71        return $user_id;
    2272    }
    2373
     
    97147        }
    98148    }
    99    
     149
     150    /**
     151     * Enhanced nonce verification with additional checks
     152     */
     153    private function verify_request_nonce($nonce) {
     154        if (empty($nonce)) {
     155            return false;
     156        }
     157       
     158        // Standard WordPress verification
     159        $result = wp_verify_nonce($nonce, 'shipo_nonce_action');
     160       
     161        // If verification fails, try additional verification methods
     162        if (!$result) {           
     163            // Check cookie for user ID
     164            if (!empty(sanitize_text_field(wp_unslash($_COOKIE[LOGGED_IN_COOKIE])))) {
     165                $cookie_elements = explode('|', sanitize_text_field(wp_unslash($_COOKIE[LOGGED_IN_COOKIE])));
     166                if (count($cookie_elements) >= 3) {
     167                    $username = $cookie_elements[0];
     168                    $user = get_user_by('login', $username);
     169                   
     170                    if ($user) {
     171                        // Try verification with this user ID
     172                       
     173                        // Save current user
     174                        $current_user_id = get_current_user_id();
     175                       
     176                        // Temporarily set user
     177                        wp_set_current_user($user->ID);
     178                       
     179                        // Try verification again
     180                        $result = wp_verify_nonce($nonce, 'shipo_nonce_action');
     181                       
     182                        // Restore original user
     183                        wp_set_current_user($current_user_id);
     184                    }
     185                }
     186            }
     187           
     188            // If still failed, try with user ID 0
     189            if (!$result) {               
     190                // Save current user
     191                $current_user_id = get_current_user_id();
     192               
     193                // Set user to 0
     194                wp_set_current_user(0);
     195               
     196                // Try verification again
     197                $result = wp_verify_nonce($nonce, 'shipo_nonce_action');
     198               
     199                // Restore original user
     200                wp_set_current_user($current_user_id);
     201            }
     202        }
     203       
     204        return (bool) $result;
     205    }
     206       
    100207    /**
    101208     * Get cities
    102209     */
    103210    public function get_cities() {       
    104         // Checks nonce for security
    105         if (!isset($_POST['shipo_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['shipo_nonce'])), 'shipo_nonce_action')) {
    106             wp_send_json_error(array('message' => 'Security failed.'));
     211        // Get parameters
     212        $nonce = '';
     213        if(isset($_POST['shipo_nonce'])) {
     214            $nonce = sanitize_text_field(wp_unslash($_POST['shipo_nonce']));
     215        }
     216       
     217        // Verify nonce
     218        if (!$this->verify_request_nonce($nonce)) {
     219            wp_send_json_error(array(
     220                'invalid_nonce',
     221                'Security check failed',
     222                ['status' => 403]
     223            ));
     224            exit;
    107225        }
    108226       
     
    130248     */
    131249    public function get_address() {
    132         // Checks nonce for security
    133         if (!isset($_POST['shipo_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['shipo_nonce'])), 'shipo_nonce_action')) {
    134             wp_send_json_error(array('message' => 'Security failed.'));
     250        // Get parameters
     251        $nonce = '';
     252        if(isset($_POST['shipo_nonce'])) {
     253            $nonce = sanitize_text_field(wp_unslash($_POST['shipo_nonce']));
     254        }
     255       
     256        // Verify nonce
     257        if (!$this->verify_request_nonce($nonce)) {
     258            wp_send_json_error(array(
     259                'invalid_nonce',
     260                'Security check failed',
     261                ['status' => 403]
     262            ));
     263            exit;
    135264        }
    136265       
     
    159288     */
    160289    public function get_map() {
    161         // Checks nonce for security
    162         if (!isset($_POST['shipo_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['shipo_nonce'])), 'shipo_nonce_action')) {
    163             wp_send_json_error(array('message' => 'Security failed.'));
     290        // Get parameters
     291        $nonce = '';
     292        if(isset($_POST['shipo_nonce'])) {
     293            $nonce = sanitize_text_field(wp_unslash($_POST['shipo_nonce']));
     294        }
     295       
     296        // Verify nonce
     297        if (!$this->verify_request_nonce($nonce)) {
     298            wp_send_json_error(array(
     299                'invalid_nonce',
     300                'Security check failed',
     301                ['status' => 403]
     302            ));
     303            exit;
    164304        }
    165305       
     
    211351     */
    212352    public function set_coord() {
    213         // Checks nonce for security
    214         if (!isset($_POST['shipo_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['shipo_nonce'])), 'shipo_nonce_action')) {
    215             wp_send_json_error(array('message' => 'Security failed.'));
     353        // Get parameters
     354        $nonce = '';
     355        if(isset($_POST['shipo_nonce'])) {
     356            $nonce = sanitize_text_field(wp_unslash($_POST['shipo_nonce']));
     357        }
     358       
     359        // Verify nonce
     360        if (!$this->verify_request_nonce($nonce)) {
     361            wp_send_json_error(array(
     362                'invalid_nonce',
     363                'Security check failed',
     364                ['status' => 403]
     365            ));
     366            exit;
    216367        }
    217368       
  • shipo/trunk/includes/class-shipo-assets.php

    r3395057 r3445533  
    11<?php
     2
     3if ( ! defined( 'ABSPATH' ) ) exit;
     4
    25/**
    36 * Manages CSS and JS
     
    1114        add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_assets'));
    1215        add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_assets'));
     16    }
     17
     18    /**
     19     * Enhanced nonce generation with user info
     20     */
     21    public function get_nonce_data() {
     22        $user_id = get_current_user_id();
     23        $is_logged_in = is_user_logged_in();
     24       
     25        // Generate nonce
     26        $nonce = wp_create_nonce('shipo_nonce_action');
     27       
     28        return [
     29            'nonce' => $nonce,
     30            'nonce_name' => 'shipo_nonce',
     31            'user_id' => $user_id,
     32            'is_logged_in' => $is_logged_in ? 'yes' : 'no'
     33        ];
    1334    }
    1435   
     
    2748            wp_enqueue_script('shipo-map-script', SHIPO_PLUGIN_URL . 'assets/js/map.js', array('jquery'), SHIPO_PLUGIN_VERSION, true);
    2849            wp_enqueue_script('shipo-checkout-script', SHIPO_PLUGIN_URL . 'assets/js/checkout.js', array('jquery'), SHIPO_PLUGIN_VERSION, true);
     50           
     51            // Get standardized nonce data
     52            $nonce_data = $this->get_nonce_data();
     53           
    2954            wp_localize_script('shipo-checkout-script', 'shipoAjax', array(
    3055                'ajaxurl' => admin_url('admin-ajax.php'),
    31                 'nonce' => wp_create_nonce('shipo_nonce_action')
     56                'resturl' => rest_url('shipo/v1'),
     57                'nonce' => $nonce_data['nonce'],
     58                'nonce_name' => $nonce_data['nonce_name'],
     59                'user_id' => get_current_user_id(),
     60                'is_logged_in' => is_user_logged_in() ? 'yes' : 'no'
    3261            ));
    3362        }
  • shipo/trunk/includes/class-shipo-checkout.php

    r3415749 r3445533  
    11<?php
     2
     3if ( ! defined( 'ABSPATH' ) ) exit;
     4
    25/**
    36 * Handles checkout modifications
  • shipo/trunk/includes/class-shipo-core.php

    r3395057 r3445533  
    11<?php
     2
     3if ( ! defined( 'ABSPATH' ) ) exit;
     4
    25/**
    36 * Main class that initializes the plugin
  • shipo/trunk/shipo.php

    r3440612 r3445533  
    33 * Plugin Name: Shipo
    44 * Description: Shipo connects your webshop with top couriers instantly, no contract. Ship to address or locker, pay only when parcels are delivered.
    5  * Version: 1.5
     5 * Version: 1.6
    66 * Author: Shipo
    77 * Author URI: https://shipo.ro
     
    1616
    1717// Define plugin constants
    18 define('SHIPO_PLUGIN_VERSION', '1.5.0');
     18define('SHIPO_PLUGIN_VERSION', '1.6.0');
    1919define('SHIPO_PLUGIN_DIR', plugin_dir_path(__FILE__));
    2020define('SHIPO_PLUGIN_URL', plugin_dir_url(__FILE__));
Note: See TracChangeset for help on using the changeset viewer.