Issue with Transients

 avatar
unknown
php
4 years ago
2.0 kB
212
No Index
add_filter( 'site_transient_update_plugins', 'push_update' );

function push_update( $transient ) {

	$slug                   = 'my-plugin';
	$plugin_name            = 'my-plugin/my-plugin.php';
	$plugin_data            = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_name );
	$plugin_current_version = $plugin_data['Version'];

	$result = wp_remote_get('https://example.com/myapi');

	if (
		is_wp_error( $result )
		|| 200 !== wp_remote_retrieve_response_code( $result )
		|| empty( wp_remote_retrieve_body( $result ) )
	) {
		return $transient;
	}

	$remote = json_decode( $result['body'] );

	// Query premium/private repo for updates.
	$update = (object) array(
		'id'            => $plugin_name,
		'slug'          => $slug,
		'plugin'        => $plugin_name,
		'new_version'   => $remote->version,
		'package'       => $remote->url,
		'url'           => '',
		'icons'         => array(),
		'banners'       => array(),
		'banners_rtl'   => array(),
		'tested'        => '',
		'requires_php'  => '',
		'compatibility' => new stdClass(),
	);
	if ( version_compare( $plugin_current_version, $remote->version, '<' ) ) {
		// Update is available.
		// $update should be an array containing all of the fields in $item below.
		$transient->response[ $plugin_name ] = $update;
	} else {
		// No update is available.
		$item = (object) array(
			'id'            => $plugin_name,
			'slug'          => $slug,
			'plugin'        => $plugin_name,
			'new_version'   => $plugin_current_version,
			'url'           => '',
			'package'       => '',
			'icons'         => array(),
			'banners'       => array(),
			'banners_rtl'   => array(),
			'tested'        => '',
			'requires_php'  => '',
			'compatibility' => new stdClass(),
		);
		// Adding the "mock" item to the `no_update` property is required
		// for the enable/disable auto-updates links to correctly appear in UI.
		$transient->no_update[ $plugin_name ] = $item;
	}

	return $transient;
}
Editor is loading...