Skip to content

Commit b7612de

Browse files
author
Praesidiarius
committed
2nd stable release. updater added
1 parent 9c17f2c commit b7612de

File tree

5 files changed

+401
-0
lines changed

5 files changed

+401
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* Plugin loader.
5+
*
6+
* @package OnePlace\Swissknife
7+
* @copyright 2019 Verein onePlace
8+
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License, version 2
9+
* @link https://1plc.ch/wordpress-plugins/swissknife
10+
*/
11+
12+
namespace OnePlace\Swissknife\Modules;
13+
14+
use OnePlace\Swissknife\Plugin;
15+
16+
final class Sitekit {
17+
/**
18+
* Main instance of the module
19+
*
20+
* @since 0.1-stable
21+
* @var Plugin|null
22+
*/
23+
private static $instance = null;
24+
25+
/**
26+
* Enable Google Sitekit IP Anonymization
27+
*
28+
* @since 0.1-stable
29+
*/
30+
public function register() {
31+
// Google Site-kit Anonymizing IP Addresses
32+
add_filter('googlesitekit_gtag_opt', function(array $options) {
33+
$options['anonymize_ip'] = true;
34+
return $options;
35+
} );
36+
}
37+
38+
/**
39+
* Loads the plugin main instance and initializes it.
40+
*
41+
* @since 0.1-stable
42+
*
43+
* @param string $main_file Absolute path to the plugin main file.
44+
* @return bool True if the plugin main instance could be loaded, false otherwise.
45+
*/
46+
public static function load() {
47+
if ( null !== static::$instance ) {
48+
return false;
49+
}
50+
static::$instance = new self();
51+
static::$instance->register();
52+
return true;
53+
}
54+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
/**
4+
* Plugin loader.
5+
*
6+
* @package OnePlace\Swissknife
7+
* @copyright 2019 Verein onePlace
8+
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License, version 2
9+
* @link https://1plc.ch/wordpress-plugins/swissknife
10+
*/
11+
12+
namespace OnePlace\Swissknife\Modules;
13+
14+
use OnePlace\Swissknife\Plugin;
15+
16+
final class Updater {
17+
/**
18+
* Main instance of the module
19+
*
20+
* @since 0.1-stable
21+
* @var Plugin|null
22+
*/
23+
private static $instance = null;
24+
25+
private $sRepoUser;
26+
private $sRepoName;
27+
private $sRepoToken;
28+
private $sSlug;
29+
private $githubAPIResult;
30+
private $aPluginData;
31+
32+
/**
33+
* Enable Google Sitekit IP Anonymization
34+
*
35+
* @since 0.1-stable
36+
*/
37+
public function register() {
38+
$this->sRepoName = 'PLC_WP_Swissknife';
39+
$this->sRepoUser = 'OnePlc';
40+
$this->sRepoToken = '';
41+
$this->sSlug = 'wpplc-swissknife';
42+
$this->aPluginData = [
43+
'Name' => 'WP PLC Swissknife',
44+
'AuthorName' => 'onePlace',
45+
'PluginURI' => 'https://wp.1plc.ch/wordpress-plugins/swissknife',
46+
'Description' => 'onePlace Swissknife for Wordpress. Increase Wordpress Security and Performance',
47+
];
48+
49+
// For TESTING ONLY
50+
set_site_transient( 'update_plugins', null );
51+
52+
add_filter( "pre_set_site_transient_update_plugins", [$this, "setTransient" ] );
53+
54+
add_filter( "plugins_api", [ $this, "setPluginInfo" ], 10, 3 );
55+
}
56+
57+
// Push in plugin version information to display in the details lightbox
58+
public function setPluginInfo( $false, $action, $response ) {
59+
// Get plugin & GitHub release information
60+
//$this->initPluginData();
61+
$this->getRepoReleaseInfo();
62+
63+
// If nothing is found, do nothing
64+
if ( empty( $response->slug ) || $response->slug != $this->sSlug ) {
65+
return false;
66+
}
67+
68+
// Add our plugin information
69+
$response->last_updated = $this->githubAPIResult->published_at;
70+
$response->slug = $this->sSlug;
71+
$response->plugin_name = $this->pluginData["Name"];
72+
$response->version = $this->githubAPIResult->tag_name;
73+
$response->author = $this->pluginData["AuthorName"];
74+
$response->homepage = $this->pluginData["PluginURI"];
75+
76+
// Create tabs in the lightbox
77+
$response->sections = [
78+
'description' => $this->pluginData["Description"],
79+
'changelog' => class_exists( "Parsedown" )
80+
? \Parsedown::instance()->parse( $this->githubAPIResult->body )
81+
: $this->githubAPIResult->body
82+
];
83+
84+
// This is our release download zip file
85+
$downloadLink = $this->githubAPIResult->zipball_url;
86+
87+
// Include the access token for private GitHub repos
88+
if ( !empty( $this->sRepoToken ) ) {
89+
$downloadLink = add_query_arg(
90+
[ "access_token" => $this->sRepoToken ],
91+
$downloadLink
92+
);
93+
}
94+
$response->download_link = $downloadLink;
95+
96+
return $response;
97+
}
98+
99+
public function setTransient($transient) {
100+
// If we have checked the plugin data before, don't re-check
101+
if ( empty( $transient->checked ) ) {
102+
return $transient;
103+
}
104+
105+
// Get plugin & GitHub release information
106+
$this->getRepoReleaseInfo();
107+
108+
// Check the versions if we need to do an update
109+
$doUpdate = version_compare( $this->githubAPIResult->tag_name, $transient->checked[$this->sSlug] );
110+
111+
// Update the transient to include our updated plugin data
112+
if ( $doUpdate == 1 ) {
113+
$package = $this->githubAPIResult->zipball_url;
114+
115+
// Include the access token for private GitHub repos
116+
if ( !empty( $this->sRepoToken ) ) {
117+
$package = add_query_arg( [ "access_token" => $this->sRepoToken ], $package );
118+
}
119+
120+
$obj = new \stdClass();
121+
$obj->slug = $this->sSlug;
122+
$obj->new_version = $this->githubAPIResult->tag_name;
123+
$obj->url = 'https://1plc.ch';
124+
$obj->package = $package;
125+
$obj->tested = '5.3';
126+
$obj->requires = '5.2.4';
127+
128+
$transient->response[$this->sSlug.'/'.$this->sSlug.'.php'] = $obj;
129+
}
130+
131+
return $transient;
132+
}
133+
134+
public function getRepoReleaseInfo() {
135+
if ( ! empty( $this->githubAPIResult ) ) {
136+
return;
137+
}
138+
// Query the Gitea API
139+
$url = "https://api.github.com/repos/{$this->sRepoUser}/{$this->sRepoName}/releases";
140+
141+
// We need the access token for private repos
142+
if ( ! empty( $this->sRepoToken ) ) {
143+
$url = add_query_arg( [ "access_token" => $this->sRepoToken ], $url );
144+
}
145+
// Get the results
146+
$this->githubAPIResult = wp_remote_retrieve_body( wp_remote_get( $url, ['sslverify'=>false] ) );
147+
148+
if ( ! empty( $this->githubAPIResult ) ) {
149+
$this->githubAPIResult = json_decode( $this->githubAPIResult );
150+
}
151+
152+
// Use only the latest release
153+
if ( is_array( $this->githubAPIResult ) ) {
154+
$this->githubAPIResult = $this->githubAPIResult[0];
155+
}
156+
}
157+
158+
/**
159+
* Loads the plugin main instance and initializes it.
160+
*
161+
* @since 0.1-stable
162+
*
163+
* @param string $main_file Absolute path to the plugin main file.
164+
* @return bool True if the plugin main instance could be loaded, false otherwise.
165+
*/
166+
public static function load() {
167+
if ( null !== static::$instance ) {
168+
return false;
169+
}
170+
static::$instance = new self();
171+
static::$instance->register();
172+
return true;
173+
}
174+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* Plugin loader.
5+
*
6+
* @package OnePlace\Swissknife
7+
* @copyright 2019 Verein onePlace
8+
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License, version 2
9+
* @link https://1plc.ch/wordpress-plugins/swissknife
10+
*/
11+
12+
namespace OnePlace\Swissknife;
13+
14+
/**
15+
* Main class for the plugin
16+
*/
17+
final class Plugin {
18+
/**
19+
* Main instance of the plugin.
20+
*
21+
* @since 0.1-stable
22+
* @var Plugin|null
23+
*/
24+
private static $instance = null;
25+
26+
/**
27+
* Retrieves the main instance of the plugin.
28+
*
29+
* @since 0.1-stable
30+
*
31+
* @return Plugin Plugin main instance.
32+
*/
33+
public static function instance() {
34+
return static::$instance;
35+
}
36+
37+
/**
38+
* Registers the plugin with WordPress.
39+
*
40+
* @since 0.1-stable
41+
*/
42+
public function register() {
43+
// Enable Sitekit Custom Settings
44+
Modules\Sitekit::load();
45+
46+
// Enable Auto-Updates via Github
47+
Modules\Updater::load();
48+
}
49+
50+
/**
51+
* Loads the plugin main instance and initializes it.
52+
*
53+
* @since 0.1-stable
54+
*
55+
* @param string $main_file Absolute path to the plugin main file.
56+
* @return bool True if the plugin main instance could be loaded, false otherwise.
57+
*/
58+
public static function load( $main_file ) {
59+
if ( null !== static::$instance ) {
60+
return false;
61+
}
62+
static::$instance = new static( $main_file );
63+
static::$instance->register();
64+
return true;
65+
}
66+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/**
4+
* Plugin loader.
5+
*
6+
* @package OnePlace\Swissknife
7+
* @copyright 2019 Verein onePlace
8+
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License, version 2
9+
* @link https://1plc.ch/wordpress-plugins/swissknife
10+
*/
11+
12+
namespace OnePlace\Swissknife;
13+
14+
/**
15+
* Load composer autoload files
16+
*
17+
* we currently don't have any external libs
18+
*
19+
require __DIR__ . '/vendor/autoload.php';
20+
**/
21+
22+
// Load Plugin
23+
require_once __DIR__.'/Plugin.php';
24+
25+
// Load Modules
26+
require_once __DIR__.'/Modules/Sitekit.php';
27+
require_once __DIR__.'/Modules/Updater.php';
28+
29+
ini_set('display_errors', 1);
30+
ini_set('display_startup_errors', 1);
31+
error_reporting(E_ALL);
32+
33+
Plugin::load(WPPLC_SWISSKNIFE_MAIN_FILE);
34+

0 commit comments

Comments
 (0)