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