-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhrswp-github-updater.php
159 lines (136 loc) · 4.21 KB
/
hrswp-github-updater.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* Plugin Name: HRSWP GitHub Updater
* Version: 1.1.4
* Description: A WSU HRS WordPress plugin to manage updates for GitHub-hosted plugins and themes.
* Author: Adam Turner, washingtonstateuniversity
* Author URI: https://hrs.wsu.edu/
* Plugin URI: https://github.com/washingtonstateuniversity/hrswp-github-updater
* Update URI: https://api.github.com/repos/washingtonstateuniversity/hrswp-github-updater/releases/latest
* Text Domain: hrswp-github-updater
* Requires at least: 5.8
* Tested up to: 6.3.1
* Requires PHP: 7.3
*
* @package HRSWP_GitHub_Updater
* @since 0.1.0
*/
namespace HRS\HrswpGitHubUpdater;
use HRS\HrswpGitHubUpdater\lib\options;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// Starts things up.
add_action( 'plugins_loaded', __NAMESPACE__ . '\pre_init' );
/* Register lifecycle methods. */
register_activation_hook( __FILE__, __NAMESPACE__ . '\activate' );
register_deactivation_hook( __FILE__, __NAMESPACE__ . '\deactivate' );
register_uninstall_hook( __FILE__, __NAMESPACE__ . '\uninstall' );
/**
* Displays a version notice.
*
* @since 0.1.0
*/
function wordpress_version_notice() {
printf(
'<div class="error"><p>%s</p></div>',
esc_html__( 'The HRSWP GitHub Updater requires WordPress 5.8.0 or later to function properly. Please upgrade WordPress before activating.', 'hrswp-github-updater' )
);
}
/**
* Verifies the plugin dependencies are present, then loads it.
*
* @since 0.1.0
*/
function pre_init() {
global $wp_version;
// Get unmodified $wp_version.
include ABSPATH . WPINC . '/version.php';
// Remove '-src' from the version string for `version_compare()`.
$version = preg_replace( '/-[A-Za-z-0-9]*$/', '', $wp_version );
if ( version_compare( $version, '5.8', '<' ) ) {
add_action( 'admin_notices', __NAMESPACE__ . '\wordpress_version_notice' );
deactivate_plugins( array( 'hrswp-github-updater/hrswp-github-updater.php' ) );
return;
}
/* Load required plugin files. */
require dirname( __FILE__ ) . '/inc/load.php';
}
/**
* Manages plugin metadata for ease of access.
*
* @since 0.1.0
*
* @param string $meta Optional. A specific plugin metadata key to return.
* @return string|array The requested metadata value or an array of all plugin metadata.
*/
function plugin_meta( $meta = '' ) {
$plugin_meta = array(
'path' => __FILE__,
'slug' => 'hrswp-github-updater',
'option_status' => 'hrswp_gu_status',
'option_plugins' => 'hrswp_gu_settings',
'transient_base' => 'hrswp_gu',
);
if ( '' !== $meta ) {
return $plugin_meta[ (string) $meta ];
}
return $plugin_meta;
}
/**
* Activates the plugin.
*
* @since 0.1.0
*/
function activate() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
if ( ! function_exists( __NAMESPACE__ . '\lib\options\update_plugin_option' ) ) {
require dirname( __FILE__ ) . '/lib/options.php';
}
if ( false === get_option( plugin_meta( 'option_status' ) ) ) {
options\set_default_options();
} else {
options\update_plugin_option( array( 'status' => 'active' ) );
}
}
/**
* Deactivates the plugin.
*
* @since 0.1.0
*/
function deactivate() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
if ( ! function_exists( __NAMESPACE__ . '\lib\options\update_plugin_option' ) ) {
require dirname( __FILE__ ) . '/lib/options.php';
}
options\update_plugin_option( array( 'status' => 'inactive' ) );
delete_transient( plugin_meta( 'transient_base' ) . '_timeout' );
}
/**
* Uninstalls the plugin.
*
* @since 0.1.0
*/
function uninstall() {
global $new_allowed_options, $wp_registered_settings;
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
if ( ! function_exists( __NAMESPACE__ . '\lib\options\update_plugin_option' ) ) {
require dirname( __FILE__ ) . '/lib/options.php';
}
// Remove plugin transients.
options\flush_transients();
// Unregister plugin settings if they exist.
if ( isset( $new_allowed_options[ plugin_meta( 'slug' ) ] ) && isset( $wp_registered_settings[ plugin_meta( 'option_plugins' ) ] ) ) {
unregister_setting( plugin_meta( 'slug' ), plugin_meta( 'option_plugins' ) );
}
// Remove plugin options.
delete_option( plugin_meta( 'option_status' ) );
delete_option( plugin_meta( 'option_plugins' ) );
}