PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.17.6
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.17.6
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
Components.php
201 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', 'error', 'success'). Defaults to 'info'.
62 * @param bool|string $dismissID The ID used to dismiss the notification. If false, the notification is not dismissible.
63 * @param bool|string $title The title of the notification. If false, the notification is displayed without a title.
64 * @param bool|array $app_notification An array containing 'end_date' and 'id' for app-specific notifications. If false, no app-specific notification is rendered.
65 * @param bool|string $dismissibleId The ID used for dismissing the notification via AJAX. If false, no AJAX dismissible functionality is added.
66 *
67 * @return void
68 */
69 public function render_notification( $msg, $type, $dismissID = false, $title = false, $app_notification = false, $dismissibleId = false ) {
70 if ( ! $type )
71 $type = 'info';
72 if ( ! $title )
73 $classes = 'compact';
74 if ( $dismissID ) {
75 if ( ! empty( $_COOKIE[ "dismissed_notice_" . $dismissID ] ) )
76 return;
77 echo '<div class="nitro-notification notification-' . $type . ' ' . $classes . ' is-dismissible" data-dismissible-id="' . $dismissibleId . '">';
78 } else {
79 echo '<div class="nitro-notification notification-' . $type . ' ' . $classes . '">';
80 } ?>
81 <div class="notification-inner">
82 <div class="title-msg">
83 <div class="title-wrapper">
84 <?php if ( $title ) {
85 echo $this->get_icon_notification( $type );
86 echo $title;
87 } ?>
88 </div>
89 <div class="msg">
90 <?php if ( ! $title ) {
91 echo $this->get_icon_notification( $type );
92 }
93 echo $msg;
94 ?>
95 </div>
96 </div>
97 <div class="col-span-4 ml-auto actions">
98 <?php if ( $app_notification ) {
99 echo '<a class="btn btn-secondary btn-dismiss rml_btn" data-notification_end="' . $app_notification['end_date'] . '" data-notification_id="' . $app_notification['id'] . '">' . esc_html__( 'Dismiss', 'nitropack' ) . '</a>';
100 } else if ( $dismissibleId ) {
101 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\').hide();">' . esc_html__( 'Dismiss', 'nitropack' ) . '</a>';
102 } ?>
103 </div>
104 </div>
105 </div>
106 <?php }
107 /**
108 * Renders a toggle switch.
109 *
110 * @param string $id The ID attribute for the checkbox input.
111 * @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.
112 */
113 public function render_toggle( $id, $value ) { ?>
114 <label class="inline-flex items-center cursor-pointer ml-auto">
115 <input type="checkbox" id="<?php echo $id; ?>" class="sr-only peer" <?php echo (int) $value > 0 ? "checked" : ""; ?>>
116 <div class="toggle"></div>
117 </label>
118 <?php
119 }
120 /**
121 * Renders a button or link element with specified attributes and options.
122 *
123 * @param array $args {
124 * Array of arguments for rendering the button or link.
125 *
126 * @type string $text The text to display inside the button or link. Default empty.
127 * @type string $type The type of element to render, either 'button' or 'link'. Default 'button'.
128 * @type string $classes CSS classes to apply to the element. Default 'btn btn-secondary'.
129 * @type string|null $href The URL for the link element. Only used if 'type' is 'link'. Default null.
130 * @type string $icon The filename of the icon to display inside the element. Default empty.
131 * @type array $attributes Additional HTML attributes to apply to the element. Default empty array.
132 * }
133 */
134 public function render_button( $args ) {
135 $defaults = [
136 'text' => '',
137 'type' => 'button',
138 'classes' => 'btn btn-secondary',
139 'href' => null,
140 'icon' => '',
141 'attributes' => []
142 ];
143 $options = wp_parse_args( $args, $defaults );
144 $attrs = '';
145 foreach ( $options['attributes'] as $key => $value ) {
146 $attrs .= sprintf( ' %s="%s"', esc_attr( $key ), esc_attr( $value ) );
147 }
148 if ( $options['type'] === 'button' ) : ?>
149 <button <?php else :
150 echo '<a href="' . $options['href'] . '"'; ?> <?php endif; ?>
151 class="<?php echo esc_attr( $options['classes'] ); ?>" <?php echo $attrs; ?>>
152 <?php if ( $options['icon'] ) : ?>
153 <img src="<?php echo esc_url( $this->plugin_dir_url . 'view/images/' . $options['icon'] ); ?>"
154 class="inline-block mr-2" alt="">
155 <?php endif; ?>
156 <span class="btn-text"><?php echo esc_html( $options['text'] ); ?></span>
157 <?php if ( $options['type'] === 'button' ) : ?>
158 </button>
159 <?php else : ?>
160 </a>
161 <?php endif; ?>
162 <?php
163 }
164 /**
165 * Renders a tooltip with the specified text and icon.
166 *
167 * @param string $id The unique identifier for the tooltip.
168 * @param string $text The text content to display inside the tooltip.
169 * @param string $icon The filename of the icon to display (default is 'info.svg').
170 */
171 public function render_tooltip( $id, $text, $icon = 'info.svg' ) { ?>
172 <span class="tooltip-icon" data-tooltip-target="tooltip-<?php echo $id; ?>">
173 <img src="<?php echo $this->plugin_dir_url . 'view/images/' . $icon; ?>">
174 </span>
175 <div id="tooltip-<?php echo $id; ?>" role="tooltip" class="tooltip-container hidden">
176 <?php echo wp_kses_post( $text ); ?>
177 <div class="tooltip-arrow" data-popper-arrow></div>
178 </div>
179 <?php
180 }
181 /**
182 * Renders a fancy radio button component.
183 *
184 * @param string $value The value attribute for the radio button.
185 * @param string $id The id attribute for the radio button.
186 * @param string $name The name attribute for the radio button.
187 * @param bool $checked Whether the radio button is checked.
188 * @param string $label The label text for the radio button.
189 * @param string $text Additional text to display under the label.
190 */
191 public function render_fancy_radio( $value, $id, $name, $checked, $label, $text ) { ?>
192 <div class="fancy-radio-container <?php echo $checked ? "selected" : ""; ?>" data-value="<?php echo $value; ?>">
193 <div class="fancy-radio <?php echo $checked ? "selected" : ""; ?>"><span class="input-fancy-radio"></span></div>
194 <label for="<?php echo $id; ?>"><?php echo $label; ?>
195 <p><?php echo $text; ?></p>
196 </label>
197 </div>
198 <?php
199 }
200 }
201