| 1 |
<?php |
| 2 |
/** |
| 3 |
* Add Button widget. |
| 4 |
*/ |
| 5 |
class Borderless_Widget_Button extends WP_Widget { |
| 6 |
|
| 7 |
/** |
| 8 |
* Register widget with WordPress. |
| 9 |
*/ |
| 10 |
function __construct() { |
| 11 |
parent::__construct( |
| 12 |
'borderless_button', // Base ID |
| 13 |
__( 'Borderless - Button', 'borderless' ), // Name |
| 14 |
array( 'description' => __( 'Create an infinite styles buttons.', 'borderless' ), ) // Args |
| 15 |
); |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
/** |
| 20 |
* Front-end display of widget. |
| 21 |
* |
| 22 |
* @see WP_Widget::widget() |
| 23 |
* |
| 24 |
* @param array $args Widget arguments. |
| 25 |
* @param array $instance Saved values from database. |
| 26 |
*/ |
| 27 |
public function widget( $args, $instance ) { |
| 28 |
extract( $args ); |
| 29 |
$color = apply_filters( 'widget_color', $instance['color'] ); |
| 30 |
$type = apply_filters( 'widget_type', $instance['type'] ); |
| 31 |
$shape = apply_filters( 'widget_shape', $instance['shape'] ); |
| 32 |
$size = apply_filters( 'widget_size', $instance['size'] ); |
| 33 |
$button_title = apply_filters( 'widget_button_title', $instance['button_title'] ); |
| 34 |
$url = apply_filters( 'widget_url', $instance['url'] ); |
| 35 |
$block = apply_filters( 'widget_block', $instance['block'] ); |
| 36 |
$no_follow = apply_filters( 'widget_no_follow', $instance['no_follow'] ); |
| 37 |
$blank = apply_filters( 'widget_blank', $instance['blank'] ); |
| 38 |
|
| 39 |
$button_title = $button_title ?: 'Borderless'; |
| 40 |
$url = $url ?: 'https://wpborderless.com'; |
| 41 |
$no_follow = ( $no_follow == '1' ) ? 'rel="nofollow"' : ''; |
| 42 |
$block = ( $block == '1' ) ? 'borderless-btn--block' : ''; |
| 43 |
$blank = ( $blank == '1' ) ? 'target="_blank"' : ''; |
| 44 |
|
| 45 |
// Retrieve data from the database. |
| 46 |
$options = get_option( 'borderless' ); |
| 47 |
$borderless_primary_color = isset( $options['primary_color'] ) ? $options['primary_color'] : '#0000FF'; //Primary Color |
| 48 |
$borderless_secondary_color = isset( $options['secondary_color'] ) ? $options['secondary_color'] : '#FF6819'; //Secondary Color |
| 49 |
$borderless_tertiary_color = isset( $options['tertiary_color'] ) ? $options['tertiary_color'] : '#3FCC14'; //Accent Color |
| 50 |
$borderless_text_color = isset( $options['text_color'] ) ? $options['text_color'] : '#333333'; //Text Color |
| 51 |
|
| 52 |
?> |
| 53 |
<style type="text/css"> |
| 54 |
:root { |
| 55 |
--borderless-color-primary-lighter: <?php echo luminanceLight ( $borderless_primary_color, 0.4 ); ?>; |
| 56 |
--borderless-color-primary-light: <?php echo luminanceLight ( $borderless_primary_color, 0.2 ); ?>; |
| 57 |
--borderless-color-primary: <?php echo $borderless_primary_color; ?>; |
| 58 |
--borderless-color-primary-dark: <?php echo luminanceDark ( $borderless_primary_color, 0.2 ); ?>; |
| 59 |
--borderless-color-primary-darker: <?php echo luminanceDark ( $borderless_primary_color, 0.4 ); ?>; |
| 60 |
|
| 61 |
--borderless-color-secondary-lighter: <?php echo luminanceLight ( $borderless_secondary_color, 0.4 ); ?>; |
| 62 |
--borderless-color-secondary-light: <?php echo luminanceLight ( $borderless_secondary_color, 0.2 ); ?>; |
| 63 |
--borderless-color-secondary: <?php echo $borderless_secondary_color; ?>; |
| 64 |
--borderless-color-secondary-dark: <?php echo luminanceDark ( $borderless_secondary_color, 0.2 ); ?>; |
| 65 |
--borderless-color-secondary-darker: <?php echo luminanceDark ( $borderless_secondary_color, 0.4 ); ?>; |
| 66 |
|
| 67 |
--borderless-color-tertiary-lighter: <?php echo luminanceLight ( $borderless_tertiary_color, 0.4 ); ?>; |
| 68 |
--borderless-color-tertiary-light: <?php echo luminanceLight ( $borderless_tertiary_color, 0.2 ); ?>; |
| 69 |
--borderless-color-tertiary: <?php echo $borderless_tertiary_color; ?>; |
| 70 |
--borderless-color-tertiary-dark: <?php echo luminanceDark ( $borderless_tertiary_color, 0.2 ); ?>; |
| 71 |
--borderless-color-tertiary-darker: <?php echo luminanceDark ( $borderless_tertiary_color, 0.4 ); ?>; |
| 72 |
|
| 73 |
--borderless-text-color: <?php echo $borderless_text_color; ?>; |
| 74 |
} |
| 75 |
</style> |
| 76 |
<?php |
| 77 |
|
| 78 |
echo $before_widget; ?> |
| 79 |
|
| 80 |
<!-- front display here --> |
| 81 |
<a href="<?php echo $url; ?>" <?php echo $blank.' '.$no_follow; ?> class="borderless-btn borderless-widget-button <?php echo $type.' '.$shape.' '.$size.' '.$color.' '.$block; ?>" role="button"><?php echo $button_title; ?></a> |
| 82 |
|
| 83 |
<?php echo $after_widget; |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Back-end widget form. |
| 90 |
* |
| 91 |
* @see WP_Widget::form() |
| 92 |
* |
| 93 |
* @param array $instance Previously saved values from database. |
| 94 |
*/ |
| 95 |
public function form( $instance ) { |
| 96 |
$type = isset( $instance['type'] ) ? $instance['type'] : ''; |
| 97 |
$color = isset( $instance['color'] ) ? $instance['color'] : ''; |
| 98 |
$shape = isset( $instance['shape'] ) ? $instance['shape'] : ''; |
| 99 |
$size = isset( $instance['size'] ) ? $instance['size'] : ''; |
| 100 |
$url = isset( $instance['url'] ) ? $instance['url'] : ''; |
| 101 |
$block = isset( $instance['block'] ) ? $instance['block'] : ''; |
| 102 |
$blank = isset( $instance['blank'] ) ? $instance['blank'] : ''; |
| 103 |
$no_follow = isset( $instance['no_follow'] ) ? $instance['no_follow'] : ''; |
| 104 |
$button_title = isset( $instance['button_title'] ) ? $instance['button_title'] : ''; |
| 105 |
|
| 106 |
?> |
| 107 |
|
| 108 |
<p> |
| 109 |
<label for="<?php echo $this->get_field_id('color'); ?>"><?php _e('Color:', 'borderless') ?></label> |
| 110 |
<select id="<?php echo $this->get_field_id('color'); ?>" name="<?php echo $this->get_field_name('color'); ?>" class="widefat"> |
| 111 |
<option value='borderless-btn--primary'<?php selected( $color, 'borderless-btn--primary'); ?>><?php _e( 'Primary Color', 'borderless' ); ?></option> |
| 112 |
<option value='borderless-btn--secondary'<?php selected( $color, 'borderless-btn--secondary'); ?>><?php _e( 'Secondary Color', 'borderless' ); ?></option> |
| 113 |
<option value='borderless-btn--tertiary'<?php selected( $color, 'borderless-btn--tertiary'); ?>><?php _e( 'Tertiary Color', 'borderless' ); ?></option> |
| 114 |
<option value='borderless-btn--light'<?php selected( $color, 'borderless-btn--light'); ?>><?php _e( 'Light Color', 'borderless' ); ?></option> |
| 115 |
<option value='borderless-btn--dark'<?php selected( $color, 'borderless-btn--dark'); ?>><?php _e( 'Dark Color', 'borderless' ); ?></option> |
| 116 |
</select> |
| 117 |
</p> |
| 118 |
|
| 119 |
<p> |
| 120 |
<label for="<?php echo $this->get_field_id('type'); ?>"><?php _e('Type:', 'borderless') ?></label> |
| 121 |
<select id="<?php echo $this->get_field_id('type'); ?>" name="<?php echo $this->get_field_name('type'); ?>" class="widefat"> |
| 122 |
<option value='borderless-btn--contained'<?php selected( $type, 'borderless-btn--contained'); ?>><?php _e( 'Contained', 'borderless' ); ?></option> |
| 123 |
<option value='borderless-btn--outline'<?php selected( $type, 'borderless-btn--outline'); ?>><?php _e( 'Outline', 'borderless' ); ?></option> |
| 124 |
<option value='borderless-btn--text'<?php selected( $type, 'borderless-btn--text'); ?>><?php _e( 'Text', 'borderless' ); ?></option> |
| 125 |
</select> |
| 126 |
</p> |
| 127 |
|
| 128 |
<p> |
| 129 |
<label for="<?php echo $this->get_field_id('shape'); ?>"><?php _e('Shape:', 'borderless') ?></label> |
| 130 |
<select id="<?php echo $this->get_field_id('shape'); ?>" name="<?php echo $this->get_field_name('shape'); ?>" class="widefat"> |
| 131 |
<option value='borderless-btn--rounded'<?php selected( $shape, 'borderless-btn--rounded'); ?>><?php _e( 'Rounded', 'borderless' ); ?></option> |
| 132 |
<option value='borderless-btn--round'<?php selected( $shape, 'borderless-btn--round'); ?>><?php _e( 'Round', 'borderless' ); ?></option> |
| 133 |
<option value='borderless-btn--square'<?php selected( $shape, 'borderless-btn--square'); ?>><?php _e( 'Square', 'borderless' ); ?></option> |
| 134 |
</select> |
| 135 |
</p> |
| 136 |
|
| 137 |
<p> |
| 138 |
<label for="<?php echo $this->get_field_id('size'); ?>"><?php _e('Size:', 'borderless') ?></label> |
| 139 |
<select id="<?php echo $this->get_field_id('size'); ?>" name="<?php echo $this->get_field_name('size'); ?>" class="widefat"> |
| 140 |
<option value=''<?php selected( $size, ''); ?>><?php _e( 'Default', 'borderless' ); ?></option> |
| 141 |
<option value='borderless-btn--sm'<?php selected( $size, 'borderless-btn--sm'); ?>><?php _e( 'Small', 'borderless' ); ?></option> |
| 142 |
<option value='borderless-btn--lg'<?php selected( $size, 'borderless-btn--lg'); ?>><?php _e( 'Large', 'borderless' ); ?></option> |
| 143 |
<option value='borderless-btn--block'<?php selected( $size, 'borderless-btn--block'); ?>><?php _e( 'Block', 'borderless' ); ?></option> |
| 144 |
</select> |
| 145 |
</p> |
| 146 |
|
| 147 |
<p> |
| 148 |
<label for="<?php echo $this->get_field_name( 'button_title' ); ?>"><?php _e( 'Title:', 'borderless' ); ?></label> |
| 149 |
<input class="widefat" id="<?php echo $this->get_field_id( 'button_title' ); ?>" name="<?php echo $this->get_field_name( 'button_title' ); ?>" type="text" value="<?php echo esc_attr( $button_title ); ?>" /> |
| 150 |
</p> |
| 151 |
|
| 152 |
<p> |
| 153 |
<label for="<?php echo $this->get_field_name( 'url' ); ?>"><?php _e( 'URL:', 'borderless' ); ?></label> |
| 154 |
<input class="widefat" id="<?php echo $this->get_field_id( 'url' ); ?>" name="<?php echo $this->get_field_name( 'url' ); ?>" type="text" value="<?php echo esc_attr( $url ); ?>" /> |
| 155 |
</p> |
| 156 |
|
| 157 |
<p> |
| 158 |
<label for="<?php echo esc_attr( $this->get_field_id( 'block' ) ); ?>"><?php _e( 'Full Width', 'borderless' ); ?></label> |
| 159 |
<input id="<?php echo esc_attr( $this->get_field_id( 'block' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'block' ) ); ?>" type="checkbox" value="1" <?php checked( '1', $block ); ?> /> |
| 160 |
</p> |
| 161 |
|
| 162 |
<p> |
| 163 |
<label for="<?php echo esc_attr( $this->get_field_id( 'no_follow' ) ); ?>"><?php _e( 'No Follow', 'borderless' ); ?></label> |
| 164 |
<input id="<?php echo esc_attr( $this->get_field_id( 'no_follow' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'no_follow' ) ); ?>" type="checkbox" value="1" <?php checked( '1', $no_follow ); ?> /> |
| 165 |
</p> |
| 166 |
|
| 167 |
<p> |
| 168 |
<label for="<?php echo esc_attr( $this->get_field_id( 'blank' ) ); ?>"><?php _e( 'Open in new window', 'borderless' ); ?></label> |
| 169 |
<input id="<?php echo esc_attr( $this->get_field_id( 'blank' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'blank' ) ); ?>" type="checkbox" value="1" <?php checked( '1', $blank ); ?> /> |
| 170 |
</p> |
| 171 |
<?php } |
| 172 |
|
| 173 |
|
| 174 |
/** |
| 175 |
* Sanitize widget form values as they are saved. |
| 176 |
* |
| 177 |
* @see WP_Widget::update() |
| 178 |
* |
| 179 |
* @param array $new_instance Values just sent to be saved. |
| 180 |
* @param array $old_instance Previously saved values from database. |
| 181 |
* |
| 182 |
* @return array Updated safe values to be saved. |
| 183 |
*/ |
| 184 |
public function update( $new_instance, $old_instance ) { |
| 185 |
$instance = array(); |
| 186 |
$instance['type'] = ( !empty( $new_instance['type'] ) ) ? strip_tags( $new_instance['type'] ) : ''; |
| 187 |
$instance['color'] = ( !empty( $new_instance['color'] ) ) ? strip_tags( $new_instance['color'] ) : ''; |
| 188 |
$instance['button_title'] = ( !empty( $new_instance['button_title'] ) ) ? strip_tags( $new_instance['button_title'] ) : ''; |
| 189 |
$instance['shape'] = ( !empty( $new_instance['shape'] ) ) ? strip_tags( $new_instance['shape'] ) : ''; |
| 190 |
$instance['size'] = ( !empty( $new_instance['size'] ) ) ? strip_tags( $new_instance['size'] ) : ''; |
| 191 |
$instance['url'] = ( !empty( $new_instance['url'] ) ) ? strip_tags( $new_instance['url'] ) : ''; |
| 192 |
$instance['block'] = ( !empty( $new_instance['block'] ) ) ? strip_tags( $new_instance['block'] ) : ''; |
| 193 |
$instance['no_follow'] = ( !empty( $new_instance['no_follow'] ) ) ? strip_tags( $new_instance['no_follow'] ) : ''; |
| 194 |
$instance['blank'] = ( !empty( $new_instance['blank'] ) ) ? strip_tags( $new_instance['blank'] ) : ''; |
| 195 |
return $instance; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
// register Borderless_Widget_Button widget |
| 200 |
add_action( 'widgets_init', 'register_borderless_button' ); |
| 201 |
|
| 202 |
function register_borderless_button() { |
| 203 |
register_widget( 'Borderless_Widget_Button' ); |
| 204 |
} |