WordPress CF7 插件

WordPress CF7 Add in

为了即将出台的 GDPR,我正在创建一个插件,用于向我客户的每个网站添加一些片段。

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

经过一些测试后,我注意到检查是否有 post 以及是否安装了 cf7。其余的不是。使用 http://www.phpliveregex.com/ 创建的 preg_match 模式。我现在不知道为什么它没有醒来,感谢您提前提供的帮助。

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

代码:

<?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功能成功接收post内容和主题:

<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>

我不知道确切原因,但 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' )
            );

        }
    }
}