AutoPurge.php
4 months ago
BeaverBuilder.php
4 months ago
CPTOptimization.php
4 months ago
CacheWarmup.php
4 months ago
CartCache.php
4 months ago
Components.php
4 months ago
EditorClearCache.php
4 months ago
GeneratePreview.php
7 months ago
HTMLCompression.php
4 months ago
Logger.php
4 months ago
OptimizationLevel.php
4 months ago
Optimizations.php
4 months ago
PurgeCache.php
4 months ago
Shortcodes.php
4 months ago
StockRefresh.php
4 months ago
Subscription.php
4 months ago
SystemReport.php
4 months ago
TestMode.php
4 months ago
Components.php
248 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|string $dismissBy by option or transient. |
| 67 | * @param bool|array $app_notification An array containing 'end_date' and 'id' for app-specific notifications. If false, no app-specific notification is rendered. |
| 68 | * |
| 69 | * @return void |
| 70 | */ |
| 71 | public function render_notification( $msg, $type = false, $title = false, $actions = false, $classes = false, $dismissibleId = false, $dismissBy = false, $app_notification = false ) { |
| 72 | $default_classes = [ 'nitro-notification', 'notification-' . $type ]; |
| 73 | |
| 74 | if ( ! $title ) { |
| 75 | $default_classes[] = 'compact'; |
| 76 | } |
| 77 | if ( $app_notification ) { |
| 78 | $default_classes[] = 'app-notification'; |
| 79 | } |
| 80 | |
| 81 | if ( ! $type ) { |
| 82 | $type = 'info'; |
| 83 | } |
| 84 | |
| 85 | if ( $dismissibleId ) { |
| 86 | |
| 87 | $classes[] = 'is-dismissible'; |
| 88 | |
| 89 | if ( $dismissBy === 'option' ) { |
| 90 | $classes[] = 'dismiss-by-option'; |
| 91 | $notices = get_option( 'nitropack-dismissed-notices' ); |
| 92 | /* Don't display if dismissed by option */ |
| 93 | if ( $notices && is_array( $notices ) && in_array( $dismissibleId, $notices ) ) { |
| 94 | return; |
| 95 | } |
| 96 | } |
| 97 | if ( $dismissBy === 'transient' ) { |
| 98 | $classes[] = 'dismiss-by-transient'; |
| 99 | $notice = get_transient( $dismissibleId ); |
| 100 | /* Don't display if dismissed by transient and the time has passed */ |
| 101 | if ( $notice && time() < $notice ) { |
| 102 | return; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | } |
| 107 | |
| 108 | if ( $classes && is_array( $classes ) ) { |
| 109 | $default_classes = array_merge( $default_classes, $classes ); |
| 110 | } |
| 111 | $default_classes = implode( ' ', $default_classes ); |
| 112 | |
| 113 | echo '<div class="' . $default_classes . '">'; |
| 114 | ?> |
| 115 | <div class="notification-inner"> |
| 116 | <div class="title-msg"> |
| 117 | <div class="title-wrapper"> |
| 118 | <?php if ( $title ) { |
| 119 | echo $this->get_icon_notification( $type ); |
| 120 | echo '<h5 class="title">' . $title . '</h5>'; |
| 121 | } ?> |
| 122 | </div> |
| 123 | <div class="msg"> |
| 124 | <?php if ( ! $title ) { |
| 125 | echo $this->get_icon_notification( $type ); |
| 126 | } |
| 127 | echo $msg; |
| 128 | ?> |
| 129 | </div> |
| 130 | </div> |
| 131 | <div class="col-span-4 actions"> |
| 132 | <?php |
| 133 | if ( $actions ) { |
| 134 | echo $actions; |
| 135 | } |
| 136 | if ( isset( $app_notification['cta_details_list'] ) && is_array( $app_notification['cta_details_list'] ) ) { |
| 137 | foreach ( $app_notification['cta_details_list'] as $cta ) { |
| 138 | echo '<a href="' . esc_url( $cta['url'] ) . '" class="btn btn-' . esc_attr( $app_notification['type'] ) . '" target="_blank">' . esc_html( $cta['text'] ) . '</a>'; |
| 139 | } |
| 140 | } |
| 141 | if ( $app_notification && $app_notification['end_date'] && $app_notification['id'] ) { |
| 142 | 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>'; |
| 143 | } else if ( $dismissibleId && $dismissBy === 'option' ) { |
| 144 | echo '<a class="btn btn-secondary btn-dismiss" data-dismissible-id="' . $dismissibleId . '">' . esc_html__( 'Dismiss', 'nitropack' ) . '</a>'; |
| 145 | } |
| 146 | |
| 147 | ?> |
| 148 | </div> |
| 149 | </div> |
| 150 | </div> |
| 151 | <?php } |
| 152 | /** |
| 153 | * Renders a toggle switch. |
| 154 | * |
| 155 | * @param string $id The ID attribute for the checkbox input. |
| 156 | * @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. |
| 157 | */ |
| 158 | public function render_toggle( $id, $value, $attr = [] ) { |
| 159 | $disabled = ! empty( $attr['disabled'] ) ? 'disabled' : ''; |
| 160 | ?> |
| 161 | <label class="inline-flex items-center cursor-pointer ml-auto"> |
| 162 | <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" : ""; ?>> |
| 163 | <div class="toggle"></div> |
| 164 | </label> |
| 165 | <?php |
| 166 | } |
| 167 | /** |
| 168 | * Renders a button or link element with specified attributes and options. |
| 169 | * |
| 170 | * @param array $args { |
| 171 | * Array of arguments for rendering the button or link. |
| 172 | * |
| 173 | * @type string $text The text to display inside the button or link. Default empty. |
| 174 | * @type string $type The type of element to render, either 'button' or 'link'. Default 'button'. |
| 175 | * @type string $classes CSS classes to apply to the element. Default 'btn btn-secondary'. |
| 176 | * @type string|null $href The URL for the link element. Only used if 'type' is 'link'. Default null. |
| 177 | * @type string $icon The filename of the icon to display inside the element. Default empty. |
| 178 | * @type array $attributes Additional HTML attributes to apply to the element. Default empty array. |
| 179 | * } |
| 180 | */ |
| 181 | public function render_button( $args ) { |
| 182 | $defaults = [ |
| 183 | 'text' => '', |
| 184 | 'type' => 'button', |
| 185 | 'classes' => 'btn btn-secondary', |
| 186 | 'href' => null, |
| 187 | 'icon' => '', |
| 188 | 'attributes' => [] |
| 189 | ]; |
| 190 | $options = wp_parse_args( $args, $defaults ); |
| 191 | $attrs = ''; |
| 192 | foreach ( $options['attributes'] as $key => $value ) { |
| 193 | $attrs .= sprintf( ' %s="%s"', esc_attr( $key ), esc_attr( $value ) ); |
| 194 | } |
| 195 | if ( $options['type'] === 'button' ) : ?> |
| 196 | <button <?php else : |
| 197 | echo '<a href="' . $options['href'] . '"'; ?> <?php endif; ?> |
| 198 | class="<?php echo esc_attr( $options['classes'] ); ?>" <?php echo $attrs; ?>> |
| 199 | <?php if ( $options['icon'] ) : ?> |
| 200 | <img src="<?php echo esc_url( $this->plugin_dir_url . 'view/images/' . $options['icon'] ); ?>" |
| 201 | class="inline-block mr-2" alt=""> |
| 202 | <?php endif; ?> |
| 203 | <span class="btn-text"><?php echo esc_html( $options['text'], 'nitropack' ); ?></span> |
| 204 | <?php if ( $options['type'] === 'button' ) : ?> |
| 205 | </button> |
| 206 | <?php else : ?> |
| 207 | </a> |
| 208 | <?php endif; ?> |
| 209 | <?php |
| 210 | } |
| 211 | /** |
| 212 | * Renders a tooltip with the specified text and icon. |
| 213 | * |
| 214 | * @param string $id The unique identifier for the tooltip. |
| 215 | * @param string $text The text content to display inside the tooltip. |
| 216 | * @param string $icon The filename of the icon to display (default is 'info.svg'). |
| 217 | */ |
| 218 | public function render_tooltip( $id, $text, $icon = 'info.svg' ) { ?> |
| 219 | <span class="tooltip-icon" data-tooltip-target="tooltip-<?php echo $id; ?>"> |
| 220 | <img src="<?php echo $this->plugin_dir_url . 'view/images/' . $icon; ?>"> |
| 221 | </span> |
| 222 | <div id="tooltip-<?php echo $id; ?>" role="tooltip" class="tooltip-container hidden"> |
| 223 | <?php echo wp_kses_post( $text ); ?> |
| 224 | <div class="tooltip-arrow" data-popper-arrow></div> |
| 225 | </div> |
| 226 | <?php |
| 227 | } |
| 228 | /** |
| 229 | * Renders a fancy radio button component. |
| 230 | * |
| 231 | * @param string $value The value attribute for the radio button. |
| 232 | * @param string $id The id attribute for the radio button. |
| 233 | * @param string $name The name attribute for the radio button. |
| 234 | * @param bool $checked Whether the radio button is checked. |
| 235 | * @param string $label The label text for the radio button. |
| 236 | * @param string $text Additional text to display under the label. |
| 237 | */ |
| 238 | public function render_fancy_radio( $value, $id, $name, $checked, $label, $text ) { ?> |
| 239 | <div class="fancy-radio-container <?php echo $checked ? "selected" : ""; ?>" data-value="<?php echo $value; ?>"> |
| 240 | <div class="fancy-radio <?php echo $checked ? "selected" : ""; ?>"><span class="input-fancy-radio"></span></div> |
| 241 | <label for="<?php echo $id; ?>"><?php echo $label; ?> |
| 242 | <p><?php echo $text; ?></p> |
| 243 | </label> |
| 244 | </div> |
| 245 | <?php |
| 246 | } |
| 247 | } |
| 248 |