diff options
Diffstat (limited to 'plugins/jetpack/modules/widgets')
36 files changed, 1254 insertions, 2280 deletions
diff --git a/plugins/jetpack/modules/widgets/authors.php b/plugins/jetpack/modules/widgets/authors.php index 402794fc..d56f2ec3 100644 --- a/plugins/jetpack/modules/widgets/authors.php +++ b/plugins/jetpack/modules/widgets/authors.php @@ -1,4 +1,4 @@ -<?php +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName /** * Disable direct access/execution to/of the widget code. */ @@ -17,6 +17,9 @@ if ( ! defined( 'ABSPATH' ) ) { * @since 4.5.0 */ class Jetpack_Widget_Authors extends WP_Widget { + /** + * Jetpack_Widget_Authors contructor. + */ public function __construct() { parent::__construct( 'authors', @@ -43,22 +46,32 @@ class Jetpack_Widget_Authors extends WP_Widget { * * @since 4.5.0 */ - function enqueue_style() { + public function enqueue_style() { wp_register_style( 'jetpack-authors-widget', plugins_url( 'authors/style.css', __FILE__ ), array(), '20161228' ); wp_enqueue_style( 'jetpack-authors-widget' ); } + /** + * Flush Authors widget cached data. + */ public static function flush_cache() { wp_cache_delete( 'widget_authors', 'widget' ); wp_cache_delete( 'widget_authors_ssl', 'widget' ); } + /** + * Echoes the widget content. + * + * @param array $args Display arguments. + * @param array $instance Widget settings for the instance. + */ public function widget( $args, $instance ) { $cache_bucket = is_ssl() ? 'widget_authors_ssl' : 'widget_authors'; - if ( '%BEG_OF_TITLE%' != $args['before_title'] ) { - if ( $output = wp_cache_get( $cache_bucket, 'widget' ) ) { - echo $output; + if ( '%BEG_OF_TITLE%' !== $args['before_title'] ) { + $output = wp_cache_get( $cache_bucket, 'widget' ); + if ( $output ) { + echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Cached widget display. return; } @@ -66,7 +79,8 @@ class Jetpack_Widget_Authors extends WP_Widget { } $instance = wp_parse_args( - $instance, array( + $instance, + array( 'title' => __( 'Authors', 'jetpack' ), 'all' => false, 'number' => 5, @@ -75,7 +89,7 @@ class Jetpack_Widget_Authors extends WP_Widget { ); $instance['number'] = min( 10, max( 0, (int) $instance['number'] ) ); - // We need to query at least one post to determine whether an author has written any posts or not + // We need to query at least one post to determine whether an author has written any posts or not. $query_number = max( $instance['number'], 1 ); /** @@ -106,14 +120,14 @@ class Jetpack_Widget_Authors extends WP_Widget { $get_author_params = apply_filters( 'jetpack_widget_authors_params', array( - 'who' => 'authors', - 'exclude' => (array) $excluded_authors, + 'capability' => array( 'edit_posts' ), + 'exclude' => (array) $excluded_authors, ) ); $authors = get_users( $get_author_params ); - echo $args['before_widget']; + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped /** This filter is documented in core/src/wp-includes/default-widgets.php */ $title = apply_filters( 'widget_title', $instance['title'] ); echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped @@ -149,9 +163,9 @@ class Jetpack_Widget_Authors extends WP_Widget { echo '<li>'; - // Display avatar and author name + // Display avatar and author name. if ( $r->have_posts() ) { - echo '<a href="' . get_author_posts_url( $author->ID ) . '">'; + echo '<a href="' . esc_url( get_author_posts_url( $author->ID ) ) . '">'; if ( $instance['avatar_size'] > 1 ) { echo ' ' . get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' '; @@ -167,7 +181,7 @@ class Jetpack_Widget_Authors extends WP_Widget { echo '<strong>' . esc_html( $author->display_name ) . '</strong>'; } - if ( 0 == $instance['number'] ) { + if ( 0 === (int) $instance['number'] ) { echo '</li>'; continue; } @@ -195,11 +209,11 @@ class Jetpack_Widget_Authors extends WP_Widget { } echo '</ul>'; - echo $args['after_widget']; + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped wp_reset_postdata(); - if ( '%BEG_OF_TITLE%' != $args['before_title'] ) { + if ( '%BEG_OF_TITLE%' !== $args['before_title'] ) { wp_cache_add( $cache_bucket, ob_get_flush(), 'widget' ); } @@ -207,9 +221,15 @@ class Jetpack_Widget_Authors extends WP_Widget { do_action( 'jetpack_stats_extra', 'widget_view', 'authors' ); } + /** + * Outputs the widget settings form. + * + * @param array $instance Current settings. + */ public function form( $instance ) { $instance = wp_parse_args( - $instance, array( + $instance, + array( 'title' => '', 'all' => false, 'avatar_size' => 48, @@ -220,27 +240,27 @@ class Jetpack_Widget_Authors extends WP_Widget { ?> <p> <label> - <?php _e( 'Title:', 'jetpack' ); ?> - <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> + <?php esc_html_e( 'Title:', 'jetpack' ); ?> + <input class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> </label> </p> <p> <label> - <input class="checkbox" type="checkbox" <?php checked( $instance['all'] ); ?> name="<?php echo $this->get_field_name( 'all' ); ?>" /> - <?php _e( 'Display all authors (including those who have not written any posts)', 'jetpack' ); ?> + <input class="checkbox" type="checkbox" <?php checked( $instance['all'] ); ?> name="<?php echo esc_attr( $this->get_field_name( 'all' ) ); ?>" /> + <?php esc_html_e( 'Display all authors (including those who have not written any posts)', 'jetpack' ); ?> </label> </p> <p> <label> - <?php _e( 'Number of posts to show for each author:', 'jetpack' ); ?> - <input style="width: 50px; text-align: center;" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo esc_attr( $instance['number'] ); ?>" /> - <?php _e( '(at most 10)', 'jetpack' ); ?> + <?php esc_html_e( 'Number of posts to show for each author:', 'jetpack' ); ?> + <input style="width: 50px; text-align: center;" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['number'] ); ?>" /> + <?php esc_html_e( '(at most 10)', 'jetpack' ); ?> </label> </p> <p> <label> - <?php _e( 'Avatar Size (px):', 'jetpack' ); ?> - <select name="<?php echo $this->get_field_name( 'avatar_size' ); ?>"> + <?php esc_html_e( 'Avatar Size (px):', 'jetpack' ); ?> + <select name="<?php echo esc_attr( $this->get_field_name( 'avatar_size' ) ); ?>"> <?php foreach ( array( '1' => __( 'No Avatars', 'jetpack' ), @@ -250,7 +270,7 @@ class Jetpack_Widget_Authors extends WP_Widget { '96' => '96x96', '128' => '128x128', ) as $value => $label ) { -?> + ?> <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $instance['avatar_size'] ); ?>><?php echo esc_html( $label ); ?></option> <?php } ?> </select> @@ -262,23 +282,26 @@ class Jetpack_Widget_Authors extends WP_Widget { /** * Updates the widget on save and flushes cache. * - * @param array $new_instance - * @param array $old_instance + * @param array $new_instance New widget instance data. + * @param array $old_instance Old widget instance data. * @return array */ - public function update( $new_instance, $old_instance ) { - $new_instance['title'] = strip_tags( $new_instance['title'] ); + public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + $new_instance['title'] = wp_strip_all_tags( $new_instance['title'] ); $new_instance['all'] = isset( $new_instance['all'] ); $new_instance['number'] = (int) $new_instance['number']; $new_instance['avatar_size'] = (int) $new_instance['avatar_size']; - Jetpack_Widget_Authors::flush_cache(); + self::flush_cache(); return $new_instance; } } add_action( 'widgets_init', 'jetpack_register_widget_authors' ); +/** + * Register the Authors widget. + */ function jetpack_register_widget_authors() { register_widget( 'Jetpack_Widget_Authors' ); -}; +} diff --git a/plugins/jetpack/modules/widgets/blog-stats.php b/plugins/jetpack/modules/widgets/blog-stats.php index 7265d114..5fb03e8b 100644 --- a/plugins/jetpack/modules/widgets/blog-stats.php +++ b/plugins/jetpack/modules/widgets/blog-stats.php @@ -1,4 +1,4 @@ -<?php +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileNam /** * Blog Stats Widget. * @@ -26,7 +26,7 @@ class Jetpack_Blog_Stats_Widget extends WP_Widget { /** * Constructor */ - function __construct() { + public function __construct() { $widget_ops = array( 'classname' => 'blog-stats', 'description' => esc_html__( 'Show a hit counter for your blog.', 'jetpack' ), @@ -85,7 +85,7 @@ class Jetpack_Blog_Stats_Widget extends WP_Widget { * * @return void */ - function form( $instance ) { + public function form( $instance ) { $instance = wp_parse_args( $instance, $this->defaults() ); ?> @@ -112,7 +112,7 @@ class Jetpack_Blog_Stats_Widget extends WP_Widget { * * @return array Updated safe values to be saved. */ - function update( $new_instance, $old_instance ) { + public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $instance = array(); $instance['title'] = wp_kses( $new_instance['title'], array() ); $instance['hits'] = wp_kses( $new_instance['hits'], array() ); @@ -128,13 +128,13 @@ class Jetpack_Blog_Stats_Widget extends WP_Widget { * @param array $args Widget arguments. * @param array $instance Saved values from database. */ - function widget( $args, $instance ) { + public function widget( $args, $instance ) { $instance = wp_parse_args( $instance, $this->defaults() ); /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ $title = apply_filters( 'widget_title', $instance['title'] ); - echo $args['before_widget']; + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped if ( ! empty( $title ) ) { echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped @@ -155,7 +155,7 @@ class Jetpack_Blog_Stats_Widget extends WP_Widget { esc_html_e( 'There was an issue retrieving stats. Please try again later.', 'jetpack' ); } - echo $args['after_widget']; + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped /** This action is already documented in modules/widgets/gravatar-profile.php */ do_action( 'jetpack_stats_extra', 'widget_view', 'blog_stats' ); diff --git a/plugins/jetpack/modules/widgets/eu-cookie-law.php b/plugins/jetpack/modules/widgets/class-jetpack-eu-cookie-law-widget.php index fffc9724..3207c85f 100644 --- a/plugins/jetpack/modules/widgets/eu-cookie-law.php +++ b/plugins/jetpack/modules/widgets/class-jetpack-eu-cookie-law-widget.php @@ -1,4 +1,9 @@ <?php +/** + * Main class file for EU Cookie Law Widget. + * + * @package automattic/jetpack + */ use Automattic\Jetpack\Assets; @@ -77,7 +82,7 @@ if ( ! class_exists( 'Jetpack_EU_Cookie_Law_Widget' ) ) { /** * Constructor. */ - function __construct() { + public function __construct() { parent::__construct( 'eu_cookie_law_widget', /** This filter is documented in modules/widgets/facebook-likebox.php */ @@ -97,7 +102,7 @@ if ( ! class_exists( 'Jetpack_EU_Cookie_Law_Widget' ) ) { /** * Enqueue scripts and styles. */ - function enqueue_frontend_scripts() { + public function enqueue_frontend_scripts() { wp_enqueue_style( 'eu-cookie-law-style', plugins_url( 'eu-cookie-law/style.css', __FILE__ ), array(), JETPACK__VERSION ); if ( ! class_exists( 'Jetpack_AMP_Support' ) || ! Jetpack_AMP_Support::is_amp_request() ) { @@ -160,7 +165,7 @@ if ( ! class_exists( 'Jetpack_EU_Cookie_Law_Widget' ) ) { $instance = wp_parse_args( $instance, $this->defaults() ); if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { - require dirname( __FILE__ ) . '/eu-cookie-law/widget-amp.php'; + require __DIR__ . '/eu-cookie-law/widget-amp.php'; return; } @@ -196,9 +201,9 @@ if ( ! class_exists( 'Jetpack_EU_Cookie_Law_Widget' ) ) { return; } - echo $args['before_widget']; + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped require_once __DIR__ . '/eu-cookie-law/widget.php'; - echo $args['after_widget']; + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped /** This action is already documented in modules/widgets/gravatar-profile.php */ do_action( 'jetpack_stats_extra', 'widget_view', 'eu_cookie_law' ); @@ -222,20 +227,21 @@ if ( ! class_exists( 'Jetpack_EU_Cookie_Law_Widget' ) ) { 'modules/widgets/eu-cookie-law/eu-cookie-law-admin.js' ), array( 'jquery' ), - 20180417 + 20180417, + false ); - require( dirname( __FILE__ ) . '/eu-cookie-law/form.php' ); + require __DIR__ . '/eu-cookie-law/form.php'; } /** * Sanitize widget form values as they are saved. * - * @param array $new_instance Values just sent to be saved. - * @param array $old_instance Previously saved values from database. + * @param array $new_instance Values just sent to be saved. + * @param array $old_instance Previously saved values from database. * @return array Updated safe values to be saved. */ - public function update( $new_instance, $old_instance ) { + public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $instance = array(); $defaults = $this->defaults(); @@ -285,7 +291,7 @@ if ( ! class_exists( 'Jetpack_EU_Cookie_Law_Widget' ) ) { $instance['policy-link-text'] = trim( mb_substr( wp_kses( $new_instance['policy-link-text'], array() ), 0, 100 ) ); } - if ( empty( $instance['policy-link-text'] ) || $instance['policy-link-text'] == $defaults['policy-link-text'] ) { + if ( empty( $instance['policy-link-text'] ) || $instance['policy-link-text'] === $defaults['policy-link-text'] ) { unset( $instance['policy-link-text'] ); } @@ -293,12 +299,12 @@ if ( ! class_exists( 'Jetpack_EU_Cookie_Law_Widget' ) ) { $instance['button'] = trim( mb_substr( wp_kses( $new_instance['button'], array() ), 0, 100 ) ); } - if ( empty( $instance['button'] ) || $instance['button'] == $defaults['button'] ) { + if ( empty( $instance['button'] ) || $instance['button'] === $defaults['button'] ) { unset( $instance['button'] ); } // Show the banner again if a setting has been changed. - setcookie( self::$cookie_name, '', time() - 86400, '/' ); + setcookie( self::$cookie_name, '', time() - 86400, '/', COOKIE_DOMAIN, is_ssl(), false ); // phpcs:ignore Jetpack.Functions.SetCookie -- Fine to have accessible. return $instance; } @@ -311,19 +317,21 @@ if ( ! class_exists( 'Jetpack_EU_Cookie_Law_Widget' ) ) { * * @return string $value if pass the check or first value from allowed values. */ - function filter_value( $value, $allowed = array() ) { + public function filter_value( $value, $allowed = array() ) { $allowed = (array) $allowed; - if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) { + if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed, true ) ) ) { $value = $allowed[0]; } return $value; } } - // Register Jetpack_EU_Cookie_Law_Widget widget. + /** + * Register Jetpack_EU_Cookie_Law_Widget widget. + */ function jetpack_register_eu_cookie_law_widget() { register_widget( 'Jetpack_EU_Cookie_Law_Widget' ); - }; + } add_action( 'widgets_init', 'jetpack_register_eu_cookie_law_widget' ); } diff --git a/plugins/jetpack/modules/widgets/contact-info.php b/plugins/jetpack/modules/widgets/contact-info.php index de9afc9d..4f53ac18 100644 --- a/plugins/jetpack/modules/widgets/contact-info.php +++ b/plugins/jetpack/modules/widgets/contact-info.php @@ -483,7 +483,7 @@ if ( ! class_exists( 'Jetpack_Contact_Info_Widget' ) ) { public function ajax_check_api_key() { if ( isset( $_POST['apikey'] ) ) { if ( check_ajax_referer( 'customize_contact_info_api_key' ) && current_user_can( 'customize' ) ) { - $apikey = wp_kses( $_POST['apikey'], array() ); + $apikey = wp_kses( wp_unslash( $_POST['apikey'] ), array() ); $default_instance = $this->defaults(); $default_instance['apikey'] = $apikey; wp_send_json( array( 'result' => esc_html( $this->has_good_map( $default_instance ) ) ) ); diff --git a/plugins/jetpack/modules/widgets/eu-cookie-law/form.php b/plugins/jetpack/modules/widgets/eu-cookie-law/form.php index d4f3bad2..7eb487e8 100644 --- a/plugins/jetpack/modules/widgets/eu-cookie-law/form.php +++ b/plugins/jetpack/modules/widgets/eu-cookie-law/form.php @@ -1,7 +1,13 @@ <?php +/** + * EU Cookie Law Widget form. + * + * @package automattic/jetpack + */ use Automattic\Jetpack\Redirect; +// phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable ?> <p> <strong> @@ -84,10 +90,21 @@ use Automattic\Jetpack\Redirect; <span class="notice notice-warning custom-policy" style="display: none;"> <span style="display: block; margin: .5em 0;"> <strong><?php esc_html_e( 'Caution:', 'jetpack' ); ?></strong> - <?php echo sprintf( - __( 'For GDPR compliance, please make sure your policy contains <a href="%s" target="_blank">privacy information relating to Jetpack Ads</a>.', 'jetpack' ), + <?php + echo sprintf( + wp_kses( + /* Translators: %s is the URL to a Jetpack support article. */ + __( 'For GDPR compliance, please make sure your policy contains <a href="%s" target="_blank">privacy information relating to WordAds</a>.', 'jetpack' ), + array( + 'a' => array( + 'href' => array(), + 'target' => array(), + ), + ) + ), esc_url( Redirect::get_url( 'jetpack-support-ads', array( 'anchor' => 'privacy' ) ) ) - ); ?> + ); + ?> </span> </span> <?php endif; ?> @@ -102,7 +119,7 @@ use Automattic\Jetpack\Redirect; <label> <input class="widefat" - name="<?php echo $this->get_field_name( 'policy-link-text' ); ?>" + name="<?php echo esc_attr( $this->get_field_name( 'policy-link-text' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['policy-link-text'] ); ?>" /> @@ -118,7 +135,7 @@ use Automattic\Jetpack\Redirect; <label> <input class="widefat" - name="<?php echo $this->get_field_name( 'button' ); ?>" + name="<?php echo esc_attr( $this->get_field_name( 'button' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['button'] ); ?>" /> @@ -129,7 +146,7 @@ use Automattic\Jetpack\Redirect; <p> <strong> - <?php _ex( 'Capture consent & hide the banner', 'action', 'jetpack' ); ?> + <?php esc_html_x( 'Capture consent & hide the banner', 'action', 'jetpack' ); ?> </strong> <ul> <li> @@ -181,7 +198,7 @@ use Automattic\Jetpack\Redirect; <?php if ( Jetpack::is_module_active( 'wordads' ) ) : ?> <span class="notice notice-warning" style="display: block;"> <span style="display: block; margin: .5em 0;"> - <?php esc_html_e( 'Visitors must provide consent by clicking the dismiss button when Jetpack Ads is turned on.', 'jetpack' ); ?> + <?php esc_html_e( 'Visitors must provide consent by clicking the dismiss button when WordAds is turned on.', 'jetpack' ); ?> </span> </span> <?php endif; ?> @@ -191,7 +208,7 @@ use Automattic\Jetpack\Redirect; <p> <strong> - <?php _ex( 'Consent expires after', 'action', 'jetpack' ); ?> + <?php esc_html_x( 'Consent expires after', 'action', 'jetpack' ); ?> </strong> <ul> <li> @@ -212,7 +229,7 @@ use Automattic\Jetpack\Redirect; <p> <strong> - <?php _e( 'Color scheme', 'jetpack' ); ?> + <?php esc_html_e( 'Color scheme', 'jetpack' ); ?> </strong> <ul> <li> @@ -244,7 +261,7 @@ use Automattic\Jetpack\Redirect; <p> <strong> - <?php _e( 'Position', 'jetpack' ); ?> + <?php esc_html_e( 'Position', 'jetpack' ); ?> </strong> <ul> <li> diff --git a/plugins/jetpack/modules/widgets/eu-cookie-law/widget.php b/plugins/jetpack/modules/widgets/eu-cookie-law/widget.php index ef540452..c1b3e328 100644 --- a/plugins/jetpack/modules/widgets/eu-cookie-law/widget.php +++ b/plugins/jetpack/modules/widgets/eu-cookie-law/widget.php @@ -7,7 +7,7 @@ ?> <div - class="<?php echo implode( ' ', $classes ); ?>" + class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>" data-hide-timeout="<?php echo (int) $instance['hide-timeout']; ?>" data-consent-expiration="<?php echo (int) $instance['consent-expiration']; ?>" id="eu-cookie-law" @@ -16,11 +16,13 @@ <input type="submit" value="<?php echo esc_attr( $instance['button'] ); ?>" class="accept" /> </form> - <?php if ( 'default' == $instance['text'] || empty( $instance['customtext'] ) ) { - echo nl2br( $instance['default-text'] ); + <?php + if ( 'default' === $instance['text'] || empty( $instance['customtext'] ) ) { + echo nl2br( esc_html( $instance['default-text'] ) ); } else { echo nl2br( esc_html( $instance['customtext'] ) ); - } ?> + } + ?> <?php $is_default_policy = 'default' === $instance['policy-url'] || empty( $instance['custom-policy-url'] ); diff --git a/plugins/jetpack/modules/widgets/facebook-likebox.php b/plugins/jetpack/modules/widgets/facebook-likebox.php index c7e38e64..a9466c42 100644 --- a/plugins/jetpack/modules/widgets/facebook-likebox.php +++ b/plugins/jetpack/modules/widgets/facebook-likebox.php @@ -1,10 +1,9 @@ -<?php +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName +add_action( 'widgets_init', 'jetpack_facebook_likebox_init' ); /** * Register the widget for use in Appearance -> Widgets */ -add_action( 'widgets_init', 'jetpack_facebook_likebox_init' ); - function jetpack_facebook_likebox_init() { register_widget( 'WPCOM_Widget_Facebook_LikeBox' ); } @@ -15,15 +14,52 @@ function jetpack_facebook_likebox_init() { * https://developers.facebook.com/docs/plugins/page-plugin */ class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { - + /** + * Default height. + * + * @var int + */ private $default_height = 580; - private $default_width = 340; - private $max_width = 500; - private $min_width = 180; - private $max_height = 9999; - private $min_height = 130; - function __construct() { + /** + * Default width. + * + * @var int + */ + private $default_width = 340; + + /** + * Max width. + * + * @var int + */ + private $max_width = 500; + + /** + * Min width. + * + * @var int + */ + private $min_width = 180; + + /** + * Max height. + * + * @var int + */ + private $max_height = 9999; + + /** + * Min height/ + * + * @var int + */ + private $min_height = 130; + + /** + * WPCOM_Widget_Facebook_LikeBox constructor. + */ + public function __construct() { parent::__construct( 'facebook-likebox', /** @@ -53,7 +89,12 @@ class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { */ public function enqueue_scripts() { wp_enqueue_script( 'jetpack-facebook-embed' ); - wp_enqueue_style( 'jetpack_facebook_likebox', plugins_url( 'facebook-likebox/style.css', __FILE__ ) ); + wp_enqueue_style( + 'jetpack_facebook_likebox', + plugins_url( 'facebook-likebox/style.css', __FILE__ ), + array(), + JETPACK__VERSION + ); wp_style_add_data( 'jetpack_facebook_likebox', 'jetpack-inline', true ); } @@ -158,14 +199,22 @@ class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { do_action( 'jetpack_stats_extra', 'widget_view', 'facebook-likebox' ); } - function update( $new_instance, $old_instance ) { + /** + * Update widget. + * + * @see WP_Widget::update() + * + * @param array $new_instance New widget instance data. + * @param array $old_instance Old widget instance data. + */ + public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $instance = array(); - $instance['title'] = trim( strip_tags( stripslashes( $new_instance['title'] ) ) ); + $instance['title'] = trim( wp_strip_all_tags( stripslashes( $new_instance['title'] ) ) ); // Set up widget values. $instance['like_args'] = array( - 'href' => trim( strip_tags( stripslashes( $new_instance['href'] ) ) ), + 'href' => trim( wp_strip_all_tags( stripslashes( $new_instance['href'] ) ) ), 'width' => (int) $new_instance['width'], 'height' => (int) $new_instance['height'], 'show_faces' => isset( $new_instance['show_faces'] ), @@ -182,9 +231,15 @@ class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { return $instance; } - function form( $instance ) { + /** + * Outputs the widget settings form. + * + * @param array $instance Current settings. + */ + public function form( $instance ) { $instance = wp_parse_args( - (array) $instance, array( + (array) $instance, + array( 'title' => '', 'like_args' => $this->get_default_args(), ) @@ -212,7 +267,25 @@ class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { <label for="<?php echo esc_attr( $this->get_field_id( 'width' ) ); ?>"> <?php esc_html_e( 'Width in pixels', 'jetpack' ); ?> <input type="number" class="smalltext" min="<?php echo esc_attr( $this->min_width ); ?>" max="<?php echo esc_attr( $this->max_width ); ?>" maxlength="3" name="<?php echo esc_attr( $this->get_field_name( 'width' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'width' ) ); ?>" value="<?php echo esc_attr( $like_args['width'] ); ?>" style="text-align: center;" /> - <small><?php echo sprintf( __( 'Minimum: %s', 'jetpack' ), $this->min_width ); ?> / <?php echo sprintf( __( 'Maximum: %s', 'jetpack' ), $this->max_width ); ?></small> + <small> + <?php + echo esc_html( + sprintf( + /* translators: %s is the minimum pixel width */ + __( 'Minimum: %s', 'jetpack' ), + $this->min_width + ) + ); + echo ' / '; + echo esc_html( + sprintf( + /* translators: %s is the maximum pixel width */ + __( 'Maximum: %s', 'jetpack' ), + $this->max_width + ) + ); + ?> + </small> </label> </p> @@ -220,7 +293,25 @@ class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { <label for="<?php echo esc_attr( $this->get_field_id( 'height' ) ); ?>"> <?php esc_html_e( 'Height in pixels', 'jetpack' ); ?> <input type="number" class="smalltext" min="<?php echo esc_attr( $this->min_height ); ?>" max="<?php echo esc_attr( $this->max_height ); ?>" maxlength="3" name="<?php echo esc_attr( $this->get_field_name( 'height' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'height' ) ); ?>" value="<?php echo esc_attr( $like_args['height'] ); ?>" style="text-align: center;" /> - <small><?php echo sprintf( __( 'Minimum: %s', 'jetpack' ), $this->min_height ); ?> / <?php echo sprintf( __( 'Maximum: %s', 'jetpack' ), $this->max_height ); ?></small> + <small> + <?php + echo esc_html( + sprintf( + /* translators: %s is the minimum pixel height */ + __( 'Minimum: %s', 'jetpack' ), + $this->min_height + ) + ); + echo ' / '; + echo esc_html( + sprintf( + /* translators: %s is the maximum pixel height */ + __( 'Maximum: %s', 'jetpack' ), + $this->max_height + ) + ); + ?> + </small> </label> </p> @@ -261,7 +352,10 @@ class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { <?php } - function get_default_args() { + /** + * Facebook Likebox default options. + */ + public function get_default_args() { $defaults = array( 'href' => '', 'width' => $this->default_width, @@ -284,10 +378,15 @@ class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { return apply_filters( 'jetpack_facebook_likebox_defaults', $defaults ); } - function normalize_facebook_args( $args ) { + /** + * Normalize the Facebook Likebox options. + * + * @param array $args Array of arguments. + */ + public function normalize_facebook_args( $args ) { $args = wp_parse_args( (array) $args, $this->get_default_args() ); - // Validate the Facebook Page URL + // Validate the Facebook Page URL. if ( $this->is_valid_facebook_url( $args['href'] ) ) { $temp = explode( '?', $args['href'] ); $args['href'] = str_replace( array( 'http://facebook.com', 'https://facebook.com' ), array( 'http://www.facebook.com', 'https://www.facebook.com' ), $temp[0] ); @@ -295,8 +394,8 @@ class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { $args['href'] = ''; } - $args['width'] = $this->normalize_int_value( (int) $args['width'], $this->default_width, $this->max_width, $this->min_width ); - $args['height'] = $this->normalize_int_value( (int) $args['height'], $this->default_height, $this->max_height, $this->min_height ); + $args['width'] = $this->normalize_int_value( (int) $args['width'], $this->max_width, $this->min_width ); + $args['height'] = $this->normalize_int_value( (int) $args['height'], $this->max_height, $this->min_height ); $args['show_faces'] = (bool) $args['show_faces']; $args['stream'] = (bool) $args['stream']; $args['cover'] = (bool) $args['cover']; @@ -305,7 +404,7 @@ class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { // The height used to be dependent on other widget settings // If the user changes those settings but doesn't customize the height, // let's intelligently assign a new height. - if ( in_array( $args['height'], array( 580, 110, 432 ) ) ) { + if ( in_array( $args['height'], array( 580, 110, 432 ), true ) ) { if ( $args['show_faces'] && $args['stream'] ) { $args['height'] = 580; } elseif ( ! $args['show_faces'] && ! $args['stream'] ) { @@ -318,11 +417,25 @@ class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { return $args; } - function is_valid_facebook_url( $url ) { + /** + * Check if URL is a valid Facebook Page URL. + * + * @param string $url URL to check. + */ + public function is_valid_facebook_url( $url ) { return ( false !== strpos( $url, 'facebook.com' ) ) ? true : false; } - function normalize_int_value( $value, $default = 0, $max = 0, $min = 0 ) { + /** + * Normalize an integer value within a given range. + * + * @param int $value Value to normalize. + * @param int $max Maximum value. + * @param int $min Minimum value. + * + * @return int Normalized value. + */ + public function normalize_int_value( $value, $max = 0, $min = 0 ) { $value = (int) $value; if ( $value > $max ) { @@ -333,14 +446,4 @@ class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { return (int) $value; } - - function normalize_text_value( $value, $default = '', $allowed = array() ) { - $allowed = (array) $allowed; - - if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) { - $value = $default; - } - - return $value; - } } diff --git a/plugins/jetpack/modules/widgets/flickr.php b/plugins/jetpack/modules/widgets/flickr.php index 9fb80962..36b524da 100644 --- a/plugins/jetpack/modules/widgets/flickr.php +++ b/plugins/jetpack/modules/widgets/flickr.php @@ -1,4 +1,4 @@ -<?php +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName /** * Disable direct access/execution to/of the widget code. */ @@ -16,7 +16,7 @@ if ( ! class_exists( 'Jetpack_Flickr_Widget' ) ) { /** * Constructor. */ - function __construct() { + public function __construct() { parent::__construct( 'flickr', /** This filter is documented in modules/widgets/facebook-likebox.php */ @@ -36,7 +36,7 @@ if ( ! class_exists( 'Jetpack_Flickr_Widget' ) ) { /** * Enqueue style. */ - function enqueue_style() { + public function enqueue_style() { wp_enqueue_style( 'flickr-widget-style', plugins_url( 'flickr/style.css', __FILE__ ), array(), '20170405' ); } @@ -66,10 +66,7 @@ if ( ! class_exists( 'Jetpack_Flickr_Widget' ) ) { public function widget( $args, $instance ) { $instance = wp_parse_args( $instance, $this->defaults() ); - $image_size_string = 'small' == $instance['flickr_image_size'] ? '_m.jpg' : '_t.jpg'; - if ( ! empty( $instance['flickr_rss_url'] ) ) { - /* * Parse the URL, and rebuild a URL that's sure to display images. * Some Flickr Feeds do not display images by default. @@ -105,7 +102,7 @@ if ( ! class_exists( 'Jetpack_Flickr_Widget' ) ) { ); } } - } // End if(). + } // Still no RSS feed URL? Get a default feed from Flickr to grab interesting photos. if ( empty( $rss_url ) ) { @@ -143,10 +140,10 @@ if ( ! class_exists( 'Jetpack_Flickr_Widget' ) ) { $photos = Jetpack_Photon::filter_the_content( $photos ); } - $flickr_home = $rss->get_link(); + $flickr_home = $rss->get_link(); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Used in flickr/widget.php template file. } - echo $args['before_widget']; + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped if ( empty( $photos ) ) { if ( current_user_can( 'edit_theme_options' ) ) { printf( @@ -157,9 +154,9 @@ if ( ! class_exists( 'Jetpack_Flickr_Widget' ) ) { } } else { echo $args['before_title'] . $instance['title'] . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - require( dirname( __FILE__ ) . '/flickr/widget.php' ); + require __DIR__ . '/flickr/widget.php'; } - echo $args['after_widget']; + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped /** This action is already documented in modules/widgets/gravatar-profile.php */ do_action( 'jetpack_stats_extra', 'widget_view', 'flickr' ); } @@ -171,7 +168,7 @@ if ( ! class_exists( 'Jetpack_Flickr_Widget' ) ) { */ public function form( $instance ) { $instance = wp_parse_args( $instance, $this->defaults() ); - require( dirname( __FILE__ ) . '/flickr/form.php' ); + require __DIR__ . '/flickr/form.php'; } /** @@ -181,9 +178,8 @@ if ( ! class_exists( 'Jetpack_Flickr_Widget' ) ) { * @param array $old_instance Previously saved values from database. * @return array Updated safe values to be saved. */ - public function update( $new_instance, $old_instance ) { + public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $instance = array(); - $defaults = $this->defaults(); if ( isset( $new_instance['title'] ) ) { $instance['title'] = wp_kses( $new_instance['title'], array() ); @@ -199,7 +195,7 @@ if ( ! class_exists( 'Jetpack_Flickr_Widget' ) ) { if ( isset( $new_instance['flickr_image_size'] ) && - in_array( $new_instance['flickr_image_size'], array( 'thumbnail', 'small', 'large' ) ) + in_array( $new_instance['flickr_image_size'], array( 'thumbnail', 'small', 'large' ), true ) ) { $instance['flickr_image_size'] = $new_instance['flickr_image_size']; } else { @@ -218,7 +214,9 @@ if ( ! class_exists( 'Jetpack_Flickr_Widget' ) ) { } } - // Register Jetpack_Flickr_Widget widget. + /** + * Register Jetpack_Flickr_Widget widget. + */ function jetpack_register_flickr_widget() { register_widget( 'Jetpack_Flickr_Widget' ); } diff --git a/plugins/jetpack/modules/widgets/flickr/form.php b/plugins/jetpack/modules/widgets/flickr/form.php index 0dfc5c9a..a1bd08dd 100644 --- a/plugins/jetpack/modules/widgets/flickr/form.php +++ b/plugins/jetpack/modules/widgets/flickr/form.php @@ -1,3 +1,12 @@ +<?php +/** + * Jetpack_Flickr_Widget settings form output. + * + * @package automattic/jetpack + */ + +//phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable +?> <p> <label> <?php esc_html_e( 'Title:', 'jetpack' ); ?> @@ -28,10 +37,21 @@ </p> <p> <small> - <?php printf( - __( 'Leave the Flickr RSS URL field blank to display <a target="_blank" href="%s">interesting</a> Flickr photos.', 'jetpack' ), + <?php + echo sprintf( + wp_kses( + /* Translators: %s is the URL to an example Flickr RSS feed. */ + __( 'Leave the Flickr RSS URL field blank to display <a target="_blank" href="%s">interesting</a> Flickr photos.', 'jetpack' ), + array( + 'a' => array( + 'href' => array(), + 'target' => array(), + ), + ) + ), 'https://www.flickr.com/explore/interesting' - ); ?> + ); + ?> </small> </p> @@ -43,9 +63,9 @@ <?php for ( $i = 1; $i <= 10; ++$i ) { ?> <option <?php selected( $instance['items'], $i ); ?> - value="<?php echo $i; ?>" + value="<?php echo esc_attr( $i ); ?>" > - <?php echo $i; ?> + <?php echo esc_html( $i ); ?> </option> <?php } ?> </select> diff --git a/plugins/jetpack/modules/widgets/flickr/widget.php b/plugins/jetpack/modules/widgets/flickr/widget.php index 0c45f3f0..e3bd118b 100644 --- a/plugins/jetpack/modules/widgets/flickr/widget.php +++ b/plugins/jetpack/modules/widgets/flickr/widget.php @@ -1,7 +1,16 @@ +<?php +/** + * Jetpack_Flickr_Widget frontend widget output. + * + * @package automattic/jetpack + */ + +//phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable +?> <!-- Start of Flickr Widget --> <div class="flickr-wrapper flickr-size-<?php echo esc_attr( $instance['flickr_image_size'] ); ?>"> <div class="flickr-images"> - <?php echo $photos; ?> + <?php echo $photos; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaping handled in Jetpack_Flickr_Widget class. ?> </div> <?php if ( isset( $flickr_home ) ) { ?> diff --git a/plugins/jetpack/modules/widgets/gallery.php b/plugins/jetpack/modules/widgets/gallery.php index 65c3a60c..44e2ecbf 100644 --- a/plugins/jetpack/modules/widgets/gallery.php +++ b/plugins/jetpack/modules/widgets/gallery.php @@ -1,21 +1,30 @@ -<?php - -/* -Plugin Name: Gallery -Description: Gallery widget -Author: Automattic Inc. -Version: 1.0 -Author URI: https://automattic.com -*/ +<?php // phpcs:ignore eWordPress.Files.FileName.InvalidClassFileName +/** + * Module Name: Gallery widget + * + * @package automattic/jetpack + */ use Automattic\Jetpack\Assets; +/** + * Jetpack_Gallery_Widget main class. + */ class Jetpack_Gallery_Widget extends WP_Widget { const THUMB_SIZE = 45; const DEFAULT_WIDTH = 265; - protected $_instance_width; + /** + * The width of the gallery widget. + * May be customized by the 'gallery_widget_content_width' filter. + * + * @var int + */ + protected $instance_width; + /** + * Jetpack_Gallery_Widget constructor. + */ public function __construct() { $widget_ops = array( 'classname' => 'widget-gallery', @@ -125,7 +134,7 @@ class Jetpack_Gallery_Widget extends WP_Widget { * @param string $args Display arguments including before_title, after_title, before_widget, and after_widget. * @param array $instance The settings for the particular instance of the widget. */ - $this->_instance_width = apply_filters( 'gallery_widget_content_width', self::DEFAULT_WIDTH, $args, $instance ); + $this->instance_width = apply_filters( 'gallery_widget_content_width', self::DEFAULT_WIDTH, $args, $instance ); // Register a filter to modify the tiled_gallery_content_width, so Jetpack_Tiled_Gallery // can appropriately size the tiles. @@ -135,8 +144,8 @@ class Jetpack_Gallery_Widget extends WP_Widget { echo $this->$method( $args, $instance ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } - // Remove the stored $_instance_width, as it is no longer needed. - $this->_instance_width = null; + // Remove the stored $instance_width, as it is no longer needed. + $this->instance_width = null; // Remove the filter, so any Jetpack_Tiled_Gallery in a post is not affected. remove_filter( 'tiled_gallery_content_width', array( $this, 'tiled_gallery_content_width' ) ); @@ -152,13 +161,13 @@ class Jetpack_Gallery_Widget extends WP_Widget { /** * Fetch the images attached to the gallery Widget * - * @param array $instance The Widget instance for which you'd like attachments + * @param array $instance The Widget instance for which you'd like attachments. * @return array Array of attachment objects for the Widget in $instance */ public function get_attachments( $instance ) { $ids = explode( ',', $instance['ids'] ); - if ( isset( $instance['random'] ) && 'on' == $instance['random'] ) { + if ( isset( $instance['random'] ) && 'on' === $instance['random'] ) { shuffle( $ids ); } @@ -184,7 +193,7 @@ class Jetpack_Gallery_Widget extends WP_Widget { * Generate HTML for a rectangular, tiled Widget * * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. - * @param array $instance The Widget instance to generate HTML for + * @param array $instance The Widget instance to generate HTML for. * @return string String of HTML representing a rectangular gallery */ public function rectangular_widget( $args, $instance ) { @@ -203,7 +212,7 @@ class Jetpack_Gallery_Widget extends WP_Widget { * Generate HTML for a square (grid style) Widget * * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. - * @param array $instance The Widget instance to generate HTML for + * @param array $instance The Widget instance to generate HTML for. * @return string String of HTML representing a square gallery */ public function square_widget( $args, $instance ) { @@ -222,7 +231,7 @@ class Jetpack_Gallery_Widget extends WP_Widget { * Generate HTML for a circular (grid style) Widget * * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. - * @param array $instance The Widget instance to generate HTML for + * @param array $instance The Widget instance to generate HTML for. * @return string String of HTML representing a circular gallery */ public function circle_widget( $args, $instance ) { @@ -240,14 +249,16 @@ class Jetpack_Gallery_Widget extends WP_Widget { /** * Generate HTML for a slideshow Widget * + * @todo Is slideshow_widget() still used? + * * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. - * @param array $instance The Widget instance to generate HTML for + * @param array $instance The Widget instance to generate HTML for. * @return string String of HTML representing a slideshow gallery */ public function slideshow_widget( $args, $instance ) { global $content_width; - require_once plugin_dir_path( realpath( dirname( __FILE__ ) . '/../shortcodes/slideshow.php' ) ) . 'slideshow.php'; + require_once plugin_dir_path( realpath( __DIR__ . '/../shortcodes/slideshow.php' ) ) . 'slideshow.php'; if ( ! class_exists( 'Jetpack_Slideshow_Shortcode' ) ) { return; @@ -267,9 +278,9 @@ class Jetpack_Gallery_Widget extends WP_Widget { foreach ( $instance['attachments'] as $attachment ) { $attachment_image_src = wp_get_attachment_image_src( $attachment->ID, 'full' ); - $attachment_image_src = jetpack_photon_url( $attachment_image_src[0], array( 'w' => $this->_instance_width ) ); // [url, width, height] + $attachment_image_src = jetpack_photon_url( $attachment_image_src[0], array( 'w' => $this->instance_width ) ); /** [url, width, height] */ - $caption = wptexturize( strip_tags( $attachment->post_excerpt ) ); + $caption = wptexturize( wp_strip_all_tags( $attachment->post_excerpt ) ); $gallery[] = (object) array( 'src' => (string) esc_url_raw( $attachment_image_src ), @@ -286,7 +297,7 @@ class Jetpack_Gallery_Widget extends WP_Widget { } $color = Jetpack_Options::get_option( 'slideshow_background_color', 'black' ); - $autostart = isset( $attr['autostart'] ) ? $attr['autostart'] : true; + $autostart = isset( $attr['autostart'] ) ? $attr['autostart'] : true; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable -- Todo: should read off the $instance? Also not sure if slideshow_widget() is used still. $js_attr = array( 'gallery' => $gallery, @@ -304,29 +315,38 @@ class Jetpack_Gallery_Widget extends WP_Widget { } /** - * tiled_gallery_content_width filter - * * Used to adjust the content width of Jetpack_Tiled_Gallery's in sidebars * - * $this->_instance_width is filtered in widget() and this filter is added then removed in widget() + * $this->instance_width is filtered in widget() and this filter is added then removed in widget() * - * @param int $width int The original width value * @return int The filtered width */ - public function tiled_gallery_content_width( $width ) { - return $this->_instance_width; + public function tiled_gallery_content_width() { + return $this->instance_width; } + /** + * Outputs the widget settings form. + * + * @param array $instance Current settings. + */ public function form( $instance ) { $defaults = $this->defaults(); - $allowed_values = $this->allowed_values(); + $allowed_values = $this->allowed_values(); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Used in included form template. $instance = wp_parse_args( (array) $instance, $defaults ); - include dirname( __FILE__ ) . '/gallery/templates/form.php'; + include __DIR__ . '/gallery/templates/form.php'; } - public function update( $new_instance, $old_instance ) { + /** + * Save the widget options. + * + * @param array $new_instance The new instance options. + * @param array $old_instance The old instance options. + * @return array The saved options. + */ + public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $instance = $this->sanitize( $new_instance ); return $instance; @@ -338,7 +358,7 @@ class Jetpack_Gallery_Widget extends WP_Widget { * * Helps keep things nice and secure by listing only allowed values. * - * @param array $instance The Widget instance to sanitize values for + * @param array $instance The Widget instance to sanitize values for. * @return array $instance The Widget instance with values sanitized */ public function sanitize( $instance ) { @@ -346,6 +366,11 @@ class Jetpack_Gallery_Widget extends WP_Widget { $defaults = $this->defaults(); foreach ( $instance as $key => $value ) { + if ( ! is_scalar( $value ) ) { + // $instance may hold an Array value type for the Jetpack widget visibility feature. + continue; + } + $value = trim( $value ); if ( isset( $allowed_values[ $key ] ) && $allowed_values[ $key ] && ! array_key_exists( $value, $allowed_values[ $key ] ) ) { @@ -370,7 +395,7 @@ class Jetpack_Gallery_Widget extends WP_Widget { $max_columns = 5; // Create an associative array of allowed column values. This just automates the generation of - // column <option>s, from 1 to $max_columns + // column <option>s, from 1 to $max_columns. $allowed_columns = array_combine( range( 1, $max_columns ), range( 1, $max_columns ) ); return array( @@ -407,22 +432,31 @@ class Jetpack_Gallery_Widget extends WP_Widget { ); } + /** + * Enqueue frontend scripts. + */ public function enqueue_frontend_scripts() { wp_register_script( 'gallery-widget', Assets::get_file_url_for_environment( '_inc/build/widgets/gallery/js/gallery.min.js', 'modules/widgets/gallery/js/gallery.js' - ) + ), + array(), + JETPACK__VERSION, + false ); wp_enqueue_script( 'gallery-widget' ); } + /** + * Enqueue admin scripts and styles. + */ public function enqueue_admin_scripts() { global $pagenow; - if ( 'widgets.php' == $pagenow || 'customize.php' == $pagenow ) { + if ( 'widgets.php' === $pagenow || 'customize.php' === $pagenow ) { wp_enqueue_media(); wp_enqueue_script( @@ -435,7 +469,8 @@ class Jetpack_Gallery_Widget extends WP_Widget { 'media-models', 'media-views', ), - '20150501' + '20150501', + false ); $js_settings = array( @@ -443,7 +478,12 @@ class Jetpack_Gallery_Widget extends WP_Widget { ); wp_localize_script( 'gallery-widget-admin', '_wpGalleryWidgetAdminSettings', $js_settings ); - wp_enqueue_style( 'gallery-widget-admin', plugins_url( '/gallery/css/admin.css', __FILE__ ) ); + wp_enqueue_style( + 'gallery-widget-admin', + plugins_url( '/gallery/css/admin.css', __FILE__ ), + array(), + JETPACK__VERSION + ); wp_style_add_data( 'gallery-widget-admin', 'rtl', 'replace' ); } } @@ -451,6 +491,9 @@ class Jetpack_Gallery_Widget extends WP_Widget { add_action( 'widgets_init', 'jetpack_gallery_widget_init' ); +/** + * Jetpack Gallery widget init; the widget is conditionally registered. + */ function jetpack_gallery_widget_init() { /** * Allow the Gallery Widget to be enabled even when Core supports the Media Gallery Widget diff --git a/plugins/jetpack/modules/widgets/gallery/css/admin-rtl.css b/plugins/jetpack/modules/widgets/gallery/css/admin-rtl.css index afd9550d..709e6d6a 100644 --- a/plugins/jetpack/modules/widgets/gallery/css/admin-rtl.css +++ b/plugins/jetpack/modules/widgets/gallery/css/admin-rtl.css @@ -1,12 +1,11 @@ -/* Do not modify this file directly. It is concatenated from individual module CSS files. */ .gallery-widget-thumbs-wrapper { - margin: -5px 0 0.3em 0; + margin: -5px 0 0.3em 0; } .gallery-widget-thumbs img { - border: 1px solid #ccc; - padding: 2px; - background-color: #fff; - margin: 0 0 5px 5px; - float: right; -}
\ No newline at end of file + border: 1px solid #ccc; + padding: 2px; + background-color: #fff; + margin: 0 0 5px 5px; + float: right; +} diff --git a/plugins/jetpack/modules/widgets/gallery/css/admin-rtl.min.css b/plugins/jetpack/modules/widgets/gallery/css/admin-rtl.min.css index de937320..6bee5b53 100644 --- a/plugins/jetpack/modules/widgets/gallery/css/admin-rtl.min.css +++ b/plugins/jetpack/modules/widgets/gallery/css/admin-rtl.min.css @@ -1 +1 @@ -.gallery-widget-thumbs-wrapper{margin:-5px 0 .3em 0}.gallery-widget-thumbs img{border:1px solid #ccc;padding:2px;background-color:#fff;margin:0 0 5px 5px;float:right}
\ No newline at end of file +.gallery-widget-thumbs-wrapper{margin:-5px 0 .3em}.gallery-widget-thumbs img{background-color:#fff;border:1px solid #ccc;float:right;margin:0 0 5px 5px;padding:2px}
\ No newline at end of file diff --git a/plugins/jetpack/modules/widgets/gallery/css/admin.min.css b/plugins/jetpack/modules/widgets/gallery/css/admin.min.css index 743791f9..0e996e08 100644 --- a/plugins/jetpack/modules/widgets/gallery/css/admin.min.css +++ b/plugins/jetpack/modules/widgets/gallery/css/admin.min.css @@ -1,2 +1 @@ -/* Do not modify this file directly. It is concatenated from individual module CSS files. */ -.gallery-widget-thumbs-wrapper{margin:-5px 0 .3em 0}.gallery-widget-thumbs img{border:1px solid #ccc;padding:2px;background-color:#fff;margin:0 5px 5px 0;float:left}
\ No newline at end of file +.gallery-widget-thumbs-wrapper{margin:-5px 0 .3em}.gallery-widget-thumbs img{background-color:#fff;border:1px solid #ccc;float:left;margin:0 5px 5px 0;padding:2px}
\ No newline at end of file diff --git a/plugins/jetpack/modules/widgets/gallery/templates/form.php b/plugins/jetpack/modules/widgets/gallery/templates/form.php index f24cf1c2..799f3eef 100644 --- a/plugins/jetpack/modules/widgets/gallery/templates/form.php +++ b/plugins/jetpack/modules/widgets/gallery/templates/form.php @@ -1,6 +1,15 @@ +<?php +/** + * Jetpack_Gallery_Widget backend settings form output. + * + * @package automattic/jetpack + */ + +// phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable +?> <p> - <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?> - <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" + <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?> + <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> </label> </p> @@ -15,20 +24,23 @@ <div class="gallery-widget-thumbs"> <?php - // Add the thumbnails to the widget box + // Add the thumbnails to the widget box. $attachments = $this->get_attachments( $instance ); - foreach( $attachments as $attachment ){ - $url = add_query_arg( array( - 'w' => self::THUMB_SIZE, - 'h' => self::THUMB_SIZE, - 'crop' => 'true' - ), wp_get_attachment_url( $attachment->ID ) ); + foreach ( $attachments as $attachment ) { + $url = add_query_arg( + array( + 'w' => self::THUMB_SIZE, + 'h' => self::THUMB_SIZE, + 'crop' => 'true', + ), + wp_get_attachment_url( $attachment->ID ) + ); ?> <img src="<?php echo esc_url( $url ); ?>" title="<?php echo esc_attr( $attachment->post_title ); ?>" alt="<?php echo esc_attr( $attachment->post_title ); ?>" - width="<?php echo self::THUMB_SIZE; ?>" height="<?php echo self::THUMB_SIZE; ?>" class="thumb" /> + width="<?php echo esc_attr( self::THUMB_SIZE ); ?>" height="<?php echo esc_attr( self::THUMB_SIZE ); ?>" class="thumb" /> <?php } ?> </div> @@ -40,50 +52,28 @@ </p> <p class="gallery-widget-link-wrapper"> - <label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php esc_html_e( 'Link To:', 'jetpack' ); ?></label> - <select name="<?php echo $this->get_field_name( 'link' ); ?>" id="<?php echo $this->get_field_id( 'link' ); ?>" class="widefat"> - <?php foreach ( $allowed_values['link'] as $key => $label ) { - $selected = ''; - - if ( $instance['link'] == $key ) { - $selected = "selected='selected' "; - } ?> - - <option value="<?php echo $key; ?>" <?php echo $selected; ?>><?php echo esc_html( $label, 'jetpack' ); ?></option> - <?php } ?> + <label for="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>"><?php esc_html_e( 'Link To:', 'jetpack' ); ?></label> + <select name="<?php echo esc_attr( $this->get_field_name( 'link' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>" class="widefat"> + <?php foreach ( $allowed_values['link'] as $key => $label ) : ?> + <option value="<?php echo esc_attr( $key ); ?>"<?php selected( $instance['link'], $key ); ?>><?php echo esc_html( $label ); ?></option> + <?php endforeach; ?> </select> </p> <p> - <label for="<?php echo $this->get_field_id( 'random' ); ?>"><?php esc_html_e( 'Random Order:', 'jetpack' ); ?></label> - <?php $checked = ''; - - if ( isset( $instance['random'] ) && $instance['random'] ) - $checked = 'checked="checked"'; - - ?> - <input name="<?php echo $this->get_field_name( 'random' ); ?>" id="<?php echo $this->get_field_id( 'random' ); ?>" type="checkbox" <?php echo $checked; ?>> + <label for="<?php echo esc_attr( $this->get_field_id( 'random' ) ); ?>"><?php esc_html_e( 'Random Order:', 'jetpack' ); ?></label> + <input name="<?php echo esc_attr( $this->get_field_name( 'random' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'random' ) ); ?>" type="checkbox"<?php checked( ! empty( $instance['random'] ) ); ?>> </p> <p class="gallery-widget-style-wrapper"> - <label for="<?php echo $this->get_field_id( 'type' ); ?>"><?php esc_html_e( 'Style:', 'jetpack' ); ?></label> - <select name="<?php echo $this->get_field_name( 'type' ); ?>" id="<?php echo $this->get_field_id( 'type' ); ?>" class="widefat gallery-widget-style"> - <?php foreach ( $allowed_values['type'] as $key => $label ) { - $selected = ''; - - if ( $instance['type'] == $key ) { - $selected = "selected='selected' "; - } ?> - - <option value="<?php echo $key; ?>" <?php echo $selected; ?>><?php echo esc_html( $label, 'jetpack' ); ?></option> - <?php } ?> + <label for="<?php echo esc_attr( $this->get_field_id( 'type' ) ); ?>"><?php esc_html_e( 'Style:', 'jetpack' ); ?></label> + <select name="<?php echo esc_attr( $this->get_field_name( 'type' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'type' ) ); ?>" class="widefat gallery-widget-style"> + <?php foreach ( $allowed_values['type'] as $key => $label ) : ?> + <option value="<?php echo esc_attr( $key ); ?>"<?php selected( $instance['type'], $key ); ?>><?php echo esc_html( $label ); ?></option> + <?php endforeach; ?> </select> </p> -<?php - - -?> -<?php // Hidden input to hold the selected image ids as a csv list ?> -<input type="hidden" class="gallery-widget-ids" name="<?php echo $this->get_field_name( 'ids' ); ?>" id="<?php echo $this->get_field_id( 'ids' ); ?>" value="<?php echo esc_attr( $instance['ids'] ); ?>" /> +<?php // Hidden input to hold the selected image ids as a csv list. ?> +<input type="hidden" class="gallery-widget-ids" name="<?php echo esc_attr( $this->get_field_name( 'ids' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'ids' ) ); ?>" value="<?php echo esc_attr( $instance['ids'] ); ?>" /> diff --git a/plugins/jetpack/modules/widgets/goodreads.php b/plugins/jetpack/modules/widgets/goodreads.php index f9b3db76..369908d5 100644 --- a/plugins/jetpack/modules/widgets/goodreads.php +++ b/plugins/jetpack/modules/widgets/goodreads.php @@ -1,9 +1,9 @@ -<?php +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName + +add_action( 'widgets_init', 'jetpack_goodreads_widget_init' ); /** * Register the widget for use in Appearance -> Widgets */ -add_action( 'widgets_init', 'jetpack_goodreads_widget_init' ); - function jetpack_goodreads_widget_init() { register_widget( 'WPCOM_Widget_Goodreads' ); } @@ -12,13 +12,19 @@ function jetpack_goodreads_widget_init() { * Goodreads widget class * Display a user's Goodreads shelf. * Customize user_id, title, and shelf - * */ class WPCOM_Widget_Goodreads extends WP_Widget { - + /** + * Widget ID based on Goodreads user ID and shelf. + * + * @var int + */ private $goodreads_widget_id = 0; - function __construct() { + /** + * WPCOM_Widget_Goodreads constructor. + */ + public function __construct() { parent::__construct( 'wpcom-goodreads', /** This filter is documented in modules/widgets/facebook-likebox.php */ @@ -29,7 +35,7 @@ class WPCOM_Widget_Goodreads extends WP_Widget { 'customize_selective_refresh' => true, ) ); - // For user input sanitization and display + // For user input sanitization and display. $this->shelves = array( 'read' => _x( 'Read', 'past participle: books I have read', 'jetpack' ), 'currently-reading' => __( 'Currently Reading', 'jetpack' ), @@ -41,12 +47,26 @@ class WPCOM_Widget_Goodreads extends WP_Widget { } } - function enqueue_style() { - wp_enqueue_style( 'goodreads-widget', plugins_url( 'goodreads/css/goodreads.css', __FILE__ ) ); + /** + * Enqueue widget styles. + */ + public function enqueue_style() { + wp_enqueue_style( + 'goodreads-widget', + plugins_url( 'goodreads/css/goodreads.css', __FILE__ ), + array(), + JETPACK__VERSION + ); wp_style_add_data( 'goodreads-widget', 'rtl', 'replace' ); } - function widget( $args, $instance ) { + /** + * Display the widget. + * + * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. + * @param array $instance The settings for the particular instance of the widget. + */ + public function widget( $args, $instance ) { /** This action is documented in modules/widgets/gravatar-profile.php */ do_action( 'jetpack_stats_extra', 'widget_view', 'goodreads' ); @@ -55,13 +75,22 @@ class WPCOM_Widget_Goodreads extends WP_Widget { if ( empty( $instance['user_id'] ) || 'invalid' === $instance['user_id'] ) { if ( current_user_can( 'edit_theme_options' ) ) { - echo $args['before_widget']; + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '<p>' . sprintf( - __( 'You need to enter your numeric user ID for the <a href="%1$s">Goodreads Widget</a> to work correctly. <a href="%2$s" target="_blank">Full instructions</a>.', 'jetpack' ), + wp_kses( + /* translators: %1$s: link to the widget settings page. %2$s: support article URL for Goodreads widget. */ + __( 'You need to enter your numeric user ID for the <a href="%1$s">Goodreads Widget</a> to work correctly. <a href="%2$s" target="_blank">Full instructions</a>.', 'jetpack' ), + array( + 'a' => array( + 'href' => array(), + 'target' => array(), + ), + ) + ), esc_url( admin_url( 'widgets.php' ) ), - 'https://support.wordpress.com/widgets/goodreads-widget/#goodreads-user-id' + 'https://wordpress.com/support/widgets/goodreads-widget/#set-up-the-widget' ) . '</p>'; - echo $args['after_widget']; + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } return; } @@ -79,21 +108,27 @@ class WPCOM_Widget_Goodreads extends WP_Widget { $title = esc_html__( 'Goodreads', 'jetpack' ); } - echo $args['before_widget']; + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - $goodreads_url = 'https://www.goodreads.com/review/custom_widget/' . urlencode( $instance['user_id'] ) . '.' . urlencode( $instance['title'] ) . ':%20' . urlencode( $instance['shelf'] ) . '?cover_position=&cover_size=small&num_books=5&order=d&shelf=' . urlencode( $instance['shelf'] ) . '&sort=date_added&widget_bg_transparent=&widget_id=' . esc_attr( $this->goodreads_widget_id ); + $goodreads_url = 'https://www.goodreads.com/review/custom_widget/' . rawurlencode( $instance['user_id'] ) . '.' . rawurlencode( $instance['title'] ) . ':%20' . rawurlencode( $instance['shelf'] ) . '?cover_position=&cover_size=small&num_books=5&order=d&shelf=' . rawurlencode( $instance['shelf'] ) . '&sort=date_added&widget_bg_transparent=&widget_id=' . rawurlencode( $this->goodreads_widget_id ); echo '<div class="gr_custom_widget" id="gr_custom_widget_' . esc_attr( $this->goodreads_widget_id ) . '"></div>' . "\n"; - echo '<script src="' . esc_url( $goodreads_url ) . '"></script>' . "\n"; + echo '<script src="' . esc_url( $goodreads_url ) . '"></script>' . "\n"; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript - echo $args['after_widget']; + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } - function goodreads_user_id_exists( $user_id ) { + /** + * Check if given Goodreads user ID exists. + * + * @param string $user_id User ID. + */ + public function goodreads_user_id_exists( $user_id ) { $url = "https://www.goodreads.com/user/show/$user_id/"; $response = wp_remote_head( - $url, array( + $url, + array( 'httpversion' => '1.1', 'timeout' => 10, 'redirection' => 2, @@ -106,7 +141,15 @@ class WPCOM_Widget_Goodreads extends WP_Widget { } } - function update( $new_instance, $old_instance ) { + /** + * Update widget. + * + * @see WP_Widget::update() + * + * @param array $new_instance New widget instance data. + * @param array $old_instance Old widget instance data. + */ + public function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['user_id'] = trim( wp_kses( stripslashes( $new_instance['user_id'] ), array() ) ); @@ -124,10 +167,16 @@ class WPCOM_Widget_Goodreads extends WP_Widget { return $instance; } - function form( $instance ) { - //Defaults + /** + * Outputs the widget settings form. + * + * @param array $instance Current settings. + */ + public function form( $instance ) { + // Defaults. $instance = wp_parse_args( - (array) $instance, array( + (array) $instance, + array( 'user_id' => '', 'title' => 'Goodreads', 'shelf' => 'read', @@ -138,9 +187,21 @@ class WPCOM_Widget_Goodreads extends WP_Widget { <input class="widefat" id="' . esc_attr( $this->get_field_id( 'title' ) ) . '" name="' . esc_attr( $this->get_field_name( 'title' ) ) . '" type="text" value="' . esc_attr( $instance['title'] ) . '" /> </label></p> <p><label for="' . esc_attr( $this->get_field_id( 'user_id' ) ) . '">'; - printf( __( 'Goodreads numeric user ID <a href="%s" target="_blank">(instructions)</a>:', 'jetpack' ), 'https://en.support.wordpress.com/widgets/goodreads-widget/#goodreads-user-id' ); + printf( + wp_kses( + /* translators: %s: support article URL for Goodreads widget. */ + __( 'Goodreads numeric user ID <a href="%s" target="_blank">(instructions)</a>:', 'jetpack' ), + array( + 'a' => array( + 'href' => array(), + 'target' => array(), + ), + ) + ), + 'https://wordpress.com/support/widgets/goodreads-widget/#set-up-the-widget' + ); if ( 'invalid' === $instance['user_id'] ) { - printf( '<br /><small class="error">%s</small> ', __( 'Invalid User ID, please verify and re-enter your Goodreads numeric user ID.', 'jetpack' ) ); + printf( '<br /><small class="error">%s</small> ', esc_html( __( 'Invalid User ID, please verify and re-enter your Goodreads numeric user ID.', 'jetpack' ) ) ); $instance['user_id'] = ''; } echo '<input class="widefat" id="' . esc_attr( $this->get_field_id( 'user_id' ) ) . '" name="' . esc_attr( $this->get_field_name( 'user_id' ) ) . '" type="text" value="' . esc_attr( $instance['user_id'] ) . '" /> @@ -148,7 +209,7 @@ class WPCOM_Widget_Goodreads extends WP_Widget { <p><label for="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '">' . esc_html__( 'Shelf:', 'jetpack' ) . ' <select class="widefat" id="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '" name="' . esc_attr( $this->get_field_name( 'shelf' ) ) . '" >'; foreach ( $this->shelves as $_shelf_value => $_shelf_display ) { - echo "\t<option value='" . esc_attr( $_shelf_value ) . "'" . selected( $_shelf_value, $instance['shelf'] ) . '>' . $_shelf_display . "</option>\n"; + echo "\t<option value='" . esc_attr( $_shelf_value ) . "'" . selected( $_shelf_value, $instance['shelf'], false ) . '>' . $_shelf_display . "</option>\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } echo '</select> </label></p> diff --git a/plugins/jetpack/modules/widgets/google-translate.php b/plugins/jetpack/modules/widgets/google-translate.php index 0e270677..d6f0e88d 100644 --- a/plugins/jetpack/modules/widgets/google-translate.php +++ b/plugins/jetpack/modules/widgets/google-translate.php @@ -1,4 +1,4 @@ -<?php +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName use Automattic\Jetpack\Assets; @@ -15,20 +15,28 @@ if ( ! defined( 'ABSPATH' ) ) { exit; } +/** + * Jetpack_Google_Translate_Widget main class. + */ class Jetpack_Google_Translate_Widget extends WP_Widget { - static $instance = null; + /** + * Singleton instance of the widget, not to show more than once. + * + * @var array + */ + public static $instance = null; /** * Default widget title. * * @var string $default_title */ - var $default_title; + public $default_title; /** * Register widget with WordPress. */ - function __construct() { + public function __construct() { parent::__construct( 'google_translate_widget', /** This filter is documented in modules/widgets/facebook-likebox.php */ @@ -52,13 +60,22 @@ class Jetpack_Google_Translate_Widget extends WP_Widget { Assets::get_file_url_for_environment( '_inc/build/widgets/google-translate/google-translate.min.js', 'modules/widgets/google-translate/google-translate.js' - ) + ), + array(), + JETPACK__VERSION, + false + ); + wp_register_script( + 'google-translate', + '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit', + array( 'google-translate-init' ), + JETPACK__VERSION, + false ); - wp_register_script( 'google-translate', '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit', array( 'google-translate-init' ) ); // Admin bar is also displayed on top of the site which causes google translate bar to hide beneath. // Overwrite position of body.admin-bar // This is a hack to show google translate bar a bit lower. - $lowerTranslateBar = ' + $lower_translate_bar = ' .admin-bar { position: inherit !important; top: auto !important; @@ -77,8 +94,8 @@ class Jetpack_Google_Translate_Widget extends WP_Widget { } } '; - wp_add_inline_style( 'admin-bar', $lowerTranslateBar ); - wp_add_inline_style( 'wpcom-admin-bar', $lowerTranslateBar ); + wp_add_inline_style( 'admin-bar', $lower_translate_bar ); + wp_add_inline_style( 'wpcom-admin-bar', $lower_translate_bar ); } /** @@ -93,7 +110,8 @@ class Jetpack_Google_Translate_Widget extends WP_Widget { // We never should show more than 1 instance of this. if ( null === self::$instance ) { $instance = wp_parse_args( - $instance, array( + $instance, + array( 'title' => $this->default_title, ) ); @@ -144,12 +162,12 @@ class Jetpack_Google_Translate_Widget extends WP_Widget { /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ $title = apply_filters( 'widget_title', $title ); - echo $args['before_widget']; + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped if ( ! empty( $title ) ) { echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } echo '<div id="google_translate_element"></div>'; - echo $args['after_widget']; + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped self::$instance = $instance; /** This action is documented in modules/widgets/gravatar-profile.php */ do_action( 'jetpack_stats_extra', 'widget_view', 'google-translate' ); @@ -186,11 +204,11 @@ class Jetpack_Google_Translate_Widget extends WP_Widget { * * @return array $instance Updated safe values to be saved. */ - public function update( $new_instance, $old_instance ) { + public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $instance = array(); $instance['title'] = wp_kses( $new_instance['title'], array() ); if ( $instance['title'] === $this->default_title ) { - $instance['title'] = false; // Store as false in case of language change + $instance['title'] = false; // Store as false in case of language change. } return $instance; } diff --git a/plugins/jetpack/modules/widgets/gravatar-profile.php b/plugins/jetpack/modules/widgets/gravatar-profile.php index d2b13d12..14f8f78a 100644 --- a/plugins/jetpack/modules/widgets/gravatar-profile.php +++ b/plugins/jetpack/modules/widgets/gravatar-profile.php @@ -1,10 +1,10 @@ -<?php +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName + +add_action( 'widgets_init', 'jetpack_gravatar_profile_widget_init' ); /** * Register the widget for use in Appearance -> Widgets */ -add_action( 'widgets_init', 'jetpack_gravatar_profile_widget_init' ); - function jetpack_gravatar_profile_widget_init() { register_widget( 'Jetpack_Gravatar_Profile_Widget' ); } @@ -14,8 +14,10 @@ function jetpack_gravatar_profile_widget_init() { * https://blog.gravatar.com/2010/03/26/gravatar-profiles/ */ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { - - function __construct() { + /** + * Jetpack_Gravatar_Profile_Widget constructor. + */ + public function __construct() { parent::__construct( 'grofile', /** This filter is documented in modules/widgets/facebook-likebox.php */ @@ -36,7 +38,15 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { } } - function widget( $args, $instance ) { + /** + * Display the Widget. + * + * @see WP_Widget::widget() + * + * @param array $args Display arguments. + * @param array $instance The settings for the particular instance of the widget. + */ + public function widget( $args, $instance ) { /** * Fires when an item is displayed on the front end. * @@ -52,7 +62,8 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { do_action( 'jetpack_stats_extra', 'widget_view', 'grofile' ); $instance = wp_parse_args( - $instance, array( + $instance, + array( 'title' => '', 'email' => '', ) @@ -63,26 +74,38 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { if ( ! $instance['email'] ) { if ( current_user_can( 'edit_theme_options' ) ) { - echo $args['before_widget']; + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped if ( ! empty( $title ) ) { echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } - echo '<p>' . sprintf( __( 'You need to select what to show in this <a href="%s">Gravatar Profile widget</a>.', 'jetpack' ), admin_url( 'widgets.php' ) ) . '</p>'; - echo $args['after_widget']; + echo '<p>' . wp_kses( + sprintf( + /* translators: %s is a link to the widget settings page. */ + __( 'You need to select what to show in this <a href="%s">Gravatar Profile widget</a>.', 'jetpack' ), + admin_url( 'widgets.php' ) + ), + array( + 'a' => array( + 'href' => true, + ), + ) + ) . '</p>'; + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } return; } - echo $args['before_widget']; + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped if ( ! empty( $title ) ) { - echo $args['before_title'] . $title . $args['after_title']; + echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } $profile = $this->get_profile( $instance['email'] ); if ( ! empty( $profile ) ) { $profile = wp_parse_args( - $profile, array( + $profile, + array( 'thumbnailUrl' => '', 'profileUrl' => '', 'displayName' => '', @@ -91,7 +114,7 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { 'accounts' => array(), ) ); - $gravatar_url = add_query_arg( 's', 320, $profile['thumbnailUrl'] ); // the default grav returned by grofiles is super small + $gravatar_url = add_query_arg( 's', 320, $profile['thumbnailUrl'] ); // The default grav returned by grofiles is super small. // Enqueue front end assets. $this->enqueue_scripts(); @@ -142,10 +165,15 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { } } - echo $args['after_widget']; + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } - function display_personal_links( $personal_links = array() ) { + /** + * Displays the "Personal Links" section. + * + * @param array $personal_links Array of links. + */ + public function display_personal_links( $personal_links = array() ) { if ( empty( $personal_links ) ) { return; } @@ -168,7 +196,7 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { __( 'Personal Links', 'jetpack' ) ) ); - ?> + ?> </h4> <ul class="grofile-urls grofile-links"> @@ -187,7 +215,12 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { <?php } - function display_accounts( $accounts = array() ) { + /** + * Displays the "Verified Services" accounts. + * + * @param array $accounts Array of social accounts. + */ + public function display_accounts( $accounts = array() ) { if ( empty( $accounts ) ) { return; } @@ -197,34 +230,40 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { <?php echo esc_html( /** - * Filter the Gravatar Profile widget's "Verified Services" section title. - * - * @module widgets - * - * @since 2.8.0 - * - * @param string $str "Verified Services" section title. - */ - apply_filters( - 'jetpack_gravatar_verified_services_title', - __( 'Verified Services', 'jetpack' ) - ) + * Filter the Gravatar Profile widget's "Verified Services" section title. + * + * @module widgets + * + * @since 2.8.0 + * + * @param string $str "Verified Services" section title. + */ + apply_filters( + 'jetpack_gravatar_verified_services_title', + __( 'Verified Services', 'jetpack' ) + ) ); - ?> + ?> </h4> <ul class="grofile-urls grofile-accounts"> <?php foreach ( $accounts as $account ) : - if ( $account['verified'] != 'true' ) { + if ( 'true' !== $account['verified'] ) { continue; } $sanitized_service_name = $this->get_sanitized_service_name( $account['shortname'] ); + $link_title = sprintf( + /* translators: %1$s: service username. %2$s: service name ( Facebook, Twitter, etc.) */ + _x( '%1$s on %2$s', '1: User Name, 2: Service Name (Facebook, Twitter, ...)', 'jetpack' ), + esc_html( $account['display'] ), + esc_html( $sanitized_service_name ) + ); ?> <li> - <a href="<?php echo esc_url( $account['url'] ); ?>" title="<?php echo sprintf( _x( '%1$s on %2$s', '1: User Name, 2: Service Name (Facebook, Twitter, ...)', 'jetpack' ), esc_html( $account['display'] ), esc_html( $sanitized_service_name ) ); ?>"> + <a href="<?php echo esc_url( $account['url'] ); ?>" title="<?php echo esc_html( $link_title ); ?>"> <span class="grofile-accounts-logo grofile-accounts-<?php echo esc_attr( $account['shortname'] ); ?> accounts_<?php echo esc_attr( $account['shortname'] ); ?>"></span> </a> </li> @@ -240,7 +279,7 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { * * @since 4.0.0 */ - function enqueue_scripts() { + public function enqueue_scripts() { wp_enqueue_style( 'gravatar-profile-widget', plugins_url( 'gravatar-profile.css', __FILE__ ), @@ -256,7 +295,12 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { ); } - function form( $instance ) { + /** + * Outputs the widget settings form. + * + * @param array $instance Current settings. + */ + public function form( $instance ) { $title = isset( $instance['title'] ) ? $instance['title'] : ''; $email = isset( $instance['email'] ) ? $instance['email'] : ''; $email_user = isset( $instance['email_user'] ) ? $instance['email_user'] : get_current_user_id(); @@ -267,19 +311,19 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { $profile_url = admin_url( 'profile.php' ); - if ( isset( $_REQUEST['calypso'] ) ) { + if ( isset( $_REQUEST['calypso'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $profile_url = 'https://wordpress.com/me'; } } ?> <p> - <label for="<?php echo $this->get_field_id( 'title' ); ?>"> - <?php esc_html_e( 'Title', 'jetpack' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> + <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"> + <?php esc_html_e( 'Title', 'jetpack' ); ?> <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> </label> </p> <p> - <label for="<?php echo $this->get_field_id( 'email_user' ); ?>"> + <label for="<?php echo esc_attr( $this->get_field_id( 'email_user' ) ); ?>"> <?php esc_html_e( 'Select a user or pick "custom" and enter a custom email address.', 'jetpack' ); ?> <br /> @@ -297,15 +341,15 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { </label> </p> - <p class="gprofile-email-container <?php echo empty( $email_user ) || $email_user == -1 ? '' : 'hidden'; ?>"> - <label for="<?php echo $this->get_field_id( 'email' ); ?>"><?php esc_html_e( 'Custom Email Address', 'jetpack' ); ?> - <input class="widefat" id="<?php echo $this->get_field_id( 'email' ); ?>" name="<?php echo $this->get_field_name( 'email' ); ?>" type="text" value="<?php echo esc_attr( $email ); ?>" /> + <p class="gprofile-email-container <?php echo empty( $email_user ) || -1 === (int) $email_user ? '' : 'hidden'; ?>"> + <label for="<?php echo esc_attr( $this->get_field_id( 'email' ) ); ?>"><?php esc_html_e( 'Custom Email Address', 'jetpack' ); ?> + <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'email' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'email' ) ); ?>" type="text" value="<?php echo esc_attr( $email ); ?>" /> </label> </p> <p> - <label for="<?php echo $this->get_field_id( 'show_personal_links' ); ?>"> - <input type="checkbox" name="<?php echo $this->get_field_name( 'show_personal_links' ); ?>" id="<?php echo $this->get_field_id( 'show_personal_links' ); ?>" <?php checked( $show_personal_links ); ?> /> + <label for="<?php echo esc_attr( $this->get_field_id( 'show_personal_links' ) ); ?>"> + <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_personal_links' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'show_personal_links' ) ); ?>" <?php checked( $show_personal_links ); ?> /> <?php esc_html_e( 'Show Personal Links', 'jetpack' ); ?> <br /> <small><?php esc_html_e( 'Links to your websites, blogs, or any other sites that help describe who you are.', 'jetpack' ); ?></small> @@ -313,8 +357,8 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { </p> <p> - <label for="<?php echo $this->get_field_id( 'show_account_links' ); ?>"> - <input type="checkbox" name="<?php echo $this->get_field_name( 'show_account_links' ); ?>" id="<?php echo $this->get_field_id( 'show_account_links' ); ?>" <?php checked( $show_account_links ); ?> /> + <label for="<?php echo esc_attr( $this->get_field_id( 'show_account_links' ) ); ?>"> + <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_account_links' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'show_account_links' ) ); ?>" <?php checked( $show_account_links ); ?> /> <?php esc_html_e( 'Show Account Links', 'jetpack' ); ?> <br /> <small><?php esc_html_e( 'Links to services that you use across the web.', 'jetpack' ); ?></small> @@ -326,7 +370,10 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { <?php } - function admin_script() { + /** + * Inline admin script. + */ + public function admin_script() { ?> <script> jQuery( function( $ ) { @@ -343,8 +390,15 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { <?php } - function update( $new_instance, $old_instance ) { - + /** + * Update widget. + * + * @see WP_Widget::update() + * + * @param array $new_instance New widget instance data. + * @param array $old_instance Old widget instance data. + */ + public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $instance = array(); $instance['title'] = isset( $new_instance['title'] ) ? wp_kses( $new_instance['title'], array() ) : ''; @@ -365,11 +419,17 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { return $instance; } + /** + * Get the Gravatar profile for a given email address. + * + * @param string $email Email address. + */ private function get_profile( $email ) { $hashed_email = md5( strtolower( trim( $email ) ) ); $cache_key = 'grofile-' . $hashed_email; + $profile = get_transient( $cache_key ); - if ( ! $profile = get_transient( $cache_key ) ) { + if ( ! $profile ) { $profile_url = sprintf( 'https://secure.gravatar.com/%s.json', $hashed_email @@ -381,19 +441,19 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { array( 'User-Agent' => 'WordPress.com Gravatar Profile Widget' ) ); $response_code = wp_remote_retrieve_response_code( $response ); - if ( 200 == $response_code ) { + if ( 200 === $response_code ) { $profile = wp_remote_retrieve_body( $response ); $profile = json_decode( $profile, true ); if ( is_array( $profile ) && ! empty( $profile['entry'] ) && is_array( $profile['entry'] ) ) { - $expire = 900; // cache for 15 minutes + $expire = 900; // Cache for 15 minutes. $profile = $profile['entry'][0]; } else { // Something strange happened. Cache for 5 minutes. $profile = array(); } } else { - $expire = 900; // cache for 15 minutes + $expire = 900; // Cache for 15 minutes. $profile = array(); } @@ -402,8 +462,15 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { return $profile; } + /** + * Return properly capitalized service name. + * + * @param string $shortname The service. + * + * @return string + */ private function get_sanitized_service_name( $shortname ) { - // Some services have stylized or mixed cap names *cough* WP *cough* + // Some services have stylized or mixed cap names *cough* WP *cough*. switch ( $shortname ) { case 'friendfeed': return 'FriendFeed'; @@ -425,11 +492,9 @@ class Jetpack_Gravatar_Profile_Widget extends WP_Widget { case 'google': return 'Google+'; default: - // Others don't + // Others don't. $shortname = ucwords( $shortname ); } return $shortname; } } - -// END diff --git a/plugins/jetpack/modules/widgets/image-widget.php b/plugins/jetpack/modules/widgets/image-widget.php index e498f577..6bfa3178 100644 --- a/plugins/jetpack/modules/widgets/image-widget.php +++ b/plugins/jetpack/modules/widgets/image-widget.php @@ -1,4 +1,4 @@ -<?php +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName /** * Module Name: Image Widget * Module Description: Easily add images to your theme's sidebar. @@ -6,10 +6,10 @@ * First Introduced: 1.2 */ -/** -* Register the widget for use in Appearance -> Widgets -*/ add_action( 'widgets_init', 'jetpack_image_widget_init', 11 ); +/** + * Register the widget for use in Appearance -> Widgets + */ function jetpack_image_widget_init() { if ( class_exists( 'WP_Widget_Media_Image' ) && Jetpack_Options::get_option( 'image_widget_migration' ) ) { return; @@ -17,10 +17,13 @@ function jetpack_image_widget_init() { register_widget( 'Jetpack_Image_Widget' ); } +/** + * Jetpack_Image_Widget main class. + */ class Jetpack_Image_Widget extends WP_Widget { /** - * Register widget with WordPress. - */ + * Register widget with WordPress. + */ public function __construct() { parent::__construct( 'image', @@ -39,27 +42,28 @@ class Jetpack_Image_Widget extends WP_Widget { } /** - * Loads file for front-end widget style. - * - * @uses wp_enqueue_style(), plugins_url() - */ + * Loads file for front-end widget style. + * + * @uses wp_enqueue_style(), plugins_url() + */ public function enqueue_style() { wp_enqueue_style( 'jetpack_image_widget', plugins_url( 'image-widget/style.css', __FILE__ ), array(), '20140808' ); } /** - * Front-end display of widget. - * - * @see WP_Widget::widget() - * - * @param array $args Widget arguments. - * @param array $instance Saved values from database. - */ + * Front-end display of widget. + * + * @see WP_Widget::widget() + * + * @param array $args Widget arguments. + * @param array $instance Saved values from database. + */ public function widget( $args, $instance ) { - echo $args['before_widget']; + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $instance = wp_parse_args( - $instance, array( + $instance, + array( 'title' => '', 'img_url' => '', ) @@ -71,23 +75,23 @@ class Jetpack_Image_Widget extends WP_Widget { echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } - if ( '' != $instance['img_url'] ) { + if ( $instance['img_url'] ) { $output = '<img src="' . esc_url( $instance['img_url'] ) . '" '; - if ( '' != $instance['alt_text'] ) { + if ( '' !== (string) $instance['alt_text'] ) { $output .= 'alt="' . esc_attr( $instance['alt_text'] ) . '" '; } - if ( '' != $instance['img_title'] ) { + if ( '' !== (string) $instance['img_title'] ) { $output .= 'title="' . esc_attr( $instance['img_title'] ) . '" '; } - if ( '' == $instance['caption'] ) { + if ( '' !== (string) $instance['caption'] ) { $output .= 'class="align' . esc_attr( $instance['align'] ) . '" '; } - if ( '' != $instance['img_width'] ) { + if ( '' !== (string) $instance['img_width'] ) { $output .= 'width="' . esc_attr( $instance['img_width'] ) . '" '; } - if ( '' != $instance['img_height'] ) { + if ( '' !== (string) $instance['img_height'] ) { $output .= 'height="' . esc_attr( $instance['img_height'] ) . '" '; } $output .= '/>'; @@ -96,29 +100,40 @@ class Jetpack_Image_Widget extends WP_Widget { $output = Jetpack_Photon::filter_the_content( $output ); } - if ( '' != $instance['link'] ) { + if ( $instance['link'] ) { $target = ! empty( $instance['link_target_blank'] ) ? 'target="_blank"' : ''; $output = '<a ' . $target . ' href="' . esc_url( $instance['link'] ) . '">' . $output . '</a>'; } - if ( '' != $instance['caption'] ) { + if ( '' !== (string) $instance['caption'] ) { /** This filter is documented in core/src/wp-includes/default-widgets.php */ $caption = apply_filters( 'widget_text', $instance['caption'] ); $img_width = ( ! empty( $instance['img_width'] ) ? 'style="width: ' . esc_attr( $instance['img_width'] ) . 'px"' : '' ); $output = '<figure ' . $img_width . ' class="wp-caption align' . esc_attr( $instance['align'] ) . '"> ' . $output . ' <figcaption class="wp-caption-text">' . $caption . '</figcaption> - </figure>'; // wp_kses_post caption on update + </figure>'; // wp_kses_post caption on update. } echo '<div class="jetpack-image-container">' . do_shortcode( $output ) . '</div>'; } else { if ( current_user_can( 'edit_theme_options' ) ) { - echo '<p>' . sprintf( __( 'Image missing or invalid URL. Please check the Image widget URL in your <a href="%s">widget settings</a>.', 'jetpack' ), admin_url( 'widgets.php' ) ) . '</p>'; + echo '<p>' . wp_kses( + sprintf( + /* translators: %s link to the widget settings page. */ + __( 'Image missing or invalid URL. Please check the Image widget URL in your <a href="%s">widget settings</a>.', 'jetpack' ), + admin_url( 'widgets.php' ) + ), + array( + 'a' => array( + 'href' => array(), + ), + ) + ) . '</p>'; } } - echo "\n" . $args['after_widget']; + echo "\n" . $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped /** This action is documented in modules/widgets/gravatar-profile.php */ do_action( 'jetpack_stats_extra', 'widget_view', 'image' ); @@ -135,24 +150,24 @@ class Jetpack_Image_Widget extends WP_Widget { * @return array Updated safe values to be saved. */ public function update( $new_instance, $old_instance ) { - $allowed_caption_html = array( - 'a' => array( - 'href' => array(), - 'title' => array(), - ), - 'b' => array(), - 'em' => array(), - 'i' => array(), - 'p' => array(), - 'strong' => array(), - ); + $allowed_caption_html = array( + 'a' => array( + 'href' => array(), + 'title' => array(), + ), + 'b' => array(), + 'em' => array(), + 'i' => array(), + 'p' => array(), + 'strong' => array(), + ); $instance = $old_instance; - $instance['title'] = strip_tags( $new_instance['title'] ); + $instance['title'] = wp_strip_all_tags( $new_instance['title'] ); $instance['img_url'] = esc_url( trim( $new_instance['img_url'] ) ); - $instance['alt_text'] = strip_tags( $new_instance['alt_text'] ); - $instance['img_title'] = strip_tags( $new_instance['img_title'] ); + $instance['alt_text'] = wp_strip_all_tags( $new_instance['alt_text'] ); + $instance['img_title'] = wp_strip_all_tags( $new_instance['img_title'] ); $instance['caption'] = wp_kses( stripslashes( $new_instance['caption'] ), $allowed_caption_html ); $instance['align'] = $new_instance['align']; $instance['link'] = esc_url( trim( $new_instance['link'] ) ); @@ -161,8 +176,8 @@ class Jetpack_Image_Widget extends WP_Widget { $new_img_width = absint( $new_instance['img_width'] ); $new_img_height = absint( $new_instance['img_height'] ); - if ( ! empty( $instance['img_url'] ) && '' == $new_img_width && '' == $new_img_height ) { - // Download the url to a local temp file and then process it with getimagesize so we can optimize browser layout + if ( ! empty( $instance['img_url'] ) && 0 === $new_img_width && 0 === $new_img_height ) { + // Download the url to a local temp file and then process it with getimagesize so we can optimize browser layout. $tmp_file = download_url( $instance['img_url'], 10 ); if ( ! is_wp_error( $tmp_file ) ) { $size = getimagesize( $tmp_file ); @@ -187,16 +202,17 @@ class Jetpack_Image_Widget extends WP_Widget { } /** - * Back end widget form. - * - * @see WP_Widget::form() - * - * @param array $instance Previously saved values from database. - */ + * Back end widget form. + * + * @see WP_Widget::form() + * + * @param array $instance Previously saved values from database. + */ public function form( $instance ) { - // Defaults + // Defaults. $instance = wp_parse_args( - (array) $instance, array( + (array) $instance, + array( 'title' => '', 'img_url' => '', 'alt_text' => '', @@ -222,20 +238,20 @@ class Jetpack_Image_Widget extends WP_Widget { $link = esc_url( $instance['link'], null, 'display' ); - echo '<p><label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Widget title:', 'jetpack' ) . ' - <input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . $title . '" /> + echo '<p><label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">' . esc_html__( 'Widget title:', 'jetpack' ) . ' + <input class="widefat" id="' . esc_attr( $this->get_field_id( 'title' ) ) . '" name="' . esc_attr( $this->get_field_name( 'title' ) ) . '" type="text" value="' . esc_attr( $title ) . '" /> </label></p> - <p><label for="' . $this->get_field_id( 'img_url' ) . '">' . esc_html__( 'Image URL:', 'jetpack' ) . ' - <input class="widefat" id="' . $this->get_field_id( 'img_url' ) . '" name="' . $this->get_field_name( 'img_url' ) . '" type="text" value="' . $img_url . '" /> + <p><label for="' . esc_attr( $this->get_field_id( 'img_url' ) ) . '">' . esc_html__( 'Image URL:', 'jetpack' ) . ' + <input class="widefat" id="' . esc_attr( $this->get_field_id( 'img_url' ) ) . '" name="' . esc_attr( $this->get_field_name( 'img_url' ) ) . '" type="text" value="' . esc_attr( $img_url ) . '" /> </label></p> - <p><label for="' . $this->get_field_id( 'alt_text' ) . '">' . esc_html__( 'Alternate text:', 'jetpack' ) . ' <a href="https://support.wordpress.com/widgets/image-widget/#image-widget-alt-text" target="_blank">( ? )</a> - <input class="widefat" id="' . $this->get_field_id( 'alt_text' ) . '" name="' . $this->get_field_name( 'alt_text' ) . '" type="text" value="' . $alt_text . '" /> + <p><label for="' . esc_attr( $this->get_field_id( 'alt_text' ) ) . '">' . esc_html__( 'Alternate text:', 'jetpack' ) . ' <a href="https://support.wordpress.com/widgets/image-widget/#image-widget-alt-text" target="_blank">( ? )</a> + <input class="widefat" id="' . esc_attr( $this->get_field_id( 'alt_text' ) ) . '" name="' . esc_attr( $this->get_field_name( 'alt_text' ) ) . '" type="text" value="' . esc_attr( $alt_text ) . '" /> </label></p> - <p><label for="' . $this->get_field_id( 'img_title' ) . '">' . esc_html__( 'Image title:', 'jetpack' ) . ' <a href="https://support.wordpress.com/widgets/image-widget/#image-widget-title" target="_blank">( ? )</a> - <input class="widefat" id="' . $this->get_field_id( 'img_title' ) . '" name="' . $this->get_field_name( 'img_title' ) . '" type="text" value="' . $img_title . '" /> + <p><label for="' . esc_attr( $this->get_field_id( 'img_title' ) ) . '">' . esc_html__( 'Image title:', 'jetpack' ) . ' <a href="https://support.wordpress.com/widgets/image-widget/#image-widget-title" target="_blank">( ? )</a> + <input class="widefat" id="' . esc_attr( $this->get_field_id( 'img_title' ) ) . '" name="' . esc_attr( $this->get_field_name( 'img_title' ) ) . '" type="text" value="' . esc_attr( $img_title ) . '" /> </label></p> - <p><label for="' . $this->get_field_id( 'caption' ) . '">' . esc_html__( 'Caption:', 'jetpack' ) . ' <a href="https://support.wordpress.com/widgets/image-widget/#image-widget-caption" target="_blank">( ? )</a> - <textarea class="widefat" id="' . $this->get_field_id( 'caption' ) . '" name="' . $this->get_field_name( 'caption' ) . '" rows="2" cols="20">' . $caption . '</textarea> + <p><label for="' . esc_attr( $this->get_field_id( 'caption' ) ) . '">' . esc_html__( 'Caption:', 'jetpack' ) . ' <a href="https://support.wordpress.com/widgets/image-widget/#image-widget-caption" target="_blank">( ? )</a> + <textarea class="widefat" id="' . esc_attr( $this->get_field_id( 'caption' ) ) . '" name="' . esc_attr( $this->get_field_name( 'caption' ) ) . '" rows="2" cols="20">' . esc_textarea( $caption ) . '</textarea> </label></p>'; $alignments = array( @@ -244,29 +260,29 @@ class Jetpack_Image_Widget extends WP_Widget { 'center' => __( 'Center', 'jetpack' ), 'right' => __( 'Right', 'jetpack' ), ); - echo '<p><label for="' . $this->get_field_id( 'align' ) . '">' . esc_html__( 'Image Alignment:', 'jetpack' ) . ' - <select id="' . $this->get_field_id( 'align' ) . '" name="' . $this->get_field_name( 'align' ) . '">'; + echo '<p><label for="' . esc_attr( $this->get_field_id( 'align' ) ) . '">' . esc_html__( 'Image Alignment:', 'jetpack' ) . ' + <select id="' . esc_attr( $this->get_field_id( 'align' ) ) . '" name="' . esc_attr( $this->get_field_name( 'align' ) ) . '">'; foreach ( $alignments as $alignment => $alignment_name ) { - echo '<option value="' . esc_attr( $alignment ) . '" '; - if ( $alignment == $align ) { + echo '<option value="' . esc_attr( $alignment ) . '" '; + if ( $alignment === $align ) { echo 'selected="selected" '; } echo '>' . esc_html( $alignment_name ) . "</option>\n"; } echo '</select></label></p>'; - echo '<p><label for="' . $this->get_field_id( 'img_width' ) . '">' . esc_html__( 'Width in pixels:', 'jetpack' ) . ' - <input size="3" id="' . $this->get_field_id( 'img_width' ) . '" name="' . $this->get_field_name( 'img_width' ) . '" type="text" value="' . $img_width . '" /> + echo '<p><label for="' . esc_attr( $this->get_field_id( 'img_width' ) ) . '">' . esc_html__( 'Width in pixels:', 'jetpack' ) . ' + <input size="3" id="' . esc_attr( $this->get_field_id( 'img_width' ) ) . '" name="' . esc_attr( $this->get_field_name( 'img_width' ) ) . '" type="text" value="' . esc_attr( $img_width ) . '" /> </label> - <label for="' . $this->get_field_id( 'img_height' ) . '">' . esc_html__( 'Height in pixels:', 'jetpack' ) . ' - <input size="3" id="' . $this->get_field_id( 'img_height' ) . '" name="' . $this->get_field_name( 'img_height' ) . '" type="text" value="' . $img_height . '" /> + <label for="' . esc_attr( $this->get_field_id( 'img_height' ) ) . '">' . esc_html__( 'Height in pixels:', 'jetpack' ) . ' + <input size="3" id="' . esc_attr( $this->get_field_id( 'img_height' ) ) . '" name="' . esc_attr( $this->get_field_name( 'img_height' ) ) . '" type="text" value="' . esc_attr( $img_height ) . '" /> </label><br /> <small>' . esc_html__( 'If empty, we will attempt to determine the image size.', 'jetpack' ) . '</small></p> - <p><label for="' . $this->get_field_id( 'link' ) . '">' . esc_html__( 'Link URL (when the image is clicked):', 'jetpack' ) . ' - <input class="widefat" id="' . $this->get_field_id( 'link' ) . '" name="' . $this->get_field_name( 'link' ) . '" type="text" value="' . $link . '" /> + <p><label for="' . esc_attr( $this->get_field_id( 'link' ) ) . '">' . esc_html__( 'Link URL (when the image is clicked):', 'jetpack' ) . ' + <input class="widefat" id="' . esc_attr( $this->get_field_id( 'link' ) ) . '" name="' . esc_attr( $this->get_field_name( 'link' ) ) . '" type="text" value="' . esc_attr( $link ) . '" /> </label> - <label for="' . $this->get_field_id( 'link_target_blank' ) . '"> - <input type="checkbox" name="' . $this->get_field_name( 'link_target_blank' ) . '" id="' . $this->get_field_id( 'link_target_blank' ) . '" value="1"' . $link_target_blank . '/> + <label for="' . esc_attr( $this->get_field_id( 'link_target_blank' ) ) . '"> + <input type="checkbox" name="' . esc_attr( $this->get_field_name( 'link_target_blank' ) ) . '" id="' . esc_attr( $this->get_field_id( 'link_target_blank' ) ) . '" value="1"' . esc_attr( $link_target_blank ) . '/> ' . esc_html__( 'Open link in a new window/tab', 'jetpack' ) . ' </label></p>'; } diff --git a/plugins/jetpack/modules/widgets/internet-defense-league.php b/plugins/jetpack/modules/widgets/internet-defense-league.php index ba4170fa..bb0404ca 100644 --- a/plugins/jetpack/modules/widgets/internet-defense-league.php +++ b/plugins/jetpack/modules/widgets/internet-defense-league.php @@ -1,20 +1,65 @@ -<?php +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName +/** + * Jetpack_Internet_Defense_League_Widget main class. + */ class Jetpack_Internet_Defense_League_Widget extends WP_Widget { - + /** + * Default widget settings. + * + * @var array + */ public $defaults = array(); + /** + * Selected display variant. + * + * @var string + */ public $variant; + /** + * Display variants. + * + * @var array + */ public $variants = array(); + /** + * Selected campaign. + * + * @var string + */ public $campaign; - public $campaigns = array(); + /** + * Campaign options. + * + * @var array + */ + public $campaigns = array(); + /** + * False when enabling campaigns other than 'none' or empty. + * + * @var bool + */ public $no_current = true; + /** + * Selected badge to display. + * + * @var string + */ public $badge; + /** + * Badge display options. + * + * @var array + */ public $badges = array(); - function __construct() { + /** + * Jetpack_Internet_Defense_League_Widget constructor. + */ + public function __construct() { parent::__construct( 'internet_defense_league_widget', /** This filter is documented in modules/widgets/facebook-likebox.php */ @@ -42,7 +87,7 @@ class Jetpack_Internet_Defense_League_Widget extends WP_Widget { 'side_bar_badge' => esc_html__( 'Red Cat Badge', 'jetpack' ), ); - if ( $this->no_current === false ) { + if ( false === $this->no_current ) { $this->badges['none'] = esc_html__( 'Don\'t display a badge (just the campaign)', 'jetpack' ); } @@ -51,24 +96,45 @@ class Jetpack_Internet_Defense_League_Widget extends WP_Widget { 'variant' => key( $this->variants ), 'badge' => key( $this->badges ), ); + + add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) ); } + /** + * Remove the "Internet Defense League" widget from the Legacy Widget block + * + * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block. + * @return array $widget_types New list of widgets that will be removed. + */ + public function hide_widget_in_block_editor( $widget_types ) { + $widget_types[] = 'internet_defense_league_widget'; + return $widget_types; + } + + /** + * Display the Widget. + * + * @see WP_Widget::widget() + * + * @param array $args Display arguments. + * @param array $instance The settings for the particular instance of the widget. + */ public function widget( $args, $instance ) { $instance = wp_parse_args( $instance, $this->defaults ); - if ( 'none' != $instance['badge'] ) { + if ( 'none' !== $instance['badge'] ) { if ( ! isset( $this->badges[ $instance['badge'] ] ) ) { $instance['badge'] = $this->defaults['badge']; } $badge_url = esc_url( 'https://www.internetdefenseleague.org/images/badges/final/' . $instance['badge'] . '.png' ); $photon_badge_url = jetpack_photon_url( $badge_url ); $alt_text = esc_html__( 'Member of The Internet Defense League', 'jetpack' ); - echo $args['before_widget']; - echo '<p><a href="https://www.internetdefenseleague.org/"><img src="' . $photon_badge_url . '" alt="' . $alt_text . '" style="max-width: 100%; height: auto;" /></a></p>'; - echo $args['after_widget']; + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + echo '<p><a href="https://www.internetdefenseleague.org/"><img src="' . esc_url( $photon_badge_url ) . '" alt="' . esc_attr( $alt_text ) . '" style="max-width: 100%; height: auto;" /></a></p>'; + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } - if ( 'none' != $instance['campaign'] ) { + if ( 'none' !== $instance['campaign'] ) { $this->campaign = $instance['campaign']; $this->variant = $instance['variant']; add_action( 'wp_footer', array( $this, 'footer_script' ) ); @@ -78,6 +144,9 @@ class Jetpack_Internet_Defense_League_Widget extends WP_Widget { do_action( 'jetpack_stats_extra', 'widget_view', 'internet_defense_league' ); } + /** + * Inline footer script. + */ public function footer_script() { if ( ! isset( $this->campaigns[ $this->campaign ] ) ) { $this->campaign = $this->defaults['campaign']; @@ -107,6 +176,13 @@ class Jetpack_Internet_Defense_League_Widget extends WP_Widget { <?php } + /** + * Widget form in the dashboard. + * + * @see WP_Widget::form() + * + * @param array $instance Previously saved values from database. + */ public function form( $instance ) { $instance = wp_parse_args( $instance, $this->defaults ); @@ -128,29 +204,57 @@ class Jetpack_Internet_Defense_League_Widget extends WP_Widget { $this->select( 'badge', $this->badges, $instance['badge'] ); echo '</label></p>'; - /* translators: %s is a name of an internet campaign called the "Internet Defense League" */ - echo '<p>' . sprintf( _x( 'Learn more about the %s', 'the Internet Defense League', 'jetpack' ), '<a href="https://www.internetdefenseleague.org/">Internet Defense League</a>' ) . '</p>'; + echo '<p>' . wp_kses( + sprintf( + /* translators: %s is an HTML link to the website of an internet campaign called the "Internet Defense League" */ + _x( 'Learn more about the %s', 'the Internet Defense League', 'jetpack' ), + '<a href="https://www.internetdefenseleague.org/">Internet Defense League</a>' + ), + array( + 'a' => array( + 'href' => array(), + ), + ) + ) . '</p>'; } + /** + * Display a select form field. + * + * @param string $field_name Name of the field. + * @param array $options Array of options. + * @param string $default Default option. + */ public function select( $field_name, $options, $default = null ) { - echo '<select class="widefat" name="' . $this->get_field_name( $field_name ) . '">'; + echo '<select class="widefat" name="' . esc_attr( $this->get_field_name( $field_name ) ) . '">'; foreach ( $options as $option_slug => $option_name ) { echo '<option value="' . esc_attr( $option_slug ) . '"' . selected( $option_slug, $default, false ) . '>' . esc_html( $option_name ) . '</option>'; } echo '</select>'; } - public function update( $new_instance, $old_instance ) { + /** + * Update widget. + * + * @see WP_Widget::update() + * + * @param array $new_instance New widget instance data. + * @param array $old_instance Old widget instance data. + */ + public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $instance = array(); $instance['campaign'] = ( isset( $new_instance['campaign'] ) && isset( $this->campaigns[ $new_instance['campaign'] ] ) ) ? $new_instance['campaign'] : $this->defaults['campaign']; - $instance['variant'] = ( isset( $new_instance['variant'] ) && isset( $this->variants[ $new_instance['variant'] ] ) ) ? $new_instance['variant'] : $this->defaults['variant']; - $instance['badge'] = ( isset( $new_instance['badge'] ) && isset( $this->badges[ $new_instance['badge'] ] ) ) ? $new_instance['badge'] : $this->defaults['badge']; + $instance['variant'] = ( isset( $new_instance['variant'] ) && isset( $this->variants[ $new_instance['variant'] ] ) ) ? $new_instance['variant'] : $this->defaults['variant']; + $instance['badge'] = ( isset( $new_instance['badge'] ) && isset( $this->badges[ $new_instance['badge'] ] ) ) ? $new_instance['badge'] : $this->defaults['badge']; return $instance; } } +/** + * Register the widget. + */ function jetpack_internet_defense_league_init() { register_widget( 'Jetpack_Internet_Defense_League_Widget' ); } diff --git a/plugins/jetpack/modules/widgets/mailchimp.php b/plugins/jetpack/modules/widgets/mailchimp.php index 6f43b4db..3a8ec7a1 100644 --- a/plugins/jetpack/modules/widgets/mailchimp.php +++ b/plugins/jetpack/modules/widgets/mailchimp.php @@ -40,6 +40,19 @@ if ( ! class_exists( 'Jetpack_MailChimp_Subscriber_Popup_Widget' ) ) { 'customize_selective_refresh' => true, ) ); + + add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) ); + } + + /** + * Remove the "Mailchimp Subscriber Popup" widget from the Legacy Widget block + * + * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block. + * @return array $widget_types New list of widgets that will be removed. + */ + public function hide_widget_in_block_editor( $widget_types ) { + $widget_types[] = 'widget_mailchimp_subscriber_popup'; + return $widget_types; } /** diff --git a/plugins/jetpack/modules/widgets/migrate-to-core/image-widget.php b/plugins/jetpack/modules/widgets/migrate-to-core/image-widget.php index 06f317ff..9acdb337 100644 --- a/plugins/jetpack/modules/widgets/migrate-to-core/image-widget.php +++ b/plugins/jetpack/modules/widgets/migrate-to-core/image-widget.php @@ -155,8 +155,8 @@ function jetpack_migrate_image_widget() { // Set correct size if dimensions fit. if ( - $media_image[ $id ]['width'] == $image_meta['width'] || // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison - $media_image[ $id ]['height'] == $image_meta['height'] // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison + $media_image[ $id ]['width'] == $image_meta['width'] || // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual + $media_image[ $id ]['height'] == $image_meta['height'] // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual ) { $media_image[ $id ]['size'] = 'full'; } @@ -170,8 +170,8 @@ function jetpack_migrate_image_widget() { // Set correct size if dimensions fit. if ( - $media_image[ $id ]['width'] == $image['width'] || // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison - $media_image[ $id ]['height'] == $image['height'] // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison + $media_image[ $id ]['width'] == $image['width'] || // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual + $media_image[ $id ]['height'] == $image['height'] // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual ) { $media_image[ $id ]['size'] = $size; } diff --git a/plugins/jetpack/modules/widgets/my-community.php b/plugins/jetpack/modules/widgets/my-community.php index c16baf21..cbd515aa 100644 --- a/plugins/jetpack/modules/widgets/my-community.php +++ b/plugins/jetpack/modules/widgets/my-community.php @@ -49,6 +49,19 @@ class Jetpack_My_Community_Widget extends WP_Widget { } $this->default_title = esc_html__( 'Community', 'jetpack' ); + + add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) ); + } + + /** + * Remove the "My Community" widget from the Legacy Widget block + * + * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block. + * @return array $widget_types New list of widgets that will be removed. + */ + public function hide_widget_in_block_editor( $widget_types ) { + $widget_types[] = 'jetpack_my_community'; + return $widget_types; } /** diff --git a/plugins/jetpack/modules/widgets/search.php b/plugins/jetpack/modules/widgets/search.php deleted file mode 100644 index 291649f2..00000000 --- a/plugins/jetpack/modules/widgets/search.php +++ /dev/null @@ -1,1083 +0,0 @@ -<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -/** - * Jetpack Search: Jetpack_Search_Widget class - * - * @package Jetpack - * @subpackage Jetpack Search - * @since 5.0.0 - */ - -use Automattic\Jetpack\Redirect; -use Automattic\Jetpack\Search\Helper; -use Automattic\Jetpack\Search\Options; -use Automattic\Jetpack\Status; -use Automattic\Jetpack\Tracking; - -add_action( 'widgets_init', 'jetpack_search_widget_init' ); - -/** - * Register the widget if Jetpack Search is available and enabled. - */ -function jetpack_search_widget_init() { - if ( - ! Jetpack::is_connection_ready() - || ( method_exists( 'Jetpack_Plan', 'supports' ) && ! Jetpack_Plan::supports( 'search' ) ) - || ! Jetpack::is_module_active( 'search' ) - ) { - return; - } - - register_widget( 'Jetpack_Search_Widget' ); -} - -/** - * Provides a widget to show available/selected filters on searches. - * - * @since 5.0.0 - * - * @see WP_Widget - */ -class Jetpack_Search_Widget extends WP_Widget { - - /** - * The Jetpack_Search instance. - * - * @since 5.7.0 - * @var Jetpack_Search - */ - protected $jetpack_search; - - /** - * Number of aggregations (filters) to show by default. - * - * @since 5.8.0 - * @var int - */ - const DEFAULT_FILTER_COUNT = 5; - - /** - * Default sort order for search results. - * - * @since 5.8.0 - * @var string - */ - const DEFAULT_SORT = 'relevance_desc'; - - /** - * Jetpack_Search_Widget constructor. - * - * @since 5.0.0 - * - * @param string $name Widget name. - */ - public function __construct( $name = null ) { - if ( empty( $name ) ) { - $name = esc_html__( 'Search', 'jetpack' ); - } - parent::__construct( - Helper::FILTER_WIDGET_BASE, - /** This filter is documented in modules/widgets/facebook-likebox.php */ - apply_filters( 'jetpack_widget_name', $name ), - array( - 'classname' => 'jetpack-filters widget_search', - 'description' => __( 'Instant search and filtering to help visitors quickly find relevant answers and explore your site.', 'jetpack' ), - ) - ); - - if ( - Helper::is_active_widget( $this->id ) && - ! $this->is_search_active() - ) { - $this->activate_search(); - } - - if ( is_admin() ) { - add_action( 'sidebar_admin_setup', array( $this, 'widget_admin_setup' ) ); - } else { - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); - } - - add_action( 'jetpack_search_render_filters_widget_title', array( 'Automattic\Jetpack\Search\Template_Tags', 'render_widget_title' ), 10, 3 ); - if ( Options::is_instant_enabled() ) { - add_action( 'jetpack_search_render_filters', array( 'Automattic\Jetpack\Search\Template_Tags', 'render_instant_filters' ), 10, 2 ); - } else { - add_action( 'jetpack_search_render_filters', array( 'Automattic\Jetpack\Search\Template_Tags', 'render_available_filters' ), 10, 2 ); - } - } - - /** - * Check whether search is currently active - * - * @since 6.3 - */ - public function is_search_active() { - return Jetpack::is_module_active( 'search' ); - } - - /** - * Activate search - * - * @since 6.3 - */ - public function activate_search() { - Jetpack::activate_module( 'search', false, false ); - } - - /** - * Enqueues the scripts and styles needed for the customizer. - * - * @since 5.7.0 - */ - public function widget_admin_setup() { - wp_enqueue_style( - 'widget-jetpack-search-filters', - plugins_url( 'search/css/search-widget-admin-ui.css', __FILE__ ), - array(), - JETPACK__VERSION - ); - - // Register jp-tracks and jp-tracks-functions. - Tracking::register_tracks_functions_scripts(); - - wp_register_script( - 'jetpack-search-widget-admin', - plugins_url( 'search/js/search-widget-admin.js', __FILE__ ), - array( 'jquery', 'jquery-ui-sortable', 'jp-tracks-functions' ), - JETPACK__VERSION, - false - ); - - wp_localize_script( - 'jetpack-search-widget-admin', - 'jetpack_search_filter_admin', - array( - 'defaultFilterCount' => self::DEFAULT_FILTER_COUNT, - 'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(), - 'tracksEventData' => array( - 'is_customizer' => (int) is_customize_preview(), - ), - 'i18n' => array( - 'month' => Helper::get_date_filter_type_name( 'month', false ), - 'year' => Helper::get_date_filter_type_name( 'year', false ), - 'monthUpdated' => Helper::get_date_filter_type_name( 'month', true ), - 'yearUpdated' => Helper::get_date_filter_type_name( 'year', true ), - ), - ) - ); - - wp_enqueue_script( 'jetpack-search-widget-admin' ); - } - - /** - * Enqueue scripts and styles for the frontend. - * - * @since 5.8.0 - */ - public function enqueue_frontend_scripts() { - if ( ! is_active_widget( false, false, $this->id_base, true ) || Options::is_instant_enabled() ) { - return; - } - - wp_enqueue_script( - 'jetpack-search-widget', - plugins_url( 'search/js/search-widget.js', __FILE__ ), - array(), - JETPACK__VERSION, - true - ); - - wp_enqueue_style( - 'jetpack-search-widget', - plugins_url( 'search/css/search-widget-frontend.css', __FILE__ ), - array(), - JETPACK__VERSION - ); - } - - /** - * Get the list of valid sort types/orders. - * - * @since 5.8.0 - * - * @return array The sort orders. - */ - private function get_sort_types() { - return array( - 'relevance|DESC' => is_admin() ? esc_html__( 'Relevance (recommended)', 'jetpack' ) : esc_html__( 'Relevance', 'jetpack' ), - 'date|DESC' => esc_html__( 'Newest first', 'jetpack' ), - 'date|ASC' => esc_html__( 'Oldest first', 'jetpack' ), - ); - } - - /** - * Callback for an array_filter() call in order to only get filters for the current widget. - * - * @see Jetpack_Search_Widget::widget() - * - * @since 5.7.0 - * - * @param array $item Filter item. - * - * @return bool Whether the current filter item is for the current widget. - */ - public function is_for_current_widget( $item ) { - return isset( $item['widget_id'] ) && $this->id == $item['widget_id']; // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison - } - - /** - * This method returns a boolean for whether the widget should show site-wide filters for the site. - * - * This is meant to provide backwards-compatibility for VIP, and other professional plan users, that manually - * configured filters via `Jetpack_Search::set_filters()`. - * - * @since 5.7.0 - * - * @return bool Whether the widget should display site-wide filters or not. - */ - public function should_display_sitewide_filters() { - $filter_widgets = get_option( 'widget_jetpack-search-filters' ); - - // This shouldn't be empty, but just for sanity. - if ( empty( $filter_widgets ) ) { - return false; - } - - // If any widget has any filters, return false. - foreach ( $filter_widgets as $number => $widget ) { - $widget_id = sprintf( '%s-%d', $this->id_base, $number ); - if ( ! empty( $widget['filters'] ) && is_active_widget( false, $widget_id, $this->id_base ) ) { - return false; - } - } - - return true; - } - - /** - * Widget defaults. - * - * @param array $instance Previously saved values from database. - */ - public function jetpack_search_populate_defaults( $instance ) { - $instance = wp_parse_args( - (array) $instance, - array( - 'title' => '', - 'search_box_enabled' => true, - 'user_sort_enabled' => true, - 'sort' => self::DEFAULT_SORT, - 'filters' => array( array() ), - 'post_types' => array(), - ) - ); - - return $instance; - } - - /** - * Populates the instance array with appropriate default values. - * - * @since 8.6.0 - * @param array $instance Previously saved values from database. - * @return array Instance array with default values approprate for instant search - */ - public function populate_defaults_for_instant_search( $instance ) { - return wp_parse_args( - (array) $instance, - array( - 'title' => '', - 'filters' => array(), - ) - ); - } - - /** - * Responsible for rendering the widget on the frontend. - * - * @since 5.0.0 - * - * @param array $args Widgets args supplied by the theme. - * @param array $instance The current widget instance. - */ - public function widget( $args, $instance ) { - $instance = $this->jetpack_search_populate_defaults( $instance ); - - if ( ( new Status() )->is_offline_mode() ) { - echo $args['before_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - ?><div id="<?php echo esc_attr( $this->id ); ?>-wrapper"> - <div class="jetpack-search-sort-wrapper"> - <label> - <?php esc_html_e( 'Jetpack Search not supported in Offline Mode', 'jetpack' ); ?> - </label> - </div> - </div> - <?php - echo $args['after_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - return; - } - - if ( Options::is_instant_enabled() ) { - if ( array_key_exists( 'id', $args ) && 'jetpack-instant-search-sidebar' === $args['id'] ) { - $this->widget_empty_instant( $args, $instance ); - } else { - $this->widget_instant( $args, $instance ); - } - } else { - $this->widget_non_instant( $args, $instance ); - } - } - - /** - * Render the non-instant frontend widget. - * - * @since 8.3.0 - * - * @param array $args Widgets args supplied by the theme. - * @param array $instance The current widget instance. - */ - public function widget_non_instant( $args, $instance ) { - $display_filters = false; - - if ( is_search() ) { - if ( Helper::should_rerun_search_in_customizer_preview() ) { - Jetpack_Search::instance()->update_search_results_aggregations(); - } - - $filters = Jetpack_Search::instance()->get_filters(); - - if ( ! Helper::are_filters_by_widget_disabled() && ! $this->should_display_sitewide_filters() ) { - $filters = array_filter( $filters, array( $this, 'is_for_current_widget' ) ); - } - - if ( ! empty( $filters ) ) { - $display_filters = true; - } - } - - if ( ! $display_filters && empty( $instance['search_box_enabled'] ) && empty( $instance['user_sort_enabled'] ) ) { - return; - } - - $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; - - /** This filter is documented in core/src/wp-includes/default-widgets.php */ - $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); - - echo $args['before_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - ?> - <div id="<?php echo esc_attr( $this->id ); ?>-wrapper" > - <?php - - if ( ! empty( $title ) ) { - /** - * Responsible for displaying the title of the Jetpack Search filters widget. - * - * @module search - * - * @since 5.7.0 - * - * @param string $title The widget's title - * @param string $args['before_title'] The HTML tag to display before the title - * @param string $args['after_title'] The HTML tag to display after the title - */ - do_action( 'jetpack_search_render_filters_widget_title', $title, $args['before_title'], $args['after_title'] ); - } - - $default_sort = isset( $instance['sort'] ) ? $instance['sort'] : self::DEFAULT_SORT; - list( $orderby, $order ) = $this->sorting_to_wp_query_param( $default_sort ); - $current_sort = "{$orderby}|{$order}"; - - // we need to dynamically inject the sort field into the search box when the search box is enabled, and display - // it separately when it's not. - if ( ! empty( $instance['search_box_enabled'] ) ) { - Automattic\Jetpack\Search\Template_Tags::render_widget_search_form( $instance['post_types'], $orderby, $order ); - } - - if ( ! empty( $instance['search_box_enabled'] ) && ! empty( $instance['user_sort_enabled'] ) ) : - ?> - <div class="jetpack-search-sort-wrapper"> - <label> - <?php esc_html_e( 'Sort by', 'jetpack' ); ?> - <select class="jetpack-search-sort"> - <?php foreach ( $this->get_sort_types() as $sort => $label ) { ?> - <option value="<?php echo esc_attr( $sort ); ?>" <?php selected( $current_sort, $sort ); ?>> - <?php echo esc_html( $label ); ?> - </option> - <?php } ?> - </select> - </label> - </div> - <?php - endif; - - if ( $display_filters ) { - /** - * Responsible for rendering filters to narrow down search results. - * - * @module search - * - * @since 5.8.0 - * - * @param array $filters The possible filters for the current query. - * @param array $post_types An array of post types to limit filtering to. - */ - do_action( - 'jetpack_search_render_filters', - $filters, - isset( $instance['post_types'] ) ? $instance['post_types'] : null - ); - } - - $this->maybe_render_sort_javascript( $instance, $order, $orderby ); - - echo '</div>'; - echo $args['after_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - } - - /** - * Render the instant frontend widget. - * - * @since 8.3.0 - * - * @param array $args Widgets args supplied by the theme. - * @param array $instance The current widget instance. - */ - public function widget_instant( $args, $instance ) { - if ( Helper::should_rerun_search_in_customizer_preview() ) { - Jetpack_Search::instance()->update_search_results_aggregations(); - } - - $filters = Jetpack_Search::instance()->get_filters(); - if ( ! Helper::are_filters_by_widget_disabled() && ! $this->should_display_sitewide_filters() ) { - $filters = array_filter( $filters, array( $this, 'is_for_current_widget' ) ); - } - - $display_filters = ! empty( $filters ); - - $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; - - /** This filter is documented in core/src/wp-includes/default-widgets.php */ - $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); - - echo $args['before_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - ?> - <div id="<?php echo esc_attr( $this->id ); ?>-wrapper" class="jetpack-instant-search-wrapper"> - <?php - - if ( ! empty( $title ) ) { - /** - * Responsible for displaying the title of the Jetpack Search filters widget. - * - * @module search - * - * @since 5.7.0 - * - * @param string $title The widget's title - * @param string $args['before_title'] The HTML tag to display before the title - * @param string $args['after_title'] The HTML tag to display after the title - */ - do_action( 'jetpack_search_render_filters_widget_title', $title, $args['before_title'], $args['after_title'] ); - } - - Automattic\Jetpack\Search\Template_Tags::render_widget_search_form( array(), '', '' ); - - if ( $display_filters ) { - /** - * Responsible for rendering filters to narrow down search results. - * - * @module search - * - * @since 5.8.0 - * - * @param array $filters The possible filters for the current query. - * @param array $post_types An array of post types to limit filtering to. - */ - do_action( - 'jetpack_search_render_filters', - $filters, - null - ); - } - - echo '</div>'; - echo $args['after_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - } - - /** - * Render the instant widget for the overlay. - * - * @since 8.3.0 - * - * @param array $args Widgets args supplied by the theme. - * @param array $instance The current widget instance. - */ - public function widget_empty_instant( $args, $instance ) { - $title = isset( $instance['title'] ) ? $instance['title'] : ''; - - if ( empty( $title ) ) { - $title = ''; - } - - /** This filter is documented in core/src/wp-includes/default-widgets.php */ - $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); - - echo $args['before_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - ?> - <div id="<?php echo esc_attr( $this->id ); ?>-wrapper" class="jetpack-instant-search-wrapper"> - <?php - - if ( ! empty( $title ) ) { - /** - * Responsible for displaying the title of the Jetpack Search filters widget. - * - * @module search - * - * @since 5.7.0 - * - * @param string $title The widget's title - * @param string $args['before_title'] The HTML tag to display before the title - * @param string $args['after_title'] The HTML tag to display after the title - */ - do_action( 'jetpack_search_render_filters_widget_title', $title, $args['before_title'], $args['after_title'] ); - } - - echo '</div>'; - echo $args['after_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - } - - /** - * Renders JavaScript for the sorting controls on the frontend. - * - * This JS is a bit complicated, but here's what it's trying to do: - * - find the search form - * - find the orderby/order fields and set default values - * - detect changes to the sort field, if it exists, and use it to set the order field values - * - * @since 5.8.0 - * - * @param array $instance The current widget instance. - * @param string $order The order to initialize the select with. - * @param string $orderby The orderby to initialize the select with. - */ - private function maybe_render_sort_javascript( $instance, $order, $orderby ) { - if ( Options::is_instant_enabled() ) { - return; - } - - if ( ! empty( $instance['user_sort_enabled'] ) ) : - ?> - <script type="text/javascript"> - var jetpackSearchModuleSorting = function() { - var orderByDefault = '<?php echo 'date' === $orderby ? 'date' : 'relevance'; ?>', - orderDefault = '<?php echo 'ASC' === $order ? 'ASC' : 'DESC'; ?>', - widgetId = decodeURIComponent( '<?php echo rawurlencode( $this->id ); ?>' ), - searchQuery = decodeURIComponent( '<?php echo rawurlencode( get_query_var( 's', '' ) ); ?>' ), - isSearch = <?php echo (int) is_search(); ?>; - - var container = document.getElementById( widgetId + '-wrapper' ), - form = container.querySelector( '.jetpack-search-form form' ), - orderBy = form.querySelector( 'input[name=orderby]' ), - order = form.querySelector( 'input[name=order]' ), - searchInput = form.querySelector( 'input[name="s"]' ), - sortSelectInput = container.querySelector( '.jetpack-search-sort' ); - - orderBy.value = orderByDefault; - order.value = orderDefault; - - // Some themes don't set the search query, which results in the query being lost - // when doing a sort selection. So, if the query isn't set, let's set it now. This approach - // is chosen over running a regex over HTML for every search query performed. - if ( isSearch && ! searchInput.value ) { - searchInput.value = searchQuery; - } - - searchInput.classList.add( 'show-placeholder' ); - - sortSelectInput.addEventListener( 'change', function( event ) { - var values = event.target.value.split( '|' ); - orderBy.value = values[0]; - order.value = values[1]; - - form.submit(); - } ); - } - - if ( document.readyState === 'interactive' || document.readyState === 'complete' ) { - jetpackSearchModuleSorting(); - } else { - document.addEventListener( 'DOMContentLoaded', jetpackSearchModuleSorting ); - } - </script> - <?php - endif; - } - - /** - * Convert a sort string into the separate order by and order parts. - * - * @since 5.8.0 - * - * @param string $sort A sort string. - * - * @return array Order by and order. - */ - private function sorting_to_wp_query_param( $sort ) { - // phpcs:disable WordPress.Security.NonceVerification.Recommended - $parts = explode( '|', $sort ); - $orderby = isset( $_GET['orderby'] ) - ? $_GET['orderby'] - : $parts[0]; - - $order = isset( $_GET['order'] ) - ? strtoupper( $_GET['order'] ) - : ( ( isset( $parts[1] ) && 'ASC' === strtoupper( $parts[1] ) ) ? 'ASC' : 'DESC' ); - - // phpcs:enable WordPress.Security.NonceVerification.Recommended - - return array( $orderby, $order ); - } - - /** - * Updates a particular instance of the widget. Validates and sanitizes the options. - * - * @since 5.0.0 - * - * @param array $new_instance New settings for this instance as input by the user via Jetpack_Search_Widget::form(). - * @param array $old_instance Old settings for this instance. - * - * @return array Settings to save. - */ - public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable - $new_instance = $this->maybe_reformat_widget( $new_instance ); - $instance = array(); - - $instance['title'] = sanitize_text_field( $new_instance['title'] ); - $instance['search_box_enabled'] = empty( $new_instance['search_box_enabled'] ) ? '0' : '1'; - $instance['user_sort_enabled'] = empty( $new_instance['user_sort_enabled'] ) ? '0' : '1'; - $instance['sort'] = $new_instance['sort']; - $instance['post_types'] = empty( $new_instance['post_types'] ) || empty( $instance['search_box_enabled'] ) - ? array() - : array_map( 'sanitize_key', $new_instance['post_types'] ); - - $filters = array(); - if ( isset( $new_instance['filter_type'] ) ) { - foreach ( (array) $new_instance['filter_type'] as $index => $type ) { - $count = (int) $new_instance['num_filters'][ $index ]; - $count = min( 50, $count ); // Set max boundary at 50. - $count = max( 1, $count ); // Set min boundary at 1. - - switch ( $type ) { - case 'taxonomy': - $filters[] = array( - 'name' => sanitize_text_field( $new_instance['filter_name'][ $index ] ), - 'type' => 'taxonomy', - 'taxonomy' => sanitize_key( $new_instance['taxonomy_type'][ $index ] ), - 'count' => $count, - ); - break; - case 'post_type': - $filters[] = array( - 'name' => sanitize_text_field( $new_instance['filter_name'][ $index ] ), - 'type' => 'post_type', - 'count' => $count, - ); - break; - case 'date_histogram': - $filters[] = array( - 'name' => sanitize_text_field( $new_instance['filter_name'][ $index ] ), - 'type' => 'date_histogram', - 'count' => $count, - 'field' => sanitize_key( $new_instance['date_histogram_field'][ $index ] ), - 'interval' => sanitize_key( $new_instance['date_histogram_interval'][ $index ] ), - ); - break; - } - } - } - - if ( ! empty( $filters ) ) { - $instance['filters'] = $filters; - } - - return $instance; - } - - /** - * Reformats the widget instance array to one that is recognized by the `update` function. - * This is only necessary when handling changes from the block-based widget editor. - * - * @param array $widget_instance - Jetpack Search widget instance. - * - * @return array - Potentially reformatted instance compatible with the save function. - */ - protected function maybe_reformat_widget( $widget_instance ) { - if ( isset( $widget_instance['filter_type'] ) || ! isset( $widget_instance['filters'] ) || ! is_array( $widget_instance['filters'] ) ) { - return $widget_instance; - } - - $instance = $widget_instance; - foreach ( $widget_instance['filters'] as $filter ) { - $instance['filter_type'][] = isset( $filter['type'] ) ? $filter['type'] : ''; - $instance['taxonomy_type'][] = isset( $filter['taxonomy'] ) ? $filter['taxonomy'] : ''; - $instance['filter_name'][] = isset( $filter['name'] ) ? $filter['name'] : ''; - $instance['num_filters'][] = isset( $filter['count'] ) ? $filter['count'] : 5; - $instance['date_histogram_field'][] = isset( $filter['field'] ) ? $filter['field'] : ''; - $instance['date_histogram_interval'][] = isset( $filter['interval'] ) ? $filter['interval'] : ''; - } - unset( $instance['filters'] ); - return $instance; - } - - /** - * Outputs the settings update form. - * - * @since 5.0.0 - * - * @param array $instance Previously saved values from database. - */ - public function form( $instance ) { - if ( Options::is_instant_enabled() ) { - return $this->form_for_instant_search( $instance ); - } - - $instance = $this->jetpack_search_populate_defaults( $instance ); - - $title = wp_strip_all_tags( $instance['title'] ); - - $hide_filters = Helper::are_filters_by_widget_disabled(); - - $classes = sprintf( - 'jetpack-search-filters-widget %s %s %s', - $hide_filters ? 'hide-filters' : '', - $instance['search_box_enabled'] ? '' : 'hide-post-types', - $this->id - ); - ?> - <div class="<?php echo esc_attr( $classes ); ?>"> - <p> - <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"> - <?php esc_html_e( 'Title (optional):', 'jetpack' ); ?> - </label> - <input - class="widefat" - id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" - name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" - type="text" - value="<?php echo esc_attr( $title ); ?>" - /> - </p> - - <p> - <label> - <input - type="checkbox" - class="jetpack-search-filters-widget__search-box-enabled" - name="<?php echo esc_attr( $this->get_field_name( 'search_box_enabled' ) ); ?>" - <?php checked( $instance['search_box_enabled'] ); ?> - /> - <?php esc_html_e( 'Show search box', 'jetpack' ); ?> - </label> - </p> - - <p> - <label> - <input - type="checkbox" - class="jetpack-search-filters-widget__sort-controls-enabled" - name="<?php echo esc_attr( $this->get_field_name( 'user_sort_enabled' ) ); ?>" - <?php checked( $instance['user_sort_enabled'] ); ?> - <?php disabled( ! $instance['search_box_enabled'] ); ?> - /> - <?php esc_html_e( 'Show sort selection dropdown', 'jetpack' ); ?> - </label> - </p> - - <p class="jetpack-search-filters-widget__post-types-select"> - <label><?php esc_html_e( 'Post types to search (minimum of 1):', 'jetpack' ); ?></label> - <?php foreach ( get_post_types( array( 'exclude_from_search' => false ), 'objects' ) as $post_type ) : ?> - <label> - <input - type="checkbox" - value="<?php echo esc_attr( $post_type->name ); ?>" - name="<?php echo esc_attr( $this->get_field_name( 'post_types' ) ); ?>[]" - <?php checked( empty( $instance['post_types'] ) || in_array( $post_type->name, $instance['post_types'], true ) ); ?> - /> - <?php echo esc_html( $post_type->label ); ?> - </label> - <?php endforeach; ?> - </p> - - <p> - <label> - <?php esc_html_e( 'Default sort order:', 'jetpack' ); ?> - <select - name="<?php echo esc_attr( $this->get_field_name( 'sort' ) ); ?>" - class="widefat jetpack-search-filters-widget__sort-order"> - <?php foreach ( $this->get_sort_types() as $sort_type => $label ) { ?> - <option value="<?php echo esc_attr( $sort_type ); ?>" <?php selected( $instance['sort'], $sort_type ); ?>> - <?php echo esc_html( $label ); ?> - </option> - <?php } ?> - </select> - </label> - </p> - - <?php if ( ! $hide_filters ) : ?> - <script class="jetpack-search-filters-widget__filter-template" type="text/template"> - <?php - echo $this->render_widget_edit_filter( array(), true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - ?> - </script> - <div class="jetpack-search-filters-widget__filters"> - <?php foreach ( (array) $instance['filters'] as $filter ) : ?> - <?php $this->render_widget_edit_filter( $filter ); ?> - <?php endforeach; ?> - </div> - <p class="jetpack-search-filters-widget__add-filter-wrapper"> - <a class="button jetpack-search-filters-widget__add-filter" href="#"> - <?php esc_html_e( 'Add a filter', 'jetpack' ); ?> - </a> - </p> - <noscript> - <p class="jetpack-search-filters-help"> - <?php echo esc_html_e( 'Adding filters requires JavaScript!', 'jetpack' ); ?> - </p> - </noscript> - <?php if ( is_customize_preview() ) : ?> - <p class="jetpack-search-filters-help"> - <a href="<?php echo esc_url( Redirect::get_url( 'jetpack-support-search', array( 'anchor' => 'filters-not-showing-up' ) ) ); ?>" target="_blank"> - <?php esc_html_e( "Why aren't my filters appearing?", 'jetpack' ); ?> - </a> - </p> - <?php endif; ?> - <?php endif; ?> - </div> - <?php - } - - /** - * Outputs the widget update form to be used in the Customizer for Instant Search. - * - * @since 8.6.0 - * - * @param array $instance Previously saved values from database. - */ - private function form_for_instant_search( $instance ) { - $instance = $this->populate_defaults_for_instant_search( $instance ); - $classes = sprintf( 'jetpack-search-filters-widget %s', $this->id ); - - ?> - <div class="<?php echo esc_attr( $classes ); ?>"> - <!-- Title control --> - <p> - <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"> - <?php esc_html_e( 'Title (optional):', 'jetpack' ); ?> - </label> - <input - class="widefat" - id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" - name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" - type="text" - value="<?php echo esc_attr( wp_strip_all_tags( $instance['title'] ) ); ?>" - /> - </p> - - <!-- Filters control --> - <?php if ( ! Helper::are_filters_by_widget_disabled() ) : ?> - <div class="jetpack-search-filters-widget__filters"> - <?php foreach ( (array) $instance['filters'] as $filter ) : ?> - <?php $this->render_widget_edit_filter( $filter ); ?> - <?php endforeach; ?> - </div> - <p class="jetpack-search-filters-widget__add-filter-wrapper"> - <a class="button jetpack-search-filters-widget__add-filter" href="#"> - <?php esc_html_e( 'Add a filter', 'jetpack' ); ?> - </a> - </p> - <script class="jetpack-search-filters-widget__filter-template" type="text/template"> - <?php $this->render_widget_edit_filter( array(), true ); ?> - </script> - <noscript> - <p class="jetpack-search-filters-help"> - <?php echo esc_html_e( 'Adding filters requires JavaScript!', 'jetpack' ); ?> - </p> - </noscript> - <?php endif; ?> - </div> - <?php - } - - /** - * We need to render HTML in two formats: an Underscore template (client-side) - * and native PHP (server-side). This helper function allows for easy rendering - * of attributes in both formats. - * - * @since 5.8.0 - * - * @param string $name Attribute name. - * @param string $value Attribute value. - * @param bool $is_template Whether this is for an Underscore template or not. - */ - private function render_widget_attr( $name, $value, $is_template ) { - echo $is_template ? "<%= $name %>" : esc_attr( $value ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - } - - /** - * We need to render HTML in two formats: an Underscore template (client-size) - * and native PHP (server-side). This helper function allows for easy rendering - * of the "selected" attribute in both formats. - * - * @since 5.8.0 - * - * @param string $name Attribute name. - * @param string $value Attribute value. - * @param string $compare Value to compare to the attribute value to decide if it should be selected. - * @param bool $is_template Whether this is for an Underscore template or not. - */ - private function render_widget_option_selected( $name, $value, $compare, $is_template ) { - $compare_js = rawurlencode( $compare ); - echo $is_template ? "<%= decodeURIComponent( '$compare_js' ) === $name ? 'selected=\"selected\"' : '' %>" : selected( $value, $compare ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - } - - /** - * Responsible for rendering a single filter in the customizer or the widget administration screen in wp-admin. - * - * We use this method for two purposes - rendering the fields server-side, and also rendering a script template for Underscore. - * - * @since 5.7.0 - * - * @param array $filter The filter to render. - * @param bool $is_template Whether this is for an Underscore template or not. - */ - public function render_widget_edit_filter( $filter, $is_template = false ) { - $args = wp_parse_args( - $filter, - array( - 'name' => '', - 'type' => 'taxonomy', - 'taxonomy' => '', - 'post_type' => '', - 'field' => '', - 'interval' => '', - 'count' => self::DEFAULT_FILTER_COUNT, - ) - ); - - $args['name_placeholder'] = Helper::generate_widget_filter_name( $args ); - - ?> - <div class="jetpack-search-filters-widget__filter is-<?php $this->render_widget_attr( 'type', $args['type'], $is_template ); ?>"> - <p class="jetpack-search-filters-widget__type-select"> - <label> - <?php esc_html_e( 'Filter Type:', 'jetpack' ); ?> - <select name="<?php echo esc_attr( $this->get_field_name( 'filter_type' ) ); ?>[]" class="widefat filter-select"> - <option value="taxonomy" <?php $this->render_widget_option_selected( 'type', $args['type'], 'taxonomy', $is_template ); ?>> - <?php esc_html_e( 'Taxonomy', 'jetpack' ); ?> - </option> - <option value="post_type" <?php $this->render_widget_option_selected( 'type', $args['type'], 'post_type', $is_template ); ?>> - <?php esc_html_e( 'Post Type', 'jetpack' ); ?> - </option> - <option value="date_histogram" <?php $this->render_widget_option_selected( 'type', $args['type'], 'date_histogram', $is_template ); ?>> - <?php esc_html_e( 'Date', 'jetpack' ); ?> - </option> - </select> - </label> - </p> - - <p class="jetpack-search-filters-widget__taxonomy-select"> - <label> - <?php - esc_html_e( 'Choose a taxonomy:', 'jetpack' ); - $seen_taxonomy_labels = array(); - ?> - <select name="<?php echo esc_attr( $this->get_field_name( 'taxonomy_type' ) ); ?>[]" class="widefat taxonomy-select"> - <?php foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $taxonomy ) : ?> - <option value="<?php echo esc_attr( $taxonomy->name ); ?>" <?php $this->render_widget_option_selected( 'taxonomy', $args['taxonomy'], $taxonomy->name, $is_template ); ?>> - <?php - $label = in_array( $taxonomy->label, $seen_taxonomy_labels, true ) - ? sprintf( - /* translators: %1$s is the taxonomy name, %2s is the name of its type to help distinguish between several taxonomies with the same name, e.g. category and tag. */ - _x( '%1$s (%2$s)', 'A label for a taxonomy selector option', 'jetpack' ), - $taxonomy->label, - $taxonomy->name - ) - : $taxonomy->label; - echo esc_html( $label ); - $seen_taxonomy_labels[] = $taxonomy->label; - ?> - </option> - <?php endforeach; ?> - </select> - </label> - </p> - - <p class="jetpack-search-filters-widget__date-histogram-select"> - <label> - <?php esc_html_e( 'Choose a field:', 'jetpack' ); ?> - <select name="<?php echo esc_attr( $this->get_field_name( 'date_histogram_field' ) ); ?>[]" class="widefat date-field-select"> - <option value="post_date" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_date', $is_template ); ?>> - <?php esc_html_e( 'Date', 'jetpack' ); ?> - </option> - <option value="post_date_gmt" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_date_gmt', $is_template ); ?>> - <?php esc_html_e( 'Date GMT', 'jetpack' ); ?> - </option> - <option value="post_modified" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_modified', $is_template ); ?>> - <?php esc_html_e( 'Modified', 'jetpack' ); ?> - </option> - <option value="post_modified_gmt" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_modified_gmt', $is_template ); ?>> - <?php esc_html_e( 'Modified GMT', 'jetpack' ); ?> - </option> - </select> - </label> - </p> - - <p class="jetpack-search-filters-widget__date-histogram-select"> - <label> - <?php esc_html_e( 'Choose an interval:', 'jetpack' ); ?> - <select name="<?php echo esc_attr( $this->get_field_name( 'date_histogram_interval' ) ); ?>[]" class="widefat date-interval-select"> - <option value="month" <?php $this->render_widget_option_selected( 'interval', $args['interval'], 'month', $is_template ); ?>> - <?php esc_html_e( 'Month', 'jetpack' ); ?> - </option> - <option value="year" <?php $this->render_widget_option_selected( 'interval', $args['interval'], 'year', $is_template ); ?>> - <?php esc_html_e( 'Year', 'jetpack' ); ?> - </option> - </select> - </label> - </p> - - <p class="jetpack-search-filters-widget__title"> - <label> - <?php esc_html_e( 'Title:', 'jetpack' ); ?> - <input - class="widefat" - type="text" - name="<?php echo esc_attr( $this->get_field_name( 'filter_name' ) ); ?>[]" - value="<?php $this->render_widget_attr( 'name', $args['name'], $is_template ); ?>" - placeholder="<?php $this->render_widget_attr( 'name_placeholder', $args['name_placeholder'], $is_template ); ?>" - /> - </label> - </p> - - <p> - <label> - <?php esc_html_e( 'Maximum number of filters (1-50):', 'jetpack' ); ?> - <input - class="widefat filter-count" - name="<?php echo esc_attr( $this->get_field_name( 'num_filters' ) ); ?>[]" - type="number" - value="<?php $this->render_widget_attr( 'count', $args['count'], $is_template ); ?>" - min="1" - max="50" - step="1" - required - /> - </label> - </p> - - <p class="jetpack-search-filters-widget__controls"> - <a href="#" class="delete"><?php esc_html_e( 'Remove', 'jetpack' ); ?></a> - </p> - </div> - <?php - } -} diff --git a/plugins/jetpack/modules/widgets/search/css/search-widget-admin-ui.css b/plugins/jetpack/modules/widgets/search/css/search-widget-admin-ui.css deleted file mode 100644 index b5f4544a..00000000 --- a/plugins/jetpack/modules/widgets/search/css/search-widget-admin-ui.css +++ /dev/null @@ -1,87 +0,0 @@ -.jetpack-search-filters-widget__filter { - background: #f6f7f7; - border: 1px solid #dcdcde; - padding: 0 12px; - margin-bottom: 12px; - cursor: move; -} - -.jetpack-search-filters-widget__controls { - text-align: right; -} - -.jetpack-search-filters-widget .jetpack-search-filters-widget__sort-controls-enabled { - margin-left: 24px; -} - -.jetpack-search-filters-widget__controls .delete { - color: #d63638; -} - -.jetpack-search-filters-widget.hide-filters .jetpack-search-filters-widget__filter { - display: none; -} - -.button.jetpack-search-filters-widget__add-filter { - margin-bottom: 10px; -} - -/* Assume that taxonomy select is the default selected. Other controls should be hidden here. */ -.jetpack-search-filters-widget__post-type-select { - display: none; -} - -.jetpack-search-filters-widget__date-histogram-select { - display: none; -} - -.jetpack-search-filters-widget__filter-placeholder { - border: 1px #555 dashed; - background-color: #f0f0f1; - height: 286px; - margin-bottom: 12px; -} - -/* When post type is selected, remove the other controls */ -.jetpack-search-filters-widget__filter.is-post_type .jetpack-search-filters-widget__taxonomy-select { - display: none; -} - -/* When date is selected, remove the other controls */ -.jetpack-search-filters-widget__filter.is-date_histogram .jetpack-search-filters-widget__date-histogram-select { - display: inline; -} - -.jetpack-search-filters-widget__filter.is-date_histogram .jetpack-search-filters-widget__taxonomy-select { - display: none; -} - -.jetpack-search-filters-widget.hide-post-types .jetpack-search-filters-widget__post-types-select { - display: none; -} - -.jetpack-search-filters-help:before { - display: inline-block; - position: relative; - font-family: dashicons; - font-size: 20px; - top: 5px; - line-height: 1px; - content:"\f223"; -} -.jetpack-search-filters-help { - padding: 5px 5px 15px 0; -} - -.jetpack-search-filters-widget__post-types-select label { - display: block; - margin-bottom: 4px; -} - -.jetpack-search-filters-widget__post-types-select input[type="checkbox"] { - margin-left: 24px; -} - -body.no-js .jetpack-search-filters-widget__add-filter-wrapper { - display: none; -} diff --git a/plugins/jetpack/modules/widgets/search/css/search-widget-frontend.css b/plugins/jetpack/modules/widgets/search/css/search-widget-frontend.css deleted file mode 100644 index 58c7cf3e..00000000 --- a/plugins/jetpack/modules/widgets/search/css/search-widget-frontend.css +++ /dev/null @@ -1,66 +0,0 @@ -.jetpack-search-filters-widget__sub-heading { - font-size: inherit; - font-weight: bold; - margin: 0 0 .5em; - padding: 0; -} - -/* The first heading after the form */ -.jetpack-search-form + .jetpack-search-filters-widget__sub-heading { - margin-top: 1.5em; - margin-bottom: 0.5em !important; -} - -.jetpack-search-filters-widget__clear { - margin-top: 0.5em; - margin-bottom: 0.5em; -} - -.jetpack-search-sort-wrapper { - margin-top: 1em; - margin-bottom: 1.5em; -} - -.jetpack-search-sort-wrapper label { - display: inherit; -} - -.widget_search .jetpack-search-filters-widget__filter-list input[type="checkbox"] { - width: auto; - height: auto; -} - -ul.jetpack-search-filters-widget__filter-list li { - border: none; - padding: 0; - list-style: none; -} - -ul.jetpack-search-filters-widget__filter-list li a { - text-decoration: none; -} - -ul.jetpack-search-filters-widget__filter-list li a:hover { - box-shadow: none; -} - -ul.jetpack-search-filters-widget__filter-list li label { - font-weight: inherit; - display: inherit; -} - -.jetpack-search-filters-widget__filter-list { - list-style: none; -} - -ul.jetpack-search-filters-widget__filter-list { - margin-bottom: 1.5em; -} - -body.search .jetpack-search-form input[name="s"]::placeholder { - color: transparent; -} - -body.search .jetpack-search-form input[name="s"].show-placeholder::placeholder { - color: inherit; -} diff --git a/plugins/jetpack/modules/widgets/search/js/search-widget-admin.js b/plugins/jetpack/modules/widgets/search/js/search-widget-admin.js deleted file mode 100644 index d4ae235f..00000000 --- a/plugins/jetpack/modules/widgets/search/js/search-widget-admin.js +++ /dev/null @@ -1,358 +0,0 @@ -/* globals jetpack_search_filter_admin, jQuery, analytics */ - -( function( $, args ) { - var defaultFilterCount = ( 'undefined' !== typeof args && args.defaultFilterCount ) ? - args.defaultFilterCount : - 5; // Just in case we couldn't find the defaultFiltercount arg - - $( document ).ready( function() { - setListeners(); - - window.JetpackSearch = window.JetpackSearch || {}; - window.JetpackSearch.addFilter = addFilter; - - // Initialize Tracks - if ( 'undefined' !== typeof analytics && args.tracksUserData ) { - analytics.initialize( args.tracksUserData.userid, args.tracksUserData.username ); - } - } ); - - function generateFilterTitlePlaceholder( container ) { - var placeholder = null, - isModified = null, - isMonth = null, - type = container.find( '.filter-select' ).val(); - - if ( 'taxonomy' === type ) { - placeholder = container.find('.taxonomy-select option:selected').text().trim(); - } else if ( 'date_histogram' === type && args && args.i18n ) { - isModified = ( -1 !== container.find( '.date-field-select' ).val().indexOf( 'modified' ) ); - isMonth = ( 'month' === container.find( '.date-interval-select' ).val() ); - - if ( isMonth ) { - placeholder = isModified ? - args.i18n.monthUpdated : - args.i18n.month; - } else { - placeholder = isModified ? - args.i18n.yearUpdated : - args.i18n.year; - } - } else { - placeholder = container.find('.filter-select option:selected').text().trim(); - } - - $( container ).find('.jetpack-search-filters-widget__title input').prop( 'placeholder', placeholder ); - } - - var addFilter = function( filtersContainer, args ) { - var template = _.template( - filtersContainer - .closest( '.jetpack-search-filters-widget' ) - .find( '.jetpack-search-filters-widget__filter-template' ) - .html() - ); - generateFilterTitlePlaceholder( filtersContainer.append( template( args ) ) ); - }; - - var setListeners = function( widget ) { - widget = ( 'undefined' === typeof widget ) ? - $( '.jetpack-search-filters-widget' ): - widget; - - var getContainer = function( el ) { - return $( el ).closest('.jetpack-search-filters-widget__filter'); - }; - - widget.on( 'change', '.filter-select', function() { - var select = $( this ), - selectVal = select.val(), - eventArgs = { - is_customizer: args.tracksEventData.is_customizer - }; - - eventArgs.type = selectVal; - - select - .closest( '.jetpack-search-filters-widget__filter' ) - .attr( 'class', 'jetpack-search-filters-widget__filter' ) - .addClass( 'is-' + selectVal ); - - generateFilterTitlePlaceholder( getContainer( this ) ); - - trackAndBumpMCStats( 'changed_filter_type', eventArgs ); - } ); - - // enable showing sort controls only if showing search box is enabled - widget.on( 'change', '.jetpack-search-filters-widget__search-box-enabled', function() { - var checkbox = $( this ), - checkboxVal = checkbox.is(':checked'), - filterParent = checkbox.closest( '.jetpack-search-filters-widget' ), - sortControl = filterParent.find( '.jetpack-search-filters-widget__sort-controls-enabled' ); - - filterParent.toggleClass( 'hide-post-types' ); - - if ( checkboxVal ) { - sortControl.removeAttr( 'disabled' ); - trackAndBumpMCStats( 'enabled_search_box', args.tracksEventData ); - } else { - sortControl.prop( 'checked', false ); - sortControl.prop( 'disabled', true ); - trackAndBumpMCStats( 'disabled_search_box', args.tracksEventData ); - } - } ); - - widget.on( 'change', '.jetpack-search-filters-widget__sort-controls-enabled', function() { - if ( $( this ).is( ':checked' ) ) { - trackAndBumpMCStats( 'enabled_sort_controls', args.tracksEventData ); - } else { - trackAndBumpMCStats( 'disabled_sort_controls', args.tracksEventData ); - } - } ); - - widget.on( 'click', '.jetpack-search-filters-widget__post-types-select input[type="checkbox"]', function( e ) { - var t = $( this ); - var siblingsChecked = t.closest( '.jetpack-search-filters-widget' ) - .find( '.jetpack-search-filters-widget__post-types-select input[type="checkbox"]:checked' ); - - if ( 0 === siblingsChecked.length ) { - e.preventDefault(); - e.stopPropagation(); - - trackAndBumpMCStats( 'attempted_no_post_types', args.tracksEventData ); - } - } ); - - widget.on( 'change', '.jetpack-search-filters-widget__post-types-select input[type="checkbox"]', function() { - var t = $( this ); - var eventArgs = { - is_customizer: args.tracksEventData.is_customizer, - post_type: t.val() - }; - - if ( wp && wp.customize ) { - wp.customize.state( 'saved' ).set( false ); - } - - if ( t.is( ':checked' ) ) { - trackAndBumpMCStats( 'added_post_type', eventArgs ); - } else { - trackAndBumpMCStats( 'removed_post_type', eventArgs ); - } - } ); - - widget.on( 'change', '.jetpack-search-filters-widget__sort-order', function() { - var eventArgs = { - is_customizer: args.tracksEventData.is_customizer - }; - - eventArgs.order = $( this ).val(); - - if ( wp && wp.customize ) { - wp.customize.state( 'saved' ).set( false ); - } - - trackAndBumpMCStats( 'changed_sort_order', eventArgs ); - } ); - - widget.on( 'change', '.jetpack-search-filters-widget__taxonomy-select select', function() { - var eventArgs = { - is_customizer: args.tracksEventData.is_customizer - }; - - eventArgs.taxonomy = $( this ).val(); - - generateFilterTitlePlaceholder( getContainer( this ) ); - - if ( wp && wp.customize ) { - wp.customize.state( 'saved' ).set( false ); - } - - trackAndBumpMCStats( 'changed_taxonomy', eventArgs ); - } ); - - widget.on( 'change', 'select.date-field-select', function() { - var eventArgs = { - is_customizer: args.tracksEventData.is_customizer - }; - - eventArgs.field = $( this ).val(); - - generateFilterTitlePlaceholder( getContainer( this ) ); - - if ( wp && wp.customize ) { - wp.customize.state( 'saved' ).set( false ); - } - - trackAndBumpMCStats( 'changed_date_field', eventArgs ); - } ); - - widget.on( 'change', 'select.date-interval-select', function() { - var eventArgs = { - is_customizer: args.tracksEventData.is_customizer - }; - - eventArgs.interval = $( this ).val(); - - generateFilterTitlePlaceholder( getContainer( this ) ); - - if ( wp && wp.customize ) { - wp.customize.state( 'saved' ).set( false ); - } - - trackAndBumpMCStats( 'changed_date_interval', eventArgs ); - } ); - - widget.on( 'change', 'input.filter-count', function() { - var eventArgs = { - is_customizer: args.tracksEventData.is_customizer - }; - - eventArgs.count = $( this ).val(); - - if ( wp && wp.customize ) { - wp.customize.state( 'saved' ).set( false ); - } - - trackAndBumpMCStats( 'changed_filter_count', eventArgs ); - } ); - - // add filter button - widget.on( 'click', '.jetpack-search-filters-widget__add-filter', function( e ) { - e.preventDefault(); - - var filtersContainer = $( this ) - .closest( '.jetpack-search-filters-widget' ) - .find( '.jetpack-search-filters-widget__filters' ); - - addFilter( filtersContainer, { - type: 'taxonomy', - taxonomy: '', - post_type: '', - field: '', - interval: '', - count: defaultFilterCount, - name_placeholder: '', - name: '' - } ); - - if ( wp && wp.customize ) { - wp.customize.state( 'saved' ).set( false ); - } - - // Trigger change event to let legacy widget admin know the widget state is "dirty" - filtersContainer - .find( '.jetpack-search-filters-widget__filter' ) - .find( 'input, textarea, select' ) - .change(); - - trackAndBumpMCStats( 'added_filter', args.tracksEventData ); - } ); - - widget.on( 'click', '.jetpack-search-filters-widget__controls .delete', function( e ) { - e.preventDefault(); - var filter = $( this ).closest( '.jetpack-search-filters-widget__filter' ), - eventArgs = { - is_customizer: args.tracksEventData.is_customizer - }; - - eventArgs.type = filter.find( '.filter-select' ).val(); - - switch ( eventArgs.type ) { - case 'taxonomy': - eventArgs.taxonomy = filter.find( '.jetpack-search-filters-widget__taxonomy-select select' ).val(); - break; - case 'date_histogram': - eventArgs.dateField = filter.find( '.jetpack-search-filters-widget__date-histogram-select:first select' ).val(); - eventArgs.dateInterval = filter.find( '.jetpack-search-filters-widget__date-histogram-select:nth-child( 2 ) select' ).val(); - break; - } - - eventArgs.filterCount = filter.find( '.filter-count' ).val(); - - trackAndBumpMCStats( 'deleted_filter', eventArgs ); - - filter.find( 'input, textarea, select' ).change(); - filter.remove(); - - if ( wp && wp.customize ) { - wp.customize.state( 'saved' ).set( false ); - } - } ); - - // make the filters sortable - $( '.jetpack-search-filters-widget__filters' ).sortable( { - placeholder: 'jetpack-search-filters-widget__filter-placeholder', - axis: 'y', - revert: true, - cancel: 'input,textarea,button,select,option,.jetpack-search-filters-widget__controls a', - change: function() { - if ( wp && wp.customize ) { - wp.customize.state( 'saved' ).set( false ); - } - }, - update: function( e, ui ) { - $( ui.item ).find( 'input, textarea, select' ).change(); - } - } ) - .disableSelection(); - }; - - // When widgets are updated, remove and re-add listeners - $( document ).on( 'widget-updated widget-added', function( e, widget ) { - var idBase = $( widget ).find('.id_base').val(), - isJetpackSearch = ( idBase && ( 'jetpack-search-filters' === idBase ) ); - - if ( ! isJetpackSearch ) { - return; - } - - // Intentionally not tracking widget additions and updates here as these events - // seem noisy in the customizer. We'll track those via PHP. - - widget.off( 'change', '.filter-select' ); - widget.off( 'click', '.jetpack-search-filters-widget__controls .delete' ); - widget.off( 'change', '.jetpack-search-filters-widget__use-filters' ); - widget.off( 'change', '.jetpack-search-filters-widget__search-box-enabled' ); - widget.off( 'change', '.jetpack-search-filters-widget__sort-controls-enabled' ); - widget.off( 'change', '.jetpack-search-filters-widget__sort-controls-enabled' ); - widget.off( 'change', '.jetpack-search-filters-widget__post-type-selector' ); - widget.off( 'change', '.jetpack-search-filters-widget__sort-order' ); - widget.off( 'change', '.jetpack-search-filters-widget__taxonomy-select' ); - widget.off( 'change', '.jetpack-search-filters-widget__date-histogram-select:first select' ); - widget.off( 'change', '.jetpack-search-filters-widget__date-histogram-select:eq(1) select' ); - widget.off( 'click', '.jetpack-search-filters-widget__post-types-select input[type="checkbox"]' ); - widget.off( 'click', '.jetpack-search-filters-widget__add-filter'); - - setListeners( widget ); - } ); - - /** - * This function will fire both a Tracks and MC stat. - * - * Tracks: Will be prefixed by 'jetpack_widget_search_' and use underscores. - * MC: Will not be prefixed, and will use dashes. - * - * Logic borrowed from `idc-notice.js`. - * - * @param eventName string - * @param extraProps object - */ - function trackAndBumpMCStats( eventName, extraProps ) { - if ( 'undefined' === typeof extraProps || 'object' !== typeof extraProps ) { - extraProps = {}; - } - - if ( eventName && eventName.length && 'undefined' !== typeof analytics && analytics.tracks && analytics.mc ) { - // Format for Tracks - eventName = eventName.replace( /-/g, '_' ); - eventName = eventName.indexOf( 'jetpack_widget_search_' ) !== 0 ? 'jetpack_widget_search_' + eventName : eventName; - analytics.tracks.recordEvent( eventName, extraProps ); - - // Now format for MC stats - eventName = eventName.replace( 'jetpack_widget_search_', '' ); - eventName = eventName.replace( /_/g, '-' ); - analytics.mc.bumpStat( 'jetpack-search-widget', eventName ); - } - } -} )( jQuery, jetpack_search_filter_admin ); diff --git a/plugins/jetpack/modules/widgets/search/js/search-widget.js b/plugins/jetpack/modules/widgets/search/js/search-widget.js deleted file mode 100644 index 0f47e8e8..00000000 --- a/plugins/jetpack/modules/widgets/search/js/search-widget.js +++ /dev/null @@ -1,47 +0,0 @@ -var jetpackSearchModule = function () { - var i, - j, - checkboxes, - filter_list = document.querySelectorAll( '.jetpack-search-filters-widget__filter-list' ); - - for ( i = 0; i < filter_list.length; i++ ) { - filter_list[ i ].addEventListener( 'click', function ( event ) { - var target = event.target; - var precedingCheckbox; - var nextAnchor; - - // If the target is an anchor, we want to toggle the checkbox. - if ( target.nodeName && 'a' === target.nodeName.toLowerCase() ) { - precedingCheckbox = target.previousElementSibling; - if ( - precedingCheckbox && - precedingCheckbox.type && - 'checkbox' === precedingCheckbox.type - ) { - precedingCheckbox.checked = ! precedingCheckbox.checked; - } - } - - // If the target is a checkbox, we want to navigate. - if ( target.type && 'checkbox' === target.type ) { - nextAnchor = target.nextElementSibling; - if ( nextAnchor && 'a' === nextAnchor.nodeName.toLowerCase() ) { - window.location.href = nextAnchor.getAttribute( 'href' ); - } - } - } ); - - // Enable checkboxes now that we're setup. - checkboxes = filter_list[ i ].querySelectorAll( 'input[type="checkbox"]' ); - for ( j = 0; j < checkboxes.length; j++ ) { - checkboxes[ j ].disabled = false; - checkboxes[ j ].style.cursor = 'inherit'; - } - } -}; - -if ( document.readyState === 'interactive' || document.readyState === 'complete' ) { - jetpackSearchModule(); -} else { - document.addEventListener( 'DOMContentLoaded', jetpackSearchModule ); -} diff --git a/plugins/jetpack/modules/widgets/simple-payments.php b/plugins/jetpack/modules/widgets/simple-payments.php index 6c721f91..4662d9d2 100644 --- a/plugins/jetpack/modules/widgets/simple-payments.php +++ b/plugins/jetpack/modules/widgets/simple-payments.php @@ -1,4 +1,4 @@ -<?php +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName use Automattic\Jetpack\Tracking; /** @@ -17,6 +17,8 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { class Jetpack_Simple_Payments_Widget extends WP_Widget { /** * Currencies should be supported by PayPal: + * + * @var array $supported_currency_list * @link https://developer.paypal.com/docs/api/reference/currency-codes/ * * List has to be in sync with list at the block's client side and API's backend side: @@ -58,7 +60,7 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { /** * Constructor. */ - function __construct() { + public function __construct() { parent::__construct( 'jetpack_simple_payments_widget', /** This filter is documented in modules/widgets/facebook-likebox.php */ @@ -75,7 +77,7 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_styles' ) ); } - $jetpack_simple_payments = Jetpack_Simple_Payments::getInstance(); + $jetpack_simple_payments = Jetpack_Simple_Payments::get_instance(); if ( is_customize_preview() && $jetpack_simple_payments->is_enabled_jetpack_simple_payments() ) { add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); @@ -138,29 +140,54 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { * @param array $nonces Array of nonces. * @return array $nonces Modified array of nonces. */ - function filter_nonces( $nonces ) { + public function filter_nonces( $nonces ) { $nonces['customize-jetpack-simple-payments'] = wp_create_nonce( 'customize-jetpack-simple-payments' ); return $nonces; } - function enqueue_style() { + /** + * Enqueue styles. + */ + public function enqueue_style() { wp_enqueue_style( 'jetpack-simple-payments-widget-style', plugins_url( 'simple-payments/style.css', __FILE__ ), array(), '20180518' ); } - function admin_enqueue_styles() { - wp_enqueue_style( 'jetpack-simple-payments-widget-customizer', plugins_url( 'simple-payments/customizer.css', __FILE__ ) ); + /** + * Enqueue admin styles. + */ + public function admin_enqueue_styles() { + wp_enqueue_style( + 'jetpack-simple-payments-widget-customizer', + plugins_url( 'simple-payments/customizer.css', __FILE__ ), + array(), + JETPACK__VERSION + ); } - function admin_enqueue_scripts() { + /** + * Enqueue admin scripts. + */ + public function admin_enqueue_scripts() { wp_enqueue_media(); - wp_enqueue_script( 'jetpack-simple-payments-widget-customizer', plugins_url( '/simple-payments/customizer.js', __FILE__ ), array( 'jquery' ), false, true ); + wp_enqueue_script( + 'jetpack-simple-payments-widget-customizer', + plugins_url( '/simple-payments/customizer.js', __FILE__ ), + array( 'jquery' ), + JETPACK__VERSION, + true + ); wp_localize_script( - 'jetpack-simple-payments-widget-customizer', 'jpSimplePaymentsStrings', array( + 'jetpack-simple-payments-widget-customizer', + 'jpSimplePaymentsStrings', + array( 'deleteConfirmation' => __( 'Are you sure you want to delete this item? It will be disabled and removed from all locations where it currently appears.', 'jetpack' ), ) ); } + /** + * Get payment buttons. + */ public function ajax_get_payment_buttons() { if ( ! check_ajax_referer( 'customize-jetpack-simple-payments', 'customize-jetpack-simple-payments-nonce', false ) ) { wp_send_json_error( 'bad_nonce', 400 ); @@ -184,11 +211,16 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { ) ); - $formatted_products = array_map( array( $this, 'format_product_post_for_ajax_reponse' ), $product_posts ); + $formatted_products = array_map( array( $this, 'format_product_post_for_ajax_reponse' ), $product_posts ); - wp_send_json_success( $formatted_products ); + wp_send_json_success( $formatted_products ); } + /** + * Format product_post object. + * + * @param object $product_post - info about the post the product is on. + */ public function format_product_post_for_ajax_reponse( $product_post ) { return array( 'ID' => $product_post->ID, @@ -196,6 +228,9 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { ); } + /** + * Handle saving the simple payments widget. + */ public function ajax_save_payment_button() { if ( ! check_ajax_referer( 'customize-jetpack-simple-payments', 'customize-jetpack-simple-payments-nonce', false ) ) { wp_send_json_error( 'bad_nonce', 400 ); @@ -214,7 +249,7 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { wp_send_json_error( 'missing_params', 400 ); } - $params = wp_unslash( $_POST['params'] ); + $params = wp_unslash( $_POST['params'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Manually validated by validate_ajax_params(). $errors = $this->validate_ajax_params( $params ); if ( ! empty( $errors->errors ) ) { wp_send_json_error( $errors ); @@ -266,6 +301,9 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { ); } + /** + * Handle deleting the simple payment widget. + */ public function ajax_delete_payment_button() { if ( ! check_ajax_referer( 'customize-jetpack-simple-payments', 'customize-jetpack-simple-payments-nonce', false ) ) { wp_send_json_error( 'bad_nonce', 400 ); @@ -279,7 +317,7 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { wp_send_json_error( 'missing_params', 400 ); } - $params = wp_unslash( $_POST['params'] ); + $params = wp_unslash( $_POST['params'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Manually validated just below. $illegal_params = array_diff( array_keys( $params ), array( 'product_post_id' ) ); if ( ! empty( $illegal_params ) ) { wp_send_json_error( 'illegal_params', 400 ); @@ -316,6 +354,11 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { return isset( $parts[1] ) ? strlen( $parts[1] ) : 0; } + /** + * Validate ajax parameters. + * + * @param array $params - the parameters. + */ public function validate_ajax_params( $params ) { $errors = new WP_Error(); @@ -335,7 +378,7 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { // Japan's Yen is the only supported currency with a zero decimal precision. $precision = strtoupper( $params['currency'] ) === 'JPY' ? 0 : 2; $price_decimal_places = $this->get_decimal_places( $params['price'] ); - if ( is_null( $price_decimal_places ) || $price_decimal_places > $precision ) { + if ( $price_decimal_places === null || $price_decimal_places > $precision ) { $errors->add( 'price', __( 'Invalid price', 'jetpack' ) ); } @@ -346,7 +389,10 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { return $errors; } - function get_first_product_id() { + /** + * Get the id of the first product. + */ + public function get_first_product_id() { $product_posts = get_posts( array( 'numberposts' => 1, @@ -367,37 +413,37 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { * @param array $args Widget arguments. * @param array $instance Saved values from database. */ - function widget( $args, $instance ) { + public function widget( $args, $instance ) { $instance = wp_parse_args( $instance, $this->defaults() ); - echo $args['before_widget']; + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped /** This filter is documented in core/src/wp-includes/default-widgets.php */ $title = apply_filters( 'widget_title', $instance['title'] ); if ( ! empty( $title ) ) { - echo $args['before_title'] . $title . $args['after_title']; + echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } echo '<div class="jetpack-simple-payments-content">'; - if ( ! empty( $instance['form_action'] ) && in_array( $instance['form_action'], array( 'add', 'edit' ) ) && is_customize_preview() ) { - require( dirname( __FILE__ ) . '/simple-payments/widget.php' ); + if ( ! empty( $instance['form_action'] ) && in_array( $instance['form_action'], array( 'add', 'edit' ), true ) && is_customize_preview() ) { + require __DIR__ . '/simple-payments/widget.php'; } else { - $jsp = Jetpack_Simple_Payments::getInstance(); + $jsp = Jetpack_Simple_Payments::get_instance(); $simple_payments_button = $jsp->parse_shortcode( array( 'id' => $instance['product_post_id'], ) ); - if ( ! is_null( $simple_payments_button ) || is_customize_preview() ) { - echo $simple_payments_button; + if ( $simple_payments_button !== null || is_customize_preview() ) { + echo $simple_payments_button; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } } echo '</div><!--simple-payments-->'; - echo $args['after_widget']; + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped /** This action is already documented in modules/widgets/gravatar-profile.php */ do_action( 'jetpack_stats_extra', 'widget_view', 'simple_payments' ); @@ -406,9 +452,9 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { /** * Gets the latests field value from either the old instance or the new instance. * - * @param array $mixed Array of values for the new form instance. - * @param array $mixed Array of values for the old form instance. - * @return mixed $mixed Field value. + * @param array $new_instance mixed Array of values for the new form instance. + * @param array $old_instance mixed Array of values for the old form instance. + * @param mixed $field mixed Field value. */ private function get_latest_field_value( $new_instance, $old_instance, $field ) { return ! empty( $new_instance[ $field ] ) @@ -420,7 +466,7 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { * Gets the product fields from the product post. If no post found * it returns the default values. * - * @param int Product Post ID. + * @param int $product_post_id Product Post ID. * @return array $fields Product Fields from the Product Post. */ private function get_product_from_post( $product_post_id ) { @@ -448,9 +494,9 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { /** * Record a Track event and bump a MC stat. * - * @param string $stat_name - * @param string $event_action - * @param array $event_properties + * @param string $stat_name - the name of the stat. + * @param string $event_action - the action we're recording. + * @param array $event_properties - proprties of the event. */ private function record_event( $stat_name, $event_action, $event_properties = array() ) { $current_user = wp_get_current_user(); @@ -482,9 +528,9 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { * * @return array Updated safe values to be saved. */ - function update( $new_instance, $old_instance ) { + public function update( $new_instance, $old_instance ) { $defaults = $this->defaults(); - //do not overrite `product_post_id` for `$new_instance` with the defaults + // do not overrite `product_post_id` for `$new_instance` with the defaults. $new_instance = wp_parse_args( $new_instance, array_diff_key( $defaults, array( 'product_post_id' => 0 ) ) ); $old_instance = wp_parse_args( $old_instance, $defaults ); @@ -495,11 +541,11 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { ); if ( strcmp( $new_instance['form_action'], $old_instance['form_action'] ) !== 0 ) { - if ( $new_instance['form_action'] == 'edit' ) { + if ( 'edit' === $new_instance['form_action'] ) { return array_merge( $this->get_product_from_post( (int) $old_instance['product_post_id'] ), $required_widget_props ); } - if ( $new_instance['form_action'] == 'clear' ) { + if ( 'clear' === $new_instance['form_action'] ) { return array_merge( $this->defaults(), $required_widget_props ); } } @@ -511,7 +557,8 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { : $defaults['form_product_email']; return array_merge( - $required_widget_props, array( + $required_widget_props, + array( 'form_product_id' => (int) $new_instance['form_product_id'], 'form_product_title' => sanitize_text_field( $new_instance['form_product_title'] ), 'form_product_description' => sanitize_text_field( $new_instance['form_product_description'] ), @@ -532,19 +579,19 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { * * @param array $instance Previously saved values from database. */ - function form( $instance ) { - $jetpack_simple_payments = Jetpack_Simple_Payments::getInstance(); + public function form( $instance ) { + $jetpack_simple_payments = Jetpack_Simple_Payments::get_instance(); if ( ! method_exists( $jetpack_simple_payments, 'is_enabled_jetpack_simple_payments' ) ) { return; } if ( ! $jetpack_simple_payments->is_enabled_jetpack_simple_payments() ) { - require dirname( __FILE__ ) . '/simple-payments/admin-warning.php'; + require __DIR__ . '/simple-payments/admin-warning.php'; return; } $instance = wp_parse_args( $instance, $this->defaults() ); - $product_posts = get_posts( + $product_posts = get_posts( // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable array( 'numberposts' => 100, 'orderby' => 'date', @@ -553,17 +600,19 @@ if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) { ) ); - require dirname( __FILE__ ) . '/simple-payments/form.php'; + require __DIR__ . '/simple-payments/form.php'; } } - // Register Jetpack_Simple_Payments_Widget widget. + /** + * Register Jetpack_Simple_Payments_Widget widget. + */ function register_widget_jetpack_simple_payments() { if ( ! class_exists( 'Jetpack_Simple_Payments' ) ) { return; } - $jetpack_simple_payments = Jetpack_Simple_Payments::getInstance(); + $jetpack_simple_payments = Jetpack_Simple_Payments::get_instance(); if ( ! $jetpack_simple_payments->is_enabled_jetpack_simple_payments() ) { return; } diff --git a/plugins/jetpack/modules/widgets/simple-payments/admin-warning.php b/plugins/jetpack/modules/widgets/simple-payments/admin-warning.php index 8e4690db..4cf81728 100644 --- a/plugins/jetpack/modules/widgets/simple-payments/admin-warning.php +++ b/plugins/jetpack/modules/widgets/simple-payments/admin-warning.php @@ -1,16 +1,29 @@ <div class='jetpack-simple-payments-disabled-error'> <p> <?php + /** + * Show error and help if Pay with PayPal is disabled. + * + * @package automattic/jetpack + */ + $support_url = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'https://wordpress.com/support/pay-with-paypal/' : 'https://jetpack.com/support/pay-with-paypal/'; printf( wp_kses( + // translators: variable is a link to the support page. __( 'Your plan doesn\'t include Pay with PayPal. <a href="%s" rel="noopener noreferrer" target="_blank">Learn more and upgrade</a>.', 'jetpack' ), - array( 'a' => array( 'href' => array(), 'rel' => array(), 'target' => array() ) ) + array( + 'a' => array( + 'href' => array(), + 'rel' => array(), + 'target' => array(), + ), + ) ), esc_url( $support_url ) ); - ?> + ?> </p> </div> diff --git a/plugins/jetpack/modules/widgets/simple-payments/form.php b/plugins/jetpack/modules/widgets/simple-payments/form.php index 41c0971e..ae1dbdde 100644 --- a/plugins/jetpack/modules/widgets/simple-payments/form.php +++ b/plugins/jetpack/modules/widgets/simple-payments/form.php @@ -3,6 +3,7 @@ * Display the Pay with PayPal Form. * * @package automattic/jetpack + * @phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable */ ?> @@ -17,7 +18,13 @@ name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" /> </p> -<p class="jetpack-simple-payments-products-fieldset" <?php if ( empty( $product_posts ) ) { echo 'style="display:none;"'; } ?>> +<p class="jetpack-simple-payments-products-fieldset" +<?php +if ( empty( $product_posts ) ) { + echo 'style="display:none;"'; +} +?> +> <label for="<?php echo esc_attr( $this->get_field_id( 'product_post_id' ) ); ?>"> <?php esc_html_e( 'Select a Pay with PayPal button:', 'jetpack' ); ?> </label> @@ -33,7 +40,13 @@ </select> </p> <?php if ( is_customize_preview() ) { ?> -<p class="jetpack-simple-payments-products-warning" <?php if ( ! empty( $product_posts ) ) { echo 'style="display:none;"'; } ?>> +<p class="jetpack-simple-payments-products-warning" + <?php + if ( ! empty( $product_posts ) ) { + echo 'style="display:none;"'; + } + ?> +> <?php esc_html_e( "Looks like you don't have any products. You can create one using the Add New button below.", 'jetpack' ); ?> </p> <p> @@ -90,10 +103,22 @@ </p> <div class="jetpack-simple-payments-image-fieldset"> <label><?php esc_html_e( 'Product image', 'jetpack' ); ?></label> - <div class="placeholder" <?php if ( ! empty( $instance['form_product_image_id'] ) ) echo 'style="display:none;"'; ?>> + <div class="placeholder" + <?php + if ( ! empty( $instance['form_product_image_id'] ) ) { + echo 'style="display:none;"'; + } + ?> + > <?php esc_html_e( 'Select an image', 'jetpack' ); ?> </div> - <div class="jetpack-simple-payments-image" <?php if ( empty( $instance['form_product_image_id'] ) ) echo 'style="display:none;"'; ?>> + <div class="jetpack-simple-payments-image" + <?php + if ( empty( $instance['form_product_image_id'] ) ) { + echo 'style="display:none;"'; + } + ?> + > <img src="<?php echo esc_url( $instance['form_product_image_src'] ); ?>" /> <button class="button jetpack-simple-payments-remove-image"><?php esc_html_e( 'Remove image', 'jetpack' ); ?></button> </div> diff --git a/plugins/jetpack/modules/widgets/simple-payments/widget.php b/plugins/jetpack/modules/widgets/simple-payments/widget.php index 46ca3382..24b1cc2c 100644 --- a/plugins/jetpack/modules/widgets/simple-payments/widget.php +++ b/plugins/jetpack/modules/widgets/simple-payments/widget.php @@ -3,12 +3,19 @@ * Display the Pay with PayPal Widget. * * @package automattic/jetpack + * @phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable */ ?> <div class='jetpack-simple-payments-wrapper'> <div class='jetpack-simple-payments-product'> - <div class='jetpack-simple-payments-product-image' <?php if ( empty( $instance['form_product_image_id'] ) ) echo 'style="display:none;"'; ?>> + <div class='jetpack-simple-payments-product-image' + <?php + if ( empty( $instance['form_product_image_id'] ) ) { + echo 'style="display:none;"'; + } + ?> + > <div class='jetpack-simple-payments-image'> <?php echo wp_get_attachment_image( $instance['form_product_image_id'], 'full' ); ?> </div> diff --git a/plugins/jetpack/modules/widgets/top-posts.php b/plugins/jetpack/modules/widgets/top-posts.php index 4f3fe912..5dd7b266 100644 --- a/plugins/jetpack/modules/widgets/top-posts.php +++ b/plugins/jetpack/modules/widgets/top-posts.php @@ -281,6 +281,7 @@ class Jetpack_Top_Posts_Widget extends WP_Widget { if ( false === $title ) { $title = $this->default_title; } + /** This filter is documented in core/src/wp-includes/default-widgets.php */ $title = apply_filters( 'widget_title', $title ); @@ -315,6 +316,7 @@ class Jetpack_Top_Posts_Widget extends WP_Widget { $display = 'text'; } + $get_image_options = array(); if ( 'text' !== $display ) { $get_image_options = array( 'fallback_to_avatars' => true, @@ -352,42 +354,28 @@ class Jetpack_Top_Posts_Widget extends WP_Widget { $posts = $this->get_by_views( $count, $args, $types ); } - if ( ! $posts ) { - $posts = $this->get_fallback_posts( $count, $types ); - } - echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + if ( ! empty( $title ) ) { echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } + /* + * If we have no posts, add some fallback posts + * and display a fallback message for admins. + */ if ( ! $posts ) { - $link = esc_url( Redirect::get_url( 'jetpack-support-getting-more-views-and-traffic' ) ); - if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { - $link = 'https://en.support.wordpress.com/getting-more-site-traffic/'; - } - if ( current_user_can( 'edit_theme_options' ) ) { - echo '<p>' . sprintf( - wp_kses( - /* Translators: Placeholder: link to the Jetpack support article. */ - __( 'There are no posts to display. <a href="%s" target="_blank">Want more traffic?</a>', 'jetpack' ), - array( - 'a' => array( - 'href' => array(), - 'target' => array(), - ), - ) - ), - esc_url( $link ) - ) . '</p>'; + echo $this->fallback_message(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } - echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - - return; + $posts = $this->get_fallback_posts( $count, $types ); } + /* + * Display our posts. + */ + /** * Filter the layout of the Top Posts Widget * @@ -395,15 +383,13 @@ class Jetpack_Top_Posts_Widget extends WP_Widget { * * @since 6.4.0 * - * @param string $layout layout of the Top Posts Widget (empty string) - * @param array $posts IDs of the posts to be displayed - * @param array $display Display option from widget form + * @param string $layout layout of the Top Posts Widget (empty string). + * @param array $posts IDs of the posts to be displayed. + * @param array $display Display option from widget form. */ $layout = apply_filters( 'jetpack_top_posts_widget_layout', '', $posts, $display ); if ( ! empty( $layout ) ) { echo $layout; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - return; } switch ( $display ) { @@ -438,15 +424,13 @@ class Jetpack_Top_Posts_Widget extends WP_Widget { $post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$width,$height" ) ); } } - unset( $post ); if ( 'grid' === $display ) { echo "<div class='widgets-grid-layout no-grav'>\n"; - foreach ( $posts as $post ) : - ?> - <div class="widget-grid-view-image"> - <?php + foreach ( $posts as $post ) { + echo '<div class="widget-grid-view-image">'; + /** * Fires before each Top Post result, inside <li>. * @@ -490,17 +474,15 @@ class Jetpack_Top_Posts_Widget extends WP_Widget { * @param string $post['post_id'] Post ID. */ do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); - ?> - </div> - <?php - endforeach; + + echo '</div>'; + } echo "</div>\n"; } else { echo "<ul class='widgets-list-layout no-grav'>\n"; - foreach ( $posts as $post ) : - ?> - <li> - <?php + foreach ( $posts as $post ) { + echo '<li>'; + /** This action is documented in modules/widgets/top-posts.php */ do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); @@ -526,19 +508,18 @@ class Jetpack_Top_Posts_Widget extends WP_Widget { /** This action is documented in modules/widgets/top-posts.php */ do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); - ?> - </li> - <?php - endforeach; + + echo '</li>'; + } echo "</ul>\n"; } break; default: echo '<ul>'; - foreach ( $posts as $post ) : - ?> - <li> - <?php + + foreach ( $posts as $post ) { + echo '<li>'; + /** This action is documented in modules/widgets/top-posts.php */ do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); @@ -554,17 +535,48 @@ class Jetpack_Top_Posts_Widget extends WP_Widget { /** This action is documented in modules/widgets/top-posts.php */ do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); - ?> - </li> - <?php - endforeach; + + echo '</li>'; + } + echo '</ul>'; + break; } echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** + * Display a message with recommendations when there are no recorded top posts. + * + * @return string $fallback_message + */ + private static function fallback_message() { + $link = esc_url( Redirect::get_url( 'jetpack-support-getting-more-views-and-traffic' ) ); + if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { + $link = 'https://en.support.wordpress.com/getting-more-site-traffic/'; + } + + $fallback_message = '<p>'; + $fallback_message .= sprintf( + wp_kses( + /* Translators: Placeholder: link to the Jetpack support article. */ + __( 'There are no popular posts to display. Instead, your visitors will see a list of your recent posts below. <a href="%s" target="_blank">Want more traffic?</a>', 'jetpack' ), + array( + 'a' => array( + 'href' => array(), + 'target' => array(), + ), + ) + ), + esc_url( $link ) + ); + $fallback_message .= '<p>'; + + return $fallback_message; + } + + /** * Widget default option values. */ public static function defaults() { @@ -678,10 +690,6 @@ class Jetpack_Top_Posts_Widget extends WP_Widget { * @return array */ public function get_fallback_posts( $count = 10, $types = array( 'post', 'page' ) ) { - if ( current_user_can( 'edit_theme_options' ) ) { - return array(); - } - $post_query = new WP_Query(); if ( ! is_array( $types ) || empty( $types ) ) { @@ -765,7 +773,7 @@ class Jetpack_Top_Posts_Widget extends WP_Widget { $posts[] = compact( 'title', 'permalink', 'post_id', 'post_type' ); $counter++; - if ( $counter == $count ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison + if ( $counter == $count ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual break; // only need to load and show x number of likes. } } diff --git a/plugins/jetpack/modules/widgets/wordpress-post-widget.php b/plugins/jetpack/modules/widgets/wordpress-post-widget.php index fb9ffa7a..3cbd6015 100644 --- a/plugins/jetpack/modules/widgets/wordpress-post-widget.php +++ b/plugins/jetpack/modules/widgets/wordpress-post-widget.php @@ -6,6 +6,9 @@ * Author: Brad Angelcyk, Kathryn Presner, Justin Shreve, Carolyn Sonnek * Author URI: https://automattic.com * License: GPL2 + * Text Domain: jetpack + * + * @package automattic/jetpack */ /** @@ -15,20 +18,22 @@ if ( ! defined( 'ABSPATH' ) ) { exit; } -require dirname( __FILE__ ) . '/wordpress-post-widget/class.jetpack-display-posts-widget-base.php'; -require dirname( __FILE__ ) . '/wordpress-post-widget/class.jetpack-display-posts-widget.php'; +require __DIR__ . '/wordpress-post-widget/class.jetpack-display-posts-widget-base.php'; +require __DIR__ . '/wordpress-post-widget/class.jetpack-display-posts-widget.php'; add_action( 'widgets_init', 'jetpack_display_posts_widget' ); +/** + * Registers widget Jetpack_Display_Posts_Widget + */ function jetpack_display_posts_widget() { register_widget( 'Jetpack_Display_Posts_Widget' ); } - /** * Cron tasks */ -add_filter( 'cron_schedules', 'jetpack_display_posts_widget_cron_intervals' ); +add_filter( 'cron_schedules', 'jetpack_display_posts_widget_cron_intervals' ); // phpcs:ignore WordPress.WP.CronInterval.CronSchedulesInterval /** * Adds 10 minute running interval to the cron schedules. @@ -56,6 +61,9 @@ function jetpack_display_posts_widget_cron_intervals( $current_schedules ) { * Execute the cron task */ add_action( 'jetpack_display_posts_widget_cron_update', 'jetpack_display_posts_update_cron_action' ); +/** + * Run the Jetpack_Display_Posts_Widget cron task. + */ function jetpack_display_posts_update_cron_action() { $widget = new Jetpack_Display_Posts_Widget(); $widget->cron_task(); @@ -70,7 +78,6 @@ function jetpack_display_posts_update_cron_action() { * `jetpack_activate_module_widgets` - Activate the cron when the Extra Sidebar widgets are activated. * * `activated_plugin` - Activate the cron when Jetpack gets activated. - * */ add_action( 'updating_jetpack_version', 'jetpack_display_posts_widget_conditionally_activate_cron' ); add_action( 'jetpack_activate_module_widgets', 'Jetpack_Display_Posts_Widget::activate_cron' ); @@ -89,6 +96,7 @@ function jetpack_conditionally_activate_cron_on_plugin_activation( $plugin_file_ /** * Activates the cron only when needed. + * * @see Jetpack_Display_Posts_Widget::should_cron_be_running */ function jetpack_display_posts_widget_conditionally_activate_cron() { @@ -104,7 +112,6 @@ function jetpack_display_posts_widget_conditionally_activate_cron() { * End of cron activation handling. */ - /** * Handle deactivation procedures where they are needed. * diff --git a/plugins/jetpack/modules/widgets/wordpress-post-widget/class.jetpack-display-posts-widget-base.php b/plugins/jetpack/modules/widgets/wordpress-post-widget/class.jetpack-display-posts-widget-base.php index e89df68e..0d9c3d67 100644 --- a/plugins/jetpack/modules/widgets/wordpress-post-widget/class.jetpack-display-posts-widget-base.php +++ b/plugins/jetpack/modules/widgets/wordpress-post-widget/class.jetpack-display-posts-widget-base.php @@ -1,6 +1,6 @@ -<?php +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -/* +/** * For back-compat, the final widget class must be named * Jetpack_Display_Posts_Widget. * @@ -16,21 +16,30 @@ * * That this widget is currently implemented as these two classes * is an implementation detail and should not be depended on :) + * + * phpcs:disable PEAR.NamingConventions.ValidClassName.Invalid */ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { + // phpcs:enable PEAR.NamingConventions.ValidClassName.Invalid + /** - * @var string Remote service API URL prefix. + * Remote service API URL prefix. + * + * @var string */ public $service_url = 'https://public-api.wordpress.com/rest/v1.1/'; + /** + * Jetpack_Display_Posts_Widget__Base constructor. + */ public function __construct() { parent::__construct( - // internal id + // Internal id. 'jetpack_display_posts_widget', /** This filter is documented in modules/widgets/facebook-likebox.php */ apply_filters( 'jetpack_widget_name', __( 'Display WordPress Posts', 'jetpack' ) ), array( - 'description' => __( 'Displays a list of recent posts from another WordPress.com or Jetpack-enabled blog.', 'jetpack' ), + 'description' => __( 'Displays a list of recent posts from another WordPress.com or Jetpack-enabled blog.', 'jetpack' ), 'customize_selective_refresh' => true, ) ); @@ -46,16 +55,20 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { * @since 4.0.0 */ public function enqueue_scripts() { - wp_enqueue_style( 'jetpack_display_posts_widget', plugins_url( 'style.css', __FILE__ ) ); + wp_enqueue_style( + 'jetpack_display_posts_widget', + plugins_url( 'style.css', __FILE__ ), + array(), + JETPACK__VERSION + ); } - - // DATA STORE: Must implement + // DATA STORE: Must implement. /** * Gets blog data from the cache. * - * @param string $site + * @param string $site Site. * * @return array|WP_Error */ @@ -70,14 +83,13 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { */ abstract public function update_instance( $site ); - - // WIDGET API + // WIDGET API. /** * Set up the widget display on the front end. * - * @param array $args - * @param array $instance + * @param array $args Widget args. + * @param array $instance Widget instance. */ public function widget( $args, $instance ) { /** This action is documented in modules/widgets/gravatar-profile.php */ @@ -97,17 +109,17 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { } $content .= $args['after_widget']; - echo $content; + echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped return; } $data = $this->get_blog_data( $instance['url'] ); - // check for errors + // Check for errors. if ( is_wp_error( $data ) || empty( $data['site_info']['data'] ) ) { $content .= '<p>' . __( 'Cannot load blog information at this time.', 'jetpack' ) . '</p>'; $content .= $args['after_widget']; - echo $content; + echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped return; } @@ -117,8 +129,7 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { /** This filter is documented in core/src/wp-includes/default-widgets.php */ $instance['title'] = apply_filters( 'widget_title', $instance['title'] ); $content .= $args['before_title'] . $instance['title'] . ': ' . $site_info->name . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - } - else { + } else { $content .= $args['before_title'] . esc_html( $site_info->name ) . $args['after_title']; } @@ -129,7 +140,7 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { $content .= '</div><!-- .jetpack-display-remote-posts -->'; $content .= $args['after_widget']; - echo $content; + echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped return; } @@ -146,11 +157,11 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { $post_title = ( $single_post['title'] ) ? $single_post['title'] : '( No Title )'; $target = ''; - if ( isset( $instance['open_in_new_window'] ) && $instance['open_in_new_window'] == true ) { + if ( isset( $instance['open_in_new_window'] ) && true === $instance['open_in_new_window'] ) { $target = ' target="_blank" rel="noopener"'; } $content .= '<h4><a href="' . esc_url( $single_post['url'] ) . '"' . $target . '>' . esc_html( $post_title ) . '</a></h4>' . "\n"; - if ( ( $instance['featured_image'] == true ) && ( ! empty ( $single_post['featured_image'] ) ) ) { + if ( ( true === $instance['featured_image'] ) && ( ! empty( $single_post['featured_image'] ) ) ) { $featured_image = $single_post['featured_image']; /** * Allows setting up custom Photon parameters to manipulate the image output in the Display Posts widget. @@ -164,10 +175,10 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { * @param array $args Array of Photon Parameters. */ $image_params = apply_filters( 'jetpack_display_posts_widget_image_params', array() ); - $content .= '<a title="' . esc_attr( $post_title ) . '" href="' . esc_url( $single_post['url'] ) . '"' . $target . '><img src="' . jetpack_photon_url( $featured_image, $image_params ) . '" alt="' . esc_attr( $post_title ) . '"/></a>'; + $content .= '<a title="' . esc_attr( $post_title ) . '" href="' . esc_url( $single_post['url'] ) . '"' . $target . '><img src="' . jetpack_photon_url( $featured_image, $image_params ) . '" alt="' . esc_attr( $post_title ) . '"/></a>'; } - if ( $instance['show_excerpts'] == true ) { + if ( true === $instance['show_excerpts'] ) { $content .= $single_post['excerpt']; } } @@ -184,7 +195,7 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { * * @param string $content Widget content. */ - echo apply_filters( 'jetpack_display_posts_widget_content', $content ); + echo apply_filters( 'jetpack_display_posts_widget_content', $content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** @@ -206,7 +217,6 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { $featured_image = ( isset( $instance['featured_image'] ) ) ? $instance['featured_image'] : false; $show_excerpts = ( isset( $instance['show_excerpts'] ) ) ? $instance['show_excerpts'] : false; - /** * Check if the widget instance has errors available. * @@ -221,15 +231,15 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { ?> <p> - <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'jetpack' ); ?></label> - <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> + <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> + <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> </p> <p> - <label for="<?php echo $this->get_field_id( 'url' ); ?>"><?php _e( 'Blog URL:', 'jetpack' ); ?></label> - <input class="widefat" id="<?php echo $this->get_field_id( 'url' ); ?>" name="<?php echo $this->get_field_name( 'url' ); ?>" type="text" value="<?php echo esc_attr( $url ); ?>" /> + <label for="<?php echo esc_attr( $this->get_field_id( 'url' ) ); ?>"><?php esc_html_e( 'Blog URL:', 'jetpack' ); ?></label> + <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'url' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'url' ) ); ?>" type="text" value="<?php echo esc_attr( $url ); ?>" /> <i> - <?php _e( "Enter a WordPress.com or Jetpack WordPress site URL.", 'jetpack' ); ?> + <?php esc_html_e( 'Enter a WordPress.com or Jetpack WordPress site URL.', 'jetpack' ); ?> </i> <?php /** @@ -240,32 +250,32 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { if ( empty( $url ) && ! preg_match( '/__i__|%i%/', $this->id ) ) { ?> <br /> - <i class="error-message"><?php echo __( 'You must specify a valid blog URL!', 'jetpack' ); ?></i> + <i class="error-message"><?php esc_html_e( 'You must specify a valid blog URL!', 'jetpack' ); ?></i> <?php } ?> </p> <p> - <label for="<?php echo $this->get_field_id( 'number_of_posts' ); ?>"><?php _e( 'Number of Posts to Display:', 'jetpack' ); ?></label> - <select name="<?php echo $this->get_field_name( 'number_of_posts' ); ?>"> + <label for="<?php echo esc_attr( $this->get_field_id( 'number_of_posts' ) ); ?>"><?php esc_html_e( 'Number of Posts to Display:', 'jetpack' ); ?></label> + <select name="<?php echo esc_attr( $this->get_field_name( 'number_of_posts' ) ); ?>"> <?php for ( $i = 1; $i <= 10; $i ++ ) { - echo '<option value="' . $i . '" ' . selected( $number_of_posts, $i ) . '>' . $i . '</option>'; + echo '<option value="' . esc_attr( $i ) . '" ' . selected( $number_of_posts, $i ) . '>' . esc_html( $i ) . '</option>'; } ?> </select> </p> <p> - <label for="<?php echo $this->get_field_id( 'open_in_new_window' ); ?>"><?php _e( 'Open links in new window/tab:', 'jetpack' ); ?></label> - <input type="checkbox" name="<?php echo $this->get_field_name( 'open_in_new_window' ); ?>" <?php checked( $open_in_new_window, 1 ); ?> /> + <label for="<?php echo esc_attr( $this->get_field_id( 'open_in_new_window' ) ); ?>"><?php esc_html_e( 'Open links in new window/tab:', 'jetpack' ); ?></label> + <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'open_in_new_window' ) ); ?>" <?php checked( $open_in_new_window, 1 ); ?> /> </p> <p> - <label for="<?php echo $this->get_field_id( 'featured_image' ); ?>"><?php _e( 'Show Featured Image:', 'jetpack' ); ?></label> - <input type="checkbox" name="<?php echo $this->get_field_name( 'featured_image' ); ?>" <?php checked( $featured_image, 1 ); ?> /> + <label for="<?php echo esc_attr( $this->get_field_id( 'featured_image' ) ); ?>"><?php esc_html_e( 'Show Featured Image:', 'jetpack' ); ?></label> + <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'featured_image' ) ); ?>" <?php checked( $featured_image, 1 ); ?> /> </p> <p> - <label for="<?php echo $this->get_field_id( 'show_excerpts' ); ?>"><?php _e( 'Show Excerpts:', 'jetpack' ); ?></label> - <input type="checkbox" name="<?php echo $this->get_field_name( 'show_excerpts' ); ?>" <?php checked( $show_excerpts, 1 ); ?> /> + <label for="<?php echo esc_attr( $this->get_field_id( 'show_excerpts' ) ); ?>"><?php esc_html_e( 'Show Excerpts:', 'jetpack' ); ?></label> + <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_excerpts' ) ); ?>" <?php checked( $show_excerpts, 1 ); ?> /> </p> <?php @@ -323,15 +333,20 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { } } - public function update( $new_instance, $old_instance ) { + /** + * Widget update function. + * + * @param array $new_instance New instance widget settings. + * @param array $old_instance Old instance widget settings. + */ + public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $instance = array(); - $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; - $instance['url'] = ( ! empty( $new_instance['url'] ) ) ? strip_tags( trim( $new_instance['url'] ) ) : ''; - $instance['url'] = preg_replace( "!^https?://!is", "", $instance['url'] ); + $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? wp_strip_all_tags( $new_instance['title'] ) : ''; + $instance['url'] = ( ! empty( $new_instance['url'] ) ) ? wp_strip_all_tags( trim( $new_instance['url'] ) ) : ''; + $instance['url'] = preg_replace( '!^https?://!is', '', $instance['url'] ); $instance['url'] = untrailingslashit( $instance['url'] ); - /** * Check if the URL should be with or without the www prefix before saving. */ @@ -366,8 +381,7 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { return $instance; } - - // DATA PROCESSING + // DATA PROCESSING. /** * Expiring transients have a name length maximum of 45 characters, @@ -443,14 +457,12 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { ); } - /** * Extract service response body from the request. */ $service_response_body = wp_remote_retrieve_body( $service_response ); - /** * No body has been set in the response. This should be pretty bad. */ @@ -470,7 +482,7 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { /** * If there is a problem with parsing the posts return an empty array. */ - if ( is_null( $parsed_data ) ) { + if ( $parsed_data === null ) { return new WP_Error( 'no_body', __( 'Invalid remote response.', 'jetpack' ), @@ -504,7 +516,7 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { */ public function fetch_site_info( $site ) { - $response = $this->fetch_service_endpoint( sprintf( '/sites/%s', urlencode( $site ) ) ); + $response = $this->fetch_service_endpoint( sprintf( '/sites/%s', rawurlencode( $site ) ) ); return $response; } @@ -629,7 +641,7 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { 'title' => $single_post->title ? $single_post->title : '', 'excerpt' => $single_post->excerpt ? $single_post->excerpt : '', 'featured_image' => $single_post->featured_image ? $single_post->featured_image : '', - 'url' => $single_post->URL, + 'url' => $single_post->URL, // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase ); /** @@ -659,8 +671,7 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { */ if ( ! empty( $original_data ) ) { $widget_data = $original_data; - } - else { + } else { $widget_data = array( 'site_info' => array( 'last_check' => null, @@ -673,7 +684,7 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { 'last_update' => null, 'error' => null, 'data' => array(), - ) + ), ); } @@ -685,7 +696,6 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { $site_info_raw_data = $this->fetch_site_info( $site ); $site_info_parsed_data = $this->parse_site_info_response( $site_info_raw_data ); - /** * If there is an error with the fetched site info, save the error and update the checked time. */ @@ -693,21 +703,18 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { $widget_data['site_info']['error'] = $site_info_parsed_data; return $widget_data; - } - /** - * If data is fetched successfully, update the data and set the proper time. - * - * Data is only updated if we have valid results. This is done this way so we can show - * something if external service is down. - * - */ - else { + } else { + /** + * If data is fetched successfully, update the data and set the proper time. + * + * Data is only updated if we have valid results. This is done this way so we can show + * something if external service is down. + */ $widget_data['site_info']['last_update'] = time(); $widget_data['site_info']['data'] = $site_info_parsed_data; $widget_data['site_info']['error'] = null; } - /** * If only site data is needed, return it here, don't fetch posts data. */ @@ -723,7 +730,6 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { $site_posts_raw_data = $this->fetch_posts_for_site( $site_info_parsed_data->ID ); $site_posts_parsed_data = $this->parse_posts_response( $site_posts_raw_data ); - /** * If there is an error with the fetched posts, save the error and update the checked time. */ @@ -731,15 +737,13 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { $widget_data['posts']['error'] = $site_posts_parsed_data; return $widget_data; - } - /** - * If data is fetched successfully, update the data and set the proper time. - * - * Data is only updated if we have valid results. This is done this way so we can show - * something if external service is down. - * - */ - else { + } else { + /** + * If data is fetched successfully, update the data and set the proper time. + * + * Data is only updated if we have valid results. This is done this way so we can show + * something if external service is down. + */ $widget_data['posts']['last_update'] = time(); $widget_data['posts']['data'] = $site_posts_parsed_data; $widget_data['posts']['error'] = null; @@ -763,7 +767,6 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { 'where' => '', ); - /** * When the cache result is an error. Usually when the cache is empty. * This is not an error case for now. @@ -801,14 +804,12 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { $extra_data = $blog_data[ $info_key ]['error']->get_error_data(); if ( is_array( $extra_data ) ) { $errors['debug'] = implode( '; ', $extra_data ); - } - else { + } else { $errors['debug'] = $extra_data; } break; - } - elseif ( is_array( $blog_data[ $info_key ]['error'] ) ) { + } elseif ( is_array( $blog_data[ $info_key ]['error'] ) ) { /** * In this case we don't have debug information, because * we have no way to know the format. The widget works with @@ -830,7 +831,7 @@ abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { /** * This is just to make method mocks in the unit tests easier. * - * @param string $url The URL to fetch + * @param string $url The URL to fetch. * @param array $args Optional. Request arguments. * * @return array|WP_Error diff --git a/plugins/jetpack/modules/widgets/wordpress-post-widget/class.jetpack-display-posts-widget.php b/plugins/jetpack/modules/widgets/wordpress-post-widget/class.jetpack-display-posts-widget.php index d0a155a2..10d66b7f 100644 --- a/plugins/jetpack/modules/widgets/wordpress-post-widget/class.jetpack-display-posts-widget.php +++ b/plugins/jetpack/modules/widgets/wordpress-post-widget/class.jetpack-display-posts-widget.php @@ -1,34 +1,36 @@ -<?php +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName use Automattic\Jetpack\Status; -/* +/** * Display a list of recent posts from a WordPress.com or Jetpack-enabled blog. */ - class Jetpack_Display_Posts_Widget extends Jetpack_Display_Posts_Widget__Base { /** - * @var string Widget options key prefix. + * Widget options key prefix. + * + * @var string */ public $widget_options_key_prefix = 'display_posts_site_data_'; /** - * @var string The name of the cron that will update widget data. + * The name of the cron that will update widget data. + * + * @var string */ public static $cron_name = 'jetpack_display_posts_widget_cron_update'; - - // DATA STORE + // DATA STORE. /** * Gets blog data from the cache. * - * @param string $site + * @param string $site Site. * * @return array|WP_Error */ public function get_blog_data( $site ) { - // load from cache, if nothing return an error + // Load from cache, if nothing return an error. $site_hash = $this->get_site_hash( $site ); $cached_data = $this->wp_get_option( $this->widget_options_key_prefix . $site_hash ); @@ -75,17 +77,21 @@ class Jetpack_Display_Posts_Widget extends Jetpack_Display_Posts_Widget__Base { */ if ( false === $instance_data ) { $this->wp_add_option( $option_key, $new_data ); - } - else { + } else { $this->wp_update_option( $option_key, $new_data ); } return $new_data; } + // WIDGET API. - // WIDGET API - + /** + * Widget update function. + * + * @param array $new_instance New instance widget settings. + * @param array $old_instance Old instance widget settings. + */ public function update( $new_instance, $old_instance ) { $instance = parent::update( $new_instance, $old_instance ); @@ -99,8 +105,7 @@ class Jetpack_Display_Posts_Widget extends Jetpack_Display_Posts_Widget__Base { return $instance; } - - // CRON + // CRON. /** * Activates widget update cron task. @@ -230,13 +235,12 @@ class Jetpack_Display_Posts_Widget extends Jetpack_Display_Posts_Widget__Base { } - - // MOCKABLES + // MOCKABLES. /** * This is just to make method mocks in the unit tests easier. * - * @param string $param Option key to get + * @param string $param Option key to get. * * @return mixed * @@ -249,8 +253,8 @@ class Jetpack_Display_Posts_Widget extends Jetpack_Display_Posts_Widget__Base { /** * This is just to make method mocks in the unit tests easier. * - * @param string $option_name Option name to be added - * @param mixed $option_value Option value + * @param string $option_name Option name to be added. + * @param mixed $option_value Option value. * * @return mixed * @@ -263,8 +267,8 @@ class Jetpack_Display_Posts_Widget extends Jetpack_Display_Posts_Widget__Base { /** * This is just to make method mocks in the unit tests easier. * - * @param string $option_name Option name to be updated - * @param mixed $option_value Option value + * @param string $option_name Option name to be updated. + * @param mixed $option_value Option value. * * @return mixed * |