Skip to content

Commit

Permalink
Merge branch 'release/3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
benhuson committed Sep 6, 2018
2 parents 473dfce + a693f52 commit c658f69
Show file tree
Hide file tree
Showing 9 changed files with 380 additions and 21 deletions.
14 changes: 12 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [3.1] - 2018-09-04

### Added
- `%%wps_subtitle%%` placeholders for Yoast SEO compatibility.
- WooCommerce compatibility. Go to `WooCommerce > Settings > Products > Display` for settings.
- `wps_subtitle_field_position` filter to position admin field `after_title`, `before_title` or otherwise in a meta box.
- Use metabox UI if editing in Gutenberg.

## [3.0] - 2017-09-05

### Added
- Make `wps_subtitle` available via WordPress REST API.
- Added `wps_subtitle_field_position` filter to show subtitle admin field `before_title`, `after_title` or in meta box.

## [2.9.1] - 2017-06-01

Expand All @@ -20,7 +29,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- 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.
- As of WordPress 4.3 no need to `esc_attr()` AND `htmlentities()` - can mess up special characters.

## [2.8.1] - 2016-09-14

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

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

### Unreleased

### 3.1
WooCommerce compatibility: Go to `WooCommerce > Settings > Products > Display` for settings. Yoast SEO compatibility: Added `%%wps_subtitle%%` placeholders. Gutenberg compatibility: Use metabox UI.

### 3.0
Make `wps_subtitle` available via WordPress REST API.

