Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Added option to save content created by temporary user [ED-16249] #7

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions core/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ public static function get_app_data() {
wp_send_json_error( esc_html__( "You don't have permission to access this request", 'temporary-login' ) );
}

$current_user = wp_get_current_user();

$data = [
'status' => 'inactive',
'current_user_logged_in_display_name' => $current_user->display_name ?? 'Unknown',
];

$temporary_users = Options::get_temporary_users();
Expand Down Expand Up @@ -57,11 +60,23 @@ private static function get_active_page_data( \WP_User $temporary_user ): array
$is_elementor_connected = $elementor_connect->is_connected();
}

$created_by_user_id = Options::get_created_by_user_id( $temporary_user->ID );
if ( $created_by_user_id ) {
$created_user = get_user_by( 'ID', $created_by_user_id );

if ( $created_user ) {
$reassign_to = $created_user->display_name;
$reassign_user_profile_link = get_edit_user_link( $created_user->ID );
}
}

return [
'status' => 'active',
'is_elementor_connected' => $is_elementor_connected,
'login_url' => Options::get_login_url( $temporary_user->ID ),
'expiration_human' => Options::get_expiration_human( $temporary_user->ID ),
'reassign_to' => $reassign_to ?? '',
'reassign_user_profile_link' => $reassign_user_profile_link ?? '',
];
}

Expand All @@ -87,9 +102,13 @@ public static function enable_access() {
wp_send_json_success();
}

$user_id = Options::generate_temporary_user();
if ( is_wp_error( $user_id ) ) {
wp_send_json_error( $user_id );
$user_ID = Options::generate_temporary_user();
if ( is_wp_error( $user_ID ) ) {
wp_send_json_error( $user_ID );
}

if ( ! empty( $_POST['is_keep_user_posts'] ) && 'true' === $_POST['is_keep_user_posts'] ) {
Options::set_created_by_user_id( $user_ID );
}

wp_send_json_success();
Expand Down
25 changes: 21 additions & 4 deletions core/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ private static function delete_site_token(): void {
delete_option( '_temporary_login_site_token' );
}

public static function set_created_by_user_id( $user_ID ) {
update_user_meta( $user_ID, '_temporary_login_created_by_user_id', get_current_user_id() );
}

public static function get_created_by_user_id( $user_ID ) {
return get_user_meta( $user_ID, '_temporary_login_created_by_user_id', true );
}

public static function is_temporary_user( $user_ID ) : bool {
return (bool) get_user_meta( $user_ID, '_temporary_login', true );
}
Expand Down Expand Up @@ -175,15 +183,24 @@ public static function remove_all_temporary_users() {
return;
}

foreach ( $temporary_users as $user ) {
static::remove_user( $user->ID );
}

static::delete_site_token();
}

private static function remove_user( $user_ID ) {
if ( ! function_exists( 'wp_delete_user' ) ) {
require_once ABSPATH . 'wp-admin/includes/user.php';
}

foreach ( $temporary_users as $user ) {
wp_delete_user( $user->ID );
$reassign_user_ID = static::get_created_by_user_id( $user_ID );
if ( empty( $reassign_user_ID ) ) {
$reassign_user_ID = null;
}

static::delete_site_token();
wp_delete_user( $user_ID, $reassign_user_ID );
}

public static function remove_expired_temporary_users() {
Expand All @@ -201,7 +218,7 @@ public static function remove_expired_temporary_users() {
}

foreach ( $users_IDs as $user_ID ) {
wp_delete_user( $user_ID );
static::remove_user( $user_ID );
}
}
}
4 changes: 2 additions & 2 deletions src/admin/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ const getConfigData = () => {
return ( window as unknown as WindowWithData ).ePremiumSupportSettings;
};

