Skip to content

Instantly share code, notes, and snippets.

@gtamborero
Last active February 22, 2022 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gtamborero/a8a2a4b34912cd46c1b4afc53224104f to your computer and use it in GitHub Desktop.
Save gtamborero/a8a2a4b34912cd46c1b4afc53224104f to your computer and use it in GitHub Desktop.
ACF Backend Notifications on Change (WordPress) - When a user makes changes from the Back End, we create a: post notification or something else
<?php
// What is interesting about this plugin is that it does a deep compare between the post and the updated post
// and get the changes inside ACF,
// so you will know if an admin or someone has changed an image, a field, a repeater, etc. and send a mail, a post, or whatever
// INSTALLATION:
// Create inside /plugins/ a folder named /backend-change-notifications
// Paste inside /backend-change-notifications this backend-change-notifications.php file
/**
* The plugin bootstrap file
*
* @link iproject.cat
* @since 1.0.0
* @package iProject - Change Backend Notifications
*
* @wordpress-plugin
* Plugin Name: iProject - ACF Backend Notifications on Change
* Plugin URI: https://iproject.cat
* Description: When a user makes changes from the Back End, we create a notification for users.
* Version: 1.0.0
* Author: Iproject.cat
* Author URI: iproject.cat
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: change-backend-notifications
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
define( 'Change_Backend_Notifications', '1.0.0' );
// PRE-GET DATA SAVED ON FIELDS
add_action('acf/save_post', 'my_acf_save_post', 5);
function my_acf_save_post( $post_id ) {
if (substr( $post_id, 0, 5 ) !== "user_") return; // If the user is not being modified do nothing
$prev_values = get_fields( $post_id );
$GLOBALS['prev_values'] = $prev_values;
}
// GET END DATA SAVED ON FIELDS
add_action('acf/save_post', 'my_acf_save_post_after');
function my_acf_save_post_after( $post_id ) {
if (substr( $post_id, 0, 5 ) !== "user_") return; // If the user is not being modified do nothing
$end_values = get_fields( $post_id );
// If the user is being created do not send notifications
error_log("user initial value:");
error_log(print_r($GLOBALS['prev_values'],TRUE));
if (empty($GLOBALS['prev_values'])){
error_log("there is nothing so is a new user");
return;
}
$cambios = compare_multi_Arrays($end_values, $GLOBALS['prev_values']);
error_log(print_r($cambios,TRUE));
$userId = str_replace("user_", "", $post_id);
if (empty($cambios)) return;
$camposModificados = "";
foreach ($cambios as $key => $cambio){
$objeto = get_field_object($key, 'user_' . $userId );
$camposModificados .= $objeto['label'] . "<br>";
}
error_log(print_r($camposModificados,TRUE));
$cambios = print_r($cambios,TRUE);
if (strpos($cambios, 'less') !== false) {
$camposModificados .= "Repeater fields have been deleted";
}
if (strpos($cambios, 'more') !== false) {
$camposModificados .= "Repeater fields have been added";
}
publicarNotificacionBackend($userId, $camposModificados);
}
// Stackoveflow compare array -> https://stackoverflow.com/questions/7389176/compare-multidimensional-arrays-in-php
// There are commented changes to the original function
function compare_multi_Arrays($array1, $array2){
$result = array();
foreach($array1 as $k => $v) {
if(is_array($v) && isset($array2[$k]) && is_array($array2[$k])){
$sub_result = compare_multi_Arrays($v, $array2[$k]);
//merge results
foreach(array_keys($sub_result) as $key){
if(!empty($sub_result[$key])){
$result[$key] = array_merge_recursive($result[$key],array($k => $sub_result[$key]));
}
}
}else{
if(isset($array2[$k])){
if($v !== $array2[$k]){
// clean response array to send data to client
//$result["diff"][$k] = array("from"=>$v,"to"=>$array2[$k]);
//$result[$k] = array($array2[$k]);
$result[$k] = "";
}
}else{
$result["more"][$k] = $v;
}
}
}
foreach($array2 as $k => $v) {
if(!isset($array1[$k])){
$result["less"][$k] = $v;
}
}
return $result;
}
// This function creates a wordpress post used as notification
function publicarNotificacionBackend($userId, $mensaje){
$user = get_user_by('id', $userId);
$mensajeFinal = "<strong>Modified fields of your profile:</strong> \n" . $mensaje;
// Post Notification creation
$new_post = array(
'post_title' => $user->user_firstname . ", has modified information in your profile",
'post_content' => $mensajeFinal,
'post_status' => 'publish',
'post_type' => 'post',
);
$postId = wp_insert_post($new_post);
// UPDATE ACF data within each user
//update_field("enviar_a", "usuarios", $postId);
//update_field("usuarios_a_enviar", array($user->ID), $postId); // 1 es admin guillermo 2 es tmagana
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment