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' ); |
||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||
40 | |||||
41 | register_activation_hook( __FILE__, array( 'PodloveSubscribeButton', 'build_models' ) ); |
||||
42 | |||||
43 | PodloveSubscribeButton::run(); |
||||
44 | |||||
45 | /** |
||||
46 | * Class PodloveSubscribeButton |
||||
47 | */ |
||||
48 | class PodloveSubscribeButton { |
||||
49 | |||||
50 | /** |
||||
51 | * @var string current plugin version |
||||
52 | */ |
||||
53 | public static $version = '1.4.0-beta'; |
||||
54 | |||||
55 | public static function run() { |
||||
56 | add_action( 'plugins_loaded', array( __CLASS__, 'load_translations' ) ); |
||||
57 | add_action( 'init', array( __CLASS__, 'register_shortcode' ) ); |
||||
58 | add_action( 'admin_init', array( __CLASS__, 'register_settings' ) ); |
||||
59 | add_action( 'admin_init', array( 'PodloveSubscribeButton\Settings\Buttons', 'process_form' ) ); |
||||
60 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) ); |
||||
61 | add_action( 'widgets_init', array( __CLASS__, 'widgets' ) ); |
||||
62 | self::menu(); |
||||
63 | |||||
64 | } |
||||
65 | |||||
66 | public static function widgets() { |
||||
67 | register_widget( '\PodloveSubscribeButton\Widget' ); |
||||
68 | |||||
69 | } |
||||
70 | |||||
71 | public static function menu() { |
||||
72 | add_action( 'admin_menu', array( 'PodloveSubscribeButton', 'admin_menu' ) ); |
||||
73 | |||||
74 | if ( is_network_admin() ) { |
||||
75 | add_action( 'network_admin_menu', array( 'PodloveSubscribeButton', 'admin_network_menu' ) ); |
||||
76 | } |
||||
77 | |||||
78 | } |
||||
79 | |||||
80 | public static function enqueue_assets( $hook ) { |
||||
81 | |||||
82 | $pages = array( 'settings_page_podlove-subscribe-button', 'widgets.php' ); |
||||
83 | |||||
84 | if ( ! in_array( $hook, $pages ) ) { |
||||
85 | return; |
||||
86 | } |
||||
87 | |||||
88 | // CSS Stylesheet |
||||
89 | wp_register_style( 'podlove-subscribe-button', \PodloveSubscribeButton\Helpers::get_url( '' ) . 'css/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 | // Admin JS |
||||
93 | wp_enqueue_style( 'wp-color-picker' ); |
||||
94 | wp_register_script( 'podlove-subscribe-button-admin-tools', \PodloveSubscribeButton\Helpers::get_url( '' ) . 'js/admin.js', array( 'jquery', 'wp-color-picker' ), self::$version ); |
||||
0 ignored issues
–
show
|
|||||
95 | $js_translations = array( |
||||
96 | 'media_library' => __( 'Media Library', 'podlove-subscribe-button' ), |
||||
97 | 'use_for' => __( 'Use for Podcast Cover Art', 'podlove-subscribe-button' ), |
||||
98 | ); |
||||
99 | wp_localize_script( 'podlove-subscribe-button-admin-tools', 'i18n', $js_translations ); |
||||
100 | wp_enqueue_script( 'podlove-subscribe-button-admin-tools' ); |
||||
101 | |||||
102 | } |
||||
103 | |||||
104 | public static function admin_menu() { |
||||
105 | add_options_page( |
||||
106 | __( 'Podlove Subscribe Button Options', 'podlove-subscribe-button' ), |
||||
107 | __( 'Podlove Subscribe Button', 'podlove-subscribe-button' ), |
||||
108 | 'manage_options', |
||||
109 | 'podlove-subscribe-button', |
||||
110 | array( 'PodloveSubscribeButton\Settings\Buttons', 'page' ) |
||||
111 | ); |
||||
112 | |||||
113 | } |
||||
114 | |||||
115 | public static function admin_network_menu() { |
||||
116 | add_submenu_page( |
||||
117 | 'settings.php', |
||||
118 | __( 'Podlove Subscribe Button Options', 'podlove-subscribe-button' ), |
||||
119 | __( 'Podlove Subscribe Button', 'podlove-subscribe-button' ), |
||||
120 | 'manage_options', |
||||
121 | 'podlove-subscribe-button', |
||||
122 | array( 'PodloveSubscribeButton\Settings\Buttons', 'page' ) |
||||
123 | ); |
||||
124 | |||||
125 | } |
||||
126 | |||||
127 | public static function load_translations() { |
||||
128 | load_plugin_textdomain( 'podlove-subscribe-button' ); |
||||
129 | |||||
130 | } |
||||
131 | |||||
132 | public static function register_settings() { |
||||
133 | $settings = array( |
||||
134 | 'size', |
||||
135 | 'autowidth', |
||||
136 | 'style', |
||||
137 | 'format', |
||||
138 | 'color', |
||||
139 | ); |
||||
140 | |||||
141 | foreach ( $settings as $setting ) { |
||||
142 | register_setting( 'podlove-subscribe-button', 'podlove_subscribe_button_default_' . $setting ); |
||||
143 | } |
||||
144 | |||||
145 | } |
||||
146 | |||||
147 | static function get_option( $key, $default = false ) { |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
It is recommend to declare an explicit visibility for
get_option .
Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed. If you are not sure which visibility to choose, it is a good idea to start with
the most restrictive visibility, and then raise visibility as needed, i.e.
start with ![]() |
|||||
148 | |||||
149 | return \get_option( 'podlove_subscribe_button_default_' . $key, $default ); |
||||
150 | |||||
151 | } |
||||
152 | |||||
153 | public static function register_shortcode() { |
||||
154 | add_shortcode( 'podlove-subscribe-button', array( 'PodloveSubscribeButton', 'shortcode' ) ); |
||||
155 | |||||
156 | } |
||||
157 | |||||
158 | public static function build_models() { |
||||
159 | // Build Databases |
||||
160 | \PodloveSubscribeButton\Model\Button::build(); |
||||
161 | |||||
162 | if ( is_multisite() ) { |
||||
163 | \PodloveSubscribeButton\Model\NetworkButton::build(); |
||||
164 | } |
||||
165 | |||||
166 | $default_values = \PodloveSubscribeButton\Defaults::options(); |
||||
167 | |||||
168 | foreach ( $default_values as $option => $default_value ) { |
||||
169 | if ( ! get_option( 'podlove_subscribe_button_default_' . $option ) ) { |
||||
170 | update_option( 'podlove_subscribe_button_default_' . $option, $default_value ); |
||||
171 | } |
||||
172 | } |
||||
173 | |||||
174 | } |
||||
175 | |||||
176 | /** |
||||
177 | * Add the shortcode |
||||
178 | * |
||||
179 | * @param $args |
||||
180 | * |
||||
181 | * @return string|void |
||||
182 | */ |
||||
183 | public static function shortcode( $args ) { |
||||
184 | if ( ! $args || ! isset( $args[ 'button' ] ) ) { |
||||
185 | return __( 'You need to create a Button first and provide its ID.', 'podlove-subscribe-button' ); |
||||
186 | } else { |
||||
187 | $buttonid = $args[ 'button' ]; |
||||
188 | } |
||||
189 | |||||
190 | // Fetch the (network)button by it's name |
||||
191 | if ( ! $button = \PodloveSubscribeButton\Model\Button::get_button_by_name( $args[ 'button' ] ) ) |
||||
192 | return sprintf( __( 'Oops. There is no button with the ID "%s".', 'podlove-subscribe-button' ), $args[ 'button' ] ); |
||||
193 | |||||
194 | // Get button styling and options |
||||
195 | $autowidth = self::interpret_width_attribute( self::get_array_value_with_fallback( $args, 'width' ) ); |
||||
196 | $size = self::get_attribute( 'size', self::get_array_value_with_fallback( $args, 'size' ) ); |
||||
197 | $style = self::get_attribute( 'style', self::get_array_value_with_fallback( $args, 'style' ) ); |
||||
198 | $format = self::get_attribute( 'format', self::get_array_value_with_fallback( $args, 'format' ) ); |
||||
199 | $color = self::get_attribute( 'color', self::get_array_value_with_fallback( $args, 'color' ) ); |
||||
200 | |||||
201 | if ( isset( $args[ 'language' ] ) ) { |
||||
202 | $language = $args[ 'language' ]; |
||||
203 | } else { |
||||
204 | $language = self::get_option( 'language' ); |
||||
205 | } |
||||
206 | |||||
207 | if ( isset( $args[ 'color' ] ) ) { |
||||
208 | $color = $args[ 'color' ]; |
||||
209 | } else { |
||||
210 | $color = self::get_option( 'color' ); |
||||
211 | } |
||||
212 | |||||
213 | if ( isset( $args[ 'hide' ] ) && $args[ 'hide' ] == 'true' ) { |
||||
214 | $hide = true; |
||||
215 | } else { |
||||
216 | $hide = false; |
||||
217 | } |
||||
218 | |||||
219 | // Render button |
||||
220 | return $button->render( $size, $autowidth, $style, $format, $color, $hide, $buttonid, $language ); |
||||
221 | |||||
222 | } |
||||
223 | |||||
224 | public static function get_array_value_with_fallback( $args, $key ) { |
||||
225 | if ( isset( $args[ $key ] ) ) { |
||||
226 | return $args[ $key ]; |
||||
227 | } |
||||
228 | |||||
229 | return false; |
||||
230 | |||||
231 | } |
||||
232 | |||||
233 | /** |
||||
234 | * @param string $attribute |
||||
235 | * @param string $attribute_value |
||||
236 | * @return string |
||||
237 | */ |
||||
238 | private static function get_attribute( $attribute = null, $attribute_value = null ) { |
||||
239 | if ( isset( $attribute_value ) && ctype_alnum( $attribute_value ) && key_exists( $attribute_value, \PodloveSubscribeButton\Model\Button::$$attribute ) ) { |
||||
240 | return $attribute_value; |
||||
241 | } else { |
||||
242 | return get_option( 'podlove_subscribe_button_default_' . $attribute, \PodloveSubscribeButton\Model\Button::$properties[ $attribute ] ); |
||||
243 | } |
||||
244 | |||||
245 | } |
||||
246 | |||||
247 | /** |
||||
248 | * Interprets the provided width attribute and return either auto- or a specific width |
||||
249 | * |
||||
250 | * @param string $width_attribute |
||||
251 | * @return string |
||||
252 | */ |
||||
253 | private static function interpret_width_attribute( $width_attribute = null ) { |
||||
254 | if ( $width_attribute == 'auto' ) { |
||||
255 | return 'on'; |
||||
256 | } |
||||
257 | |||||
258 | if ( $width_attribute && $width_attribute !== 'auto' ) { |
||||
259 | return 'off'; |
||||
260 | } |
||||
261 | |||||
262 | return self::get_option( 'autowidth', 'on' ); |
||||
263 | |||||
264 | } |
||||
265 | |||||
266 | public static function plugin_file() { |
||||
0 ignored issues
–
show
|
|||||
267 | return __FILE__; |
||||
268 | } |
||||
0 ignored issues
–
show
|
|||||
269 | |||||
270 | } // END class |
||||
271 |