Skip to content

Commit

Permalink
merge new version 2.5.788
Browse files Browse the repository at this point in the history
  • Loading branch information
Povilas Kačinskas committed Dec 17, 2019
1 parent 5d7f471 commit bb0aaf4
Show file tree
Hide file tree
Showing 22 changed files with 319 additions and 141 deletions.
2 changes: 1 addition & 1 deletion Trustpilot/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
define('TRUSTPILOT_GENERAL_CONFIGURATION', 'general');
define('TRUSTPILOT_TRUSTBOX_CONFIGURATION', 'trustbox');
define('TRUSTPILOT_INTEGRATION_KEY', 'key');
define('TRUSTPILOT_PLUGIN_VERSION', '2.5.763');
define('TRUSTPILOT_PLUGIN_VERSION', '2.5.788');
define('TRUSTPILOT_SCRIPT', 'TrustpilotScriptUrl');
define('TRUSTPILOT_INTEGRATION_APP', 'IntegrationAppUrl');
define('TRUSTPILOT_WIDGET_SCRIPT', 'WidgetScriptUrl');
Expand Down
70 changes: 42 additions & 28 deletions Trustpilot/helper.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

use Trustpilot\Review\TrustpilotHttpClient;
use Trustpilot\Review\TrustpilotLogger;

function trustpilot_get_default_settings() {
return '{"general":{"key":"","invitationTrigger":"orderConfirmed"},"trustbox":{"trustboxes":[]},
return '{"general":{"key":"","invitationTrigger":"orderConfirmed", "mappedInvitationTrigger":[]},"trustbox":{"trustboxes":[]},
"skuSelector": "none", "mpnSelector": "none", "gtinSelector": "none"}';
}

