1 | <?php |
||||
2 | /** |
||||
3 | * Plugin Name: Podlove Subscribe Button |
||||
4 | * Plugin URI: https://wordpress.org/plugins/podlove-subscribe-button/ |
||||
5 | * Description: Brings the Podlove Subscribe Button to your WordPress installation. |
||||
6 | * Version: 1.4.0-beta |
||||
7 | * Author: Podlove |
||||
8 | * Author URI: https://podlove.org/ |
||||
9 | * License: MIT |
||||
10 | * License URI: license.txt |
||||
11 | * Text Domain: podlove-subscribe-button |
||||
12 | * Domain Path: /languages |
||||
13 | */ |
||||
14 | |||||
15 | $correct_php_version = version_compare( phpversion(), "5.3", ">=" ); |
||||
16 | |||||
17 | if ( ! $correct_php_version ) { |
||||
18 | printf( __( 'Podlove Subscribe Button Plugin requires %s or higher.<br>', 'podlove-subscribe-button' ), '<code>PHP 5.3</code>' ); |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
19 | echo '<br />'; |
||||
20 | printf( __( 'You are running %s', 'podlove-subscribe-button' ), '<code>PHP ' . phpversion() . '</code>' ); |
||||
0 ignored issues
–
show
|
|||||
21 | exit; |
||||
22 | } |
||||
23 | |||||
24 | // Constants |
||||
25 | require('constants.php'); |
||||
26 | require('settings/buttons.php'); |
||||
27 | // Models |
||||
28 | require('model/base.php'); |
||||
29 | require('model/button.php'); |
||||
30 | require('model/network_button.php'); |
||||
31 | // Table |
||||
32 | require('settings/buttons_list_table.php'); |
||||
33 | // Media Types |
||||
34 | require('media_types.php'); |
||||
35 | // Widget |
||||
36 | require('widget.php'); |
||||
37 | // Version control |
||||
38 | require('version.php'); |
||||
39 | // Helper functions |
||||
40 | require('helper.php'); |
||||
41 | |||||
42 | add_action( 'admin_menu', array( 'PodloveSubscribeButton', 'admin_menu') ); |
||||
43 | if ( is_multisite() ) |
||||
44 | add_action( 'network_admin_menu', array( 'PodloveSubscribeButton', 'admin_network_menu') ); |
||||
45 | |||||
46 | add_action( 'admin_init', array( 'PodloveSubscribeButton\Settings\Buttons', 'process_form' ) ); |
||||
47 | register_activation_hook( __FILE__, array( 'PodloveSubscribeButton', 'build_models' ) ); |
||||
48 | |||||
49 | // Register Settings |
||||
50 | add_action( 'admin_init', function () { |
||||
51 | $settings = array('size', 'autowidth', 'style', 'format', 'color'); |
||||
52 | |||||
53 | foreach ($settings as $setting) { |
||||
54 | register_setting( 'podlove-subscribe-button', 'podlove_subscribe_button_default_' . $setting ); |
||||
55 | } |
||||
56 | } ); |
||||
57 | |||||
58 | add_shortcode( 'podlove-subscribe-button', array( 'PodloveSubscribeButton', 'shortcode' ) ); |
||||
59 | |||||
60 | add_action( 'plugins_loaded', function () { |
||||
61 | load_plugin_textdomain( 'podlove-subscribe-button', false, dirname(plugin_basename( __FILE__)) . '/languages/'); |
||||
0 ignored issues
–
show
false of type false is incompatible with the type string expected by parameter $deprecated of load_plugin_textdomain() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
62 | } ); |
||||
63 | |||||
64 | PodloveSubscribeButton::run(); |
||||
65 | |||||
66 | /** |
||||
67 | * Class PodloveSubscribeButton |
||||
68 | */ |
||||
69 | class PodloveSubscribeButton { |
||||
70 | |||||
71 | /** |
||||
0 ignored issues
–
show
|
|||||
72 | * @var string current plugin version |
||||
73 | */ |
||||
74 | public static $version = '1.4.0-beta'; |
||||
75 | |||||
76 | public static function run() { |
||||
0 ignored issues
–
show
|
|||||
77 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) ); |
||||
78 | } |
||||
0 ignored issues
–
show
|
|||||
79 | |||||
80 | public static function enqueue_assets( $hook ) { |
||||
0 ignored issues
–
show
|
|||||
81 | |||||
82 | $pages = array( 'settings_page_podlove-subscribe-button', 'widgets.php' ); |
||||
83 | |||||
84 | if ( ! in_array( $hook, $pages ) ) { |
||||
0 ignored issues
–
show
|
|||||
85 | return; |
||||
86 | } |
||||
87 | |||||
88 | // CSS Stylesheet |
||||
0 ignored issues
–
show
|
|||||
89 | wp_register_style( 'podlove-subscribe-button', plugin_dir_url( __FILE__ ) . 'style.css', false, self::$version ); |
||||
0 ignored issues
–
show
false of type false is incompatible with the type array expected by parameter $deps of wp_register_style() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
90 | wp_enqueue_style( 'podlove-subscribe-button' ); |
||||
91 | |||||
92 | // Spectrum JS |
||||
0 ignored issues
–
show
|
|||||
93 | wp_enqueue_style( 'podlove-subscribe-button-spectrum', plugin_dir_url( __FILE__ ) . 'js/spectrum/spectrum.css', array(), '1.8.0' ); |
||||
94 | wp_enqueue_script( 'podlove-subscribe-button-spectrum', plugin_dir_url( __FILE__ ) . 'js/spectrum/spectrum.js', array( 'jquery' ), '1.8.0' ); |
||||
95 | |||||
96 | // Admin JS |
||||
0 ignored issues
–
show
|
|||||
97 | wp_register_script( 'podlove-subscribe-button-admin-tools', plugin_dir_url( __FILE__ ) . 'js/admin.js', array( 'jquery' ), self::$version ); |
||||
0 ignored issues
–
show
|
|||||
98 | $js_translations = array( |
||||
99 | 'select_color' => __( 'Select Color', 'podlove-subscribe-button' ), |
||||
100 | 'cancel' => __( 'Cancel', 'podlove-subscribe-button' ), |
||||
101 | 'media_library' => __( 'Media Library', 'podlove-subscribe-button' ), |
||||
102 | 'use_for' => __( 'Use for Podcast Cover Art', 'podlove-subscribe-button' ), |
||||
103 | ); |
||||
104 | wp_localize_script( 'podlove-subscribe-button-admin-tools', 'i18n', $js_translations ); |
||||
105 | wp_enqueue_script( 'podlove-subscribe-button-admin-tools' ); |
||||
106 | } |
||||
0 ignored issues
–
show
|
|||||
107 | |||||
108 | public static function admin_menu() { |
||||
109 | add_options_page( |
||||
110 | 'Podlove Subscribe Button Options', |
||||
111 | 'Podlove Subscribe Button', |
||||
112 | 'manage_options', |
||||
113 | 'podlove-subscribe-button', |
||||
114 | array( 'PodloveSubscribeButton\Settings\Buttons', 'page') |
||||
115 | ); |
||||
116 | } |
||||
117 | |||||
118 | public static function admin_network_menu() { |
||||
119 | add_submenu_page( |
||||
120 | 'settings.php', |
||||
121 | 'Podlove Subscribe Button Options', |
||||
122 | 'Podlove Subscribe Button', |
||||
123 | 'manage_options', |
||||
124 | 'podlove-subscribe-button', |
||||
125 | array( 'PodloveSubscribeButton\Settings\Buttons', 'page') |
||||
126 | ); |
||||
127 | } |
||||
128 | |||||
129 | public static function build_models() { |
||||
130 | // Build Databases |
||||
131 | \PodloveSubscribeButton\Model\Button::build(); |
||||
132 | if ( is_multisite() ) |
||||
133 | \PodloveSubscribeButton\Model\NetworkButton::build(); |
||||
134 | |||||
135 | // Set Button "default" values |
||||
136 | $default_values = array( |
||||
137 | 'size' => 'big', |
||||
138 | 'autowidth' => 'on', |
||||
139 | 'color' => '#599677', |
||||
140 | 'style' => 'filled', |
||||
141 | 'format' => 'rectangle' |
||||
142 | ); |
||||
143 | |||||
144 | foreach ($default_values as $option => $default_value) { |
||||
145 | if ( ! get_option('podlove_subscribe_button_default_' . $option ) ) { |
||||
146 | update_option('podlove_subscribe_button_default_' . $option, $default_value); |
||||
147 | } |
||||
148 | } |
||||
149 | } |
||||
150 | |||||
151 | public static function shortcode( $args ) { |
||||
152 | if ( ! $args || ! isset($args['button']) ) { |
||||
153 | return __('You need to create a Button first and provide its ID.', 'podlove-subscribe-button'); |
||||
154 | } else { |
||||
155 | $buttonid = $args['button']; |
||||
156 | } |
||||
157 | |||||
158 | // Fetch the (network)button by it's name |
||||
159 | if ( ! $button = \PodloveSubscribeButton\Model\Button::get_button_by_name($args['button']) ) |
||||
160 | return sprintf( __('Oops. There is no button with the ID "%s".', 'podlove-subscribe-button'), $args['button'] ); |
||||
161 | |||||
162 | // Get button styling and options |
||||
163 | $autowidth = self::interpret_width_attribute( self::get_array_value_with_fallback($args, 'width') ); |
||||
164 | $size = self::get_attribute( 'size', self::get_array_value_with_fallback($args, 'size') ); |
||||
165 | $style = self::get_attribute( 'style', self::get_array_value_with_fallback($args, 'style') ); |
||||
166 | $format = self::get_attribute( 'format', self::get_array_value_with_fallback($args, 'format') ); |
||||
167 | $color = self::get_attribute( 'color', self::get_array_value_with_fallback($args, 'color') ); |
||||
168 | |||||
169 | if ( isset($args['language']) ) { |
||||
170 | $language = $args['language']; |
||||
171 | } else { |
||||
172 | $language = 'en'; |
||||
173 | } |
||||
174 | |||||
175 | if ( isset($args['color']) ) { |
||||
176 | $color = $args['color']; |
||||
177 | } else { |
||||
178 | $color = get_option('podlove_subscribe_button_default_color', '#599677'); |
||||
179 | } |
||||
180 | |||||
181 | if ( isset($args['hide']) && $args['hide'] == 'true' ) { |
||||
182 | $hide = true; |
||||
183 | } else { |
||||
184 | $hide = false; |
||||
185 | } |
||||
186 | |||||
187 | // Render button |
||||
188 | return $button->render($size, $autowidth, $style, $format, $color, $hide, $buttonid, $language); |
||||
189 | } |
||||
190 | |||||
191 | public static function get_array_value_with_fallback($args, $key) { |
||||
192 | if ( isset($args[$key]) ) |
||||
193 | return $args[$key]; |
||||
194 | |||||
195 | return false; |
||||
196 | } |
||||
197 | |||||
198 | /** |
||||
199 | * @param string $attribute |
||||
200 | * @param string $attribute_value |
||||
201 | * @return string |
||||
202 | */ |
||||
203 | private static function get_attribute($attribute=null, $attribute_value=null) { |
||||
204 | if ( isset($attribute_value) && ctype_alnum($attribute_value) && key_exists( $attribute_value, \PodloveSubscribeButton\Model\Button::$$attribute ) ) { |
||||
205 | return $attribute_value; |
||||
206 | } else { |
||||
207 | return get_option('podlove_subscribe_button_default_' . $attribute, \PodloveSubscribeButton\Model\Button::$properties[$attribute]); |
||||
208 | } |
||||
209 | } |
||||
210 | |||||
211 | /** |
||||
212 | * Interprets the provided width attribute and return either auto- or a specific width |
||||
213 | * @param string $width_attribute |
||||
214 | * @return string |
||||
215 | */ |
||||
216 | private static function interpret_width_attribute( $width_attribute = null ) { |
||||
217 | if ( $width_attribute == 'auto' ) |
||||
218 | return 'on'; |
||||
219 | if ( $width_attribute && $width_attribute !== 'auto' ) |
||||
220 | return 'off'; |
||||
221 | |||||
222 | return get_option('podlove_subscribe_button_default_autowidth', 'on'); |
||||
223 | } |
||||
224 | } |