Skip to content

Commit

Permalink
Disable plugin functionality, bug fixes, past orders improvement, tru…
Browse files Browse the repository at this point in the history
…stboxes imporvement, improved logging
  • Loading branch information
vilgro committed Oct 15, 2019
1 parent d762376 commit 5d7f471
Show file tree
Hide file tree
Showing 16 changed files with 842 additions and 196 deletions.
4 changes: 3 additions & 1 deletion Trustpilot/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
define('TRUSTPILOT_ORDER_DATA', 'OrderData');
define('TRUSTPILOT_PAST_ORDERS_FIELD', 'trustpilot_past_orders');
define('TRUSTPILOT_FAILED_ORDERS_FIELD', 'trustpilot_failed_orders');
define('TRUSTPILOT_PLUGIN_STATUS', 'trustpilot_plugin_status');
define('TRUSTPILOT_GENERAL_CONFIGURATION', 'general');
define('TRUSTPILOT_TRUSTBOX_CONFIGURATION', 'trustbox');
define('TRUSTPILOT_INTEGRATION_KEY', 'key');
define('TRUSTPILOT_PLUGIN_VERSION', '2.5.645');
define('TRUSTPILOT_PLUGIN_VERSION', '2.5.763');
define('TRUSTPILOT_SCRIPT', 'TrustpilotScriptUrl');
define('TRUSTPILOT_INTEGRATION_APP', 'IntegrationAppUrl');
define('TRUSTPILOT_WIDGET_SCRIPT', 'WidgetScriptUrl');
Expand All @@ -24,3 +25,4 @@
define('TRUSTPILOT_WP_PREVIEW_CSS_URL', '//ecommplugins-scripts.trustpilot.com/v2.1/css/preview_wp.css');
define('TRUSTPILOT_IS_FROM_MARKETPLACE', 'false');
define('TRUSTPILOT_TRUSTBOX_PREVIEW_URL', '//ecommplugins-trustboxpreview.trustpilot.com/v1.0/trustboxpreview.js');
define('TRUSTPILOT_PRODUCT_ID_PREFIX', 'TRUSTPILOT_SKU_VALUE_');
68 changes: 36 additions & 32 deletions Trustpilot/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ function trustpilot_get_default_failed_orders() {
return '{}';
}

function trustpilot_get_default_plugin_status() {
return '{"pluginStatus": 200, "blockedDomains": []}';
}

