Add a button for recalculating score

[MAILPOET-3525]
This commit is contained in:
Pavel Dohnal
2021-04-21 14:16:13 +02:00
committed by Veljko V
parent 74f9505772
commit 9eeeb10415
8 changed files with 102 additions and 0 deletions

View File

@ -67,6 +67,7 @@
// this violation has been added by a formed employee and we don't know how that works any more, so ignore // this violation has been added by a formed employee and we don't know how that works any more, so ignore
"files": [ "files": [
"assets/js/src/settings/pages/advanced/reinstall.tsx", "assets/js/src/settings/pages/advanced/reinstall.tsx",
"assets/js/src/settings/pages/advanced/recalculate_subscriber_score.tsx",
"assets/js/src/settings/pages/key_activation/key_activation.tsx", "assets/js/src/settings/pages/key_activation/key_activation.tsx",
"assets/js/src/settings/pages/send_with/other/activate_or_cancel.tsx", "assets/js/src/settings/pages/send_with/other/activate_or_cancel.tsx",
"assets/js/src/settings/pages/send_with/send_with_choice.tsx" "assets/js/src/settings/pages/send_with/send_with_choice.tsx"

View File

@ -9,6 +9,7 @@ import ShareData from './share_data';
import { Libs3rdParty } from './libs_3rd_party'; import { Libs3rdParty } from './libs_3rd_party';
import Captcha from './captcha'; import Captcha from './captcha';
import Reinstall from './reinstall'; import Reinstall from './reinstall';
import { RecalculateSubscriberScore } from './recalculate_subscriber_score';
import Logging from './logging'; import Logging from './logging';
import BounceAddress from './bounce_address'; import BounceAddress from './bounce_address';
@ -20,6 +21,7 @@ export default function Advanced() {
<Roles /> <Roles />
<Tracking /> <Tracking />
<Transactional /> <Transactional />
<RecalculateSubscriberScore />
<InactiveSubscribers /> <InactiveSubscribers />
<ShareData /> <ShareData />
<Libs3rdParty /> <Libs3rdParty />

View File

@ -0,0 +1,36 @@
import React from 'react';
import Button from 'common/button/button';
import { t } from 'common/functions';
import { GlobalContext } from 'context';
import { useAction } from 'settings/store/hooks';
import { Label, Inputs } from 'settings/components';
export function RecalculateSubscriberScore(): JSX.Element {
const recalculateSubscribersScore = useAction('recalculateSubscribersScore');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { notices } = React.useContext<any>(GlobalContext);
const onClick = async (): Promise<void> => {
await recalculateSubscribersScore();
notices.info(<p>{t('recalculateSubscribersScoreNotice')}</p>, { scroll: true });
};
return (
<>
<Label
title={t('recalculateSubscribersScoreTitle')}
description={t('recalculateSubscribersScoreDescription')}
htmlFor=""
/>
<Inputs>
<Button
type="button"
onClick={onClick}
variant="light"
dimension="small"
>
{t('recalculateSubscribersScoreNow')}
</Button>
</Inputs>
</>
);
}

View File

@ -1,5 +1,6 @@
export * from './settings'; export * from './settings';
export * from './mss_and_premium'; export * from './mss_and_premium';
export { default as reinstall } from './reinstall'; export { default as reinstall } from './reinstall';
export { recalculateSubscribersScore } from './recalculate_subscribers_score';
export { default as sendTestEmail } from './send_test_email'; export { default as sendTestEmail } from './send_test_email';
export { default as openWoocommerceCustomizer } from './open_woocommerce_customizer'; export { default as openWoocommerceCustomizer } from './open_woocommerce_customizer';

View File

@ -0,0 +1,16 @@
import MailPoet from 'mailpoet';
export function* recalculateSubscribersScore(): Generator<{
type: string;
endpoint: string;
action: string;
}> {
MailPoet.Modal.loading(true);
yield {
type: 'CALL_API',
endpoint: 'settings',
action: 'recalculateSubscribersScore',
};
MailPoet.Modal.loading(false);
return { type: 'SAVE_DONE' };
}

View File

@ -6,12 +6,17 @@ use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError; use MailPoet\API\JSON\Error as APIError;
use MailPoet\Config\AccessControl; use MailPoet\Config\AccessControl;
use MailPoet\Config\ServicesChecker; use MailPoet\Config\ServicesChecker;
use MailPoet\Cron\Workers\SubscribersEngagementScore;
use MailPoet\Entities\ScheduledTaskEntity;
use MailPoet\Mailer\MailerLog; use MailPoet\Mailer\MailerLog;
use MailPoet\Services\AuthorizedEmailsController; use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Services\Bridge; use MailPoet\Services\Bridge;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
use MailPoet\Statistics\StatisticsOpensRepository;
use MailPoet\WooCommerce\TransactionalEmails; use MailPoet\WooCommerce\TransactionalEmails;
use MailPoet\WP\Functions as WPFunctions; use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Carbon\Carbon;
use MailPoetVendor\Doctrine\ORM\EntityManager;
class Settings extends APIEndpoint { class Settings extends APIEndpoint {
@ -30,15 +35,31 @@ class Settings extends APIEndpoint {
/** @var ServicesChecker */ /** @var ServicesChecker */
private $servicesChecker; private $servicesChecker;
/** @var WPFunctions */
private $wp;
public $permissions = [ public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS, 'global' => AccessControl::PERMISSION_MANAGE_SETTINGS,
]; ];
/**
* @var EntityManager
*/
private $entityManager;
/**
* @var StatisticsOpensRepository
*/
private $statisticsOpensRepository;
public function __construct( public function __construct(
SettingsController $settings, SettingsController $settings,
Bridge $bridge, Bridge $bridge,
AuthorizedEmailsController $authorizedEmailsController, AuthorizedEmailsController $authorizedEmailsController,
TransactionalEmails $wcTransactionalEmails, TransactionalEmails $wcTransactionalEmails,
WPFunctions $wp,
EntityManager $entityManager,
StatisticsOpensRepository $statisticsOpensRepository,
ServicesChecker $servicesChecker ServicesChecker $servicesChecker
) { ) {
$this->settings = $settings; $this->settings = $settings;
@ -46,6 +67,9 @@ class Settings extends APIEndpoint {
$this->authorizedEmailsController = $authorizedEmailsController; $this->authorizedEmailsController = $authorizedEmailsController;
$this->wcTransactionalEmails = $wcTransactionalEmails; $this->wcTransactionalEmails = $wcTransactionalEmails;
$this->servicesChecker = $servicesChecker; $this->servicesChecker = $servicesChecker;
$this->wp = $wp;
$this->entityManager = $entityManager;
$this->statisticsOpensRepository = $statisticsOpensRepository;
} }
public function get() { public function get() {
@ -81,6 +105,17 @@ class Settings extends APIEndpoint {
} }
} }
public function recalculateSubscribersScore() {
$this->statisticsOpensRepository->resetSubscribersScoreCalculation();
$task = new ScheduledTaskEntity();
$task->setType(SubscribersEngagementScore::TASK_TYPE);
$task->setScheduledAt(Carbon::createFromTimestamp($this->wp->currentTime('timestamp')));
$task->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED);
$this->entityManager->persist($task);
$this->entityManager->flush();
return $this->successResponse();
}
public function setAuthorizedFromAddress($data = []) { public function setAuthorizedFromAddress($data = []) {
$address = $data['address'] ?? null; $address = $data['address'] ?? null;
if (!$address) { if (!$address) {

View File

@ -47,4 +47,11 @@ class StatisticsOpensRepository extends Repository {
$subscriber->setEngagementScore($score); $subscriber->setEngagementScore($score);
$this->entityManager->flush(); $this->entityManager->flush();
} }
public function resetSubscribersScoreCalculation() {
$this->entityManager->createQueryBuilder()->update(SubscriberEntity::class, 's')
->set('s.engagementScoreUpdatedAt', ':verified')
->setParameter('verified', null)
->getQuery()->execute();
}
} }

View File

@ -150,6 +150,10 @@
'yourReCaptchaSecret': __('Your reCAPTCHA Secret Key'), 'yourReCaptchaSecret': __('Your reCAPTCHA Secret Key'),
'fillReCaptchaKeys': __('Please fill the reCAPTCHA keys.'), 'fillReCaptchaKeys': __('Please fill the reCAPTCHA keys.'),
'disable': __('Disable'), 'disable': __('Disable'),
'recalculateSubscribersScoreTitle': __('Recalculate Subscriber Scores'),
'recalculateSubscribersScoreDescription': __('MailPoet will recalculate subscriber engagement scores for all subscribers. This may take some time to complete.'),
'recalculateSubscribersScoreNow': __('Update now…'),
'recalculateSubscribersScoreNotice': __('Subscriber score recalculation has been scheduled and will start soon. It may take some time to complete.'),
'reinstallTitle': __('Reinstall from scratch'), 'reinstallTitle': __('Reinstall from scratch'),
'reinstallDescription': __('Want to start from the beginning? This will completely delete MailPoet and reinstall it from scratch. Remember: you will lose all of your data!'), 'reinstallDescription': __('Want to start from the beginning? This will completely delete MailPoet and reinstall it from scratch. Remember: you will lose all of your data!'),
'reinstallNow': __('Reinstall now...'), 'reinstallNow': __('Reinstall now...'),