Create a mail counter for Contact Form 7

為{幸葍}努か 提交于 2020-06-17 13:08:49

问题


I was wondering if you could help me solving this issue; I have created a email counter which increase the number +1 once the user send an email (this works great).

What I was trying to do is to reset the counter after 86400 seconds (one day) in a loop, so every day the counter will start counting from 0 the emails sent. I have tried many scripts but none of them works, this is the last sketch:

//Define the key to store in the database
define( 'CF7_COUNTER', 'cf7-counter' );

//Create the shortcode which will set the value for the DTX field
function cf7dtx_counter(){
    $val = get_option( CF7_COUNTER, 0) + 1;  //Increment the current count
    return $val;
}
add_shortcode('CF7_counter', 'cf7dtx_counter');

function setInter($f, $milliseconds) { 
$seconds=(int)$milliseconds/1000;
while(true) {
$f();
sleep($seconds);
}
}

//Action performed when the mail is actually sent by CF7
function cf7dtx_increment_mail_counter(){
    $val = get_option( CF7_COUNTER, 0) + 1; //Increment the current count
    update_option(CF7_COUNTER, $val); //Update the settings with the new count
    setInter(function(){$val=0; update_option(CF7_COUNTER,$val); cf7dtx_increment_mail_counter();},30000); //Reset the counter after 60 seconds
}

add_action('wpcf7_mail_sent', 'cf7dtx_increment_mail_counter');

来源:https://stackoverflow.com/questions/62122889/create-a-mail-counter-for-contact-form-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!