admin管理员组

文章数量:1122846

I have this code for the auto update plugins manually from my own server:

<?php
// Take over the update check
add_filter( 'pre_set_site_transient_update_plugins', 'gb_check_for_plugin_update' );

function gb_check_for_plugin_update( $checked_data ) {
    $api_url     = '/';
    $plugin_slug = 'plugin-slug';

    if ( empty( $checked_data->checked ) ) {
        return $checked_data;
    }

    $request_args = [
        'slug'    => $plugin_slug,
        'version' => $checked_data->checked[ $plugin_slug . '/' . $plugin_slug . '.php' ],
    ];

    $request_string = gb_prepare_request( 'basic_check', $request_args );

    // Start checking for an update
    $raw_response = wp_remote_post( $api_url, $request_string );

    if ( ! is_wp_error( $raw_response ) && ( (int) $raw_response['response']['code'] === 200 ) ) {
        $response = unserialize( $raw_response['body'] );
    }

    if ( is_object( $response ) && ! empty( $response ) ) { // Feed the update data into WP updater
        $checked_data->response[ $plugin_slug . '/' . $plugin_slug . '.php' ] = $response;
    }

    return $checked_data;
}

// Take over the Plugin info screen
add_filter( 'plugins_api', 'gb_plugin_api_call', 10, 3 );

function gb_plugin_api_call( $def, $action, $args ) {
    $api_url     = '/';
    $plugin_slug = 'plugin-slug';

    // Do nothing if this is not about getting plugin information
    if ( $action !== 'plugin_information' ) {
        return false;
    }

    if ( (string) $args->slug !== (string) $plugin_slug ) {
        // Conserve the value of previous filter of plugins list in alternate API
        return $def;
    }

    // Get the current version
    $plugin_info     = get_site_transient( 'update_plugins' );
    $current_version = $plugin_info->checked[ $plugin_slug . '/' . $plugin_slug . '.php' ];
    $args->version   = $current_version;

    $request_string = gb_prepare_request( $action, $args );

    $request = wp_remote_post( $api_url, $request_string );

    if ( is_wp_error( $request ) ) {
        $res = new WP_Error( 'plugins_api_failed', __( 'An Unexpected HTTP Error occurred during the API request.</p> <p><a href="?" onclick="document.location.reload(); return false;">Try again</a>' ), $request->get_error_message() );
    } else {
        $res = unserialize( $request['body'] );

        if ( $res === false ) {
            $res = new WP_Error( 'plugins_api_failed', __( 'An unknown error occurred' ), $request['body'] );
        }
    }

    return $res;
}

function gb_prepare_request( $action, $args ) {
    global $wp_version;

    return [
        'body'       => [
            'action'  => $action,
            'request' => serialize( $args ),
            'api-key' => md5( get_bloginfo( 'url' ) ),
        ],
        'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ),
    ];
}

And here's the server side updater:

<?php
/**
 * WordPress Update Server
 *
 * @version 0.0.1
 */

$packages['plugin-slug'] = [
    'versions' => [
        '1.0.0' => [
            'name'              => 'WP Awesome Plugin',
            'version'           => '1.0.0',
            'new_version'       => '1.0.0',
            'date'              => '2023-02-10 14:17:00',
            'tested'            => '6.1.1',
            'package'           => 'https://127.0.0.1/plugin-slug-1.0.0.zip',
            //
            'author'            => '<a href="https://127.0.0.1/">Author</a>',
            'author_profile'    => 'https://127.0.0.1/',
            'homepage'          => 'https://127.0.0.1/wordpress-plugins/plugin-slug/',
            'requires'          => '5.8',
            'requires_php'      => '7.0',
            'description'       => 'This is an awesome plugin!',
            'short_description' => 'This is an awesome plugin!',
        ],
    ],
    'info'     => [
        'url' => '',
    ],
];



// Process API requests
$action = (string) $_POST['action'];
$args   = unserialize( $_POST['request'] );

if ( is_array( $args ) ) {
    $args = array_to_object( $args );
}

$latest_package = array_shift( $packages[ $args->slug ]['versions'] );



// Process basic_check request
if ( $action === 'basic_check' ) {
    $update_info       = array_to_object( $latest_package );
    $update_info->slug = $args->slug;

    if ( version_compare( $args->version, $latest_package['version'], '<' ) ) {
        $update_info->new_version = $update_info->version;

        print serialize( $update_info );
    }
}



