Passed
Push — master ( d5badb...35381c )
by Christian
21s
created

podlove.php (1 issue)

1
<?php
2
/**
3
 * Plugin Name: Podlove Subscribe Button
4
 * Plugin URI:  http://wordpress.org/extend/plugins/podlove-subscribe-button/
5
 * Description: Brings the Podlove Subscribe Button to your WordPress installation.
6
 * Version:     1.3.4
7
 * Author:      Podlove
8
 * Author URI:  http://podlove.org
9
 * License:     MIT
10
 * License URI: license.txt
11
 * Text Domain: podlove-subscribe-button
12
 */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @package tag in file comment
Loading history...
13
14
$correct_php_version = version_compare( phpversion(), "5.3", ">=" );
15
16
if ( ! $correct_php_version ) {
17
	_e("Podlove Subscribe Button Plugin requires <strong>PHP 5.3</strong> or higher.<br>", 'podlove-subscribe-button');
18
	echo __("You are running PHP ", 'podlove-subscribe-button') . phpversion();
19
	exit;
20
}
21
22
// Constants
23
require('constants.php');
24
require('settings/buttons.php');
25
// Models
26
require('model/base.php');
27
require('model/button.php');
28
require('model/network_button.php');
29
// Table
30
require('settings/buttons_list_table.php');
31
// Media Types
32
require('media_types.php');
33
// Widget
34
require('widget.php');
35
// Version control
36
require('version.php');
37
// Helper functions
38
require('helper.php');
39
40
add_action( 'admin_menu', array( 'PodloveSubscribeButton', 'admin_menu') );
41
if ( is_multisite() )
42
	add_action( 'network_admin_menu', array( 'PodloveSubscribeButton', 'admin_network_menu') );
