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
|
|
|
* @version 1.4.0-beta |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Plugin Name: Podlove Subscribe Button |
12
|
|
|
* Plugin URI: https://wordpress.org/plugins/podlove-subscribe-button/ |
13
|
|
|
* Description: Brings the Podlove Subscribe Button to your WordPress installation. |
14
|
|
|
* Version: 1.4.0-beta |
15
|
|
|
* Author: Podlove |
16
|
|
|
* Author URI: https://podlove.org/ |
17
|
|
|
* License: MIT |
18
|
|
|
* License URI: license.txt |
19
|
|
|
* Text Domain: podlove-subscribe-button |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** Check if PHP version is sufficient */ |
23
|
|
|
if ( ! version_compare( phpversion(), '5.4', ">=" ) ) { |
|
|
|
|
24
|
|
|
|
25
|
|
|
function podlove_psb_php_notice() { |
|
|
|
|
26
|
|
|
?> |
27
|
|
|
<div id="message" class="error"> |
28
|
|
|
<p> |
29
|
|
|
<strong>The Podlove Subscribe Button Plugin could not be activated</strong> |
30
|
|
|
</p> |
31
|
|
|
<p> |
32
|
|
|
The Podlove Subscribe Button Plugin requires <code>PHP 5.3</code> or higher.<br> |
33
|
|
|
You are running <code>PHP <?php echo phpversion(); ?></code>.<br> |
|
|
|
|
34
|
|
|
Please ask your hoster how to upgrade to an up-to-date PHP version. |
35
|
|
|
</p> |
36
|
|
|
</div> |
37
|
|
|
<?php |
38
|
|
|
} |
|
|
|
|
39
|
|
|
|
40
|
|
|
function podlove_psb_deactivate() { |
|
|
|
|
41
|
|
|
deactivate_plugins( plugin_basename( __FILE__ ) ); |
42
|
|
|
} |
|
|
|
|
43
|
|
|
|
44
|
|
|
add_action( 'admin_notices', 'podlove_psb_php_notice' ); |
45
|
|
|
add_action( 'admin_init', 'podlove_psb_deactivate' ); |
46
|
|
|
|
47
|
|
|
return; |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
require_once __DIR__ . '/vendor/autoload.php'; |
52
|
|
|
|
53
|
|
|
register_activation_hook( __FILE__, array( 'PodloveSubscribeButton\Setup', 'activation' ) ); |
|
|
|
|
54
|
|
|
register_deactivation_hook( __FILE__, array( 'PodloveSubscribeButton\Setup', 'deactivation' ) ); |
55
|
|
|
register_uninstall_hook( __FILE__, array( 'PodloveSubscribeButton\Setup', 'uninstall' ) ); |
|
|
|
|
56
|
|
|
|
57
|
|
|
PodloveSubscribeButton\Migration::eval_db(); |
58
|
|
|
PodloveSubscribeButton::run(); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Class PodloveSubscribeButton |
62
|
|
|
*/ |
63
|
|
|
class PodloveSubscribeButton { |
64
|
|
|
|
65
|
|
|
/** |
|
|
|
|
66
|
|
|
* @var string current plugin version |
67
|
|
|
*/ |
68
|
|
|
public static $version = '1.4.0-beta'; |
69
|
|
|
|
70
|
|
|
public static function run() { |
|
|
|
|
71
|
|
|
|
72
|
|
|
add_action( 'plugins_loaded', array( __CLASS__, 'load_translations' ) ); |
73
|
|
|
add_action( 'init', array( __CLASS__, 'register_shortcode' ) ); |
74
|
|
|
add_action( 'admin_init', array( 'PodloveSubscribeButton\Options', 'register_settings' ) ); |
75
|
|
|
add_action( 'admin_init', array( 'PodloveSubscribeButton\Settings\Buttons', 'process_form' ) ); |
76
|
|
|
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) ); |
77
|
|
|
add_action( 'widgets_init', array( __CLASS__, 'widgets' ) ); |
78
|
|
|
add_action( 'network_admin_edit_podlove_psb_update_network_options', array( 'PodloveSubscribeButton\Settings\Buttons', 'podlove_psb_update_network_options' ) ); |
79
|
|
|
|
80
|
|
|
if ( is_multisite() ) { |
81
|
|
|
add_filter( 'podlove_psb_defaults_options', array( __CLASS__, 'get_network_defaults' ) ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
self::menu(); |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public static function get_network_defaults( $options ) { |
|
|
|
|
89
|
|
|
$network_defaults = get_site_option( 'podlove_psb_defaults' ); |
90
|
|
|
return $network_defaults; |
91
|
|
|
} |
|
|
|
|
92
|
|
|
|
93
|
|
|
public static function widgets() { |
|
|
|
|
94
|
|
|
register_widget( '\PodloveSubscribeButton\Widget' ); |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public static function menu() { |
|
|
|
|
99
|
|
|
add_action( 'admin_menu', array( 'PodloveSubscribeButton', 'admin_menu' ) ); |
100
|
|
|
|
101
|
|
|
if ( is_network_admin() ) { |
102
|
|
|
add_action( 'network_admin_menu', array( 'PodloveSubscribeButton', 'admin_network_menu' ) ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public static function enqueue_assets( $hook ) { |
|
|
|
|
108
|
|
|
|
109
|
|
|
$pages = array( 'settings_page_podlove-subscribe-button', 'widgets.php' ); |
110
|
|
|
|
111
|
|
|
if ( ! in_array( $hook, $pages ) ) { |
|
|
|
|
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// CSS Stylesheet |
|
|
|
|
116
|
|
|
wp_register_style( 'podlove-subscribe-button', \PodloveSubscribeButton\Helpers::get_url( '' ) . 'css/style.css' , false, self::$version ); |
|
|
|
|
117
|
|
|
wp_enqueue_style( 'podlove-subscribe-button' ); |
118
|
|
|
|
119
|
|
|
// Admin JS |
|
|
|
|
120
|
|
|
wp_enqueue_style( 'wp-color-picker' ); |
121
|
|
|
wp_register_script( 'podlove-subscribe-button-admin-tools', \PodloveSubscribeButton\Helpers::get_url( '' ) . 'js/admin.js', array( 'jquery', 'wp-color-picker' ), self::$version ); |
|
|
|
|
122
|
|
|
$js_translations = array( |
123
|
|
|
'media_library' => __( 'Media Library', 'podlove-subscribe-button' ), |
124
|
|
|
'use_for' => __( 'Use for Podcast Cover Art', 'podlove-subscribe-button' ), |
125
|
|
|
); |
126
|
|
|
wp_localize_script( 'podlove-subscribe-button-admin-tools', 'i18n', $js_translations ); |
127
|
|
|
wp_enqueue_script( 'podlove-subscribe-button-admin-tools' ); |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public static function admin_menu() { |
|
|
|
|
132
|
|
|
add_options_page( |
133
|
|
|
__( 'Podlove Subscribe Button Options', 'podlove-subscribe-button' ), |
134
|
|
|
__( 'Podlove Subscribe Button', 'podlove-subscribe-button' ), |
135
|
|
|
'manage_options', |
136
|
|
|
'podlove-subscribe-button', |
137
|
|
|
array( 'PodloveSubscribeButton\Settings\Buttons', 'page' ) |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public static function admin_network_menu() { |
|
|
|
|
143
|
|
|
add_submenu_page( |
144
|
|
|
'settings.php', |
145
|
|
|
__( 'Podlove Subscribe Button Options', 'podlove-subscribe-button' ), |
146
|
|
|
__( 'Podlove Subscribe Button', 'podlove-subscribe-button' ), |
147
|
|
|
'manage_options', |
148
|
|
|
'podlove-subscribe-button', |
149
|
|
|
array( 'PodloveSubscribeButton\Settings\Buttons', 'page' ) |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public static function load_translations() { |
|
|
|
|
155
|
|
|
load_plugin_textdomain( 'podlove-subscribe-button' ); |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
|
|
|
|
160
|
|
|
* Get value from the associative array of the plugin defaults option |
161
|
|
|
* |
162
|
|
|
* @param $key |
|
|
|
|
163
|
|
|
* @param bool $default |
|
|
|
|
164
|
|
|
* |
165
|
|
|
* @return string|bool |
166
|
|
|
*/ |
167
|
|
|
public static function get_option( $key, $default = false ) { |
|
|
|
|
168
|
|
|
|
169
|
|
|
$options = \get_option( 'podlove_psb_defaults' ); |
170
|
|
|
|
171
|
|
|
if ( array_key_exists( $key, $options ) ) { |
|
|
|
|
172
|
|
|
return $options[ $key ]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $default; |
176
|
|
|
} // END get_option() |
|
|
|
|
177
|
|
|
|
178
|
|
|
public static function register_shortcode() { |
|
|
|
|
179
|
|
|
add_shortcode( 'podlove-subscribe-button', array( 'PodloveSubscribeButton', 'shortcode' ) ); |
180
|
|
|
} |
|
|
|
|
181
|
|
|
|
182
|
|
|
/** |
|
|
|
|
183
|
|
|
* Add the shortcode |
184
|
|
|
* |
185
|
|
|
* @param $args |
|
|
|
|
186
|
|
|
* |
187
|
|
|
* @return string|void |
188
|
|
|
*/ |
189
|
|
|
public static function shortcode( $args ) { |
190
|
|
|
if ( ! $args || ! isset( $args[ 'button' ] ) ) { |
|
|
|
|
191
|
|
|
return __( 'You need to create a Button first and provide its ID.', 'podlove-subscribe-button' ); |
192
|
|
|
} else { |
193
|
|
|
$buttonid = $args[ 'button' ]; |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// Fetch the (network)button by it's name |
|
|
|
|
197
|
|
|
if ( ! $button = \PodloveSubscribeButton\Model\Button::get_button_by_name( $args[ 'button' ] ) ) |
|
|
|
|
198
|
|
|
return sprintf( __( 'Oops. There is no button with the ID "%s".', 'podlove-subscribe-button' ), $args[ 'button' ] ); |
|
|
|
|
199
|
|
|
|
200
|
|
|
// Get button styling and options |
|
|
|
|
201
|
|
|
$autowidth = self::interpret_width_attribute( self::get_array_value_with_fallback( $args, 'width' ) ); |
|
|
|
|
202
|
|
|
$size = self::get_attribute( 'size', self::get_array_value_with_fallback( $args, 'size' ) ); |
|
|
|
|
203
|
|
|
$style = self::get_attribute( 'style', self::get_array_value_with_fallback( $args, 'style' ) ); |
204
|
|
|
$format = self::get_attribute( 'format', self::get_array_value_with_fallback( $args, 'format' ) ); |
205
|
|
|
|
206
|
|
|
if ( isset( $args[ 'language' ] ) ) { |
|
|
|
|
207
|
|
|
$language = $args[ 'language' ]; |
|
|
|
|
208
|
|
|
} else { |
209
|
|
|
$language = self::get_attribute( 'language', self::get_array_value_with_fallback( $args, 'language' ) ); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if ( isset( $args[ 'color' ] ) ) { |
|
|
|
|
213
|
|
|
$color = $args[ 'color' ]; |
|
|
|
|
214
|
|
|
} else { |
215
|
|
|
$color = self::get_attribute( 'color', self::get_array_value_with_fallback( $args, 'color' ) ); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
if ( isset( $args[ 'hide' ] ) && $args[ 'hide' ] == 'true' ) { |
|
|
|
|
219
|
|
|
$hide = true; |
220
|
|
|
} else { |
221
|
|
|
$hide = false; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// Render button |
|
|
|
|
225
|
|
|
return $button->render( $size, $autowidth, $style, $format, $color, $hide, $buttonid, $language ); |
226
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public static function get_array_value_with_fallback( $args, $key ) { |
|
|
|
|
230
|
|
|
if ( isset( $args[ $key ] ) ) { |
231
|
|
|
return $args[ $key ]; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return false; |
235
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
|
|
|
|
239
|
|
|
* @param string $attribute |
|
|
|
|
240
|
|
|
* @param string $attribute_value |
|
|
|
|
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
|
|
private static function get_attribute( $attribute = null, $attribute_value = null ) { |
|
|
|
|
244
|
|
|
if ( isset( $attribute_value ) && ctype_alnum( $attribute_value ) && key_exists( $attribute_value, \PodloveSubscribeButton\Model\Button::$$attribute ) ) { |
245
|
|
|
return $attribute_value; |
246
|
|
|
} else { |
247
|
|
|
$default = get_option( 'podlove_psb_defaults', \PodloveSubscribeButton\Defaults::options() ); |
248
|
|
|
return $default[ $attribute ]; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Interprets the provided width attribute and return either auto- or a specific width |
255
|
|
|
* |
256
|
|
|
* @param string $width_attribute |
|
|
|
|
257
|
|
|
* @return string |
258
|
|
|
*/ |
259
|
|
|
private static function interpret_width_attribute( $width_attribute = null ) { |
|
|
|
|
260
|
|
|
if ( $width_attribute == 'auto' ) { |
|
|
|
|
261
|
|
|
return 'on'; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
if ( $width_attribute && $width_attribute !== 'auto' ) { |
|
|
|
|
265
|
|
|
return 'off'; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return self::get_option( 'autowidth', 'on' ); |
|
|
|
|
269
|
|
|
|
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public static function plugin_file() { |
|
|
|
|
273
|
|
|
return __FILE__; |
274
|
|
|
} |
|
|
|
|
275
|
|
|
|
276
|
|
|
} // END class |
277
|
|
|
|