/wp-content/plugins/elementskit-lite/libs/banner/banner.php

https://gitlab.com/campus-academy/krowkaramel · PHP · 252 lines · 173 code · 79 blank · 0 comment · 38 complexity · 80e2136f22ba00a6e6e0beaf4376d58a MD5 · raw file

  1. <?php
  2. namespace Wpmet\Libs;
  3. defined( 'ABSPATH' ) || exit;
  4. if(!class_exists('\Wpmet\Libs\Banner')):
  5. class Banner {
  6. protected $script_version = '2.1.0';
  7. protected $key = 'wpmet_banner';
  8. protected $data;
  9. protected $last_check;
  10. protected $check_interval = (3600 * 6);
  11. protected $plugin_screens;
  12. protected $text_domain;
  13. protected $filter_string;
  14. protected $filter_array = [];
  15. protected $api_url;
  16. public function get_version(){
  17. return $this->script_version;
  18. }
  19. public function get_script_location(){
  20. return __FILE__;
  21. }
  22. public function call(){
  23. add_action( 'admin_head', [$this, 'display_content'] );
  24. }
  25. public function display_content(){
  26. $this->get_data();
  27. if(!empty($this->data->error)) {
  28. return;
  29. }
  30. if(empty($this->data)) {
  31. return;
  32. }
  33. foreach($this->data as $content) {
  34. if(!empty($this->filter_array) && $this->in_blacklist($content, $this->filter_array)) {
  35. continue;
  36. }
  37. if($content->start <= time() && time() <= $content->end) {
  38. $screen = get_current_screen();
  39. if($this->is_correct_screen_to_show($content->screen, $screen->id) && class_exists('\Oxaim\Libs\Notice')) {
  40. $inline_css = '';
  41. $banner_unique_id = ((isset($content->data->unique_key) && $content->data->unique_key != '') ? $content->data->unique_key : $content->id );
  42. if(!empty($content->data->style_css)) {
  43. $inline_css =' style="'.$content->data->style_css.'"';
  44. }
  45. $instance = \Oxaim\Libs\Notice::instance('wpmet-jhanda', $banner_unique_id)
  46. ->set_dismiss('global', (3600 * 24 * 15));
  47. if($content->type == 'banner'){
  48. $this->init_banner($content, $instance, $inline_css);
  49. }
  50. if($content->type == 'notice'){
  51. $this->init_notice($content, $instance, $inline_css);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. private function init_notice($content, $instance, $inline_css){
  58. $instance->set_message($content->data->notice_body);
  59. if($content->data->notice_image != ''){
  60. $instance->set_logo($content->data->notice_image);
  61. }
  62. if($content->data->button_text != ''){
  63. $instance->set_button([
  64. 'default_class' => 'button',
  65. 'class' => 'button-secondary button-small', // button-primary button-secondary button-small button-large button-link
  66. 'text' => $content->data->button_text,
  67. 'url' => $content->data->button_link,
  68. ]);
  69. }
  70. $instance->call();
  71. }
  72. private function init_banner($content, $instance, $inline_css){
  73. $html = '<a target="_blank" '.$inline_css.' class="wpmet-jhanda-href" href="'.$content->data->banner_link.'"><img style="display: block;margin: 0 auto;" src="'.$content->data->banner_image.'" /></a>';
  74. $instance->set_gutter(false)
  75. ->set_html($html)
  76. ->call();
  77. }
  78. private function in_whitelist($conf, $list) {
  79. $match = $conf->data->whitelist;
  80. if(empty($match)) {
  81. return true;
  82. };
  83. $match_arr = explode(',', $match);
  84. foreach($list as $word) {
  85. if(in_array($word, $match_arr)) {
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91. private function in_blacklist($conf, $list) {
  92. $match = $conf->data->blacklist;
  93. if(empty($match)) {
  94. return false;
  95. };
  96. $match_arr = explode(',', $match);
  97. foreach($match_arr as $idx => $item) {
  98. $match_arr[$idx] = trim($item);
  99. }
  100. foreach($list as $word) {
  101. if(in_array($word, $match_arr)) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. public function is_test($is_test = false) {
  108. if($is_test === true){
  109. $this->check_interval = 1;
  110. }
  111. return $this;
  112. }
  113. public function set_text_domain($text_domain) {
  114. $this->text_domain = $text_domain;
  115. return $this;
  116. }
  117. public function set_filter($filter_string) {
  118. $this->filter_string = $filter_string;
  119. if(!empty($filter_string)) {
  120. $filter = explode(',', $this->filter_string);
  121. foreach ($filter as $id => $item) {
  122. $this->filter_array[$id] = trim($item);
  123. }
  124. }
  125. return $this;
  126. }
  127. public function set_api_url($url) {
  128. $this->api_url = $url;
  129. return $this;
  130. }
  131. public function set_plugin_screens($screen) {
  132. $this->plugin_screens[] = $screen;
  133. return $this;
  134. }
  135. private function get_data() {
  136. $this->data = get_option($this->text_domain . '__banner_data');
  137. $this->data = $this->data == '' ? [] : $this->data;
  138. $this->last_check = get_option($this->text_domain . '__banner_last_check');
  139. $this->last_check = $this->last_check == '' ? 0 : $this->last_check;
  140. if(($this->check_interval + $this->last_check) < time()){
  141. $response = wp_remote_get( $this->api_url . '/cache/'.$this->text_domain.'.json?nocache='.time(),
  142. [
  143. 'timeout' => 10,
  144. 'httpversion' => '1.1',
  145. ]
  146. );
  147. if(!is_wp_error($response) && isset($response['body']) && $response['body'] != ''){
  148. $response = json_decode($response['body']);
  149. if(!empty($response)) {
  150. $this->data = $response;
  151. update_option($this->text_domain . '__banner_last_check', time());
  152. update_option($this->text_domain . '__banner_data', $this->data);
  153. }
  154. return;
  155. }
  156. }
  157. }
  158. public function is_correct_screen_to_show($b_screen, $screen_id) {
  159. if(in_array($b_screen, [$screen_id, 'all_page'])) {
  160. return true;
  161. }
  162. if($b_screen == 'plugin_page') {
  163. return in_array($screen_id, $this->plugin_screens);
  164. }
  165. return false;
  166. }
  167. private static $instance;
  168. public static function instance($text_domain = '') {
  169. self::$instance = new static();
  170. return self::$instance->set_text_domain($text_domain);
  171. }
  172. }
  173. endif;