Skip to content

Commit

Permalink
Create activation notifications and file writing
Browse files Browse the repository at this point in the history
  • Loading branch information
daanrijpkemacb committed Apr 18, 2024
1 parent 80c8053 commit 7b4196e
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 75 deletions.
78 changes: 3 additions & 75 deletions bluem.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
require __DIR__ . '/vendor/autoload.php';

use Bluem\BluemPHP\Bluem;
use Bluem\Wordpress\Observability\BluemActivationNotifier;
use Bluem\Wordpress\Observability\SentryLogger;


Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 = "[email protected]";

$subject = "[" . get_bloginfo( 'name' ) . "] ";
$subject .= "WordPress plug-in activation";

$message = "<p>WordPress plug-in activation.<br />$author_name <$author_email>,</p>";
$message .= "<p>Data: <br>" . json_encode( $data ) . "</p>";

ob_start();
foreach ( $data as $k => $v ) {
if ( is_null( $v ) ) {
continue;
}

bluem_render_obj_row_recursive(
"<strong>" . ucfirst( $k ) . "</strong>",
$v
);
}
$message_p = ob_get_clean();

$message .= $message_p;
$message .= "</p>";

$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 "<p>Ga naar de site op " . home_url() . " om dit verzoek in detail te bekijken.</p>";
Expand Down
2 changes: 2 additions & 0 deletions scripts/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MAILJET_API_KEY=
MAILJET_SECRET_KEY=
11 changes: 11 additions & 0 deletions scripts/loadenv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
[email protected]
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":"[email protected]","Name":"Your Name"},"To":[{"Email":"[email protected]","Name":"Pluginsupport"}],"Subject":"Release Completed","TextPart":"Release $(NEW_TAG) of $(PLUGIN_SLUG) has been completed. (test)"}]}' \
&& echo "\nNotification email sent to $EMAIL}"
111 changes: 111 additions & 0 deletions src/Observability/BluemActivationNotifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Bluem\Wordpress\Observability;

use Exception;
use stdClass;

class BluemActivationNotifier
{
private const NOTIFICATION_EMAIL = "[email protected]";


/**
* Registration reporting email functionality
*/
public function reportActivatedPlugin(): bool
{
$data = $this->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("<p>WordPress plug-in activation (door %s <%s>),</p>", $author_name, $author_email);
$message .= sprintf("<p>Data:<br>%s</p>", $this->createStringFromData($data));
$message .= sprintf("<p>Raw Data:<br>%s</p>", 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("<strong>%s</strong>", ucfirst($k)),
$v
);
}
return ob_get_clean() ?? '';
}
}

0 comments on commit 7b4196e

Please sign in to comment.