Changeset 2920201
- Timestamp:
- 06/01/2023 03:11:38 PM (3 years ago)
- Location:
- ko-fi-button/trunk
- Files:
-
- 3 added
- 4 edited
-
Ko_fi.php (modified) (3 diffs)
-
Readme.txt (modified) (2 diffs)
-
class-default-ko-fi-options.php (modified) (1 diff)
-
class-ko-fi-button-widget.php (added)
-
class-ko-fi-options.php (added)
-
class-ko-fi-panel-widget.php (modified) (1 diff)
-
class-ko-fi.php (added)
Legend:
- Unmodified
- Added
- Removed
-
ko-fi-button/trunk/Ko_fi.php
r2903835 r2920201 1 <?php 1 <?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase (ignore the file name as changing it will deactivate the plugin upon updating) 2 2 /** 3 3 * Plugin Name: Ko-fi Button 4 4 * Plugin URI: 5 5 * Description: A Ko-fi donate button for your website! 6 * Version: 1.3. 36 * Version: 1.3.4 7 7 * Author: Ko-fi Team 8 8 * Author URI: https://www.ko-fi.com … … 16 16 } 17 17 18 if (!function_exists('write_log')) { 19 function write_log ( $log ) { 18 if ( ! function_exists( 'write_log' ) ) { 19 /** 20 * Write to the debug log is `WP_DEBUG` is enabled 21 * Appears to be unused 22 * 23 * @param mixed $log Input to send to the error log. 24 */ 25 function write_log( $log ) { 20 26 if ( true === WP_DEBUG ) { 21 27 if ( is_array( $log ) || is_object( $log ) ) { … … 28 34 } 29 35 30 class Ko_Fi 31 { 32 33 public static $options = []; 34 35 public static function init() 36 { 37 add_action('widgets_init', [__CLASS__, 'widget']); 38 add_action( 'init', array( __CLASS__, 'register_scripts' ) ); 39 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_admin_scripts' ), 9 ); 40 add_action( 'wp_head', array( __CLASS__, 'maybe_display_floating_button' ) ); 41 add_action( 'add_meta_boxes', array( __CLASS__, 'register_posts_meta_box' ) ); 42 add_action( 'save_post', array( __CLASS__, 'save_post_meta' ), 10, 2 ); 43 add_action( 'rest_api_init', array( __CLASS__, 'prevent_floating_button_displaying_on_widget_previews' ) ); 44 45 add_shortcode('kofi', [__CLASS__, 'kofi_shortcode']); 46 47 48 require_once 'class-default-ko-fi-options.php'; 49 require_once 'Ko_fi_Options.php'; 50 self::$options = (new Ko_fi_Options())->get(); 51 add_filter('plugin_action_links', [__CLASS__,'add_action_links'],10,5); 52 register_uninstall_hook( __FILE__, array( __CLASS__,'deactivate' ) ); 53 } 54 55 public static function add_action_links($actions, $plugin_file) 56 { 57 static $plugin; 58 59 if (!isset($plugin)) 60 $plugin = plugin_basename(__FILE__); 61 if ($plugin == $plugin_file) { 62 63 $settings_url = sprintf('<a href="%s">Settings</a>', menu_page_url(Default_ko_fi_options::get()['menu_slug'], false)); 64 $plugin_links = [ 65 'settings' => $settings_url 66 ]; 67 68 $actions = array_merge($plugin_links, $actions); 69 } 70 71 return $actions; 72 } 73 74 /** 75 * Register any custom scripts and styles we'll need 76 */ 77 public static function register_scripts() { 78 if ( ! function_exists( 'get_plugin_data' ) ) { 79 require_once ABSPATH . 'wp-admin/includes/plugin.php'; 80 } 81 $dir_url = plugin_dir_url( __FILE__ ); 82 $plugin_data = get_plugin_data( __FILE__ ); 83 wp_register_script( 'extra', $dir_url . 'extra.js', array( 'jquery' ), $plugin_data['Version'], true ); 84 wp_register_script( 'ko-fi-button-widget', 'https://storage.ko-fi.com/cdn/widget/Widget_2.js', array( 'jquery' ), $plugin_data['Version'], true ); 85 wp_register_script( 'ko-fi-button', trailingslashit( $dir_url ) . 'js/widget.js', array( 'jquery', 'ko-fi-button-widget' ), $plugin_data['Version'], true ); 86 wp_register_script( 'ko-fi-floating-button', 'https://storage.ko-fi.com/cdn/scripts/overlay-widget.js', array( 'jquery' ), $plugin_data['Version'], true ); 87 } 88 89 /** 90 * Enqueue scripts for the admin 91 */ 92 public static function enqueue_admin_scripts() { 93 wp_enqueue_script( 'extra' ); 94 // Color picker. 95 wp_enqueue_style( 'wp-color-picker' ); 96 wp_enqueue_script( 'wp-color-picker' ); 97 } 98 99 /** 100 * Instantiate the widgets 101 */ 102 public static function widget() { 103 require_once 'ko-fi-button-widget.php'; 104 register_widget( 'ko_fi_widget' ); 105 require_once 'class-ko-fi-panel-widget.php'; 106 register_widget( 'ko_fi_panel_widget' ); 107 108 } 109 110 /** 111 * Add Shortcode 112 * 113 * @param array $atts Shortcode attributes. 114 * @return string 115 */ 116 public static function kofi_shortcode( $atts ) { 117 // Attributes. 118 $atts = shortcode_atts( 119 array( 120 'text' => self::get_plugin_option( 'coffee_text' ), 121 'color' => self::get_plugin_option( 'coffee_color' ), 122 'type' => 'button', 123 'code' => self::get_plugin_option( 'coffee_code' ), 124 ), 125 $atts 126 ); 127 128 // Return custom embed code. 129 if ( 'button' === $atts['type'] ) { 130 return self::get_button_embed_code( $atts ); 131 } elseif ( 'panel' === $atts['type'] ) { 132 return self::get_panel_embed_code( $atts ); 133 } 134 } 135 136 public static function get_jscolor( $args ) { 137 $value = ! empty( $args['value'] ) ? '#' . ltrim( $args['value'], '#' ) : $args['value']; 138 echo sprintf( 139 '<input class="jscolor" id="%1$s" name="%2$s" value="%3$s" />', 140 esc_attr( $args['option_id'] ), 141 esc_attr( empty( $args['name'] ) ? $args['option_id'] : $args['name'] ), 142 esc_attr( $value ) 143 ); 144 } 145 146 public static function get_button_embed_code($atts, $widget_id = '') 147 { 148 $settings = wp_parse_args($atts, self::$options); 149 foreach ( $atts as $key => $value ) { 150 switch ($key) { 151 case 'title' : 152 $key = 'coffee_title'; 153 break; 154 case 'text' : 155 $key = 'coffee_text'; 156 break; 157 case 'hyperlink' : 158 $key = 'coffee_hyperlink'; 159 break; 160 case 'color' : 161 $key = 'coffee_color'; 162 $value = '#' . ltrim( $value, '#' ); 163 break; 164 case 'code': 165 $key = 'coffee_code'; 166 if ( empty( $value ) ) { 167 $value = self::sanitise_username( self::get_plugin_option( 'coffee_code' ) ); 168 } else { 169 $value = self::sanitise_username( $value ); 170 } 171 break; 172 case 'button_alignment': 173 $key = 'coffee_button_alignment'; 174 break; 175 } 176 177 $settings[ $key ] = $value; 178 } 179 180 $style_registry = array( 181 'left' => 'float: none; text-align: left;', 182 'right' => 'float: right; text-align: left;', 183 'centre' => 'width: 100%; text-align: center;', 184 ); 185 186 $btn_container_style = $style_registry[ $settings['coffee_button_alignment'] ]; 187 188 if ( ! empty( $settings['coffee_hyperlink'] ) && $settings['coffee_hyperlink'] ) { 189 return sprintf( 190 '<div style="%1$s" class="ko-fi-button"><div class="btn-container"><a href="http://www.ko-fi.com/%2$s">%3$s</a></div></div>', 191 esc_attr( $btn_container_style ), 192 esc_attr( $settings['coffee_code'] ), 193 esc_attr( wp_strip_all_tags( $settings['coffee_text'] ) ) 194 ); 195 } else { 196 197 $html_variable_name = str_replace( '-', '_', $widget_id ); 198 if ( empty( $html_variable_name ) ) { 199 $html_variable_name = 'kofiShortcode' . wp_rand( 1, 1000 ); 200 } 201 202 $html_variable_name .= 'Html'; 203 204 wp_enqueue_script( 'ko-fi-button-widget' ); 205 wp_enqueue_script( 'ko-fi-button' ); 206 207 return sprintf( 208 '<div class="ko-fi-button" data-text="%1$s" data-color="%2$s" data-code="%3$s" id="%4$s" style="%5$s"></div>', 209 esc_attr( wp_strip_all_tags( $settings['coffee_text'] ) ), 210 esc_attr( $settings['coffee_color'] ), 211 esc_attr( $settings['coffee_code'] ), 212 esc_attr( $html_variable_name ), 213 esc_attr( $btn_container_style ) 214 ); 215 } 216 } 217 218 public static function deactivate() { 219 delete_option( 'ko_fi_options' ); 220 } 221 222 /** 223 * Get the embed code for the donation panel 224 * 225 * @param array $atts Optional (usually shortcode) attributes. 226 * @param string $widget_id Unique widget ID. 227 * @return string 228 */ 229 public static function get_panel_embed_code( $atts, $widget_id = '' ) { 230 if ( isset( $atts['code'] ) && ! empty( $atts['code'] ) ) { 231 $code = $atts['code']; 232 } else { 233 $code = self::get_plugin_option( 'coffee_code' ); 234 } 235 if ( ! empty( $code ) ) { 236 $return = '<iframe id="kofiframe" src="https://ko-fi.com/' . esc_attr( $code ) . '/?hidefeed=true&widget=true&embed=true&preview=true" style="border:none;width:100%;padding:4px;background:#f9f9f9;" height="712" title="%1$s"></iframe>'; 237 } 238 return $return; 239 } 240 241 /** 242 * Get a username and sanitise it 243 * 244 * @param string $username Username string to sanitise. 245 * @return string 246 */ 247 public static function sanitise_username( $username ) { 248 // Strip URL down to just a username. 249 $username = str_replace( 'http://ko-fi.com/', '', $username ); 250 $username = str_replace( 'https://ko-fi.com/', '', $username ); 251 $username = rtrim( $username, '/' ); 252 return $username; 253 } 254 255 /** 256 * Determine whether to display floating button on the page 257 */ 258 public static function maybe_display_floating_button() { 259 $value = false; 260 // Get default from global options. 261 $settings = self::get_plugin_option( 'coffee_floating_button_display' ); 262 if ( ! empty( $settings ) ) { 263 if ( 'all' === $settings ) { 264 $value = true; 265 } elseif ( 'none' === $settings ) { 266 $value = false; 267 } 268 } else { 269 $value = false; 270 } 271 if ( is_singular() ) { 272 // Overrides on individual pages. 273 $post_meta = get_post_meta( get_the_ID(), 'kofi_display_floating_button', true ); 274 if ( ! empty( $post_meta ) ) { 275 if ( 'yes' === $post_meta ) { 276 $value = true; 277 } elseif ( 'no' === $post_meta ) { 278 $value = false; 279 } 280 } 281 } 282 if ( apply_filters( 'kofi_display_floating_button', $value ) ) { 283 self::render_floating_button(); 284 } 285 } 286 287 /** 288 * Display the floating button 289 */ 290 public static function render_floating_button() { 291 $code = self::get_plugin_option( 'coffee_code' ); 292 $text = self::get_plugin_option( 'coffee_floating_button_text' ); 293 $color = ! empty( self::get_plugin_option( 'coffee_floating_button_color' ) ) ? self::get_plugin_option( 'coffee_floating_button_color' ) : self::get_plugin_option( 'coffee_color' ); 294 wp_enqueue_script( 'ko-fi-floating-button' ); 295 wp_add_inline_script( 296 'ko-fi-floating-button', 297 sprintf( 298 'kofiWidgetOverlay.draw( "%1$s", { 299 "type": "floating-chat", 300 "floating-chat.donateButton.text": "%2$s", 301 "floating-chat.donateButton.background-color": "%3$s", 302 "floating-chat.donateButton.text-color": "%4$s" 303 });', 304 esc_attr( $code ), 305 esc_attr( $text ), 306 esc_attr( $color ), 307 esc_attr( self::get_contrast_yiq( $color ) ) 308 ) 309 ); 310 } 311 312 /** 313 * Determine the color text should be based on the background colour 314 * 315 * @link https://24ways.org/2010/calculating-color-contrast/ 316 * @param string $hex Colour hex value. 317 * @return string Black or white 318 */ 319 public static function get_contrast_yiq( $hex ) { 320 $r = hexdec( substr( $hex, 0, 2 ) ); 321 $g = hexdec( substr( $hex, 2, 2 ) ); 322 $b = hexdec( substr( $hex, 4, 2 ) ); 323 $yiq = ( ( $r * 299 ) + ( $g * 587 ) + ( $b * 114 ) ) / 1000; 324 return ( $yiq >= 128 ) ? '#323842' : '#fff'; 325 } 326 327 /** 328 * Create a meta box on each public post type for Ko-fi related settings 329 */ 330 public static function register_posts_meta_box() { 331 $public_post_types = get_post_types( 332 array( 333 'public' => true, 334 ), 335 'names' 336 ); 337 if ( ! empty( $public_post_types ) ) { 338 foreach ( $public_post_types as $public_post_type ) { 339 add_meta_box( 'kofi_posts', __( 'Ko-fi', 'Ko_fi' ), array( __CLASS__, 'posts_meta_box_callback' ), $public_post_type, 'side' ); 340 } 341 } 342 } 343 344 /** 345 * Callback function to render posts meta box 346 * 347 * @param WP_Post $post Post object. 348 */ 349 public static function posts_meta_box_callback( $post ) { 350 $default = self::get_plugin_option( 'coffee_floating_button_display' ); 351 $value = get_post_meta( get_the_ID( $post ), 'kofi_display_floating_button', true ); 352 wp_nonce_field( 'kofi_meta_box_save', 'kofi_meta_box_nonce' ); 353 ?> 354 <label> 355 <p><strong><?php esc_html_e( 'Display floating button on this page', 'Ko_fi' ); ?></strong></p> 356 <select name="kofi_display_floating_button"> 357 <option value="">Default (<?php echo 'all' === $default ? 'Show' : 'Hide'; ?>)</option> 358 <option value="yes" <?php selected( 'yes', $value ); ?>>Always show on this page</option> 359 <option value="no" <?php selected( 'no', $value ); ?>>Never show on this page</option> 360 </select> 361 </label> 362 <?php 363 } 364 365 /** 366 * Save post meta from Ko-fi meta box 367 * 368 * @param int $post_id Post ID. 369 * @param WP_POST $post Post object. 370 */ 371 public static function save_post_meta( $post_id, $post ) { 372 if ( isset( $_POST['kofi_meta_box_nonce'] ) && ! empty( $_POST['kofi_meta_box_nonce'] ) ) { 373 $nonce = sanitize_text_field( wp_unslash( $_POST['kofi_meta_box_nonce'] ) ); 374 if ( wp_verify_nonce( $nonce, 'kofi_meta_box_save' ) ) { 375 if ( current_user_can( 'edit_post', $post_id ) ) { 376 if ( isset( $_POST['kofi_display_floating_button'] ) ) { 377 $kofi_display_floating_button = sanitize_text_field( wp_unslash( $_POST['kofi_display_floating_button'] ) ); 378 update_post_meta( $post_id, 'kofi_display_floating_button', $kofi_display_floating_button ); 379 } 380 } 381 } 382 } 383 } 384 385 /** 386 * Prevent the floating button from displaying over the top of legacy widgets when enabled. 387 */ 388 public static function prevent_floating_button_displaying_on_widget_previews() { 389 add_filter( 'kofi_display_floating_button', '__return_false' ); 390 } 391 392 /** 393 * Get plugin option if set, otherwise get the default 394 * 395 * @param string $option Option array key. 396 * @return mixed 397 */ 398 public static function get_plugin_option( $option ) { 399 return isset( self::$options[ $option ] ) && ! empty( self::$options[ $option ] ) ? self::$options[ $option ] : Default_ko_fi_options::get()['defaults'][ $option ]; 400 } 401 402 } 36 require_once 'class-ko-fi.php'; 403 37 404 38 add_action( 'plugins_loaded', array( 'Ko_Fi', 'init' ) ); -
ko-fi-button/trunk/Readme.txt
r2903835 r2920201 5 5 Requires at least: 4.6 6 6 Tested up to: 6.2 7 Stable tag: 1.3. 38 Requires PHP: 5. 2.47 Stable tag: 1.3.4 8 Requires PHP: 5.6 9 9 License: GPLv2 or later 10 10 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 92 92 == Changelog == 93 93 94 = 1.3.4 = 95 * Fix fatal error on outdated versions of PHP 96 * Code quality improvements - now compliant with the WordPress PHP coding standards 97 * Increase minimum PHP version to 5.6 98 94 99 = 1.3.3 = 95 100 * XSS security fix -
ko-fi-button/trunk/class-default-ko-fi-options.php
r2896151 r2920201 10 10 } 11 11 12 /** 13 * Class that holds the options page settings 14 */ 12 15 class Default_Ko_Fi_Options { 13 16 17 /** 18 * Get the options for the settings page, including default values 19 */ 14 20 public static function get() { 15 21 return array( -
ko-fi-button/trunk/class-ko-fi-panel-widget.php
r2700132 r2920201 47 47 } 48 48 49 echo Ko_Fi::get_panel_embed_code( array( 'code' => $code ), $args[ 'widget_id' ] );49 echo Ko_Fi::get_panel_embed_code( array( 'code' => $code ), $args['widget_id'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 50 50 echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 51 51 }
Note: See TracChangeset
for help on using the changeset viewer.