152 lines
5.3 KiB
PHP
152 lines
5.3 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: Ansico Plugins
|
|
* Plugin URI: https://ansico.dk
|
|
* Description: Gives access to Ansico plugin directory with Ansico plugins
|
|
* Version: 1.0.0
|
|
* Author: Andreas Andersen (Ansico)
|
|
* Author URI: https://ansico.dk
|
|
* Contributors: aphandersen
|
|
* Support URI: https://ansico.dk/Ansico/Ansico-plugins
|
|
* License: GPL-3.0-or-later
|
|
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
|
|
* Tested up to: 6.9.4
|
|
* Text Domain: ansico-plugins
|
|
* Requires at least: 6.3
|
|
* Requires PHP: 7.0
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
define('ANSICO_PLUGINS_VERSION', '1.0.0');
|
|
define('ANSICO_PLUGINS_MANAGED_OPTION', 'ansico_plugins_managed_plugins');
|
|
define('ANSICO_PLUGINS_FILE', __FILE__);
|
|
define('ANSICO_PLUGINS_DIR', plugin_dir_path(__FILE__));
|
|
define('ANSICO_PLUGINS_URL', plugin_dir_url(__FILE__));
|
|
define('ANSICO_PLUGINS_OPTION', 'ansico_plugins_settings');
|
|
|
|
require_once ANSICO_PLUGINS_DIR . 'includes/class-ansico-plugins-client.php';
|
|
require_once ANSICO_PLUGINS_DIR . 'includes/class-ansico-plugins-admin.php';
|
|
require_once ANSICO_PLUGINS_DIR . 'includes/class-ansico-plugins-installer.php';
|
|
require_once ANSICO_PLUGINS_DIR . 'includes/class-ansico-plugins-updater.php';
|
|
|
|
final class Ansico_Plugins {
|
|
private static $instance = null;
|
|
private $admin;
|
|
private $installer;
|
|
private $updater;
|
|
|
|
public static function instance() {
|
|
if (null === self::$instance) {
|
|
self::$instance = new self();
|
|
}
|
|
|
|
return self::$instance;
|
|
}
|
|
|
|
private function __construct() {
|
|
$this->admin = new Ansico_Plugins_Admin();
|
|
$this->installer = new Ansico_Plugins_Installer();
|
|
$this->updater = new Ansico_Plugins_Updater();
|
|
|
|
add_action('admin_init', array($this, 'maybe_handle_actions'));
|
|
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'plugin_action_links'));
|
|
}
|
|
|
|
public static function default_settings() {
|
|
return array(
|
|
'forgejo_base_url' => 'https://ansico.dk',
|
|
'forgejo_owner' => 'Ansico',
|
|
'owner_type' => 'org',
|
|
'access_token' => '',
|
|
'topic_filter' => 'wordpress-plugin',
|
|
'verify_ssl' => 1,
|
|
'request_timeout' => 20,
|
|
'self_update_enabled' => 1,
|
|
'self_update_owner' => 'Ansico',
|
|
'self_update_repo' => 'Ansico-plugins',
|
|
);
|
|
|
|
}
|
|
|
|
public static function get_settings() {
|
|
$settings = get_option(ANSICO_PLUGINS_OPTION, array());
|
|
return wp_parse_args($settings, self::default_settings());
|
|
}
|
|
|
|
|
|
public static function clear_http_cache() {
|
|
global $wpdb;
|
|
$like = $wpdb->esc_like('_transient_ansico_plugins_') . '%';
|
|
$like_timeout = $wpdb->esc_like('_transient_timeout_ansico_plugins_') . '%';
|
|
$wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", $like, $like_timeout));
|
|
}
|
|
|
|
public static function get_managed_plugins() {
|
|
$managed = get_option(ANSICO_PLUGINS_MANAGED_OPTION, array());
|
|
return is_array($managed) ? $managed : array();
|
|
}
|
|
|
|
public static function set_managed_plugin($plugin_file, $meta) {
|
|
$managed = self::get_managed_plugins();
|
|
$managed[$plugin_file] = wp_parse_args($meta, array(
|
|
'slug' => dirname($plugin_file),
|
|
'owner' => '',
|
|
'repo' => '',
|
|
'description' => '',
|
|
'repo_html_url' => '',
|
|
));
|
|
update_option(ANSICO_PLUGINS_MANAGED_OPTION, $managed, false);
|
|
}
|
|
|
|
public function plugin_action_links($links) {
|
|
$settings_link = sprintf(
|
|
'<a href="%s">%s</a>',
|
|
esc_url(admin_url('options-general.php?page=ansico-plugins')),
|
|
esc_html__('Indstillinger', 'ansico-plugins')
|
|
);
|
|
array_unshift($links, $settings_link);
|
|
return $links;
|
|
}
|
|
|
|
public function maybe_handle_actions() {
|
|
if (!is_admin() || !current_user_can('install_plugins')) {
|
|
return;
|
|
}
|
|
|
|
if (empty($_GET['ansico_action'])) {
|
|
return;
|
|
}
|
|
|
|
$action = sanitize_key(wp_unslash($_GET['ansico_action']));
|
|
|
|
if (!isset($_GET['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'])), 'ansico_plugins_action')) {
|
|
wp_die(esc_html__('Ugyldig sikkerheds-token.', 'ansico-plugins'));
|
|
}
|
|
|
|
if ('install_latest' === $action) {
|
|
$repo = isset($_GET['repo']) ? sanitize_text_field(wp_unslash($_GET['repo'])) : '';
|
|
$slug = isset($_GET['slug']) ? sanitize_title(wp_unslash($_GET['slug'])) : '';
|
|
$this->installer->install_latest_release($repo, $slug);
|
|
}
|
|
|
|
if ('delete_plugin' === $action) {
|
|
$repo = isset($_GET['repo']) ? sanitize_text_field(wp_unslash($_GET['repo'])) : '';
|
|
$plugin_file = isset($_GET['plugin_file']) ? sanitize_text_field(wp_unslash($_GET['plugin_file'])) : '';
|
|
$this->installer->delete_plugin($repo, $plugin_file);
|
|
}
|
|
}
|
|
}
|
|
|
|
register_activation_hook(__FILE__, function() {
|
|
if (!get_option(ANSICO_PLUGINS_OPTION)) {
|
|
add_option(ANSICO_PLUGINS_OPTION, Ansico_Plugins::default_settings());
|
|
}
|
|
if (!get_option(ANSICO_PLUGINS_MANAGED_OPTION)) {
|
|
add_option(ANSICO_PLUGINS_MANAGED_OPTION, array());
|
|
}
|
|
});
|
|
|
|
Ansico_Plugins::instance();
|