export const generateTemporaryUser = async () => {
export const generateTemporaryUser = async ( isKeepUserPosts = false ) => {
const configData = getConfigData();

const response = await fetch( configData.ajaxurl, {
method: 'POST',
headers: new Headers( {
'Content-Type': 'application/x-www-form-urlencoded',
} ),
body: `action=temporary_login_generate_temporary_user&nonce=${ configData.nonce }`,
body: `action=temporary_login_generate_temporary_user&nonce=${ configData.nonce }&is_keep_user_posts=${ isKeepUserPosts }`,
} );

const responseJson = await response.json();
Expand Down
4 changes: 2 additions & 2 deletions src/admin/components/confirm-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ export const ConfirmDialog = ( props: ConfirmDialogParams ): ReactElement => {
isDismissible={ false }
onRequestClose={ () => setIsConfirmDialogOpen( false ) }
>
<p
<div
style={ {
maxWidth: '400px',
marginBlockEnd: '30px',
marginBlockStart: '0',
} }
>
{ children }
</p>
</div>
<ButtonGroup
style={ {
display: 'flex',
Expand Down
21 changes: 21 additions & 0 deletions src/admin/components/page-active.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface IActiveData {
login_url: string;
expiration_human: string;
is_elementor_connected: boolean;
reassign_to: string;
reassign_user_profile_link: string;
}

export const PageActive = ( props: IActiveData ): ReactElement => {
Expand Down Expand Up @@ -92,12 +94,16 @@ export const PageActive = ( props: IActiveData ): ReactElement => {
} }
>
<TextControl
__nextHasNoMarginBottom
value={ props.login_url }
type="url"
label={ __( 'Access URL', 'temporary-login' ) + ':' }
readOnly={ true }
onFocus={ ( event ) => event.target.select() }
onChange={ () => {} }
style={ {
marginBottom: 'calc(8px)',
} }
/>

<Flex direction="row">
Expand All @@ -122,6 +128,21 @@ export const PageActive = ( props: IActiveData ): ReactElement => {
</Button>
</FlexItem>
<FlexItem>
{ props.reassign_to && (
<>
<strong>
{ __(
'Content Attributed:',
'temporary-login'
) }
</strong>{ ' ' }
<a href={ props.reassign_user_profile_link }>
{ props.reassign_to }
</a>
{ '. ' }
</>
) }

<strong>
{ __( 'Expiration', 'temporary-login' ) + ':' }
</strong>
Expand Down
55 changes: 48 additions & 7 deletions src/admin/components/page-inactive.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { ReactElement } from 'react';
import { Button } from '@wordpress/components';
import { Button, CheckboxControl, Notice } from '@wordpress/components';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { QUERY_KEY } from '../common/constants';
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { generateTemporaryUser } from '../api';
import { ConfirmDialog } from './confirm-dialog';
import { useState } from '@wordpress/element';

interface IInActiveData {
status: 'inactive';
current_user_logged_in_display_name: string;
}

export const PageInactive = (): ReactElement => {
const [ isConfirmDialogOpen, setIsConfirmDialogOpen ] = useState( false );
const [ isKeepUserPosts, setIsKeepUserPosts ] = useState( false );

const queryClient = useQueryClient();

Expand All @@ -22,7 +24,7 @@ export const PageInactive = (): ReactElement => {
mutationFn: () => {
setIsConfirmDialogOpen( false );

return generateTemporaryUser();
return generateTemporaryUser( isKeepUserPosts );
},
onSuccess: () => {
return queryClient.invalidateQueries( {
Expand All @@ -33,11 +35,29 @@ export const PageInactive = (): ReactElement => {

return (
<>
<Notice isDismissible={ false } status="info">
<ContentAttributedExplain
displayName={ data.current_user_logged_in_display_name }
/>
<div>
<CheckboxControl
__nextHasNoMarginBottom
label={ __(
'Save content after access expires',
'temporary-login'
) }
checked={ isKeepUserPosts }
onChange={ setIsKeepUserPosts }
/>
</div>
</Notice>

<Button
variant="primary"
onClick={ () => setIsConfirmDialogOpen( true ) }
disabled={ generateUserMutation.isPending }
isBusy={ generateUserMutation.isPending }
style={ { marginTop: '20px' } }
>
{ __( 'Grant Access', 'temporary-login' ) }
</Button>
Expand All @@ -56,12 +76,33 @@ export const PageInactive = (): ReactElement => {
onConfirm={ generateUserMutation.mutate }
confirmButtonText={ __( 'Grant', 'temporary-login' ) }
>
{ __(
'Authorize Temporary Login to create admin-level access to your website.',
'temporary-login'
) }
<p>
{ __(
'Authorize Temporary Login to create admin-level access to your website.',
'temporary-login'
) }
</p>
</ConfirmDialog>
) }
</>
);
};

const ContentAttributedExplain = ( { displayName } ) => {
const formattedMessage = sprintf(
/* translators: %s: user display name */
__(
'Content created by the temporary login user is temporary. To save it, select the option provided. The content will then be attributed to the %s user once the temporary access expires.',
'temporary-login'
),
'<strong>' + displayName + '</strong>'
);

return (
<p
dangerouslySetInnerHTML={ {
__html: formattedMessage,
} }
/>
);
};