问题描述

我想知道是否有任何方式挂钩到 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 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。