如何在多服务器设置中触发WordPress挂钩?

时间:2014-03-15 09:24:31

标签: php wordpress wordpress-plugin w3-total-cache

站点在后端( BE )WP服务器和多个前端( FE )服务器上运行。

FE有 BE 的MySQL db r / o Slave,以及带有HyperDB插件的WP安装,因此它从本地读取,写入 BE W3TC插件用于 FE 的缓存。

BE 上创建新帖子。发布这些帖子只会触发 BE 上的挂钩。

问题:如何在所有 FE 上触发这些挂钩以重置其缓存?

P.S。我刚才在W3TC插件支持论坛中问the similar question,没有回复。

1 个答案:

答案 0 :(得分:1)

你可以在FE和BE上使用迷你插件来做到这一点。逻辑如下所示;

  1. 在发布后发布的后端,实施和操作
  2. 此触发操作将使用特定usernamepasswordpost_id
  3. 调用前端服务
  4. 在FE上,实施另一个插件,用于检查包含usernamepasswordpost_id
  5. 的请求
  6. 如果有,则实例化W3_CacheFlush并调用特定功能。
  7. 我开发了迷你插件,您可以从管理面板安装它们。简单地说,将代码保存为php文件并压缩它。然后上传到服务器。插件也有一些重点。 Serivce通信是通过用户名和密码进行的。所以你需要在两个插件上提供相同的用户名和密码。如果没有,他们就无法沟通。我已输入用户名和密码以防止其他人拨打您的FE服务。这是插件。

    <强> W3TC_BE.php

    <?php
    /*
    Plugin Name: W3TC Backend
    Plugin URI: http://huseyinbabal.net
    Description: Calls wstc cache clean on frontend services when new post published.
    Version: 1.0
    Author: Hüseyin BABAL
    Author URI: http://huseyinbabal.net
    */
    
    function call_cache_clean_service( $url, $post_id ) {
        if ( !function_exists( 'curl_version' ) ) {
            wp_die( 'Curl must be enabled' );
        }
    
        $url = 'http://frontendservice.com';
        $fields = array(
                        'username' => "your_username", // Username password protection for service usage. Those username password will be same on server side
                        'password' => "your_password",
                        'post_id' => $post_id
                        );
        foreach( $fields as $key => $value ) { 
            $fields_string .= $key . '=' . $value . '&'; 
        }
        rtrim($fields_string, '&');
    
        $ch = curl_init();
    
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_POST, count($fields));
        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    
        $result = curl_exec($ch);
        // To track which posts triggered
        error_log("[W3TC] Cache clean service called for : $post_id to $url", 3, '/path/to/log/file.txt');
        curl_close($ch);
    }
    
    // This for triggering service for all frontends
    function post_published( $post_id ) {
        $frontend_urls = array (
            "http://frontendservice1.com", 
            "http://frontendservice2.com", 
            "http://frontendservice3.com"
            );
        foreach ($frontend_urls as $url) {
            call_cache_clean_service( $post_id );
        }
    }
    
    // Publish action for calling service
    add_action( 'publish_post', 'post_published' );
    
    ?>
    

    <强> W3TC_FE.php

    <?php
    /*
    Plugin Name: W3TC Frontend
    Plugin URI: http://huseyinbabal.net
    Description: Check specific request and clear cache
    Version: 1.0
    Author: Hüseyin BABAL
    Author URI: http://huseyinbabal.net
    */
    
    function check_w3c_request() {
        // this username and password must be same as in be
        $original_username = "your_username";
        $original_password = "your_password";
    
        $username = $_POST["username"];
        $password = $_POST["username"];
        $post_id = $_POST["post_id"];
    
        if ( (!empty($username) && $username != $original_username) || ( !empty($password) || $password != $original_password ) ) {
            wp_die( "Page not allowed!" );
        } else {
            if ( class_exists('W3_CacheFlush') ) {
                $w3_pgcache = w3_instance('W3_CacheFlush');
                return $w3_pgcache->prime_post( $post_id );     
            }   
        }
    
    
    }
    
    // Get Posted variables
    add_action( 'after_setup_theme', 'check_w3tc_request' );
    
    ?>