代码之家  ›  专栏  ›  技术社区  ›  JUVO_Webdes

WordPress CF7加载项

  •  0
  • JUVO_Webdes  · 技术社区  · 6 年前

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

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

    经过一些测试,我注意到检查是否有一个职位,如果cf7是安装工作。其余的是t。preg\u匹配使用创建的模式 http://www.phpliveregex.com/ 。我现在不知道它为什么不工作,感谢您提前提供的帮助。

    编辑:经过一些调试(仍在代码中),我注意到现在check\u submit和check\u 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\u 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 回复  |  直到 6 年前
        1
  •  0
  •   JUVO_Webdes    6 年前

    我不知道确切的原因,但wp\u update\u post()使chek函数无法正常工作。wpdb querys的以下代码正在运行:

    <?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' )
                );
    
            }
        }
    }