Expand Down
74 changes: 71 additions & 3 deletions admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,23 @@ public static function _admin_init() {

// Setup Field / Meta Box
if ( WPSubtitle::is_supported_post_type( $post_type ) ) {
if ( self::edit_form_after_title_supported( $post_type ) ) {

$position = self::subtitle_field_position( $post_type );

if ( 'after_title' == $position ) {
add_action( 'admin_head', array( 'WPSubtitle_Admin', '_add_admin_styles' ) );
add_action( 'edit_form_after_title', array( 'WPSubtitle_Admin', '_add_subtitle_field' ) );
} elseif ( 'before_title' == $position ) {
add_action( 'admin_head', array( 'WPSubtitle_Admin', '_add_admin_styles' ) );
add_action( 'edit_form_top', array( 'WPSubtitle_Admin', '_add_subtitle_field' ) );
} else {
add_action( 'add_meta_boxes', array( 'WPSubtitle_Admin', '_add_meta_boxes' ) );
}

add_filter( 'manage_edit-' . $post_type . '_columns', array( 'WPSubtitle_Admin', 'manage_subtitle_columns' ) );
add_action( 'manage_' . $post_type . '_posts_custom_column', array( 'WPSubtitle_Admin', 'manage_subtitle_columns_content' ), 10, 2 );
add_action( 'quick_edit_custom_box', array( 'WPSubtitle_Admin', 'quick_edit_custom_box' ), 10, 2 );

}

}
Expand Down Expand Up @@ -110,9 +117,24 @@ public static function manage_subtitle_columns( $columns ) {

$new_columns = array();

// Subtitle column after...
$after_column = '';
if ( array_key_exists( 'title', $columns ) ) {
$after_column = 'title';
} elseif ( array_key_exists( 'name', $columns ) ) {
$after_column = 'name';
}

// Add column
if ( empty( $after_column ) ) {
$columns['wps_subtitle'] = __( 'Subtitle', 'wp-subtitle' );
return $columns;
}

// Insert column
foreach ( $columns as $column => $value ) {
$new_columns[ $column ] = $value;
if ( 'title' == $column ) {
if ( $after_column == $column ) {
$new_columns['wps_subtitle'] = __( 'Subtitle', 'wp-subtitle' );
}
}
Expand Down Expand Up @@ -253,7 +275,11 @@ public static function get_meta_box_title( $post_type ) {
public static function _add_meta_boxes() {
$post_types = WPSubtitle::get_supported_post_types();
foreach ( $post_types as $post_type ) {
add_meta_box( 'wps_subtitle_panel', self::get_meta_box_title( $post_type ), array( 'WPSubtitle_Admin', '_add_subtitle_meta_box' ), $post_type, 'normal', 'high' );

$positiom = self::gutenberg_supported( $post_type ) ? 'side' : 'normal';

add_meta_box( 'wps_subtitle_panel', self::get_meta_box_title( $post_type ), array( 'WPSubtitle_Admin', '_add_subtitle_meta_box' ), $post_type, $positiom, 'high' );

}
}

Expand Down Expand Up @@ -439,4 +465,46 @@ private static function edit_form_after_title_supported( $post_type = '' ) {
return ! apply_filters( 'wps_subtitle_use_meta_box', false, $post_type );
}

/**
* Gutenberg Supported
*
* @since 3.1
*
* @param string $post_type Post type.
* @return bool
*/
private static function gutenberg_supported( $post_type = '' ) {

if ( function_exists( 'gutenberg_can_edit_post_type' ) && gutenberg_can_edit_post_type( $post_type ) && ! isset( $_GET['classic-editor'] ) ) {
return true;
}

return false;

}

/**
* Subtitle Field Position
*
* @since 3.1
*
* @param string $post_type Post type.
* @param string Position.
*/
private static function subtitle_field_position( $post_type = '' ) {

$position = apply_filters( 'wps_subtitle_field_position', 'after_title', $post_type );

if ( self::gutenberg_supported( $post_type ) ) {
return '';
}

if ( ! self::edit_form_after_title_supported( $post_type ) ) {
return '';
}

return $position;

}

}
138 changes: 138 additions & 0 deletions includes/compat/woocommerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

/**
* @package WP Subtitle
* @subpackage WooCommerce
*
* @since 3.1
*
* Compatibility for the WooCommerce plugin:
* https://wordpress.org/plugins/woocommerce/
*/

class WPSubtitle_WooCommerce {

/**
* Constructor
*
* @since 3.1
*
* @internal Do not create multiple instances.
*/
public function __construct() {

if ( 'yes' == get_option( 'wp_subtitle_woocommerce_enabled' ) ) {

add_action( 'init', array( $this, 'add_product_post_type_support' ) );

if ( 'yes' == get_option( 'wp_subtitle_woocommerce_show_on_single' ) ) {
add_action( 'woocommerce_single_product_summary' , array( $this, 'single_product_summary' ), 6 );
}

if ( 'yes' == get_option( 'wp_subtitle_woocommerce_show_in_loop' ) ) {
add_action( 'woocommerce_shop_loop_item_title' , array( $this, 'shop_loop_item_title' ) );
}

}

add_filter( 'woocommerce_product_settings' , array( $this, 'product_settings' ) );

}

/**
* Add Product Post Type Support
*
* @since 3.1
*
* @internal Private. Called via the `init` action.
*/
public function add_product_post_type_support() {

add_post_type_support( 'product', 'wps_subtitle' );

}

/**
* Single Product Summary
*
* @since 3.1
*
* @internal Private. Called via the `woocommerce_single_product_summary` action.
*/
public function single_product_summary() {

the_subtitle( '<h2 class="product_subtitle entry-subtitle wp-subtitle">', '</h2>' );

}

/**
* Shop Loop Item Title
*
* @since 3.1
*
* @internal Private. Called via the `woocommerce_shop_loop_item_title` action.
*/
public function shop_loop_item_title() {

the_subtitle( '<p class="woocommerce-loop-product__subtitle wp-subtitle">', '</p>' );

}

/**
* Product Settings
*
* @since 3.1
*
* @param array $settings Settings.
* @return array Settings.
*
* @internal Private. Called via the `woocommerce_product_settings` filter.
*/
public function product_settings( $settings ) {

$subtitle_settings = array(

array(
'title' => __( 'WP Subtitle', 'wp-subtitle' ),
'type' => 'title',
'desc' => '',
'id' => 'wp_subtitle_options'
),

array(
'title' => __( 'Enable Product Subtitles', 'wp-subtitle' ),
'desc' => __( 'Add subtitle field to product edit screen', 'wp-subtitle' ),
'id' => 'wp_subtitle_woocommerce_enabled',
'default' => 'no',
'type' => 'checkbox',
),

array(
'title' => __( 'Subtitle Display', 'wp-subtitle' ),
'desc' => __( 'Show on single product pages', 'wp-subtitle' ),
'id' => 'wp_subtitle_woocommerce_show_on_single',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => 'start'
),

array(
'desc' => __( 'Show on product archives', 'wp-subtitle' ),
'id' => 'wp_subtitle_woocommerce_show_in_loop',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => 'end'
),

array(
'type' => 'sectionend',
'id' => 'wp_subtitle_options'
)

);

return array_merge( $settings, $subtitle_settings );

}

}
81 changes: 81 additions & 0 deletions includes/compat/wordpress-seo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/**
* @package WP Subtitle
* @subpackage WordPress SEO
*
* @since 3.1
*
* Compatibility for the Yoast SEO plugin:
* https://wordpress.org/plugins/wordpress-seo/
*
* Adds support for a `%%wps_subtitle%%` placeholder to include
* the subtitle in browser titles and meta descriptions.
*
* Also adds `%%wps_subtitle_before_sep%%` and `%%wps_subtitle_after_sep%%`.
* These can be used to add seperators before/after the subtitle. If there
* is no subtitle set these placeholders will not be output.
*
* The seperator placeholders can be customized using the `wps_subtitle_seo_before_sep`
* and `wps_subtitle_seo_after_sep` filters.
*
* The seperator placeholders include a 'space' either side by default. This means that
* you should create your title template with no whitespace around the seperator placeholders.
* It allows you to customize the seperators to include commas and butt the seperator up to
* the preceding/following text.
*
* e.g. If '%%wps_subtitle_before_sep%%' is set to ', ':
* `%%title%%%%wps_subtitle_before_sep%%%%wps_subtitle%% %%sep%% %%sitename%%`
* "Title, Subtitle - Sitename"
*/

class WPSubtitle_WPSEO {

/**
* Constructor
*
* @since 3.1
*
* @internal Do not create multiple instances.
*/
public function __construct() {

add_filter( 'wpseo_replacements', array( $this, 'add_wpseo_replacements' ) );

}

/**
* Add SEO Replacements
*
* @since 3.1
*
* @param array $replacements SEO replacements.
* @return array Filtered replacements.
*
* @internal Called via the `wpseo_replacements` filter.
*/
public function add_wpseo_replacements( $replacements ) {

global $post;

$wp_subtitle = new WP_Subtitle( $post );
$subtitle = $wp_subtitle->get_subtitle();

$replacements['%%wps_subtitle%%'] = $subtitle;
$replacements['%%wps_subtitle_before_sep%%'] = '';
$replacements['%%wps_subtitle_after_sep%%'] = '';

if ( ! empty( $subtitle ) ) {

$sep = isset( $replacements['%%sep%%'] ) ? ' ' . $replacements['%%sep%%'] . ' ' : ' - ';

$replacements['%%wps_subtitle_before_sep%%'] = apply_filters( 'wps_subtitle_seo_before_sep', $sep );
$replacements['%%wps_subtitle_after_sep%%'] = apply_filters( 'wps_subtitle_seo_after_sep', $sep );

}

return $replacements;

}

}
Loading

0 comments on commit c658f69

Please sign in to comment.