function trustpilot_get_field($field) {
return json_decode(stripslashes(get_option($field, '{}')));
}
Expand Down Expand Up @@ -59,11 +63,20 @@ function trustpilot_get_page_url($page) {
$url = trustpilot_get_landing_url();
}
if (is_wp_error($url)) {
throw new Exception($url->get_error_message());
throw new \Exception($url->get_error_message());
}
return str_replace(['http:', 'https:'],'', $url);
} catch (Exception $e) {
trustpilot_log_error('Unable to find URL for a page ' . $page . '. Error: ' . $e->getMessage());
} catch (\Throwable $e) {
$message = 'Unable to find URL for a page ' . $page;
Logger::error($e, $message, array(
'page' => $page,
));
return str_replace(['http:', 'https:'],'', get_home_url());
} catch (\Exception $e) {
$message = 'Unable to find URL for a page ' . $page;
Logger::error($e, $message, array(
'page' => $page,
));
return str_replace(['http:', 'https:'],'', get_home_url());
}
}
Expand Down Expand Up @@ -149,17 +162,23 @@ function trustpilot_get_page_id($page) {

function trustpilot_get_product_sku() {
$product = trustpilot_get_first_product();
$skus = array();
array_push($skus, TRUSTPILOT_PRODUCT_ID_PREFIX . trustpilot_get_inventory_attribute('id', $product));

if (!empty($product)) {
return trustpilot_get_inventory_attribute('sku', $product);
} else {
return '';
array_push($skus, trustpilot_get_inventory_attribute('sku', $product));
}
return implode(',', $skus);
}

function trustpilot_get_product_name() {
$product = trustpilot_get_first_product();
if (!empty($product)) {
return $product->get_name();
if (method_exists($product, 'get_name')) {
return $product->get_name();
} else {
return $product->get_title();
}
} else {
return '';
}
Expand Down Expand Up @@ -191,7 +210,7 @@ function trustpilot_get_inventory_attribute_field($attr) {
case 'sku':
$field = trustpilot_get_settings('skuSelector');
// treat 'none' as 'sku'
if ($field == 'none') $field = 'sku';
if ($field == 'none' || $field == '') $field = 'sku';
return $field;
case 'gtin':
return trustpilot_get_settings('gtinSelector');
Expand All @@ -205,35 +224,20 @@ function trustpilot_get_inventory_attribute_field($attr) {
/**
* Get product sku based on product reviews settings
*/
function trustpilot_get_inventory_attribute($attr, $product) {
$attr_field = trustpilot_get_inventory_attribute_field($attr);
function trustpilot_get_inventory_attribute($attr, $product, $useDbAttribute = true) {
$attr_field = $useDbAttribute ? trustpilot_get_inventory_attribute_field($attr) : $attr;
switch ($attr_field) {
case 'sku':
$value = $product->get_sku();
$value = (string) (method_exists($product, 'get_sku') ?
$product->get_sku() : '');
return $value ? $value : '';
case 'id':
return (string)$product->get_id();
case 'upc':
case 'isbn':
case 'brand':
case 'mpn':
case 'gtin':
$value = $product->get_attribute($attr_field);
$value = (string) (method_exists($product, 'get_id') ?
$product->get_id() :
$product->id);
return $value ? $value : '';
default:
return '';
}
}

function trustpilot_log_error($message) {
try {
$logger = wc_get_logger();
$logger->error($message, array('source' => 'trustpilot-reviews'));

$trustpilot_api = new TrustpilotHttpClient(TRUSTPILOT_API_URL);
$data = array('error' => $message);
$trustpilot_api->postLog($data);
} catch (Exception $e) {
return false;
$value = $product->get_attribute($attr_field);
return $value ? $value : '';
}
}
178 changes: 143 additions & 35 deletions Trustpilot/review/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ private function do_hooks() {
add_action( 'wp_ajax_handle_save_changes', array( $this, 'trustpilot_save_changes' ) );
add_action( 'wp_ajax_update_trustpilot_plugin', array( $this, 'wc_update_trustpilot_plugin' ) );
add_action( 'wp_ajax_reload_trustpilot_settings', array( $this, 'wc_reload_trustpilot_settings' ) );
add_action( 'wp_ajax_check_product_skus', array( $this, 'trustpilot_check_product_skus' ) );
add_action( 'wp_ajax_get_signup_data', array( $this, 'trustpilot_get_signup_data' ) );

// Add the options page and menu item.
add_action( 'admin_menu', array( $this, 'trustpilot_menu' ) );
// Include Http client
include(plugin_dir_path( __FILE__ ) . 'api/TrustpilotHttpClient.php');
include(plugin_dir_path( __FILE__ ) . 'api/TrustpilotPluginStatus.php');
include(plugin_dir_path( __FILE__ ) . 'util/Logger.php');
}

Expand Down Expand Up @@ -135,6 +138,19 @@ public function trustpilot_handle_past_orders_callback() {
die();
}

public function trustpilot_check_product_skus() {
$products = Products::get_instance();
$results = $products->trustpilot_check_skus($_POST['skuSelector']);
echo json_encode($results);
die();
}

public function trustpilot_get_signup_data() {
$results = base64_encode(json_encode($this->get_business_information()));
echo $results;
die();
}

private function trustpilot_get_past_orders_info() {
$orders = PastOrders::get_instance();
$info = $orders->get_past_orders_info();
Expand Down Expand Up @@ -201,21 +217,47 @@ public function wc_display_trustpilot_admin_page() {
}

public function get_product_identification_options() {
$fields = ['none', 'sku', 'id'];
$optionalFields = ['upc', 'isbn', 'brand', 'mpn', 'gtin'];
$fields = array('none', 'sku', 'id');
$optionalFields = array('upc', 'isbn', 'brand');
$dynamicFields = array('mpn', 'gtin');
$attrs = array_map(function ($t) { return $t->attribute_name; }, wc_get_attribute_taxonomies());

foreach ($attrs as $attr) {
foreach ($optionalFields as $field) {
if ($attr == $field && !in_array($field, $fields)) {
array_push($fields, $field);
}
}
foreach ($dynamicFields as $field) {
if (stripos($attr, $field) !== false) {
array_push($fields, $attr);
}
}
}

return json_encode($fields);
}

public function format_url($siteUrl) {
$newUrl = (parse_url($siteUrl, PHP_URL_HOST) != '') ? parse_url($siteUrl, PHP_URL_HOST) : $siteUrl;
return preg_replace('/^www./', '', $newUrl);
}

public function get_business_country() {
return \WC_Geolocation::geolocate_ip('', true, true)['country'] ?: WC()->countries->get_base_country();
}

public function get_business_information() {
$owner = is_super_admin() ? wp_get_current_user() : get_user_by('login', get_super_admins()[0]);
$this->get_business_country();
return array(
'website' => $this->format_url(get_site_url()),
'company' => html_entity_decode(get_bloginfo( 'name' ), ENT_QUOTES),
'name' => html_entity_decode($owner ? $owner->first_name . ' ' . $owner->last_name : '', ENT_QUOTES),
'email' => get_bloginfo('admin_email'),
'country' => $this->get_business_country(),
);
}

private function load_iframe() {
$pageUrls = new \stdClass();
$pageUrls->landing = trustpilot_get_page_url('landing');
Expand All @@ -233,48 +275,114 @@ private function load_iframe() {
$version = trustpilot_get_woo_version_number();
$startingUrl = trustpilot_get_page_url('landing');
$productIdentificationOptions = $this->get_product_identification_options();
$configuration_scope_tree = base64_encode(json_encode($this->get_configuration_scope_tree()));
$pluginStatus = base64_encode(json_encode(trustpilot_get_field(TRUSTPILOT_PLUGIN_STATUS)));
return "
<script type='text/javascript'>
function onTrustpilotIframeLoad() {
if (typeof sendSettings === 'function' && typeof sendPastOrdersInfo === 'function') {
sendSettings();
sendPastOrdersInfo();
} else {
window.addEventListener('load', function () {
sendSettings();
sendPastOrdersInfo();
});
}
}
</script>
<div style='display:block;'>
<iframe
style='display: inline-block;'
src='" . $integration_app_url . "'
id='configuration_iframe'
frameborder='0'
scrolling='no'
width='100%'
height='1400px'
data-plugin-version='" . TRUSTPILOT_PLUGIN_VERSION . "'
data-source='WooCommerce'
data-version='WooCommerce-" . $version . "'
data-page-urls='" . $pageUrlsBase64 . "'
data-transfer='" . $integration_app_url . "'
data-past-orders='" . $past_orders_info . "'
data-settings='" . $settings . "'
data-product-identification-options = '" . $productIdentificationOptions . "'
data-is-from-marketplace = '" . TRUSTPILOT_IS_FROM_MARKETPLACE . "'
onload='sendSettings(); sendPastOrdersInfo();'>
</iframe>
<div id='trustpilot-trustbox-preview'
hidden='true'
data-page-urls='" . $pageUrlsBase64 . "'
data-custom-trustboxes='" . $customTrustBoxes . "'
data-settings='" . $settings . "'
data-src='" . $startingUrl . "'
data-name='" . $name . "'
data-sku='" . $sku . "'
data-source='WooCommerce'>
<iframe
style='display: inline-block;'
src='" . $integration_app_url . "'
id='configuration_iframe'
frameborder='0'
scrolling='no'
width='100%'
height='1400px'
data-plugin-version='" . TRUSTPILOT_PLUGIN_VERSION . "'
data-source='WooCommerce'
data-version='WooCommerce-" . $version . "'
data-page-urls='" . $pageUrlsBase64 . "'
data-transfer='" . $integration_app_url . "'
data-past-orders='" . $past_orders_info . "'
data-settings='" . $settings . "'
data-product-identification-options='" . $productIdentificationOptions . "'
data-is-from-marketplace='" . TRUSTPILOT_IS_FROM_MARKETPLACE . "'
data-configuration-scope-tree='" . $configuration_scope_tree . "'
data-plugin-status='" . $pluginStatus . "'
onload='onTrustpilotIframeLoad();'>
</iframe>
<div id='trustpilot-trustbox-preview'
hidden='true'
data-page-urls='" . $pageUrlsBase64 . "'
data-custom-trustboxes='" . $customTrustBoxes . "'
data-settings='" . $settings . "'
data-src='" . $startingUrl . "'
data-name='" . $name . "'
data-sku='" . $sku . "'
data-source='WooCommerce'>
</div>
<script src='" . TRUSTPILOT_TRUSTBOX_PREVIEW_URL . "' id='TrustBoxPreviewComponent'></script>
</div>
<script src='" . TRUSTPILOT_TRUSTBOX_PREVIEW_URL . "' id='TrustBoxPreviewComponent'></script>
</div>
";
}

private function get_configuration_scope_tree() {
if (is_multisite()) { // Multisite
$networks = array();
$sites = array();
$args = array('public' => 1, 'deleted' => 0, 'archived' => 0, 'limit' => 0);
if (function_exists('get_sites')) {
$sites = get_sites($args); // WordPress >= 4.6
} else if (function_exists('wp_get_sites')) {
$sites = wp_get_sites($args); // WordPress < 4.6
}
foreach ($sites as $site) {
array_push($networks, $this->get_network_info($site->blog_id));
}
return $networks;
} else { // Single site
return array(
array(
'ids' => array(1),
'names' => array('store' => get_bloginfo('name')),
'domain' => preg_replace('#^https?://#', '', get_bloginfo('url')),
)
);
}
}

private function get_network_info($network_id) {
$network = get_blog_details($network_id);
return array(
'ids' => array($network->blog_id),
'names' => array('store' => $network->blogname),
'domain' => preg_replace(array('#^https?://#', '#/?$#'), '', $network->domain),
);
}

private function get_protocol() {
return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https:" : "http:";
return ((!empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == '1'))
|| (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443')) ? "https:" : "http:";
}

private function get_integration_app_url() {
return $this->get_protocol() . TRUSTPILOT_INTEGRATION_APP_URL;
$protocol = 'https:';
try {
$protocol = $this->get_protocol();
} catch (\Throwable $e) { // For PHP 7
$message = 'Unable get protocol of the website switching to default: ' . $protocol;
Logger::error($e, $message, array(
'protocol' => $protocol,
));
} catch (\Exception $e) { // For PHP 5
$message = 'Unable get protocol of the website switching to default: ' . $protocol;
Logger::error($e, $message, array(
'protocol' => $protocol,
));
}
return $protocol . TRUSTPILOT_INTEGRATION_APP_URL;
}

private function wc_display_trustpilot_settings() {
Expand Down
Loading

0 comments on commit 5d7f471

Please sign in to comment.