PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/campus-academy/krowkaramel
PHP | 507 lines | 273 code | 120 blank | 114 comment | 19 complexity | d5704ac9cf37da5f114cfa7474b964bb MD5 | raw file
  1. <?php
  2. namespace Oxaim\Libs;
  3. defined( 'ABSPATH' ) || exit;
  4. if(!class_exists('\Oxaim\Libs\Notice')):
  5. class Notice{
  6. /**
  7. * scripts version
  8. *
  9. * @var string
  10. */
  11. protected $script_version = '2.1.1';
  12. /**
  13. * Unique ID to identify each notice
  14. *
  15. * @var string
  16. */
  17. protected $notice_id;
  18. /**
  19. * Plugin text-domain
  20. *
  21. * @var string
  22. */
  23. protected $text_domain;
  24. /**
  25. * Unique ID
  26. *
  27. * @var string
  28. */
  29. protected $unique_id;
  30. /**
  31. * Notice div container's class
  32. *
  33. * @var string
  34. */
  35. protected $class;
  36. /**
  37. * Single button's data
  38. *
  39. * @var array
  40. */
  41. protected $button;
  42. /**
  43. * Size class
  44. *
  45. * @var array
  46. */
  47. protected $size;
  48. /**
  49. * List of all buttons with it's config data
  50. *
  51. * @var array
  52. */
  53. protected $buttons;
  54. /**
  55. * Notice title
  56. *
  57. * @var string
  58. */
  59. protected $title;
  60. /**
  61. * Notice message
  62. *
  63. * @var string
  64. */
  65. protected $message;
  66. /**
  67. * Left logo
  68. *
  69. * @var string
  70. */
  71. protected $logo;
  72. /**
  73. * Container gutter
  74. *
  75. * @var string
  76. */
  77. protected $gutter;
  78. /**
  79. * Left logo style
  80. *
  81. * @var string
  82. */
  83. protected $logo_style;
  84. /**
  85. * Left logo style
  86. *
  87. * @var string
  88. */
  89. protected $dismissible;
  90. protected $expired_time;
  91. /**
  92. * html markup for notice
  93. *
  94. * @var string
  95. */
  96. protected $html;
  97. /**
  98. * get_version
  99. *
  100. * @return string
  101. */
  102. public function get_version(){
  103. return $this->script_version;
  104. }
  105. /**
  106. * get_script_location
  107. *
  108. * @return string
  109. */
  110. public function get_script_location(){
  111. return __FILE__;
  112. }
  113. // config
  114. /**
  115. * Configures all setter variables
  116. *
  117. * @param string $prefix
  118. * @return void
  119. */
  120. public function config($text_domain = '', $unique_id = ''){
  121. $this->text_domain = $text_domain;
  122. $this->unique_id = $unique_id;
  123. $this->notice_id = $text_domain . '-' . $unique_id;
  124. $this->dismissible = false; // false, user, global
  125. $this->expired_time = 1;
  126. $this->html = '';
  127. $this->title = '';
  128. $this->message = '';
  129. $this->class = '';
  130. $this->gutter = true;
  131. $this->logo = '';
  132. $this->logo_style = '';
  133. $this->size = [ ];
  134. $this->button = [
  135. 'default_class' => 'button',
  136. 'class' => 'button-secondary ', // button-primary button-secondary button-small button-large button-link
  137. 'text' => 'Button',
  138. 'url' => '#',
  139. 'icon' => ''
  140. ];
  141. $this->buttons = [];
  142. return $this;
  143. }
  144. // setters begin
  145. /**
  146. * Adds classes to the container
  147. *
  148. * @param string $classname
  149. * @return void
  150. */
  151. public function set_class($classname = ''){
  152. $this->class .= $classname;
  153. return $this;
  154. }
  155. public function set_type($type = ''){
  156. $this->class .= ' notice-' . $type;
  157. return $this;
  158. }
  159. public function set_button($button = []){
  160. $button = array_merge($this->button, $button);
  161. $this->buttons[] = $button;
  162. return $this;
  163. }
  164. public function set_id($id){
  165. $this->notice_id = $id;
  166. return $this;
  167. }
  168. public function set_title($title = ''){
  169. $this->title .= $title;
  170. return $this;
  171. }
  172. public function set_message($message = ''){
  173. $this->message .= $message;
  174. return $this;
  175. }
  176. public function set_gutter($gutter = true){
  177. $this->gutter .= $gutter;
  178. $this->class .= ($gutter === true ? '' : ' no-gutter');
  179. return $this;
  180. }
  181. public function set_logo($logo = '', $logo_style = ""){
  182. $this->logo = $logo;
  183. $this->logo_style = $logo_style;
  184. return $this;
  185. }
  186. public function set_html($html = ''){
  187. $this->html .= $html;
  188. return $this;
  189. }
  190. // setters ends
  191. // group getter
  192. public function get_data(){
  193. return [
  194. 'message' => $this->message,
  195. 'title' => $this->title,
  196. 'buttons' => $this->buttons,
  197. 'class' => $this->class,
  198. 'html' => $this->html,
  199. ];
  200. }
  201. public function call(){
  202. add_action( 'admin_notices', [$this, 'get_notice'] );
  203. }
  204. public function get_notice(){
  205. // dismissible conditions
  206. if ( 'user' === $this->dismissible) {
  207. $expired = get_user_meta( get_current_user_id(), $this->notice_id, true );
  208. } elseif ( 'global' === $this->dismissible ) {
  209. $expired = get_transient( $this->notice_id );
  210. }else{
  211. $expired = '';
  212. }
  213. global $oxaim_lib_notice_list;
  214. if(!isset($oxaim_lib_notice_list[$this->notice_id])){
  215. $oxaim_lib_notice_list[$this->notice_id] = __FILE__;
  216. // is transient expired?
  217. if ( false === $expired || empty( $expired ) ) {
  218. $this->generate_html();
  219. }
  220. }
  221. }
  222. public function set_dismiss($scope = 'global', $time = (3600 * 24 * 7)){
  223. $this->dismissible = $scope;
  224. $this->expired_time = $time;
  225. return $this;
  226. }
  227. public function generate_html() {
  228. ?>
  229. <div
  230. id="<?php echo esc_attr($this->notice_id); ?>"
  231. class="notice wpmet-notice notice-<?php echo esc_attr($this->notice_id . ' ' .$this->class); ?> <?php echo (false === $this->dismissible ? '' : 'is-dismissible') ;?>"
  232. expired_time="<?php echo ($this->expired_time); ?>"
  233. dismissible="<?php echo ($this->dismissible); ?>"
  234. >
  235. <?php if(!empty($this->logo)):?>
  236. <img class="notice-logo" style="<?php echo esc_attr($this->logo_style); ?>" src="<?php echo esc_url($this->logo);?>" />
  237. <?php endif; ?>
  238. <div class="notice-right-container <?php echo (empty($this->logo) ? 'notice-container-full-width' : ''); ?>">
  239. <?php if(empty($this->html)): ?>
  240. <?php echo (empty($this->title) ? '' : sprintf('<div class="notice-main-title notice-vert-space">%s</div>', $this->title)); ?>
  241. <div class="notice-message notice-vert-space">
  242. <?php echo ( $this->message );?>
  243. </div>
  244. <?php if(!empty($this->buttons)): ?>
  245. <div class="button-container notice-vert-space">
  246. <?php foreach($this->buttons as $button): ?>
  247. <a id="<?php echo (!isset($button['id']) ? '' : $button['id']); ?>" href="<?php echo esc_url($button['url']); ?>" class="wpmet-notice-button <?php echo esc_attr($button['class']); ?>">
  248. <?php if(!empty($button['icon'])) :?>
  249. <i class="notice-icon <?php echo esc_attr($button['icon']); ?>"></i>
  250. <?php endif; ?>
  251. <?php echo esc_html($button['text']);?>
  252. </a>
  253. &nbsp;
  254. <?php endforeach; ?>
  255. </div>
  256. <?php endif;?>
  257. <?php else:?>
  258. <?php echo $this->html; ?>
  259. <?php endif;?>
  260. </div>
  261. <?php if(false !== $this->dismissible): ?>
  262. <button type="button" class="notice-dismiss">
  263. <span class="screen-reader-text">x</span>
  264. </button>
  265. <?php endif;?>
  266. <div style="clear:both"></div>
  267. </div>
  268. <?php
  269. }
  270. public static function init(){
  271. add_action( 'wp_ajax_wpmet-notices', [ __CLASS__, 'dismiss_ajax_call' ] );
  272. add_action( 'admin_head', [ __CLASS__, 'enqueue_scripts' ] );
  273. }
  274. public static function dismiss_ajax_call() {
  275. $notice_id = ( isset( $_POST['notice_id'] ) ) ? $_POST['notice_id'] : '';
  276. $dismissible = ( isset( $_POST['dismissible'] ) ) ? $_POST['dismissible'] : '';
  277. $expired_time = ( isset( $_POST['expired_time'] ) ) ? $_POST['expired_time'] : '';
  278. if ( ! empty( $notice_id ) ) {
  279. if ( 'user' === $dismissible ) {
  280. update_user_meta( get_current_user_id(), $notice_id, true );
  281. } else {
  282. set_transient( $notice_id, true, $expired_time );
  283. }
  284. wp_send_json_success();
  285. }
  286. wp_send_json_error();
  287. }
  288. public static function enqueue_scripts() {
  289. echo "
  290. <script>
  291. jQuery(document).ready(function ($) {
  292. $( '.wpmet-notice.is-dismissible' ).on( 'click', '.notice-dismiss', function() {
  293. _this = $( this ).parents('.wpmet-notice').eq(0);
  294. var notice_id = _this.attr( 'id' ) || '';
  295. var expired_time = _this.attr( 'expired_time' ) || '';
  296. var dismissible = _this.attr( 'dismissible' ) || '';
  297. var x = $( this ).attr('class');
  298. // console.log({
  299. // _this, x, notice_id, expired_time, dismissible
  300. // });
  301. // return;
  302. _this.hide();
  303. $.ajax({
  304. url: ajaxurl,
  305. type: 'POST',
  306. data: {
  307. action : 'wpmet-notices',
  308. notice_id : notice_id,
  309. dismissible : dismissible,
  310. expired_time : expired_time,
  311. },
  312. });
  313. });
  314. });
  315. </script>
  316. <style>
  317. .wpmet-notice{
  318. margin-bottom: 15px;
  319. padding: 0!important;
  320. display: flex;
  321. flex-direction: row;
  322. justify-content: flex-start;
  323. align-items: center;
  324. }
  325. .wpmet-notice .notice-right-container{
  326. margin: .7rem .8rem .8rem;
  327. }
  328. .notice-container-full-width{
  329. width:100%!important;
  330. }
  331. .wpmet-notice.no-gutter{
  332. padding: 0!important;
  333. border-width: 0!important;
  334. }
  335. .wpmet-notice.no-gutter .notice-right-container{
  336. padding: 0!important;
  337. margin: 0!important;
  338. }
  339. .notice-right-container .notice-vert-space{
  340. margin-bottom: .8rem;
  341. }
  342. .notice-right-container .notice-vert-space:last-child,
  343. .notice-right-container .notice-vert-space:only-child{
  344. margin-bottom: 0;
  345. }
  346. .wpmet-notice .notice-logo{
  347. padding: 3px;
  348. max-width: 110px;
  349. max-height: 110px;
  350. }
  351. .wpmet-notice-button {
  352. text-decoration:none;
  353. }
  354. .wpmet-notice-button > i{
  355. margin-right: 3px;
  356. }
  357. .wpmet-notice-button .notice-icon{
  358. display:inline-block;
  359. }
  360. .wpmet-notice-button .notice-icon:before{
  361. vertical-align: middle!important;
  362. margin-top: -1px;
  363. }
  364. .wpmet-notice .notice-main-title{
  365. color: #1d2327;
  366. font-size: 1.2rem;
  367. }
  368. </style>
  369. ";
  370. }
  371. private static $instance;
  372. /**
  373. * Method: instance -> Return Notice module class instance
  374. *
  375. * @param string|null $text_domain
  376. * @param string|null $unique_id
  377. * @return mixed
  378. */
  379. public static function instance($text_domain = null, $unique_id = null) {
  380. if($text_domain == null){
  381. return false;
  382. }
  383. self::$instance = new self();
  384. return self::$instance->config($text_domain, (is_null($unique_id) ? uniqid() : $unique_id));
  385. }
  386. }
  387. endif;