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