| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: KIA Subtitle |
| 4 |
* Plugin URI: http://www.kathyisawesome.com/436/kia-subtitle/ |
| 5 |
* Description: Adds a subtitle field to WordPress' Post editor |
| 6 |
* Author: Kathy Darling |
| 7 |
* Version: 4.0.2 |
| 8 |
* Author URI: http://www.kathyisawesome.com |
| 9 |
* License: GPL3 |
| 10 |
* Text Domain: kia-subtitle |
| 11 |
* |
| 12 |
* Copyright 2024 Kathy Darling |
| 13 |
* |
| 14 |
* This program is free software; you can redistribute it and/or modify |
| 15 |
* it under the terms of the GNU General Public License, version 2, as |
| 16 |
* published by the Free Software Foundation. |
| 17 |
* |
| 18 |
* This program is distributed in the hope that it will be useful, |
| 19 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
* GNU General Public License for more details. |
| 22 |
* |
| 23 |
* You should have received a copy of the GNU General Public License |
| 24 |
* along with this program; if not, write to the Free Software |
| 25 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 |
*/ |
| 27 |
|
| 28 |
// Don't load directly. |
| 29 |
if ( ! function_exists( 'is_admin' ) ) { |
| 30 |
header( 'Status: 403 Forbidden' ); |
| 31 |
header( 'HTTP/1.1 403 Forbidden' ); |
| 32 |
exit(); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
if ( ! class_exists( 'KIA_Subtitle' ) ) : |
| 37 |
|
| 38 |
class KIA_Subtitle { |
| 39 |
|
| 40 |
/** |
| 41 |
* @var KIA_Subtitle The single instance of the class |
| 42 |
* @since 1.6 |
| 43 |
*/ |
| 44 |
protected static $_instance = null; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var KIA_Subtitle The single instance of the class |
| 48 |
* @since 1.6 |
| 49 |
*/ |
| 50 |
public $version = '4.0.2'; |
| 51 |
|
| 52 |
/** |
| 53 |
* @constant string donate url |
| 54 |
* @since 1.5 |
| 55 |
*/ |
| 56 |
CONST DONATE_URL = 'https://www.paypal.me/kathyisawesome'; |
| 57 |
|
| 58 |
/** |
| 59 |
* Main KIA_Subtitle Instance |
| 60 |
* |
| 61 |
* Ensures only one instance of KIA_Subtitle is loaded or can be loaded. |
| 62 |
* |
| 63 |
* @since 1.6 |
| 64 |
* @static |
| 65 |
* @see KIA_Subtitle() |
| 66 |
* @return KIA_Subtitle - Main instance |
| 67 |
*/ |
| 68 |
public static function instance() { |
| 69 |
if ( is_null( self::$_instance ) ) { |
| 70 |
self::$_instance = new self(); |
| 71 |
} |
| 72 |
return self::$_instance; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Cloning is forbidden. |
| 77 |
* |
| 78 |
* @since 1.6 |
| 79 |
*/ |
| 80 |
public function __clone() { |
| 81 |
_doing_it_wrong( __FUNCTION__, __( 'Cloning this object is forbidden.' , 'kia-subtitle' ), '1.6' ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Unserializing instances of this class is forbidden. |
| 86 |
* |
| 87 |
* @since 1.6 |
| 88 |
*/ |
| 89 |
public function __wakeup() { |
| 90 |
_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.' , 'kia-subtitle' ), '1.6' ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* KIA_Subtitle Constructor. |
| 95 |
* |
| 96 |
* @return KIA_Subtitle |
| 97 |
* @since 1.0 |
| 98 |
*/ |
| 99 |
public function __construct() { |
| 100 |
|
| 101 |
// Set-up uninstall action. |
| 102 |
register_uninstall_hook( __FILE__, array( __CLASS__, 'delete_plugin_options' ) ); |
| 103 |
|
| 104 |
// Load the textdomain. |
| 105 |
add_action( 'init', array( $this, 'load_textdomain' ) ); |
| 106 |
|
| 107 |
// Register settings. |
| 108 |
add_action( 'admin_init', array( $this, 'admin_init' )); |
| 109 |
|
| 110 |
// Add plugin options page. |
| 111 |
add_action( 'admin_menu', array( $this, 'add_options_page' ) ); |
| 112 |
|
| 113 |
// Add settings link to plugins page. |
| 114 |
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_action_links' ), 10, 2 ); |
| 115 |
|
| 116 |
// Add Donate link to plugin. |
| 117 |
add_filter( 'plugin_row_meta', array( $this, 'add_meta_links' ), 10, 2 ); |
| 118 |
|
| 119 |
// Register the shortcode. |
| 120 |
add_shortcode( 'the-subtitle', array( $this, 'shortcode' ) ); |
| 121 |
|
| 122 |
// Backend functions. |
| 123 |
|
| 124 |
// Load the subtitle script. |
| 125 |
add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts' ) ); |
| 126 |
|
| 127 |
// Add metaboxes for CPTs that don't support custom-fields in Gutenberg. |
| 128 |
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) ); |
| 129 |
|
| 130 |
// Add the input field in Classic Editor. |
| 131 |
add_action( 'edit_form_after_title', array( $this, 'add_input' ) ); |
| 132 |
|
| 133 |
// Hide key from Custom Fields |
| 134 |
add_filter( 'is_protected_meta', array( $this, 'make_key_private' ), 10, 2 ); |
| 135 |
|
| 136 |
// Save the subtitle as post meta. |
| 137 |
add_action( 'save_post', array( $this, 'meta_save' ) ); |
| 138 |
add_action( 'edit_attachment', array( $this, 'meta_save' ) ); |
| 139 |
|
| 140 |
// Edit Columns + Quickedit. |
| 141 |
foreach( self::get_enabled_post_types() as $post_type ) { |
| 142 |
add_action( "manage_{$post_type}_posts_columns", array( $this, 'column_header' ), 20 ); |
| 143 |
add_filter( "manage_{$post_type}_posts_custom_column", array( $this, 'column_value'), 10, 2 ); |
| 144 |
} |
| 145 |
|
| 146 |
// Quick edit support. |
| 147 |
add_action( 'quick_edit_custom_box', array( $this, 'quick_edit_custom_box'), 10 ); |
| 148 |
|
| 149 |
// Upgrade routine. |
| 150 |
add_action( 'admin_init', array( $this, 'upgrade_routine' ) ); |
| 151 |
|
| 152 |
// Add Block Editor compatibility. |
| 153 |
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_assets' ) ); |
| 154 |
|
| 155 |
// Register meta key in REST |
| 156 |
add_action( 'init', array( $this, 'register_meta' ) ); |
| 157 |
|
| 158 |
// Register block |
| 159 |
add_action( 'init', array( $this, 'register_block' ) ); |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Delete options table entries ONLY when plugin deactivated AND deleted |
| 165 |
* register_uninstall_hook( __FILE__, array( $this,'delete_plugin_options' ) ); |
| 166 |
* |
| 167 |
* @since 1.4 |
| 168 |
*/ |
| 169 |
public static function delete_plugin_options() { |
| 170 |
$options = get_option( 'kia_subtitle_options' ); |
| 171 |
if ( isset( $options['delete'] ) && $options['delete'] ) { |
| 172 |
delete_option( 'kia_subtitle_options' ); |
| 173 |
delete_option( 'kia_subtitle_db_version' ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Make Plugin Translation-ready |
| 179 |
* |
| 180 |
* @since 1.0 |
| 181 |
*/ |
| 182 |
public function load_textdomain() { |
| 183 |
load_plugin_textdomain( 'kia-subtitle', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
/** |
| 188 |
* Register admin settings |
| 189 |
* |
| 190 |
* @since 1.4 |
| 191 |
*/ |
| 192 |
public function admin_init() { |
| 193 |
register_setting( 'kia_subtitle_options', 'kia_subtitle_options', array( $this, 'validate_options' ) ); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
/** |
| 198 |
* Add options page |
| 199 |
* |
| 200 |
* @since 1.4 |
| 201 |
*/ |
| 202 |
public function add_options_page() { |
| 203 |
add_options_page(__( 'KIA Subtitle Options Page', 'kia-subtitle' ), __( 'KIA Subtitle', 'kia-subtitle' ), 'manage_options', 'kia_subtitle', array( $this, 'render_form' ) ); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Display a Settings link on the main Plugins page |
| 208 |
* |
| 209 |
* @since 1.6.4 |
| 210 |
* |
| 211 |
* @param array $links |
| 212 |
* @param string $file |
| 213 |
* @return array |
| 214 |
*/ |
| 215 |
public function add_action_links( $links, $file ) { |
| 216 |
|
| 217 |
$plugin_link = '<a href="'. add_query_arg( 'page', 'kia_subtitle', admin_url( 'options-general.php' ) ) . '">' . __( 'Settings', 'kia-subtitle' ) . '</a>'; |
| 218 |
// make the 'Settings' link appear first |
| 219 |
array_unshift( $links, $plugin_link ); |
| 220 |
|
| 221 |
return $links; |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Add donation link |
| 227 |
* |
| 228 |
* @since 1.6.7 |
| 229 |
* |
| 230 |
* @param array $plugin_meta |
| 231 |
* @param string $plugin_file |
| 232 |
*/ |
| 233 |
public function add_meta_links( $plugin_meta, $plugin_file ) { |
| 234 |
if ( $plugin_file === plugin_basename(__FILE__) ) { |
| 235 |
$plugin_meta[] = '<a class="dashicons-before dashicons-awards" href="' . self::DONATE_URL . '" target="_blank">' . __( 'Donate', 'kia-subtitle' ) . '</a>'; |
| 236 |
} |
| 237 |
return $plugin_meta; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Render the Plugin options form |
| 242 |
* |
| 243 |
* @since 1.4 |
| 244 |
*/ |
| 245 |
public function render_form() { |
| 246 |
include( 'includes/views/plugin-options.php' ); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Sanitize and validate input. |
| 251 |
* Accepts an array, return a sanitized array. |
| 252 |
* |
| 253 |
* @since 1.4 |
| 254 |
* |
| 255 |
* @param array $input all posted data |
| 256 |
* @return array $clean data that is allowed to be save |
| 257 |
*/ |
| 258 |
public function validate_options( $input ) { |
| 259 |
|
| 260 |
$clean = array(); |
| 261 |
|
| 262 |
// Probably overkill, but make sure that the post type actually exists and is one we're cool with modifying. |
| 263 |
$args = ( array ) apply_filters( 'kia_subtitle_post_type_args', array( 'show_in_menu' => true ) ); |
| 264 |
|
| 265 |
$post_types = get_post_types( $args ); |
| 266 |
|
| 267 |
if ( isset( $input['post_types'] ) && is_array( $input['post_types'] ) ) foreach ( $input['post_types'] as $post_type ) { |
| 268 |
if ( in_array( $post_type, $post_types ) ) $clean['post_types'][] = $post_type; |
| 269 |
} |
| 270 |
|
| 271 |
$clean['delete'] = isset( $input['delete'] ) && $input['delete'] ? 1 : 0 ; // Checkbox. |
| 272 |
|
| 273 |
return $clean; |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Outputs the Subtitle |
| 279 |
* |
| 280 |
* @since 1.0 |
| 281 |
* |
| 282 |
* @param array $args. In 3.1 converted to array that accepts multiple keys. |
| 283 |
* array( |
| 284 |
* 'post_id' => int |
| 285 |
* 'before' => string - Any text that should be prepended to the subtitle |
| 286 |
* 'after' => string - Any text that should be appended to the subtitle. |
| 287 |
* 'echo' => bool |
| 288 |
* ) |
| 289 |
* @param mixed $after - Deprecated in 3.1 |
| 290 |
* @param boolean $echo - Deprecated in 3.1 |
| 291 |
* @return string |
| 292 |
*/ |
| 293 |
public function the_subtitle( $args = array(), $after = false, $echo = null ) { |
| 294 |
|
| 295 |
$new_args = array(); |
| 296 |
|
| 297 |
if ( is_array( $args ) ) { |
| 298 |
$new_args = $args; |
| 299 |
} else { |
| 300 |
_deprecated_argument( __FUNCTION__, '3.1.0', 'All arguments are now passed as a single array parameter.' ); |
| 301 |
$new_args['before'] = $args; |
| 302 |
} |
| 303 |
|
| 304 |
if ( is_string( $after ) ) { |
| 305 |
_deprecated_argument( __FUNCTION__, '3.1.0', 'All arguments are now passed as a single array parameter.' ); |
| 306 |
$new_args['after'] = $after; |
| 307 |
} |
| 308 |
|
| 309 |
if ( ! is_null( $echo ) ) { |
| 310 |
_deprecated_argument( __FUNCTION__, '3.1.0', 'All arguments are now passed as a single array parameter.' ); |
| 311 |
$new_args['echo'] = $echo; |
| 312 |
} |
| 313 |
|
| 314 |
$defaults = |
| 315 |
array( |
| 316 |
'post_id' => null, |
| 317 |
'before' => '', |
| 318 |
'after' => '', |
| 319 |
'echo' => true |
| 320 |
); |
| 321 |
|
| 322 |
$args = wp_parse_args( $new_args, $defaults ); |
| 323 |
|
| 324 |
$subtitle = $this->get_the_subtitle( $args['post_id'] ); |
| 325 |
|
| 326 |
if ( strlen( $subtitle ) === 0 ) { |
| 327 |
return; |
| 328 |
} |
| 329 |
|
| 330 |
$subtitle = $args['before'] . $subtitle . $args['after']; |
| 331 |
|
| 332 |
if ( $args['echo'] ) { |
| 333 |
echo $subtitle; |
| 334 |
} else { |
| 335 |
return $subtitle; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Returns the Subtitle |
| 341 |
* |
| 342 |
* @since 1.0 |
| 343 |
* |
| 344 |
* @param int $post_id the post ID for which you want to retrieve the subtitle |
| 345 |
* @return string |
| 346 |
*/ |
| 347 |
public function get_the_subtitle( $post_id = null ) { |
| 348 |
$post_id = ! is_null( $post_id ) ? intval( $post_id ) : get_the_ID(); |
| 349 |
$sub = get_post_meta( $post_id, 'kia_subtitle', true ); |
| 350 |
return apply_filters( 'the_subtitle', $sub, $post_id ); |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* Callback for the Shortcode [the-subtitle] |
| 355 |
* |
| 356 |
* @since 1.0 |
| 357 |
* |
| 358 |
* @return string |
| 359 |
*/ |
| 360 |
public function shortcode( $atts ) { |
| 361 |
|
| 362 |
$atts = shortcode_atts( |
| 363 |
array( |
| 364 |
'post_id' => null, |
| 365 |
'before' => '<h2 class="subtitle">', |
| 366 |
'after' => '</h2>' |
| 367 |
), $atts, 'the-subtitle' ); |
| 368 |
|
| 369 |
$atts['before'] = wp_kses_post( $atts['before'] ); |
| 370 |
$atts['after'] = wp_kses_post( $atts['after'] ); |
| 371 |
$atts['echo'] = false; |
| 372 |
|
| 373 |
return $this->the_subtitle( $atts ); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
/** |
| 378 |
* Load Script in Admin |
| 379 |
* Uses jquery to re-locate the subtitle text input to just below the Title input |
| 380 |
* |
| 381 |
* @since 1.0 |
| 382 |
* |
| 383 |
* @param string $hook the name of the page we're on in the WP admin |
| 384 |
*/ |
| 385 |
public function load_scripts( $hook ) { |
| 386 |
|
| 387 |
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
| 388 |
$current_screen = get_current_screen(); |
| 389 |
|
| 390 |
wp_register_script( 'kia_subtitle', plugins_url( 'assets/js/subtitle'. $suffix . '.js', __FILE__ ), array( 'jquery' ), $this->version, true ); |
| 391 |
|
| 392 |
// Add styles and scripts for classic editor. |
| 393 |
if ( in_array( $hook, array( 'post.php', 'post-new.php' ) ) && self::is_enabled_for_post_type( $current_screen->post_type ) && method_exists( $current_screen, 'is_block_editor' ) && ! $current_screen->is_block_editor() ) { |
| 394 |
add_action( 'admin_head', array( $this, 'inline_style' ) ); |
| 395 |
wp_enqueue_script( 'kia_subtitle' ); |
| 396 |
// Add scripts and styles for edit screen. |
| 397 |
} elseif ( $hook === 'edit.php' && self::is_enabled_for_post_type( $current_screen->post_type ) ) { |
| 398 |
add_action( 'admin_head', array( $this, 'subtitle_column_style' ) ); |
| 399 |
wp_enqueue_script( 'kia_subtitle' ); |
| 400 |
} |
| 401 |
|
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Style the Subtitle's text input |
| 406 |
* |
| 407 |
* @since 1.0 |
| 408 |
*/ |
| 409 |
|
| 410 |
public function inline_style() { ?> |
| 411 |
<style type="text/css"> |
| 412 |
#the_subtitle { |
| 413 |
margin: 5px 0px; |
| 414 |
} |
| 415 |
</style> |
| 416 |
<?php |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Add the text input as a metabox in Gutenberg |
| 421 |
* |
| 422 |
* @since 2.0 |
| 423 |
*/ |
| 424 |
public function add_meta_box() { |
| 425 |
|
| 426 |
global $post; |
| 427 |
|
| 428 |
$current_screen = get_current_screen(); |
| 429 |
|
| 430 |
// Classic editor screens don't need metaboxes since they use the edit_form_after_title hook. |
| 431 |
if ( is_callable( array( $current_screen, 'is_block_editor' ) ) && ! $current_screen->is_block_editor() ) { |
| 432 |
return; |
| 433 |
} |
| 434 |
|
| 435 |
if ( $post && in_array( $post->post_type, self::get_enabled_post_types() ) && ! post_type_supports( $post->post_type, 'custom-fields' ) ) { |
| 436 |
|
| 437 |
add_meta_box( |
| 438 |
'kia_subtitle_meta_box', |
| 439 |
__( 'Subtitle', 'kia-subtitle' ), |
| 440 |
array( $this, 'render_meta_box_content' ), |
| 441 |
$post->post_type, |
| 442 |
'advanced', |
| 443 |
'high', |
| 444 |
array( |
| 445 |
'__block_editor_compatible_meta_box' => true, |
| 446 |
) |
| 447 |
); |
| 448 |
|
| 449 |
} |
| 450 |
|
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
/** |
| 455 |
* Render Meta Box content. |
| 456 |
* |
| 457 |
* @param WP_Post $post The post object. |
| 458 |
*/ |
| 459 |
public function render_meta_box_content( $post = false ) { |
| 460 |
|
| 461 |
$post_id = $post === false ? get_the_ID() : $post->ID; |
| 462 |
|
| 463 |
// Create the meta field (don't use a metabox, we have our own styling). |
| 464 |
wp_nonce_field( plugin_basename( __FILE__ ), 'kia_subnonce' ); |
| 465 |
|
| 466 |
// Get the subtitle value (if set). |
| 467 |
$value = get_post_meta( $post_id, 'kia_subtitle', true ); |
| 468 |
|
| 469 |
// Display the form, using the current value. |
| 470 |
?> |
| 471 |
<label for="the_subtitle" class="screen-reader-text"> |
| 472 |
<?php _e( 'Subtitle', 'kia-subtitle' ); ?> |
| 473 |
</label> |
| 474 |
<input type="text" id="the_subtitle" class="widefat" name="subtitle" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php echo __( 'Enter subtitle', 'kia-subtitle' ); ?>" /> |
| 475 |
|
| 476 |
<?php |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
/** |
| 481 |
* Add the text input on the post screen |
| 482 |
* |
| 483 |
* @since 1.0 |
| 484 |
*/ |
| 485 |
public function add_input() { |
| 486 |
|
| 487 |
global $post; |
| 488 |
|
| 489 |
// Only show input if the post type was enabled in options. |
| 490 |
if ( self::is_enabled_for_post_type( $post->post_type ) ) { |
| 491 |
$this->render_meta_box_content( $post ); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
/** |
| 497 |
* Make the meta key private |
| 498 |
* |
| 499 |
* @since 2.0 |
| 500 |
* |
| 501 |
* @param bool $is_private |
| 502 |
* @param string $key |
| 503 |
* @return bool |
| 504 |
*/ |
| 505 |
public function make_key_private( $is_private, $key ) { |
| 506 |
if ( 'kia_subtitle' === $key ) { |
| 507 |
$is_private = true; |
| 508 |
} |
| 509 |
return $is_private; |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
/** |
| 514 |
* Save the Meta Value |
| 515 |
* |
| 516 |
* @since 1.0 |
| 517 |
* |
| 518 |
* @param int $post_id the ID of the post we're saving |
| 519 |
* @return int |
| 520 |
*/ |
| 521 |
public function meta_save( $post_id ) { |
| 522 |
|
| 523 |
// Check to see if this is an autosafe and if the nonce is verified. |
| 524 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 525 |
return $post_id; |
| 526 |
} |
| 527 |
|
| 528 |
if ( ! isset( $_POST['kia_subnonce'] ) || ! wp_verify_nonce( $_POST['kia_subnonce'], plugin_basename( __FILE__ ) ) ) { |
| 529 |
return $post_id; |
| 530 |
} |
| 531 |
|
| 532 |
// Check permissions. |
| 533 |
if ( 'page' === $_POST['post_type'] && ! current_user_can( 'edit_page', $post_id ) ) { |
| 534 |
return $post_id; |
| 535 |
} else if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 536 |
return $post_id; |
| 537 |
} |
| 538 |
|
| 539 |
// Save if set. |
| 540 |
if ( isset( $_POST['subtitle'] ) ) { |
| 541 |
|
| 542 |
/** |
| 543 |
* `kia_subtitle_sanitize_subtitle` filter |
| 544 |
* |
| 545 |
* @since 4.0.0 |
| 546 |
* |
| 547 |
* @param string $subtitle the subtitle to be sanitized. |
| 548 |
* @param int $post_id the post ID of the post being saved. |
| 549 |
*/ |
| 550 |
$sanitized_subtitle = apply_filters( 'kia_subtitle_sanitize_subtitle', wp_unslash( $_POST['subtitle'] ), $post_id ); |
| 551 |
|
| 552 |
update_post_meta( $post_id, 'kia_subtitle', sanitize_post_field( 'post_title', $sanitized_subtitle, $post_id, 'db' ) ); |
| 553 |
} |
| 554 |
|
| 555 |
return $post_id; |
| 556 |
} |
| 557 |
|
| 558 |
/** |
| 559 |
* Style the Subtitle's column for WooCommerce products |
| 560 |
* |
| 561 |
* @since 1.6.8 |
| 562 |
*/ |
| 563 |
public function subtitle_column_style() { ?> |
| 564 |
<style type="text/css"> |
| 565 |
.post-type-product .column-subtitle { |
| 566 |
width: 20ch; |
| 567 |
} |
| 568 |
</style> |
| 569 |
<?php |
| 570 |
} |
| 571 |
|
| 572 |
/** |
| 573 |
* Create the Subtitle Column |
| 574 |
* |
| 575 |
* @since 1.1 |
| 576 |
* |
| 577 |
* @param array $columns the columns for edit screen |
| 578 |
* @return array of new columns |
| 579 |
*/ |
| 580 |
public function column_header( $columns ) { |
| 581 |
|
| 582 |
// Insert after title column. |
| 583 |
if ( isset( $columns['title'] ) || isset( $columns['name'] ) ) { |
| 584 |
|
| 585 |
// The subtitle as an array for subsequent array manip. |
| 586 |
$subtitle = array( 'subtitle' => __( 'Subtitle', 'kia-subtitle' ) ); |
| 587 |
|
| 588 |
// WooCommerce uses the "name" column for titles. |
| 589 |
$needle = isset( $columns['title'] ) ? 'title' : 'name'; |
| 590 |
|
| 591 |
// Find the "title" column. |
| 592 |
$index = array_search( $needle, array_keys( $columns) ); |
| 593 |
|
| 594 |
// Reform the array. |
| 595 |
$columns = array_merge( array_slice( $columns, 0, $index + 1, true ), $subtitle, array_slice( $columns, $index, count( $columns ) - $index, true ) ); |
| 596 |
|
| 597 |
// Or add to end. |
| 598 |
} else { |
| 599 |
$columns['subtitle'] = __( 'Subtitle', 'kia-subtitle' ); |
| 600 |
} |
| 601 |
|
| 602 |
return $columns; |
| 603 |
} |
| 604 |
|
| 605 |
/** |
| 606 |
* Add subtitle value to column data |
| 607 |
* |
| 608 |
* @since 1.1 |
| 609 |
* |
| 610 |
* @param string $column_name |
| 611 |
* @param int $post_id the post ID of the row |
| 612 |
* @return string |
| 613 |
*/ |
| 614 |
public function column_value( $column_name, $post_id ) { |
| 615 |
switch ( $column_name ) : |
| 616 |
case 'subtitle' : |
| 617 |
echo $sub = get_post_meta( get_the_ID(), 'kia_subtitle', true ); |
| 618 |
echo '<div class="hidden kia-subtitle-value">' . esc_html($sub) . '</div>'; |
| 619 |
break; |
| 620 |
endswitch; |
| 621 |
} |
| 622 |
|
| 623 |
/** |
| 624 |
* Add Quick Edit Form |
| 625 |
* |
| 626 |
* @param string $column_name |
| 627 |
* @return string |
| 628 |
*/ |
| 629 |
public function quick_edit_custom_box( $column_name ) { |
| 630 |
if ( $column_name === 'subtitle' ) { |
| 631 |
|
| 632 |
global $post; |
| 633 |
|
| 634 |
// Only show input if the post type was enabled in options. |
| 635 |
if ( self::is_enabled_for_post_type( $post->post_type ) ) { ?> |
| 636 |
|
| 637 |
<label class="kia-subtitle"> |
| 638 |
<span class="title"><?php _e( 'Subtitle', 'kia-subtitle' ) ?></span> |
| 639 |
<span class="input-text-wrap"><input type="text" name="<?php echo esc_attr($column_name); ?>" class="ptitle kia-subtitle-input" value=""></span> |
| 640 |
|
| 641 |
<?php wp_nonce_field( plugin_basename( __FILE__ ), 'kia_subnonce' ); ?> |
| 642 |
|
| 643 |
</label> |
| 644 |
<?php } |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
/** |
| 650 |
* Plugin Upgrade Routine |
| 651 |
* previously used options to *exclude* post types from having subtitle |
| 652 |
* going to switch to including them all by default |
| 653 |
* |
| 654 |
* @since 1.5 |
| 655 |
*/ |
| 656 |
public function upgrade_routine() { |
| 657 |
|
| 658 |
$db_version = get_option( 'kia_subtitle_db_version', false ); |
| 659 |
|
| 660 |
if ( ! ( $db_version ) || version_compare( $db_version, '1.5', '<' ) ) { |
| 661 |
|
| 662 |
// Get any existing options. |
| 663 |
$options = get_option( 'kia_subtitle_options' ); |
| 664 |
|
| 665 |
// Get all post types showing up in the menu. |
| 666 |
$args = array( 'show_in_menu' => true ); |
| 667 |
$post_types = get_post_types( $args ); |
| 668 |
ksort( $post_types ); |
| 669 |
|
| 670 |
// We're going to switch any checked to *disable* options from previous version into unchecked to disable. |
| 671 |
$post_types = array_diff( $post_types, self::get_enabled_post_types() ); |
| 672 |
|
| 673 |
// Merge the new options with any old options. |
| 674 |
$update_options = array_merge( (array)$options, array ( 'post_types' => $post_types, 'db_version' => '1.5' ) ); |
| 675 |
|
| 676 |
// Update the options. |
| 677 |
update_option( 'kia_subtitle_options', $update_options ); |
| 678 |
|
| 679 |
} |
| 680 |
|
| 681 |
update_option( 'kia_subtitle_db_version', $this->version ); |
| 682 |
|
| 683 |
} |
| 684 |
|
| 685 |
/*-----------------------------------------------------------------------------------*/ |
| 686 |
/* Block Editor Functions */ |
| 687 |
/*-----------------------------------------------------------------------------------*/ |
| 688 |
|
| 689 |
/** |
| 690 |
* Load Script in Block Editor |
| 691 |
* |
| 692 |
* @since 3.0 |
| 693 |
* |
| 694 |
* @param string $hook the name of the page we're on in the WP admin |
| 695 |
* @return null |
| 696 |
*/ |
| 697 |
public function enqueue_assets( $hook ) { |
| 698 |
|
| 699 |
$current_screen = get_current_screen(); |
| 700 |
|
| 701 |
// Add styles and scripts for block editor. |
| 702 |
if ( 'site-editor' === $current_screen->base || ( self::is_enabled_for_post_type( $current_screen->post_type ) && post_type_supports( $current_screen->post_type, 'custom-fields' ) ) ) { |
| 703 |
|
| 704 |
$script_asset_path = trailingslashit( plugin_dir_path( __FILE__ ) ) . 'assets/js/dist/frontend/index.asset.php'; |
| 705 |
$script_asset = file_exists( $script_asset_path ) |
| 706 |
? require $script_asset_path |
| 707 |
: array( |
| 708 |
'dependencies' => array(), |
| 709 |
'version' => $this->version, |
| 710 |
); |
| 711 |
|
| 712 |
wp_enqueue_script( |
| 713 |
'kia-subtitle-block-editor', |
| 714 |
plugins_url( 'assets/js/dist/index.js', __FILE__ ), |
| 715 |
$script_asset[ 'dependencies' ], |
| 716 |
$script_asset[ 'version' ], |
| 717 |
true |
| 718 |
); |
| 719 |
|
| 720 |
} |
| 721 |
|
| 722 |
} |
| 723 |
|
| 724 |
/** |
| 725 |
* Register meta key for block editor. |
| 726 |
* |
| 727 |
* @since 3.0 |
| 728 |
*/ |
| 729 |
public function register_meta() { |
| 730 |
|
| 731 |
register_meta('post', 'kia_subtitle', array( |
| 732 |
'show_in_rest' => true, |
| 733 |
'type' => 'string', |
| 734 |
'single' => true, |
| 735 |
'sanitize_callback' => 'sanitize_text_field', |
| 736 |
'auth_callback' => function() { |
| 737 |
return current_user_can( 'edit_posts' ); |
| 738 |
} |
| 739 |
)); |
| 740 |
|
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
/** |
| 745 |
* Register block |
| 746 |
* |
| 747 |
* @since 4.0 |
| 748 |
*/ |
| 749 |
public function register_block() { |
| 750 |
register_block_type( __DIR__ . '/assets/js/dist/block' ); |
| 751 |
} |
| 752 |
|
| 753 |
|
| 754 |
/*-----------------------------------------------------------------------------------*/ |
| 755 |
/* Helper Functions */ |
| 756 |
/*-----------------------------------------------------------------------------------*/ |
| 757 |
|
| 758 |
/** |
| 759 |
* Version compare for WordPress core. |
| 760 |
* |
| 761 |
* @since 3.0 |
| 762 |
* |
| 763 |
* @param string $version - The version to test against. |
| 764 |
* @return bool |
| 765 |
*/ |
| 766 |
public static function is_wp_gte( $version = '5.3' ) { |
| 767 |
global $wp_version; |
| 768 |
return version_compare( $wp_version, '4.3', '>=' ); |
| 769 |
} |
| 770 |
|
| 771 |
/** |
| 772 |
* Get enabled post types. |
| 773 |
* |
| 774 |
* @since 3.0 |
| 775 |
* |
| 776 |
* @return array |
| 777 |
*/ |
| 778 |
public static function get_enabled_post_types() { |
| 779 |
$options = get_option( 'kia_subtitle_options', false ); |
| 780 |
return isset( $options['post_types'] ) && is_array( $options[ 'post_types'] ) ? $options['post_types'] : array(); |
| 781 |
} |
| 782 |
|
| 783 |
/** |
| 784 |
* Is a post type enabled? |
| 785 |
* |
| 786 |
* @since 3.0 |
| 787 |
* |
| 788 |
* @param string $type - The post type to check is enabled. |
| 789 |
* @return bool |
| 790 |
*/ |
| 791 |
public static function is_enabled_for_post_type( $type ) { |
| 792 |
return in_array( $type, self::get_enabled_post_types() ); |
| 793 |
} |
| 794 |
|
| 795 |
} // End class. |
| 796 |
|
| 797 |
endif; // End class_exists check. |
| 798 |
|
| 799 |
|
| 800 |
/** |
| 801 |
* Launch the whole plugin |
| 802 |
* Returns the main instance of WC to prevent the need to use globals. |
| 803 |
* |
| 804 |
* @since 1.6 |
| 805 |
* @return KIA_Subtitle |
| 806 |
*/ |
| 807 |
function KIA_Subtitle() { |
| 808 |
return KIA_Subtitle::instance(); |
| 809 |
} |
| 810 |
|
| 811 |
add_action( 'plugins_loaded', 'KIA_Subtitle' ); |
| 812 |
|
| 813 |
/** |
| 814 |
* Public Shortcut Function to KIA_Subtitle::the_subtitle() |
| 815 |
* do not compete with any other subtitle plugins |
| 816 |
* |
| 817 |
* @since 1.0 |
| 818 |
* |
| 819 |
* @param string $before any text that should be prepended to the subtitle |
| 820 |
* @param string $after any text that should be appended to the subtitle |
| 821 |
* @param boolean $echo should the subtitle be printed or returned |
| 822 |
* @return string |
| 823 |
*/ |
| 824 |
if ( ! function_exists( 'the_subtitle' ) ) { |
| 825 |
function the_subtitle( $before = '', $after = '', $echo = true ) { |
| 826 |
$args = array( |
| 827 |
'before' => $before, |
| 828 |
'after' => $after, |
| 829 |
'echo' => $echo |
| 830 |
); |
| 831 |
return KIA_Subtitle()->the_subtitle( $args ); |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
/** |
| 836 |
* Public Shortcut Function to KIA_Subtitle::get_the_subtitle($post_id) |
| 837 |
* do not compete with any other subtitle plugins |
| 838 |
* |
| 839 |
* @since 1.0 |
| 840 |
* |
| 841 |
* @param int $post_id the post ID for which you want to retrieve the subtitle |
| 842 |
* @return string |
| 843 |
*/ |
| 844 |
if ( ! function_exists( 'get_the_subtitle' )) { |
| 845 |
function get_the_subtitle( $post_id = null ) { |
| 846 |
return KIA_Subtitle()->get_the_subtitle( $post_id ); |
| 847 |
} |
| 848 |
} |
| 849 |
|