wip newsletter i dont know how to detect if offer is new

This commit is contained in:
asus
2024-05-02 18:06:54 +02:00
parent 746919082a
commit b75f2718b8
3 changed files with 197 additions and 91 deletions

View File

@@ -4,7 +4,7 @@ Plugin Name: hggg_cipf
Plugin URI:
Description:
Author: hugogogo
Version: 0.5.13
Version: 0.5.15
Author URI:
*/
@@ -100,8 +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'];
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

@@ -12,11 +12,58 @@ if (!defined('ABSPATH')) {
/*
* add a monthly interval to the schedules
*
*/
function remove_subscriber_CIPF($id) {
Plgntls::debug_infos();
function cron_add_monthly_CIPF($schedules) {
// Adds once weekly to the existing schedules.
$schedules['monthly_cipf'] = array(
'interval' => MONTH_IN_SECONDS,
'display' => __( 'every month' )
);
return $schedules;
}
//add_filter( 'cron_schedules', 'cron_add_monthly_CIPF' );
/*
* create the scheduled event
*
*/
function schedule_monthly_newsletter_CIPF() {
Plgntls::debug_infos();
if (! wp_next_scheduled('CIPF_send_newsletter')) {
wp_schedule_event(strtotime('2:00:00'), 'monthly_cipf', 'CIPF_send_newsletter');
}
}
//add_action('init', 'schedule_monthly_newsletter_CIPF');
/*
* main action for the newsletter
*
*/
function send_newsletter_CIPF() {
Plgntls::debug_infos();
$email_body = compose_newsletter_body_CIPF();
error_log("email_body:" . json_encode($email_body));
$emails = get_subscribers_emails_CIPF();
error_log("emails:" . json_encode($emails));
// foreach ($emails as $email) {
//
// }
}
add_action('CIPF_send_newsletter', 'send_newsletter_CIPF');
/*
* returns an array of all the subscribers emails
*
*/
function get_subscribers_emails_CIPF() {
Plgntls::debug_infos();
$acf_newsletter_receive = Cipf::ACF_NEWSLETTER_RECEIVE;
@@ -29,7 +76,7 @@ function get_subscribers_emails_CIPF() {
$args = array(
'meta_query' => array(
array(
'key' => $acf_newsletter_receive['name'],
'key' => $acf_newsletter_receive['_name'],
'value' => $acf_newsletter_receive['true'],
'compare' => 'LIKE',
),
@@ -53,31 +100,28 @@ function get_subscribers_emails_CIPF() {
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) {
/*
*/
function remove_subscriber_CIPF($id) {
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>]
$all_posts = get_all_partner_posts();
//error_log("all_posts: " . json_encode($all_posts));
$ending_offers = extract_ending_offers_CIPF($all_posts); // [<post_id> => <offer_number>, <post_id> => <offer_number>]
//error_log("ending_offers: " . json_encode($ending_offers));
$new_offers = extract_new_offers_CIPF($all_posts); // [<post_id> => <offer_number>, <post_id> => <offer_number>]
error_log("new_offers: " . json_encode($new_offers));
/*
$new_partners = extract_new_partners_CIPF($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>]
$remaining_partners = extract_partners_if_less_than_CIPF($all_posts, $total, 5); // [<post_id>, <post_id>]
ob_start();
?>
@@ -94,85 +138,107 @@ function compose_newsletter_body_CIPF() {
<?php
$body_html = ob_get_clean();
return $body_html;
*/
return '';
}
function send_newsletter_CIPF() {
function extract_ending_offers_CIPF(&$all_posts) {
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');
$offers = array();
foreach ($all_posts as $key => $page_id) {
$ending_offer = get_ending_offer_CIPF($page_id);
if ($ending_offer !== null) {
$offers[$page_id] = $ending_offer;
unset($all_posts[$key]);
//$all_posts = array_values($all_posts);
}
}
return $offers;
}
function get_ending_offer_CIPF($page_id) {
Plgntls::debug_infos();
$i = 1;
while ($i <= 3) {
$offer_duration = get_field_init_CIPF('duree_offre_'.$i, $page_id);
if ($offer_duration !== 'Temporaire') {
++$i;
continue;
}
$offer_output = get_field_init_CIPF('afficher_offre_'.$i, $page_id);
if ($offer_output === 'Masquer') {
++$i;
continue;
}
if (is_offer_date_exceeded_CIPF($i, $page_id)) {
++$i;
continue;
}
$date_limit = get_offer_date_expiration_CIPF($i, $page_id);
if (false === $date_limit) {
++$i;
continue;
}
$date_now = date_create('today');
$date_diff = date_diff($date_now, $date_limit);
$date_diff = (int)$date_diff->format('%R%a');
if ($date_diff > 30) {
++$i;
continue;
}
return $i;
}
return null;
}
add_action('init', 'schedule_monthly_newsletter_CIPF');
add_action('CIPF_send_newsletter', 'send_newsletter_CIPF');
function extract_new_offers_CIPF(&$all_posts) {
Plgntls::debug_infos();
$offers = array();
foreach ($all_posts as $key => $page_id) {
$ending_offer = get_new_offer_CIPF($page_id);
if ($ending_offer !== null) {
$offers[$page_id] = $ending_offer;
unset($all_posts[$key]);
//$all_posts = array_values($all_posts);
}
}
return $offers;
}
function get_new_offer_CIPF($page_id) {
Plgntls::debug_infos();
$i = 1;
while ($i <= 3) {
$offer_output = get_field_init_CIPF('afficher_offre_'.$i, $page_id);
if ($offer_output === 'Masquer') {
++$i;
continue;
}
//return $i;
}
return null;
}
//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";
//}
function extract_new_partners_CIPF($all_posts) {
Plgntls::debug_infos();
}
function extract_partners_if_less_than_CIPF($all_posts, $total_count, $number) {
Plgntls::debug_infos();
}
?>

View File

@@ -374,7 +374,10 @@ function isset_acf_field_CIPF($acf_field, $acf_id) {
if (is_null($value)) {
return false;
}
else if (empty($value)) {
$value = reduce_array_CIPF($value);
if (empty($value)) {
return false;
}
else {
@@ -382,6 +385,43 @@ function isset_acf_field_CIPF($acf_field, $acf_id) {
}
}
/*
* this function will try to transform an array with a single value into this value, recursively
* it's not perfect but will do for this project
* ex :
* [] -> []
* [""] -> []
* ["test"] -> ["test"]
* ["test", ""] -> ["test"]
* ["", "test"] -> ["1":"test"]
* ["test", []] -> ["test"]
* ["test", [""]] -> ["test"]
* ["test1", ["test2"]] -> ["test1",["test2"]]
* ["test1", ["", "test2"]] -> ["test1",["1":"test2"]]
* ["test", ["", ""]] -> ["test"]
* [["test"], []] -> [["test"]]
* [[""], [""]] -> []
* [[], []] -> []
* [[], ["", ""]] -> []
* [[], ["", "test"]] -> ["1":["1":"test"]]
* [[], [], "test"] -> ["2":"test"]
*
*/
function reduce_array_CIPF($arr) {
if (!is_array($arr)) {
return $arr;
}
foreach ($arr as $key=>$elem) {
$result = reduce_array_CIPF($elem);
if (empty($result)) {
unset($arr[$key]);
}
else {
$arr[$key] = $result;
}
}
return $arr;
}