thoughtis / cata-co-authors-plus

Common functions, configuration and compatibility fixes for Co-Authors Plus when used in Cata child themes. Not a fork or replacement for CAP.
GNU General Public License v3.0
0 stars 0 forks source link

Add modern social networks to Guest Author profiles #3

Closed douglas-johnson closed 3 years ago

douglas-johnson commented 3 years ago

On Thought Catalog we handled it this way:

add_filter( 'coauthors_guest_author_fields', array( $this, 'tc_add_social_fields' ), 10, 2 );
/**
 * Add social media fields to Guest Author profile, used to pull in buttons and timeline
 */

public function tc_add_social_fields( $fields_to_return, $groups ) {

    if ( in_array( 'all', $groups ) || in_array( 'contact-info', $groups ) ) {

        $fields_to_return[] = array(
            'key'      => 'quotecatalog',
            'label'    => 'Quote Catalog',
            'group'    => 'contact-info',
            'sanitize_function' => 'sanitize_text_field',
        );

        $fields_to_return[] = array(
            'key'      => 'collectiveworld',
            'label'    => 'Collective World',
            'group'    => 'contact-info',
            'sanitize_function' => 'sanitize_text_field',
        );

        $fields_to_return[] = array(
            'key'      => 'twitter',
            'label'    => 'Twitter',
            'group'    => 'contact-info',
            'sanitize_function' => 'sanitize_text_field',
        );

        $fields_to_return[] = array(
            'key'      => 'facebook',
            'label'    => 'Facebook',
            'group'    => 'contact-info',
            'sanitize_function' => 'sanitize_text_field',
        );

        $fields_to_return[] = array(
            'key'      => 'instagram',
            'label'    => 'Instagram',
            'group'    => 'contact-info',
            'sanitize_function' => 'sanitize_text_field',
        );

        $fields_to_return[] = array(
            'key'      => 'tumblr',
            'label'    => 'Tumblr',
            'group'    => 'contact-info',
            'sanitize_function' => 'sanitize_text_field',
        );

        $fields_to_return[] = array(
            'key'      => 'amazon',
            'label'    => 'Amazon',
            'group'    => 'contact-info',
            'sanitize_function' => 'sanitize_text_field',
        );

        $fields_to_return[] = array(
            'key'      => 'ibooks',
            'label'    => 'iBooks',
            'group'    => 'contact-info',
            'sanitize_function' => 'sanitize_text_field',
        );

    }// End if().

    return $fields_to_return;
}

On Shop Catalog we added the fields through ACF. We won't have that on every site, but here are the getter functions just as examples.

/**
 * Social
 * 
 * @package Shop_Catalog\CoAuthors
 */

namespace Shop_Catalog\CoAuthors;

use stdClass;

/**
 * Social
 */
class Social {

    /**
     * Platforms
     * 
     * @var array - social platform URLs we can record.
     */
    const PLATFORMS = array(
        'instagram',
        'twitter',
        'facebook',
        'pinterest'
    );

    /**
     * Get Platforms
     * 
     * @return array - PLATFORMS constant.
     */
    public static function get_platforms() {
        return self::PLATFORMS;
    }

    /**
     * Get Links
     * 
     * @param stdClass $author - Guest Author object.
     */
    public static function get_links( stdClass $author ) : array {

        $links = array_map(
            function( $platform_id ) use ( $author ) {
                return array(
                    'platform' => $platform_id,
                    'url'      => self::get_link( $platform_id, $author ),
                );
            },
            self::get_platforms()
        );

        $links = array_values(
            array_filter( $links, function( $link ) {
                return false !== wp_http_validate_url( $link['url'] );
            } )
        );

        return $links;

    }

    /**
     * Get Link
     * 
     * @param  string $platform_id - the link we're looking for.
     * @param  stdClass $author - Guest Author object.
     * @return string - a URL or empty string.
     */
    public static function get_link( string $platform_id = 'instagram', stdClass $author ) : string {
        return shopcatalog_get_guest_author_acf_data( $platform_id, $author, '' );
    }

}
douglas-johnson commented 3 years ago

TikTok, Twitter, Instagram, and website where requested by editorial / community folks.