問題描述

我想知道是否有任何方式掛鉤到 WordPress 更新過程,併傳送 $_POST 變數更新伺服器?

我正在從私人伺服器提供外掛/主題更新,並且我將鉤住這一點:

add_filter('pre_set_site_transient_update_themes', 'check_for_update');

它的工作很好。主題/外掛的較新版本顯示在 Dashboard> 更新,我可以更新。但問題是 – 我希望使用者只有提供正確的登入/密碼 (首先透過 add_option()) 才能下載/更新。理想情況下,除非客戶端將 $_POST 與登入/密碼傳送給 update.php(更新伺服器上將傳送 plugin.ZIP 的檔案作為回報),否則直接連結不應該工作。

我在尋找這樣的東西:

add_filter('updating', 'my_func');
function my_func($request){
   $request['login'] = get_option('login');
   $request['pass'] = get_option('pass');
   return $request;
}

而 WordPress 在更新主題/外掛時,應該將 $_POST['login']$_POST['pass']傳送到 http://example.com/update.php,並且 update.php 應該只允許下載/更新,如果登入符合在那裡定義的那個 (update.php 是更新伺服器上傳送 ZIP 包與較新外掛的檔案到 WordPress) 。

我希望很清楚:)

最佳解決方案

更新& 內部 WP HTTP API

一個稍微修改版本的 my answer to this question,也是一個外掛,顯示它如何工作。

注意:程式碼未經測試 – 我不知道您的伺服器設定等,只是寫在我的頭上。你必須測試它,找到適當的位置合併引數和設定你的 URL 等。

如果自定義遠端儲存庫發回可用的標題 (通常不是這種情況),則可以改進初始測試 (#1) 。所以如果這樣做,你最好不要使用 wp_remote_head(),因為它使 HTTP 請求更輕巧。

<?php
defined( 'ABSPATH' ) OR exit;
/**
 * Plugin Name: (#78267) Custom Theme Update Args
 * Description: Adds custom arguments to the HTTP request for a theme or plugin update from a custom location.
 * Version:     2013-04-02.2139
 * Author:      Franz Josef Kaiser <wecodemore@gmail.com>
 * Author URI:  http://unserkaiser.com
 * License:     The MIT License (MIT)
 * LicenseURI:  http://www.opensource.org/licenses/mit-license.php
 */

add_filter( 'http_request_args', 'custom_upgrade_process', 9, 2 );
/**
 * Callback for a HTTP request used to switch the
 * SSL verification in case of a WP error response
 * and routing to a custom Theme or Plugin repository.
 * @param  array  $r   Request arguments
 * @param  string $url Request URL
 * @return array  $r
 */
function custom_upgrade_process( $r, $url )
{
    // Alter the following settings according to your
    // update procedure and admin pages that deliver it.
    # A) The admin URL
    $custom_repo = 'https://example.com?foo=bar';

    if (
        0 !== strpos( $url, 'http://api.wordpress.org/plugins/update-check' )
        XOR 0 !== strpos( $url, 'http://api.wordpress.org/themes/update-check' )
    )
        return $r;

    # 1) Do an initial test to check if things are working as expected
    $response = wp_remote_get(
        $custom_repo,
        array(
            'timeout'     => 120,
            'httpversion' => '1.1',
        )
    );
    # 2) Turn off SSL verification in case the HTTP request didn't work out
    if (
        is_wp_error( $response )
        AND strstr( $response->get_error_message(), 'SSL: certificate subject name' )
    )
        add_filter( 'https_ssl_verify', '__return_false' );

    # 3) Add your custom request arguments
    $r = array_merge( $r, array(
        'login' => get_option( 'login' ),
        'pass'  => get_option( 'pass' ),
    ) );

    return $r;
}

祝你好運。 🙂

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。