예제 #1
0
function wc_proccess_yotpo_register()
{
    $errors = array();
    if ($_POST['yotpo_user_email'] === '') {
        array_push($errors, 'Provide valid email address');
    }
    if (strlen($_POST['yotpo_user_password']) < 6 || strlen($_POST['yotpo_user_password']) > 128) {
        array_push($errors, 'Password must be at least 6 characters');
    }
    if ($_POST['yotpo_user_password'] != $_POST['yotpo_user_confirm_password']) {
        array_push($errors, 'Passwords are not identical');
    }
    if ($_POST['yotpo_user_name'] === '') {
        array_push($errors, 'Name is missing');
    }
    if (count($errors) == 0) {
        $yotpo_api = new Yotpo();
        $shop_url = get_bloginfo('url');
        $user = array('email' => $_POST['yotpo_user_email'], 'display_name' => $_POST['yotpo_user_name'], 'first_name' => '', 'password' => $_POST['yotpo_user_password'], 'last_name' => '', 'website_name' => $shop_url, 'support_url' => $shop_url, 'callback_url' => $shop_url, 'url' => $shop_url);
        try {
            $response = $yotpo_api->create_user($user, true);
            if (!empty($response['status']) && !empty($response['status']['code'])) {
                if ($response['status']['code'] == 200) {
                    $app_key = $response['response']['app_key'];
                    $secret = $response['response']['secret'];
                    $yotpo_api->set_app_key($app_key);
                    $yotpo_api->set_secret($secret);
                    $shop_domain = parse_url($shop_url, PHP_URL_HOST);
                    $account_platform_response = $yotpo_api->create_account_platform(array('shop_domain' => wc_yotpo_get_shop_domain(), 'utoken' => $response['response']['token'], 'platform_type_id' => 12));
                    if (!empty($response['status']) && !empty($response['status']['code']) && $response['status']['code'] == 200) {
                        $current_settings = get_option('yotpo_settings', wc_yotpo_get_degault_settings());
                        $current_settings['app_key'] = $app_key;
                        $current_settings['secret'] = $secret;
                        update_option('yotpo_settings', $current_settings);
                        return true;
                    } elseif ($response['status']['code'] >= 400) {
                        if (!empty($response['status']['message'])) {
                            wc_yotpo_display_message($response['status']['message'], true);
                        }
                    }
                } elseif ($response['status']['code'] >= 400) {
                    if (!empty($response['status']['message'])) {
                        if (is_array($response['status']['message']) && !empty($response['status']['message']['email'])) {
                            if (is_array($response['status']['message']['email'])) {
                                wc_yotpo_display_message($response['status']['message']['email'][0], false);
                            } else {
                                wc_yotpo_display_message($response['status']['message']['email'], false);
                            }
                        } else {
                            wc_yotpo_display_message($response['status']['message'], true);
                        }
                    }
                }
            } else {
                if ($response == 'b2c') {
                    return $response;
                }
            }
        } catch (Exception $e) {
            wc_yotpo_display_message($e->getMessage(), true);
        }
    } else {
        wc_yotpo_display_message($errors, false);
    }
    return false;
}
예제 #2
0
function wc_yotpo_send_past_orders()
{
    $yotpo_settings = get_option('yotpo_settings', wc_yotpo_get_degault_settings());
    if (!empty($yotpo_settings['app_key']) && !empty($yotpo_settings['secret'])) {
        $past_orders = wc_yotpo_get_past_orders();
        $is_success = true;
        if (!is_null($past_orders) && is_array($past_orders)) {
            $yotpo_api = new Yotpo($yotpo_settings['app_key'], $yotpo_settings['secret']);
            $get_oauth_token_response = $yotpo_api->get_oauth_token();
            if (!empty($get_oauth_token_response) && !empty($get_oauth_token_response['access_token'])) {
                foreach ($past_orders as $post_bulk) {
                    if (!is_null($post_bulk)) {
                        $post_bulk['utoken'] = $get_oauth_token_response['access_token'];
                        $response = $yotpo_api->create_purchases($post_bulk);
                        if ($response['code'] != 200 && $is_success) {
                            $is_success = false;
                            $message = !empty($response['status']) && !empty($response['status']['message']) ? $response['status']['message'] : 'Error occurred';
                            wc_yotpo_display_message($message, true);
                        }
                    }
                }
                if ($is_success) {
                    wc_yotpo_display_message('Past orders sent successfully', false);
                    $yotpo_settings['show_submit_past_orders'] = false;
                    update_option('yotpo_settings', $yotpo_settings);
                }
            }
        } else {
            wc_yotpo_display_message('Could not retrieve past orders', true);
        }
    } else {
        wc_yotpo_display_message('You need to set your app key and secret token to post past orders', false);
    }
}