From 419caaf740690fe37c09ee0bb8265f8ce768d1e1 Mon Sep 17 00:00:00 2001 From: Ben Huson Date: Sun, 19 Apr 2020 10:16:34 +0100 Subject: [PATCH 1/3] Term subtitle support. --- plugin/admin/admin-terms.php | 171 ++++++++++++++++++++++++++++++ plugin/includes/class-api.php | 90 +++++++++++++++- plugin/includes/subtitle-term.php | 98 +++++++++++++++++ plugin/plugin.php | 3 + 4 files changed, 357 insertions(+), 5 deletions(-) create mode 100644 plugin/admin/admin-terms.php create mode 100644 plugin/includes/subtitle-term.php diff --git a/plugin/admin/admin-terms.php b/plugin/admin/admin-terms.php new file mode 100644 index 0000000..94889b9 --- /dev/null +++ b/plugin/admin/admin-terms.php @@ -0,0 +1,171 @@ + $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 ) { + + ?> +
+ + +
+ + + + + + 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' ) ); + + } + +} diff --git a/plugin/includes/class-api.php b/plugin/includes/class-api.php index e078b04..68b00ad 100644 --- a/plugin/includes/class-api.php +++ b/plugin/includes/class-api.php @@ -34,6 +34,9 @@ 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 ); + } /** @@ -62,11 +65,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 ); @@ -79,4 +78,85 @@ 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 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 ); + + } + + /** + * 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' => is_category() || is_tag() || is_tax() ? get_queried_object_id() : 0, + 'before' => '', + 'after' => '' + ) ); + + return $args; + + } + } diff --git a/plugin/includes/subtitle-term.php b/plugin/includes/subtitle-term.php new file mode 100644 index 0000000..4c0b9b7 --- /dev/null +++ b/plugin/includes/subtitle-term.php @@ -0,0 +1,98 @@ +object_id = absint( $term->term_id ); + } else { + $this->object_id = absint( $term ); + } + + } + + /** + * Get Meta Value + * + * @return string The subtitle meta value. + */ + public function get_meta_value() { + + return get_term_meta( $this->object_id, $this->get_meta_key(), true ); + + } + + /** + * Update Subtitle + * + * @param string $subtitle Subtitle. + * @return integer|WP_Error|bool Meta ID if new entry. True if updated, false if not updated or the same + * as current value. WP_Error when term_id is ambiguous between taxonomies. + */ + public function update_subtitle( $subtitle ) { + + $subtitle = trim( sanitize_text_field( $subtitle ) ); + + if ( '' == $subtitle ) { + return $this->delete_subtitle(); + } + + return update_term_meta( $this->object_id, $this->get_meta_key(), $subtitle ); + + } + + /** + * Delete Subtitle + * + * @return boolean True if deleted, false if failed. + */ + protected function delete_subtitle() { + + return delete_term_meta( $this->object_id, $this->get_meta_key() ); + + } + + /** + * Get Meta Key + * + * @return string The subtitle meta key. + */ + protected function get_meta_key() { + + return 'wps_subtitle'; + + } + + /** + * Current User Can Edit + * + * @return boolean + */ + public function current_user_can_edit() { + + $term = get_term( $this->object_id ); + $tax = get_taxonomy( $term->taxonomy ); + + return current_user_can( $tax->cap->edit_terms ); + + } + +} diff --git a/plugin/plugin.php b/plugin/plugin.php index 09f193d..b560332 100644 --- a/plugin/plugin.php +++ b/plugin/plugin.php @@ -14,6 +14,7 @@ // Includes include_once( WPSUBTITLE_DIR . 'includes/class-api.php' ); include_once( WPSUBTITLE_DIR . 'includes/subtitle.php' ); +require_once( WPSUBTITLE_DIR . 'includes/subtitle-term.php' ); include_once( WPSUBTITLE_DIR . 'includes/deprecated.php' ); include_once( WPSUBTITLE_DIR . 'includes/shortcode.php' ); include_once( WPSUBTITLE_DIR . 'includes/rest.php' ); @@ -24,6 +25,8 @@ // Include admin-only functionality if ( is_admin() ) { require_once( WPSUBTITLE_DIR . 'admin/admin.php' ); + require_once( WPSUBTITLE_DIR . 'admin/admin-terms.php' ); + if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { // Load AJAX functions here if required... } else { From cb49d9d96ada36a709e034246c029f58525e9703 Mon Sep 17 00:00:00 2001 From: Ben Huson Date: Sun, 19 Apr 2020 19:14:10 +0100 Subject: [PATCH 2/3] Add `plugins/wp_subtitle/the_archive_subtitle` and `plugins/wp_subtitle/get_archive_subtitle` APIs. --- plugin/includes/class-api.php | 41 +++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/plugin/includes/class-api.php b/plugin/includes/class-api.php index 68b00ad..2a8be75 100644 --- a/plugin/includes/class-api.php +++ b/plugin/includes/class-api.php @@ -37,6 +37,9 @@ public function setup_hooks() { 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 ); + } /** @@ -90,7 +93,7 @@ public function the_term_subtitle( $args = '' ) { } /** - * The Subtitle (Filter) + * The Term Subtitle (Filter) * * @param string $subtitle Subtitle. * @param array $args Display args. @@ -106,6 +109,40 @@ public function get_term_subtitle( $subtitle = '', $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) + * + * @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 * @@ -150,7 +187,7 @@ protected function post_parse_args( $args = '' ) { protected function term_parse_args( $args = '' ) { $args = wp_parse_args( $args, array( - 'term_id' => is_category() || is_tag() || is_tax() ? get_queried_object_id() : 0, + 'term_id' => 0, 'before' => '', 'after' => '' ) ); From d0d388b6c74f96c4a96de6be30b081c0283c1f69 Mon Sep 17 00:00:00 2001 From: Ben Huson Date: Sun, 19 Apr 2020 19:19:15 +0100 Subject: [PATCH 3/3] Document term and archive subtitle API. --- plugin/includes/class-api.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/plugin/includes/class-api.php b/plugin/includes/class-api.php index 2a8be75..5a3e55e 100644 --- a/plugin/includes/class-api.php +++ b/plugin/includes/class-api.php @@ -20,6 +20,34 @@ * 'after' => '

', // 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' => '

', // Before subtitle HTML output (default empty string) + * 'after' => '

', // 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' => '

', // Before subtitle HTML output (default empty string) + * 'after' => '

', // 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' => '

', // Before subtitle HTML output (default empty string) + * 'after' => '

', // 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' => '

', // Before subtitle HTML output (default empty string) + * 'after' => '

', // After subtitle HTML output (default empty string) + * ) ); */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly @@ -123,6 +151,9 @@ public function the_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.