// Process plugin_information request
if ( $action === 'plugin_information' ) {
    $data = new stdClass;

    $data->slug              = $args->slug;
    $data->name              = $latest_package['name'];
    $data->version           = $latest_package['version'];
    $data->new_version       = $latest_package['new_version'];
    $data->last_updated      = $latest_package['date'];
    $data->download_link     = $latest_package['package'];
    $data->tested            = $latest_package['tested'];
    //
    $data->author            = $latest_package['author'];
    $data->author_profile    = $latest_package['author_profile'];
    $data->homepage          = $latest_package['homepage'];
    $data->requires          = $latest_package['requires'];
    $data->requires_php      = $latest_package['requires_php'];
    //
    $data->description       = $latest_package['description'];
    $data->short_description = $latest_package['short_description'];

    $data->sections = [
        'description' => $latest_package['description'],
    ];

    print serialize( $data );
}



function array_to_object( $array = [] ) {
    if ( empty( $array ) || ! is_array( $array ) ) {
        return false;
    }

    $data = new stdClass;

    foreach ( $array as $akey => $aval ) {
        $data->{$akey} = $aval;
    }

    return $data;
}

I'm wondering how would it be to add a "Change Log" section/tab to the item? Any help is appreciated

I have this code for the auto update plugins manually from my own server:

<?php
// Take over the update check
add_filter( 'pre_set_site_transient_update_plugins', 'gb_check_for_plugin_update' );

function gb_check_for_plugin_update( $checked_data ) {
    $api_url     = 'https://www.example.com/api/wp/update/';
    $plugin_slug = 'plugin-slug';

    if ( empty( $checked_data->checked ) ) {
        return $checked_data;
    }

    $request_args = [
        'slug'    => $plugin_slug,
        'version' => $checked_data->checked[ $plugin_slug . '/' . $plugin_slug . '.php' ],
    ];

    $request_string = gb_prepare_request( 'basic_check', $request_args );

    // Start checking for an update
    $raw_response = wp_remote_post( $api_url, $request_string );

    if ( ! is_wp_error( $raw_response ) && ( (int) $raw_response['response']['code'] === 200 ) ) {
        $response = unserialize( $raw_response['body'] );
    }

    if ( is_object( $response ) && ! empty( $response ) ) { // Feed the update data into WP updater
        $checked_data->response[ $plugin_slug . '/' . $plugin_slug . '.php' ] = $response;
    }

    return $checked_data;
}

// Take over the Plugin info screen
add_filter( 'plugins_api', 'gb_plugin_api_call', 10, 3 );

function gb_plugin_api_call( $def, $action, $args ) {
    $api_url     = 'https://www.example.com/api/wp/update/';
    $plugin_slug = 'plugin-slug';

    // Do nothing if this is not about getting plugin information
    if ( $action !== 'plugin_information' ) {
        return false;
    }

    if ( (string) $args->slug !== (string) $plugin_slug ) {
        // Conserve the value of previous filter of plugins list in alternate API
        return $def;
    }

    // Get the current version
    $plugin_info     = get_site_transient( 'update_plugins' );
    $current_version = $plugin_info->checked[ $plugin_slug . '/' . $plugin_slug . '.php' ];
    $args->version   = $current_version;

    $request_string = gb_prepare_request( $action, $args );

    $request = wp_remote_post( $api_url, $request_string );

    if ( is_wp_error( $request ) ) {
        $res = new WP_Error( 'plugins_api_failed', __( 'An Unexpected HTTP Error occurred during the API request.</p> <p><a href="?" onclick="document.location.reload(); return false;">Try again</a>' ), $request->get_error_message() );
    } else {
        $res = unserialize( $request['body'] );

        if ( $res === false ) {
            $res = new WP_Error( 'plugins_api_failed', __( 'An unknown error occurred' ), $request['body'] );
        }
    }

    return $res;
}

function gb_prepare_request( $action, $args ) {
    global $wp_version;

    return [
        'body'       => [
            'action'  => $action,
            'request' => serialize( $args ),
            'api-key' => md5( get_bloginfo( 'url' ) ),
        ],
        'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ),
    ];
}

And here's the server side updater:

<?php
/**
 * WordPress Update Server
 *
 * @version 0.0.1
 */