43
44
add_action( 'admin_init', array( 'PodloveSubscribeButton\Settings\Buttons', 'process_form' ) );
45
register_activation_hook( __FILE__, array( 'PodloveSubscribeButton', 'build_models' ) );
46
47
// Register Settings
48
add_action( 'admin_init', function () {
49
	$settings = array('size', 'autowidth', 'style', 'format', 'color');
50
51
	foreach ($settings as $setting) {
52
		register_setting( 'podlove-subscribe-button', 'podlove_subscribe_button_default_' . $setting );
53
	}
54
} );
55
56
add_shortcode( 'podlove-subscribe-button', array( 'PodloveSubscribeButton', 'shortcode' ) );
57
58
add_action( 'plugins_loaded', function () {
59
	load_plugin_textdomain( 'podlove-subscribe-button' );
60
} );
61
62
PodloveSubscribeButton::run();
63
64
65
class PodloveSubscribeButton {
66
67
	public static function run() {
68
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
69
	}
70
71
	public static function enqueue_assets( $hook ) {
72
73
		$pages = array( 'settings_page_podlove-subscribe-button', 'widgets.php' );
74
75
		if ( ! in_array( $hook, $pages )  ) {
76
			return;
77
		}
78
79
		// CSS Stylesheet
80
		wp_register_style( 'podlove-subscribe-button', plugin_dir_url( __FILE__ ) . 'style.css', false, '1.3.3' );
81
		wp_enqueue_style( 'podlove-subscribe-button' );
82
83
		// Spectrum JS
84
		wp_enqueue_style( 'podlove-subscribe-button-spectrum', plugin_dir_url( __FILE__ ) . 'js/spectrum/spectrum.css', array(), '1.8.0' );
85
		wp_enqueue_script( 'podlove-subscribe-button-spectrum', plugin_dir_url( __FILE__ ) . 'js/spectrum/spectrum.js', array( 'jquery' ), '1.8.0' );
86
87
		// Admin JS
88
		wp_register_script( 'podlove-subscribe-button-admin-tools', plugin_dir_url( __FILE__ ) . 'js/admin.js', array( 'jquery' ), '1.3.3' );
89
		$js_translations = array(
90
			'select_color'  => __( 'Select Color', 'podlove-subscribe-button' ),
91
			'cancel'        => __( 'Cancel', 'podlove-subscribe-button' ),
92
			'media_library' => __( 'Media Library', 'podlove-subscribe-button' ),
93
			'use_for'       => __( 'Use for Podcast Cover Art', 'podlove-subscribe-button' ),
94
		);
95
		wp_localize_script( 'podlove-subscribe-button-admin-tools', 'i18n', $js_translations );
96
		wp_enqueue_script( 'podlove-subscribe-button-admin-tools' );
97
	}
98
99
	public static function admin_menu() {
100
		add_options_page(
101
				'Podlove Subscribe Button Options',
102
				'Podlove Subscribe Button',
103
				'manage_options',
104
				'podlove-subscribe-button',
105
				array( 'PodloveSubscribeButton\Settings\Buttons', 'page')
106
			);
107
	}
108
109
	public static function admin_network_menu() {
110
		add_submenu_page(
111
				'settings.php',
112
				'Podlove Subscribe Button Options',
113
				'Podlove Subscribe Button',
114
				'manage_options',
115
				'podlove-subscribe-button',
116
				array( 'PodloveSubscribeButton\Settings\Buttons', 'page')
117
			);
118
	}
119
120
	public static function build_models() {
121
		// Build Databases
122
		\PodloveSubscribeButton\Model\Button::build();
123
		if ( is_multisite() )
124
			\PodloveSubscribeButton\Model\NetworkButton::build();
125
126
		// Set Button "default" values
127
		$default_values = array(
128
				'size' => 'big',
129
				'autowidth' => 'on',
130
				'color' => '#599677',
131
				'style' => 'filled',
132
				'format' => 'rectangle'
133
			);
134
135
		foreach ($default_values as $option => $default_value) {
136
			if ( ! get_option('podlove_subscribe_button_default_' . $option ) ) {
137
				update_option('podlove_subscribe_button_default_' . $option, $default_value);
138
			}
139
		}
140
	}
141
142
	public static function shortcode( $args ) {
143
		if ( ! $args || ! isset($args['button']) ) {
144
			return __('You need to create a Button first and provide its ID.', 'podlove-subscribe-button');
145
		} else {
146
			$buttonid = $args['button'];
147
		}
148
149
		// Fetch the (network)button by it's name
150
		if ( ! $button = \PodloveSubscribeButton\Model\Button::get_button_by_name($args['button']) )
151
			return sprintf( __('Oops. There is no button with the ID "%s".', 'podlove-subscribe-button'), $args['button'] );
152
153
		// Get button styling and options
154
		$autowidth = self::interpret_width_attribute( self::get_array_value_with_fallback($args, 'width') );
155
		$size = self::get_attribute( 'size', self::get_array_value_with_fallback($args, 'size') );
156
		$style = self::get_attribute( 'style', self::get_array_value_with_fallback($args, 'style') );
157
		$format = self::get_attribute( 'format', self::get_array_value_with_fallback($args, 'format') );
158
		$color = self::get_attribute( 'color', self::get_array_value_with_fallback($args, 'color') );
159
160
		if ( isset($args['language']) ) {
161
			$language = $args['language'];
162
		} else {
163
			$language = 'en';
164
		}
165
166
		if ( isset($args['color']) ) {
167
			$color = $args['color'];
168
		} else {
169
			$color = get_option('podlove_subscribe_button_default_color', '#599677');
170
		}
171
172
		if ( isset($args['hide']) && $args['hide'] == 'true' ) {
173
			$hide = TRUE;
174
		} else {
175
			$hide = FALSE;
176
		}
177
178
		// Render button
179
		return $button->render($size, $autowidth, $style, $format, $color, $hide, $buttonid, $language);
180
	}
181
182
	public static function get_array_value_with_fallback($args, $key) {
183
		if ( isset($args[$key]) )
184
			return $args[$key];
185
186
		return FALSE;
187
	}
188
189
	/**
190
	 * @param  string $attribute
191
	 * @param  string $attribute_value
192
	 * @return string
193
	 */
194
	private static function get_attribute($attribute=NULL, $attribute_value=NULL) {
195
		if ( isset($attribute_value) && ctype_alnum($attribute_value) && key_exists( $attribute_value, \PodloveSubscribeButton\Model\Button::$$attribute ) ) {
196
			return $attribute_value;
197
		} else {
198
			return get_option('podlove_subscribe_button_default_' . $attribute, \PodloveSubscribeButton\Model\Button::$properties[$attribute]);
199
		}
200
	}
201
202
	/**
203
	 * Interprets the provided width attribute and return either auto- or a specific width
204
	 * @param  string $width_attribute
205
	 * @return string
206
	 */
207
	private static function interpret_width_attribute( $width_attribute = NULL ) {
208
		if ( $width_attribute == 'auto' )
209
			return 'on';
210
		if ( $width_attribute && $width_attribute !== 'auto' )
211
			return 'off';
212
213
		return get_option('podlove_subscribe_button_default_autowidth', 'on');
214
	}
215
}