File: /home/beestg/public_html/wp-content/plugins/gogetters_dropshipping/gogetters_dropshipping_admin.php
<?php
// Check if parent class exists
if (!class_exists('GogettersDropshipping')) {
require_once(__DIR__ . '/gogetters_dropshipping.php');
}
class GogettersDropshippingAdmin extends GogettersDropshipping
{
function __construct()
{
// Run parent construct
parent::__construct();
// Actions WP admin
add_action('admin_menu', [$this, 'gogetters_dropshipping_menu']);
$page = isset($_GET[ "page" ]) ? sanitize_text_field($_GET[ "page" ]) : null;
if (stristr($page, 'gogetters')) {
add_filter('admin_footer_text', [$this, 'admin_footer_text'], 100000, 1);
}
}
function gogetters_dropshipping_menu()
{
// Check if the current user can manage woocommerce
if (!current_user_can('manage_woocommerce')) {
return;
}
$api_key_id = get_option('gogetters_eudropshipping_api_key_id');
$api_shared_secret = get_option('gogetters_eudropshipping_shared_secret');
if (!empty($api_key_id) && !empty($api_shared_secret)) {
add_menu_page(
__('GOGETTERS.'),
__('GOGETTERS.'),
'manage_woocommerce',
'gogetters-dropshipping',
[$this, 'gogetters_dropshipping_dashboard'],
'dashicons-products',
10
);
add_submenu_page(
'gogetters-dropshipping',
__('Import list'),
__('Import list'),
'manage_woocommerce',
'gogetters-dropshipping-import-list',
[$this, 'gogetters_dropshipping_import_list']
);
add_submenu_page(
'gogetters-dropshipping',
__('Browse suppliers'),
__('Browse suppliers'),
'manage_woocommerce',
'gogetters-dropshipping-browse-suppliers',
[$this, 'gogetters_dropshipping_browse_suppliers']
);
add_submenu_page(
'gogetters-dropshipping',
__('Search products'),
__('Search products'),
'manage_woocommerce',
'gogetters-dropshipping-search-products',
[$this, 'gogetters_dropshipping_search_products']
);
add_submenu_page(
'gogetters-dropshipping',
__('Orders'),
__('Orders'),
'manage_woocommerce',
'gogetters-dropshipping-orders',
[$this, 'gogetters_dropshipping_orders']
);
} else {
add_menu_page(
__('GOGETTERS.'),
__('GOGETTERS.'),
'manage_woocommerce',
'gogetters',
[$this, 'gogetters_main_menu'],
'dashicons-products',
10
);
}
add_submenu_page(
'gogetters-dropshipping',
__('Settings'),
__('Settings'),
'manage_woocommerce',
'gogetters-dropshipping-settings',
[$this, 'gogetters_dropshipping_settings']
);
add_submenu_page(
null,
__('Setup'),
__('Setup'),
'manage_woocommerce',
'gogetters-dropshipping-setup',
[$this, 'gogetters_dropshipping_setup']
);
add_submenu_page(
null,
__('Reset'),
__('Reset'),
'manage_woocommerce',
'gogetters-dropshipping-reset',
[$this, 'gogetters_dropshipping_reset']
);
}
function gogetters_main_menu()
{
// Check if the current user can manage woocommerce
if (!current_user_can('manage_woocommerce')) {
return;
}
// Output header
echo $this->_gogetters_header();
// Output about us page
echo "<h1>" . __('GOGETTERS.') . "</h1>\n";
echo "<p>" . __('To setup this plugin, please') . ' <a href="' . get_admin_url() . 'admin.php?page=gogetters-dropshipping-settings">' . __('Connect to GOGETTERS.') . '</a>' . "</p>\n";
echo "<p>" . __('For more information, please visit') . ' <a href="https://gogetters.nl/" target="_blank">GOGETTERS.</a>' . "</p>\n";
}
function gogetters_dropshipping_dashboard()
{
// Check if the current user can manage woocommerce
if (!current_user_can('manage_woocommerce')) {
return;
}
// Output iframe
echo $this->_output_iframe('/woocommerce/');
}
function gogetters_dropshipping_import_list()
{
// Check if the current user can manage woocommerce
if (!current_user_can('manage_woocommerce')) {
return;
}
// Output iframe
echo $this->_output_iframe('/product/list');
}
function gogetters_dropshipping_browse_suppliers()
{
// Check if the current user can manage woocommerce
if (!current_user_can('manage_woocommerce')) {
return;
}
// Output iframe
echo $this->_output_iframe('/supplier');
}
function gogetters_dropshipping_search_products()
{
// Check if the current user can manage woocommerce
if (!current_user_can('manage_woocommerce')) {
return;
}
// Output iframe
echo $this->_output_iframe('/product/search');
}
function gogetters_dropshipping_orders()
{
// Check if the current user can manage woocommerce
if (!current_user_can('manage_woocommerce')) {
return;
}
// Output iframe
echo $this->_output_iframe('/order');
}
function gogetters_dropshipping_settings()
{
// Globals
global $wpdb;
// Output header
echo $this->_gogetters_header();
if (!$this->_check_requirements()) {
return;
}
?>
<div>
<h1><?php
echo __('Settings') ?></h1>
<?php
// Shared secret set?
$shared_secret = get_option('gogetters_eudropshipping_shared_secret');
// No keys?
if (empty($shared_secret)) {
?>
<p style="font-size: 1.2rem;">
<b><?php
echo __('Click \'Connect\' to create an API key and connect to GOGETTERS.') ?></b>
</p>
<p style="font-size: 1.1rem;">
<a href="<?php
echo get_admin_url() ?>admin.php?page=gogetters-dropshipping-setup"><?php
echo __('Connect') ?></a>
</p>
<?php
} else {
?>
<p style="font-size: 1.2rem;">
<b><?php
echo __('You are connected') ?></b>
</p>
<?php
}
?>
</div>
<?php
}
function gogetters_dropshipping_setup()
{
// Globals
global $wpdb;
// Header
echo $this->_gogetters_header();
// Prepare query
$query = $wpdb->prepare(
"SELECT description, consumer_key, consumer_secret FROM {$wpdb->prefix}woocommerce_api_keys WHERE permissions='read_write' AND description LIKE %s",
"GOGETTERS***",
);
// Check for existing key
$existingKey = $wpdb->get_row(
str_replace('***', '%', $query)
);
// No key found?
if ($existingKey == null) {
// Set random key and secret
$ck = 'ck_' . bin2hex(openssl_random_pseudo_bytes(20));
$cs = 'cs_' . bin2hex(openssl_random_pseudo_bytes(20));
// Create key in database
$wpdb->insert(
$wpdb->prefix . "woocommerce_api_keys",
[
"user_id" => get_current_user_id(),
"description" => "GOGETTERS. Dropshipping",
"permissions" => "read_write",
"consumer_key" => wc_api_hash($ck),
"consumer_secret" => $cs,
"truncated_key" => substr($cs, -7),
]
);
// Connect to GOGETTERS.
$connectionResult = $this->_connect_to_gogetters($ck, $cs);
if (!$connectionResult) {
?>
<p style="font-size: 1.2rem; color: red;">
<b><?php
echo __('An error occured') ?></b>
</p>
<?php
} else {
?>
<p style="font-size: 1.2rem;">
<b><?php
echo __('Connection successful') ?></b>
</p>
<?php
}
} else {
?>
<p style="font-size: 1.2rem; color: red;">
<b><?php
echo __('Your plugin already seems to be set up. If you need to reset your plugin, please contact GOGETTERS. support') ?></b>
</p>
<?php
}
?>
<p style="font-size: 1.1rem;">
<a href="<?php
echo get_admin_url() ?>admin.php?page=gogetters-dropshipping-settings"><?php
echo __('Click here to continue') ?></a>
</p>
<?php
}
public function gogetters_dropshipping_reset()
{
// Globals
global $wpdb;
// Check if the current user can manage woocommerce
if (!current_user_can('manage_woocommerce')) {
return;
}
// Delete option values
delete_option('gogetters_eudropshipping_api_key_id');
delete_option('gogetters_eudropshipping_shared_secret');
delete_option('gogetters_eudropshipping_shop_id');
// Delete key
$query = $wpdb->prepare(
"DELETE FROM {$wpdb->prefix}woocommerce_api_keys WHERE permissions='read_write' AND description LIKE %s",
"GOGETTERS***",
);
$wpdb->query(
str_replace('***', '%', $query)
);
// Delete webhooks
$query = $wpdb->prepare(
"DELETE FROM {$wpdb->prefix}wc_webhooks WHERE name=%s",
"GOGETTERS. Auto Order",
);
$wpdb->query(
$query
);
// Output header
echo $this->_gogetters_header();
?>
<p style="font-size: 1.2rem;">
<b><?php
echo __('Reset complete') ?></b>
</p>
<p style="font-size: 1.1rem;">
<a href="<?php
echo get_admin_url() ?>admin.php?page=gogetters-dropshipping-settings"><?php
echo __('Click here to continue') ?></a>
</p>
<?php
}
private function _gogetters_header()
{
$header = "<div class=\"gogetters_wrapper\">\n";
$header .= " <div class=\"gogetters_header\">\n";
$header .= " <img src=\"[[PLUGIN_DIR]]/img/logo.png\" class=\"gogetters_logo\" />\n";
$header .= " </div>\n";
$header .= "</div>\n";
// Replaces
$header = str_replace('[[PLUGIN_DIR]]', plugin_dir_url(__DIR__) . plugin_basename(__DIR__), $header);
// Return CSS and header
return $this->_gogetters_css() . "\n\n" . $header;
}
private function _gogetters_css()
{
$css = <<<CSS
<style>
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;700&display=swap');
#wpbody-content {
font-family: Montserrat;
font-weight: 400;
}
.gogetters_wrapper {
margin-left: -20px;
margin-right: 30px;
}
.gogetters_header {
width: 100%;
min-height: 61px;
max-height: 61px;
padding: 15px 20px;
background-color: #FEBE14;
}
.gogetters_logo {
max-height: 61px !important;
}
</style>
CSS;
return $css;
}
public function admin_footer_text($current)
{
return sprintf(
__('Thanks for using <a href="https://gogetters.nl/?utm_source=woocommerce&utm_campaign=module" target="_blank">GOGETTERS.</a>! This is the GOGETTERS. EU Dropshipping module version %1$s'),
$this->_version
);
}
private function _connect_to_gogetters($ck, $cs, $key_ID = false)
{
// Use WP DB
global $wpdb;
// Prepare query
if ($key_ID) {
$query = $wpdb->prepare(
"SELECT key_id, consumer_secret FROM {$wpdb->prefix}woocommerce_api_keys WHERE permissions='read_write' AND key_id=%d AND consumer_secret=%s",
$key_ID,
$cs
);
} else {
$query = $wpdb->prepare(
"SELECT key_id, consumer_secret FROM {$wpdb->prefix}woocommerce_api_keys WHERE permissions='read_write' AND description LIKE %s AND consumer_secret=%s",
"GOGETTERS***",
$cs
);
}
// Check for existing key
$existingKey = $wpdb->get_row(
str_replace('***', '%', $query)
);
// No key? Nothing is impossible
if ($existingKey == null) {
update_option('gogetters_eudropshipping_error', __('Could not create keys'));
return false;
}
// Obtain a shared secret
$post_data = [
'shop' => get_site_url(),
'consumer_key' => esc_attr($ck),
'consumer_secret' => $existingKey->consumer_secret,
'store_address' => get_option('woocommerce_store_address'),
'store_address2' => get_option('woocommerce_store_address2'),
'store_postalcode' => get_option('woocommerce_store_postcode'),
'store_city' => get_option('woocommerce_store_city'),
'store_countrycode' => get_option('woocommerce_default_country'),
];
// Send request to webhook with service key to obtain token
$args = [
'body' => $post_data,
'sslverify' => false,
'timeout' => 30,
];
$result = wp_remote_post($this->_webhook_install_url, $args);
// Store the access token
$result = json_decode($result[ 'body' ] ?? '');
if (isset($result->shared_secret)) {
// Set option
update_option('gogetters_eudropshipping_api_key_id', $existingKey->key_id);
update_option('gogetters_eudropshipping_shared_secret', $result->shared_secret);
update_option('gogetters_eudropshipping_shop_id', $result->shop_id ?? 0);
update_option('gogetters_eudropshipping_error', '');
// Create webhook
$webhook = new WC_Webhook();
$webhook->set_name('GOGETTERS. Auto Order');
$webhook->set_user_id(get_current_user_id());
$webhook->set_topic('order.updated');
$webhook->set_secret(get_option('gogetters_eudropshipping_shared_secret')); // Secret to validate webhook when received.
$webhook->set_delivery_url('https://dropshipping.gogettersapp.com/webhook/woocommerce/order/' . (int)get_option('gogetters_eudropshipping_shop_id'));
$webhook->set_status('active'); // Webhook status.
$webhook->save();
// Return
return true;
} elseif (isset($result->error)) {
// Set option
update_option('gogetters_eudropshipping_api_key_id', null);
update_option('gogetters_eudropshipping_error', $result->error);
// Clean up - key
$wpdb->delete($wpdb->prefix . "woocommerce_api_keys", ['key_id' => $existingKey->key_id]);
// Return
return false;
}
}
private function _check_requirements()
{
// Check for WooCommerce
if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
?>
<p style="font-size: 1.2rem; color: red;">
<b><?php
echo __('Please install WooCommerce') ?></b>
</p>
<?php
// Return
return false;
}
// Check if the current user can manage woocommerce
if (!current_user_can('manage_woocommerce')) {
?>
<p style="font-size: 1.2rem; color: red;">
<b><?php
echo __('You don\'t have enough rights') ?></b>
</p>
<?php
// Return
return false;
}
// Check for SSL - no need to return false here, we can continue
if (!is_ssl()) {
?>
<p style="font-size: 1.2rem; color: red;">
<b><?php
echo __('It seems you are not using SSL. This makes your site insecure and vulnerable.') ?></b>
</p>
<?php
}
// Check for rewrites
if (empty(get_option('permalink_structure'))) {
?>
<p style="font-size: 1.2rem; color: red;">
<b><?php
echo __('Permalinks need to be enabled for this plugin to work.') ?></b><br/>
<a href="<?php
echo get_admin_url() ?>options-permalink.php"><?php
echo __('Click here to enable permalinks') ?></a>
</p>
<?php
// Return
return false;
}
// Check for error messages
$error = get_option('gogetters_eudropshipping_error');
if (!empty($error)) {
?>
<p style="font-size: 1.2rem; color: red;">
<b><?php
echo __('An error occured: ') . $error ?></b><br/>
<b><?php
echo __('Please contact GOGETTERS. support.') ?></b><br/>
</p>
<?php
}
// Return true by default
return true;
}
private function _calculate_hmac($shop, $shared_secret, $timestamp, $url = '')
{
// Create querystring
$querystring = [
'shop' => $shop,
'timestamp' => $timestamp,
];
// URL set?
if (!empty($url) && $url != '/woocommerce/') {
$querystring[ 'url' ] = $url;
}
// Return calculated hmac
return hash_hmac('sha256', http_build_query($querystring), $shared_secret);
}
private function _output_iframe($url)
{
// Set
$shop = get_site_url();
$shared_secret = get_option('gogetters_eudropshipping_shared_secret');
$timestamp = time();
// Return
if ($url == '/woocommerce/') {
return "<iframe src='" . $this->_portal_url . $url . '?hmac=' . $this->_calculate_hmac($shop, $shared_secret, $timestamp) . '&shop=' . urlencode($shop) . '×tamp=' . $timestamp . "' style='width: 100%; height: 100%; min-height: 100vh;'></iframe>\n";
} else {
return "<iframe src='" . $this->_portal_url . '/woocommerce/?hmac=' . $this->_calculate_hmac($shop, $shared_secret, $timestamp, $url) . '&shop=' . urlencode($shop) . '×tamp=' . $timestamp . "&url=" . $url . "' style='width: 100%; height: 100%; min-height: 100vh;'></iframe>\n";
}
}
}