From b75f2718b8150e884582d7919cb0e226b620ae27 Mon Sep 17 00:00:00 2001 From: asus Date: Thu, 2 May 2024 18:06:54 +0200 Subject: [PATCH] wip newsletter i dont know how to detect if offer is new --- plugins/cipf_plugin/cipf_plugin.php | 6 +- .../cipf_plugin/php/_actions_newsletter.php | 240 +++++++++++------- plugins/cipf_plugin/php/_utils_acf_fields.php | 42 ++- 3 files changed, 197 insertions(+), 91 deletions(-) diff --git a/plugins/cipf_plugin/cipf_plugin.php b/plugins/cipf_plugin/cipf_plugin.php index 8d3b690..edeff9a 100644 --- a/plugins/cipf_plugin/cipf_plugin.php +++ b/plugins/cipf_plugin/cipf_plugin.php @@ -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'; diff --git a/plugins/cipf_plugin/php/_actions_newsletter.php b/plugins/cipf_plugin/php/_actions_newsletter.php index 6f4f4b7..bdb5be4 100644 --- a/plugins/cipf_plugin/php/_actions_newsletter.php +++ b/plugins/cipf_plugin/php/_actions_newsletter.php @@ -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); // [ => , => ] - $new_offers = extract_new_offers($all_posts); // [ => , => ] - $new_partners = extract_new_partners($all_posts); // [, ] + $all_posts = get_all_partner_posts(); +//error_log("all_posts: " . json_encode($all_posts)); + $ending_offers = extract_ending_offers_CIPF($all_posts); // [ => , => ] +//error_log("ending_offers: " . json_encode($ending_offers)); + $new_offers = extract_new_offers_CIPF($all_posts); // [ => , => ] +error_log("new_offers: " . json_encode($new_offers)); +/* + $new_partners = extract_new_partners_CIPF($all_posts); // [, ] $total = count($ending_offers) + count($new_offers) + count($new_partners); - $remaining_partners = extract_partners_if_less_than($all_posts, $total, 5); // [, ] + $remaining_partners = extract_partners_if_less_than_CIPF($all_posts, $total, 5); // [, ] ob_start(); ?> @@ -94,85 +138,107 @@ function compose_newsletter_body_CIPF() { $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(); +} + + + ?> diff --git a/plugins/cipf_plugin/php/_utils_acf_fields.php b/plugins/cipf_plugin/php/_utils_acf_fields.php index 8751b7c..d1c9614 100644 --- a/plugins/cipf_plugin/php/_utils_acf_fields.php +++ b/plugins/cipf_plugin/php/_utils_acf_fields.php @@ -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; +}