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