1 | <?php |
||
2 | |||
3 | namespace PodloveSubscribeButton; |
||
4 | |||
5 | include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
||
6 | |||
7 | class Widget extends \WP_Widget { |
||
8 | |||
9 | public static $widget_settings = array( |
||
10 | 'infotext', |
||
11 | 'title', |
||
12 | 'size', |
||
13 | 'style', |
||
14 | 'format', |
||
15 | 'autowidth', |
||
16 | 'button', |
||
17 | 'color', |
||
18 | ); |
||
19 | |||
20 | public function __construct() { |
||
21 | parent::__construct( |
||
22 | 'podlove_subscribe_button_wp_plugin_widget', |
||
23 | ( self::is_podlove_publisher_active() ? 'Podlove Subscribe Button (WordPress plugin)' : 'Podlove Subscribe Button' ), |
||
24 | array( 'description' => __( 'Adds a Podlove Subscribe Button to your Sidebar', 'podlove-subscribe-button' ), ) |
||
25 | ); |
||
26 | |||
27 | } |
||
28 | |||
29 | public static function is_podlove_publisher_active() { |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
30 | if ( is_plugin_active( "podlove-podcasting-plugin-for-wordpress/podlove.php" ) ) { |
||
31 | return true; |
||
32 | } |
||
33 | |||
34 | return false; |
||
35 | |||
36 | } |
||
37 | |||
38 | public function widget( $args, $instance ) { |
||
39 | // Fetch the (network)button by it's name |
||
40 | if ( ! $button = \PodloveSubscribeButton\Model\Button::get_button_by_name( $instance[ 'button' ] ) ) { |
||
41 | return sprintf( __( 'Oops. There is no button with the ID "%s".', 'podlove-subscribe-button' ), $args['button'] ); |
||
42 | } |
||
43 | |||
44 | echo $args[ 'before_widget' ]; |
||
45 | echo $args[ 'before_title' ] . apply_filters( 'widget_title', $instance[ 'title' ] ) . $args[ 'after_title' ]; |
||
46 | |||
47 | echo $button->render( |
||
48 | \PodloveSubscribeButton::get_array_value_with_fallback( $instance, 'size' ), |
||
49 | \PodloveSubscribeButton::get_array_value_with_fallback( $instance, 'autowidth' ), |
||
50 | \PodloveSubscribeButton::get_array_value_with_fallback( $instance, 'style' ), |
||
51 | \PodloveSubscribeButton::get_array_value_with_fallback( $instance, 'format' ), |
||
52 | \PodloveSubscribeButton::get_array_value_with_fallback( $instance, 'color' ) |
||
53 | ); |
||
54 | |||
55 | if ( strlen( $instance[ 'infotext' ] ) ) { |
||
56 | echo wpautop( $instance[ 'infotext' ] ); |
||
57 | } |
||
58 | |||
59 | echo $args[ 'after_widget' ]; |
||
60 | |||
61 | } |
||
62 | |||
63 | public function form( $instance ) { |
||
64 | $title = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : ''; |
||
65 | $button = isset( $instance[ 'button' ] ) ? $instance[ 'button' ] : ''; |
||
66 | $size = isset( $instance[ 'size' ] ) ? $instance[ 'size' ] : 'big'; |
||
67 | $style = isset( $instance[ 'style' ] ) ? $instance[ 'style' ] : 'filled'; |
||
68 | $format = isset( $instance[ 'format' ] ) ? $instance[ 'format' ] : 'cover'; |
||
69 | $autowidth = isset( $instance[ 'autowidth' ] ) ? $instance[ 'autowidth' ] : true; |
||
70 | $infotext = isset( $instance[ 'infotext' ] ) ? $instance[ 'infotext' ] : ''; |
||
71 | $color = isset( $instance[ 'color' ] ) ? $instance[ 'color' ] : '#75ad91'; |
||
72 | |||
73 | $buttons = \PodloveSubscribeButton\Model\Button::all(); |
||
74 | if ( is_multisite() ) { |
||
75 | $network_buttons = \PodloveSubscribeButton\Model\NetworkButton::all(); |
||
76 | } |
||
77 | ?> |
||
78 | <p> |
||
79 | <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'podlove-subscribe-button' ); ?></label> |
||
80 | <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $title; ?>" /> |
||
81 | <label for="<?php echo $this->get_field_id( 'color' ); ?>"><?php _e( 'Color', 'podlove-subscribe-button' ); ?></label> |
||
82 | <input class="podlove_subscribe_button_color" id="<?php echo $this->get_field_id( 'color' ); ?>" name="<?php echo $this->get_field_name( 'color' ); ?>" value="<?php echo $color; ?>" /> |
||
83 | <style type="text/css"> |
||
84 | .sp-replacer { |
||
85 | display: flex; |
||
86 | } |
||
87 | .sp-preview { |
||
88 | flex-grow: 10; |
||
89 | } |
||
90 | </style> |
||
91 | <label for="<?php echo $this->get_field_id( 'button' ); ?>"><?php _e( 'Button', 'podlove-subscribe-button' ); ?></label> |
||
92 | <select class="widefat" id="<?php echo $this->get_field_id( 'button' ); ?>" |
||
93 | name="<?php echo $this->get_field_name( 'button' ); ?>"> |
||
94 | <?php if ( isset( $network_buttons ) && count( $network_buttons ) > 0 ) : ?> |
||
95 | <optgroup label="<?php _e( 'Local', 'podlove-subscribe-button' ); ?>"> |
||
96 | <?php |
||
97 | foreach ( $buttons as $subscribebutton ) { |
||
98 | echo "<option value='" . $subscribebutton->name . "' " . selected( $subscribebutton->name, $button ) . " >" . $subscribebutton->title . " (" . $subscribebutton->name . ")</option>"; |
||
99 | } ?> |
||
100 | </optgroup> |
||
101 | <optgroup label="<?php _e( 'Network', 'podlove-subscribe-button' ); ?>"> |
||
102 | <?php |
||
103 | foreach ( $network_buttons as $subscribebutton ) { |
||
104 | echo "<option value='" . $subscribebutton->name . "' " . selected( $subscribebutton->name, $button ) . " >" . $subscribebutton->title . " (" . $subscribebutton->name . ")</option>"; |
||
105 | } ?> |
||
106 | </optgroup> |
||
107 | <?php else : |
||
108 | foreach ( $buttons as $subscribebutton ) { |
||
109 | echo "<option value='" . $subscribebutton->name . "' " . selected( $subscribebutton->name, $button ) . " >" . $subscribebutton->title . " (" . $subscribebutton->name . ")</option>"; |
||
110 | } |
||
111 | endif; ?> |
||
112 | </select> |
||
113 | <?php |
||
114 | $customize_options = array( |
||
115 | 'size' => array( |
||
116 | 'name' => __( 'Size', 'podlove-subscribe-button' ), |
||
117 | 'options' => \PodloveSubscribeButton\Model\Button::$size |
||
118 | ), |
||
119 | 'style' => array( |
||
120 | 'name' => __( 'Style', 'podlove-subscribe-button' ), |
||
121 | 'options' => \PodloveSubscribeButton\Model\Button::$style |
||
122 | ), |
||
123 | 'format' => array( |
||
124 | 'name' => __( 'Format', 'podlove-subscribe-button' ), |
||
125 | 'options' => \PodloveSubscribeButton\Model\Button::$format |
||
126 | ), |
||
127 | 'autowidth' => array( |
||
128 | 'name' => __( 'Autowidth', 'podlove-subscribe-button' ), |
||
129 | 'options' => \PodloveSubscribeButton\Model\Button::$width |
||
130 | ) |
||
131 | ); |
||
132 | |||
133 | foreach ( $customize_options as $slug => $properties ) : ?> |
||
134 | <label for="<?php echo $this->get_field_id( $slug ); ?>"><?php echo $properties[ 'name' ]; ?></label> |
||
135 | <select class="widefat" id="<?php echo $this->get_field_id( $slug ); ?>" name="<?php echo $this->get_field_name( $slug ); ?>"> |
||
136 | <option value="default" <?php echo ( $$slug == 'default' ? 'selected="selected"' : '' ); ?>><?php printf( __( 'Default %s', 'podlove-subscribe-button' ), $properties[ 'name' ] ) ?></option> |
||
137 | <optgroup> |
||
138 | <?php foreach ( $properties[ 'options' ] as $property => $name ) : ?> |
||
139 | <option value="<?php echo $property; ?>" <?php echo ( $$slug == $property ? 'selected="selected"' : '' ); ?>><?php echo $name; ?></option> |
||
140 | <?php endforeach; ?> |
||
141 | </optgroup> |
||
142 | </select> |
||
143 | <?php endforeach; ?> |
||
144 | <label for="<?php echo $this->get_field_id( 'infotext' ); ?>"><?php _e( 'Description', 'podlove-subscribe-button' ); ?></label> |
||
145 | <textarea class="widefat" rows="10" id="<?php echo $this->get_field_id( 'infotext' ); ?>" name="<?php echo $this->get_field_name( 'infotext' ); ?>"><?php echo $infotext; ?></textarea> |
||
146 | </p> |
||
147 | <?php |
||
148 | |||
149 | } |
||
150 | |||
151 | public function update( $new_instance, $old_instance ) { |
||
152 | $instance = array(); |
||
153 | |||
154 | foreach ( self::$widget_settings as $setting ) { |
||
155 | $instance[ $setting ] = ( ! empty( $new_instance[ $setting ] ) ) ? strip_tags( $new_instance[ $setting ] ) : ''; |
||
156 | } |
||
157 | |||
158 | return $instance; |
||
159 | |||
160 | } |
||
161 | |||
162 | } // END class |
||
163 |