245 lines
5.2 KiB
PHP
245 lines
5.2 KiB
PHP
<?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!');
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
* add a monthly interval to the schedules
|
|
*
|
|
*/
|
|
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;
|
|
$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 remove_subscriber_CIPF($id) {
|
|
Plgntls::debug_infos();
|
|
}
|
|
|
|
|
|
|
|
|
|
function compose_newsletter_body_CIPF() {
|
|
Plgntls::debug_infos();
|
|
|
|
$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_CIPF($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;
|
|
*/
|
|
return '';
|
|
}
|
|
|
|
function extract_ending_offers_CIPF(&$all_posts) {
|
|
Plgntls::debug_infos();
|
|
$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;
|
|
}
|
|
|
|
|
|
|
|
|
|
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 extract_new_partners_CIPF($all_posts) {
|
|
Plgntls::debug_infos();
|
|
}
|
|
function extract_partners_if_less_than_CIPF($all_posts, $total_count, $number) {
|
|
Plgntls::debug_infos();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|