wip newsletter, started by creating the acf field and the scheduled event

This commit is contained in:
asus
2024-05-01 19:34:51 +02:00
parent 61be6143a3
commit 746919082a
2 changed files with 182 additions and 1 deletions

View File

@@ -4,7 +4,7 @@ Plugin Name: hggg_cipf
Plugin URI:
Description:
Author: hugogogo
Version: 0.5.12
Version: 0.5.13
Author URI:
*/
@@ -51,6 +51,7 @@ include_once(Plgntls::root_path() . 'php/_actions_payments.php');
include_once(Plgntls::root_path() . 'php/_actions_random_posts.php');
include_once(Plgntls::root_path() . 'php/_actions_scheduled_events.php');
include_once(Plgntls::root_path() . 'php/_actions_emails.php');
include_once(Plgntls::root_path() . 'php/_actions_newsletter.php');
// admin
include_once(Plgntls::root_path() . 'php/admin_hide_bar.php');
include_once(Plgntls::root_path() . 'php/admin_user_profil.php');
@@ -99,6 +100,8 @@ class Cipf {
const ACF_PARTNER_OFFER_3_OUTPUT = ['_name'=>'afficher_offre_3', 'show'=>'Afficher', 'hide'=>'Masquer'];
const ACF_READONLY_CLASS = 'readonly_acf';
const ACF_DATE_FORMAT = 'Ymd';
const ACF_NEWSLETTER_RECEIVE = ['name' => 'recevoir_la_newsletter', 'true' => 'recevoir'];
const ACF_NEWSLETTER_ID = ['name' => 'identifiant_newsletter'];
// META
const META_PAYEMENT_STATUS = 'cipf_payement_status';

View File

@@ -0,0 +1,178 @@
<?php
/*
* it means someone outside wp is accessing the file, in this case kill it.
*/
if (!defined('ABSPATH')) {
die('You can not access this file!');
}
/*
*/
function remove_subscriber_CIPF($id) {
Plgntls::debug_infos();
}
function get_subscribers_emails_CIPF() {
Plgntls::debug_infos();
$acf_newsletter_receive = Cipf::ACF_NEWSLETTER_RECEIVE;
$acf_newsletter_id = Cipf::ACF_NEWSLETTER_ID;
/*
* using LIKE to compare an array, because the content of the acf field is an array
*
*/
$args = array(
'meta_query' => array(
array(
'key' => $acf_newsletter_receive['name'],
'value' => $acf_newsletter_receive['true'],
'compare' => 'LIKE',
),
),
'fields' => array(
'user_email',
),
);
$users = get_users($args);
/*
* transform the array of associatives arrays into an array of string :
* [['user_email' => <email>], ['user_email' => <email>]] -> [<email>, <email>]
*
*/
$users_string = array_map(function($arr) {
return reset($arr);
}, $users);
return $users_string;
}
function get_all_partners_posts() {
Plgntls::debug_infos();
}
function extract_ending_offers($all_posts) {
Plgntls::debug_infos();
}
function extract_new_offers($all_posts) {
Plgntls::debug_infos();
}
function extract_new_partners($all_posts) {
Plgntls::debug_infos();
}
function extract_partners_if_less_than($all_posts, $total_count, $number) {
Plgntls::debug_infos();
}
function compose_newsletter_body_CIPF() {
Plgntls::debug_infos();
$all_posts = get_all_partners_posts();
$ending_offers = extract_ending_offers($all_posts); // [<post_id> => <offer_number>, <post_id> => <offer_number>]
$new_offers = extract_new_offers($all_posts); // [<post_id> => <offer_number>, <post_id> => <offer_number>]
$new_partners = extract_new_partners($all_posts); // [<post_id>, <post_id>]
$total = count($ending_offers) + count($new_offers) + count($new_partners);
$remaining_partners = extract_partners_if_less_than($all_posts, $total, 5); // [<post_id>, <post_id>]
ob_start();
?>
<h1>newsletter cipf</h1>
<?php foreach ($new_partners as $post_id) { ?>
<?php } ?>
<?php foreach ($new_offers as $post_id => $offer_number) { ?>
<?php } ?>
<?php foreach ($ending_offers as $post_id => $offer_number) { ?>
<?php } ?>
<?php foreach ($remaining_partners as $post_id) { ?>
<?php } ?>
<footer><a href="">se desinscrire</a></footer>
<?php
$body_html = ob_get_clean();
return $body_html;
}
function send_newsletter_CIPF() {
Plgntls::debug_infos();
//$email_body = compose_newsletter_body_CIPF();
$emails = get_subscribers_emails_CIPF();
error_log("emails:" . json_encode($emails));
// foreach ($emails as $email) {
//
// }
}
function schedule_monthly_newsletter_CIPF() {
Plgntls::debug_infos();
if (! wp_next_scheduled('CIPF_monthly_newsletter')) {
wp_schedule_event(strtotime('2:00:00'), 'monthly', 'CIPF_send_newsletter');
}
}
add_action('init', 'schedule_monthly_newsletter_CIPF');
add_action('CIPF_send_newsletter', 'send_newsletter_CIPF');
//function newsletter_filter_content_CIPF($message, $email, $user) {
// Plgntls::debug_infos();
//error_log("inside newsletter_filter_content_CIPF");
////error_log("message: " . json_encode($message));
////error_log("email: " . json_encode($email));
////error_log("user: " . json_encode($user));
//
// //$user_id = $user->id;
// $old_body = $message->body;
////error_log("user_id: " . json_encode($user_id));
////error_log("old_body: " . json_encode($old_body));
//
// $pattern = '/\~\~(.*?)\~\~/';
//
// $new_body = preg_replace_callback($pattern, 'newsletter_add_content_CIPF', $old_body);
// $message->body = $new_body;
//
// return $message;
//}
//add_filter("newsletter_message", "newsletter_filter_content_CIPF", 10, 3);
//
//
//function newsletter_add_content_CIPF($matches) {
// Plgntls::debug_infos();
// error_log("matches: " . json_encode($matches));
//
// $action = $matches[1].'_CIPF';
//
// return $action();
//}
//
//function nouveau_partenaire_CIPF() {
// Plgntls::debug_infos();
// error_log("nouveau partenaire");
// return "new partner";
//}
//
//function nouvelle_offre_CIPF() {
// Plgntls::debug_infos();
// error_log("nouvelle offre");
// return "offre nouvelle";
//}
//
//function fin_offre_CIPF() {
// Plgntls::debug_infos();
// error_log("fin offre");
// return "end offer";
//}
?>