Skip to content

Commit

Permalink
Merge branch 'release/2.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
benhuson committed May 3, 2017
2 parents 326060a + 0d9cd17 commit 5342455
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 12 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [2.9] - 2017-05-03

### Added
- Add support for post revisions. Props [Fabian Marz](https://github.com/fabianmarz).

### Fixed
- As of WordPress 4.3 no need to esc_attr() AND htmlentities() - can mess up special characters.

## [2.8.1] - 2016-09-14

### Fixed
Expand Down Expand Up @@ -124,7 +132,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added
- First version.

[Unreleased]: https://github.com/benhuson/wp-subtitle/compare/2.8.1...HEAD
[Unreleased]: https://github.com/benhuson/wp-subtitle/compare/2.9...HEAD
[2.9]: https://github.com/benhuson/wp-subtitle/compare/2.8.1...2.9
[2.8.1]: https://github.com/benhuson/wp-subtitle/compare/2.8...2.8.1
[2.8]: https://github.com/benhuson/wp-subtitle/compare/2.7.1...2.8
[2.7.1]: https://github.com/benhuson/wp-subtitle/compare/2.7...2.7.1
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ The plugin is [hosted on GitHub](https://github.com/benhuson/wp-subtitle) and pu
Upgrade Notice
--------------

### 2.9
Add support for revisions and fix special character encoding.

### 2.8.1
Fix PHP warning - `get_admin_subtitle_value()` should be declared static.

Expand Down
62 changes: 57 additions & 5 deletions admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ public static function _setup() {
load_plugin_textdomain( 'wp-subtitle', false, dirname( WPSUBTITLE_BASENAME ) . '/languages' );

add_action( 'admin_init', array( 'WPSubtitle_Admin', '_admin_init' ) );
add_action( 'post_updated', array( 'WPSubtitle_Admin', '_save_post' ), 9 );
add_action( 'save_post', array( 'WPSubtitle_Admin', '_save_post' ) );
add_action( 'admin_enqueue_scripts', array( 'WPSubtitle_Admin', '_add_admin_scripts' ) );

add_filter( '_wp_post_revision_fields', array( 'WPSubtitle_Admin', '_wp_post_revision_fields' ), 9 );
add_action( 'wp_restore_post_revision', array( 'WPSubtitle_Admin', 'wp_restore_post_revision' ), 10, 2 );

}

/**
Expand Down Expand Up @@ -151,6 +156,37 @@ public static function _add_admin_scripts( $hook ) {

}

/**
* Add `wps_subtitle` to post revision fields.
*
* @since 2.9
* @internal
*
* @param array $fields Revision fields.
*/
public static function _wp_post_revision_fields( $fields ) {

$fields['wps_subtitle'] = __( 'Subtitle', 'wp-subtitle' );

return $fields;

}

/**
* Restore revisioned `wps_subtitle` value to post.
*
* @since 2.9
*
* @param int $post_id Post ID.
* @param int $revision_id Revision ID.
*/
public static function wp_restore_post_revision( $post_id, $revision_id ) {

$subtitle = new WP_Subtitle( $post_id );
$subtitle->restore_post_revision( $revision_id );

}

/**
* Add Admin Styles
*
Expand Down Expand Up @@ -237,8 +273,13 @@ public static function _add_subtitle_meta_box() {
$value = self::get_admin_subtitle_value( $post );

echo '<input type="hidden" name="wps_noncename" id="wps_noncename" value="' . wp_create_nonce( 'wp-subtitle' ) . '" />';
echo '<input type="text" id="wpsubtitle" name="wps_subtitle" value="' . esc_attr( htmlentities( $value ) ) . '" autocomplete="off" placeholder="' . esc_attr( apply_filters( 'wps_subtitle_field_placeholder', __( 'Enter subtitle here', 'wp-subtitle' ) ) ) . '" style="width:99%;" />';

// As of WordPress 4.3 no need to esc_attr() AND htmlentities().
// @see https://core.trac.wordpress.org/changeset/33271
echo '<input type="text" id="wpsubtitle" name="wps_subtitle" value="' . esc_attr( $value ) . '" autocomplete="off" placeholder="' . esc_attr( apply_filters( 'wps_subtitle_field_placeholder', __( 'Enter subtitle here', 'wp-subtitle' ) ) ) . '" style="width:99%;" />';

echo apply_filters( 'wps_subtitle_field_description', '', $post );

}

/**
Expand All @@ -258,9 +299,13 @@ public static function _add_subtitle_field() {

echo '<input type="hidden" name="wps_noncename" id="wps_noncename" value="' . wp_create_nonce( 'wp-subtitle' ) . '" />';
echo '<div id="subtitlediv" class="top">';
echo '<div id="subtitlewrap">';
echo '<input type="text" id="wpsubtitle" name="wps_subtitle" value="' . esc_attr( htmlentities( $value ) ) . '" autocomplete="off" placeholder="' . esc_attr( apply_filters( 'wps_subtitle_field_placeholder', __( 'Enter subtitle here', 'wp-subtitle' ) ) ) . '" />';
echo '</div>';
echo '<div id="subtitlewrap">';

// As of WordPress 4.3 no need to esc_attr() AND htmlentities().
// @see https://core.trac.wordpress.org/changeset/33271
echo '<input type="text" id="wpsubtitle" name="wps_subtitle" value="' . esc_attr( $value ) . '" autocomplete="off" placeholder="' . esc_attr( apply_filters( 'wps_subtitle_field_placeholder', __( 'Enter subtitle here', 'wp-subtitle' ) ) ) . '" />';

echo '</div>';

// Description
$description = apply_filters( 'wps_subtitle_field_description', '', $post );
Expand Down Expand Up @@ -323,10 +368,17 @@ public static function _save_post( $post_id ) {
// Check data and save
if ( isset( $_POST['wps_subtitle'] ) ) {

$new_value = $_POST['wps_subtitle'];

$subtitle = new WP_Subtitle( $post_id );

// Don't save if value not changed
if ( $subtitle->is_current_subtitle( $new_value ) ) {
return;
}

if ( $subtitle->current_user_can_edit() ) {
$subtitle->update_subtitle( $_POST['wps_subtitle'] );
$subtitle->update_subtitle( $new_value );
}

}
Expand Down
42 changes: 40 additions & 2 deletions includes/subtitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,22 @@ public function get_default_subtitle() {
*/
public function update_subtitle( $subtitle ) {

return update_post_meta( $this->post_id, $this->get_post_meta_key(), $subtitle );
// Uses `update_metadata` as `update_post_meta` doesn't work with revisions.
return update_metadata( 'post', $this->post_id, $this->get_post_meta_key(), $subtitle );

}

/**
* Is Current Subtitle?
*
* @since 2.9
*
* @param string $subtitle Subtitle value.
* @return boolean
*/
public function is_current_subtitle( $subtitle ) {

return $subtitle === get_metadata( 'post', $this->post_id, 'wps_subtitle', true );

}

Expand All @@ -121,6 +136,25 @@ private function get_post_meta_key() {

}

/**
* Restore revision.
*
* @since 2.9
*
* @param int $revision_id Revision ID.
*/
public function restore_post_revision( $revision_id ) {

$meta_value = get_metadata( 'post', $revision_id, $this->get_post_meta_key(), true );

if ( $meta_value ) {
$this->update_subtitle( $meta_value );
} else {
delete_post_meta( $this->post_id, $this->get_post_meta_key() );
}

}

/**
* Is Supported Post Type?
*
Expand All @@ -147,7 +181,7 @@ private function get_supported_post_types() {
'_builtin' => false
) );

$post_types = array_merge( $post_types, array( 'post', 'page' ) );
$post_types = array_merge( $post_types, array( 'post', 'page', 'revision' ) );

$supported = array();

Expand Down Expand Up @@ -175,6 +209,10 @@ public function current_user_can_edit() {

$post_type = get_post_type( $this->post_id );

if ( $revision = wp_is_post_revision( $this->post_id ) ) {
$post_type = get_post_type( $revision );
}

// Current user can...
switch ( $post_type ) {

Expand Down
13 changes: 11 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Contributors: husobj, husani
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=SLZUF4XJTS4E6
Tags: subtitle, content, title, subheading, subhead, alternate title
Requires at least: 3.7
Tested up to: 4.6.1
Stable tag: 2.8.1
Tested up to: 4.7.4
Stable tag: 2.9
License: GPLv2
License URI: http://www.gnu.org/licenses/gpl-2.0.txt

Expand Down Expand Up @@ -100,6 +100,12 @@ The plugin is [hosted on GitHub](https://github.com/benhuson/wp-subtitle) and pu

== Changelog ==

= Unreleased =

= 2.9 =
* Add support for post revisions. Props [Fabian Marz](https://github.com/fabianmarz).
* As of WordPress 4.3 no need to esc_attr() AND htmlentities() - can mess up special characters.

= 2.8.1 =
* Fix PHP warning - `get_admin_subtitle_value()` should be declared static.

Expand Down Expand Up @@ -170,6 +176,9 @@ The plugin is [hosted on GitHub](https://github.com/benhuson/wp-subtitle) and pu

== Upgrade Notice ==

= 2.9 =
Add support for revisions and fix special character encoding.

= 2.8.1 =
Fix PHP warning - `get_admin_subtitle_value()` should be declared static.

Expand Down
31 changes: 29 additions & 2 deletions wp-subtitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Plugin Name: WP Subtitle
Plugin URI: http://wordpress.org/plugins/wp-subtitle/
Description: Adds a subtitle field to pages and posts. Possible to add support for custom post types.
Version: 2.8.1
Version: 2.9
Author: Ben Huson, Husani Oakley
Author URI: https://github.com/benhuson/wp-subtitle
License: GPLv2
Expand Down Expand Up @@ -69,6 +69,33 @@ class WPSubtitle {
public static function _add_default_post_type_support() {
add_post_type_support( 'page', 'wps_subtitle' );
add_post_type_support( 'post', 'wps_subtitle' );
add_post_type_support( 'revision', 'wps_subtitle' );

add_filter( 'the_preview', array( 'WPSubtitle', 'the_preview' ), 10, 2 );

}

/**
* Returns the autosaved data to make changes visible in preview mode.
*
* @since 2.9
*
* @param object $post Post object.
* @param object $query Query object.
* @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
*/
public static function the_preview( $post, $query ) {

if ( isset( $_GET['preview_id'] ) ) {
return wp_get_post_autosave( $post->ID );
}

if ( $revisions = wp_get_post_revisions( $post->ID ) ) {
return array_shift( $revisions );
}

return $post;

}

/**
Expand All @@ -82,7 +109,7 @@ public static function get_supported_post_types() {
$post_types = (array) get_post_types( array(
'_builtin' => false
) );
$post_types = array_merge( $post_types, array( 'post', 'page' ) );
$post_types = array_merge( $post_types, array( 'post', 'page', 'revision' ) );
$supported = array();
foreach ( $post_types as $post_type ) {
if ( post_type_supports( $post_type, 'wps_subtitle' ) ) {
Expand Down

0 comments on commit 5342455

Please sign in to comment.