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