PluginProbe ʕ •ᴥ•ʔ
پارسی دیت – Parsi Date / trunk
پارسی دیت – Parsi Date vtrunk
6.2 6.1 5.1.6 5.1.7 5.1.8 5.1.8.2 6.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.0.0-alpha 2.1 2.1.1 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0.1 2.3.0.2 2.3.1 2.3.2 2.3.3 2.3.4 3.0.1 3.0.2 3.0.3 4.0.0 4.0.1 4.0.2 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5
wp-parsidate / inc / Helper / Notice.php
wp-parsidate / inc / Helper Last commit date
Assets.php 6 hours ago Cache.php 3 weeks ago Date.php 6 hours ago Debug.php 5 hours ago FeedReader.php 6 hours ago HTML.php 6 hours ago Helper.php 3 weeks ago JSON.php 3 weeks ago Nonce.php 3 weeks ago Notice.php 6 hours ago Number.php 3 weeks ago NumberConverter.php 6 hours ago Param.php 3 weeks ago Sanitizing.php 6 hours ago Strip.php 3 weeks ago Templates.php 3 weeks ago User.php 3 weeks ago Validating.php 3 weeks ago WooCommerce.php 3 weeks ago WordPress.php 6 hours ago
Notice.php
185 lines
1 <?php
2
3 namespace WPParsidate\Helper;
4
5 defined( 'ABSPATH' ) || die();
6
7 class Notice {
8 private static array $messages = [];
9
10 /**
11 * Add notice and display immediately
12 *
13 * @param string $key Notice key
14 * @param array $notices Notice list
15 * @param bool $echo True, print notice
16 *
17 * @return string
18 */
19 public static function addAndDisplay( string $key, array $notices, bool $echo = true ): string {
20 self::clear( $key );
21
22 foreach ( $notices as $notice ) {
23 if ( is_array( $notice ) ) {
24 self::add( $key, $notice['message'], $notice['type'] ?? null, $notice['link_title'] ?? null,
25 $notice['link'] ?? null );
26 }
27 }
28
29 return self::display( $key, null, $echo );
30 }
31
32 /**
33 * Add notice
34 *
35 * @param string $key Notice key
36 * @param string $message Notice message
37 * @param string|null $type Notice type (default, info, success, warning, error)
38 * @param string|null $linkTitle Notice link title
39 * @param string|null $link Notice link url
40 *
41 * @return void
42 */
43 public static function add(
44 string $key,
45 string $message,
46 ?string $type = null,
47 ?string $linkTitle = null,
48 ?string $link = null
49 ): void {
50 if ( ! $key || ! $message ) {
51 return;
52 }
53
54 $type = self::getType( $type );
55 self::$messages[ $key ][] = array(
56 'type' => $type,
57 'message' => $message,
58 'link_title' => $linkTitle,
59 'link' => $link,
60 );
61 }
62
63 /**
64 * Clear notice(s) by the Key
65 *
66 * @param string $key Notice key
67 *
68 * @return void
69 */
70 public static function clear( string $key ): void {
71 self::$messages[ $key ] = array();
72 }
73
74 /**
75 * Display notice(s)
76 *
77 * @param string $key Notice key
78 * @param string|null $type Notice type, if is null display all type of notice
79 * @param bool $echo Print notice
80 *
81 * @return string Notice(s) HTML
82 */
83 public static function display( string $key, ?string $type = null, bool $echo = true ): string {
84 $type = is_null( $type ) ? $type : self::getType( $type );
85 $messages = self::$messages[ $key ] ?? [];
86 $notices = $noticeWrap = '';
87
88 if ( ! empty( $messages ) ) {
89 foreach ( $messages as $message ) {
90 if ( ! is_null( $type ) && $message['type'] !== $type ) {
91 continue;
92 }
93
94 $notices .= self::html( $message['type'], $message['message'], $message['link_title'] ?? null,
95 $message['link'] ?? null );
96 }
97
98 self::clear( $key );
99 }
100
101 if ( ! empty( $notices ) ) {
102 $noticeWrap = '<div class="' . WP_PARSI_KEY_SLUG . '-notices">' . $notices . '</div>';
103 }
104
105 if ( $echo ) {
106 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
107 echo $noticeWrap;
108 } else {
109 return $noticeWrap;
110 }
111
112 return '';
113 }
114
115 /**
116 * Get HTML of a notice
117 *
118 * @param string $type Type
119 * @param string $message Message
120 * @param string|null $linkTitle Link title
121 * @param string|null $link Link URL
122 *
123 * @return string HTML of notice
124 */
125 public static function html( string $type, string $message, ?string $linkTitle = '', ?string $link = '' ): string {
126 $type = self::getType( $type );
127
128 $link = $link && $linkTitle ? '<a href="' . $link . '" ' . ( Validating::isExternalLink( $link ) ? 'target="_blank"' : '' ) . ' class="' . WP_PARSI_CLASS_PREFIX . 'notice-link">' . $linkTitle . '</a>' : '';
129
130 return '<div class="' . WP_PARSI_CLASS_PREFIX . 'notice ' . WP_PARSI_CLASS_PREFIX . 'notice-' . $type . '" ><div>' . self::getIcon( $type ) . '<p>' . $message . '</p></div>' . $link . '</div>';
131 }
132
133 /**
134 * Get notice icon base on type
135 *
136 * @param string $type Type of notice
137 *
138 * @return string HTML of Icon
139 */
140 private static function getIcon( $type ): string {
141 $icons = array(
142 'default' => '<svg width="22" height="22" viewBox="0 0 22 22" fill="none" xmlns="http://www.w3.org/2000/svg">
143 <path d="M7.85 17.2542H7.4C3.8 17.2542 2 16.3569 2 11.8704V7.38385C2 3.79462 3.8 2 7.4 2H14.6C18.2 2 20 3.79462 20 7.38385V11.8704C20 15.4596 18.2 17.2542 14.6 17.2542H14.15C13.871 17.2542 13.601 17.3888 13.43 17.6132L12.08 19.4078C11.486 20.1974 10.514 20.1974 9.92 19.4078L8.57 17.6132C8.426 17.4158 8.093 17.2542 7.85 17.2542Z" stroke="#3c3c3c" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"/>
144 <path opacity="0.6" d="M7 8H16" stroke="#3c3c3c" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
145 <path opacity="0.6" d="M7 12H12" stroke="#3c3c3c" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
146 </svg>',
147 'info' => '<svg width="22" height="22" viewBox="0 0 22 22" fill="none" xmlns="http://www.w3.org/2000/svg">
148 <path d="M11 20.1666C16.0417 20.1666 20.1667 16.0416 20.1667 11C20.1667 5.95831 16.0417 1.83331 11 1.83331C5.95833 1.83331 1.83333 5.95831 1.83333 11C1.83333 16.0416 5.95833 20.1666 11 20.1666Z" stroke="#4F8CCF" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
149 <path opacity="0.6" d="M11 7.33331V11.9166" stroke="#4F8CCF" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
150 <path opacity="0.6" d="M10.9946 14.6667H11.0029" stroke="#4F8CCF" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>
151 </svg>',
152 'success' => '<svg width="22" height="22" viewBox="0 0 22 22" fill="none" xmlns="http://www.w3.org/2000/svg">
153 <path d="M20.0933 13.7757V8.29768C20.0933 3.73268 18.2673 1.90668 13.7023 1.90668H8.22433C3.65933 1.90668 1.83333 3.73268 1.83333 8.29768V13.7757C1.83333 18.3407 3.65933 20.1667 8.22433 20.1667H13.7023C18.2673 20.1667 20.0933 18.3407 20.0933 13.7757Z" stroke="#44B05C" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
154 <path opacity="0.6" d="M7.18667 11.0367L9.70875 13.5667L14.74 8.50665" stroke="#44B05C" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
155 </svg>',
156 'warning' => '<svg width="22" height="22" viewBox="0 0 22 22" fill="none" xmlns="http://www.w3.org/2000/svg">
157 <path d="M10.9994 19.6257H5.44449C2.26365 19.6257 0.934486 17.3523 2.47449 14.5748L5.33449 9.42317L8.02949 4.58316C9.66112 1.64066 12.3378 1.64066 13.9694 4.58316L16.6644 9.43233L19.5244 14.584C21.0644 17.3615 19.7261 19.6348 16.5544 19.6348H10.9994V19.6257Z" stroke="#AB873A" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
158 <path opacity="0.6" d="M11 8.25V12.8333" stroke="#AB873A" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
159 <path opacity="0.6" d="M10.9946 15.5833H11.0028" stroke="#AB873A" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>
160 </svg>',
161 'error' => '<svg width="22" height="22" viewBox="0 0 22 22" fill="none" xmlns="http://www.w3.org/2000/svg">
162 <path d="M19.3235 7.86501V14.135C19.3235 15.1616 18.7735 16.115 17.8844 16.6375L12.4394 19.7816C11.5502 20.295 10.4502 20.295 9.55185 19.7816L4.10681 16.6375C3.21765 16.1241 2.66765 15.1708 2.66765 14.135V7.86501C2.66765 6.83834 3.21765 5.88497 4.10681 5.36247L9.55185 2.21831C10.441 1.70498 11.541 1.70498 12.4394 2.21831L17.8844 5.36247C18.7735 5.88497 19.3235 6.82917 19.3235 7.86501Z" stroke="#BC5660" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
163 <path opacity="0.6" d="M11 7.10419V11.9167" stroke="#BC5660" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
164 <path opacity="0.6" d="M11 14.8502V14.9418" stroke="#BC5660" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>
165 </svg>',
166 );
167
168 return $icons[ $type ] ?? $icons['default'];
169 }
170
171 /**
172 * Get type of notice
173 *
174 * @param string $type Notice type
175 *
176 * @return string Notice type
177 */
178 private static function getType( $type ): string {
179 $types = array( 'default', 'info', 'success', 'warning', 'error' );
180 $type = strtolower( $type );
181
182 return in_array( $type, $types, true ) ? $type : 'default';
183 }
184 }
185