Skip to content

Commit

Permalink
add: settings pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
nirbhayel committed Dec 27, 2024
1 parent ab453fe commit 8593f89
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 38 deletions.
1 change: 1 addition & 0 deletions includes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static function get_module_list(): array {
'Legacy',
'Connect',
'Settings',
'Core',
];
}

Expand Down
49 changes: 49 additions & 0 deletions modules/core/components/pointers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace EA11y\Modules\Core\Components;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

class Pointers {
const DISMISSED_POINTERS_META_KEY = 'ea11y_dismissed_pointers';

public function dismiss_pointers() {
if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'ea11y-pointer-dismissed' ) ) {
wp_send_json_error( [ 'message' => 'Invalid nonce' ] );
}

$pointer = sanitize_text_field( $_POST['data']['pointer'] ) ?? null;

if ( empty( $pointer ) ) {
wp_send_json_error( [ 'message' => 'The pointer id must be provided' ] );
}

$pointer = explode( ',', $pointer );

$user_dismissed_meta = get_user_meta( get_current_user_id(), self::DISMISSED_POINTERS_META_KEY, true );

if ( ! $user_dismissed_meta ) {
$user_dismissed_meta = [];
}

foreach ( $pointer as $item ) {
$user_dismissed_meta[ $item ] = true;
}

update_user_meta( get_current_user_id(), self::DISMISSED_POINTERS_META_KEY, $user_dismissed_meta );

wp_send_json_success( [] );
}

public static function is_dismissed( string $slug ): bool {
$meta = (array) get_user_meta( get_current_user_id(), self::DISMISSED_POINTERS_META_KEY, true );

return key_exists( $slug, $meta );
}

public function __construct() {
add_action( 'wp_ajax_ea11y_pointer_dismissed', [ $this, 'dismiss_pointers' ] );
}
}
60 changes: 60 additions & 0 deletions modules/core/module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace EA11y\Modules\Core;

use EA11y\Classes\Module_Base;
use EA11y\Modules\Connect\Module as Connect;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

class Module extends Module_Base {
public function get_name(): string {
return 'core';
}

public static function component_list() : array {
return [
'Pointers',
];
}

public function add_plugin_links( $links, $plugin_file_name ): array {
if ( ! str_ends_with( $plugin_file_name, '/pojo-accessibility.php' ) ) {
return (array) $links;
}

$custom_links = [
'dashboard' => sprintf(
'<a href="%s">%s</a>',
admin_url( 'admin.php?page=' . \EA11y\Modules\Settings\Module::SETTING_BASE_SLUG ),
esc_html__( 'Dashboard', 'pojo-accessibility' )
),
];

if ( Connect::is_connected() ) {
$custom_links['upgrade'] = sprintf(
'<a href="%s" style="color: #524CFF; font-weight: 700;" target="_blank" rel="noopener noreferrer">%s</a>',
'https://go.elementor.com/sm-panel-wp-dash-upgrade-plugins/',
esc_html__( 'Upgrade', 'pojo-accessibility' )
);
} else {
$custom_links['connect'] = sprintf(
'<a href="%s" style="color: #524CFF; font-weight: 700;">%s</a>',
admin_url( 'admin.php?page=' . \EA11y\Modules\Settings\Module::SETTING_BASE_SLUG ),
esc_html__( 'Connect', 'pojo-accessibility' )
);
}

return array_merge( $custom_links, $links );
}

/**
* Module constructor.
*/
public function __construct() {
$this->register_components();

add_filter( 'plugin_action_links', [ $this, 'add_plugin_links' ], 10, 2 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const MyAccountMenu = () => {
const redirectToBilling = () => {
window.open(BILLING_LINK, '_blank').focus();
};

return (
<>
<List>
Expand Down
92 changes: 92 additions & 0 deletions modules/settings/components/settings-pointer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace EA11y\Modules\Settings\Components;

use EA11y\Modules\Core\Components\Pointers;
use EA11y\Modules\Settings\Module as SettingsModule;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

class Settings_Pointer {
const CURRENT_POINTER_SLUG = 'ea11y-settings';

public function admin_print_script() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}

if ( Pointers::is_dismissed( self::CURRENT_POINTER_SLUG ) ) {
return;
}

wp_enqueue_script( 'wp-pointer' );
wp_enqueue_style( 'wp-pointer' );

$pointer_content = '<h3>' . esc_html__( 'One Click Accessibility', 'pojo-accessibility' ) . '</h3>';
$pointer_content .= '<p>' . esc_html__( "Start setting up and customizing your site's accessibility widget.", 'pojo-accessibility' ) . '</p>';

$pointer_content .= sprintf(
'<p><a class="button button-primary ea11y-pointer-settings-link" href="%s">%s</a></p>',
admin_url( 'admin.php?page=' . SettingsModule::SETTING_BASE_SLUG ),
esc_html__( 'Get Started', 'pojo-accessibility' )
);

$allowed_tags = [
'h3' => [],
'p' => [],
'a' => [
'class' => [],
'href' => [],
],
];
?>
<script>
const onClose = () => {
return wp.ajax.post( 'ea11y_pointer_dismissed', {
data: {
pointer: '<?php echo esc_attr( static::CURRENT_POINTER_SLUG ); ?>',
},
nonce: '<?php echo esc_attr( wp_create_nonce( 'ea11y-pointer-dismissed' ) ); ?>',
} );
}

jQuery( document ).ready( function( $ ) {
//TODO: Update page URL
$( '#toplevel_page_accessibility-settings-2' ).pointer( {
content: '<?php echo wp_kses( $pointer_content, $allowed_tags ); ?>',
pointerClass: 'ea11y-settings-pointer',
position: {
edge: <?php echo is_rtl() ? "'right'" : "'left'"; ?>,
align: 'center'
},
close: onClose
} ).pointer( 'open' );

$( '.ea11y-pointer-settings-link' ).first().on( 'click', function( e ) {
e.preventDefault();

$(this).attr( 'disabled', true );

onClose().promise().done(() => {
location = $(this).attr( 'href' );
});
})
} );
</script>

<style>
/* TODO: Update icon */
.ea11y-settings-pointer .wp-pointer-content h3::before {
content: '';
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='32' height='32' fill='none'%3E%3Cg clip-path='url(%23a)'%3E%3Ccircle cx='16' cy='16' r='16' fill='%23FF7BE5'/%3E%3Cpath fill='%23fff' fill-rule='evenodd' d='M15.945 7.707a.142.142 0 0 1 .11 0l9.981 4.212c.3.126.496.42.496.746V23.9a.405.405 0 0 1-.405.404H5.872a.405.405 0 0 1-.405-.404V12.168L16 15.798l3.443-.507L16 17.013v7.183a.04.04 0 0 0 .07.027l10.122-11.567a.304.304 0 0 0-.229-.504H5.468v-.024l10.477-4.42Z' clip-rule='evenodd'/%3E%3C/g%3E%3Cdefs%3E%3CclipPath id='a'%3E%3Cpath fill='%23fff' d='M0 0h32v32H0z'/%3E%3C/clipPath%3E%3C/defs%3E%3C/svg%3E%0A");
}
</style>
<?php
}

public function __construct() {
add_action( 'in_admin_header', [ $this, 'admin_print_script' ] );
}
}
80 changes: 43 additions & 37 deletions modules/settings/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public function get_name(): string {
return 'settings';
}

public static function component_list(): array {
return [
'Settings_Pointer',
];
}

public function render_app() {
?>
<!-- The hack required to wrap WP notifications -->
Expand Down Expand Up @@ -115,42 +121,42 @@ public function on_connect(): void {

if ( $register_response && ! is_wp_error( $register_response ) ) {
Data::set_subscription_id( $register_response->id );
update_option( Settings::PLAN_DATA, $register_response );
update_option( Settings::IS_VALID_PLAN_DATA, true );
update_option( Settings::PLAN_DATA, $register_response );
update_option( Settings::IS_VALID_PLAN_DATA, true );
} else {
Logger::error( esc_html( $register_response->get_error_message() ) );
update_option( Settings::IS_VALID_PLAN_DATA, false );
}
}

/**
* Retry registering the site if it fails during connect.
*
* Retry registering the site if it fails during connect.
*
* @param $current_screen
* @return void
*/
public function check_plan_data( $current_screen ) : void {
//TODO: Update page name
if ( 'toplevel_page_accessibility-settings-2' !== $current_screen->base ) {
return;
}

if ( Connect::is_connected() && get_option( Settings::PLAN_DATA ) === false ) {
$register_response = Utils::get_api_client()->make_request(
'POST',
'site/register'
);

if ( $register_response && ! is_wp_error( $register_response ) ) {
Data::set_subscription_id( $register_response->id );
update_option( Settings::PLAN_DATA, $register_response );
update_option( Settings::IS_VALID_PLAN_DATA, true );
} else {
Logger::error( esc_html( $register_response->get_error_message() ) );
update_option( Settings::IS_VALID_PLAN_DATA, false );
}
}
}
public function check_plan_data( $current_screen ) : void {
//TODO: Update page name
if ( 'toplevel_page_accessibility-settings-2' !== $current_screen->base ) {
return;
}

if ( Connect::is_connected() && get_option( Settings::PLAN_DATA ) === false ) {
$register_response = Utils::get_api_client()->make_request(
'POST',
'site/register'
);

if ( $register_response && ! is_wp_error( $register_response ) ) {
Data::set_subscription_id( $register_response->id );
update_option( Settings::PLAN_DATA, $register_response );
update_option( Settings::IS_VALID_PLAN_DATA, true );
} else {
Logger::error( esc_html( $register_response->get_error_message() ) );
update_option( Settings::IS_VALID_PLAN_DATA, false );
}
}
}

/**
* Register settings.
Expand All @@ -167,34 +173,34 @@ public function register_settings(): void {
'show_in_rest' => [
'schema' => [
'type' => 'object',
'additionalProperties' => true
'additionalProperties' => true,
],
]
],
],
'widget_icon_settings' => [
'type' => 'object',
'show_in_rest' => [
'schema' => [
'type' => 'object',
'additionalProperties' => true
'additionalProperties' => true,
],
]
],
],
'plan_data' => [
'type' => 'object',
'show_in_rest' => [
'schema' => [
'type' => 'object',
'additionalProperties' => true
'additionalProperties' => true,
],
]
],
],
'close_post_connect_modal' => [
'type' => 'boolean',
],
'hide_minimum_active_options_alert' => [
'type' => 'boolean',
]
'hide_minimum_active_options_alert' => [
'type' => 'boolean',
],
];

foreach ( $settings as $setting => $args ) {
Expand All @@ -210,11 +216,11 @@ public function register_settings(): void {
*/
public function __construct() {
$this->register_routes();
$this->register_components();
$this->register_components( self::component_list() );
add_action( 'admin_menu', [ $this, 'register_page' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
add_action( 'rest_api_init', [ $this, 'register_settings' ] );
add_action( 'on_connect_' . Config::APP_PREFIX . '_connected', [ $this, 'on_connect' ] );
add_action( 'current_screen', [ $this, 'check_plan_data' ] );
add_action( 'current_screen', [ $this, 'check_plan_data' ] );
}
}

0 comments on commit 8593f89

Please sign in to comment.