class-hustle-custom-fonts-helper.php
4 years ago
class-hustle-layout-helper.php
1 month ago
class-hustle-palettes-helper.php
5 months ago
class-hustle-templates-helper.php
3 years ago
class-hustle-time-helper.php
5 years ago
class-hustle-layout-helper.php
388 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Hustle_Layout_Helper class. |
| 4 | * |
| 5 | * @package Hustle |
| 6 | * @since 4.2.0 |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Helper class for rendering markup on admin side. |
| 11 | * This is used along admin pages to standardize certain elements markup. |
| 12 | * |
| 13 | * @since 4.2.0 |
| 14 | */ |
| 15 | class Hustle_Layout_Helper { |
| 16 | |
| 17 | /** |
| 18 | * Instance of the class that controls the template. |
| 19 | * |
| 20 | * @since 4.2.0 |
| 21 | * @var Object |
| 22 | */ |
| 23 | private $admin; |
| 24 | |
| 25 | /** |
| 26 | * White labeling based on Dash Plugin Settings. |
| 27 | * |
| 28 | * @since 4.2.0 |
| 29 | * @var boolean |
| 30 | */ |
| 31 | private $is_branding_hidden = false; |
| 32 | |
| 33 | /** |
| 34 | * White labeling branding image. |
| 35 | * |
| 36 | * @since 4.4.7 |
| 37 | * @var string |
| 38 | */ |
| 39 | private $branding_image; |
| 40 | |
| 41 | /** Array list of quicktags for tinymce editor. |
| 42 | * |
| 43 | * @since 4.4.7 |
| 44 | * @var array |
| 45 | */ |
| 46 | private $tinymce_quicktags; |
| 47 | |
| 48 | /** |
| 49 | * To be removed. |
| 50 | * |
| 51 | * @var string something. |
| 52 | */ |
| 53 | public static $plugin_url; |
| 54 | |
| 55 | /** |
| 56 | * Flag for letting SUI doesn't run auto init selects to suiSelect. |
| 57 | * |
| 58 | * @var bool |
| 59 | */ |
| 60 | private static $dont_init_selects; |
| 61 | |
| 62 | /** |
| 63 | * Hustle_Layout_Helper class constructor. |
| 64 | * |
| 65 | * @since 4.2.0 |
| 66 | * @param object $referer The class that has the properties to access from within templates. |
| 67 | */ |
| 68 | public function __construct( $referer = null ) { |
| 69 | |
| 70 | self::$plugin_url = Opt_In::$plugin_url; |
| 71 | |
| 72 | $this->is_branding_hidden = apply_filters( 'wpmudev_branding_hide_branding', $this->is_branding_hidden ); |
| 73 | |
| 74 | // White label custom branding image. |
| 75 | $this->branding_image = apply_filters( 'wpmudev_branding_hero_image', null ); |
| 76 | |
| 77 | // Filter allowed HTML tags for the content. |
| 78 | $this->tinymce_quicktags = apply_filters( |
| 79 | 'hustle_tinymce_quicktags', |
| 80 | array( |
| 81 | 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close', |
| 82 | ) |
| 83 | ); |
| 84 | |
| 85 | /** |
| 86 | * Sets the referer class as a property. |
| 87 | * This allows us to access the referer class' properties if needed |
| 88 | * from the template files. |
| 89 | */ |
| 90 | // TODO maybe check if the referer has the two allowed classes. |
| 91 | if ( $referer ) { |
| 92 | $this->admin = $referer; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Gets the previously set referer. |
| 98 | * |
| 99 | * @since 4.2.0 |
| 100 | * @return object |
| 101 | */ |
| 102 | public function get_referer() { |
| 103 | if ( ! $this->admin ) { |
| 104 | return false; |
| 105 | } |
| 106 | return $this->admin; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns or echoes markup from the given $options array. |
| 111 | * Uses the file 'admin/commons/options' as the markup template. |
| 112 | * |
| 113 | * @since 4.2.0 |
| 114 | * |
| 115 | * @param array $options Array with the options that define the markup to be returned. |
| 116 | * @param boolean $return_value Whether to echo or return the markup. |
| 117 | * @return string |
| 118 | */ |
| 119 | public static function get_html_for_options( $options, $return_value = false ) { |
| 120 | $instance = new self(); |
| 121 | $html = ''; |
| 122 | foreach ( $options as $key => $option ) { |
| 123 | $html .= $instance->render( 'admin/commons/options', $option, $return_value ); |
| 124 | } |
| 125 | return $html; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Renders a view file with static call. |
| 130 | * |
| 131 | * @since 1.0 |
| 132 | * @since 4.2.0 Moved from Opt_In to this class. |
| 133 | * |
| 134 | * @param string $file Path to the view file. |
| 135 | * @param array $params Array whose keys will be variable names when within the view file. |
| 136 | * @param bool|false $return_value Whether to echo or return the contents. |
| 137 | * @return string |
| 138 | */ |
| 139 | public function render( $file, $params = array(), $return_value = false ) { |
| 140 | |
| 141 | extract( $params, EXTR_OVERWRITE ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract |
| 142 | |
| 143 | if ( $return_value ) { |
| 144 | ob_start(); |
| 145 | } |
| 146 | |
| 147 | $template_file = $this->locate_file( $file ); |
| 148 | if ( file_exists( $template_file ) ) { |
| 149 | include $template_file; |
| 150 | } |
| 151 | |
| 152 | if ( $return_value ) { |
| 153 | return ob_get_clean(); |
| 154 | } |
| 155 | |
| 156 | if ( ! empty( $params ) ) { |
| 157 | foreach ( $params as $param ) { |
| 158 | unset( $param ); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Locates a file and returns its path. |
| 165 | * |
| 166 | * @since 4.2.0 |
| 167 | * |
| 168 | * @param string $layout Layout name, which is used to create the file name. |
| 169 | * @return string |
| 170 | */ |
| 171 | public function locate_file( $layout ) { |
| 172 | // Assign $file to a variable which is unlikely to be used by users of the method. |
| 173 | $opt_in_to_be_file_name = $layout; |
| 174 | |
| 175 | $template_file = trailingslashit( Opt_In::$plugin_path ) . Opt_In::VIEWS_FOLDER . '/' . $opt_in_to_be_file_name . '.php'; |
| 176 | |
| 177 | if ( file_exists( $template_file ) ) { |
| 178 | return apply_filters( 'hustle_locate_file', $template_file, $layout ); |
| 179 | } |
| 180 | |
| 181 | $template_path = Opt_In::$template_path . $opt_in_to_be_file_name . '.php'; |
| 182 | if ( file_exists( $template_path ) ) { |
| 183 | return apply_filters( 'hustle_locate_file', $template_path, $layout ); |
| 184 | } |
| 185 | |
| 186 | $external_path = $opt_in_to_be_file_name . '.php'; |
| 187 | if ( file_exists( $external_path ) ) { |
| 188 | return apply_filters( 'hustle_locate_file', $external_path, $layout ); |
| 189 | } |
| 190 | |
| 191 | return apply_filters( 'hustle_locate_file', $opt_in_to_be_file_name, $layout ); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Renders html. |
| 196 | * |
| 197 | * @param string $content Content - HTML. |
| 198 | */ |
| 199 | public function render_html( $content ) { |
| 200 | $common_arrts = array( |
| 201 | 'id' => true, |
| 202 | 'data-*' => true, |
| 203 | 'title' => true, |
| 204 | 'sandbox' => true, |
| 205 | 'class' => true, |
| 206 | 'aria-hidden' => true, |
| 207 | 'aria-labelledby' => true, |
| 208 | 'aria-describedby' => true, |
| 209 | 'role' => true, |
| 210 | 'xmlns' => true, |
| 211 | 'xmlns:xlink' => true, |
| 212 | 'width' => true, |
| 213 | 'height' => true, |
| 214 | 'viewbox' => true, |
| 215 | 'type' => true, |
| 216 | 'name' => true, |
| 217 | 'value' => true, |
| 218 | 'checked' => true, |
| 219 | 'selected' => true, |
| 220 | 'placeholder' => true, |
| 221 | 'disabled' => true, |
| 222 | 'method' => true, |
| 223 | 'tabindex' => true, |
| 224 | ); |
| 225 | $allowed_html = wp_kses_allowed_html( 'post' ); |
| 226 | $allowed_tags = array_merge( |
| 227 | $allowed_html, |
| 228 | array( |
| 229 | 'iframe' => $common_arrts, |
| 230 | 'form' => $common_arrts, |
| 231 | 'svg' => $common_arrts, |
| 232 | 'defs' => true, |
| 233 | 'g' => array( |
| 234 | 'fill' => true, |
| 235 | 'fill-rule' => true, |
| 236 | 'clip-rule' => true, |
| 237 | 'd' => true, |
| 238 | ), |
| 239 | 'path' => array( |
| 240 | 'd' => true, |
| 241 | 'id' => true, |
| 242 | 'fill' => true, |
| 243 | 'fill-rule' => true, |
| 244 | ), |
| 245 | 'input' => $common_arrts, |
| 246 | 'select' => $common_arrts, |
| 247 | 'option' => $common_arrts, |
| 248 | ) |
| 249 | ); |
| 250 | |
| 251 | $allowed_tags = apply_filters( 'hustle_content_allowed_tags', $allowed_tags ); |
| 252 | |
| 253 | echo wp_kses( $content, $allowed_tags ); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Renders custom attributes within views templates. |
| 258 | * |
| 259 | * @since 1.0.0 |
| 260 | * @since 4.2.0 Moved from Opt_In to this class. |
| 261 | * @since 4.3.0 Removed the $echo parameter. |
| 262 | * |
| 263 | * @param array $html_options Attributes as an array to be renderd. |
| 264 | * @return string |
| 265 | */ |
| 266 | public function render_attributes( $html_options ) { |
| 267 | |
| 268 | if ( array() === $html_options ) { |
| 269 | return ''; |
| 270 | } |
| 271 | |
| 272 | $special_attributes = array( |
| 273 | 'async', |
| 274 | 'autofocus', |
| 275 | 'autoplay', |
| 276 | 'checked', |
| 277 | 'controls', |
| 278 | 'declare', |
| 279 | 'default', |
| 280 | 'defer', |
| 281 | 'disabled', |
| 282 | 'formnovalidate', |
| 283 | 'hidden', |
| 284 | 'ismap', |
| 285 | 'loop', |
| 286 | 'multiple', |
| 287 | 'muted', |
| 288 | 'nohref', |
| 289 | 'noresize', |
| 290 | 'novalidate', |
| 291 | 'open', |
| 292 | 'readonly', |
| 293 | 'required', |
| 294 | 'reversed', |
| 295 | 'scoped', |
| 296 | 'seamless', |
| 297 | 'selected', |
| 298 | 'typemustmatch', |
| 299 | ); |
| 300 | |
| 301 | foreach ( $html_options as $name => $value ) { |
| 302 | if ( in_array( $name, $special_attributes, true ) ) { |
| 303 | if ( $value ) { |
| 304 | echo ' ' . esc_attr( $name ) . '="' . esc_attr( $name ) . '"'; |
| 305 | } |
| 306 | } elseif ( null !== $value ) { |
| 307 | echo ' ' . esc_attr( $name ) . '="' . esc_attr( $value ) . '"'; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Renders a basic modal with the passed attributes. |
| 314 | * |
| 315 | * @since 4.2.0 |
| 316 | * @param array $arguments Arguments for the modal. Documented in the template file. |
| 317 | */ |
| 318 | private function render_modal( $arguments ) { |
| 319 | $this->render( '/admin/commons/modal-template', $arguments ); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Image function |
| 324 | * Return image element with 2x and 1x support. |
| 325 | * |
| 326 | * @since 4.3.1 |
| 327 | * |
| 328 | * @param string $image_path URL for the given image. |
| 329 | * @param string $image_suffix Image format, like png, jpg, etc. |
| 330 | * @param string $image_class Class for the image HTML element. |
| 331 | * @param string|bool $support Whether the image has retina support. |
| 332 | */ |
| 333 | private function hustle_image( $image_path, $image_suffix, $image_class, $support ) { |
| 334 | /* translators: Plugin name */ |
| 335 | $image_name = esc_html( sprintf( __( '%s image', 'hustle' ), Opt_In_Utils::get_plugin_name() ) ); |
| 336 | |
| 337 | echo '<img src="' . esc_url( $image_path . '.' . $image_suffix ) . '" alt="' . esc_attr( $image_name ) . '"'; |
| 338 | if ( true === $support || '2x' === $support ) { |
| 339 | echo ' srcset="' . esc_attr( $image_path . '.' . $image_suffix ) . ' 1x, ' . esc_attr( $image_path . '@2x.' . $image_suffix ) . ' 2x"'; |
| 340 | } |
| 341 | if ( '' !== $image_class ) { |
| 342 | echo ' class="' . esc_attr( $image_class ) . '"'; |
| 343 | } |
| 344 | echo ' aria-hidden="true">'; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Color Picker |
| 349 | * |
| 350 | * Return the correct color picker markup that's compatible with Shared UI 2.0 |
| 351 | * |
| 352 | * @since 4.3.1 |
| 353 | * |
| 354 | * @param string $id "id" attribute of the input. |
| 355 | * @param string $name "name" attribute of the input. |
| 356 | * @param string $alpha "false"/"true". Enables or disables the alpha selector in the colorpicker. |
| 357 | * @param bool $is_js_template whether this colorpicker will be filled via js templating. |
| 358 | * @param string $value Value to be used when js templating isn't used. |
| 359 | */ |
| 360 | private function sui_colorpicker( $id, $name, $alpha = 'false', $is_js_template = true, $value = false ) { |
| 361 | |
| 362 | $value = ( ! $is_js_template && $value ) ? $value : '{{ ' . $name . ' }}'; |
| 363 | |
| 364 | echo '<div class="sui-colorpicker-wrap"> |
| 365 | |
| 366 | <div class="sui-colorpicker" aria-hidden="true"> |
| 367 | <div class="sui-colorpicker-value"> |
| 368 | <span role="button"> |
| 369 | <span style="background-color: ' . esc_attr( $value ) . '"></span> |
| 370 | </span> |
| 371 | <input class="hustle-colorpicker-input" type="text" value="' . esc_attr( $value ) . '"/> |
| 372 | <button><span class="sui-icon-close" aria-hidden="true"></span></button> |
| 373 | </div> |
| 374 | <button class="sui-button">' . esc_html__( 'Select', 'hustle' ) . '</button> |
| 375 | </div> |
| 376 | |
| 377 | <input type="text" |
| 378 | name="' . esc_attr( $name ) . '" |
| 379 | value="' . esc_attr( $value ) . '" |
| 380 | id="' . esc_attr( $id ) . '" |
| 381 | class="sui-colorpicker-input" |
| 382 | data-alpha-enabled="' . esc_attr( $alpha ) . '" |
| 383 | data-attribute="' . esc_attr( $name ) . '" /> |
| 384 | |
| 385 | </div>'; |
| 386 | } |
| 387 | } |
| 388 |