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