Skip to content
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
2 changes: 1 addition & 1 deletion scripts/db
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ ENV_FILE="${ROOT_DIR}/.env"
if [ -f "$ENV_FILE" ]; then
source $ENV_FILE
fi
FILE_NAME="db.backup.sql"
FILE_NAME="me_db.sql"
if [[ "$COMMAND" == "pull" || "$COMMAND" == "download" ]]; then
log_message "INFO" "Downloading ${DB} database..."
if [ "$ENVIRONMENT" == "local" ]; then
Expand Down
58 changes: 58 additions & 0 deletions themes/marianaerato/app/Features/Template_Parser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace App\Features;

class Template_Parser {
protected array $placeholders = [];

public function __construct($order_id = null) {
$this->load_context($order_id);
}

private function load_context($order_id): void {
$user = wp_get_current_user();
$this->placeholders['current_user'] = (array) $user->data;
$this->placeholders['current_user']['first_name'] = $user->first_name;
$this->placeholders['current_user']['last_name'] = $user->last_name;

$this->placeholders['site'] = [
'name' => get_bloginfo('name'),
'url' => get_bloginfo('url'),
'description' => get_bloginfo('description'),
'admin_email' => get_bloginfo('admin_email'),
'language' => get_bloginfo('language'),
];

if ($order_id) {
$order = wc_get_order($order_id);
if ($order) {
$this->placeholders['order'] = [
'id' => $order->get_id(),
'total' => $order->get_total(),
'currency' => $order->get_currency(),
'date' => $order->get_date_created()->date('Y-m-d'),
];
}
}
}

public function parse(?string $content): string {
if (empty($content)) return '';

return preg_replace_callback('/{{\s*(.+?)\s*}}/', function($matches) {
$path = explode('.', trim($matches[1]));
return $this->resolve_path($path);
}, $content);
}

private function resolve_path(array $path) {
$data = $this->placeholders;
foreach ($path as $key) {
if (is_array($data) && array_key_exists($key, $data)) {
$data = $data[$key];
} else {
return $matches[0] ?? '';
}
}
return is_scalar($data) ? $data : '';
}
}
6 changes: 6 additions & 0 deletions themes/marianaerato/app/Features/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ public function get_order_url(\WC_Order $order, $purchased = true): string
'purchased' => time(),
], $post_url) : $post_url;
}

public function send_email($to, $subject, $content): bool {
$mailer = WC()->mailer();
$wrapped_content = $mailer->wrap_message($subject, $content);
return $mailer->send($to, $subject, $wrapped_content);
}
}
22 changes: 21 additions & 1 deletion themes/marianaerato/app/Hooks/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Hooks;

use Automattic\WooCommerce\Enums\OrderStatus;
use \App\Features\Template_Parser;

class WooCommerce {
use \App\Features\WooCommerce;
Expand Down Expand Up @@ -151,7 +152,8 @@ public function assign_sponsorship_to_user(int $user_id, int $product_id): void

$this->items = array_filter(array_unique($this->items));

$this->create_order_for_products($user_id);
$this->process_thank_you_message($user_id, $product_id, $this->order?->get_id());
// $this->create_order_for_products($user_id);
}

public function set_content_access(int $user_id, array $fields): void {
Expand Down Expand Up @@ -221,6 +223,24 @@ public function set_early_access($user_id, $fields) : void {
update_user_meta($user_id, 'me_early_access_to_blog_posts', $early_access_time_amount);
}

public function process_thank_you_message(int $user_id, int $product_id, ?int $order_id): void {
$status = get_field('thank_you_message_status', $product_id);

if ($status === 'enabled') {
$subject_raw = get_field('thank_you_message_subject', $product_id);
$content_raw = get_field('thank_you_message_content', $product_id);
$video_url = get_field('thank_you_message_thank_you_video_url', $product_id);

$parser = new Template_Parser($order_id);

$subject = $parser->parse($subject_raw);
$content = $parser->parse($content_raw);

$current_user = get_userdata($user_id);
$this->send_email($current_user->user_email, $subject, $content);
}
}

public function create_order_for_products(int $user_id) : void {
$new_order = wc_create_order();
foreach ($this->items as $product_id) {
Expand Down
Loading