Upload folder via GUI - integrations
This commit is contained in:
113
integrations/classes/class-integrations.php
Normal file
113
integrations/classes/class-integrations.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class WMF_Integrations {
|
||||
private static $instance=null;
|
||||
private static $hooked=false;
|
||||
private $option_name='_wmf_service_credentials';
|
||||
private $services=array();
|
||||
private $grouped_services=array();
|
||||
private $credentials=array();
|
||||
private $notice=array();
|
||||
public $action_update='wmf-service-update';
|
||||
public $integrations_action='wmf-integrations-update';
|
||||
|
||||
public static function instance() {
|
||||
if(is_null(self::$instance)) self::$instance=new self();
|
||||
return self::$instance;
|
||||
}
|
||||
private function __construct() {
|
||||
$b=WMF_INT_DIR;
|
||||
$services=array(
|
||||
'recaptcha/class-service-recaptcha.php' =>'WMF_Service_Recaptcha',
|
||||
'recaptchav3/class-service-recaptchav3.php' =>'WMF_Service_RecaptchaV3',
|
||||
'mailchimp/class-service-mailchimp.php' =>'WMF_Service_Mailchimp',
|
||||
'mailerlite/class-service-mailerlite.php' =>'WMF_Service_MailerLite',
|
||||
'active-campaign/class-service-active-campaign.php'=>'WMF_Service_ActiveCampaign',
|
||||
'convertkit/class-service-convertkit.php' =>'WMF_Service_ConvertKit',
|
||||
'stripe/class-service-stripe.php' =>'WMF_Service_Stripe',
|
||||
'paypal/class-service-paypal.php' =>'WMF_Service_PayPal',
|
||||
'zapier/class-service-zapier.php' =>'WMF_Service_Zapier',
|
||||
'google-analytics/class-service-google-analytics.php'=>'WMF_Service_Google_Analytics',
|
||||
'google-places/class-service-google-places.php' =>'WMF_Service_Google_Places',
|
||||
);
|
||||
foreach($services as $file=>$cls) {
|
||||
require_once $b.'services/'.$file;
|
||||
$this->register_service($cls);
|
||||
}
|
||||
}
|
||||
public function hook() {
|
||||
if(self::$hooked) return; self::$hooked=true;
|
||||
add_action('wp_ajax_'.$this->action_update, array($this,'ajax_service_update'));
|
||||
add_action('wp_ajax_'.$this->integrations_action,array($this,'ajax_integrations_update'));
|
||||
add_action('wmf_integrations_print_notices', array($this,'print_notices'));
|
||||
$this->read_credentials();
|
||||
$this->configure_services();
|
||||
}
|
||||
public function register_service($cls) {
|
||||
$s=$cls instanceof WMF_Service?$cls:new $cls();
|
||||
$this->services[$s->id]=$s;
|
||||
}
|
||||
public function configure_services() {
|
||||
foreach($this->services as $s) {
|
||||
$s->set_credentials($this->credentials[$s->id]??array());
|
||||
$s->configure(); $s->load();
|
||||
}
|
||||
}
|
||||
public function read_credentials(){$this->credentials=get_option($this->option_name,array());}
|
||||
public function write_credentials(){
|
||||
$this->credentials=array_map(fn($s)=>$s->get_credentials(),$this->services);
|
||||
update_option($this->option_name,$this->credentials);
|
||||
}
|
||||
public function get_services(){return $this->services;}
|
||||
public function get_service($id){return $this->services[$id]??false;}
|
||||
public function get_service_group($group){
|
||||
// Neu aufbauen falls noch nicht geschehen oder leer
|
||||
if(empty($this->grouped_services)) {
|
||||
$this->grouped_services = array();
|
||||
foreach($this->services as $s) {
|
||||
$this->grouped_services[$s->group][] = $s;
|
||||
}
|
||||
}
|
||||
return $this->grouped_services[$group] ?? array();
|
||||
}
|
||||
|
||||
public function reset_grouped_cache() {
|
||||
$this->grouped_services = array();
|
||||
}
|
||||
public function ajax_service_update() {
|
||||
if(!check_ajax_referer($this->action_update)||!current_user_can('manage_options')) wp_die();
|
||||
$services=(array)($_REQUEST['services']??array());
|
||||
$group=sanitize_text_field($_REQUEST['group']??'');
|
||||
ob_start();
|
||||
foreach($services as $sid) {
|
||||
if(!isset($this->services[$sid])) continue;
|
||||
$svc=$this->services[$sid];
|
||||
$def=$svc->get_credentials();
|
||||
$raw=$_REQUEST['credentials'][$sid]??array();
|
||||
$creds=array_map('sanitize_text_field',array_intersect_key(wp_parse_args($raw,$def),$def));
|
||||
$svc->set_credentials($creds,$raw);
|
||||
$this->write_credentials();
|
||||
}
|
||||
$this->notice=array('status'=>'success','message'=>'Änderungen gespeichert.');
|
||||
if($group==='antispam') require WMF_INT_DIR.'templates/admin-antispam-integrations.php';
|
||||
echo ob_get_clean(); die();
|
||||
}
|
||||
public function ajax_integrations_update() {
|
||||
if(!check_ajax_referer($this->integrations_action)||!current_user_can('manage_options')) wp_die();
|
||||
if(!isset($_REQUEST['service'],$_REQUEST['credentials'])) wp_die();
|
||||
$sid=sanitize_text_field($_REQUEST['service']);
|
||||
if(!isset($this->services[$sid])) wp_send_json_error();
|
||||
$svc=$this->services[$sid]; $def=$svc->get_credentials();
|
||||
$raw=$_REQUEST['credentials'][$sid]??array();
|
||||
$creds=array_map('sanitize_text_field',array_intersect_key(wp_parse_args($raw,$def),$def));
|
||||
$prev=$svc->get_credentials(); $svc->set_credentials($creds,$raw); $this->write_credentials();
|
||||
$this->notice=array('status'=>'success','message'=>'Änderungen gespeichert.');
|
||||
ob_start(); $svc->admin_widget($prev); echo ob_get_clean(); die();
|
||||
}
|
||||
public function print_notices() {
|
||||
if(empty($this->notice)) return;
|
||||
printf('<div class="notice notice-%s"><p>%s</p></div>',esc_attr($this->notice['status']),esc_html($this->notice['message']));
|
||||
}
|
||||
}
|
||||
function wmf_get_integrations(){return WMF_Integrations::instance();}
|
||||
Reference in New Issue
Block a user