| 1 |
<?php |
| 2 |
/** |
| 3 |
* Field: adminify_icon |
| 4 |
* |
| 5 |
* Options-framework field that renders the shared WP Adminify icon picker |
| 6 |
* (assets/vendors/adminify-icon-picker) — the same picker the Menu Editor uses — |
| 7 |
* instead of the framework's built-in Font Awesome modal. |
| 8 |
* |
| 9 |
* The framework resolves a field type by looking for a global |
| 10 |
* `ADMINIFY_Field_{type}` class before falling back to its own |
| 11 |
* `fields/{type}/{type}.php`, so declaring this class here adds the type without |
| 12 |
* modifying Libs/adminify-framework. |
| 13 |
* |
| 14 |
* Two value shapes are stored: |
| 15 |
* - an icon class, e.g. "dashicons dashicons-chart-bar" |
| 16 |
* - a custom (uploaded) icon as "{attachment_id},{url}" |
| 17 |
* |
| 18 |
* @package Adminify |
| 19 |
*/ |
| 20 |
|
| 21 |
if ( ! defined( 'ABSPATH' ) ) { |
| 22 |
die; |
| 23 |
} |
| 24 |
|
| 25 |
if ( ! class_exists( 'ADMINIFY_Fields' ) ) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
if ( ! class_exists( 'ADMINIFY_Field_adminify_icon' ) ) { |
| 30 |
|
| 31 |
class ADMINIFY_Field_adminify_icon extends ADMINIFY_Fields { |
| 32 |
|
| 33 |
public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { |
| 34 |
parent::__construct( $field, $value, $unique, $where, $parent ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Whether a stored value is a custom uploaded icon ("{id},{url}"). |
| 39 |
* |
| 40 |
* @param string $value Stored field value. |
| 41 |
* @return bool |
| 42 |
*/ |
| 43 |
public static function is_custom_icon( $value ) { |
| 44 |
return ( is_string( $value ) && (bool) preg_match( '/^[^,]*,\s*https?:\/\//i', $value ) ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* URL part of a custom icon value. |
| 49 |
* |
| 50 |
* @param string $value Stored field value. |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
public static function custom_icon_url( $value ) { |
| 54 |
$parts = explode( ',', (string) $value, 2 ); |
| 55 |
|
| 56 |
return isset( $parts[1] ) ? trim( $parts[1] ) : ''; |
| 57 |
} |
| 58 |
|
| 59 |
public function render() { |
| 60 |
|
| 61 |
$args = wp_parse_args( |
| 62 |
$this->field, |
| 63 |
array( |
| 64 |
'button_title' => esc_html__( 'Icon Library', 'adminify' ), |
| 65 |
'remove_title' => esc_html__( 'None', 'adminify' ), |
| 66 |
) |
| 67 |
); |
| 68 |
|
| 69 |
echo wp_kses_post( $this->field_before() ); |
| 70 |
|
| 71 |
$value = (string) $this->value; |
| 72 |
$custom = self::is_custom_icon( $value ); |
| 73 |
|
| 74 |
// Markup mirrors the Menu Editor's picker wrapper: the picker binds |
| 75 |
// delegated handlers to .select-icon / .icon-none and resolves the |
| 76 |
// owning field with .closest('.icon-picker-wrap'). |
| 77 |
echo '<div class="icon-picker-wrap adminify-icon-picker-input icon-select-button is-clickable is-pulled-left">'; |
| 78 |
echo '<ul class="icon-picker">'; |
| 79 |
echo '<li class="icon-none" title="' . esc_attr( $args['remove_title'] ) . '"><i class="dashicons dashicons-dismiss"></i></li>'; |
| 80 |
echo '<li class="select-icon' . ( $custom ? ' custom-icon' : '' ) . '" title="' . esc_attr( $args['button_title'] ) . '">'; |
| 81 |
|
| 82 |
if ( $custom ) { |
| 83 |
echo '<i class=""><img width="24" height="24" src="' . esc_url( self::custom_icon_url( $value ) ) . '" alt="" /></i>'; |
| 84 |
} elseif ( '' !== $value ) { |
| 85 |
echo '<i class="' . esc_attr( $value ) . '"></i>'; |
| 86 |
} else { |
| 87 |
echo '<i>' . esc_html__( 'Select Icon', 'adminify' ) . '</i>'; |
| 88 |
} |
| 89 |
|
| 90 |
echo '</li>'; |
| 91 |
echo '</ul>'; |
| 92 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name() ) . '" value="' . esc_attr( $value ) . '" class="adminify-icon-value"' . $this->field_attributes() . ' />'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- field_attributes() escapes each attribute via esc_attr(). |
| 93 |
echo '</div>'; |
| 94 |
|
| 95 |
echo wp_kses_post( $this->field_after() ); |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
} |
| 100 |
} |
| 101 |
|