web-dev-qa-db-fra.com

Wordpress plugin de son propre serveur

Je développe un jeu, et je prévois cela, je vais avoir des partenaires pour qu'ils puissent exécuter un jeu comme le mien.

Pour cela, j'ai développé un plugin, avec quelques classes et fonctions, pour gérer les requêtes de jeu.

Parce que, ce plugin est totalement inutilisable pour tous les utilisateurs de wordpress, je veux envoyer ce plugin à mes partenaires dans un email au format zippé, mais je veux qu'ils puissent mettre à jour le plugin, si besoin est.

Alors, est-il possible de stocker le plug-in sur mon propre serveur et de le forcer à vérifier les mises à jour à partir de mon site et non à partir du répertoire des plug-ins wp?

5
vaso123

Tout le crédit au poste suivant va à AbidOmar . Le tutoriel complet est disponible sur Tuts +: Guide de l'API HTTP WordPress: Mises à jour automatiques des plugins

View Plugin On Github


Le Gist consiste à créer une nouvelle classe qui publiera un fichier externe (API) et renverra le plug-in à télécharger s'il est obsolète. Ce qui suit suppose que vous créez un plugin en utilisant le code suivant (pas dans le répertoire functions.php ou mu-plugins).

  1. wp_autoupdate.php sera un fichier externe (dans votre plugin) contenant la classe de mise à jour automatique définie ci-dessous
  2. update.php est un fichier "distant" externe utilisé pour vérifier la mise à jour, crée l'API.

Le ci-dessous sera notre configuration initiale.

add_action( 'init', 'wptuts_activate_au' );
function wptuts_activate_au()
{
    require_once ('wp_autoupdate.php');      // File which contains the Class below
    $wptuts_plugin_current_version = '1.0';
    $wptuts_plugin_remote_path     = 'http://localhost/update.php';
    $wptuts_plugin_slug            = plugin_basename(__FILE__);
    new wp_auto_update( $wptuts_plugin_current_version, $wptuts_plugin_remote_path, $wptuts_plugin_slug );
}

Classe de mise à jour automatique - wp_autoupdate.php

class wp_auto_update
{
    /**
     * The plugin current version
     * @var string
     */
    public $current_version;

    /**
     * The plugin remote update path
     * @var string
     */
    public $update_path;

    /**
     * Plugin Slug (plugin_directory/plugin_file.php)
     * @var string
     */
    public $plugin_slug;

    /**
     * Plugin name (plugin_file)
     * @var string
     */
    public $slug;

    /**
     * Initialize a new instance of the WordPress Auto-Update class
     * @param string $current_version
     * @param string $update_path
     * @param string $plugin_slug
     */
    function __construct( $current_version, $update_path, $plugin_slug )
    {
        // Set the class public variables
        $this->current_version = $current_version;
        $this->update_path     = $update_path;
        $this->plugin_slug     = $plugin_slug;
        list ($t1, $t2)        = explode('/', $plugin_slug);
        $this->slug            = str_replace( '.php', '', $t2 );

        // define the alternative API for updating checking
        add_filter( 'pre_set_site_transient_update_plugins', array( &$this, 'check_update' ) );

        // Define the alternative response for information checking
        add_filter('plugins_api', array(&$this, 'check_info'), 10, 3);
    }

    /**
     * Add our self-hosted autoupdate plugin to the filter transient
     *
     * @param $transient
     * @return object $ transient
     */
    public function check_update( $transient )
    {
        if( empty( $transient->checked ) ) {
            return $transient;
        }

        // Get the remote version
        $remote_version = $this->getRemote_version();

        // If a newer version is available, add the update
        if ( version_compare( $this->current_version, $remote_version, '<' ) ) {
            $obj          = new stdClass();
            $obj->slug    = $this->slug;
            $obj->new_version = $remote_version;
            $obj->url     = $this->update_path;
            $obj->package = $this->update_path;
            $transient->response[$this->plugin_slug] = $obj;
        }
        var_dump( $transient );
        return $transient;
    }

    /**
     * Add our self-hosted description to the filter
     *
     * @param boolean $false
     * @param array $action
     * @param object $arg
     * @return bool|object
     */
    public function check_info( $false, $action, $arg )
    {
        if( $arg->slug === $this->slug ) {
            $information = $this->getRemote_information();
            return $information;
        }
        return false;
    }

    /**
     * Return the remote version
     * @return string $remote_version
     */
    public function getRemote_version()
    {
        $request = wp_remote_post( $this->update_path, array( 'body' => array( 'action' => 'version' ) ) );
        if( ! is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) === 200 ) {
            return $request['body'];
        }
        return false;
    }

    /**
     * Get information about the remote version
     * @return bool|object
     */
    public function getRemote_information()
    {
        $request = wp_remote_post( $this->update_path, array( 'body' => array( 'action' => 'info' ) ) );
        if( ! is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) === 200) {
            return unserialize( $request['body'] );
        }
        return false;
    }

    /**
     * Return the status of the plugin licensing
     * @return boolean $remote_license
     */
    public function getRemote_license()
    {
        $request = wp_remote_post( $this->update_path, array( 'body' => array( 'action' => 'license' ) ) );
        if( ! is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) === 200 ) {
            return $request['body'];
        }
        return false;
    }
}

API de mise à jour (externe) distante - update.php

if( isset( $_POST['action'] ) ) {
  switch( $_POST['action'] ) {
    case 'version':
      echo '1.1';
        break;

    case 'info':
      $obj              = new stdClass();
      $obj->slug        = 'plugin.php';
      $obj->plugin_name = 'plugin.php';
      $obj->new_version = '1.1';
      $obj->requires    = '3.0';
      $obj->tested      = '3.3.1';
      $obj->downloaded  = 12540;
      $obj->last_updated = '2012-01-12';
      $obj->sections    = array(
        'description'     => 'The new version of the Auto-Update plugin',
        'another_section' => 'This is another section',
        'changelog'       => 'Some new features'
      );
      $obj->download_link = 'http://localhost/update.php';
      echo serialize( $obj );

    case 'license':
      echo 'false';
        break;
  }
} else {
    header( 'Cache-Control: public' );
    header( 'Content-Description: File Transfer' );
    header( 'Content-Type: application/Zip' );
    readfile( 'update.Zip' );
}

update.php conservera les informations les plus récentes sur votre plugin. En utilisant la fonction init, vous transmettriez la version actuelle installée afin qu’elle puisse la publier sur update.php et la comparer à la version actuelle. Il y a quelques améliorations à apporter au code pour le rendre plus simple, mais je l'ai utilisé pour faire exactement ce que vous essayez de faire, c'est donc un bon point de départ.

5
Howdy_McGee

Oui, il est possible d'héberger votre propre plugin et de le faire vérifier les mises à jour de votre serveur. Vous devez insérer du code personnalisé dans votre plug-in pour lui dire de vérifier auprès de votre serveur les mises à jour. Il existe quelques cadres pour vous aider à faire cela, essayez ceux-ci:

1
Ben Cole