WC_GZD_Trusted_Shops_Schedule

The WooCommerce Germanized WC GZD Trusted Shops Schedule class.

Defined (1)

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

/includes/trusted-shops/class-wc-gzd-trusted-shops-schedule.php  
  1. class WC_GZD_Trusted_Shops_Schedule { 
  2.  
  3. public $base = null; 
  4.  
  5. protected static $_instance = null; 
  6.  
  7. public static function instance( $base ) { 
  8. if ( is_null( self::$_instance ) ) 
  9. self::$_instance = new self( $base ); 
  10. return self::$_instance; 
  11.  
  12. private function __construct( $base ) { 
  13. $this->base = $base; 
  14.  
  15. if ( $this->base->is_rich_snippets_enabled() ) { 
  16.  
  17. add_action( 'woocommerce_gzd_trusted_shops_reviews', array( $this, 'update_reviews' ) ); 
  18. $reviews = $this->base->reviews_cache; 
  19.  
  20. // Generate reviews for the first time 
  21. if ( empty( $reviews ) ) 
  22. add_action( 'init', array( $this, 'update_reviews' ) ); 
  23.  
  24. if ( $this->base->is_review_widget_enabled() ) { 
  25.  
  26. add_action( 'woocommerce_gzd_trusted_shops_reviews', array( $this, 'update_review_widget' ) ); 
  27. $attachment = $this->base->review_widget_attachment; 
  28.  
  29. // Generate attachment for the first time 
  30. if ( empty( $attachment ) ) 
  31. add_action( 'init', array( $this, 'update_review_widget' ) ); 
  32.  
  33. if ( $this->base->is_review_reminder_enabled() ) 
  34. add_action( 'woocommerce_gzd_trusted_shops_reviews', array( $this, 'send_mails' ) ); 
  35.  
  36. /** 
  37. * Update Review Cache by grabbing information from xml file 
  38. */ 
  39. public function update_reviews() { 
  40.  
  41. $update = array(); 
  42.  
  43. if ( $this->base->is_enabled() ) { 
  44.  
  45. if ( function_exists( 'curl_version' ) ) { 
  46.  
  47. $success = false; 
  48. $ch = curl_init(); 
  49. curl_setopt( $ch, CURLOPT_HEADER, false ); 
  50. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); 
  51. curl_setopt( $ch, CURLOPT_POST, false ); 
  52. curl_setopt( $ch, CURLOPT_URL, $this->base->api_url ); 
  53. $output = curl_exec( $ch ); 
  54. $httpcode = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); 
  55.  
  56. if ( ! curl_errno( $ch ) && $httpcode != 503 ) 
  57. $success = true; 
  58.  
  59. curl_close( $ch ); 
  60.  
  61. if ( $success ) { 
  62. $output = json_decode( $output, true ); 
  63. $reviews = $output[ 'response' ][ 'data' ][ 'shop' ][ 'qualityIndicators' ][ 'reviewIndicator' ]; 
  64. $update[ 'count' ] = (string) $reviews[ 'activeReviewCount' ]; 
  65. $update[ 'avg' ] = (float) $reviews[ 'overallMark' ]; 
  66. $update[ 'max' ] = '5.00'; 
  67.  
  68. update_option( 'woocommerce_' . $this->base->option_prefix . 'trusted_shops_reviews_cache', $update ); 
  69.  
  70. /** 
  71. * Updates the review widget graphic and saves it as an attachment 
  72. */ 
  73. public function update_review_widget() { 
  74.  
  75. $uploads = wp_upload_dir(); 
  76.  
  77. if ( is_wp_error( $uploads ) ) 
  78. return; 
  79.  
  80. $filename = $this->base->id . '.gif'; 
  81. $raw_data = $this->get_file_content( 'https://www.trustedshops.com/bewertung/widget/widgets/' . $filename ); 
  82.  
  83. // Seems like neither CURL nor file_get_contents does work 
  84. if ( ! $raw_data ) 
  85. return; 
  86.  
  87. $filepath = trailingslashit( $uploads['path'] ) . $filename; 
  88. file_put_contents( $filepath, $raw_data ); 
  89.  
  90. $attachment = array( 
  91. 'guid' => $uploads[ 'url' ] . '/' . basename( $filepath ),  
  92. 'post_mime_type' => 'image/gif',  
  93. 'post_title' => _x( 'Trusted Shops Customer Reviews', 'trusted-shops', 'woocommerce-germanized' ),  
  94. 'post_content' => '',  
  95. 'post_status' => 'publish',  
  96. ); 
  97.  
  98. $existing_attachment_id = $this->base->get_review_widget_attachment(); 
  99.  
  100. if ( ! $existing_attachment_id || ! get_post( $existing_attachment_id ) ) { 
  101. $attachment_id = wp_insert_attachment( $attachment, $filepath ); 
  102. update_option( 'woocommerce_' . $this->base->option_prefix . 'trusted_shops_review_widget_attachment', $attachment_id ); 
  103. } else { 
  104. $attachment_id = $existing_attachment_id; 
  105. update_attached_file( $attachment_id, $filepath ); 
  106. $attachment[ 'ID' ] = $attachment_id; 
  107. wp_update_post( $attachment ); 
  108.  
  109. require_once( ABSPATH . 'wp-admin/includes/image.php' ); 
  110.  
  111. // Generate the metadata for the attachment, and update the database record. 
  112. $attach_data = wp_generate_attachment_metadata( $attachment_id, $filepath ); 
  113. wp_update_attachment_metadata( $attachment_id, $attach_data ); 
  114.  
  115. /** 
  116. * Send review reminder mails after x days 
  117. */ 
  118. public function send_mails() { 
  119.  
  120. $order_query = new WP_Query( 
  121. array(  
  122. 'post_type' => 'shop_order',  
  123. 'post_status' => array( 'wc-completed' ),  
  124. 'showposts' => -1,  
  125. 'meta_query' => array( 
  126. array( 
  127. 'key' => '_trusted_shops_review_mail_sent',  
  128. 'compare' => 'NOT EXISTS',  
  129. ),  
  130. ),  
  131. ); 
  132.  
  133. while ( $order_query->have_posts() ) { 
  134.  
  135. $order_query->next_post(); 
  136. $order = wc_get_order( $order_query->post->ID ); 
  137. $diff = $this->base->plugin->get_date_diff( wc_gzd_get_crud_data( $order, 'completed_date' ), date( 'Y-m-d H:i:s' ) ); 
  138.  
  139. if ( $diff[ 'd' ] >= (int) $this->base->review_reminder_days ) { 
  140.  
  141. if ( $mail = $this->base->plugin->emails->get_email_instance_by_id( 'customer_trusted_shops' ) ) { 
  142. $mail->trigger( wc_gzd_get_crud_data( $order, 'id' ) ); 
  143. update_post_meta( wc_gzd_get_crud_data( $order, 'id' ), '_trusted_shops_review_mail_sent', 1 ); 
  144.  
  145. /** 
  146. * Helper Function which decides between CURL or file_get_contents based on fopen 
  147. *  
  148. * @param [type] $url [description] 
  149. */ 
  150. private function get_file_content( $url ) { 
  151.  
  152. if ( function_exists( 'curl_init' ) ) { 
  153.  
  154. $ch = curl_init(); 
  155. curl_setopt( $ch, CURLOPT_URL, $url ); 
  156. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); 
  157. $output = curl_exec( $ch ); 
  158. curl_close( $ch ); 
  159.  
  160. return $output; 
  161.  
  162. } elseif ( ini_get( 'allow_url_fopen' ) ) { 
  163. return file_get_contents( $url ); 
  164.  
  165. return false; 
  166.