Skip to content

Widgets: add missing internet-defense-league widget#7030

Merged
eliorivero merged 12 commits intomasterfrom
add/internet-defense-league-widget
Apr 24, 2017
Merged

Widgets: add missing internet-defense-league widget#7030
eliorivero merged 12 commits intomasterfrom
add/internet-defense-league-widget

Conversation

@gwwar
Copy link
Copy Markdown
Contributor

@gwwar gwwar commented Apr 21, 2017

This is part of 185-gh-customization

Changes proposed in this Pull Request:

This adds a wpcom widget to Jetpack. This is copied mostly verbatim from wpcom, with some small modifications for stats and the registration of the module.

Testing instructions:

  • In your test sandbox, navigate to your Jetpack plugin and checkout this branch
  • Navigate to the customizer /wp-admin/customize.php
  • Click on widgets
  • Then select a widget area
  • Click add a widget
  • With a connected Jetpack site, you should be able to search for Internet Defense League
    screen shot 2017-04-21 at 1 03 06 pm
  • Add it to your sidebar
  • The widget options should work, (different img types/visibility)
    screen shot 2017-04-21 at 1 04 59 pm
  • Publish your page
  • We should see that the Jetpack site now has an option of widget_internet_defense_league_widget which might have a shape like
    screen shot 2017-04-21 at 1 09 13 pm

Copy link
Copy Markdown
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few comments of things that could be fixed.

