WC_GZD_Trusted_Shops

Trusted Shops implementation.

Defined (1)

The class is defined in the following location(s).

/includes/trusted-shops/class-wc-gzd-trusted-shops.php  
  1. class WC_GZD_Trusted_Shops { 
  2.  
  3. /** 
  4. * TS ID 
  5. * @var mixed 
  6. */ 
  7. public $id; 
  8.  
  9. /** 
  10. * Trusted Shops URL Parameters  
  11. * @var array 
  12. */ 
  13. public $et_params = array(); 
  14.  
  15. /** 
  16. * Trusted Shops Signup URL Parameters 
  17. * @var array 
  18. */ 
  19. public $signup_params = array(); 
  20.  
  21. /** 
  22. * Class Prefix for auto dependency loading 
  23. * @var string 
  24. */ 
  25. public $prefix = ''; 
  26.  
  27. /** 
  28. * Option prefix for DB strings 
  29. * @var string 
  30. */ 
  31. public $option_prefix = ''; 
  32.  
  33. /** 
  34. * Support functionality 
  35. * @var array 
  36. */ 
  37. public $supports = array(); 
  38.  
  39. /** 
  40. * Admin URLs 
  41. * @var array 
  42. */ 
  43. public $urls = array(); 
  44.  
  45. /** 
  46. * WooCommerce Gateway mapping 
  47. * @var array 
  48. */ 
  49. public $gateways = array(); 
  50.  
  51. /** 
  52. * Parent Plugin 
  53. * @var null 
  54. */ 
  55. public $plugin = null; 
  56.  
  57. /** 
  58. * Trusted Shops Version 
  59. * @var string 
  60. */ 
  61. public $version = '1.1.0'; 
  62.  
  63. /** 
  64. * API URL for review collection 
  65. * @var string 
  66. */ 
  67. public $api_url; 
  68.  
  69. /** 
  70. * Sets Trusted Shops payment gateways and load dependencies 
  71. */ 
  72. public function __construct( $plugin, $params = array() ) { 
  73.  
  74. $this->plugin = $plugin; 
  75.  
  76. $args = wp_parse_args( $params, array( 
  77. 'et_params' => array(),  
  78. 'signup_params' => array(),  
  79. 'prefix' => '',  
  80. 'urls' => array(),  
  81. 'supports' => array( 'reminder' ),  
  82. ) ); 
  83.  
  84. foreach ( $args as $arg => $val ) { 
  85. $this->$arg = $val; 
  86.  
  87. $this->option_prefix = strtolower( $this->prefix ); 
  88.  
  89. // Refresh TS ID + API URL 
  90. $this->refresh(); 
  91.  
  92. if ( is_admin() ) 
  93. $this->get_dependency( 'admin' ); 
  94.  
  95. $this->get_dependency( 'schedule' ); 
  96. $this->get_dependency( 'shortcodes' ); 
  97. $this->get_dependency( 'widgets' ); 
  98. $this->get_dependency( 'template_hooks' ); 
  99.  
  100. if ( $this->is_enabled() ) 
  101. add_action( 'wp_enqueue_scripts', array( $this, 'load_frontend_assets' ), 50 ); 
  102.  
  103. add_action( 'init', array( $this, 'setup_payment_options' ) ); 
  104.  
  105.  
  106. public function setup_payment_options() { 
  107. $this->gateways = apply_filters( 'woocommerce_trusted_shops_gateways', array( 
  108. 'prepayment' => _x( 'Prepayment', 'trusted-shops', 'woocommerce-germanized' ),  
  109. 'cash_on_delivery' => _x( 'Cash On Delivery', 'trusted-shops', 'woocommerce-germanized' ),  
  110. 'credit_card' => _x( 'Credit Card', 'trusted-shops', 'woocommerce-germanized' ),  
  111. 'paypal' => _x( 'Paypal', 'trusted-shops', 'woocommerce-germanized' ),  
  112. 'invoice' => _x( 'Invoice', 'trusted-shops', 'woocommerce-germanized' ),  
  113. 'direct_debit' => _x( 'Direct Debit', 'trusted-shops', 'woocommerce-germanized' ),  
  114. 'financing' => _x( 'Financing', 'trusted-shops', 'woocommerce-germanized' ),  
  115. ); 
  116.  
  117. public function load_frontend_assets() { 
  118.  
  119. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; 
  120. $assets_path = $this->plugin->plugin_url() . '/assets/css'; 
  121. $script_prefix = str_replace( '_', '-', $this->option_prefix ); 
  122.  
  123. wp_register_style( 'woocommerce-' . $script_prefix . 'trusted-shops', $assets_path . '/woocommerce-' . $script_prefix . 'trusted-shops' . $suffix . '.css', false, $this->plugin->version ); 
  124. wp_enqueue_style( 'woocommerce-' . $script_prefix . 'trusted-shops' ); 
  125.  
  126.  
  127. public function get_dependency_name( $name ) { 
  128. $classname = 'WC_' . $this->prefix . 'Trusted_Shops_' . ucwords( str_replace( '-', '_', strtolower( $name ) ) ); 
  129. return $classname; 
  130.  
  131. public function get_dependency( $name ) { 
  132. $classname = $this->get_dependency_name( $name ); 
  133. return call_user_func_array( array( $classname, 'instance' ), array( $this ) ); 
  134.  
  135. public function refresh() { 
  136. $this->id = get_option( 'woocommerce_' . $this->option_prefix . 'trusted_shops_id' ); 
  137. $this->api_url = 'http://api.trustedshops.com/rest/public/v2/shops/'. $this->id .'/quality.json'; 
  138.  
  139. /** 
  140. * Get Trusted Shops Options 
  141. * @param string $key 
  142. * @return mixed 
  143. */ 
  144. public function __get( $key ) { 
  145. return get_option( 'woocommerce_' . $this->option_prefix . 'trusted_shops_' . $key ); 
  146.  
  147. /** 
  148. * Checks whether a certain Trusted Shops Option isset 
  149. * @param string $key 
  150. * @return boolean 
  151. */ 
  152. public function __isset( $key ) { 
  153. return ( ! get_option( 'woocommerce_' . $this->option_prefix . 'trusted_shops_' . $key ) ) ? false : true; 
  154.  
  155. /** 
  156. * Checks whether Trusted Shops is enabled 
  157. * @return boolean 
  158. */ 
  159. public function is_enabled() { 
  160. return ( $this->id ) ? true : false; 
  161.  
  162. /** 
  163. * Checks whether Trusted Shops Rich Snippets are enabled 
  164. *  
  165. * @return boolean 
  166. */ 
  167. public function is_rich_snippets_enabled() { 
  168. return ( $this->rich_snippets_enable === 'yes' && $this->is_enabled() ? true : false ); 
  169.  
  170. /** 
  171. * Checks whether review widget is enabled 
  172. *  
  173. * @return boolean 
  174. */ 
  175. public function is_review_widget_enabled() { 
  176. return ( $this->review_widget_enable === 'yes' && $this->is_enabled() ? true : false ); 
  177.  
  178. public function is_review_reminder_enabled() { 
  179. return ( $this->review_reminder_enable === 'yes' && $this->supports( 'reminder' ) && $this->is_enabled() ? true : false ); 
  180.  
  181. public function is_product_reviews_enabled() { 
  182. return ( $this->enable_reviews === 'yes' && $this->is_enabled() ? true : false ); 
  183.  
  184. public function is_product_sticker_enabled() { 
  185. return ( $this->is_product_reviews_enabled() && $this->product_sticker_enable === 'yes' ? true : false ); 
  186.  
  187. public function is_product_widget_enabled() { 
  188. return ( $this->is_product_reviews_enabled() && $this->product_widget_enable === 'yes' ? true : false ); 
  189.  
  190. public function supports( $type ) { 
  191. return ( in_array( $type, $this->supports ) ? true : false ); 
  192.  
  193. /** 
  194. * Gets Trusted Shops payment gateway by woocommerce payment id 
  195. * @param integer $payment_method_id 
  196. * @return string 
  197. */ 
  198. public function get_payment_gateway( $payment_method_id ) { 
  199. return ( $this->{"gateway_$payment_method_id"} ? strtoupper( $this->{"gateway_$payment_method_id"} ) : '' ); 
  200.  
  201. /** 
  202. * Returns the average rating by grabbing the rating from the cache 
  203. * @return array 
  204. */ 
  205. public function get_average_rating() { 
  206. return ( $this->reviews_cache ? $this->reviews_cache : array() ); 
  207.  
  208. /** 
  209. * Returns the certificate link 
  210. * @return string 
  211. */ 
  212. public function get_certificate_link() { 
  213. return 'https://www.trustedshops.com/shop/certificate.php?shop_id=' . $this->id; 
  214.  
  215. /** 
  216. * Returns add new rating link 
  217. *  
  218. * @return string 
  219. */ 
  220. public function get_new_review_link( $email, $order_id ) { 
  221. return 'https://www.trustedshops.de/bewertung/bewerten_' . $this->id . '.html&buyerEmail=' . urlencode( base64_encode( $email ) ) . '&shopOrderID=' . urlencode( base64_encode( $order_id ) ); 
  222.  
  223. /** 
  224. * Returns the rating link 
  225. * @return string 
  226. */ 
  227. public function get_rating_link() { 
  228. return 'https://www.trustedshops.de/bewertung/info_' . $this->id . '.html'; 
  229.  
  230. /** 
  231. * Gets the attachment id of review widget graphic 
  232. *  
  233. * @return mixed 
  234. */ 
  235. public function get_review_widget_attachment() { 
  236. return ( ! $this->review_widget_attachment ? false : $this->review_widget_attachment ); 
  237.  
  238. public function get_template( $name ) { 
  239. $html = ""; 
  240. ob_start(); 
  241. wc_get_template( 'trusted-shops/' . str_replace( '_', '-', $name ) . '-tpl.php' ); 
  242. $html = ob_get_clean(); 
  243. return preg_replace('/^\h*\v+/m', '', strip_tags( $html ) ); 
  244.  
  245. public function get_script( $name, $replace = true, $args = array() ) { 
  246. $script = $this->get_template( $name ); 
  247.  
  248. if ( $this->integration_mode === 'expert' ) 
  249. $script = $this->{$name . "_code"}; 
  250.  
  251. if ( $replace ) { 
  252.  
  253. $args = wp_parse_args( $args, array( 
  254. 'id' => $this->id,  
  255. 'locale' => $this->get_locale(),  
  256. ) ); 
  257.  
  258. foreach ( $args as $key => $arg ) { 
  259. $script = str_replace( '{' . $key . '}', $arg, $script ); 
  260.  
  261.  
  262. return $script; 
  263.  
  264. public function get_product_sticker_code( $replace = true, $args = array() ) { 
  265. if ( $replace ) { 
  266.  
  267. $args = wp_parse_args( $args, array( 
  268. 'border_color' => $this->product_sticker_border_color,  
  269. 'star_color' => $this->product_sticker_star_color,  
  270. 'star_size' => $this->product_sticker_star_size,  
  271. ) ); 
  272.  
  273.  
  274. return $this->get_script( 'product_sticker', $replace, $args ); 
  275.  
  276. public function get_product_widget_code( $replace = true, $args = array() ) { 
  277. if ( $replace ) { 
  278.  
  279. $args = wp_parse_args( $args, array( 
  280. 'element' => '#ts_product_widget',  
  281. 'star_color' => $this->product_widget_star_color,  
  282. 'star_size' => $this->product_widget_star_size,  
  283. 'font_size' => $this->product_widget_font_size,  
  284. ) ); 
  285.  
  286.  
  287. return $this->get_script( 'product_widget', $replace, $args ); 
  288.  
  289. public function get_trustbadge_code( $replace = true, $args = array() ) { 
  290. if ( $replace ) { 
  291.  
  292. $args = wp_parse_args( $args, array( 
  293. 'offset' => $this->trustbadge_y,  
  294. 'variant' => $this->trustbadge_variant === 'standard' ? 'reviews' : 'default',  
  295. 'disable' => $this->trustbadge_variant === 'disable' ? 'true' : 'false',  
  296. ) ); 
  297.  
  298.  
  299. return $this->get_script( 'trustbadge', $replace, $args ); 
  300.  
  301. public function get_locale() { 
  302.  
  303. $supported = array( 
  304. 'de' => 'de_DE',  
  305. 'en' => 'en_GB',  
  306. 'fr' => 'fr_FR',  
  307. 'es' => 'es_ES',  
  308. 'it' => 'it_IT',  
  309. 'nl' => 'nl_NL',  
  310. 'pl' => 'pl_PL',  
  311. ); 
  312.  
  313. $locale = 'en_GB'; 
  314. $base = substr( get_locale(), 0, 2 ); 
  315.  
  316. if ( isset( $supported[ $base ] ) ) 
  317. $locale = $supported[ $base ]; 
  318.  
  319. return $locale; 
  320.  
  321.