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

Assorted error handling fixes #1641

Merged
merged 6 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions includes/class-newspack-newsletters-renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,8 @@ function ( $block ) {
* Embed block.
*/
case 'core/embed':
$oembed = _wp_oembed_get_object();
$data = $oembed->get_data( $attrs['url'] );
$oembed = apply_filters( 'newspack_newsletters_get_oembed_object', _wp_oembed_get_object() );
adekbadek marked this conversation as resolved.
Show resolved Hide resolved
$data = $oembed->get_data( $attrs['url'] );

if ( ! $data || empty( $data->type ) ) {
break;
Expand Down Expand Up @@ -1063,7 +1063,7 @@ function ( $block ) {
'height' => $data->height,
'href' => $attrs['url'],
);
$markup .= '<mj-image ' . self::array_to_attributes( $img_attrs ) . ' />';
$markup .= '<mj-image ' . self::array_to_attributes( $img_attrs ) . ' />';
if ( ! empty( $caption ) ) {
$markup .= '<mj-text ' . self::array_to_attributes( $caption_attrs ) . '>' . esc_html( $caption ) . ' - ' . esc_html( $data->provider_name ) . '</mj-text>';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,15 @@ private static function get_default_campaign_data() {
*/
private function get_campaign_data( $last_n_days ) {
$last_campaigns_data = get_option( self::LAST_CAMPAIGNS_DATA_OPTION_NAME );
$campaigns_data = self::get_default_campaign_data();

$current_campaign_data = $this->get_current_campaign_data( $last_n_days );
if ( \is_wp_error( $current_campaign_data ) ) {
return $current_campaign_data;
}
update_option( self::LAST_CAMPAIGNS_DATA_OPTION_NAME, $current_campaign_data );

$campaigns_data = self::get_default_campaign_data();

if ( ! $last_campaigns_data ) {
// No data about campaigns yet, so there is nothing to compare the new data with.
return $campaigns_data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ public function remove_contacts_from_lists( $contact_ids, $list_ids ) {
public function upsert_contact( $email_address, $data = [] ) {
$contact = $this->get_contact( $email_address );
$body = [];
if ( $contact ) {
if ( $contact && ! \is_wp_error( $contact ) ) {
$body = [
'email_address' => get_object_vars( $contact->email_address ),
'list_memberships' => $contact->list_memberships,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private static function get_list_activity_reports( $days_in_past_count = 1 ) {
* Get usage reports for last n days.
*
* @param int $days_in_past How many days in past.
* @return Newspack_Newsletters_Service_Provider_Usage_Report[] Usage reports.
* @return Newspack_Newsletters_Service_Provider_Usage_Report[]|WP_Error Usage reports or error.
*/
public static function get_usage_reports( $days_in_past ) {

Expand Down Expand Up @@ -179,10 +179,13 @@ public static function get_usage_reports( $days_in_past ) {
/**
* Creates a usage report.
*
* @return Newspack_Newsletters_Service_Provider_Usage_Report Usage report.
* @return Newspack_Newsletters_Service_Provider_Usage_Report|WP_Error Usage report or error.
*/
public static function get_usage_report() {
$reports = self::get_usage_reports( 1 );
if ( \is_wp_error( $reports ) ) {
return $reports;
}
return reset( $reports );
}
}
72 changes: 52 additions & 20 deletions tests/test-renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,48 @@ public function test_render_mjml_component() {
}

/**
* Test embed blocks rendering.
* Filter the OEmbed return value.
*
* @param array $data The data to return.
*/
public function test_render_embed_blocks() {
// Make the embed request return a static custom response.
public function set_oembed_value( $data = [] ) {
global $newspack_newsletters_test_oembed_data;
$newspack_newsletters_test_oembed_data = $data;
add_filter(
'http_response',
function( $response ) {
$response['body'] = wp_json_encode(
array_merge(
json_decode( $response['body'], true ),
[
'title' => 'Embed',
'url' => 'embed.com',
'thumbnail_url' => 'embed.com/image',
'html' => '<div>Embed</div>',
]
)
);
return $response;
'newspack_newsletters_get_oembed_object',
function() {
return new class() {
public function get_data() { // phpcs:disable Squiz.Commenting.FunctionComment.Missing
global $newspack_newsletters_test_oembed_data;
return (object) array_merge(
[
'title' => 'Embed',
'url' => 'embed.com',
'width' => 480,
'height' => 360,
'thumbnail_url' => 'embed.com/image',
'thumbnail_width' => 480,
'thumbnail_height' => 360,
'html' => 'Embed',
],
$newspack_newsletters_test_oembed_data
);
}
};
}
);
}

/**
* Test embed blocks rendering.
*/
public function test_render_embed_blocks() {
$this->set_oembed_value(
[
'type' => 'video',
'provider_name' => 'YouTube',
]
);
// Video embed.
$inner_html = '<figure><div>https://www.youtube.com/watch?v=aIRgcb3cQ1Q</div></figure>';
$this->assertEquals(
Expand All @@ -97,6 +117,12 @@ function( $response ) {
'Renders image from video embed block with title as caption'
);

$this->set_oembed_value(
[
'type' => 'photo',
'provider_name' => 'Flickr',
]
);
// Image with custom caption.
$inner_html = '<figure><div>https://flickr.com/photos/thomashawk/9274246246</div><figcaption>Automattic</figcaption></figure>';
$this->assertEquals(
Expand All @@ -111,13 +137,19 @@ function( $response ) {
'innerHTML' => $inner_html,
]
),
'<mj-section url="https://flickr.com/photos/thomashawk/9274246246" padding="0"><mj-column padding="12px" width="100%"><mj-image src="embed.com" alt="Automattic" width="500" height="333" href="https://flickr.com/photos/thomashawk/9274246246" /><mj-text align="center" color="#555d66" line-height="1.56" font-size="13px" >Automattic - Flickr</mj-text></mj-column></mj-section>',
'<mj-section url="https://flickr.com/photos/thomashawk/9274246246" padding="0"><mj-column padding="12px" width="100%"><mj-image src="embed.com" alt="Automattic" width="480" height="360" href="https://flickr.com/photos/thomashawk/9274246246" /><mj-text align="center" color="#555d66" line-height="1.56" font-size="13px" >Automattic - Flickr</mj-text></mj-column></mj-section>',
'Renders image with inline figcaption as caption'
);

// Rich embed as HTML.
$inner_html = '<figure><div>https://twitter.com/automattic/status/1395447061336711181</div></figure>';
$rendered_string = Newspack_Newsletters_Renderer::render_mjml_component(
$inner_html = '<figure><div>https://twitter.com/automattic/status/1395447061336711181</div></figure>';
$this->set_oembed_value(
[
'type' => 'rich',
'provider_name' => 'Twitter',
]
);
$rendered_string = Newspack_Newsletters_Renderer::render_mjml_component(
[
'blockName' => 'core/embed',
'attrs' => [
Expand Down