fields
7 years ago
types
2 years ago
fontpack.php
7 years ago
loader.php
7 years ago
registry.php
7 years ago
loader.php
336 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Icon Picker Loader |
| 5 | * |
| 6 | * @package Icon_Picker |
| 7 | * @author Dzikri Aziz <kvcrvt@gmail.com> |
| 8 | */ |
| 9 | |
| 10 | final class Icon_Picker_Loader { |
| 11 | |
| 12 | /** |
| 13 | * Icon_Picker_Loader singleton |
| 14 | * |
| 15 | * @static |
| 16 | * @since 0.1.0 |
| 17 | * @access protected |
| 18 | * @var Icon_Picker_Loader |
| 19 | */ |
| 20 | protected static $instance; |
| 21 | |
| 22 | /** |
| 23 | * Script IDs |
| 24 | * |
| 25 | * @since 0.1.0 |
| 26 | * @access protected |
| 27 | * @var array |
| 28 | */ |
| 29 | protected $script_ids = array(); |
| 30 | |
| 31 | /** |
| 32 | * Stylesheet IDs |
| 33 | * |
| 34 | * @since 0.1.0 |
| 35 | * @access protected |
| 36 | * @var array |
| 37 | */ |
| 38 | protected $style_ids = array(); |
| 39 | |
| 40 | /** |
| 41 | * Printed media templates |
| 42 | * |
| 43 | * @since 0.1.0 |
| 44 | * @access protected |
| 45 | * @var array |
| 46 | */ |
| 47 | protected $printed_templates = array(); |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * Setter magic |
| 52 | * |
| 53 | * @since 0.1.0 |
| 54 | * @param string $name Property name. |
| 55 | * @return bool |
| 56 | */ |
| 57 | public function __isset( $name ) { |
| 58 | return isset( $this->$name ); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Getter magic |
| 64 | * |
| 65 | * @since 0.1.0 |
| 66 | * @param string $name Property name. |
| 67 | * @return mixed NULL if attribute doesn't exist. |
| 68 | */ |
| 69 | public function __get( $name ) { |
| 70 | if ( isset( $this->$name ) ) { |
| 71 | return $this->$name; |
| 72 | } |
| 73 | |
| 74 | return null; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /** |
| 79 | * Get instance |
| 80 | * |
| 81 | * @since 0.1.0 |
| 82 | * @return Icon_Picker_Loader |
| 83 | */ |
| 84 | public static function instance() { |
| 85 | if ( is_null( self::$instance ) ) { |
| 86 | self::$instance = new self(); |
| 87 | } |
| 88 | |
| 89 | return self::$instance; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /** |
| 94 | * Constructor |
| 95 | * |
| 96 | * @since 0.1.0 |
| 97 | * @access protected |
| 98 | */ |
| 99 | protected function __construct() { |
| 100 | $this->register_assets(); |
| 101 | |
| 102 | add_filter( 'media_view_strings', array( $this, '_media_view_strings' ) ); |
| 103 | |
| 104 | /** |
| 105 | * Fires when Icon Picker loader is ready |
| 106 | * |
| 107 | * @since 0.1.0 |
| 108 | * @param Icon_Picker_Loader $this Icon_Picker_Loader instance. |
| 109 | */ |
| 110 | do_action( 'icon_picker_loader_init', $this ); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * Add script |
| 116 | * |
| 117 | * @since 0.1.0 |
| 118 | * @param string $script_id Script ID. |
| 119 | * @return bool |
| 120 | */ |
| 121 | public function add_script( $script_id ) { |
| 122 | if ( wp_script_is( $script_id, 'registered' ) ) { |
| 123 | $this->script_ids[] = $script_id; |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /** |
| 133 | * Add stylesheet |
| 134 | * |
| 135 | * @since 0.1.0 |
| 136 | * @param string $stylesheet_id Stylesheet ID. |
| 137 | * @return bool |
| 138 | */ |
| 139 | public function add_style( $stylesheet_id ) { |
| 140 | if ( wp_style_is( $stylesheet_id, 'registered' ) ) { |
| 141 | $this->style_ids[] = $stylesheet_id; |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /** |
| 151 | * Register scripts & styles |
| 152 | * |
| 153 | * @since 0.1.0 |
| 154 | * @since 0.5.0 Use webpack dev server's URL as the asset URL. |
| 155 | * @access protected |
| 156 | * @return void |
| 157 | */ |
| 158 | protected function register_assets() { |
| 159 | $icon_picker = Icon_Picker::instance(); |
| 160 | |
| 161 | if ( defined( 'ICON_PICKER_SCRIPT_DEBUG' ) && ICON_PICKER_SCRIPT_DEBUG ) { |
| 162 | $assets_url = '//localhost:8080'; |
| 163 | $suffix = ''; |
| 164 | } else { |
| 165 | $assets_url = $icon_picker->url; |
| 166 | $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
| 167 | } |
| 168 | |
| 169 | wp_register_script( |
| 170 | 'icon-picker', |
| 171 | "{$assets_url}/js/icon-picker{$suffix}.js", |
| 172 | array( 'media-views' ), |
| 173 | Icon_Picker::VERSION, |
| 174 | true |
| 175 | ); |
| 176 | $this->add_script( 'icon-picker' ); |
| 177 | |
| 178 | wp_register_style( |
| 179 | 'icon-picker', |
| 180 | "{$icon_picker->url}/css/icon-picker{$suffix}.css", |
| 181 | false, |
| 182 | Icon_Picker::VERSION |
| 183 | ); |
| 184 | $this->add_style( 'icon-picker' ); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | /** |
| 189 | * Load admin functionalities |
| 190 | * |
| 191 | * @since 0.1.0 |
| 192 | * @return void |
| 193 | */ |
| 194 | public function load() { |
| 195 | if ( ! is_admin() ) { |
| 196 | _doing_it_wrong( |
| 197 | __METHOD__, |
| 198 | 'It should only be called on admin pages.', |
| 199 | esc_html( Icon_Picker::VERSION ) |
| 200 | ); |
| 201 | |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | if ( ! did_action( 'icon_picker_loader_init' ) ) { |
| 206 | _doing_it_wrong( |
| 207 | __METHOD__, |
| 208 | sprintf( |
| 209 | 'It should not be called until the %s hook.', |
| 210 | '<code>icon_picker_loader_init</code>' |
| 211 | ), |
| 212 | esc_html( Icon_Picker::VERSION ) |
| 213 | ); |
| 214 | |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | add_action( 'admin_enqueue_scripts', array( $this, '_enqueue_assets' ) ); |
| 219 | add_action( 'print_media_templates', array( $this, '_media_templates' ) ); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /** |
| 224 | * Filter media view strings |
| 225 | * |
| 226 | * @since 0.1.0 |
| 227 | * @wp_hook filter media_view_strings |
| 228 | * |
| 229 | * @param array $strings Media view strings. |
| 230 | * |
| 231 | * @return array |
| 232 | */ |
| 233 | public function _media_view_strings( $strings ) { |
| 234 | $strings['iconPicker'] = array( |
| 235 | 'frameTitle' => __( 'Icon Picker', 'icon-picker' ), |
| 236 | 'allFilter' => __( 'All', 'icon-picker' ), |
| 237 | 'selectIcon' => __( 'Select Icon', 'icon-picker' ), |
| 238 | ); |
| 239 | |
| 240 | return $strings; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /** |
| 245 | * Enqueue scripts & styles |
| 246 | * |
| 247 | * @since 0.1.0 |
| 248 | * @wp_hook action admin_enqueue_scripts |
| 249 | * @return void |
| 250 | */ |
| 251 | public function _enqueue_assets() { |
| 252 | $icon_picker = Icon_Picker::instance(); |
| 253 | |
| 254 | wp_localize_script( |
| 255 | 'icon-picker', |
| 256 | 'iconPicker', |
| 257 | array( |
| 258 | 'types' => $icon_picker->registry->get_types_for_js(), |
| 259 | ) |
| 260 | ); |
| 261 | |
| 262 | // Some pages don't call this by default, so let's make sure. |
| 263 | wp_enqueue_media(); |
| 264 | |
| 265 | foreach ( $this->script_ids as $script_id ) { |
| 266 | wp_enqueue_script( $script_id ); |
| 267 | } |
| 268 | |
| 269 | foreach ( $this->style_ids as $style_id ) { |
| 270 | wp_enqueue_style( $style_id ); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Fires when admin functionality is loaded |
| 275 | * |
| 276 | * @since 0.1.0 |
| 277 | * @param Icon_Picker $icon_picker Icon_Picker instance. |
| 278 | */ |
| 279 | do_action( 'icon_picker_admin_loaded', $icon_picker ); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | /** |
| 284 | * Media templates |
| 285 | * |
| 286 | * @since 0.1.0 |
| 287 | * @wp_hook action print_media_templates |
| 288 | * @return void |
| 289 | */ |
| 290 | public function _media_templates() { |
| 291 | $icon_picker = Icon_Picker::instance(); |
| 292 | |
| 293 | foreach ( $icon_picker->registry->types as $type ) { |
| 294 | if ( empty( $type->templates ) ) { |
| 295 | continue; |
| 296 | } |
| 297 | |
| 298 | $template_id_prefix = "tmpl-iconpicker-{$type->template_id}"; |
| 299 | if ( in_array( $template_id_prefix, $this->printed_templates, true ) ) { |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | foreach ( $type->templates as $template_id_suffix => $template ) { |
| 304 | $this->_print_template( "{$template_id_prefix}-{$template_id_suffix}", $template ); |
| 305 | } |
| 306 | |
| 307 | $this->printed_templates[] = $template_id_prefix; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Fires after all media templates have been printed |
| 312 | * |
| 313 | * @since 0.1.0 |
| 314 | * @param Icon_Picker $icon_picker Icon Picker instance. |
| 315 | */ |
| 316 | do_action( 'icon_picker_print_media_templates', $icon_picker ); |
| 317 | } |
| 318 | |
| 319 | |
| 320 | /** |
| 321 | * Print media template |
| 322 | * |
| 323 | * @since 0.1.0 |
| 324 | * @param string $id Template ID. |
| 325 | * @param string $template Media template HTML. |
| 326 | * @return void |
| 327 | */ |
| 328 | protected function _print_template( $id, $template ) { |
| 329 | ?> |
| 330 | <script type="text/html" id="<?php echo esc_attr( $id ) ?>"> |
| 331 | <?php echo $template; // xss ok ?> |
| 332 | </script> |
| 333 | <?php |
| 334 | } |
| 335 | } |
| 336 |