fields
7 years ago
types
2 years ago
fontpack.php
7 years ago
loader.php
7 years ago
registry.php
7 years ago
registry.php
179 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Icon types registry |
| 4 | * |
| 5 | * @package Icon_Picker |
| 6 | * @author Dzikri Aziz <kvcrvt@gmail.com> |
| 7 | */ |
| 8 | |
| 9 | final class Icon_Picker_Types_Registry { |
| 10 | |
| 11 | /** |
| 12 | * Icon_Picker_Types_Registry singleton |
| 13 | * |
| 14 | * @static |
| 15 | * @since 0.1.0 |
| 16 | * @access protected |
| 17 | * @var Icon_Picker_Types_Registry |
| 18 | */ |
| 19 | protected static $instance; |
| 20 | |
| 21 | /** |
| 22 | * Base icon type class name |
| 23 | * |
| 24 | * @access protected |
| 25 | * @since 0.1.0 |
| 26 | * @var string |
| 27 | */ |
| 28 | protected $base_class = 'Icon_Picker_Type'; |
| 29 | |
| 30 | /** |
| 31 | * All types |
| 32 | * |
| 33 | * @access protected |
| 34 | * @since 0.1.0 |
| 35 | * @var array |
| 36 | */ |
| 37 | protected $types = array(); |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * Get instance |
| 42 | * |
| 43 | * @static |
| 44 | * @since 0.1.0 |
| 45 | * @param array $args Arguments. |
| 46 | * |
| 47 | * @return Icon_Picker_Types_Registry |
| 48 | */ |
| 49 | public static function instance( $args = array() ) { |
| 50 | if ( is_null( self::$instance ) ) { |
| 51 | self::$instance = new self( $args ); |
| 52 | } |
| 53 | |
| 54 | return self::$instance; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * Getter magic |
| 60 | * |
| 61 | * @since 0.1.0 |
| 62 | * @param string $name Property name. |
| 63 | * @return mixed NULL if attribute doesn't exist. |
| 64 | */ |
| 65 | public function __get( $name ) { |
| 66 | if ( isset( $this->$name ) ) { |
| 67 | return $this->$name; |
| 68 | } |
| 69 | |
| 70 | return null; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * Setter magic |
| 76 | * |
| 77 | * @since 0.1.0 |
| 78 | * @return bool |
| 79 | */ |
| 80 | public function __isset( $name ) { |
| 81 | return isset( $this->$name ); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /** |
| 86 | * Constructor |
| 87 | * |
| 88 | * @since 0.1.0 |
| 89 | * @access protected |
| 90 | * @return Icon_Picker_Types_Registry |
| 91 | */ |
| 92 | protected function __construct() { |
| 93 | /** |
| 94 | * Fires when Icon Picker types registry is ready |
| 95 | * |
| 96 | * @since 0.1.0 |
| 97 | * @param Icon_Picker_Types_Registry $this Icon_Picker_Types_Registry instance. |
| 98 | */ |
| 99 | do_action( 'icon_picker_types_registry_init', $this ); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /** |
| 104 | * Register icon type |
| 105 | * |
| 106 | * @since 0.1.0 |
| 107 | * @param Icon_Picker_Type $type Icon type. |
| 108 | * @return void |
| 109 | */ |
| 110 | public function add( Icon_Picker_Type $type ) { |
| 111 | if ( $this->is_valid_type( $type ) ) { |
| 112 | $this->types[ $type->id ] = $type; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /** |
| 118 | * Get icon type |
| 119 | * |
| 120 | * @since 0.1.0 |
| 121 | * @param string $id Icon type ID. |
| 122 | * @return mixed Icon type or NULL if it's not registered. |
| 123 | */ |
| 124 | public function get( $id ) { |
| 125 | if ( isset( $this->types[ $id ] ) ) { |
| 126 | return $this->types[ $id ]; |
| 127 | } |
| 128 | |
| 129 | return null; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * Check if icon type is valid |
| 135 | * |
| 136 | * @since 0.1.0 |
| 137 | * @param Icon_Picker_Type $type Icon type. |
| 138 | * @return bool |
| 139 | */ |
| 140 | protected function is_valid_type( Icon_Picker_Type $type ) { |
| 141 | foreach ( array( 'id', 'controller' ) as $var ) { |
| 142 | $value = $type->$var; |
| 143 | |
| 144 | if ( empty( $value ) ) { |
| 145 | trigger_error( esc_html( sprintf( 'Icon Picker: "%s" cannot be empty.', $var ) ) ); |
| 146 | return false; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if ( isset( $this->types[ $type->id ] ) ) { |
| 151 | trigger_error( esc_html( sprintf( 'Icon Picker: Icon type %s is already registered. Please use a different ID.', $type->id ) ) ); |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /** |
| 160 | * Get all icon types for JS |
| 161 | * |
| 162 | * @since 0.1.0 |
| 163 | * @return array |
| 164 | */ |
| 165 | public function get_types_for_js() { |
| 166 | $types = array(); |
| 167 | $names = array(); |
| 168 | |
| 169 | foreach ( $this->types as $type ) { |
| 170 | $types[ $type->id ] = $type->get_props(); |
| 171 | $names[ $type->id ] = $type->name; |
| 172 | } |
| 173 | |
| 174 | array_multisort( $names, SORT_ASC, $types ); |
| 175 | |
| 176 | return $types; |
| 177 | } |
| 178 | } |
| 179 |