Skip to content

Commit f406c15

Browse files
show warning to admins if form shows without api key. show nothing to guests. closes #796
1 parent b293544 commit f406c15

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

includes/forms/class-output-manager.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,41 @@ public function shortcode($attributes = [], $content = '')
7575
* @return string
7676
*/
7777
public function output_form($id = 0, $config = [], $echo = true)
78+
{
79+
$html = $this->generate_html($id, $config);
80+
81+
// echo content if necessary
82+
if ($echo) {
83+
echo $html;
84+
}
85+
86+
return $html;
87+
}
88+
89+
protected function generate_html($id = 0, $config = [])
7890
{
7991
try {
8092
$form = mc4wp_get_form($id);
8193
} catch (Exception $e) {
8294
if (current_user_can('manage_options')) {
83-
return sprintf('<strong>Mailchimp for WordPress error:</strong> %s', $e->getMessage());
95+
return sprintf('<strong style="color: indianred;">Mailchimp for WordPress error:</strong> %s', $e->getMessage());
8496
}
8597

8698
return '';
8799
}
88100

101+
$html = '';
102+
103+
if (!mc4wp_get_api_key()) {
104+
if (current_user_can('manage_options')) {
105+
$html .= '<p style="color: indianred;">' . __('You need to configure your Mailchimp API key for this form to work properly.', 'mailchimp-for-wp') . '</p>';
106+
} else {
107+
// if no API key set and request is for an unauthorized user
108+
// show nothing
109+
return '';
110+
}
111+
}
112+
89113
++$this->count;
90114

91115
// set a default element_id if none is given
@@ -112,14 +136,9 @@ public function output_form($id = 0, $config = [], $echo = true)
112136
echo $form_html;
113137

114138
// grab all contents in current output buffer & then clean + end it.
115-
$html = ob_get_clean();
139+
$html .= ob_get_clean();
116140
} catch (Error $e) {
117-
$html = $form_html;
118-
}
119-
120-
// echo content if necessary
121-
if ($echo) {
122-
echo $html;
141+
$html .= $form_html;
123142
}
124143

125144
return $html;

0 commit comments

Comments
 (0)