PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.18.0
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.18.0
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
Components.php 1 year ago Logger.php 1 year ago Shortcodes.php 1 year ago TestMode.php 1 year ago
Components.php
229 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 * Components constructor.
19 *
20 * Initializes the plugin directory URL.
21 */
22 public function __construct() {
23 $this->plugin_dir_url = plugin_dir_url( NITROPACK_FILE );
24 }
25 /**
26 * Get the URL of the notification icon based on the type.
27 *
28 * @param string $type The type of notification (success, danger, warning, info).
29 * @return string The URL of the notification icon.
30 */
31 private function get_icon_notification_url( $type ) {
32 $icon_path = $this->plugin_dir_url . 'view/images/';
33 switch ( $type ) {
34 case 'success':
35 return $icon_path . 'check.svg';
36 case 'error':
37 return $icon_path . 'alert-triangle.svg';
38 case 'warning':
39 return $icon_path . 'info.svg';
40 case 'info':
41 return $icon_path . 'bell.svg';
42 default:
43 return $icon_path . 'bell.svg';
44 }
45 }
46 /**
47 * Get the HTML for the notification icon based on the type.
48 *
49 * @param string $type The type of notification (success, danger, warning, info).
50 * @return string The HTML string for the notification icon.
51 */
52 private function get_icon_notification( $type ) {
53 $icon_path = $this->get_icon_notification_url( $type );
54 return '<img src="' . $icon_path . '" class="icon" width="16" height="16" alt="icon-' . $type . '">';
55 }
56
57 /**
58 * Renders a notification message.
59 *
60 * @param string $msg The notification message to display.
61 * @param string $type The type of notification (e.g., 'info', 'warning, 'error', 'success' and 'promo'. Defaults to 'info'.
62 * @param bool|string $title The title of the notification. If false, the notification is displayed without a title.
63 * @param bool|string $actions The actions of the notification, mostly buttons (CTAs).
64 * @param bool|array $classes The css classes of the notification. If present, an array must be used.
65 * @param bool|string $dismissibleId The ID used for dismissing the notification as cookie in np_notices.js
66 * @param bool|array $app_notification An array containing 'end_date' and 'id' for app-specific notifications. If false, no app-specific notification is rendered.
67 *
68 * @return void
69 */
70 public function render_notification( $msg, $type = false, $title = false, $actions = false, $classes = false, $dismissibleId = false, $app_notification = false ) {
71 $default_classes = [ 'nitro-notification', 'notification-' . $type ];
72 if ( ! $title ) {
73 $default_classes[] = 'compact';
74 }
75 if ( $app_notification ) {
76 $default_classes[] = 'app-notification';
77 }
78 if ( $classes && is_array( $classes ) ) {
79 $default_classes = array_merge( $default_classes, $classes );
80
81 }
82 $default_classes = implode( ' ', $default_classes );
83 if ( ! $type ) {
84 $type = 'info';
85 }
86
87 /* Don't display if dismissed by cookie */
88 if ( $dismissibleId ) {
89 if ( ! empty( $_COOKIE[ "dismissed_notice_" . $dismissibleId ] ) && $_COOKIE[ "dismissed_notice_" . $dismissibleId ] === '1' ) {
90 return;
91 }
92
93 echo '<div class="' . $default_classes . ' is-dismissible" data-dismissible-id="' . $dismissibleId . '">';
94 } else {
95 echo '<div class="' . $default_classes . '">';
96 } ?>
97 <div class="notification-inner">
98 <div class="title-msg">
99 <div class="title-wrapper">
100 <?php if ( $title ) {
101 echo $this->get_icon_notification( $type );
102 echo '<h5 class="title">' . $title . '</h5>';
103 } ?>
104 </div>
105 <div class="msg">
106 <?php if ( ! $title ) {
107 echo $this->get_icon_notification( $type );
108 }
109 echo $msg;
110 ?>
111 </div>
112 </div>
113 <div class="col-span-4 actions">
114 <?php
115 if ( $actions ) {
116 echo $actions;
117
118 }
119 if ( isset( $app_notification['cta_details_list'] ) && is_array( $app_notification['cta_details_list'] ) ) {
120 foreach ( $app_notification['cta_details_list'] as $cta ) {
121 echo '<a href="' . esc_url( $cta['url'] ) . '" class="btn btn-' . esc_attr( $app_notification['type'] ) . '" target="_blank">' . esc_html( $cta['text'] ) . '</a>';
122 }
123 }
124 if ( $app_notification && $app_notification['end_date'] && $app_notification['id'] ) {
125 echo '<a class="btn btn-secondary btn-dismiss" data-notification_end="' . $app_notification['end_date'] . '" data-notification_id="' . $app_notification['id'] . '">' . esc_html__( 'Dismiss', 'nitropack' ) . '</a>';
126 } else if ( $dismissibleId ) {
127 echo '<a class="btn btn-secondary btn-dismiss" onclick="jQuery.post(ajaxurl, {action: \'' . $dismissibleId . '\', nonce: \'' . wp_create_nonce( NITROPACK_NONCE ) . '\'}); jQuery(this).closest(\'.is-dismissible\').remove();">' . esc_html__( 'Dismiss', 'nitropack' ) . '</a>';
128 }
129
130 ?>
131 </div>
132 </div>
133 </div>
134 <?php }
135 /**
136 * Renders a toggle switch.
137 *
138 * @param string $id The ID attribute for the checkbox input.
139 * @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.
140 */
141 public function render_toggle( $id, $value ) { ?>
142 <label class="inline-flex items-center cursor-pointer ml-auto">
143 <input type="checkbox" id="<?php echo $id; ?>" class="sr-only peer" <?php echo (int) $value > 0 ? "checked" : ""; ?>>
144 <div class="toggle"></div>
145 </label>
146 <?php
147 }
148 /**
149 * Renders a button or link element with specified attributes and options.
150 *
151 * @param array $args {
152 * Array of arguments for rendering the button or link.
153 *
154 * @type string $text The text to display inside the button or link. Default empty.
155 * @type string $type The type of element to render, either 'button' or 'link'. Default 'button'.
156 * @type string $classes CSS classes to apply to the element. Default 'btn btn-secondary'.
157 * @type string|null $href The URL for the link element. Only used if 'type' is 'link'. Default null.
158 * @type string $icon The filename of the icon to display inside the element. Default empty.
159 * @type array $attributes Additional HTML attributes to apply to the element. Default empty array.
160 * }
161 */
162 public function render_button( $args ) {
163 $defaults = [
164 'text' => '',
165 'type' => 'button',
166 'classes' => 'btn btn-secondary',
167 'href' => null,
168 'icon' => '',
169 'attributes' => []
170 ];
171 $options = wp_parse_args( $args, $defaults );
172 $attrs = '';
173 foreach ( $options['attributes'] as $key => $value ) {
174 $attrs .= sprintf( ' %s="%s"', esc_attr( $key ), esc_attr( $value ) );
175 }
176 if ( $options['type'] === 'button' ) : ?>
177 <button <?php else :
178 echo '<a href="' . $options['href'] . '"'; ?> <?php endif; ?>
179 class="<?php echo esc_attr( $options['classes'] ); ?>" <?php echo $attrs; ?>>
180 <?php if ( $options['icon'] ) : ?>
181 <img src="<?php echo esc_url( $this->plugin_dir_url . 'view/images/' . $options['icon'] ); ?>"
182 class="inline-block mr-2" alt="">
183 <?php endif; ?>
184 <span class="btn-text"><?php echo esc_html( $options['text'] ); ?></span>
185 <?php if ( $options['type'] === 'button' ) : ?>
186 </button>
187 <?php else : ?>
188 </a>
189 <?php endif; ?>
190 <?php
191 }
192 /**
193 * Renders a tooltip with the specified text and icon.
194 *
195 * @param string $id The unique identifier for the tooltip.
196 * @param string $text The text content to display inside the tooltip.
197 * @param string $icon The filename of the icon to display (default is 'info.svg').
198 */
199 public function render_tooltip( $id, $text, $icon = 'info.svg' ) { ?>
200 <span class="tooltip-icon" data-tooltip-target="tooltip-<?php echo $id; ?>">
201 <img src="<?php echo $this->plugin_dir_url . 'view/images/' . $icon; ?>">
202 </span>
203 <div id="tooltip-<?php echo $id; ?>" role="tooltip" class="tooltip-container hidden">
204 <?php echo wp_kses_post( $text ); ?>
205 <div class="tooltip-arrow" data-popper-arrow></div>
206 </div>
207 <?php
208 }
209 /**
210 * Renders a fancy radio button component.
211 *
212 * @param string $value The value attribute for the radio button.
213 * @param string $id The id attribute for the radio button.
214 * @param string $name The name attribute for the radio button.
215 * @param bool $checked Whether the radio button is checked.
216 * @param string $label The label text for the radio button.
217 * @param string $text Additional text to display under the label.
218 */
219 public function render_fancy_radio( $value, $id, $name, $checked, $label, $text ) { ?>
220 <div class="fancy-radio-container <?php echo $checked ? "selected" : ""; ?>" data-value="<?php echo $value; ?>">
221 <div class="fancy-radio <?php echo $checked ? "selected" : ""; ?>"><span class="input-fancy-radio"></span></div>
222 <label for="<?php echo $id; ?>"><?php echo $label; ?>
223 <p><?php echo $text; ?></p>
224 </label>
225 </div>
226 <?php
227 }
228 }
229