| 1 | <?php |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 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\Model; |
||
| 10 | |||
| 11 | class Button extends Base { |
||
| 12 | |||
| 13 | public static $properties = array( |
||
| 14 | // $property => $default value |
||
| 15 | 'size' => 'big', |
||
| 16 | 'color' => '#599677', |
||
| 17 | 'autowidth' => 'on', |
||
| 18 | 'style' => 'filled', |
||
| 19 | 'format' => 'rectangle', |
||
| 20 | 'hide' => 'false', |
||
| 21 | 'buttonid' => '', |
||
| 22 | // Note: the fields 'language' and 'json-data' cannot be set here (No function call allowed within class variables) |
||
| 23 | ); |
||
| 24 | |||
| 25 | public static $style = array( |
||
| 26 | 'filled' => 'Filled', |
||
| 27 | 'outline' => 'Outline', |
||
| 28 | 'frameless' => 'Frameless', |
||
| 29 | ); |
||
| 30 | |||
| 31 | public static $format = array( |
||
| 32 | 'rectangle' => 'Rectangle', |
||
| 33 | 'square' => 'Square', |
||
| 34 | 'cover' => 'Cover', |
||
| 35 | ); |
||
| 36 | |||
| 37 | public static $width = array( |
||
| 38 | 'on' => 'Yes', |
||
| 39 | 'off' => 'No', |
||
| 40 | ); |
||
| 41 | |||
| 42 | public static $size = array( |
||
| 43 | 'small' => 'Small', |
||
| 44 | 'medium' => 'Medium', |
||
| 45 | 'big' => 'Big', |
||
| 46 | ); |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * Fetches a Button or Network Button with a specific name |
||
| 51 | * @param string $name |
||
| 52 | * @return object||FALSE |
||
| 53 | */ |
||
| 54 | public static function get_button_by_name( $name ) { |
||
| 55 | if ( $button = \PodloveSubscribeButton\Model\Button::find_one_by_property( 'name', $name ) ) { |
||
| 56 | return $button; |
||
| 57 | } |
||
| 58 | |||
| 59 | if ( $network_button = \PodloveSubscribeButton\Model\NetworkButton::find_one_by_property( 'name', $name ) ) { |
||
| 60 | $network_button->id = $network_button->id . 'N'; |
||
| 61 | return $network_button; |
||
| 62 | } |
||
| 63 | |||
| 64 | return false; |
||
| 65 | |||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Returns either global buttons settings or the default settings |
||
| 70 | * @param array |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | public static function get_global_setting_with_fallback( $settings = array() ) { |
||
| 74 | foreach ( self::$properties as $property => $default ) { |
||
| 75 | $settings[ $property ] = ( get_option( 'podlove_subscribe_button_default_' . $property ) ? get_option( 'podlove_subscribe_button_default_' . $property ) : $default ); |
||
| 76 | } |
||
| 77 | |||
| 78 | return $settings; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Gathers all information and renders the Subscribe button. |
||
| 83 | * @param string $size |
||
| 84 | * @param string $autowidth |
||
| 85 | * @param string $style |
||
| 86 | * @param string $format |
||
| 87 | * @param string $color |
||
| 88 | * @param boolean $hide |
||
| 89 | * @param boolean $buttonid |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public function render( $size = 'big', $autowidth = 'on', $style = 'filled', $format = 'rectangle', $color = '#599677', $hide = false, $buttonid = false, $language = 'en' ) { |
||
| 93 | $button_styling = array_merge( |
||
| 94 | $this->get_button_styling( $size, $autowidth, $style, $format, $color ), |
||
| 95 | array( |
||
| 96 | 'hide' => $hide, |
||
| 97 | 'buttonid' => $buttonid, |
||
| 98 | 'language' => $language, |
||
| 99 | ) |
||
| 100 | ); |
||
| 101 | |||
| 102 | return $this->provide_button_html( |
||
| 103 | array( |
||
| 104 | 'title' => $this->title, |
||
| 105 | 'subtitle' => $this->subtitle, |
||
| 106 | 'description' => $this->description, |
||
| 107 | 'cover' => $this->cover, |
||
| 108 | 'feeds' => $this->get_feeds_as_array( $this->feeds ), |
||
| 109 | ), |
||
| 110 | $button_styling |
||
| 111 | ); |
||
| 112 | |||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Provides the feed as an array in the required format |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | private function get_feeds_as_array( $feeds = array() ) { |
||
| 120 | foreach ( $feeds as $feed ) { |
||
| 121 | if ( isset( \PodloveSubscribeButton\MediaTypes::$audio[ $feed[ 'format' ] ][ 'extension' ] ) ) { |
||
| 122 | $new_feed = array( |
||
| 123 | 'type' => 'audio', |
||
| 124 | 'format' => \PodloveSubscribeButton\MediaTypes::$audio[ $feed['format'] ]['extension'], |
||
| 125 | 'url' => $feed['url'], |
||
| 126 | 'variant' => 'high', |
||
| 127 | ); |
||
| 128 | |||
| 129 | if ( isset( $feed[ 'itunesfeedid' ] ) && $feed[ 'itunesfeedid' ] > 0 ) { |
||
| 130 | $new_feed['directory-url-itunes'] = "https://itunes.apple.com/podcast/id" . $feed['itunesfeedid']; |
||
| 131 | } |
||
| 132 | |||
| 133 | $feeds[] = $new_feed; |
||
| 134 | |||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | return $feeds; |
||
| 139 | |||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Provides the HTML source of the Subscribe Button |
||
| 144 | * @param array $podcast_data |
||
| 145 | * @param array $button_styling |
||
| 146 | * @param string $data_attributes |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | private function provide_button_html( $podcast_data, $button_styling, $data_attributes = "" ) { |
||
| 150 | // Create data attributes for Button |
||
| 151 | foreach ( $button_styling as $attribute => $value ) { |
||
| 152 | $data_attributes .= 'data-' . $attribute . '="' . $value . '" '; |
||
| 153 | } |
||
| 154 | |||
| 155 | return" |
||
| 156 | <script> |
||
| 157 | podcastData".$this->id . " = " . json_encode( $podcast_data ) . " |
||
| 158 | </script> |
||
| 159 | <script |
||
| 160 | class=\"podlove-subscribe-button\" |
||
| 161 | src=\"https://cdn.podlove.org/subscribe-button/javascripts/app.js\" " . $data_attributes . "> |
||
| 162 | </script> |
||
| 163 | "; |
||
| 164 | |||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns an array with either the set or default values |
||
| 169 | * @param string $size |
||
| 170 | * @param string $autowidth |
||
| 171 | * @param string $style |
||
| 172 | * @param string $format |
||
| 173 | * @param string $color |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | public function get_button_styling( $size, $autowidth, $style, $format, $color ) { |
||
| 177 | |||
| 178 | return array( |
||
| 179 | // $attribute => $value |
||
| 180 | 'size' => ( $size == 'default' ? get_option( 'podlove_subscribe_button_default_size', $size ) : $size ) |
||
| 181 | . self::interpret_autowidth_attribute( $autowidth ), |
||
| 182 | 'style' => ( $style == 'default' ? get_option( 'podlove_subscribe_button_default_style', $style ) : $style ), |
||
| 183 | 'format' => ( $format == 'default' ? get_option( 'podlove_subscribe_button_default_format', $format ) : $format ), |
||
| 184 | 'color' => ( isset( $color ) ? $color : get_option( 'podlove_subscribe_button_default_color', $color ) ), |
||
| 185 | 'json-data' => 'podcastData' . $this->id |
||
| 186 | ); |
||
| 187 | |||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Helper function to interpret the given $autowidth value correctly |
||
| 192 | * @param string $autowidth |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | private static function interpret_autowidth_attribute( $autowidth ) { |
||
| 196 | if ( $autowidth == 'default' && get_option( 'podlove_subscribe_button_default_autowidth' ) !== 'on' ) |
||
| 197 | return ''; |
||
| 198 | |||
| 199 | if ( $autowidth !== 'default' && $autowidth !== 'on' ) |
||
| 200 | return ''; |
||
| 201 | |||
| 202 | return ' auto'; |
||
| 203 | |||
| 204 | } |
||
| 205 | |||
| 206 | } // END class |
||
| 207 | |||
| 208 | Button::property( 'id', 'INT NOT NULL AUTO_INCREMENT PRIMARY KEY' ); |
||
| 209 | Button::property( 'name', 'VARCHAR(255)' ); |
||
| 210 | Button::property( 'title', 'VARCHAR(255)' ); |
||
| 211 | Button::property( 'subtitle', 'VARCHAR(255)' ); |
||
| 212 | Button::property( 'description', 'TEXT' ); |
||
| 213 | Button::property( 'cover', 'VARCHAR(255)' ); |
||
| 214 | Button::property( 'feeds', 'TEXT' ); |
||
| 215 |