← All changes
|
includes/integrations/contactform7/class-convertkit-contactform7-admin-section.php
+163
-0
3.4.0
→
3.4.5
View file →
| @@ -128,8 +128,12 @@ | ||
| 128 | 128 | $this->render_container_end(); |
| 129 | 129 | return; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | + // Warn the user if any Contact Form 7 Form subscribes to Kit, and Contact Form 7 | |
| 133 | + // has no spam protection enabled. | |
| 134 | + $this->maybe_output_spam_protection_warning( $cf7_forms ); | |
| 135 | + | |
| 132 | 136 | // Get Creator Network Recommendations script. |
| 133 | 137 | $creator_network_recommendations = new ConvertKit_Resource_Creator_Network_Recommendations( 'contact_form_7' ); |
| 134 | 138 | $creator_network_recommendations_enabled = $creator_network_recommendations->enabled(); |
| 135 | 139 | |
| @@ -198,8 +202,167 @@ | ||
| 198 | 202 | $this->render_container_end(); |
| 199 | 203 | |
| 200 | 204 | // Render submit button. |
| 201 | 205 | submit_button(); |
| 206 | + | |
| 207 | + } | |
| 208 | + | |
| 209 | + /** | |
| 210 | + * Outputs a warning naming Contact Form 7 Forms that subscribe email addresses | |
| 211 | + * to Kit, and have no spam protection. | |
| 212 | + * | |
| 213 | + * @since 3.4.5 | |
| 214 | + * | |
| 215 | + * @param array $cf7_forms Contact Form 7 Forms. | |
| 216 | + */ | |
| 217 | + private function maybe_output_spam_protection_warning( $cf7_forms ) { | |
| 218 | + | |
| 219 | + // Build a list of Contact Form 7 Forms that subscribe to Kit, with no spam protection. | |
| 220 | + $unprotected_forms = array(); | |
| 221 | + foreach ( $cf7_forms as $cf7_form ) { | |
| 222 | + if ( ! $this->settings->get_convertkit_subscribe_setting_by_cf7_form_id( $cf7_form['id'] ) ) { | |
| 223 | + continue; | |
| 224 | + } | |
| 225 | + | |
| 226 | + if ( $this->form_has_spam_protection( $cf7_form['id'] ) ) { | |
| 227 | + continue; | |
| 228 | + } | |
| 229 | + | |
| 230 | + $unprotected_forms[] = $cf7_form['name']; | |
| 231 | + } | |
| 232 | + | |
| 233 | + // Bail if all Contact Form 7 Forms subscribing to Kit have spam protection. | |
| 234 | + if ( ! count( $unprotected_forms ) ) { | |
| 235 | + return; | |
| 236 | + } | |
| 237 | + | |
| 238 | + // Akismet only checks Contact Form 7 Forms whose fields define an `akismet:` option, | |
| 239 | + // so tell the user to configure their fields instead of enabling a provider. | |
| 240 | + if ( $this->has_akismet() ) { | |
| 241 | + $call_to_action_url = 'https://contactform7.com/spam-filtering-with-akismet/'; | |
| 242 | + $call_to_action_text = __( 'Akismet is enabled in Contact Form 7, but these forms do not use it. Add Akismet options to each form\'s fields.', 'convertkit' ); | |
| 243 | + } else { | |
| 244 | + $call_to_action_url = admin_url( 'admin.php?page=wpcf7-integration' ); | |
| 245 | + $call_to_action_text = __( 'Enable reCAPTCHA, Turnstile or Akismet in Contact Form 7.', 'convertkit' ); | |
| 246 | + } | |
| 247 | + | |
| 248 | + $this->output_warning( | |
| 249 | + sprintf( | |
| 250 | + '%s <strong>%s</strong>. %s <a href="%s">%s</a>', | |
| 251 | + esc_html__( 'The following Contact Form 7 Forms subscribe email addresses to Kit, but have no spam protection:', 'convertkit' ), | |
| 252 | + esc_html( implode( ', ', $unprotected_forms ) ), | |
| 253 | + esc_html__( 'Bots may submit fake email addresses, which are then added to your Kit account.', 'convertkit' ), | |
| 254 | + esc_url( $call_to_action_url ), | |
| 255 | + esc_html( $call_to_action_text ) | |
| 256 | + ), | |
| 257 | + 'convertkit-spam-protection-warning' | |
| 258 | + ); | |
| 259 | + | |
| 260 | + } | |
| 261 | + | |
| 262 | + /** | |
| 263 | + * Determines if the given Contact Form 7 Form has spam protection. | |
| 264 | + * | |
| 265 | + * @since 3.4.5 | |
| 266 | + * | |
| 267 | + * @param int $cf7_form_id Contact Form 7 Form ID. | |
| 268 | + * @return bool | |
| 269 | + */ | |
| 270 | + private function form_has_spam_protection( $cf7_form_id ) { | |
| 271 | + | |
| 272 | + // Contact Form 7 configures reCAPTCHA and Turnstile site wide, applying to | |
| 273 | + // every Contact Form 7 Form. | |
| 274 | + if ( $this->has_site_wide_spam_protection() ) { | |
| 275 | + return true; | |
| 276 | + } | |
| 277 | + | |
| 278 | + return $this->form_has_akismet( $cf7_form_id ); | |
| 279 | + | |
| 280 | + } | |
| 281 | + | |
| 282 | + /** | |
| 283 | + * Determines if Contact Form 7 has reCAPTCHA or Turnstile configured, which | |
| 284 | + * apply to every Contact Form 7 Form. | |
| 285 | + * | |
| 286 | + * @since 3.4.5 | |
| 287 | + * | |
| 288 | + * @return bool | |
| 289 | + */ | |
| 290 | + private function has_site_wide_spam_protection() { | |
| 291 | + | |
| 292 | + if ( ! class_exists( 'WPCF7_Integration' ) ) { | |
| 293 | + return false; | |
| 294 | + } | |
| 295 | + | |
| 296 | + $integration = WPCF7_Integration::get_instance(); | |
| 297 | + | |
| 298 | + foreach ( array( 'recaptcha', 'turnstile' ) as $name ) { | |
| 299 | + $service = $integration->get_service( $name ); | |
| 300 | + if ( $service && $service->is_active() ) { | |
| 301 | + return true; | |
| 302 | + } | |
| 303 | + } | |
| 304 | + | |
| 305 | + return false; | |
| 306 | + | |
| 307 | + } | |
| 308 | + | |
| 309 | + /** | |
| 310 | + * Determines if Contact Form 7's Akismet service is configured. | |
| 311 | + * | |
| 312 | + * @since 3.4.5 | |
| 313 | + * | |
| 314 | + * @return bool | |
| 315 | + */ | |
| 316 | + private function has_akismet() { | |
| 317 | + | |
| 318 | + if ( ! class_exists( 'WPCF7_Integration' ) ) { | |
| 319 | + return false; | |
| 320 | + } | |
| 321 | + | |
| 322 | + $service = WPCF7_Integration::get_instance()->get_service( 'akismet' ); | |
| 323 | + | |
| 324 | + return ( $service && $service->is_active() ); | |
| 325 | + | |
| 326 | + } | |
| 327 | + | |
| 328 | + /** | |
| 329 | + * Determines if Akismet checks the given Contact Form 7 Form. | |
| 330 | + * | |
| 331 | + * Contact Form 7 only sends a Form to Akismet when one or more of the Form's | |
| 332 | + * fields define an akismet: option, so an active Akismet service alone doesn't | |
| 333 | + * mean the Form is checked. | |
| 334 | + * | |
| 335 | + * @since 3.4.5 | |
| 336 | + * | |
| 337 | + * @param int $cf7_form_id Contact Form 7 Form ID. | |
| 338 | + * @return bool | |
| 339 | + */ | |
| 340 | + private function form_has_akismet( $cf7_form_id ) { | |
| 341 | + | |
| 342 | + if ( ! class_exists( 'WPCF7_ContactForm' ) ) { | |
| 343 | + return false; | |
| 344 | + } | |
| 345 | + | |
| 346 | + // Bail if Akismet isn't configured. | |
| 347 | + if ( ! $this->has_akismet() ) { | |
| 348 | + return false; | |
| 349 | + } | |
| 350 | + | |
| 351 | + // Bail if the Form can't be read. | |
| 352 | + $contact_form = WPCF7_ContactForm::get_instance( $cf7_form_id ); | |
| 353 | + if ( ! $contact_form ) { | |
| 354 | + return false; | |
| 355 | + } | |
| 356 | + | |
| 357 | + // Determine if any of the Form's fields define an akismet: option. | |
| 358 | + foreach ( $contact_form->scan_form_tags() as $tag ) { | |
| 359 | + if ( $tag->get_option( 'akismet', '(author|author_email|author_url)', true ) ) { | |
| 360 | + return true; | |
| 361 | + } | |
| 362 | + } | |
| 363 | + | |
| 364 | + return false; | |
| 202 | 365 | |
| 203 | 366 | } |
| 204 | 367 | |
| 205 | 368 | /** |