wip newsletter i dont know how to detect if offer is new
This commit is contained in:
@@ -4,7 +4,7 @@ Plugin Name: hggg_cipf
|
|||||||
Plugin URI:
|
Plugin URI:
|
||||||
Description:
|
Description:
|
||||||
Author: hugogogo
|
Author: hugogogo
|
||||||
Version: 0.5.13
|
Version: 0.5.15
|
||||||
Author URI:
|
Author URI:
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -100,8 +100,8 @@ class Cipf {
|
|||||||
const ACF_PARTNER_OFFER_3_OUTPUT = ['_name'=>'afficher_offre_3', 'show'=>'Afficher', 'hide'=>'Masquer'];
|
const ACF_PARTNER_OFFER_3_OUTPUT = ['_name'=>'afficher_offre_3', 'show'=>'Afficher', 'hide'=>'Masquer'];
|
||||||
const ACF_READONLY_CLASS = 'readonly_acf';
|
const ACF_READONLY_CLASS = 'readonly_acf';
|
||||||
const ACF_DATE_FORMAT = 'Ymd';
|
const ACF_DATE_FORMAT = 'Ymd';
|
||||||
const ACF_NEWSLETTER_RECEIVE = ['name' => 'recevoir_la_newsletter', 'true' => 'recevoir'];
|
const ACF_NEWSLETTER_RECEIVE = ['_name' => 'recevoir_la_newsletter', 'true' => 'recevoir'];
|
||||||
const ACF_NEWSLETTER_ID = ['name' => 'identifiant_newsletter'];
|
const ACF_NEWSLETTER_ID = ['_name' => 'identifiant_newsletter'];
|
||||||
|
|
||||||
// META
|
// META
|
||||||
const META_PAYEMENT_STATUS = 'cipf_payement_status';
|
const META_PAYEMENT_STATUS = 'cipf_payement_status';
|
||||||
|
|||||||
@@ -12,11 +12,58 @@ if (!defined('ABSPATH')) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
* add a monthly interval to the schedules
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
function remove_subscriber_CIPF($id) {
|
function cron_add_monthly_CIPF($schedules) {
|
||||||
Plgntls::debug_infos();
|
// 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() {
|
function get_subscribers_emails_CIPF() {
|
||||||
Plgntls::debug_infos();
|
Plgntls::debug_infos();
|
||||||
$acf_newsletter_receive = Cipf::ACF_NEWSLETTER_RECEIVE;
|
$acf_newsletter_receive = Cipf::ACF_NEWSLETTER_RECEIVE;
|
||||||
@@ -29,7 +76,7 @@ function get_subscribers_emails_CIPF() {
|
|||||||
$args = array(
|
$args = array(
|
||||||
'meta_query' => array(
|
'meta_query' => array(
|
||||||
array(
|
array(
|
||||||
'key' => $acf_newsletter_receive['name'],
|
'key' => $acf_newsletter_receive['_name'],
|
||||||
'value' => $acf_newsletter_receive['true'],
|
'value' => $acf_newsletter_receive['true'],
|
||||||
'compare' => 'LIKE',
|
'compare' => 'LIKE',
|
||||||
),
|
),
|
||||||
@@ -53,31 +100,28 @@ function get_subscribers_emails_CIPF() {
|
|||||||
return $users_string;
|
return $users_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_all_partners_posts() {
|
/*
|
||||||
Plgntls::debug_infos();
|
*/
|
||||||
}
|
function remove_subscriber_CIPF($id) {
|
||||||
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();
|
Plgntls::debug_infos();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function compose_newsletter_body_CIPF() {
|
function compose_newsletter_body_CIPF() {
|
||||||
Plgntls::debug_infos();
|
Plgntls::debug_infos();
|
||||||
|
|
||||||
$all_posts = get_all_partners_posts();
|
$all_posts = get_all_partner_posts();
|
||||||
$ending_offers = extract_ending_offers($all_posts); // [<post_id> => <offer_number>, <post_id> => <offer_number>]
|
//error_log("all_posts: " . json_encode($all_posts));
|
||||||
$new_offers = extract_new_offers($all_posts); // [<post_id> => <offer_number>, <post_id> => <offer_number>]
|
$ending_offers = extract_ending_offers_CIPF($all_posts); // [<post_id> => <offer_number>, <post_id> => <offer_number>]
|
||||||
$new_partners = extract_new_partners($all_posts); // [<post_id>, <post_id>]
|
//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);
|
$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();
|
ob_start();
|
||||||
?>
|
?>
|
||||||
@@ -94,85 +138,107 @@ function compose_newsletter_body_CIPF() {
|
|||||||
<?php
|
<?php
|
||||||
$body_html = ob_get_clean();
|
$body_html = ob_get_clean();
|
||||||
return $body_html;
|
return $body_html;
|
||||||
|
*/
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
function send_newsletter_CIPF() {
|
function extract_ending_offers_CIPF(&$all_posts) {
|
||||||
Plgntls::debug_infos();
|
Plgntls::debug_infos();
|
||||||
|
$offers = array();
|
||||||
//$email_body = compose_newsletter_body_CIPF();
|
foreach ($all_posts as $key => $page_id) {
|
||||||
|
$ending_offer = get_ending_offer_CIPF($page_id);
|
||||||
$emails = get_subscribers_emails_CIPF();
|
if ($ending_offer !== null) {
|
||||||
error_log("emails:" . json_encode($emails));
|
$offers[$page_id] = $ending_offer;
|
||||||
|
unset($all_posts[$key]);
|
||||||
// foreach ($emails as $email) {
|
//$all_posts = array_values($all_posts);
|
||||||
//
|
}
|
||||||
// }
|
|
||||||
}
|
|
||||||
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');
|
|
||||||
}
|
}
|
||||||
|
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) {
|
function extract_new_partners_CIPF($all_posts) {
|
||||||
// Plgntls::debug_infos();
|
Plgntls::debug_infos();
|
||||||
//error_log("inside newsletter_filter_content_CIPF");
|
}
|
||||||
////error_log("message: " . json_encode($message));
|
function extract_partners_if_less_than_CIPF($all_posts, $total_count, $number) {
|
||||||
////error_log("email: " . json_encode($email));
|
Plgntls::debug_infos();
|
||||||
////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";
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -374,7 +374,10 @@ function isset_acf_field_CIPF($acf_field, $acf_id) {
|
|||||||
if (is_null($value)) {
|
if (is_null($value)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else if (empty($value)) {
|
|
||||||
|
$value = reduce_array_CIPF($value);
|
||||||
|
|
||||||
|
if (empty($value)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else {
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user