initial
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Data_Store;
|
||||
|
||||
class Const_Data_Store implements Data_Store {
|
||||
|
||||
public function get( $setting_name, $connector ) {
|
||||
$const_name = sprintf( 'GRAVITYSMTP_%s_%s', strtoupper( $connector ), strtoupper( $setting_name ) );
|
||||
|
||||
if ( defined( $const_name ) ) {
|
||||
return constant( $const_name );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function save( $setting_name, $value, $connector ) {
|
||||
return;
|
||||
}
|
||||
|
||||
public function get_plugin_const( $setting_name ) {
|
||||
$const_name = sprintf( 'GRAVITYSMTP_%s', strtoupper( $setting_name ) );
|
||||
|
||||
if ( defined( $const_name ) ) {
|
||||
return constant( $const_name );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Data_Store;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Connectors\Endpoints\Save_Connector_Settings_Endpoint;
|
||||
use Gravity_Forms\Gravity_SMTP\Enums\Connector_Status_Enum;
|
||||
|
||||
class Data_Store_Router {
|
||||
|
||||
protected $const_data_store;
|
||||
|
||||
protected $opts_data_store;
|
||||
|
||||
protected $plugin_opts_data_store;
|
||||
|
||||
public function __construct( $const_data_store, $opts_data_store, $plugin_opts_data_store ) {
|
||||
$this->const_data_store = $const_data_store;
|
||||
$this->opts_data_store = $opts_data_store;
|
||||
$this->plugin_opts_data_store = $plugin_opts_data_store;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method for retrieving plugin settings (i.e., settings for the plugin globally
|
||||
* and not specific to this connector).
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param $setting_name
|
||||
* @param $default
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function get_plugin_setting( $setting_name, $default = null ) {
|
||||
if ( ! is_null( $this->const_data_store->get_plugin_const( $setting_name ) ) ) {
|
||||
return $this->const_data_store->get_plugin_const( $setting_name );
|
||||
}
|
||||
|
||||
$val = $this->plugin_opts_data_store->get( $setting_name );
|
||||
|
||||
if ( is_null( $val ) ) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
return $val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to retrieve a saved setting specifically for this connector.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param $connector
|
||||
* @param $setting_name
|
||||
* @param $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_setting( $connector, $setting_name, $default = null ) {
|
||||
if ( ! is_null( $this->const_data_store->get( $setting_name, $connector ) ) ) {
|
||||
return $this->const_data_store->get( $setting_name, $connector );
|
||||
}
|
||||
|
||||
$val = $this->opts_data_store->get( $setting_name, $connector );
|
||||
|
||||
if ( is_null( $val ) ) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
return $val;
|
||||
}
|
||||
|
||||
public function get_active_connector( $default = false ) {
|
||||
$connectors = $this->get_plugin_setting( Save_Connector_Settings_Endpoint::SETTING_ENABLED_CONNECTOR, array() );
|
||||
$connectors = array_filter( $connectors );
|
||||
$connector = empty( $connectors ) ? false : array_key_first( $connectors );
|
||||
|
||||
if ( empty( $connector ) ) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
return $connector;
|
||||
}
|
||||
|
||||
public function get_connector_status_of_type( $status_type, $default = false ) {
|
||||
$const_check = sprintf( 'GRAVITYSMTP_INTEGRATION_%s', strtoupper( $status_type ) );
|
||||
|
||||
if ( defined( $const_check ) ) {
|
||||
return constant( $const_check );
|
||||
}
|
||||
|
||||
$setting = Connector_Status_Enum::setting_for_status( $status_type );
|
||||
$connectors = $this->get_plugin_setting( $setting, array() );
|
||||
$connectors = array_filter( $connectors, function( $enabled ) {
|
||||
return ! empty( $enabled ) && $enabled !== false && $enabled !== 'false';
|
||||
} );
|
||||
$connector = empty( $connectors ) ? false : array_key_first( $connectors );
|
||||
|
||||
if ( empty( $connector ) ) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
return $connector;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Data_Store;
|
||||
|
||||
class Opts_Data_Store implements Data_Store {
|
||||
|
||||
public function get( $setting_name, $connector ) {
|
||||
$opts = $this->get_opts( $connector );
|
||||
|
||||
return isset( $opts[ $setting_name ] ) ? $opts[ $setting_name ] : null;
|
||||
}
|
||||
|
||||
public function save( $setting_name, $value, $connector ) {
|
||||
$opts = $this->get_opts( $connector );
|
||||
$opts_name = sprintf( 'gravitysmtp_%s', strtolower( $connector ) );
|
||||
$opts[ $setting_name ] = $value;
|
||||
|
||||
update_option( $opts_name, json_encode( $opts ) );
|
||||
}
|
||||
|
||||
public function save_all( $value, $connector ) {
|
||||
$opts = $this->get_opts( $connector );
|
||||
$opts_name = sprintf( 'gravitysmtp_%s', strtolower( $connector ) );
|
||||
|
||||
foreach( $value as $key => $val ) {
|
||||
if ( $val === '****************' ){
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( $val === 'true' ) {
|
||||
$val = true;
|
||||
}
|
||||
|
||||
if ( $val === 'false' ) {
|
||||
$val = false;
|
||||
}
|
||||
|
||||
$opts[ $key ] = $val;
|
||||
}
|
||||
|
||||
update_option( $opts_name, json_encode( $opts ) );
|
||||
}
|
||||
|
||||
public function get_opts( $connector ) {
|
||||
$opts_name = sprintf( 'gravitysmtp_%s', strtolower( $connector ) );
|
||||
$opts = get_option( $opts_name, '{}' );
|
||||
$opts = json_decode( $opts, true );
|
||||
|
||||
return $opts;
|
||||
}
|
||||
|
||||
public function delete_all( $connector ) {
|
||||
$opts_name = sprintf( 'gravitysmtp_%s', strtolower( $connector ) );
|
||||
delete_option( $opts_name );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Data_Store;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Connectors\Endpoints\Save_Connector_Settings_Endpoint;
|
||||
|
||||
class Plugin_Opts_Data_Store implements Data_Store {
|
||||
|
||||
public function get( $setting_name, $connector = 'config', $default = null ) {
|
||||
$opts = $this->get_opts();
|
||||
|
||||
return isset( $opts[ $setting_name ] ) ? $opts[ $setting_name ] : $default;
|
||||
}
|
||||
|
||||
public function save( $setting_name, $value, $connector = 'config' ) {
|
||||
$opts_name = sprintf( 'gravitysmtp_%s', strtolower( $connector ) );
|
||||
$opts = $this->get_opts();
|
||||
|
||||
$opts[ $setting_name ] = $value;
|
||||
|
||||
update_option( $opts_name, json_encode( $opts ) );
|
||||
}
|
||||
|
||||
public function save_all( $value, $connector = 'config' ) {
|
||||
$opts_name = sprintf( 'gravitysmtp_%s', strtolower( $connector ) );
|
||||
|
||||
update_option( $opts_name, json_encode( $value ) );
|
||||
}
|
||||
|
||||
private function get_opts() {
|
||||
$opts_name = 'gravitysmtp_config';
|
||||
$opts = get_option( $opts_name, '{}' );
|
||||
$opts = json_decode( $opts, true );
|
||||
|
||||
return $opts;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Data_Store;
|
||||
|
||||
interface Data_Store {
|
||||
|
||||
public function get( $setting_name, $connector );
|
||||
|
||||
public function save( $setting_name, $value, $connector );
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user