function __construct() {
parent::__construct(
'internet_defense_league_widget',
apply_filters( 'jetpack_widget_name_widget', esc_html__( 'Internet Defense League', 'jetpack' ) ),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jetpack_widget_name is the right filter name here, and you'll need to add a docblock in front of that filter. Something like /** This filter is documented in modules/widgets/facebook-likebox.php */

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious, why not link to a doc page or simply copy the doc here?

/**
 * Filter the name of a widget included in the Extra Sidebar Widgets module.
 *
 * @module widgets
 *
 * @since 2.1.2
 *
 * @param string $widget_name Widget title.
 */

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in c960d98

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gwwar We only include the full docblock once, so the parser doesn't create multiple entries for the same hook. That's how WordPress Core works as well.

'internet_defense_league_widget',
apply_filters( 'jetpack_widget_name_widget', esc_html__( 'Internet Defense League', 'jetpack' ) ),
array(
'description' => __( 'Show your support for the Internet Defense League.', 'internetdefenseleague' ),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to use the jetpack textdomain everywhere in that file, so strings can be picked up and translated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this look about right in d340db8 ?

// When enabling campaigns other than 'none' or empty, change $no_current to false above.
$this->campaigns = array(
'' => __( 'All current and future campaigns', 'internetdefenseleague' ),
// 'nsa' => __( 'NSA Protest on July 4th, 2013', 'internetdefenseleague' ),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this doesn't apply anymore, you can remove it I think.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good catch, it looks like we can hide the Don't display a badge (just the campaign) option as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the badge options in 84411a5

array(
'description' => __( 'Show your support for the Internet Defense League.', 'internetdefenseleague' ),
)
);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest enabling Customizer's Selective Refresh, as mentioned here:
https://github.com/Automattic/jetpack/blob/master/docs/coding-guidelines.md#widgets

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The widget is very simple, so it appears that adding the flag is enough in 7b37c44. Let me know if I missed anything though.

echo $args['before_widget'];
echo '<p><a href="https://internetdefenseleague.org/"><img src="' . esc_url( 'https://internetdefenseleague.org/images/badges/final/' . $instance['badge'] . '.png' ) . '" alt="Member of The Internet Defense League" style="max-width: 100%; height: auto;" /></a></p>';
echo $args['after_widget'];
do_action( 'jetpack_stats_extra', 'widget_view', 'internet_defense_league' );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a docblock before the action? /** This action is already documented in modules/widgets/gravatar-profile.php */ should do.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in f357d56

$instance['badge'] = $this->defaults['badge'];
}
echo $args['before_widget'];
echo '<p><a href="https://internetdefenseleague.org/"><img src="' . esc_url( 'https://internetdefenseleague.org/images/badges/final/' . $instance['badge'] . '.png' ) . '" alt="Member of The Internet Defense League" style="max-width: 100%; height: auto;" /></a></p>';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about serving that image with Photon, thanks to jetpack_photon_url()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 70ca32a

screen shot 2017-04-21 at 3 54 26 pm

Should I also add translation strings here for the alt text?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it would be nice!

$this->campaign = $instance['campaign'];
$this->variant = $instance['variant'];
add_action( 'wp_footer', array( $this, 'footer_script' ) );
do_action( 'jetpack_stats_extra', 'widget_view', 'internet_defense_league' );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding the same action twice, how about placing it at the end of the widget output, so it's always triggered when the widget is in use, and always triggered only once?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be safe now with 84411a5. Previously I think it was possible for you to select none for a badge, and with no active campaigns this would bump stats even if nothing was rendered.

Updated in f357d56

}

public function footer_script() {
if ( ! isset( $this->campaigns[ $this->campaign ] ) )
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to add opening and closing brackets here, as per the WP coding standards:
https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#brace-style

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 1d9b30a

if ( ! isset( $this->campaigns[ $this->campaign ] ) )
$this->campaign = $this->defaults['campaign'];

if ( ! isset( $this->variants[ $this->variant ] ) )
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing brackets here as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 1d9b30a


**************************************************************************/

class Internet_Defense_League_Widget extends WP_Widget {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could prefix it with Jetpack_ to avoid any potential conflicts with other plugins or themes that may use the same class name?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Updated in 76a219a

@jeherve jeherve added [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. and removed [Status] Needs Review This PR is ready for review. labels Apr 21, 2017
@gwwar
Copy link
Copy Markdown
Contributor Author

gwwar commented Apr 21, 2017

Thanks for the detailed review @jeherve 👍 This is ready for another look when folks have time.

@gwwar gwwar added [Status] Needs Review This PR is ready for review. and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Apr 21, 2017
@jeherve jeherve modified the milestone: 4.9 Apr 24, 2017
parent::__construct(
'internet_defense_league_widget',
/** This filter is documented in modules/widgets/facebook-likebox.php */
apply_filters( 'jetpack_widget_name_widget', esc_html__( 'Internet Defense League', 'jetpack' ) ),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the correct filter name is jetpack_widget_name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch there 👍

);

if ( $this->no_current === false ) {
$this->badges[ 'none' ] = __( 'Don\'t display a badge (just the campaign)', 'jetpack' );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coding standards say whitespace only for variables in the bracket


**************************************************************************

Plugin Name: Internet Defense League
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this doc stuff. The format is kinda weird and it's not included in other widgets. Also not a plugin :)

@dereksmart
Copy link
Copy Markdown
Contributor

Looks great aside from my comments above!

@dereksmart dereksmart added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review This PR is ready for review. labels Apr 24, 2017
@gwwar
Copy link
Copy Markdown
Contributor Author

gwwar commented Apr 24, 2017

Thanks for the reviews @dereksmart @jeherve! Let me know if I need to tidy anything else

Copy link
Copy Markdown
Contributor

@eliorivero eliorivero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @gwwar! There are some changes that would be great to make before merging.

}

function jetpack_internet_defense_league_init() {
if ( Jetpack::is_active() ) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This widget doesn't require anything from wpcom so it doesn't need a connection. This condition can be removed.

// Hide first two form fields if no current campaigns.
if ( false === $this->no_current ) {
echo '<p><label>';
echo __( 'Which Internet Defense League campaign do you want to participate in?', 'jetpack' ) . '<br />';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and similar instances should use esc_html_e() (or _e() when necessary) instead of echo __()

$badge_url = esc_url( 'https://internetdefenseleague.org/images/badges/final/' . $instance['badge'] . '.png' );
$photon_badge_url = jetpack_photon_url( $badge_url );
echo $args['before_widget'];
echo '<p><a href="https://internetdefenseleague.org/"><img src="' . $photon_badge_url . '" alt="Member of The Internet Defense League" style="max-width: 100%; height: auto;" /></a></p>';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The string Member of The Internet Defense League must be translatable.

)
);

// When enabling campaigns other than 'none' or empty, change $no_current to false above.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the code associated to $no_current being false is never used since there's no way for user to change the property, which is always true, so maybe it should be removed.

Copy link
Copy Markdown
Contributor Author

@gwwar gwwar Apr 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this was dev note in case another campaign was to be added, so someone could modify this quickly. If we remove this, we should remove the campaigns array as well since there are no active campaigns to choose from.

/** This filter is documented in modules/widgets/facebook-likebox.php */
apply_filters( 'jetpack_widget_name', esc_html__( 'Internet Defense League', 'jetpack' ) ),
array(
'description' => __( 'Show your support for the Internet Defense League.', 'jetpack' ),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually use the safer esc_html__() instead of __().

@eliorivero eliorivero added [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Status] Needs Review This PR is ready for review. and removed [Status] Ready to Merge Go ahead, you can push that green button! labels Apr 24, 2017
@gwwar
Copy link
Copy Markdown
Contributor Author

gwwar commented Apr 24, 2017

@eliorivero updated in f3d2612

@gwwar gwwar removed the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Apr 24, 2017
@eliorivero
Copy link
Copy Markdown
Contributor

Fantastic! Merging now.

@eliorivero eliorivero merged commit 00ff093 into master Apr 24, 2017
@eliorivero eliorivero deleted the add/internet-defense-league-widget branch April 24, 2017 23:43
@eliorivero eliorivero removed the [Status] Needs Review This PR is ready for review. label Apr 24, 2017
jeherve added a commit that referenced this pull request Apr 24, 2017
eliorivero pushed a commit that referenced this pull request Apr 25, 2017
* Changelog: initial commit for 4.9 release.

* Changelog: add #6929

* Changelog: move old changelogs to changelog.txt

* Readme: restore deleted release post link.

The post is now live.

* Changelog: add #6853

* Changelog: add #6856

* Changelog: add #6857

* Changelog: add #6884

* Changelog: add #6885

* Changelog: add #6892

* Changelog: add #6894

* Changelog: add #6898

* Changelog: add #6899

* Changelog: add #6900

* Changelog: add #6909

* Changelog: add #6927

* Changelog: add #6947

* Chagelog: add #6958

* Changelog: add #6961

* Changelog: add #6963

* Changelog: add #6965

* Changelog: add #6986

* Changelog: add #7000

* Changelog: add #7013

* Changelog: add #7015

* Changelog: add #7019

* Changelog: add #7028

* Changelog: add #6998

* Changelog: add #6999

* Changelog: add #7044

* Changelog: add #6881

* Changelog: add #6922

* Changelog: add #6940

* Changelog: add #6962

* Changelog: add #6942

* Changelog: add #6959

* Changelog: add #7018

* Changelog: add #6948

* Changelog: add #6657

* Changelog: add #7030

* Changelog: add #7048

* Changelog: add #7031

* Changelog: add #6990

* Changelog: add #6957

* Changelog: add #7027
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants