Skip to content

Commit

Permalink
Merge branch 'feature/APP-705-add-connect-module' into feature/APP-70…
Browse files Browse the repository at this point in the history
…7-general-settings-fields
  • Loading branch information
nirbhayel committed Nov 11, 2024
2 parents 8462c2b + e01148b commit 6e9a818
Show file tree
Hide file tree
Showing 17 changed files with 108 additions and 89 deletions.
4 changes: 2 additions & 2 deletions modules/connect/classes/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
class Config {
const APP_NAME = 'once-click-accessibility';
const APP_PREFIX = 'ea11y';
const APP_REST_NAMESPACE = 'site_mailer';
const APP_REST_NAMESPACE = 'ea11y';
const BASE_URL = 'https://my.elementor.com/connect';
const ADMIN_PAGE = 'accessibility-settings-2';
const APP_TYPE = 'app_mailer';
const SCOPES = 'openid offline_access';
const STATE_NONCE = 'site_mailer_auth_nonce';
const STATE_NONCE = 'ea11y_auth_nonce';
const CONNECT_MODE = 'site';
}
6 changes: 3 additions & 3 deletions modules/connect/classes/service.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public static function jwt_decode( $payload ): string {

$jwks_url = Utils::get_jwks_url();
if ( ! $jwks_url ) {
return __( 'Missing JWKS URL', 'site-mailer' );
return __( 'Missing JWKS URL', 'pojo-accessibility' );
}

if ( ! $jwks ) {
Expand All @@ -214,9 +214,9 @@ public static function jwt_decode( $payload ): string {
]);
}
if ( ! class_exists( 'JWT' ) ) {
require_once SITE_MAILER_PATH . 'vendor/autoload.php';
require_once EA11Y_PATH . 'vendor/autoload.php';
if ( ! class_exists( 'JWT' ) ) {
return __( 'JWT class not found', 'site-mailer' );
return __( 'JWT class not found', 'pojo-accessibility' );
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/connect/classes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static function get_clients_patch_url( string $client_id ): string {
* @return string
*/
public static function get_base_url(): string {
return apply_filters( 'site_mailer_connect_get_base_url', Config::BASE_URL );
return apply_filters( 'ea11y_connect_get_base_url', Config::BASE_URL );
}

/**
Expand Down
22 changes: 0 additions & 22 deletions modules/connect/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,9 @@ public static function is_connected() : bool {
return ! ! Data::get_access_token() && Utils::is_valid_home_url();
}

public function authorize_url( $authorize_url ) {
$utm_params = [];

$sm_campaign = get_transient( 'elementor_site_mailer_campaign' );
if ( false === $sm_campaign ) {
return $authorize_url;
}

foreach ( [ 'source', 'medium', 'campaign' ] as $key ) {
if ( ! empty( $sm_campaign[ $key ] ) ) {
$utm_params[ 'utm_' . $key ] = $sm_campaign[ $key ];
}
}

if ( ! empty( $utm_params ) ) {
$authorize_url = add_query_arg( $utm_params, $authorize_url );
}

return $authorize_url;
}

public function __construct() {
$this->register_components();
$this->register_routes();
add_filter( 'site_mailer_connect_authorize_url', [ $this, 'authorize_url' ] );
}
}

6 changes: 3 additions & 3 deletions modules/connect/rest/authorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function POST( WP_REST_Request $request ) {

if ( Connect::is_connected() && Utils::is_valid_home_url() ) {
return $this->respond_error_json( [
'message' => esc_html__( 'You are already connected', 'site-mailer' ),
'message' => esc_html__( 'You are already connected', 'pojo-accessibility' ),
'code' => 'forbidden',
] );
}
Expand All @@ -57,15 +57,15 @@ public function POST( WP_REST_Request $request ) {
Service::update_redirect_uri();
} else {
return $this->respond_error_json( [
'message' => esc_html__( 'Connected domain mismatch', 'site-mailer' ),
'message' => esc_html__( 'Connected domain mismatch', 'pojo-accessibility' ),
'code' => 'forbidden',
] );
}
}

$authorize_url = Utils::get_authorize_url( $client_id );

$authorize_url = apply_filters( 'site_mailer_connect_authorize_url', $authorize_url );
$authorize_url = apply_filters( 'ea11y_connect_authorize_url', $authorize_url );

return $this->respond_success_json( $authorize_url );
} catch ( Throwable $t ) {
Expand Down
15 changes: 10 additions & 5 deletions modules/connect/rest/switch-domain.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php

namespace EA11y\modules\connect\rest;

use EA11y\Modules\Connect\Classes\{Data, Route_Base, Service, Utils};
namespace EA11y\Modules\Connect\Rest;

use EA11y\Modules\Connect\Classes\{
Data,
Route_Base,
Service,
Utils
};
use Throwable;
use WP_REST_Request;

Expand Down Expand Up @@ -36,14 +41,14 @@ public function POST( WP_REST_Request $request ) {

if ( ! $client_id ) {
return $this->respond_error_json( [
'message' => __( 'Client ID not found', 'site-mailer' ),
'message' => __( 'Client ID not found', 'pojo-accessibility' ),
'code' => 'ignore_error',
] );
}

Service::update_redirect_uri();

return $this->respond_success_json( [ 'message' => __( 'Domain updated!', 'site-mailer' ) ] );
return $this->respond_success_json( [ 'message' => __( 'Domain updated!', 'pojo-accessibility' ) ] );
} catch ( Throwable $t ) {
return $this->respond_error_json( [
'message' => $t->getMessage(),
Expand Down
14 changes: 8 additions & 6 deletions modules/settings/assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StrictMode, Fragment, createRoot } from '@wordpress/element';
import App from './app';
import AdminTopBar from './components/admin-top-bar';
import { PluginSettingsProvider } from './contexts/plugin-settings-context';
import { SettingsProvider } from './hooks';
import { SettingsProvider, NotificationsProvider } from './hooks';

const rootNode = document.getElementById( 'ea11y-app' );
const topBarNode = document.getElementById( 'ea11y-app-top-bar' );
Expand All @@ -23,10 +23,12 @@ topBar.render(

root.render(
<AppWrapper>
<SettingsProvider>
<PluginSettingsProvider>
<App />
</PluginSettingsProvider>
</SettingsProvider>
<NotificationsProvider>
<SettingsProvider>
<PluginSettingsProvider>
<App />
</PluginSettingsProvider>
</SettingsProvider>
</NotificationsProvider>
</AppWrapper>,
);
11 changes: 5 additions & 6 deletions modules/settings/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ import DirectionProvider from '@elementor/ui/DirectionProvider';
import { ThemeProvider } from '@elementor/ui/styles';
import { ConnectModal, Notifications } from './components';
import { usePluginSettingsContext } from './contexts/plugin-settings-context';
import { useSettings } from './hooks';
import { useNotificationSettings } from './hooks';

const App = () => {
const {
isConnected,
} = usePluginSettingsContext();
const { isConnected } = usePluginSettingsContext();
const {
notificationMessage,
notificationType,
} = useSettings();
} = useNotificationSettings();

return (
<DirectionProvider rtl={ false /* Add RTL detection in settings */ }>
<DirectionProvider rtl={ false /* TODO:Add RTL detection in settings */ }>
<ThemeProvider colorScheme="light">
{ ! isConnected && <ConnectModal /> }
<Notifications message={ notificationMessage } type={ notificationType } />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function ConnectModal() {
<Typography variant="subtitle1"
marginTop={ 5 }
marginBottom={ 3 }>
{ __( 'Connect plugin on your site!', 'site-mailer' ) }
{ __( 'Connect plugin on your site!', 'pojo-accessibility' ) }
</Typography>
<Button variant="contained"
color="secondary"
Expand Down
8 changes: 5 additions & 3 deletions modules/settings/assets/js/components/notifications/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Alert from '@elementor/ui/Alert';
import Snackbar from '@elementor/ui/Snackbar';
import { useSettings } from '../../hooks';
import { useNotificationSettings } from '../../hooks';

const Notifications = ( { type, message } ) => {
const {
showNotification,
setShowNotification,
setNotificationMessage,
setNotificationType,
} = useSettings();
} = useNotificationSettings();

const closeNotification = () => {
setShowNotification( ! showNotification );
Expand All @@ -24,7 +24,9 @@ const Notifications = ( { type, message } ) => {
anchorOrigin={ { vertical: 'bottom', horizontal: 'right' } }
sx={ { zIndex: 99999 } }
>
<Alert onClose={ () => setShowNotification( ! showNotification ) } severity={ type } variant="filled" >
<Alert onClose={ () => setShowNotification( ! showNotification ) }
severity={ type }
variant="filled" >
{ message }
</Alert>
</Snackbar>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createContext, useCallback, useContext, useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import API from '../api';
import { useToastNotification } from '../hooks/use-settings';
import { useToastNotification } from '../hooks';

const PluginSettingsContext = createContext( {} );

Expand All @@ -19,7 +19,7 @@ export const PluginSettingsProvider = ( { children } ) => {
setPluginSettings( settings );
setLoaded( true );
} ).catch( () => {
error( __( 'An error occurred.', 'site-mailer' ) );
error( __( 'An error occurred.', 'pojo-accessibility' ) );
setLoaded( true );
} );
}, [] );
Expand Down
5 changes: 4 additions & 1 deletion modules/settings/assets/js/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export { default as useAuth } from './use-auth';
export { default as useModal } from './use-modal';
export { useSettings } from './use-settings';
export { useToastNotification } from './use-settings';
export { default as SettingsProvider } from './use-settings';
export { default as useStorage } from './use-storage';
export { useToastNotification } from './use-notifications';
export { default as NotificationsProvider } from './use-notifications';
export { useNotificationSettings } from './use-notifications';

2 changes: 1 addition & 1 deletion modules/settings/assets/js/hooks/use-auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import API from '../api';
import { UPGRADE_LINK } from '../constants';
import { usePluginSettingsContext } from '../context/plugin-settings-context';
import { usePluginSettingsContext } from '../contexts/plugin-settings-context';

const useAuth = () => {
const { subscriptionId } = 123;
Expand Down
55 changes: 55 additions & 0 deletions modules/settings/assets/js/hooks/use-notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { useState, createContext, useContext } from '@wordpress/element';

const NotificationsContext = createContext( undefined );

export function useNotificationSettings() {
return useContext( NotificationsContext );
}

export const NotificationsProvider = ( { children } ) => {
const [ showNotification, setShowNotification ] = useState( false );
const [ notificationMessage, setNotificationMessage ] = useState( '' );
const [ notificationType, setNotificationType ] = useState( '' );

return (
<NotificationsContext.Provider
value={ {
showNotification,
setShowNotification,
notificationMessage,
setNotificationMessage,
notificationType,
setNotificationType,
} }
>
{ children }
</NotificationsContext.Provider>
);
};

export const useToastNotification = () => {
const {
setNotificationMessage,
setNotificationType,
setShowNotification,
} = useContext( NotificationsContext );

const error = ( message ) => {
setNotificationMessage( message );
setNotificationType( 'error' );
setShowNotification( true );
};

const success = ( message ) => {
setNotificationMessage( message );
setNotificationType( 'success' );
setShowNotification( true );
};

return {
success,
error,
};
};

export default NotificationsProvider;
35 changes: 3 additions & 32 deletions modules/settings/assets/js/hooks/use-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,17 @@ export function useSettings() {
}

const SettingsProvider = ( { children } ) => {
// Notification
const [ showNotification, setShowNotification ] = useState( false );
const [ notificationMessage, setNotificationMessage ] = useState( '' );
const [ notificationType, setNotificationType ] = useState( '' );

const [ test, setTest ] = useState( 'Test' );
return (
<SettingsContext.Provider
value={ {
showNotification,
setShowNotification,
notificationMessage,
setNotificationMessage,
notificationType,
setNotificationType,
test,
setTest,
} }
>
{ children }
</SettingsContext.Provider>
);
};

export const useToastNotification = () => {
const { setNotificationMessage, setNotificationType, setShowNotification } = useContext( SettingsContext );

const error = ( message ) => {
setNotificationMessage( message );
setNotificationType( 'error' );
setShowNotification( true );
};

const success = ( message ) => {
setNotificationMessage( message );
setNotificationType( 'success' );
setShowNotification( true );
};

return {
success,
error,
};
};

export default SettingsProvider;
3 changes: 3 additions & 0 deletions modules/settings/classes/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ public static function get( string $option_name ) {
return $data;
}
}
public static function set( string $option_name, $value ): bool {
return update_option( $option_name, $value, false );
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@wordpress/i18n": "^5.10.0",
"@wordpress/url": "^4.10.0",
"husky": "^9.1.6",
"react-colorful": "^5.6.1"
"react-colorful": "^5.6.1",
"prop-types": "^15.8.1"
}
}

0 comments on commit 6e9a818

Please sign in to comment.