$packages['plugin-slug'] = [
    'versions' => [
        '1.0.0' => [
            'name'              => 'WP Awesome Plugin',
            'version'           => '1.0.0',
            'new_version'       => '1.0.0',
            'date'              => '2023-02-10 14:17:00',
            'tested'            => '6.1.1',
            'package'           => 'https://127.0.0.1/plugin-slug-1.0.0.zip',
            //
            'author'            => '<a href="https://127.0.0.1/">Author</a>',
            'author_profile'    => 'https://127.0.0.1/',
            'homepage'          => 'https://127.0.0.1/wordpress-plugins/plugin-slug/',
            'requires'          => '5.8',
            'requires_php'      => '7.0',
            'description'       => 'This is an awesome plugin!',
            'short_description' => 'This is an awesome plugin!',
        ],
    ],
    'info'     => [
        'url' => '',
    ],
];



// Process API requests
$action = (string) $_POST['action'];
$args   = unserialize( $_POST['request'] );

if ( is_array( $args ) ) {
    $args = array_to_object( $args );
}

$latest_package = array_shift( $packages[ $args->slug ]['versions'] );



// Process basic_check request
if ( $action === 'basic_check' ) {
    $update_info       = array_to_object( $latest_package );
    $update_info->slug = $args->slug;

    if ( version_compare( $args->version, $latest_package['version'], '<' ) ) {
        $update_info->new_version = $update_info->version;

        print serialize( $update_info );
    }
}



// Process plugin_information request
if ( $action === 'plugin_information' ) {
    $data = new stdClass;

    $data->slug              = $args->slug;
    $data->name              = $latest_package['name'];
    $data->version           = $latest_package['version'];
    $data->new_version       = $latest_package['new_version'];
    $data->last_updated      = $latest_package['date'];
    $data->download_link     = $latest_package['package'];
    $data->tested            = $latest_package['tested'];
    //
    $data->author            = $latest_package['author'];
    $data->author_profile    = $latest_package['author_profile'];
    $data->homepage          = $latest_package['homepage'];
    $data->requires          = $latest_package['requires'];
    $data->requires_php      = $latest_package['requires_php'];
    //
    $data->description       = $latest_package['description'];
    $data->short_description = $latest_package['short_description'];

    $data->sections = [
        'description' => $latest_package['description'],
    ];

    print serialize( $data );
}



function array_to_object( $array = [] ) {
    if ( empty( $array ) || ! is_array( $array ) ) {
        return false;
    }

    $data = new stdClass;

    foreach ( $array as $akey => $aval ) {
        $data->{$akey} = $aval;
    }

    return $data;
}

I'm wondering how would it be to add a "Change Log" section/tab to the item? Any help is appreciated

Share Improve this question asked May 3, 2024 at 12:45 user242510user242510
Add a comment  | 

1 Answer 1

Reset to default 1

I believe you have to add it into your plugin data array, see below:

$packages['plugin-slug'] = [
    'versions' => [
        '1.0.0' => [
            'name'              => 'WP Awesome Plugin',
            'version'           => '1.0.0',
            'new_version'       => '1.0.0',
            'date'              => '2023-02-10 14:17:00',
            'tested'            => '6.1.1',
            'package'           => 'https://127.0.0.1/plugin-slug-1.0.0.zip',
            //
            'author'            => '<a href="https://127.0.0.1/">Author</a>',
            'author_profile'    => 'https://127.0.0.1/',
            'homepage'          => 'https://127.0.0.1/wordpress-plugins/plugin-slug/',
            'requires'          => '5.8',
            'requires_php'      => '7.0',
            'description'       => 'This is an awesome plugin!',
            'short_description' => 'This is an awesome plugin!',
            'changelog' => 'Put your changelog here.'
        ],
    ],
    'info'     => [
        'url' => '',
    ],
];

Then, when you're running through the output for your plugin updater:

if ( $action === 'plugin_information' ) {
    $data = new stdClass;

    $data->slug              = $args->slug;
    $data->name              = $latest_package['name'];
    $data->version           = $latest_package['version'];
    $data->new_version       = $latest_package['new_version'];
    $data->last_updated      = $latest_package['date'];
    $data->download_link     = $latest_package['package'];
    $data->tested            = $latest_package['tested'];
    //
    $data->author            = $latest_package['author'];
    $data->author_profile    = $latest_package['author_profile'];
    $data->homepage          = $latest_package['homepage'];
    $data->requires          = $latest_package['requires'];
    $data->requires_php      = $latest_package['requires_php'];
    //
    $data->description       = $latest_package['description'];
    $data->short_description = $latest_package['short_description'];
    $data->sections = [
        'description' => $latest_package['description'],
        'changelog'  => $latest_package['changelog'],
    ];

    print serialize( $data );
}

I think the changelog is supposed to be in the sections portion.

本文标签: customizationAdding quotChangelogquot as a second tab to the auto updater custom plugin