config-validator.php 3.3 KB

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