Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subtitle support for terms. #51

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 171 additions & 0 deletions plugin/admin/admin-terms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php

/**
* @package WP Subtitle
* @subpackage Admin Terms
*/

add_action( 'plugins_loaded', array( 'WPSubtitle_Admin_Terms', 'setup_hooks' ) );

class WPSubtitle_Admin_Terms {

/**
* Setup Hooks
*/
public static function setup_hooks() {

add_action( 'admin_init', array( get_class(), 'add_admin_fields' ) );

$taxonomies = self::get_supported_taxonomies();

foreach ( $taxonomies as $taxonomy ) {
add_filter( 'manage_edit-' . $taxonomy . '_columns', array( get_class(), 'taxonomy_columns' ) );
add_filter( 'manage_' . $taxonomy . '_custom_column', array( get_class(), 'term_row' ), 15, 3 );
add_action( 'create_' . $taxonomy, array( get_class(), 'update_term_meta' ), 10 );
add_action( 'edited_' . $taxonomy, array( get_class(), 'update_term_meta' ), 10 );
}

}

/**
* Edit Taxonomy Columns
*
* @param array A list of columns.
* @return array List of columns with "Subtitle" inserted after the title.
*
* @internal Private. Called via the `manage_edit-{$taxonomy}_columns` filter.
*/
public function taxonomy_columns( $original_columns ) {

$new_columns = array();

foreach ( $original_columns as $key => $value ) {

$new_columns[ $key ] = $value;

if ( 'name' == $key ) {
$new_columns['subtitle'] = esc_html__( 'Subtitle', 'wp-subtitle' );
}

}

return $new_columns;

}

/**
* Edit Term Row
*
* @param string Row.
* @param string Name of the current column.
* @param integer Term ID.
* @return string HTML display.
*
* @internal Private. Called via the `manage_{$taxonomy}_custom_column` filter.
*/
public function term_row( $row, $column_name, $term_id ) {

if ( 'subtitle' === $column_name ) {

$subtitle = new WP_Subtitle_Term( $term_id );

return $row . esc_html( $subtitle->get_meta_value() );

}

return $row;

}

/**
* Add Admin Fields
*
* @internal Private. Called via the `admin_init` action.
*/
public static function add_admin_fields() {

$taxonomies = self::get_supported_taxonomies();

foreach ( $taxonomies as $taxonomy ) {
add_action( $taxonomy . '_add_form_fields', array( get_class(), 'add_form' ) );
add_action( $taxonomy . '_edit_form_fields', array( get_class(), 'edit_form' ), 30, 2 );
}

}

/**
* Add Term Form
*
* Create image control for `wp-admin/term.php`.
*
* @param string Taxonomy slug.
*
* @internal Private. Called via the `{$taxonomy}_add_form_fields` action.
*/
public static function add_form( $taxonomy ) {

?>
<div class="form-field term-wps-subtitle-wrap">
<label for="wps_subtitle"><?php esc_html_e( 'Subtitle', 'wp-subtitle' ); ?></label>
<input name="wps_subtitle" id="wps_subtitle" type="text" value="" size="40">
</div>
<?php

}

/**
* Edit Term Form
*
* Create image control for `wp-admin/term.php`.
*
* @param WP_Term Term object.
* @param string Taxonomy slug.
*
* @internal Private. Called via the `{$taxonomy}_edit_form_fields` action.
*/
public static function edit_form( $term, $taxonomy ) {

$term_subtitle = new WP_Subtitle_Term( $term );

?>
<tr class="form-field term-wps-subtitle-wrap">
<th scope="row"><label for="wps_subtitle"><?php esc_html_e( 'Subtitle', 'wp-subtitle' ); ?></label></th>
<td><input name="wps_subtitle" id="wps_subtitle" type="text" value="<?php echo esc_attr( $term_subtitle->get_meta_value() ); ?>" size="40"></td>
</tr>
<?php

}

/**
* Update Term Meta
*
* @param integer $term_id Term ID.
*
* @internal Private. Called via the `edited_{$taxonomy}` action.
*/
public static function update_term_meta( $term_id ) {

$term_subtitle = new WP_Subtitle_Term( $term_id );

if ( ! $term_subtitle->current_user_can_edit() ) {
return;
}

if ( isset( $_POST[ 'wps_subtitle' ] ) ) {
$term_subtitle->update_subtitle( $_POST[ 'wps_subtitle' ] );
}

}

/**
* Get Supported Taxonomies
*
* @return array
*/
private static function get_supported_taxonomies() {

return apply_filters( 'plugins/wp_subtitle/supported_taxonomies', array( 'category' ) );

}

}
158 changes: 153 additions & 5 deletions plugin/includes/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,34 @@
* 'after' => '</p>', // After subtitle HTML output (default empty string)
* 'post_id' => get_the_ID() // Post ID (default current post ID)
* ) );
*
* // Example: Display term subtitle
* do_action( 'plugins/wp_subtitle/the_term_subtitle', array(
* 'before' => '<p class="subtitle">', // Before subtitle HTML output (default empty string)
* 'after' => '</p>', // After subtitle HTML output (default empty string)
* 'term_id' => 0, // Term ID (default to none)
* 'default_value' => '' // Default output (if no subtitle)
* ) );
*
* // Example: Get term subtitle display
* $subtitle = apply_filters( 'plugins/wp_subtitle/get_term_subtitle', '', array(
* 'before' => '<p class="subtitle">', // Before subtitle HTML output (default empty string)
* 'after' => '</p>', // After subtitle HTML output (default empty string)
* 'term_id' => 0 // Term ID (default to none)
* ) );
*
* // Example: Display archive subtitle
* do_action( 'plugins/wp_subtitle/the_archive_subtitle', array(
* 'before' => '<p class="subtitle">', // Before subtitle HTML output (default empty string)
* 'after' => '</p>', // After subtitle HTML output (default empty string)
* 'default_value' => '' // Default output (if no subtitle)
* ) );
*
* // Example: Get archive subtitle display
* $subtitle = apply_filters( 'plugins/wp_subtitle/get_archive_subtitle', '', array(
* 'before' => '<p class="subtitle">', // Before subtitle HTML output (default empty string)
* 'after' => '</p>', // After subtitle HTML output (default empty string)
* ) );
*/

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
Expand All @@ -34,6 +62,12 @@ public function setup_hooks() {
add_action( 'plugins/wp_subtitle/the_subtitle', array( $this, 'the_subtitle' ) );
add_filter( 'plugins/wp_subtitle/get_subtitle', array( $this, 'get_subtitle' ), 10, 2 );

add_action( 'plugins/wp_subtitle/the_term_subtitle', array( $this, 'the_term_subtitle' ) );
add_filter( 'plugins/wp_subtitle/get_term_subtitle', array( $this, 'get_term_subtitle' ), 10, 2 );

add_action( 'plugins/wp_subtitle/the_archive_subtitle', array( $this, 'the_archive_subtitle' ) );
add_filter( 'plugins/wp_subtitle/get_archive_subtitle', array( $this, 'get_archive_subtitle' ), 10, 2 );

}

