diff --git a/projects/plugins/jetpack/_inc/lib/icalendar-reader.php b/projects/plugins/jetpack/_inc/lib/icalendar-reader.php index 8871f7077ad1..ed29771b5f8b 100644 --- a/projects/plugins/jetpack/_inc/lib/icalendar-reader.php +++ b/projects/plugins/jetpack/_inc/lib/icalendar-reader.php @@ -568,7 +568,7 @@ public function parse( $url = '' ) { // rewrite webcal: URI schem to HTTP. $url = preg_replace( '/^webcal/', 'http', $url ); // try to fetch. - $r = wp_remote_get( + $r = wp_safe_remote_get( $url, array( 'timeout' => 3, @@ -969,8 +969,7 @@ public function formatted_date( $event ) { if ( ! $all_day && $this->timezone ) { try { - $start_time = new DateTime( $event['DTSTART'] ); - $timezone_offset = $this->timezone->getOffset( $start_time ); + $timezone_offset = $this->timezone->getOffset( new DateTime( $event['DTSTART'] ) ); $start += $timezone_offset; if ( $end ) { @@ -1063,8 +1062,7 @@ function icalendar_get_events( $url = '', $count = 5 ) { * Find your calendar's address * https://support.google.com/calendar/bin/answer.py?hl=en&answer=37103 */ - $ical = new iCalendarReader(); - return $ical->get_events( $url, $count ); + return ( new iCalendarReader() )->get_events( $url, $count ); } /** @@ -1076,6 +1074,5 @@ function icalendar_get_events( $url = '', $count = 5 ) { * @return mixed bool|string false on failure, rendered HTML string on success. */ function icalendar_render_events( $url = '', $args = array() ) { - $ical = new iCalendarReader(); - return $ical->render( $url, $args ); + return ( new iCalendarReader() )->render( $url, $args ); } diff --git a/projects/plugins/jetpack/_inc/lib/tonesque.php b/projects/plugins/jetpack/_inc/lib/tonesque.php index d4bfbaf8fcd7..c12c84785434 100644 --- a/projects/plugins/jetpack/_inc/lib/tonesque.php +++ b/projects/plugins/jetpack/_inc/lib/tonesque.php @@ -2,11 +2,9 @@ /** * Tonesque * Grab an average color representation from an image. - * Author: Automattic, Matias Ventura - * Author URI: https://automattic.com/ - * License: GNU General Public License v2 or later - * License URI: https://www.gnu.org/licenses/gpl-2.0.html * + * @author Automattic + * @author Matias Ventura * @package automattic/jetpack */ @@ -14,7 +12,6 @@ * Color representation class. */ class Tonesque { - /** * Image URL. * @@ -65,7 +62,7 @@ public function __construct( $image_url ) { * * @param string $image_url Image URL. * - * @return object Image object. + * @return object|bool Image object or false if the image could not be loaded. */ public static function imagecreatefromurl( $image_url ) { $data = null; @@ -88,7 +85,7 @@ public static function imagecreatefromurl( $image_url ) { } if ( empty( $data ) ) { - $response = wp_remote_get( $image_url ); + $response = wp_safe_remote_get( $image_url ); if ( is_wp_error( $response ) ) { return false; } @@ -115,7 +112,7 @@ public static function imagecreatefromurl( $image_url ) { * * @param string $type Type (hex, rgb, hsv) (optional). * - * @return color as a string formatted as $type + * @return string|bool color as a string formatted as $type or false if the image could not be loaded. */ public function color( $type = 'hex' ) { // Bail if there is no image to work with. @@ -126,8 +123,7 @@ public function color( $type = 'hex' ) { // Finds dominant color. $color = self::grab_color(); // Passes value to Color class. - $color = self::get_color( $color, $type ); - return $color; + return self::get_color( $color, $type ); } /** @@ -135,7 +131,7 @@ public function color( $type = 'hex' ) { * * @param string $type can be 'index' or 'hex'. * - * @return array with color indices + * @return array|false color indices or false if the image could not be loaded. */ public function grab_points( $type = 'index' ) { $img = $this->image_obj; @@ -186,7 +182,7 @@ public function grab_points( $type = 'index' ) { /** * Finds the average color of the image based on five sample points * - * @return array with rgb color + * @return array|bool array with rgb color or false if the image could not be loaded. */ public function grab_color() { $img = $this->image_obj; @@ -248,8 +244,7 @@ public function get_color( $color, $type ) { $color = implode( ',', $c->toHsvInt() ); break; default: - $color = $c->toHex(); - return $color; + return $c->toHex(); } return $color; @@ -260,7 +255,7 @@ public function get_color( $color, $type ) { * Checks contrast against main color * Gives either black or white for using with opacity * - * @return string + * @return string|bool Returns black or white or false if the image could not be loaded. */ public function contrast() { if ( ! $this->color ) { diff --git a/projects/plugins/jetpack/changelog/improve-ical-reader b/projects/plugins/jetpack/changelog/improve-ical-reader new file mode 100644 index 000000000000..f4af487de3a7 --- /dev/null +++ b/projects/plugins/jetpack/changelog/improve-ical-reader @@ -0,0 +1,4 @@ +Significance: patch +Type: bugfix + +Calendar Library: various fixes diff --git a/projects/plugins/jetpack/modules/shortcodes/upcoming-events.php b/projects/plugins/jetpack/modules/shortcodes/upcoming-events.php index de3f14f89982..150fe57417b0 100644 --- a/projects/plugins/jetpack/modules/shortcodes/upcoming-events.php +++ b/projects/plugins/jetpack/modules/shortcodes/upcoming-events.php @@ -27,7 +27,7 @@ public static function init() { */ public static function shortcode( $atts = array() ) { require_once JETPACK__PLUGIN_DIR . '/_inc/lib/icalendar-reader.php'; - $atts = shortcode_atts( + $atts = shortcode_atts( array( 'url' => '', 'number' => 0, @@ -35,17 +35,32 @@ public static function shortcode( $atts = array() ) { $atts, 'upcomingevents' ); - $args = array( + $args = array( 'context' => 'shortcode', 'number' => absint( $atts['number'] ), ); + + if ( empty( $atts['url'] ) ) { + // If the current user can access the Appearance->Widgets page. + if ( current_user_can( 'edit_theme_options' ) ) { + return sprintf( '
%s
', __( 'You must specify a URL to an iCalendar feed in the shortcode. This notice is only displayed to administrators.', 'jetpack' ) ); + } + return self::no_upcoming_event_text(); + } $events = icalendar_render_events( $atts['url'], $args ); if ( ! $events ) { - $events = sprintf( '%s
', __( 'No upcoming events', 'jetpack' ) ); + $events = self::no_upcoming_event_text(); } return $events; } + + /** + * Returns No Upcoming Event text. + */ + private static function no_upcoming_event_text() { + return sprintf( '%s
', __( 'No upcoming events', 'jetpack' ) ); + } } add_action( 'plugins_loaded', array( 'Upcoming_Events_Shortcode', 'init' ), 101 ); diff --git a/projects/plugins/jetpack/modules/widgets/upcoming-events.php b/projects/plugins/jetpack/modules/widgets/upcoming-events.php index 65319527c2ae..04b9b972ac7b 100644 --- a/projects/plugins/jetpack/modules/widgets/upcoming-events.php +++ b/projects/plugins/jetpack/modules/widgets/upcoming-events.php @@ -170,7 +170,7 @@ public function widget( $args, $instance ) { echo $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', 'grofile' ); + do_action( 'jetpack_stats_extra', 'widget_view', 'upcoming_events' ); } /** @@ -182,7 +182,6 @@ public function widget( $args, $instance ) { private function apply_timezone_offset( $events ) { require_once JETPACK__PLUGIN_DIR . '/_inc/lib/icalendar-reader.php'; - $ical = new iCalendarReader(); - return $ical->apply_timezone_offset( $events ); + return ( new iCalendarReader() )->apply_timezone_offset( $events ); } }