File size: 5,792 Bytes
bd39fdf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | <?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\Feedback;
use Piwik\Common;
use Piwik\Config;
use Piwik\Config\GeneralConfig;
use Piwik\Container\StaticContainer;
use Piwik\DataTable\Renderer\Json;
use Piwik\Date;
use Piwik\Mail;
use Piwik\Piwik;
use Piwik\SettingsServer;
use Piwik\Url;
use Piwik\Version;
/**
* Provides API methods for submitting product feedback and managing feedback reminders.
*
* @method static \Piwik\Plugins\Feedback\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
/**
* Sends a survey response to the Matomo team or to the configured feedback email address.
*
* @param string $featureName Name of the feature the feedback is about.
* @param bool|null $like Whether the user likes the feature.
* @param string|null $choice Optional selected multiple-choice answer.
* @param string|null $message Feedback message entered by the user.
* @return string Translation key text when validation fails, or `success` when the feedback email is sent.
*/
public function sendFeedbackForFeature(string $featureName, ?bool $like = null, ?string $choice = null, ?string $message = null)
{
Piwik::checkUserIsNotAnonymous();
Piwik::checkUserHasSomeViewAccess();
if (empty($message) || $message === 'undefined' || strlen($message) < 4) {
return Piwik::translate("Feedback_FormNotEnoughFeedbackText");
}
$featureName = $this->getEnglishTranslationForFeatureName($featureName);
$likeText = 'Yes';
if (empty($like)) {
$likeText = 'No';
}
$body = sprintf("Feature: %s\nLike: %s\n", $featureName, $likeText);
if (!empty($choice) && $choice !== 'undefined') {
$body .= "Choice: " . $choice . "\n";
}
$body .= sprintf("Feedback:\n%s\n", trim($message));
$subject = sprintf(
"%s for %s",
empty($like) ? "-1" : "+1",
$featureName
);
// Determine where Matomo is running and add as source
if (Config::getHostname() === 'demo.matomo.cloud') {
$source = 'Demo';
} elseif (SettingsServer::isMatomoForWordPress()) {
$source = 'Wordpress';
} else {
$source = 'On-Premise';
}
$body .= "Source: " . $source . "\n";
$this->sendMail($subject, $body);
return 'success';
}
/**
* Sends feedback for a specific feature to the Matomo team or alternatively to the email address configured in the
* config: "feedback_email_address".
*
* @param string $question Survey question or feature label the answer belongs to.
* @param string|false $message Survey answer entered by the user.
* @return string Translation key text when validation fails, or `success` when the feedback email is sent.
*/
public function sendFeedbackForSurvey(string $question, $message = false): string
{
Piwik::checkUserIsNotAnonymous();
Piwik::checkUserHasSomeViewAccess();
if (empty($message) || strlen($message) < 10) {
return Piwik::translate("Feedback_MessageBodyValidationError");
}
$featureName = $this->getEnglishTranslationForFeatureName($question);
$body = sprintf("Question: %s\n", $featureName);
$body .= sprintf("Answer:\n%s\n", trim($message));
$subject = sprintf(
"-1 for %s (w/ feedback Survey)",
$featureName
);
$this->sendMail($subject, $body);
//if feedback is sent set next one to 6 month.
$nextReminder = Date::now()->getStartOfDay()->addMonth(6)->toString('Y-m-d');
$feedbackReminder = new FeedbackReminder();
$feedbackReminder->setUserOption($nextReminder);
return 'success';
}
/**
* Postpones the feedback reminder for the current user by six months.
*
* @return string JSON-encoded array containing the next reminder date.
*/
public function updateFeedbackReminderDate(): string
{
Piwik::checkUserIsNotAnonymous();
//push reminder for 6 month
$nextReminder = Date::now()->getStartOfDay()->addMonth(6)->toString('Y-m-d');
$feedbackReminder = new FeedbackReminder();
$feedbackReminder->setUserOption($nextReminder);
Json::sendHeaderJSON();
return json_encode(['Next reminder date: ' . $nextReminder]) ?: '';
}
private function sendMail(string $subject, string $body): void
{
/** @var string $feedbackEmailAddress */
$feedbackEmailAddress = GeneralConfig::getConfigValue('feedback_email_address');
$subject = '[ Feedback Feature - Matomo ] ' . $subject;
$body = Common::unsanitizeInputValue($body) . "\n"
. 'Matomo ' . Version::VERSION . "\n"
. 'URL: ' . Url::getReferrer() . "\n";
$mail = new Mail();
$mail->setDefaultFromPiwik();
$mail->addReplyTo(Piwik::getCurrentUserEmail());
$mail->addTo($feedbackEmailAddress, 'Matomo Team');
$mail->setSubject($subject);
$mail->setBodyText($body);
@$mail->send();
}
private function getEnglishTranslationForFeatureName(string $featureName): string
{
$translator = StaticContainer::get('Piwik\Translation\Translator');
if ($translator->getCurrentLanguage() === 'en') {
return $featureName;
}
$translationKeyForFeature = $translator->findTranslationKeyForTranslation($featureName);
return Piwik::translate($translationKeyForFeature ?? '', [], 'en');
}
}
|