PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / trunk
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization vtrunk
1.19.8 1.19.7 1.19.6 1.19.5 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.12.0 1.13.0 1.14.0 1.15.0 1.15.1 1.15.2 1.15.3 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.16.7 1.16.8 1.17.0 1.17.6 1.17.7 1.17.8 1.17.9 1.18.0 1.18.1 1.18.2 1.18.3 1.18.4 1.18.5 1.18.6 1.18.7 1.18.8 1.18.9 1.19.0 1.19.1 1.19.2 1.19.3 1.19.4 1.3.19 1.3.20 1.4.0 1.4.1 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.16 1.5.17 1.5.18 1.5.19 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.8.1 1.8.3 1.9.0 1.9.1 1.9.2
nitropack / classes / WordPress / Settings / Components.php
nitropack / classes / WordPress / Settings Last commit date
AutoPurge.php 4 months ago BeaverBuilder.php 4 months ago CPTOptimization.php 1 month ago CacheWarmup.php 1 month ago CartCache.php 4 months ago Components.php 1 month ago EditorClearCache.php 1 month ago GeneratePreview.php 7 months ago HTMLCompression.php 3 months ago Logger.php 4 months ago OptimizationLevel.php 3 days ago Optimizations.php 1 month ago PurgeCache.php 3 days ago Shortcodes.php 4 months ago StockRefresh.php 2 months ago Subscription.php 4 months ago SystemReport.php 3 months ago TestMode.php 1 month ago
Components.php
254 lines
1 <?php
2
3 namespace NitroPack\WordPress\Settings;
4 /**
5 * Class Components
6 *
7 * This class handles the settings components for the NitroPack plugin in WordPress.
8 *
9 * @package NitroPack\WordPress\Settings
10 */
11 class Components {
12
13 /**
14 * @var string $plugin_dir_url The URL of the plugin directory.
15 */
16 private $plugin_dir_url;
17 /**
18 * @var string $img_path The URL path to the plugin's image assets.
19 */
20 private $img_path;
21 /**
22 * Components constructor.
23 *
24 * Initializes the plugin directory URL.
25 */
26 public function __construct() {
27 $this->plugin_dir_url = plugin_dir_url( NITROPACK_FILE );
28 $this->img_path = $this->plugin_dir_url . 'assets/img/';
29 }
30 /**
31 * Get the URL of the notification icon based on the type.
32 *
33 * @param string $type The type of notification (success, danger, warning, info).
34 * @return string The URL of the notification icon.
35 */
36 private function get_icon_notification_url( $type ) {
37 $icon_path = $this->img_path;
38 switch ( $type ) {
39 case 'success':
40 return $icon_path . 'check.svg';
41 case 'error':
42 return $icon_path . 'alert-triangle.svg';
43 case 'warning':
44 return $icon_path . 'info.svg';
45 case 'info':
46 return $icon_path . 'bell.svg';
47 default:
48 return $icon_path . 'bell.svg';
49 }
50 }
51 /**
52 * Get the HTML for the notification icon based on the type.
53 *
54 * @param string $type The type of notification (success, danger, warning, info).
55 * @return string The HTML string for the notification icon.
56 */
57 private function get_icon_notification( $type ) {
58 $icon_path = $this->get_icon_notification_url( $type );
59 return '<img src="' . $icon_path . '" class="icon" width="16" height="16" alt="icon-' . $type . '">';
60 }
61
62 /**
63 * Renders a notification message.
64 *
65 * @param string $msg The notification message to display.
66 * @param string $type The type of notification (e.g., 'info', 'warning, 'error', 'success' and 'promo'. Defaults to 'info'.
67 * @param bool|string $title The title of the notification. If false, the notification is displayed without a title.
68 * @param bool|string $actions The actions of the notification, mostly buttons (CTAs).
69 * @param bool|array $classes The css classes of the notification. If present, an array must be used.
70 * @param bool|string $dismissibleId The ID used for dismissing the notification as cookie in np_notices.js
71 * @param bool|string $dismissBy by option or transient.
72 * @param bool|array $app_notification An array containing 'end_date' and 'id' for app-specific notifications. If false, no app-specific notification is rendered.
73 *
74 * @return void
75 */
76 public function render_notification( $msg, $type = false, $title = false, $actions = false, $classes = false, $dismissibleId = false, $dismissBy = false, $app_notification = false ) {
77 $default_classes = [ 'nitro-notification', 'notification-' . $type ];
78
79 if ( ! $title ) {
80 $default_classes[] = 'compact';
81 }
82 if ( $app_notification ) {
83 $default_classes[] = 'app-notification';
84 }
85
86 if ( ! $type ) {
87 $type = 'info';
88 }
89
90 if ( $dismissibleId ) {
91
92 $classes[] = 'is-dismissible';
93
94 if ( $dismissBy === 'option' ) {
95 $classes[] = 'dismiss-by-option';
96 $notices = get_option( 'nitropack-dismissed-notices' );
97 /* Don't display if dismissed by option */
98 if ( $notices && is_array( $notices ) && in_array( $dismissibleId, $notices ) ) {
99 return;
100 }
101 }
102 if ( $dismissBy === 'transient' ) {
103 $classes[] = 'dismiss-by-transient';
104 $notice = get_transient( $dismissibleId );
105 /* Don't display if dismissed by transient and the time has passed */
106 if ( $notice && time() < $notice ) {
107 return;
108 }
109 }
110
111 }
112
113 if ( $classes && is_array( $classes ) ) {
114 $default_classes = array_merge( $default_classes, $classes );
115 }
116 $default_classes = implode( ' ', $default_classes );
117
118 echo '<div class="' . $default_classes . '">';
119 ?>
120 <div class="notification-inner">
121 <div class="title-msg">
122 <div class="title-wrapper">
123 <?php if ( $title ) {
124 echo $this->get_icon_notification( $type );
125 echo '<h5 class="title">' . $title . '</h5>';
126 } ?>
127 </div>
128 <div class="msg">
129 <?php if ( ! $title ) {
130 echo $this->get_icon_notification( $type );
131 }
132 echo $msg;
133 ?>
134 </div>
135 </div>
136 <div class="col-span-4 actions">
137 <?php
138 if ( $actions ) {
139 echo $actions;
140 }
141 if ( isset( $app_notification['cta_details_list'] ) && is_array( $app_notification['cta_details_list'] ) ) {
142 foreach ( $app_notification['cta_details_list'] as $cta ) {
143 echo '<a href="' . esc_url( $cta['url'] ) . '" class="btn btn-' . esc_attr( $app_notification['type'] ) . '" target="_blank">' . esc_html( $cta['text'] ) . '</a>';
144 }
145 }
146 if ( $app_notification && $app_notification['end_date'] && $app_notification['id'] ) {
147 $dismiss_url = $app_notification['dismiss_url'] ? $app_notification['dismiss_url'] : '';
148 echo '<a class="btn btn-secondary btn-dismiss" data-notification_end="' . $app_notification['end_date'] . '" data-notification_id="' . $app_notification['id'] . '" data-dismiss-url="' . $dismiss_url . '">' . esc_html__( 'Dismiss', 'nitropack' ) . '</a>';
149 } else if ( $dismissibleId && $dismissBy === 'option' ) {
150 echo '<a class="btn btn-secondary btn-dismiss" data-dismissible-id="' . $dismissibleId . '">' . esc_html__( 'Dismiss', 'nitropack' ) . '</a>';
151 }
152
153 ?>
154 </div>
155 </div>
156 </div>
157 <?php }
158 /**
159 * Renders a toggle switch.
160 *
161 * @param string $id The ID attribute for the checkbox input.
162 * @param int $value The value to determine if the checkbox should be checked. If the value is greater than 0, the checkbox will be checked.
163 */
164 public function render_toggle( $id, $value, $attr = [] ) {
165 $disabled = ! empty( $attr['disabled'] ) ? 'disabled' : '';
166 ?>
167 <label class="inline-flex items-center cursor-pointer ml-auto">
168 <input type="checkbox" id="<?php echo esc_attr( $id ); ?>" name="<?php echo esc_attr( $id ); ?>" <?php echo $disabled; ?> class="sr-only peer" <?php echo (int) $value > 0 ? "checked" : ""; ?>>
169 <div class="toggle"></div>
170 </label>
171 <?php
172 }
173 /**
174 * Renders a button or link element with specified attributes and options.
175 *
176 * @param array $args {
177 * Array of arguments for rendering the button or link.
178 *
179 * @type string $text The text to display inside the button or link. Default empty.
180 * @type string $type The type of element to render, either 'button' or 'link'. Default 'button'.
181 * @type string $classes CSS classes to apply to the element. Default 'btn btn-secondary'.
182 * @type string|null $href The URL for the link element. Only used if 'type' is 'link'. Default null.
183 * @type string $icon The filename of the icon to display inside the element. Default empty.
184 * @type array $attributes Additional HTML attributes to apply to the element. Default empty array.
185 * }
186 */
187 public function render_button( $args ) {
188 $defaults = [
189 'text' => '',
190 'type' => 'button',
191 'classes' => 'btn btn-secondary',
192 'href' => null,
193 'icon' => '',
194 'attributes' => []
195 ];
196 $options = wp_parse_args( $args, $defaults );
197 $attrs = '';
198 foreach ( $options['attributes'] as $key => $value ) {
199 $attrs .= sprintf( ' %s="%s"', esc_attr( $key ), esc_attr( $value ) );
200 }
201 if ( $options['type'] === 'button' ) : ?>
202 <button <?php else :
203 echo '<a href="' . $options['href'] . '"'; ?> <?php endif; ?>
204 class="<?php echo esc_attr( $options['classes'] ); ?>" <?php echo $attrs; ?>>
205 <?php if ( $options['icon'] ) : ?>
206 <img src="<?php echo esc_url( $this->img_path . $options['icon'] ); ?>"
207 class="inline-block mr-2" alt="">
208 <?php endif; ?>
209 <span class="btn-text"><?php echo esc_html( $options['text'], 'nitropack' ); ?></span>
210 <?php if ( $options['type'] === 'button' ) : ?>
211 </button>
212 <?php else : ?>
213 </a>
214 <?php endif; ?>
215 <?php
216 }
217 /**
218 * Renders a tooltip with the specified text and icon.
219 *
220 * @param string $id The unique identifier for the tooltip.
221 * @param string $text The text content to display inside the tooltip.
222 * @param string $icon The filename of the icon to display (default is 'info.svg').
223 */
224 public function render_tooltip( $id, $text, $icon = 'info.svg' ) { ?>
225 <span class="tooltip-icon" data-tooltip-target="tooltip-<?php echo $id; ?>">
226 <img src="<?php echo $this->img_path . $icon; ?>" alt="info">
227 </span>
228 <div id="tooltip-<?php echo $id; ?>" role="tooltip" class="tooltip-container hidden">
229 <?php echo wp_kses_post( $text ); ?>
230 <div class="tooltip-arrow" data-popper-arrow></div>
231 </div>
232 <?php
233 }
234 /**
235 * Renders a fancy radio button component.
236 *
237 * @param string $value The value attribute for the radio button.
238 * @param string $id The id attribute for the radio button.
239 * @param string $name The name attribute for the radio button.
240 * @param bool $checked Whether the radio button is checked.
241 * @param string $label The label text for the radio button.
242 * @param string $text Additional text to display under the label.
243 */
244 public function render_fancy_radio( $value, $id, $name, $checked, $label, $text ) { ?>
245 <div class="fancy-radio-container <?php echo $checked ? "selected" : ""; ?>" data-value="<?php echo $value; ?>">
246 <div class="fancy-radio <?php echo $checked ? "selected" : ""; ?>"><span class="input-fancy-radio"></span></div>
247 <label for="<?php echo $id; ?>"><?php echo $label; ?>
248 <p><?php echo $text; ?></p>
249 </label>
250 </div>
251 <?php
252 }
253 }
254