| 1 | <?php |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 2 | /** |
||
| 3 | * Plugin Name: Podlove Subscribe Button |
||
| 4 | * Plugin URI: https://wordpress.org/plugins/podlove-subscribe-button/ |
||
| 5 | * Description: Brings the Podlove Subscribe Button to your WordPress installation. |
||
| 6 | * Version: 1.4.0-beta |
||
| 7 | * Author: Podlove |
||
| 8 | * Author URI: https://podlove.org/ |
||
| 9 | * License: MIT |
||
| 10 | * License URI: license.txt |
||
| 11 | * Text Domain: podlove-subscribe-button |
||
| 12 | */ |
||
|
0 ignored issues
–
show
|
|||
| 13 | |||
| 14 | $correct_php_version = version_compare( phpversion(), "5.3", ">=" ); |
||
| 15 | |||
| 16 | if ( ! $correct_php_version ) { |
||
| 17 | printf( __( 'Podlove Subscribe Button Plugin requires %s or higher.<br>', 'podlove-subscribe-button' ), '<code>PHP 5.3</code>' ); |
||
| 18 | echo '<br />'; |
||
| 19 | printf( __( 'You are running %s', 'podlove-subscribe-button' ), '<code>PHP ' . phpversion() . '</code>' ); |
||
| 20 | exit; |
||
| 21 | } |
||
| 22 | |||
| 23 | // Constants |
||
| 24 | require( 'constants.php' ); |
||
| 25 | require( 'settings/buttons.php' ); |
||
| 26 | // Models |
||
| 27 | require( 'model/base.php' ); |
||
| 28 | require( 'model/button.php' ); |
||
| 29 | require( 'model/network_button.php' ); |
||
| 30 | // Table |
||
| 31 | require( 'settings/buttons_list_table.php' ); |
||
| 32 | // Media Types |
||
| 33 | require( 'media_types.php' ); |
||
| 34 | // Widget |
||
| 35 | require( 'widget.php' ); |
||
| 36 | // Version control |
||
| 37 | require( 'version.php' ); |
||
| 38 | // Helper functions |
||
| 39 | require( 'helper.php' ); |
||
| 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', plugin_dir_url( __FILE__ ) . 'style.css', false, self::$version ); |
||
| 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', plugin_dir_url( __FILE__ ) . 'js/admin.js', array( 'jquery', 'wp-color-picker' ), self::$version ); |
||
| 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 | public static function register_shortcode() { |
||
| 148 | add_shortcode( 'podlove-subscribe-button', array( 'PodloveSubscribeButton', 'shortcode' ) ); |
||
| 149 | |||
| 150 | } |
||
| 151 | |||
| 152 | public static function build_models() { |
||
| 153 | // Build Databases |
||
| 154 | \PodloveSubscribeButton\Model\Button::build(); |
||
| 155 | |||
| 156 | if ( is_multisite() ) { |
||
| 157 | \PodloveSubscribeButton\Model\NetworkButton::build(); |
||
| 158 | } |
||
| 159 | |||
| 160 | // Set Button "default" values |
||
| 161 | $default_values = array( |
||
| 162 | 'size' => 'big', |
||
| 163 | 'autowidth' => 'on', |
||
| 164 | 'color' => '#599677', |
||
| 165 | 'style' => 'filled', |
||
| 166 | 'format' => 'rectangle' |
||
| 167 | ); |
||
| 168 | |||
| 169 | foreach ( $default_values as $option => $default_value ) { |
||
| 170 | if ( ! get_option( 'podlove_subscribe_button_default_' . $option ) ) { |
||
| 171 | update_option( 'podlove_subscribe_button_default_' . $option, $default_value ); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | } |
||
| 176 | |||
| 177 | public static function shortcode( $args ) { |
||
| 178 | if ( ! $args || ! isset( $args[ 'button' ] ) ) { |
||
| 179 | return __( 'You need to create a Button first and provide its ID.', 'podlove-subscribe-button' ); |
||
| 180 | } else { |
||
| 181 | $buttonid = $args[ 'button' ]; |
||
| 182 | } |
||
| 183 | |||
| 184 | // Fetch the (network)button by it's name |
||
| 185 | if ( ! $button = \PodloveSubscribeButton\Model\Button::get_button_by_name( $args[ 'button' ] ) ) |
||
| 186 | return sprintf( __( 'Oops. There is no button with the ID "%s".', 'podlove-subscribe-button' ), $args[ 'button' ] ); |
||
| 187 | |||
| 188 | // Get button styling and options |
||
| 189 | $autowidth = self::interpret_width_attribute( self::get_array_value_with_fallback( $args, 'width' ) ); |
||
| 190 | $size = self::get_attribute( 'size', self::get_array_value_with_fallback( $args, 'size' ) ); |
||
| 191 | $style = self::get_attribute( 'style', self::get_array_value_with_fallback( $args, 'style' ) ); |
||
| 192 | $format = self::get_attribute( 'format', self::get_array_value_with_fallback( $args, 'format' ) ); |
||
| 193 | $color = self::get_attribute( 'color', self::get_array_value_with_fallback( $args, 'color' ) ); |
||
| 194 | |||
| 195 | if ( isset( $args[ 'language' ] ) ) { |
||
| 196 | $language = $args[ 'language' ]; |
||
| 197 | } else { |
||
| 198 | $language = 'en'; |
||
| 199 | } |
||
| 200 | |||
| 201 | if ( isset( $args[ 'color' ] ) ) { |
||
| 202 | $color = $args[ 'color' ]; |
||
| 203 | } else { |
||
| 204 | $color = get_option( 'podlove_subscribe_button_default_color', '#599677' ); |
||
| 205 | } |
||
| 206 | |||
| 207 | if ( isset( $args[ 'hide' ] ) && $args[ 'hide' ] == 'true' ) { |
||
| 208 | $hide = true; |
||
| 209 | } else { |
||
| 210 | $hide = false; |
||
| 211 | } |
||
| 212 | |||
| 213 | // Render button |
||
| 214 | return $button->render( $size, $autowidth, $style, $format, $color, $hide, $buttonid, $language ); |
||
| 215 | |||
| 216 | } |
||
| 217 | |||
| 218 | public static function get_array_value_with_fallback( $args, $key ) { |
||
| 219 | if ( isset( $args[ $key ] ) ) { |
||
| 220 | return $args[ $key ]; |
||
| 221 | } |
||
| 222 | |||
| 223 | return false; |
||
| 224 | |||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $attribute |
||
| 229 | * @param string $attribute_value |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | private static function get_attribute( $attribute = null, $attribute_value = null ) { |
||
| 233 | if ( isset( $attribute_value ) && ctype_alnum( $attribute_value ) && key_exists( $attribute_value, \PodloveSubscribeButton\Model\Button::$$attribute ) ) { |
||
| 234 | return $attribute_value; |
||
| 235 | } else { |
||
| 236 | return get_option( 'podlove_subscribe_button_default_' . $attribute, \PodloveSubscribeButton\Model\Button::$properties[ $attribute ] ); |
||
| 237 | } |
||
| 238 | |||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Interprets the provided width attribute and return either auto- or a specific width |
||
| 243 | * |
||
| 244 | * @param string $width_attribute |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | private static function interpret_width_attribute( $width_attribute = null ) { |
||
| 248 | if ( $width_attribute == 'auto' ) { |
||
| 249 | return 'on'; |
||
| 250 | } |
||
| 251 | |||
| 252 | if ( $width_attribute && $width_attribute !== 'auto' ) { |
||
| 253 | return 'off'; |
||
| 254 | } |
||
| 255 | |||
| 256 | return get_option( 'podlove_subscribe_button_default_autowidth', 'on' ); |
||
| 257 | |||
| 258 | } |
||
| 259 | |||
| 260 | } // END class |
||
| 261 |