From 4018af61acbc8a952355a5b138808618de3efc26 Mon Sep 17 00:00:00 2001 From: Adam Boro Date: Thu, 3 Nov 2022 17:35:26 +0100 Subject: [PATCH] feat: enable setting user custom fields for job title etc. (#2102) Closes #1723 --- .../authors/class-authors-custom-fields.php | 144 ++++++++++++++++++ includes/class-newspack.php | 1 + 2 files changed, 145 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..1497642d44 --- /dev/null +++ b/includes/authors/class-authors-custom-fields.php @@ -0,0 +1,144 @@ + '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( '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 ); + } + + /** + * Save custom fields. + * + * @param int $user_id User ID. + */ + public static function edit_user_profile_update( $user_id ) { + foreach ( self::get_custom_fields() as $field ) { + if ( isset( $_POST[ $field['name'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing + \update_user_meta( $user_id, $field['name'], sanitize_text_field( $_POST[ $field['name'] ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing + } + } + } + + /** + * Get custom profile fields list. + */ + public static function get_custom_fields() { + $default_fields = [ + [ + 'name' => self::USER_META_NAMES['job_title'], + 'label' => __( 'Job Title', 'newspack' ), + ], + [ + 'name' => self::USER_META_NAMES['role'], + 'label' => __( 'Role', 'newspack' ), + ], + [ + 'name' => self::USER_META_NAMES['employer'], + 'label' => __( 'Employer/Organization', 'newspack' ), + ], + [ + 'name' => self::USER_META_NAMES['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 ) { + ?> +
+

+

+ + + + + + + + +
+ ' . $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(); diff --git a/includes/class-newspack.php b/includes/class-newspack.php index ac014ab236..332fda5466 100644 --- a/includes/class-newspack.php +++ b/includes/class-newspack.php @@ -98,6 +98,7 @@ private function includes() { include_once NEWSPACK_ABSPATH . 'includes/class-blocks.php'; include_once NEWSPACK_ABSPATH . 'includes/class-meta-pixel.php'; include_once NEWSPACK_ABSPATH . 'includes/revisions-control/class-revisions-control.php'; + include_once NEWSPACK_ABSPATH . 'includes/authors/class-authors-custom-fields.php'; include_once NEWSPACK_ABSPATH . 'includes/optional-modules/class-rss.php';