Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calendar library and Tonesque: various cleanup #28274

Merged
merged 3 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions projects/plugins/jetpack/_inc/lib/icalendar-reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 );
}

/**
Expand All @@ -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 );
}
25 changes: 10 additions & 15 deletions projects/plugins/jetpack/_inc/lib/tonesque.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@
/**
* 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
*/

/**
* Color representation class.
*/
class Tonesque {

/**
* Image URL.
*
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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.
Expand All @@ -126,16 +123,15 @@ 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 );
}

/**
* Grabs the color index for each of five sample points of the image
*
* @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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 ) {
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/improve-ical-reader
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Calendar Library: various fixes
21 changes: 18 additions & 3 deletions projects/plugins/jetpack/modules/shortcodes/upcoming-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,40 @@ 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,
),
$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( '<p>%s</p>', __( '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( '<p>%s</p>', __( '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( '<p>%s</p>', __( 'No upcoming events', 'jetpack' ) );
}
}
add_action( 'plugins_loaded', array( 'Upcoming_Events_Shortcode', 'init' ), 101 );
5 changes: 2 additions & 3 deletions projects/plugins/jetpack/modules/widgets/upcoming-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}

/**
Expand All @@ -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 );
}
}