WordPress CF7加入

时间:2018-03-24 13:08:51

标签: php wordpress preg-match contact-form

对于即将推出的GDPR,我正在创建一个插件,为我的客户的每个网站添加一些代码段。

我现在遇到问题的代码,应检查是否安装了Contact Form 7,以及是否存在帖子类型为'wpcf7_contact_form'且标题为'Kontakt'的帖子。如果是这样,它应该获得帖子内容并检查是否存在提交和接受字段。如果有一个提交按钮但没有接受字段,它应该将接受字符串(包括按钮短代码)与找到的提交部分连接起来,并将其替换为帖子内容。

经过一些测试后,我注意到检查是否有帖子以及是否安装了cf7。其余的不是。使用http://www.phpliveregex.com/创建的preg_match模式。我现在不知道为什么它不会催促,谢谢你事先提供任何帮助。

编辑:经过一些调试(仍然在代码中)我注意到,现在check_submit和check_acceptance函数不起作用。其余的cos现在应该运行。

代码:

<?php 

add_action( 'admin_init', 'init_cf7_privacy');
function init_cf7_privacy() {

    if ((check_cf7_installation()) && (find_contact_form() != "")) {

        $formContent = get_contactform_content();
        $acceptance = '[acceptance acceptance-842]<small>Ich habe die <a href="/datenschutzerklaerung">Datenschutzerklärung</a> zur Kenntnis genommen. Ich stimme zu, dass meine Angaben zur Kontaktaufnahme und für Rückfragen dauerhaft gespeichert werden. </small>[/acceptance]';

        if (check_cf7_installation()) 
            echo "CF7 Returned true!" . "\r\n";

        echo "CF Page ID: " . find_contact_form();

        if (check_submit( $formContent )) 
            echo "Submit Returned true!" . "\r\n";

        if (check_acceptance( $formContent )) 
            echo "Acceptance Returned true!" . "\r\n";

        //echo concatenate_acceptance_submit( $formContent, $acceptance ) . "\r\n";
        echo add_acceptance( $formContent, $acceptance );

        $my_post = array(
              'ID'           => find_contact_form(),
              'post_content' => add_acceptance( $formContent, $acceptance ) ,
        );

        // Update the post into the database
        wp_update_post( $my_post ); 

    }
}

function check_cf7_installation() {
    if (class_exists('wpcf7'))
        return true;
}

function find_contact_form() {
    $searchTitle = 'Kontakt';
    $page = get_page_by_title( $searchTitle, OBJECT, 'wpcf7_contact_form');

    return $page->ID;
}

function get_contactform_content() {
    $my_postid = find_contact_form();
    $post_object = get_post($my_postid);
    $content = $post_object->post_content;

    return $content;
}

function concatenate_acceptance_submit( $formContent, $acceptance ) {
    if( preg_match('/.+\[submit\s["].+["]\].+/', $formContent, $matches ) ) {
        return $acceptance . "\r\n" . "\r\n" . $matches[0];
    }
}

function check_submit( $formContent ) {
    if ( preg_match('/\[submit\s["].+["]\]/', $formContent) )
         return true;
}

function check_acceptance( $formContent ) {
    if ( preg_match('/\[acceptance\s.+\]/', $formContent) )
         return true;
}

function add_acceptance( $formContent, $acceptance ) {
    if ( check_submit( $formContent ) && !check_acceptance( $formContent ) ) {

        if ( preg_match('/.+\[submit\s["].+["]\].+/', $formContent, $matches) ) {

            $formContentRep = str_replace( $matches[0], concatenate_acceptance_submit( $formContent, $acceptance ), $formContent );

            return $formContentRep;

        }
    }
}

成功收到preg_match函数的帖子内容和主题:

<p>Ihr Name (Pflichtfeld)<br />    [text* your-name] </p>
<p>Ihre E-Mail-Adresse (Pflichtfeld)<br />
[email* your-email] </p>

<p>Betreff<br />
[text your-subject] </p>

<p>Ihre Nachricht<br />
[textarea your-message x3] </p>


<p class="submit">[submit "tohuwabohu"]</p>

1 个答案:

答案 0 :(得分:0)

我不确切知道为什么,但是wp_update_post()使得chek函数无法正常工作。以下带有wpdb查询的代码现在正在运行:

<?php 

add_action( 'admin_init', 'init_cf7_privacy');
function init_cf7_privacy() {

    if ((check_cf7_installation()) && (find_contact_form() != "")) {

        $formContent = get_contactform_content();
        $acceptance = '[acceptance acceptance-842]<small>Ich habe die <a href="/datenschutzerklaerung">Datenschutzerklärung</a> zur Kenntnis genommen. Ich stimme zu, dass meine Angaben zur Kontaktaufnahme und für Rückfragen dauerhaft gespeichert werden. </small>[/acceptance]';

        /*
        if (check_cf7_installation()) 
            echo "CF7 Returned true!" . "\r\n";

        echo "CF Page ID: " . find_contact_form() . "\r\n";

        if (check_submit( $formContent )) 
            echo "Submit Returned true!" . "\r\n";

        if (check_acceptance( $formContent )) 
            echo "Acceptance Returned true!" . "\r\n";

        echo concatenate_acceptance_submit( $formContent, $acceptance ) . "\r\n";
        */

        add_acceptance( $formContent, $acceptance );

    }
}

function check_cf7_installation() {
    if (class_exists('wpcf7'))
        return true;
}

function find_contact_form() {
    $searchTitle = 'Kontakt';
    $page = get_page_by_title( $searchTitle, OBJECT, 'wpcf7_contact_form');

    return $page->ID;
}

function get_contactform_content() {
    $my_postid = find_contact_form();

    global $wpdb;
    $content = $wpdb->get_var( 
        $wpdb->prepare( "
            SELECT meta_value
            FROM ".$wpdb->prefix."postmeta
            WHERE post_id = %d
            AND meta_key = %s
            ",
           $my_postid, '_form' ) 
    );

    return $content;
}

function concatenate_acceptance_submit( $formContent, $acceptance ) {
    if( preg_match('/.+\[submit\s["].+["]\].+/', $formContent, $matches ) ) {
        return $acceptance . "\r\n" . "\r\n" . $matches[0];
    }
}

function check_submit( $formContent ) {
    if ( preg_match('/\[submit\s["].+["]\]/', $formContent) )
        return true;
}

function check_acceptance( $formContent ) {
    if ( preg_match('/\[acceptance\s.+\]/', $formContent, $matches) )
        return true;
}

function add_acceptance( $formContent, $acceptance ) {
    if ( check_submit( $formContent ) && !check_acceptance( $formContent ) ) {

        if ( preg_match('/.+\[submit\s["].+["]\].+/', $formContent, $matches) ) {

            $formContentRep = str_replace( $matches[0], concatenate_acceptance_submit( $formContent, $acceptance ), $formContent );

            //Write new Cotact Form Content to Database
            global $wpdb;    

            $wpdb->query(
                $wpdb->prepare( "
                    UPDATE ".$wpdb->prefix."postmeta
                    SET meta_value = %s
                    WHERE post_id = %d
                    AND meta_key = %s
                    ", 
                    $formContentRep, find_contact_form(), '_form' )
            );

        }
    }
}