Expand Down Expand Up @@ -68,13 +71,13 @@ function trustpilot_get_page_url($page) {
return str_replace(['http:', 'https:'],'', $url);
} catch (\Throwable $e) {
$message = 'Unable to find URL for a page ' . $page;
Logger::error($e, $message, array(
TrustpilotLogger::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(
TrustpilotLogger::error($e, $message, array(
'page' => $page,
));
return str_replace(['http:', 'https:'],'', get_home_url());
Expand All @@ -85,7 +88,7 @@ function trustpilot_get_landing_url() {
return get_home_url();
}

function trustpilot_get_category_url() {
function trustpilot_get_first_category() {
$category_args = array(
'taxonomy' => 'product_cat',
'childless' => true,
Expand All @@ -96,8 +99,16 @@ function trustpilot_get_category_url() {
);
$categories = (array) get_categories($category_args);
if (is_array($categories) && !empty($categories)) {
$firstCategory = (object) array_values($categories)[0];
return get_term_link($firstCategory->term_id, 'product_cat');
return (object)array_values($categories)[0];
} else {
return false;
}
}

function trustpilot_get_category_url() {
$category = trustpilot_get_first_category();
if ($category) {
return get_term_link($category->term_id, 'product_cat');
} else {
return get_permalink(trustpilot_get_page_id('shop'));
}
Expand Down Expand Up @@ -130,7 +141,7 @@ function trustpilot_get_first_product() {

$posts = get_posts($product_args);

if (!empty($posts)) {
if (!empty($posts) && function_exists('wc_get_product')) {
return wc_get_product($posts[0]);
}
else {
Expand Down Expand Up @@ -161,44 +172,47 @@ 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 ( is_plugin_active('woocommerce/woocommerce.php') ) {
$product = trustpilot_get_first_product();
array_push($skus, TRUSTPILOT_PRODUCT_ID_PREFIX . trustpilot_get_inventory_attribute('id', $product));

if (!empty($product)) {
array_push($skus, trustpilot_get_inventory_attribute('sku', $product));
if (!empty($product)) {
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)) {
if (method_exists($product, 'get_name')) {
return $product->get_name();
} else {
return $product->get_title();
if ( is_plugin_active('woocommerce/woocommerce.php') ) {
$product = trustpilot_get_first_product();
if (!empty($product)) {
if (method_exists($product, 'get_name')) {
return $product->get_name();
} else {
return $product->get_title();
}
}
} else {
return '';
}
return '';
}

/**
* WooCommerce get version number
*/
function trustpilot_get_woo_version_number() {
if ( ! function_exists( 'get_plugins' ) ) {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}

$plugin_folder = get_plugins( '/' . 'woocommerce' );
$plugin_file = 'woocommerce.php';
if ( is_plugin_active('woocommerce/woocommerce.php') ) {
$plugin_folder = get_plugins( '/' . 'woocommerce' );
$plugin_file = 'woocommerce.php';

if ( isset( $plugin_folder[$plugin_file]['Version'] ) ) {
return $plugin_folder[$plugin_file]['Version'];
if ( isset( $plugin_folder[$plugin_file]['Version'] ) ) {
return $plugin_folder[$plugin_file]['Version'];
} else {
return NULL;
}
} else {
return NULL;
return get_bloginfo('version');
}
}

Expand Down
55 changes: 35 additions & 20 deletions Trustpilot/review/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @link https://trustpilot.com
* @copyright 2018 Trustpilot
*/

namespace Trustpilot\Review;
/**
* @subpackage Admin
Expand Down Expand Up @@ -61,13 +62,10 @@ private function do_hooks() {
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_action( 'wp_ajax_get_category_product_info', array( $this, 'trustpilot_get_category_product_info'));

// 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');
}

public function trustpilot_save_changes() {
Expand Down Expand Up @@ -151,6 +149,16 @@ public function trustpilot_get_signup_data() {
die();
}

public function trustpilot_get_category_product_info() {
$trustbox = TrustBox::get_instance();
$products = Products::get_instance();
$category = trustpilot_get_first_category();
$categoryProducts = $category ? $products->trustpilot_get_products(16, 1, $category) : array();
$results = base64_encode(json_encode($trustbox->get_category_product_info($categoryProducts)));
echo $results;
die();
}

private function trustpilot_get_past_orders_info() {
$orders = PastOrders::get_instance();
$info = $orders->get_past_orders_info();
Expand All @@ -173,9 +181,9 @@ private function trustpilot_resync_failed_orders() {
*/
public function enqueue_admin_styles($hook) {
if ($hook == 'toplevel_page_woocommerce-trustpilot-settings-page') {
wp_enqueue_style( 'trustpilotSettingsStylesheet', plugins_url('/assets/css/trustpilot.css', __FILE__));
wp_enqueue_style( 'trustpilotSettingsStylesheet', plugins_url('/assets/css/trustpilot.min.css', __FILE__));
}
wp_enqueue_style('trustpilotSideLogoStylesheet', plugins_url('/assets/css/trustpilot.css', __FILE__));
wp_enqueue_style('trustpilotSideLogoStylesheet', plugins_url('/assets/css/trustpilot.min.css', __FILE__));
}

/**
Expand All @@ -185,7 +193,7 @@ public function enqueue_admin_scripts($hook) {
if ( 'toplevel_page_woocommerce-trustpilot-settings-page' != $hook ) {
return;
}
wp_enqueue_script( 'boot_js', plugins_url('/assets/js/integrationScript.js', __FILE__ ));
wp_enqueue_script( 'boot_js', plugins_url('/assets/js/integrationScript.min.js', __FILE__ ));
wp_localize_script('boot_js', 'trustpilot_integration_settings', array(
'TRUSTPILOT_INTEGRATION_APP_URL' => $this->get_integration_app_url(),
));
Expand Down Expand Up @@ -220,16 +228,18 @@ public function get_product_identification_options() {
$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);
if (is_plugin_active('woocommerce/woocommerce.php')) {
$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);
foreach ($dynamicFields as $field) {
if (stripos($attr, $field) !== false) {
array_push($fields, $attr);
}
}
}
}
Expand Down Expand Up @@ -261,8 +271,10 @@ public function get_business_information() {
private function load_iframe() {
$pageUrls = new \stdClass();
$pageUrls->landing = trustpilot_get_page_url('landing');
$pageUrls->category = trustpilot_get_page_url('category');
$pageUrls->product = trustpilot_get_page_url('product');
if (is_plugin_active('woocommerce/woocommerce.php')) {
$pageUrls->category = trustpilot_get_page_url('category');
$pageUrls->product = trustpilot_get_page_url('product');
}
$urls = trustpilot_get_field('trustpilot_page_urls');
$customTrustBoxes = json_encode(trustpilot_get_field('trustpilot_custom_TrustBoxes'));
$pageUrls = (object) array_merge((array) $urls, (array) $pageUrls);
Expand All @@ -277,6 +289,7 @@ private function load_iframe() {
$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)));
$mode = is_plugin_active('woocommerce/woocommerce.php') ? '' : 'data-mode=\'trustbox-only\'';
return "
<script type='text/javascript'>
function onTrustpilotIframeLoad() {
Expand Down Expand Up @@ -311,6 +324,7 @@ function onTrustpilotIframeLoad() {
data-is-from-marketplace='" . TRUSTPILOT_IS_FROM_MARKETPLACE . "'
data-configuration-scope-tree='" . $configuration_scope_tree . "'
data-plugin-status='" . $pluginStatus . "'
" . $mode . "
onload='onTrustpilotIframeLoad();'>
</iframe>
<div id='trustpilot-trustbox-preview'
Expand All @@ -321,6 +335,7 @@ function onTrustpilotIframeLoad() {
data-src='" . $startingUrl . "'
data-name='" . $name . "'
data-sku='" . $sku . "'
" . $mode . "
data-source='WooCommerce'>
</div>
<script src='" . TRUSTPILOT_TRUSTBOX_PREVIEW_URL . "' id='TrustBoxPreviewComponent'></script>
Expand Down Expand Up @@ -373,12 +388,12 @@ private function get_integration_app_url() {
$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(
TrustpilotLogger::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(
TrustpilotLogger::error($e, $message, array(
'protocol' => $protocol,
));
}
Expand Down
Loading

0 comments on commit bb0aaf4

Please sign in to comment.