From 331987984c4938a836af81c48544ffd5658c62bf Mon Sep 17 00:00:00 2001 From: Adam Borowski Date: Tue, 1 Nov 2022 11:25:51 +0100 Subject: [PATCH 1/4] feat: enable setting user custom fields for job title etc. Closes #1723 --- .../authors/class-authors-custom-fields.php | 94 +++++++++++++++++++ includes/class-newspack.php | 1 + 2 files changed, 95 insertions(+) create mode 100644 includes/authors/class-authors-custom-fields.php diff --git a/includes/authors/class-authors-custom-fields.php b/includes/authors/class-authors-custom-fields.php new file mode 100644 index 0000000000..b0647ce70c --- /dev/null +++ b/includes/authors/class-authors-custom-fields.php @@ -0,0 +1,94 @@ + 'newspack_job_title', + 'label' => __( 'Job Title', 'newspack' ), + ], + [ + 'name' => 'newspack_role', + 'label' => __( 'Role', 'newspack' ), + ], + [ + 'name' => 'newspack_employer', + 'label' => __( 'Employer/Organization', 'newspack' ), + ], + [ + 'name' => 'newspack_phone_number', + 'label' => __( 'Phone number', 'newspack' ), + ], + ]; + return apply_filters( 'newspack_authors_custom_fields', $default_fields ); + } + + /** + * Add user profile fields. + * + * @param WP_User $user The current WP_User object. + */ + public static function edit_user_profile( $user ) { + ?> +
+

+

+ + + + + + + + +
+ Date: Tue, 1 Nov 2022 12:08:36 +0100 Subject: [PATCH 2/4] feat: add job title to author footer if using Newspack theme --- .../authors/class-authors-custom-fields.php | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/includes/authors/class-authors-custom-fields.php b/includes/authors/class-authors-custom-fields.php index b0647ce70c..3de08f0c81 100644 --- a/includes/authors/class-authors-custom-fields.php +++ b/includes/authors/class-authors-custom-fields.php @@ -13,12 +13,20 @@ * Newspack Authors Custom Fields class. */ final class Authors_Custom_Fields { + const USER_META_NAMES = [ + 'job_title' => 'newspack_job_title', + 'role' => 'newspack_role', + 'employer' => 'newspack_employer', + 'phone_number' => 'newspack_phone_number', + ]; + /** * Initialize hooks. */ public static function init() { \add_action( 'edit_user_profile', [ __CLASS__, 'edit_user_profile' ] ); \add_action( 'edit_user_profile_update', [ __CLASS__, 'edit_user_profile_update' ] ); + \add_filter( 'newspack_author_bio_name', [ __CLASS__, 'newspack_author_bio_name' ], 10, 2 ); } /** @@ -40,19 +48,19 @@ public static function edit_user_profile_update( $user_id ) { public static function get_custom_fields() { $default_fields = [ [ - 'name' => 'newspack_job_title', + 'name' => self::USER_META_NAMES['job_title'], 'label' => __( 'Job Title', 'newspack' ), ], [ - 'name' => 'newspack_role', + 'name' => self::USER_META_NAMES['role'], 'label' => __( 'Role', 'newspack' ), ], [ - 'name' => 'newspack_employer', + 'name' => self::USER_META_NAMES['employer'], 'label' => __( 'Employer/Organization', 'newspack' ), ], [ - 'name' => 'newspack_phone_number', + 'name' => self::USER_META_NAMES['phone_number'], 'label' => __( 'Phone number', 'newspack' ), ], ]; @@ -90,5 +98,19 @@ class="regular-text" ' . $job_title . ''; + } + return $author_name; + } } Authors_Custom_Fields::init(); From 728f9e2cb5da8c09ecd033bfa9feba35877246c4 Mon Sep 17 00:00:00 2001 From: Adam Borowski Date: Tue, 1 Nov 2022 12:48:48 +0100 Subject: [PATCH 3/4] feat: support co-authors-plus --- .../authors/class-authors-custom-fields.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/includes/authors/class-authors-custom-fields.php b/includes/authors/class-authors-custom-fields.php index 3de08f0c81..d9dce1d6fd 100644 --- a/includes/authors/class-authors-custom-fields.php +++ b/includes/authors/class-authors-custom-fields.php @@ -27,6 +27,7 @@ public static function init() { \add_action( 'edit_user_profile', [ __CLASS__, 'edit_user_profile' ] ); \add_action( 'edit_user_profile_update', [ __CLASS__, 'edit_user_profile_update' ] ); \add_filter( 'newspack_author_bio_name', [ __CLASS__, 'newspack_author_bio_name' ], 10, 2 ); + \add_filter( 'coauthors_guest_author_fields', [ __CLASS__, 'coauthors_guest_author_fields' ], 10, 2 ); } /** @@ -107,10 +108,35 @@ class="regular-text" */ public static function newspack_author_bio_name( $author_name, $author_id ) { $job_title = \get_user_meta( $author_id, self::USER_META_NAMES['job_title'], true ); + + // Try a Co-Authors Plus field. + if ( empty( $job_title ) ) { + $job_title = \get_post_meta( $author_id, 'cap-' . self::USER_META_NAMES['job_title'], true ); + } + if ( ! empty( $job_title ) ) { $author_name .= '' . $job_title . ''; } return $author_name; } + + /** + * Add custom fields to the Co-Authors Plus guest author form. + * + * @param array $fields Fields. + * @param array $groups Queried groups. + */ + public static function coauthors_guest_author_fields( $fields, $groups ) { + if ( in_array( 'about', $groups ) || in_array( 'all', $groups ) ) { + foreach ( self::get_custom_fields() as $custom_field ) { + $fields[] = [ + 'key' => $custom_field['name'], + 'label' => $custom_field['label'], + 'group' => 'about', + ]; + } + } + return $fields; + } } Authors_Custom_Fields::init(); From c72631b2bb8eafab2c966093fe975a5018966887 Mon Sep 17 00:00:00 2001 From: Adam Borowski Date: Wed, 2 Nov 2022 14:42:50 +0100 Subject: [PATCH 4/4] fix: also handle the current user --- includes/authors/class-authors-custom-fields.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/authors/class-authors-custom-fields.php b/includes/authors/class-authors-custom-fields.php index d9dce1d6fd..1497642d44 100644 --- a/includes/authors/class-authors-custom-fields.php +++ b/includes/authors/class-authors-custom-fields.php @@ -25,7 +25,9 @@ final class Authors_Custom_Fields { */ public static function init() { \add_action( 'edit_user_profile', [ __CLASS__, 'edit_user_profile' ] ); + \add_action( 'show_user_profile', [ __CLASS__, 'edit_user_profile' ] ); \add_action( 'edit_user_profile_update', [ __CLASS__, 'edit_user_profile_update' ] ); + \add_action( 'personal_options_update', [ __CLASS__, 'edit_user_profile_update' ] ); \add_filter( 'newspack_author_bio_name', [ __CLASS__, 'newspack_author_bio_name' ], 10, 2 ); \add_filter( 'coauthors_guest_author_fields', [ __CLASS__, 'coauthors_guest_author_fields' ], 10, 2 ); }