/**
Expand Down Expand Up @@ -62,11 +96,7 @@ public function the_subtitle( $args = '' ) {
*/
public function get_subtitle( $default_subtitle, $args = '' ) {

$args = wp_parse_args( $args, array(
'post_id' => get_the_ID(), // Post ID
'before' => '', // Before subtitle HTML output
'after' => '' // After subtitle HTML output
) );
$args = $this->post_parse_args( $args );

$subtitle_obj = new WP_Subtitle( $args['post_id'] );
$subtitle = $subtitle_obj->get_subtitle( $args );
Expand All @@ -79,4 +109,122 @@ public function get_subtitle( $default_subtitle, $args = '' ) {

}

/**
* The Term Subtitle (Action)
*
* @param array $args Display args.
*/
public function the_term_subtitle( $args = '' ) {

echo apply_filters( 'plugins/wp_subtitle/get_term_subtitle', '', $args );

}

/**
* The Term Subtitle (Filter)
*
* @param string $subtitle Subtitle.
* @param array $args Display args.
* @return string Subtitle.
*/
public function get_term_subtitle( $subtitle = '', $args = '' ) {

$args = $this->term_parse_args( $args );

$subtitle = new WP_Subtitle_Term( $args['term_id'] );

return $this->get_display( $subtitle->get_meta_value(), $args );

}

/**
* The Archive Subtitle (Action)
*
* @param array $args Display args.
*/
public function the_archive_subtitle( $args = '' ) {

echo apply_filters( 'plugins/wp_subtitle/get_archive_subtitle', '', $args );

}

/**
* The Archive Subtitle (Filter)
*
* If the main blog page when posts page is set, get the subtitle of the page.
* If a categiry/tag/term archive, get the term subtitle.
*
* @param string $subtitle Subtitle.
* @param array $args Display args.
* @return string Subtitle.
*/
public function get_archive_subtitle( $subtitle = '', $args = '' ) {

if ( is_home() && ! is_front_page() ) {
$args['post_id'] = get_option( 'page_for_posts', 0 );
return apply_filters( 'plugins/wp_subtitle/get_subtitle', '', $args );
}

if ( is_category() || is_tag() || is_tax() ) {
$args['term_id'] = get_queried_object_id();
return apply_filters( 'plugins/wp_subtitle/get_term_subtitle', '', $args );
}

return '';

}

/**
* Get Display
*
* @param string $subtitle Subtitle.
* @param array $args Display args.
* @return string Subtitle.
*/
protected function get_display( $subtitle, $args ) {

if ( ! empty( $subtitle ) ) {
$subtitle = $args['before'] . $subtitle . $args['after'];
}

return $subtitle;

}

/**
* Post Parse Args
*
* @param array $args Args.
* @return array Args.
*/
protected function post_parse_args( $args = '' ) {

$args = wp_parse_args( $args, array(
'post_id' => get_the_ID(), // Post ID
'before' => '', // Before subtitle HTML output
'after' => '' // After subtitle HTML output
) );

return $args;

}

/**
* Term Parse Args
*
* @param array $args Args.
* @return array Args.
*/
protected function term_parse_args( $args = '' ) {

$args = wp_parse_args( $args, array(
'term_id' => 0,
'before' => '',
'after' => ''
) );

return $args;

}

}
Loading