From 7b4196e5d65ad4d953f28f3617208902076cf970 Mon Sep 17 00:00:00 2001
From: "daan.rijpkema"
Date: Thu, 18 Apr 2024 16:15:53 +0200
Subject: [PATCH] Create activation notifications and file writing
---
bluem.php | 78 +-----------
scripts/.env.sample | 2 +
scripts/loadenv.sh | 11 ++
src/Observability/BluemActivationNotifier.php | 111 ++++++++++++++++++
4 files changed, 127 insertions(+), 75 deletions(-)
create mode 100644 scripts/.env.sample
create mode 100755 scripts/loadenv.sh
create mode 100644 src/Observability/BluemActivationNotifier.php
diff --git a/bluem.php b/bluem.php
index 1b2e530..43627ac 100644
--- a/bluem.php
+++ b/bluem.php
@@ -38,6 +38,7 @@
require __DIR__ . '/vendor/autoload.php';
use Bluem\BluemPHP\Bluem;
+use Bluem\Wordpress\Observability\BluemActivationNotifier;
use Bluem\Wordpress\Observability\SentryLogger;
@@ -358,7 +359,8 @@ function bluem_plugin_activation() {
$bluem_registration['tech_contact']['email'] = $tech_email;
// Sent registration notify email
- bluem_registration_report_email();
+ (new BluemActivationNotifier())->reportActivatedPlugin();
+
// Update Bluem options
update_option('bluem_woocommerce_options', $bluem_options);
@@ -1373,81 +1375,7 @@ function bluem_error_report_email( $data = [] ): bool {
return false;
}
-/**
- * Registration reporting email functionality
- * @return bool
- */
-function bluem_registration_report_email( $data = [] ): bool
-{
- $bluem_options = get_option( 'bluem_woocommerce_options' );
- $bluem_registration = get_option( 'bluem_woocommerce_registration' );
-
- $dependency_bluem_php_version = get_composer_dependency_version('bluem-development/bluem-php');
-
- $activation_report_id = date( "Ymdhis" ) . '_' . rand( 0, 512 );
-
- $data = (object) $data;
- $data->activation_report_id = $activation_report_id;
- $data->{'Bluem SenderID'} = $bluem_options['senderID'] ?? '';
- $data->{'Website name'} = get_bloginfo( 'name' );
- $data->{'Website URL'} = get_bloginfo( 'url' );
- $data->{'Company name'} = $bluem_registration['company']['name'];
- $data->{'Company telephone'} = $bluem_registration['company']['telephone'];
- $data->{'Company email'} = $bluem_registration['company']['email'];
- $data->{'Tech name'} = $bluem_registration['tech_contact']['name'];
- $data->{'Tech telephone'} = $bluem_registration['tech_contact']['telephone'];
- $data->{'Tech email'} = $bluem_registration['tech_contact']['email'];
- $data->{'WooCommerce version'} = class_exists('WooCommerce') ? WC()->version : __('WooCommerce not installed', 'bluem');
- $data->{'WordPress version'} = get_bloginfo( 'version' );
- $data->{'Bluem PHP-library'} = $dependency_bluem_php_version;
- $data->{'Plug-in version'} = $bluem_options['bluem_plugin_version'] ?? '0';
- $data->{'PHP version'} = phpversion();
-
- if ( is_null( $data ) ) {
- return false;
- }
-
- $author_name = "Administratie van " . get_bloginfo( 'name' );
- $author_email = esc_attr(
- get_option( "admin_email" )
- );
-
- $to = "pluginsupport@bluem.nl";
-
- $subject = "[" . get_bloginfo( 'name' ) . "] ";
- $subject .= "WordPress plug-in activation";
- $message = "WordPress plug-in activation.
$author_name <$author_email>,
";
- $message .= "Data:
" . json_encode( $data ) . "
";
-
- ob_start();
- foreach ( $data as $k => $v ) {
- if ( is_null( $v ) ) {
- continue;
- }
-
- bluem_render_obj_row_recursive(
- "" . ucfirst( $k ) . "",
- $v
- );
- }
- $message_p = ob_get_clean();
-
- $message .= $message_p;
- $message .= "
";
-
- $message = nl2br( $message );
-
- $headers = array( 'Content-Type: text/html; charset=UTF-8' );
-
- $mailing = wp_mail( $to, $subject, $message, $headers );
-
- if ( $mailing ) {
- bluem_db_request_log( $activation_report_id, "Sent activation report mail to " . $to );
- }
-
- return $mailing;
-}
function bluem_email_footer(): string {
return "Ga naar de site op " . home_url() . " om dit verzoek in detail te bekijken.
";
diff --git a/scripts/.env.sample b/scripts/.env.sample
new file mode 100644
index 0000000..063f964
--- /dev/null
+++ b/scripts/.env.sample
@@ -0,0 +1,2 @@
+MAILJET_API_KEY=
+MAILJET_SECRET_KEY=
diff --git a/scripts/loadenv.sh b/scripts/loadenv.sh
new file mode 100755
index 0000000..4abc168
--- /dev/null
+++ b/scripts/loadenv.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+EMAIL=d.rijpkema@bluem.nl
+export $(cat .env | xargs)
+echo "Loaded keys: ${MAILJET_API_KEY} ${MAILJET_SECRET_KEY}"
+curl -s \
+ -X POST \
+ --user "$MAILJET_API_KEY:$MAILJET_SECRET_KEY" \
+ "https://api.mailjet.com/v3.1/send" \
+ -H "Content-Type: application/json" \
+ -d '{"Messages":[{"From":{"Email":"d.rijpkema@bluem.nl","Name":"Your Name"},"To":[{"Email":"pluginsupport@bluem.nl","Name":"Pluginsupport"}],"Subject":"Release Completed","TextPart":"Release $(NEW_TAG) of $(PLUGIN_SLUG) has been completed. (test)"}]}' \
+ && echo "\nNotification email sent to $EMAIL}"
diff --git a/src/Observability/BluemActivationNotifier.php b/src/Observability/BluemActivationNotifier.php
new file mode 100644
index 0000000..6e544f3
--- /dev/null
+++ b/src/Observability/BluemActivationNotifier.php
@@ -0,0 +1,111 @@
+createReportData();
+
+ $author_name = "Administratie van " . esc_attr(get_bloginfo('name'));
+ $author_email = esc_attr(
+ get_option("admin_email")
+ );
+
+ $to = self::NOTIFICATION_EMAIL;
+
+ $subject = "[" . get_bloginfo('name') . "] WordPress plug-in activation";
+
+ $message = sprintf("WordPress plug-in activation (door %s <%s>),
", $author_name, $author_email);
+ $message .= sprintf("Data:
%s
", $this->createStringFromData($data));
+ $message .= sprintf("Raw Data:
%s
", json_encode($data));
+
+ $message = nl2br($message);
+
+ $headers = array('Content-Type: text/html; charset=UTF-8');
+
+ $mailing = wp_mail($to, $subject, $message, $headers);
+
+ if (!$mailing) {
+ return false;
+ }
+
+ bluem_db_request_log($data->activation_report_id, "Sent activation report mail to " . $to);
+
+ if ($this->writeActivationFile($data)) {
+ bluem_db_request_log($data->activation_report_id, "Written activation log file");
+ }
+
+ return $mailing;
+ }
+
+ private function createReportData(): object {
+ $bluem_options = get_option('bluem_woocommerce_options');
+ $bluem_registration = get_option('bluem_woocommerce_registration');
+
+ $dependency_bluem_php_version = get_composer_dependency_version('bluem-development/bluem-php') ?? 'unknown';
+
+ $activation_report_id = sprintf("%s_%s", date("Ymdhis"), random_int(0, 512));
+
+ $data = new Stdclass();
+ $data->activation_report_id = $activation_report_id;
+ $data->{'Bluem SenderID'} = $bluem_options['senderID'] ?? '';
+ $data->{'Website name'} = esc_attr(get_bloginfo('name'));
+ $data->{'Website URL'} = esc_attr(get_bloginfo('url'));
+ $data->{'Admin email'} = esc_attr(get_bloginfo('admin_email'));
+ $data->{'Company name'} = $bluem_registration['company']['name'];
+ $data->{'Company telephone'} = $bluem_registration['company']['telephone'];
+ $data->{'Company email'} = $bluem_registration['company']['email'];
+ $data->{'Tech name'} = $bluem_registration['tech_contact']['name'];
+ $data->{'Tech telephone'} = $bluem_registration['tech_contact']['telephone'];
+ $data->{'Tech email'} = $bluem_registration['tech_contact']['email'];
+ $data->{'WooCommerce version'} = class_exists('WooCommerce') ? WC()->version : __('WooCommerce not installed', 'bluem');
+ $data->{'WordPress version'} = get_bloginfo('version');
+ $data->{'Bluem PHP-library'} = $dependency_bluem_php_version;
+ $data->{'Plug-in version'} = $bluem_options['bluem_plugin_version'] ?? '0';
+ $data->{'PHP version'} = PHP_VERSION;
+ $data->{'Activation date'} = date("Y-m-d H:i:s");
+ $data->{'Activation IP'} = ''; // @todo: get IP?
+
+ return $data;
+ }
+
+ private function writeActivationFile(stdClass $data)
+ {
+ $filename = sprintf("logs/activations_%s.json", date("Ymd"));
+
+ try {
+ $fileContent = json_encode($data, JSON_THROW_ON_ERROR);
+ } catch (Exception) {
+
+ }
+
+ return file_put_contents($filename, $fileContent, FILE_APPEND);
+ }
+
+ private function createStringFromData(stdClass $data): string
+ {
+ ob_start();
+ foreach ($data as $k => $v) {
+ if (is_null($v)) {
+ continue;
+ }
+
+ bluem_render_obj_row_recursive(
+ sprintf("%s", ucfirst($k)),
+ $v
+ );
+ }
+ return ob_get_clean() ?? '';
+ }
+}