base.php
105 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! function_exists( 'wp_get_attachment_image_url' ) ) { |
| 4 | /** |
| 5 | * Get the URL of an image attachment. |
| 6 | * |
| 7 | * @since WordPress 4.4.0 |
| 8 | * |
| 9 | * @param int $attachment_id Image attachment ID. |
| 10 | * @param string|array $size Optional. Image size to retrieve. Accepts any valid image size, or an array |
| 11 | * of width and height values in pixels (in that order). Default 'thumbnail'. |
| 12 | * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. |
| 13 | * @return string|false Attachment URL or false if no image is available. |
| 14 | */ |
| 15 | function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) { |
| 16 | $image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); |
| 17 | return isset( $image['0'] ) ? $image['0'] : false; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * Get Icon URL |
| 24 | * |
| 25 | * @since 0.2.0 |
| 26 | * |
| 27 | * @param string $type Icon type. |
| 28 | * @param mixed $id Icon ID. |
| 29 | * @param string $size Optional. Icon size, defaults to 'thumbnail'. |
| 30 | * |
| 31 | * @return string |
| 32 | */ |
| 33 | function icon_picker_get_icon_url( $type, $id, $size = 'thumbnail' ) { |
| 34 | $url = ''; |
| 35 | |
| 36 | if ( ! in_array( $type, array( 'image', 'svg' ), true ) ) { |
| 37 | return $url; |
| 38 | } |
| 39 | |
| 40 | if ( empty( $id ) ) { |
| 41 | return $url; |
| 42 | } |
| 43 | |
| 44 | return wp_get_attachment_image_url( $id, $size, false ); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * The Icon Picker Field |
| 50 | * |
| 51 | * @since 0.2.0 |
| 52 | * |
| 53 | * @param mixed $args { |
| 54 | * } |
| 55 | * @param bool $echo Whether to return the field or print it. Defaults to TRUE. |
| 56 | * |
| 57 | * @return mixed |
| 58 | */ |
| 59 | function icon_picker_field( $args, $echo = true ) { |
| 60 | $defaults = array( |
| 61 | 'id' => '', |
| 62 | 'name' => '', |
| 63 | 'value' => array( |
| 64 | 'type' => '', |
| 65 | 'icon' => '', |
| 66 | ), |
| 67 | 'select' => sprintf( '<a class="ipf-select">%s</a>', esc_html__( 'Select Icon', 'icon-picker-field' ) ), |
| 68 | 'remove' => sprintf( '<a class="ipf-remove button hidden">%s</a>', esc_html__( 'Remove', 'icon-picker-field' ) ), |
| 69 | ); |
| 70 | |
| 71 | $args = wp_parse_args( $args, $defaults ); |
| 72 | $args['value'] = wp_parse_args( $args['value'], $defaults['value'] ); |
| 73 | |
| 74 | $field = sprintf( '<div id="%s" class="ipf">', $args['id'] ); |
| 75 | $field .= $args['select']; |
| 76 | $field .= $args['remove']; |
| 77 | |
| 78 | foreach ( $args['value'] as $key => $value ) { |
| 79 | $field .= sprintf( |
| 80 | '<input type="hidden" id="%s" name="%s" class="%s" value="%s" />', |
| 81 | esc_attr( "{$args['id']}-{$key}" ), |
| 82 | esc_attr( "{$args['name']}[{$key}]" ), |
| 83 | esc_attr( "ipf-{$key}" ), |
| 84 | esc_attr( $value ) |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | // This won't be saved. It's here for the preview. |
| 89 | $field .= sprintf( |
| 90 | '<input type="hidden" class="url" value="%s" />', |
| 91 | esc_attr( icon_picker_get_icon_url( $args['value']['type'], $args['value']['icon'] ) ) |
| 92 | ); |
| 93 | $field .= '</div>'; |
| 94 | |
| 95 | if ( $echo ) { |
| 96 | echo $field; // xss ok |
| 97 | } else { |
| 98 | return $field; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
| 103 | // Add-ons for other plugins. |
| 104 | |
| 105 |