etruel / wpematico

11 stars 9 forks source link

PHP Fatal error on campaigns_list #91

Closed etruel closed 7 months ago

etruel commented 7 months ago

[29-Mar-2024 05:54:25 UTC] PHP Fatal error: Uncaught Error: Object of class WP_Error could not be converted to string in /home/1211698.cloudwaysapps.com/ckncczpwsg/public_html/wp-content/plugins/wpematico/app/campaigns_list.php:660 Stack trace: #0 /home/1211698.cloudwaysapps.com/ckncczpwsg/public_html/wp-content/plugins/wpematico/app/campaigns_list.php(660): implode()

1 /home/1211698.cloudwaysapps.com/ckncczpwsg/public_html/wp-includes/class-wp-hook.php(326): WPeMatico_Campaigns::inline_custom_fields()

2 /home/1211698.cloudwaysapps.com/ckncczpwsg/public_html/wp-includes/plugin.php(205): WP_Hook->apply_filters()

3 /home/1211698.cloudwaysapps.com/ckncczpwsg/public_html/wp-admin/includes/template.php(323): apply_filters()

4 /home/1211698.cloudwaysapps.com/ckncczpwsg/public_html/wp-admin/includes/class-wp-posts-list-table.php(1175): get_inline_data()

5 /home/1211698.cloudwaysapps.com/ckncczpwsg/public_html/wp-admin/includes/class-wp-posts-list-table.php(1072): WP_Posts_List_Table->column_title()

6 /home/1211698.cloudwaysapps.com/ckncczpwsg/public_html/wp-admin/includes/class-wp-list-table.php(1790): WP_Posts_List_Table->_column_title()

7 /home/1211698.cloudwaysapps.com/ckncczpwsg/public_html/wp-admin/includes/class-wp-posts-list-table.php(1432): WP_List_Table->single_row_columns()

8 /home/1211698.cloudwaysapps.com/ckncczpwsg/public_html/wp-admin/includes/class-wp-posts-list-table.php(834): WP_Posts_List_Table->single_row()

9 /home/1211698.cloudwaysapps.com/ckncczpwsg/public_html/wp-admin/includes/class-wp-posts-list-table.php(810): WP_Posts_List_Table->_display_rows()

10 /home/1211698.cloudwaysapps.com/ckncczpwsg/public_html/wp-admin/includes/class-wp-list-table.php(1709): WP_Posts_List_Table->display_rows()

11 /home/1211698.cloudwaysapps.com/ckncczpwsg/public_html/wp-admin/includes/class-wp-list-table.php(1636): WP_List_Table->display_rows_or_placeholder()

12 /home/1211698.cloudwaysapps.com/ckncczpwsg/public_html/wp-admin/edit.php(503): WP_List_Table->display()

13 {main} thrown in /home/1211698.cloudwaysapps.com/ckncczpwsg/public_html/wp-content/plugins/wpematico/app/campaigns_list.php on line 660

GPT Fix:

static function inline_custom_fields($text) {
global $post, $pagenow;
if (($pagenow == 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] == 'wpematico') || ($pagenow == 'admin-ajax.php' && isset($post) && $post->post_type == 'wpematico')) {
$campaign_data = WPeMatico::get_campaign($post->ID);
// Check if $campaign_data is a WP_Error object
if (is_wp_error($campaign_data)) {
// Handle error appropriately, possibly logging it and/or setting default values
$errorMessage = $campaign_data->get_error_message();
// For simplicity, we'll just return here, but you could handle it differently
return $text . " Error retrieving campaign data: " . $errorMessage;
}

/* Custom inline data for wpematico */
// (initialization of campaign data variables)

// Ensure $campaign_categories is an array
$campaign_categories = (is_array($campaign_data['campaign_categories'])) ? $campaign_data['campaign_categories'] : array();
$campaign_tags = (isset($campaign_data['campaign_tags'])) ? $campaign_data['campaign_tags'] : '';

// (concatenation of the $text variable with HTML elements and campaign data)

$text .= '</div>
<div class="post_id">' . $post->ID . '</div>
// (other div elements)
<div class="campaign_categories">' . implode(',', $campaign_categories) . '</div>
<div class="campaign_tags">' . stripslashes($campaign_tags);
}
return $text;
}
etruel commented 7 months ago

Related issue https://github.com/etruel/wpematico/issues/7 Surely the way a campaign could be broken by an unexpected interruption on fetching.

etruel commented 7 months ago

We'll manage this to show a notice of the broken campaign on campaign list and give the chance of trash it.
We should test if it's failing with fatal error on another places, but it is really hard get a broken campaign.