first commit
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Brizy_Editor_Accounts_AbstractAccountManager {
|
||||
|
||||
/**
|
||||
* @var Brizy_Editor_Project
|
||||
*/
|
||||
private $project;
|
||||
|
||||
/**
|
||||
* @var Brizy_Editor_Accounts_Account[]
|
||||
*/
|
||||
private $accounts;
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Accounts_AbstractAccountManager constructor.
|
||||
*
|
||||
* @param $class
|
||||
* @param Brizy_Editor_Project $project
|
||||
*/
|
||||
public function __construct( $class, Brizy_Editor_Project $project ) {
|
||||
$this->project = $project;
|
||||
try {
|
||||
$this->loadAccounts( $class, $project );
|
||||
} catch ( Exception $exception ) {
|
||||
$this->accounts = array();
|
||||
}
|
||||
}
|
||||
|
||||
public function getAllAccounts() {
|
||||
return $this->accounts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $service
|
||||
* @param $accountId
|
||||
*
|
||||
* @return Brizy_Editor_Accounts_Account[]|null
|
||||
*/
|
||||
public function getAccounts( $service ) {
|
||||
|
||||
if ( isset( $this->accounts[ $service ] ) ) {
|
||||
return array_values( $this->accounts[ $service ] );
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $service
|
||||
* @param $accountId
|
||||
*
|
||||
* @return Brizy_Editor_Accounts_Account|null
|
||||
*/
|
||||
public function getAccount( $service, $accountId ) {
|
||||
return $this->accounts[ $service ][ $accountId ];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $service
|
||||
* @param Brizy_Editor_Accounts_Account $anAccount
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAccount( $service, Brizy_Editor_Accounts_Account $anAccount ) {
|
||||
foreach ( $this->getAccounts( $service ) as $account ) {
|
||||
if ( $anAccount->isEqual( $account ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $service
|
||||
* @param Brizy_Editor_Accounts_Account $account
|
||||
*/
|
||||
public function addAccount( $service, Brizy_Editor_Accounts_Account $account ) {
|
||||
|
||||
if ( $this->hasAccount( $service, $account ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->accounts[ $service ][ $account->getId() ] = $account;
|
||||
|
||||
|
||||
$this->updateStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $service
|
||||
* @param Brizy_Editor_Accounts_Account $account
|
||||
*/
|
||||
public function deleteAccount( $service, Brizy_Editor_Accounts_Account $account ) {
|
||||
|
||||
unset( $this->accounts[ $service ][ $account->getId() ] );
|
||||
|
||||
$this->updateStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $service
|
||||
* @param $accountId
|
||||
*/
|
||||
public function deleteAccountById( $service, $accountId ) {
|
||||
|
||||
unset( $this->accounts[ $service ][ $accountId ] );
|
||||
|
||||
$this->updateStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function updateStorage() {
|
||||
|
||||
$data = array();
|
||||
|
||||
foreach ( $this->accounts as $service => $accounts ) {
|
||||
foreach ( $accounts as $account ) {
|
||||
$data[ $service ][] = $account->convertToOptionValue();
|
||||
}
|
||||
}
|
||||
|
||||
$this->project->setAccounts( $data );
|
||||
$this->project->saveStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param Brizy_Editor_Project $project
|
||||
*
|
||||
* @throws Brizy_Editor_Exceptions_NotFound
|
||||
*/
|
||||
private function loadAccounts( $class, Brizy_Editor_Project $project ) {
|
||||
|
||||
//$this->project->setMetaValue( 'accounts', [] );
|
||||
$meta_value = $project->getMetaValue( 'accounts' );
|
||||
|
||||
if ( is_array( $meta_value ) ) {
|
||||
foreach ( $meta_value as $service => $accounts ) {
|
||||
foreach ( $accounts as $account ) {
|
||||
$account1 = Brizy_Editor_Accounts_Account::createFromSerializedData( $account );
|
||||
|
||||
|
||||
if ( $this->hasAccount( $service, $account1 ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->accounts[ $service ][ $account1->getId() ] = $account1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
178
wp-content/plugins/brizy/editor/accounts/abstract-account.php
Normal file
178
wp-content/plugins/brizy/editor/accounts/abstract-account.php
Normal file
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
|
||||
abstract class Brizy_Editor_Accounts_AbstractAccount extends Brizy_Admin_Serializable {
|
||||
|
||||
const INTEGRATIONS_GROUP = 'form-integration';
|
||||
const RECAPTCHA_GROUP = 'recaptcha';
|
||||
const SOCIAL_GROUP = 'social';
|
||||
|
||||
|
||||
use Brizy_Editor_Forms_DynamicPropsAware;
|
||||
|
||||
/**
|
||||
* This will group the accounts
|
||||
* ex: form-integration,
|
||||
* recaptcha
|
||||
* other implementations
|
||||
*
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getGroup();
|
||||
|
||||
/**
|
||||
* This will return the service name for this account:
|
||||
* ex: facebook, twitter, linkedin....
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function getService();
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Accounts_Account constructor.
|
||||
*
|
||||
* @param null $data
|
||||
*/
|
||||
public function __construct( $data = null ) {
|
||||
if ( is_array( $data ) ) {
|
||||
foreach ( $data as $key => $val ) {
|
||||
$this->set( $key, $val );
|
||||
}
|
||||
} else {
|
||||
$this->data = array();
|
||||
}
|
||||
|
||||
if ( ! isset( $data['id'] ) ) {
|
||||
$this->set( 'id', md5( time() . rand( 0, 10000 ) ) );
|
||||
}
|
||||
|
||||
$this->set( 'group', $this->getGroup() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param self $account
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isEqual( self $account ) {
|
||||
$aData = $account->convertToOptionValue();
|
||||
foreach ( $this->data as $key => $val ) {
|
||||
if ( $key == 'id' ) {
|
||||
continue;
|
||||
}
|
||||
if ( ! isset( $aData[ $key ] ) || $aData[ $key ] != $val ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
public function convertToAuthData() {
|
||||
|
||||
$data = $this->data;
|
||||
|
||||
unset( $data['id'] );
|
||||
unset( $data['group'] );
|
||||
unset( $data['service'] );
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $data
|
||||
*
|
||||
* @return Brizy_Editor_Accounts_AbstractAccount
|
||||
*/
|
||||
public function setData( $data ) {
|
||||
$this->data = $data;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function serialize() {
|
||||
return serialize( $this->jsonSerialize() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $serialized
|
||||
*/
|
||||
public function unserialize( $serialized ) {
|
||||
$this->data = unserialize( $serialized );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|mixed|null
|
||||
*/
|
||||
public function jsonSerialize() {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
public function convertToOptionValue() {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return Brizy_Editor_Accounts_AbstractAccount
|
||||
* @throws Exception
|
||||
*/
|
||||
static public function createFromSerializedData( $data ) {
|
||||
|
||||
if ( isset( $data['group'] ) )
|
||||
switch ( $data['group'] ) {
|
||||
default:
|
||||
case self::INTEGRATIONS_GROUP:
|
||||
return new Brizy_Editor_Accounts_Account( $data );
|
||||
case self::SOCIAL_GROUP:
|
||||
return new Brizy_Editor_Accounts_SocialAccount( $data );
|
||||
case self::RECAPTCHA_GROUP:
|
||||
return new Brizy_Editor_Accounts_RecaptchaAccount( $data );
|
||||
}
|
||||
|
||||
throw new Exception( 'Invalid account group.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $json_obj
|
||||
*
|
||||
* @return Brizy_Editor_Accounts_AbstractAccount
|
||||
* @throws Exception`
|
||||
*/
|
||||
public static function createFromJson( $json_obj ) {
|
||||
|
||||
if ( ! isset( $json_obj ) ) {
|
||||
throw new Exception( 'Bad Request', 400 );
|
||||
}
|
||||
|
||||
if ( is_object( $json_obj ) ) {
|
||||
return self::createFromSerializedData( get_object_vars( $json_obj ) );
|
||||
}
|
||||
|
||||
throw new Exception( 'Invalid json provided.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Some accounts may need advanced validation
|
||||
*
|
||||
* Ex: a request to an external api may be needed
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validate() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
53
wp-content/plugins/brizy/editor/accounts/account.php
Normal file
53
wp-content/plugins/brizy/editor/accounts/account.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Brizy_Editor_Accounts_Account extends Brizy_Editor_Accounts_AbstractAccount {
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getGroup() {
|
||||
return Brizy_Editor_Accounts_AbstractAccount::INTEGRATIONS_GROUP;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getService() {
|
||||
return $this->data['service'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return Brizy_Editor_Accounts_AbstractAccount
|
||||
* @throws Exception
|
||||
*/
|
||||
static public function createFromSerializedData( $data ) {
|
||||
|
||||
$data['group'] = Brizy_Editor_Accounts_AbstractAccount::INTEGRATIONS_GROUP;
|
||||
|
||||
return Brizy_Editor_Accounts_AbstractAccount::createFromSerializedData( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $json_obj
|
||||
*
|
||||
* @return Brizy_Editor_Accounts_AbstractAccount
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function createFromJson( $json_obj ) {
|
||||
|
||||
if ( ! isset( $json_obj ) ) {
|
||||
throw new Exception( 'Bad Request', 400 );
|
||||
}
|
||||
|
||||
if ( is_object( $json_obj ) ) {
|
||||
return Brizy_Editor_Accounts_AbstractAccount::createFromSerializedData( get_object_vars( $json_obj ) );
|
||||
}
|
||||
|
||||
throw new Exception( 'Invalid json provided.' );
|
||||
}
|
||||
|
||||
}
|
||||
156
wp-content/plugins/brizy/editor/accounts/api.php
Normal file
156
wp-content/plugins/brizy/editor/accounts/api.php
Normal file
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: alex
|
||||
* Date: 2/7/19
|
||||
* Time: 10:13 AM
|
||||
*/
|
||||
|
||||
|
||||
class Brizy_Editor_Accounts_Api extends Brizy_Admin_AbstractApi {
|
||||
|
||||
const nonce = 'brizy-api';
|
||||
const BRIZY_GET_ACCOUNT = '_get_account';
|
||||
const BRIZY_GET_ACCOUNTS = '_get_accounts';
|
||||
const BRIZY_ADD_ACCOUNT = '_add_account';
|
||||
const BRIZY_UPDATE_ACCOUNT = '_update_account';
|
||||
const BRIZY_DELETE_ACCOUNT = '_delete_account';
|
||||
|
||||
/**
|
||||
* @var Brizy_Editor_Accounts_ServiceAccountManager
|
||||
*/
|
||||
private $manager;
|
||||
|
||||
/**
|
||||
* Brizy_Admin_Rules_Api constructor.
|
||||
*
|
||||
* @param Brizy_Editor_Accounts_ServiceAccountManager $manager
|
||||
*/
|
||||
public function __construct( $manager ) {
|
||||
$this->manager = $manager;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Brizy_Editor_Accounts_Api
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function _init() {
|
||||
static $instance;
|
||||
|
||||
if ( ! $instance ) {
|
||||
$instance = new self( new Brizy_Editor_Accounts_ServiceAccountManager( Brizy_Editor_Project::get() ) );
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/***
|
||||
* @return null
|
||||
*/
|
||||
protected function getRequestNonce() {
|
||||
return $this->param( 'hash' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register all api actions
|
||||
*/
|
||||
protected function initializeApiActions() {
|
||||
$pref = 'wp_ajax_' . Brizy_Editor::prefix();
|
||||
add_action( $pref . self::BRIZY_GET_ACCOUNT, array( $this, 'actionGetAccount' ) );
|
||||
add_action( $pref . self::BRIZY_GET_ACCOUNTS, array( $this, 'actionGetAccounts' ) );
|
||||
add_action( $pref . self::BRIZY_ADD_ACCOUNT, array( $this, 'actionAddAccount' ) );
|
||||
add_action( $pref . self::BRIZY_UPDATE_ACCOUNT, array( $this, 'actionUpdateAccount' ) );
|
||||
add_action( $pref . self::BRIZY_DELETE_ACCOUNT, array( $this, 'actionDeleteAccount' ) );
|
||||
}
|
||||
|
||||
|
||||
public function actionGetAccount() {
|
||||
$this->verifyNonce( self::nonce );
|
||||
if ( ! $this->param( 'id' ) ) {
|
||||
$this->error( 400, 'Invalid account id' );
|
||||
}
|
||||
try {
|
||||
$manager = new Brizy_Editor_Accounts_ServiceAccountManager( Brizy_Editor_Project::get() );
|
||||
$account = $manager->getAccount( $this->param( 'id' ) );
|
||||
|
||||
if ( ! $account ) {
|
||||
$this->error( 404, 'Account not found' );
|
||||
}
|
||||
|
||||
$this->success( $account );
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->critical( $e->getMessage(), array( $e ) );
|
||||
$this->error( 500, $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function actionGetAccounts() {
|
||||
$this->verifyNonce( self::nonce );
|
||||
|
||||
$filter = array();
|
||||
|
||||
if ( $this->param( 'group' ) ) {
|
||||
$filter['group'] = $this->param( 'group' );
|
||||
}
|
||||
if ( $this->param( 'service' ) ) {
|
||||
$filter['service'] = $this->param( 'service' );
|
||||
}
|
||||
|
||||
try {
|
||||
$manager = new Brizy_Editor_Accounts_ServiceAccountManager( Brizy_Editor_Project::get() );
|
||||
$accounts = $manager->getFilteredAccounts( $filter );
|
||||
$this->success( $accounts );
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->critical( $e->getMessage(), array( $e ) );
|
||||
$this->error( 500, $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
public function actionAddAccount() {
|
||||
$this->verifyNonce( self::nonce );
|
||||
try {
|
||||
$manager = new Brizy_Editor_Accounts_ServiceAccountManager( Brizy_Editor_Project::get() );
|
||||
$instance = Brizy_Editor_Accounts_AbstractAccount::createFromJson( json_decode( file_get_contents( 'php://input' ) ) );
|
||||
$instance->validate();
|
||||
$manager->addAccount( $instance );
|
||||
$this->success( $instance );
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->critical( $e->getMessage(), array( $e ) );
|
||||
$this->error( 500, $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
public function actionUpdateAccount() {
|
||||
$this->verifyNonce( self::nonce );
|
||||
try {
|
||||
$manager = new Brizy_Editor_Accounts_ServiceAccountManager( Brizy_Editor_Project::get() );
|
||||
$instance = Brizy_Editor_Accounts_AbstractAccount::createFromJson( json_decode( file_get_contents( 'php://input' ) ) );
|
||||
$manager->updateAccount( $instance );
|
||||
$this->success( $instance );
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->critical( $e->getMessage(), array( $e ) );
|
||||
$this->error( 500, $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
public function actionDeleteAccount() {
|
||||
$this->verifyNonce( self::nonce );
|
||||
try {
|
||||
|
||||
if ( ! $this->param( 'id' ) ) {
|
||||
$this->error( 400, 'Invalid account id' );
|
||||
}
|
||||
|
||||
$manager = new Brizy_Editor_Accounts_ServiceAccountManager( Brizy_Editor_Project::get() );
|
||||
$manager->deleteAccountById( $this->param( 'id' ) );
|
||||
$this->success( null );
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->critical( $e->getMessage(), array( $e ) );
|
||||
$this->error( 500, $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
100
wp-content/plugins/brizy/editor/accounts/recaptcha-account.php
Normal file
100
wp-content/plugins/brizy/editor/accounts/recaptcha-account.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: alex
|
||||
* Date: 11/26/18
|
||||
* Time: 5:00 PM
|
||||
*/
|
||||
|
||||
class Brizy_Editor_Accounts_RecaptchaAccount extends Brizy_Editor_Accounts_AbstractAccount {
|
||||
|
||||
const SERVICE_NAME = 'recaptcha';
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getGroup() {
|
||||
return Brizy_Editor_Accounts_AbstractAccount::RECAPTCHA_GROUP;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getService() {
|
||||
return self::SERVICE_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return Brizy_Editor_Accounts_AbstractAccount
|
||||
* @throws Exception
|
||||
*/
|
||||
static public function createFromSerializedData( $data ) {
|
||||
|
||||
$data['group'] = Brizy_Editor_Accounts_AbstractAccount::RECAPTCHA_GROUP;
|
||||
$data['service'] = self::SERVICE_NAME;
|
||||
|
||||
return Brizy_Editor_Accounts_AbstractAccount::createFromSerializedData( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $json_obj
|
||||
*
|
||||
* @return Brizy_Editor_Accounts_AbstractAccount
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function createFromJson( $json_obj ) {
|
||||
|
||||
if ( ! isset( $json_obj ) ) {
|
||||
throw new Exception( 'Bad Request', 400 );
|
||||
}
|
||||
|
||||
if ( is_object( $json_obj ) ) {
|
||||
$json_obj->group = Brizy_Editor_Accounts_AbstractAccount::RECAPTCHA_GROUP;
|
||||
$json_obj->service = self::SERVICE_NAME;
|
||||
|
||||
return Brizy_Editor_Accounts_AbstractAccount::createFromSerializedData( get_object_vars( $json_obj ) );
|
||||
}
|
||||
|
||||
throw new Exception( 'Invalid json provided.' );
|
||||
}
|
||||
|
||||
public function getSiteKey() {
|
||||
return $this->get( 'sitekey' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function validate() {
|
||||
|
||||
if ( ! isset( $_REQUEST['secretkey'] ) ) {
|
||||
throw new Exception( 'Invalid secret provided' );
|
||||
}
|
||||
|
||||
if ( ! isset( $_REQUEST['response'] ) ) {
|
||||
throw new Exception( 'Invalid response provided' );
|
||||
}
|
||||
|
||||
$http = new WP_Http();
|
||||
$response = $http->post( 'https://www.google.com/recaptcha/api/siteverify', array(
|
||||
'body' => array(
|
||||
'secret' => $_REQUEST['secretkey'],
|
||||
'response' => $_REQUEST['response']
|
||||
)
|
||||
) );
|
||||
|
||||
$body = wp_remote_retrieve_body( $response );
|
||||
|
||||
$responseJsonObject = json_decode( $body );
|
||||
|
||||
if ( ! is_object( $responseJsonObject ) || ! $responseJsonObject->success ) {
|
||||
throw new Exception( "Unable to validate account" );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Brizy_Editor_Accounts_ServiceAccountManager {
|
||||
|
||||
/**
|
||||
* @var Brizy_Editor_Project
|
||||
*/
|
||||
private $project;
|
||||
|
||||
/**
|
||||
* @var Brizy_Editor_Accounts_Account[]
|
||||
*/
|
||||
private $accounts;
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Forms_Manager constructor.
|
||||
*
|
||||
* @param Brizy_Editor_Project $project
|
||||
*/
|
||||
public function __construct( Brizy_Editor_Project $project ) {
|
||||
$this->accounts = array();
|
||||
$this->project = $project;
|
||||
try {
|
||||
$this->loadAccounts( $project );
|
||||
} catch ( Exception $exception ) {
|
||||
$this->accounts = array();
|
||||
}
|
||||
}
|
||||
|
||||
public function getAllAccounts() {
|
||||
return $this->accounts;
|
||||
}
|
||||
|
||||
public function getFilteredAccounts( $filter ) {
|
||||
$accounts = array();
|
||||
foreach ( $this->getAllAccounts() as $account ) {
|
||||
|
||||
if ( isset( $filter['service'] ) && $filter['service'] != $account->getService() ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( isset( $filter['group'] ) && $filter['group'] != $account->getGroup() ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$accounts[] = $account;
|
||||
}
|
||||
|
||||
return $accounts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $group
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAccountsByGroup( $group ) {
|
||||
return $this->getFilteredAccounts( array( 'group' => $group ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $service
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAccountsByService( $service ) {
|
||||
return $this->getFilteredAccounts( array( 'service' => $service ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $accountId
|
||||
*
|
||||
* @return Brizy_Editor_Accounts_Account|mixed
|
||||
*/
|
||||
public function getAccount( $accountId ) {
|
||||
foreach ( $this->accounts as $key => $account ) {
|
||||
if ( $account->getId() == $accountId ) {
|
||||
return $account;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Accounts_AbstractAccount $anAccount
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAccount( Brizy_Editor_Accounts_AbstractAccount $anAccount ) {
|
||||
foreach ( $this->getAllAccounts() as $account ) {
|
||||
if ( $anAccount->isEqual( $account ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Accounts_AbstractAccount $account
|
||||
*/
|
||||
public function addAccount( Brizy_Editor_Accounts_AbstractAccount $account ) {
|
||||
|
||||
if ( $this->hasAccount( $account ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->accounts[] = $account;
|
||||
$this->updateStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Accounts_AbstractAccount $anAccount
|
||||
*/
|
||||
public function updateAccount( Brizy_Editor_Accounts_AbstractAccount $anAccount ) {
|
||||
|
||||
foreach ( $this->getAllAccounts() as $index => $account ) {
|
||||
if ( $account->getId() == $anAccount->getId() ) {
|
||||
$this->accounts[ $index ] = $anAccount;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$this->updateStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Accounts_AbstractAccount $account
|
||||
*/
|
||||
public function deleteAccount( Brizy_Editor_Accounts_AbstractAccount $account ) {
|
||||
|
||||
$this->deleteAccountById( $account->getId() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $accountId
|
||||
*/
|
||||
public function deleteAccountById( $accountId ) {
|
||||
|
||||
foreach ( $this->getAllAccounts() as $key => $account ) {
|
||||
if ( $account->getId() == $accountId ) {
|
||||
unset( $this->accounts[ $key ] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$this->accounts = array_values( $this->accounts );
|
||||
|
||||
$this->updateStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function updateStorage() {
|
||||
|
||||
$data = array();
|
||||
|
||||
foreach ( $this->getAllAccounts() as $account ) {
|
||||
$data[] = $account->convertToOptionValue();
|
||||
}
|
||||
|
||||
$this->project->setAccounts( $data );
|
||||
$this->project->saveStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Brizy_Editor_Project $project
|
||||
*
|
||||
* @throws Brizy_Editor_Exceptions_NotFound
|
||||
*/
|
||||
private function loadAccounts( Brizy_Editor_Project $project ) {
|
||||
|
||||
$meta_value = $project->getMetaValue( 'accounts' );
|
||||
|
||||
if ( is_array( $meta_value ) ) {
|
||||
foreach ( $meta_value as $account ) {
|
||||
|
||||
try {
|
||||
$anAccount = Brizy_Editor_Accounts_AbstractAccount::createFromSerializedData( $account );
|
||||
|
||||
if ( $this->hasAccount( $anAccount ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->accounts[] = $anAccount;
|
||||
|
||||
} catch ( Exception $e ) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
52
wp-content/plugins/brizy/editor/accounts/social-account.php
Normal file
52
wp-content/plugins/brizy/editor/accounts/social-account.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: alex
|
||||
* Date: 11/26/18
|
||||
* Time: 5:00 PM
|
||||
*/
|
||||
|
||||
class Brizy_Editor_Accounts_SocialAccount extends Brizy_Editor_Accounts_Account {
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getGroup() {
|
||||
return Brizy_Editor_Accounts_AbstractAccount::SOCIAL_GROUP;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return Brizy_Editor_Accounts_AbstractAccount
|
||||
* @throws Exception
|
||||
*/
|
||||
static public function createFromSerializedData( $data ) {
|
||||
|
||||
$data['group'] = Brizy_Editor_Accounts_AbstractAccount::SOCIAL_GROUP;
|
||||
|
||||
return Brizy_Editor_Accounts_AbstractAccount::createFromSerializedData( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $json_obj
|
||||
*
|
||||
* @return Brizy_Editor_Accounts_AbstractAccount
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function createFromJson( $json_obj ) {
|
||||
|
||||
if ( ! isset( $json_obj ) ) {
|
||||
throw new Exception( 'Bad Request', 400 );
|
||||
}
|
||||
|
||||
if ( is_object( $json_obj ) ) {
|
||||
|
||||
$json_obj->group = Brizy_Editor_Accounts_AbstractAccount::SOCIAL_GROUP;
|
||||
|
||||
return Brizy_Editor_Accounts_AbstractAccount::createFromSerializedData( get_object_vars( $json_obj ) );
|
||||
}
|
||||
|
||||
throw new Exception( 'Invalid json provided.' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user