|
| 1 | +<?php |
| 2 | +namespace Soderlind\WordPress; |
| 3 | + |
| 4 | +use YahnisElsts\PluginUpdateChecker\v5\PucFactory; |
| 5 | + |
| 6 | +/** |
| 7 | + * Generic WordPress Plugin GitHub Updater |
| 8 | + * |
| 9 | + * A reusable class for handling WordPress plugin updates from GitHub repositories |
| 10 | + * using the plugin-update-checker library. |
| 11 | + * |
| 12 | + * @package Soderlind\WordPress |
| 13 | + * @version 1.0.0 |
| 14 | + * @author Per Soderlind |
| 15 | + * @license GPL-2.0+ |
| 16 | + */ |
| 17 | +class GitHub_Plugin_Updater { |
| 18 | + /** |
| 19 | + * @var string GitHub repository URL |
| 20 | + */ |
| 21 | + private $github_url; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var string Branch to check for updates |
| 25 | + */ |
| 26 | + private $branch; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var string Regex pattern to match the plugin zip file name |
| 30 | + */ |
| 31 | + private $name_regex; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var string The plugin slug |
| 35 | + */ |
| 36 | + private $plugin_slug; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var string The main plugin file path |
| 40 | + */ |
| 41 | + private $plugin_file; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var bool Whether to enable release assets |
| 45 | + */ |
| 46 | + private $enable_release_assets; |
| 47 | + |
| 48 | + /** |
| 49 | + * Constructor |
| 50 | + * |
| 51 | + * @param array $config Configuration array with the following keys: |
| 52 | + * - github_url: GitHub repository URL (required) |
| 53 | + * - plugin_file: Main plugin file path (required) |
| 54 | + * - plugin_slug: Plugin slug for updates (required) |
| 55 | + * - branch: Branch to check for updates (default: 'main') |
| 56 | + * - name_regex: Regex pattern for zip file name (optional) |
| 57 | + * - enable_release_assets: Whether to enable release assets (default: true if name_regex provided) |
| 58 | + */ |
| 59 | + public function __construct( $config = array() ) { |
| 60 | + // Validate required parameters |
| 61 | + $required = array( 'github_url', 'plugin_file', 'plugin_slug' ); |
| 62 | + foreach ( $required as $key ) { |
| 63 | + if ( empty( $config[ $key ] ) ) { |
| 64 | + throw new \InvalidArgumentException( "Required parameter '{$key}' is missing or empty." ); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + $this->github_url = $config[ 'github_url' ]; |
| 69 | + $this->plugin_file = $config[ 'plugin_file' ]; |
| 70 | + $this->plugin_slug = $config[ 'plugin_slug' ]; |
| 71 | + $this->branch = isset( $config[ 'branch' ] ) ? $config[ 'branch' ] : 'main'; |
| 72 | + $this->name_regex = isset( $config[ 'name_regex' ] ) ? $config[ 'name_regex' ] : ''; |
| 73 | + $this->enable_release_assets = isset( $config[ 'enable_release_assets' ] ) |
| 74 | + ? $config[ 'enable_release_assets' ] |
| 75 | + : ! empty( $this->name_regex ); |
| 76 | + |
| 77 | + // Initialize the updater |
| 78 | + add_action( 'init', array( $this, 'setup_updater' ) ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Set up the update checker using GitHub integration |
| 83 | + */ |
| 84 | + public function setup_updater() { |
| 85 | + try { |
| 86 | + $update_checker = PucFactory::buildUpdateChecker( |
| 87 | + $this->github_url, |
| 88 | + $this->plugin_file, |
| 89 | + $this->plugin_slug |
| 90 | + ); |
| 91 | + |
| 92 | + $update_checker->setBranch( $this->branch ); |
| 93 | + |
| 94 | + // Enable release assets if configured |
| 95 | + if ( $this->enable_release_assets && ! empty( $this->name_regex ) ) { |
| 96 | + $update_checker->getVcsApi()->enableReleaseAssets( $this->name_regex ); |
| 97 | + } |
| 98 | + |
| 99 | + } catch (\Exception $e) { |
| 100 | + // Log error if WordPress debug is enabled |
| 101 | + if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 102 | + error_log( 'GitHub Plugin Updater Error: ' . $e->getMessage() ); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Create updater instance with minimal configuration |
| 109 | + * |
| 110 | + * @param string $github_url GitHub repository URL |
| 111 | + * @param string $plugin_file Main plugin file path |
| 112 | + * @param string $plugin_slug Plugin slug |
| 113 | + * @param string $branch Branch name (default: 'main') |
| 114 | + * |
| 115 | + * @return GitHub_Plugin_Updater |
| 116 | + */ |
| 117 | + public static function create( $github_url, $plugin_file, $plugin_slug, $branch = 'main' ) { |
| 118 | + return new self( array( |
| 119 | + 'github_url' => $github_url, |
| 120 | + 'plugin_file' => $plugin_file, |
| 121 | + 'plugin_slug' => $plugin_slug, |
| 122 | + 'branch' => $branch, |
| 123 | + ) ); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Create updater instance for plugins with release assets |
| 128 | + * |
| 129 | + * @param string $github_url GitHub repository URL |
| 130 | + * @param string $plugin_file Main plugin file path |
| 131 | + * @param string $plugin_slug Plugin slug |
| 132 | + * @param string $name_regex Regex pattern for release assets |
| 133 | + * @param string $branch Branch name (default: 'main') |
| 134 | + * |
| 135 | + * @return GitHub_Plugin_Updater |
| 136 | + */ |
| 137 | + public static function create_with_assets( $github_url, $plugin_file, $plugin_slug, $name_regex, $branch = 'main' ) { |
| 138 | + return new self( array( |
| 139 | + 'github_url' => $github_url, |
| 140 | + 'plugin_file' => $plugin_file, |
| 141 | + 'plugin_slug' => $plugin_slug, |
| 142 | + 'branch' => $branch, |
| 143 | + 'name_regex' => $name_regex, |
| 144 | + ) ); |
| 145 | + } |
| 146 | +} |
0 commit comments