jaredatch / Shared-Counts

WordPress plugin that leverages SharedCount.com API to quickly retrieve, cache, and display various social sharing counts.
GNU General Public License v2.0
47 stars 16 forks source link

FR - Remove jQuery dependency #113

Closed JiveDig closed 8 months ago

JiveDig commented 3 years ago

I just wanted to make an official feature request to remove the dependency on jQuery. For many of our client sites and theme demos we'd like to use Shared Counts (has been our go to for a while) but on many of these sites it's the only plugin relying on jQuery, which causes performance issues. I know it's not a big deal to many, but we see much better pagespeed and lighthouse scores on sites/pages without jQuery.

Figured I'd throw this out there to see what y'all think :)

billerickson commented 3 years ago

I agree, that would be a good move.

Personally I disable all the JavaScript and CSS from Shared Counts and change the email link to a mailto link, both to remove the need for the JS and markup for the modal, but also to decrease spam / remove the need for reCAPTCHA.

I'm not very good with vanilla JS. Would you be able to update Shared Counts to remove the dependency without reducing functionality (ie: modal still works)? Or should we include a "no JavaScript" option in the plugin that replicates what I already do on my client sites?

JiveDig commented 3 years ago

Or should we include a "no JavaScript" option in the plugin that replicates what I already do on my client sites?

Oh that's a good idea. That seems like the path of least resistance. Then another update at some point could remove jQuery. I could probably do it, but it would be a while.

I'd love a snippet if you have that handy too btw :)

billerickson commented 3 years ago

Here's what I do:

<?php
/**
 * Shared Counts
 *
 * @package      Cultivate
 * @author       CultivateWP
 * @since        1.0.0
 * @license      GPL-2.0+
 **/

// Disable CSS and JS.
add_filter( 'shared_counts_load_css', '__return_false' );
add_filter( 'shared_counts_load_js', '__return_false' );

/**
 * Simple email button
 * Does not require loading JS
 *
 * @param array $link Link.
 * @param int   $id ID.
 */
function be_shared_counts_email_link( $link, $id ) {
    if ( 'email' !== $link['type'] ) {
        return $link;
    }

    $subject      = esc_html__( 'Your friend has shared an article with you.', 'shared-counts' );
    $subject      = apply_filters( 'shared_counts_amp_email_subject', $subject, $id );
    $body         = html_entity_decode( get_the_title( $id ), ENT_QUOTES ) . "\r\n";
    $body        .= get_permalink( $id ) . "\r\n";
    $body         = apply_filters( 'shared_counts_amp_email_body', $body, $id );
    $link['link'] = 'mailto:?subject=' . rawurlencode( $subject ) . '&body=' . rawurlencode( $body );

    return $link;
}
add_filter( 'shared_counts_link', 'be_shared_counts_email_link', 10, 2 );
add_filter( 'shared_counts_disable_email_modal', '__return_true' );
billerickson commented 3 years ago

Now that I look at it, I remember that I already built this functionality into Shared Counts for when AMP is active :)

https://github.com/jaredatch/Shared-Counts/blob/develop/includes/class-shared-counts-amp.php

But I agree, there should be an option to enable this for non-AMP pages too.