Skip to content

Commit 6c99f49

Browse files
perf: remove default_form_id logic, can just query form with lowest ID directly
1 parent 7e8a01d commit 6c99f49

5 files changed

Lines changed: 21 additions & 24 deletions

File tree

includes/admin/migrations/3.0.0-form-1-post-type.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
]
3737
);
3838

39-
// set default_form_id
40-
update_option('mc4wp_default_form_id', $id);
41-
4239
// set form settings
4340
$setting_keys = [
4441
'css',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
defined('ABSPATH') or exit;
4+
5+
delete_option('mc4wp_default_form_id');

includes/forms/class-admin.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,6 @@ public function process_add_form()
168168
update_post_meta($form_id, '_mc4wp_settings', $form_data['settings']);
169169
}
170170

171-
// set default form ID
172-
$this->set_default_form_id($form_id);
173-
174171
$this->messages->flash(__('Form saved.', 'mailchimp-for-wp'));
175172
$edit_form_url = mc4wp_get_edit_form_url($form_id);
176173
wp_redirect($edit_form_url);
@@ -299,22 +296,9 @@ public function process_save_form()
299296
$form_data = $_POST['mc4wp_form'];
300297

301298
$this->save_form($form_id, $form_data);
302-
$this->set_default_form_id($form_id);
303299
$this->messages->flash(__('Form saved.', 'mailchimp-for-wp'));
304300
}
305301

306-
/**
307-
* @param int $form_id
308-
*/
309-
private function set_default_form_id($form_id)
310-
{
311-
$default_form_id = get_option('mc4wp_default_form_id', 0);
312-
313-
if (empty($default_form_id)) {
314-
update_option('mc4wp_default_form_id', $form_id);
315-
}
316-
}
317-
318302
/**
319303
* Goes through each form and aggregates array of stylesheet slugs to load.
320304
*

includes/forms/class-form.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static function throw_not_found_exception($post_id)
2626
{
2727
// translators: %d is the form post ID.
2828
$message = sprintf(__('There is no form with ID %d, perhaps it was deleted?', 'mailchimp-for-wp'), $post_id);
29+
2930
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Exception text is not direct output and is escaped at render time.
3031
throw new Exception($message);
3132
}
@@ -44,8 +45,19 @@ public static function get_instance($post = 0)
4445
} else {
4546
$post_id = (int) $post;
4647

48+
// if no explicit ID given, query the first published form
4749
if ($post_id === 0) {
48-
$post_id = (int) get_option('mc4wp_default_form_id', 0);
50+
$posts = get_posts([
51+
'post_type' => 'mc4wp-form',
52+
'numberposts' => 1,
53+
'post_status' => 'publish',
54+
'orderby' => 'ID',
55+
'order' => 'ASC',
56+
]);
57+
if ($posts) {
58+
$post = $posts[0];
59+
$post_id = $post->ID;
60+
}
4961
}
5062
}
5163

includes/forms/class-output-manager.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected function generate_html($id = 0, $config = [])
9696
$form = mc4wp_get_form($id);
9797
} catch (Exception $e) {
9898
if (current_user_can('manage_options')) {
99-
return sprintf('<strong style="color: indianred;">Mailchimp for WordPress error:</strong> %s', $e->getMessage());
99+
return '<p><strong style="color: indianred;">Mailchimp for WordPress error:</strong> ' . esc_html($e->getMessage()) . '.</p>';
100100
}
101101

102102
return '';
@@ -106,10 +106,9 @@ protected function generate_html($id = 0, $config = [])
106106

107107
if (!mc4wp_get_api_key()) {
108108
if (current_user_can('manage_options')) {
109-
$html .= '<p style="color: indianred;">' . __('You need to configure your Mailchimp API key for this form to work properly.', 'mailchimp-for-wp') . '</p>';
109+
$html .= '<p style="color: indianred;">' . esc_html__('You need to configure your Mailchimp API key for this form to work properly.', 'mailchimp-for-wp') . '</p>';
110110
} else {
111-
// if no API key set and request is for an unauthorized user
112-
// show nothing
111+
// show nothing if no API key set and request is for an unauthorized user
113112
return '';
114113
}
115114
}

0 commit comments

Comments
 (0)