WC_GZD_Ekomi

EKomi Implementation - This Class adds eKomi Rating functionality.

Defined (1)

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

/includes/class-wc-gzd-ekomi.php  
  1. class WC_GZD_Ekomi { 
  2.  
  3. /** 
  4. * API Version 
  5. * @var string 
  6. */ 
  7. public $version; 
  8. /** 
  9. * Shop ID 
  10. * @var integer 
  11. */ 
  12. public $id; 
  13. /** 
  14. * A user is needed to add the eKomi Reviews 
  15. * @var object 
  16. */ 
  17. public $user; 
  18.  
  19. /** 
  20. * Creates a new User if the eKomi user does not already exist. Adds hooks to schedules to manage review updates and Email notifications 
  21. */ 
  22. public function __construct() { 
  23.  
  24. $this->version = 'cust-1.0.0'; 
  25. $this->id = $this->shop_id; 
  26.  
  27. if ( $this->is_enabled() ) 
  28. add_action( 'init', array( $this, 'init' ) ); 
  29.  
  30. // Register sections 
  31. add_filter( 'woocommerce_gzd_settings_sections', array( $this, 'register_section' ), 5 ); 
  32. add_filter( 'woocommerce_gzd_get_settings_ekomi', array( $this, 'get_settings' ) ); 
  33.  
  34. public function init() { 
  35. if ( ! username_exists( 'ekomi' ) ) { 
  36. wp_create_user( __( 'eKomi Customer', 'woocommerce-germanized' ), wp_generate_password(), 'ekomi@loremipsumdolorom.com' ); 
  37. $this->user = get_user_by( 'email', 'ekomi@loremipsumdolorom.com' ); 
  38. wp_update_user( array( 'ID' => $this->user->ID, 'role' => 'customer' ) ); 
  39. // Cronjobs & Hooks 
  40. $this->user = get_user_by( 'email', 'ekomi@loremipsumdolorom.com' ); 
  41. add_action( 'woocommerce_gzd_ekomi', array( $this, 'put_products' ), 5 ); 
  42. add_action( 'woocommerce_gzd_ekomi', array( $this, 'send_mails' ), 10 ); 
  43. add_action( 'woocommerce_gzd_ekomi', array( $this, 'get_reviews' ), 15 ); 
  44. add_action( 'woocommerce_order_status_completed', array( $this, 'put_order' ), 0, 1 ); 
  45. add_action( 'wp_footer', array( $this, 'add_scripts' ), 10 ); 
  46. // add_action( 'admin_init', array( $this, 'get_reviews' ) ); 
  47.  
  48. /** 
  49. * Gets eKomi Options 
  50. * @param string $key 
  51. * @return mixed 
  52. */ 
  53. public function __get( $key ) { 
  54. return get_option( 'woocommerce_gzd_ekomi_' . $key ); 
  55.  
  56. /** 
  57. * Checks whether a certain eKomi Option isset 
  58. * @param string $key 
  59. * @return boolean 
  60. */ 
  61. public function __isset( $key ) { 
  62. return ( ! get_option( 'woocommerce_gzd_ekomi_' . $key ) ) ? false : true; 
  63.  
  64. /** 
  65. * Checks whether eKomi API is enabled 
  66. * @return boolean 
  67. */ 
  68. public function is_enabled() { 
  69. return ( empty( $this->id ) || empty( $this->interface_id ) || empty( $this->interface_password ) || empty( $this->certificate_link ) || empty( $this->partner_id ) ) ? false : true; 
  70.  
  71. /** 
  72. * Transfers all Shop Products including IDs and Titles to eKomi 
  73. */ 
  74. public function put_products() { 
  75. $posts = get_posts( array( 'post_type' => array( 'product' ), 'post_status' => 'publish', 'showposts' => -1 ) ); 
  76. if ( !empty( $posts ) ) { 
  77. foreach ( $posts as $post ) { 
  78. $product = get_product( $post ); 
  79. if ( $product->is_type( 'variable' ) ) { 
  80. $variations = $product->get_available_variations(); 
  81. if ( !empty( $variations ) ) { 
  82. foreach ( $variations as $variation ) { 
  83. $this->put_product( get_product( $variation['variation_id'] ) ); 
  84. } elseif ( $product->is_type( 'simple' ) ) { 
  85. $this->put_product( $product ); 
  86.  
  87. /** 
  88. * Transfers a single Product to eKomi 
  89. * @param object $product 
  90. */ 
  91. public function put_product( $product ) { 
  92. $response = unserialize( file_get_contents( 
  93. 'https://api.ekomi.de/v2/putProduct?auth=' . $this->interface_id . '|' . $this->interface_password . '&version=' . $this->version . '&product_id=' . urlencode( esc_attr( $this->get_product_id( $product ) ) ) . '&product_name=' . urlencode( esc_attr( $this->get_product_name( $product ) ) ) 
  94. ) ); 
  95. return ( $response['done'] == 1 ) ? true : false; 
  96.  
  97. /** 
  98. * Returns the product id. If is variation returns variation id instead. 
  99. *  
  100. * @param object $product  
  101. * @return integer  
  102. */ 
  103. public function get_product_id( $product ) { 
  104. return ( isset( $product->variation_id ) ? $product->variation_id : $product->id ); 
  105.  
  106. /** 
  107. * Gets the Product's name based on it's type 
  108. * @param object $product 
  109. * @return string 
  110. */ 
  111. public function get_product_name( $product ) { 
  112. return isset( $product->variation_id ) ? get_the_title( $product->id ) . ' (' . implode( ', ', $product->get_variation_attributes() ) . ')' : get_the_title( $product->id ); 
  113.  
  114. /** 
  115. * Transfers a single Order to eKomi 
  116. * @param integer $order_id 
  117. * @return boolean 
  118. */ 
  119. public function put_order( $order_id ) { 
  120. $order = wc_get_order( $order_id ); 
  121. if ( ! isset( $order->ekomi_review_link ) ) { 
  122. $items = $order->get_items(); 
  123. $product_ids = array(); 
  124. if ( !empty( $items ) ) { 
  125. foreach ( $items as $item ) { 
  126. $product_ids[] = ( empty( $item[ 'variation_id' ] ) ? $item[ 'product_id' ] : $item[ 'variation_id' ] ); 
  127. $response = unserialize( file_get_contents( 
  128. 'https://api.ekomi.de/v2/putOrder?auth=' . $this->interface_id . '|' . $this->interface_password . '&version=' . $this->version . '&order_id=' . urlencode( esc_attr( $order->id ) ) . '&product_ids=' . urlencode( esc_attr( implode( ', ', $product_ids ) ) ) 
  129. ) ); 
  130. if ( $response['done'] == 1 && isset( $response['link'] ) ) 
  131. update_post_meta( $order->id, '_ekomi_review_link', $response[ 'link' ] ); 
  132. return ( $response['done'] == 1 && isset( $response['link'] ) ) ? true : false; 
  133. return false; 
  134.  
  135. /** 
  136. * Send Customer Email notifications if necessary (loops through orders and checks day difference after completion) 
  137. */ 
  138. public function send_mails() { 
  139. $order_query = new WP_Query( 
  140. array( 'post_type' => 'shop_order', 'post_status' => 'wc-completed', 'meta_query' => 
  141. array( 
  142. array( 
  143. 'key' => '_ekomi_review_link',  
  144. 'compare' => 'EXISTS',  
  145. ),  
  146. array( 
  147. 'key' => '_ekomi_review_mail_sent',  
  148. 'compare' => 'NOT EXISTS',  
  149. ),  
  150. ),  
  151. ); 
  152. while ( $order_query->have_posts() ) { 
  153. $order_query->next_post(); 
  154. $order = wc_get_order( $order_query->post->ID ); 
  155. $diff = WC_germanized()->get_date_diff( $order->completed_date, date( 'Y-m-d H:i:s' ) ); 
  156. if ( $diff[ 'd' ] >= (int) get_option( 'woocommerce_gzd_ekomi_day_diff' ) ) { 
  157.  
  158. if ( $mail = WC_germanized()->emails->get_email_instance_by_id( 'customer_ekomi' ) ) { 
  159. $mail->trigger( $order->id ); 
  160. update_post_meta( $order->id, '_ekomi_review_mail_sent', 1 ); 
  161. update_post_meta( $order->id, '_ekomi_review_link', '' ); 
  162.  
  163.  
  164. /** 
  165. * Grabs the reviews from eKomi and saves them as review within the Shop 
  166. * @return boolean 
  167. */ 
  168. public function get_reviews() { 
  169.  
  170. $response = file_get_contents( 
  171. 'https://api.ekomi.de/get_productfeedback.php?interface_id=' . $this->interface_id . '&interface_pw=' . $this->interface_password . '&version=' . $this->version . '&type=csv&product=all' 
  172. ); 
  173. if ( ! empty( $response ) ) { 
  174. $reviews = explode( PHP_EOL, $response ); 
  175.  
  176. if ( ! empty( $reviews ) ) { 
  177.  
  178. foreach ( $reviews as $review ) { 
  179. $review = str_getcsv( $review ); 
  180. if ( ! empty( $review[0] ) ) { 
  181.  
  182. if ( ! $this->review_exists( $review[1] ) && $this->is_product( (int) esc_attr( $review[2] ) ) ) { 
  183.  
  184. $product = wc_get_product( (int) esc_attr( $review[2] ) ); 
  185.  
  186. $data = array( 
  187. 'comment_post_ID' => $product->id,  
  188. 'comment_author' => $this->user->user_login,  
  189. 'comment_author_email' => $this->user->user_email,  
  190. 'comment_content' => preg_replace( '/\v+|\\\[rn]/', '<br/>', esc_attr( $review[4] ) ),  
  191. 'comment_date' => date( 'Y-m-d H:i:s', (int) esc_attr( $review[0] ) ),  
  192. 'comment_approved' => 1,  
  193. ); 
  194.  
  195. $comment_id = wp_insert_comment( $data ); 
  196.  
  197. if ( $comment_id ) { 
  198. add_comment_meta( $comment_id, 'rating', (int) esc_attr( $review[3] ), true ); 
  199. add_comment_meta( $comment_id, 'order_id', esc_attr( $review[1] ), true ); 
  200. return ( is_array( $reviews ) ) ? true : false; 
  201.  
  202. public function is_product( $id ) { 
  203. return ( get_post_status( $id ) == 'publish' ) ? true : false; 
  204.  
  205. /** 
  206. * Checks if a review already exists by using a eKomi order ID 
  207. * @param string $review_order_id 
  208. * @return boolean 
  209. */ 
  210. public function review_exists( $review_order_id ) { 
  211. $comments_query = new WP_Comment_Query; 
  212. $comments = $comments_query->query( array( 'meta_key' => 'order_id', 'meta_value' => $review_order_id ) ); 
  213. return empty( $comments ) ? false : true; 
  214.  
  215. /** 
  216. * Returns the eKomi Widget html 
  217. * @param array $atts 
  218. * @return string 
  219. */ 
  220. public function get_widget( $atts = array() ) { 
  221. return ( $this->is_enabled() ) ? '<div id="eKomiWidget_default"></div>' : ''; 
  222.  
  223. /** 
  224. * Returns the eKomi Badge html 
  225. * @param array $atts 
  226. * @return string 
  227. */ 
  228. public function get_badge( $atts = array() ) { 
  229. extract( shortcode_atts( array( 'width' => '', ), $atts ) ); 
  230. return ( $this->is_enabled() ) ? '<div id="eKomiSeal_default" style="' . ( $width ? 'width:' . $width . 'px;height:' . $width . 'px;' : '' ) . '"></div>' : ''; 
  231.  
  232. /** 
  233. * Adds necessary scripts to the Footer to enable badge + widget generation 
  234. */ 
  235. public function add_scripts() { 
  236. echo ' 
  237. <script type="text/javascript"> 
  238. (function() { 
  239. eKomiIntegrationConfig = new Array( 
  240. {certId:\'' . get_option( 'woocommerce_gzd_ekomi_partner_id' ) . '\'} 
  241. ); 
  242. if(typeof eKomiIntegrationConfig != "undefined") {for(var eKomiIntegrationLoop=0;eKomiIntegrationLoop<eKomiIntegrationConfig.length;eKomiIntegrationLoop++) { 
  243. var eKomiIntegrationContainer = document.createElement(\'script\'); 
  244. eKomiIntegrationContainer.type = \'text/javascript\'; eKomiIntegrationContainer.defer = true; 
  245. eKomiIntegrationContainer.src = (document.location.protocol==\'https:\'?\'https:\':\'http:\') +"//connect.ekomi.de/integration_1409045085/" + eKomiIntegrationConfig[eKomiIntegrationLoop].certId + ".js"; 
  246. document.getElementsByTagName("head")[0].appendChild(eKomiIntegrationContainer); 
  247. }}else{if(\'console\' in window) { console.error(\'connectEkomiIntegration - Cannot read eKomiIntegrationConfig\'); }} 
  248. })(); 
  249. </script> 
  250. '; 
  251.  
  252. /** 
  253. * Returns eKomi Settings for Admin Interface 
  254. * @return array 
  255. */ 
  256. public function get_settings() { 
  257.  
  258. return array( 
  259.  
  260. array( 'title' => _x( 'Ekomi Integration', 'ekomi', 'woocommerce-germanized' ), 'type' => 'title', 'id' => 'ekomi_options' ),  
  261.  
  262. array( 
  263. 'title' => _x( 'Shop ID', 'ekomi', 'woocommerce-germanized' ),  
  264. 'desc' => _x( 'Insert your Shop ID here.', 'ekomi', 'woocommerce-germanized' ),  
  265. 'desc_tip' => true,  
  266. 'id' => 'woocommerce_gzd_ekomi_shop_id',  
  267. 'type' => 'text',  
  268. 'css' => 'min-width:300px;',  
  269. ),  
  270.  
  271. array( 
  272. 'title' => _x( 'Link to Certificate', 'ekomi', 'woocommerce-germanized' ),  
  273. 'desc' => _x( 'Insert the link to your Certificate', 'ekomi', 'woocommerce-germanized' ),  
  274. 'desc_tip' => true,  
  275. 'id' => 'woocommerce_gzd_ekomi_certificate_link',  
  276. 'type' => 'text',  
  277. 'css' => 'min-width:300px;',  
  278. ),  
  279.  
  280. array( 
  281. 'title' => _x( 'Partner ID', 'ekomi', 'woocommerce-germanized' ),  
  282. 'desc' => _x( 'Insert your Partner ID here (you may find that ID on your certificate website)', 'ekomi', 'woocommerce-germanized' ),  
  283. 'desc_tip' => true,  
  284. 'id' => 'woocommerce_gzd_ekomi_partner_id',  
  285. 'type' => 'text',  
  286. 'css' => 'min-width:300px;',  
  287. ),  
  288.  
  289. array( 
  290. 'title' => _x( 'Interface ID', 'ekomi', 'woocommerce-germanized' ),  
  291. 'desc' => _x( 'Insert your Interface ID here.', 'ekomi', 'woocommerce-germanized' ),  
  292. 'desc_tip' => true,  
  293. 'id' => 'woocommerce_gzd_ekomi_interface_id',  
  294. 'type' => 'text',  
  295. 'css' => 'min-width:300px;',  
  296. ),  
  297.  
  298. array( 
  299. 'title' => _x( 'Interface Password', 'ekomi', 'woocommerce-germanized' ),  
  300. 'desc' => _x( 'Insert your Interface Password here.', 'ekomi', 'woocommerce-germanized' ),  
  301. 'desc_tip' => true,  
  302. 'id' => 'woocommerce_gzd_ekomi_interface_password',  
  303. 'type' => 'text',  
  304. 'css' => 'min-width:300px;',  
  305. ),  
  306.  
  307. array( 
  308. 'title' => _x( 'Days until Email', 'ekomi', 'woocommerce-germanized' ),  
  309. 'desc' => _x( 'Number of days between an order being marked as completed and review email to customer.', 'ekomi', 'woocommerce-germanized' ),  
  310. 'desc_tip' => true,  
  311. 'id' => 'woocommerce_gzd_ekomi_day_diff',  
  312. 'type' => 'number',  
  313. 'custom_attributes' => array( 'min' => 0, 'step' => 1 ),  
  314. 'default' => 7,  
  315. ),  
  316.  
  317. array( 'type' => 'sectionend', 'id' => 'ekomi_options' ),  
  318.  
  319. ); 
  320.  
  321.  
  322. public function register_section( $sections ) { 
  323. $sections[ 'ekomi' ] = _x( 'eKomi Options', 'ekomi', 'woocommerce-germanized' ); 
  324. return $sections; 
  325.