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.3', ">=" ) ) { |
|
|
|
|
24
|
|
|
|
25
|
|
|
require_once 'php-version.php'; |
26
|
|
|
add_action( 'admin_notices', 'podlove_psb_php_notice' ); |
27
|
|
|
add_action( 'admin_init', function() { |
|
|
|
|
28
|
|
|
deactivate_plugins( plugin_basename( __FILE__ ) ); |
29
|
|
|
} ); |
|
|
|
|
30
|
|
|
return; |
31
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// Constants |
|
|
|
|
35
|
|
|
require( 'constants.php' ); |
|
|
|
|
36
|
|
|
require( 'settings/buttons.php' ); |
|
|
|
|
37
|
|
|
// Models |
|
|
|
|
38
|
|
|
require( 'model/base.php' ); |
|
|
|
|
39
|
|
|
require( 'model/button.php' ); |
|
|
|
|
40
|
|
|
require( 'model/network_button.php' ); |
|
|
|
|
41
|
|
|
// Table |
|
|
|
|
42
|
|
|
require( 'settings/buttons_list_table.php' ); |
|
|
|
|
43
|
|
|
// Media Types |
|
|
|
|
44
|
|
|
require( 'media_types.php' ); |
|
|
|
|
45
|
|
|
// Widget |
|
|
|
|
46
|
|
|
require( 'widget.php' ); |
|
|
|
|
47
|
|
|
// Version control |
|
|
|
|
48
|
|
|
require( 'version.php' ); |
|
|
|
|
49
|
|
|
// Helper functions |
|
|
|
|
50
|
|
|
require( 'helper.php' ); |
|
|
|
|
51
|
|
|
|
52
|
|
|
register_activation_hook( __FILE__, array( 'PodloveSubscribeButton', 'build_models' ) ); |
53
|
|
|
|
54
|
|
|
PodloveSubscribeButton::run(); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Class PodloveSubscribeButton |
58
|
|
|
*/ |
59
|
|
|
class PodloveSubscribeButton { |
60
|
|
|
|
61
|
|
|
/** |
|
|
|
|
62
|
|
|
* @var string current plugin version |
63
|
|
|
*/ |
64
|
|
|
public static $version = '1.4.0-beta'; |
65
|
|
|
|
66
|
|
|
public static function run() { |
|
|
|
|
67
|
|
|
add_action( 'plugins_loaded', array( __CLASS__, 'load_translations' ) ); |
68
|
|
|
add_action( 'init', array( __CLASS__, 'register_shortcode' ) ); |
69
|
|
|
add_action( 'admin_init', array( __CLASS__, 'register_settings' ) ); |
70
|
|
|
add_action( 'admin_init', array( 'PodloveSubscribeButton\Settings\Buttons', 'process_form' ) ); |
71
|
|
|
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) ); |
72
|
|
|
add_action( 'widgets_init', array( __CLASS__, 'widgets' ) ); |
73
|
|
|
self::menu(); |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function widgets() { |
|
|
|
|
78
|
|
|
register_widget( '\PodloveSubscribeButton\Widget' ); |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function menu() { |
|
|
|
|
83
|
|
|
add_action( 'admin_menu', array( 'PodloveSubscribeButton', 'admin_menu' ) ); |
84
|
|
|
|
85
|
|
|
if ( is_network_admin() ) { |
86
|
|
|
add_action( 'network_admin_menu', array( 'PodloveSubscribeButton', 'admin_network_menu' ) ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public static function enqueue_assets( $hook ) { |
|
|
|
|
92
|
|
|
|
93
|
|
|
$pages = array( 'settings_page_podlove-subscribe-button', 'widgets.php' ); |
94
|
|
|
|
95
|
|
|
if ( ! in_array( $hook, $pages ) ) { |
|
|
|
|
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// CSS Stylesheet |
|
|
|
|
100
|
|
|
wp_register_style( 'podlove-subscribe-button', plugin_dir_url( __FILE__ ) . 'style.css', false, self::$version ); |
|
|
|
|
101
|
|
|
wp_enqueue_style( 'podlove-subscribe-button' ); |
102
|
|
|
|
103
|
|
|
// Admin JS |
|
|
|
|
104
|
|
|
wp_enqueue_style( 'wp-color-picker' ); |
105
|
|
|
wp_register_script( 'podlove-subscribe-button-admin-tools', plugin_dir_url( __FILE__ ) . 'js/admin.js', array( 'jquery', 'wp-color-picker' ), self::$version ); |
|
|
|
|
106
|
|
|
$js_translations = array( |
107
|
|
|
'media_library' => __( 'Media Library', 'podlove-subscribe-button' ), |
108
|
|
|
'use_for' => __( 'Use for Podcast Cover Art', 'podlove-subscribe-button' ), |
109
|
|
|
); |
110
|
|
|
wp_localize_script( 'podlove-subscribe-button-admin-tools', 'i18n', $js_translations ); |
111
|
|
|
wp_enqueue_script( 'podlove-subscribe-button-admin-tools' ); |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public static function admin_menu() { |
|
|
|
|
116
|
|
|
add_options_page( |
117
|
|
|
__( 'Podlove Subscribe Button Options', 'podlove-subscribe-button' ), |
118
|
|
|
__( 'Podlove Subscribe Button', 'podlove-subscribe-button' ), |
119
|
|
|
'manage_options', |
120
|
|
|
'podlove-subscribe-button', |
121
|
|
|
array( 'PodloveSubscribeButton\Settings\Buttons', 'page' ) |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public static function admin_network_menu() { |
|
|
|
|
127
|
|
|
add_submenu_page( |
128
|
|
|
'settings.php', |
129
|
|
|
__( 'Podlove Subscribe Button Options', 'podlove-subscribe-button' ), |
130
|
|
|
__( 'Podlove Subscribe Button', 'podlove-subscribe-button' ), |
131
|
|
|
'manage_options', |
132
|
|
|
'podlove-subscribe-button', |
133
|
|
|
array( 'PodloveSubscribeButton\Settings\Buttons', 'page' ) |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public static function load_translations() { |
|
|
|
|
139
|
|
|
load_plugin_textdomain( 'podlove-subscribe-button' ); |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public static function register_settings() { |
|
|
|
|
144
|
|
|
$settings = array( |
145
|
|
|
'size', |
146
|
|
|
'autowidth', |
147
|
|
|
'style', |
148
|
|
|
'format', |
149
|
|
|
'color', |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
foreach ( $settings as $setting ) { |
153
|
|
|
register_setting( 'podlove-subscribe-button', 'podlove_subscribe_button_default_' . $setting ); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public static function register_shortcode() { |
|
|
|
|
159
|
|
|
add_shortcode( 'podlove-subscribe-button', array( 'PodloveSubscribeButton', 'shortcode' ) ); |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public static function build_models() { |
|
|
|
|
164
|
|
|
// Build Databases |
|
|
|
|
165
|
|
|
\PodloveSubscribeButton\Model\Button::build(); |
166
|
|
|
|
167
|
|
|
if ( is_multisite() ) { |
168
|
|
|
\PodloveSubscribeButton\Model\NetworkButton::build(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// Set Button "default" values |
|
|
|
|
172
|
|
|
$default_values = array( |
173
|
|
|
'size' => 'big', |
174
|
|
|
'autowidth' => 'on', |
175
|
|
|
'color' => '#599677', |
176
|
|
|
'style' => 'filled', |
177
|
|
|
'format' => 'rectangle' |
|
|
|
|
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
foreach ( $default_values as $option => $default_value ) { |
181
|
|
|
if ( ! get_option( 'podlove_subscribe_button_default_' . $option ) ) { |
182
|
|
|
update_option( 'podlove_subscribe_button_default_' . $option, $default_value ); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
|
|
|
|
189
|
|
|
* Add the shortcode |
190
|
|
|
* |
191
|
|
|
* @param $args |
|
|
|
|
192
|
|
|
* |
193
|
|
|
* @return string|void |
194
|
|
|
*/ |
195
|
|
|
public static function shortcode( $args ) { |
196
|
|
|
if ( ! $args || ! isset( $args[ 'button' ] ) ) { |
|
|
|
|
197
|
|
|
return __( 'You need to create a Button first and provide its ID.', 'podlove-subscribe-button' ); |
198
|
|
|
} else { |
199
|
|
|
$buttonid = $args[ 'button' ]; |
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
// Fetch the (network)button by it's name |
|
|
|
|
203
|
|
|
if ( ! $button = \PodloveSubscribeButton\Model\Button::get_button_by_name( $args[ 'button' ] ) ) |
|
|
|
|
204
|
|
|
return sprintf( __( 'Oops. There is no button with the ID "%s".', 'podlove-subscribe-button' ), $args[ 'button' ] ); |
|
|
|
|
205
|
|
|
|
206
|
|
|
// Get button styling and options |
|
|
|
|
207
|
|
|
$autowidth = self::interpret_width_attribute( self::get_array_value_with_fallback( $args, 'width' ) ); |
|
|
|
|
208
|
|
|
$size = self::get_attribute( 'size', self::get_array_value_with_fallback( $args, 'size' ) ); |
|
|
|
|
209
|
|
|
$style = self::get_attribute( 'style', self::get_array_value_with_fallback( $args, 'style' ) ); |
210
|
|
|
$format = self::get_attribute( 'format', self::get_array_value_with_fallback( $args, 'format' ) ); |
211
|
|
|
$color = self::get_attribute( 'color', self::get_array_value_with_fallback( $args, 'color' ) ); |
|
|
|
|
212
|
|
|
|
213
|
|
|
if ( isset( $args[ 'language' ] ) ) { |
|
|
|
|
214
|
|
|
$language = $args[ 'language' ]; |
|
|
|
|
215
|
|
|
} else { |
216
|
|
|
$language = 'en'; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if ( isset( $args[ 'color' ] ) ) { |
|
|
|
|
220
|
|
|
$color = $args[ 'color' ]; |
|
|
|
|
221
|
|
|
} else { |
222
|
|
|
$color = get_option( 'podlove_subscribe_button_default_color', '#599677' ); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
if ( isset( $args[ 'hide' ] ) && $args[ 'hide' ] == 'true' ) { |
|
|
|
|
226
|
|
|
$hide = true; |
227
|
|
|
} else { |
228
|
|
|
$hide = false; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
// Render button |
|
|
|
|
232
|
|
|
return $button->render( $size, $autowidth, $style, $format, $color, $hide, $buttonid, $language ); |
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public static function get_array_value_with_fallback( $args, $key ) { |
|
|
|
|
237
|
|
|
if ( isset( $args[ $key ] ) ) { |
238
|
|
|
return $args[ $key ]; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return false; |
242
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
|
|
|
|
246
|
|
|
* @param string $attribute |
|
|
|
|
247
|
|
|
* @param string $attribute_value |
|
|
|
|
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
|
|
private static function get_attribute( $attribute = null, $attribute_value = null ) { |
|
|
|
|
251
|
|
|
if ( isset( $attribute_value ) && ctype_alnum( $attribute_value ) && key_exists( $attribute_value, \PodloveSubscribeButton\Model\Button::$$attribute ) ) { |
252
|
|
|
return $attribute_value; |
253
|
|
|
} else { |
254
|
|
|
return get_option( 'podlove_subscribe_button_default_' . $attribute, \PodloveSubscribeButton\Model\Button::$properties[ $attribute ] ); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Interprets the provided width attribute and return either auto- or a specific width |
261
|
|
|
* |
262
|
|
|
* @param string $width_attribute |
|
|
|
|
263
|
|
|
* @return string |
264
|
|
|
*/ |
265
|
|
|
private static function interpret_width_attribute( $width_attribute = null ) { |
|
|
|
|
266
|
|
|
if ( $width_attribute == 'auto' ) { |
|
|
|
|
267
|
|
|
return 'on'; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
if ( $width_attribute && $width_attribute !== 'auto' ) { |
|
|
|
|
271
|
|
|
return 'off'; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return get_option( 'podlove_subscribe_button_default_autowidth', 'on' ); |
275
|
|
|
|
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
} // END class |
279
|
|
|
|