wip first strategy to overwrite the formbuilder post update is a dead end

This commit is contained in:
asus
2024-03-24 23:43:06 +01:00
parent 569f4524d3
commit 85e5a34e42
3 changed files with 350 additions and 16 deletions

View File

@@ -4,7 +4,7 @@ Plugin Name: fbpatch_plugin
Plugin URI:
Description: some patchs for the form_builder plugin's bugs
Author: hugogogo
Version: 0.1.0
Version: 0.1.2
Author URI:
*/

View File

@@ -21,16 +21,10 @@ class Fbpatch {
const NONCE = ['_name'=>'nonce_name', '_action'=>'action_name'];
const ADMIN_POST_PATCH_CHOICE = 'add_patches';
private static $_patches = [
'_name'=>'fbpatch_list_of_patches',
'calculations'=>['checked'=>true, 'title'=>'calculations title', 'description'=>'calculation description'],
'hide_show' =>['checked'=>true, 'title'=>'hide/show title', 'description'=>'hide/show description'],
'modals' =>['checked'=>false, 'title'=>'modals title', 'description'=>'modals description'],
'urls' =>['checked'=>false, 'title'=>'urls title', 'description'=>'urls description'],
];
//private static $_patches = ['_name'=>'fbpatch_list_of_patches', 'hide_show'];
//private static $_patches = ['_name'=>'fbpatch_list_of_patches'];
/*
* get path an url from plugin root
*
*/
public static function root_path() {
return plugin_dir_path(__DIR__);
}
@@ -39,6 +33,23 @@ class Fbpatch {
}
/*
* ---------------------------------------------------------------------------
* OPTIONS
* these functions are used to select which patch is applied
*
*/
private static $_patches = [
'_name'=>'fbpatch_list_of_patches',
'calculations'=>['checked'=>true, 'title'=>'calculations title', 'description'=>'calculation description'],
'hide_show' =>['checked'=>true, 'title'=>'hide/show title', 'description'=>'hide/show description'],
'modals' =>['checked'=>false, 'title'=>'modals title', 'description'=>'modals description'],
'urls' =>['checked'=>false, 'title'=>'urls title', 'description'=>'urls description'],
];
private static function set_option_patches() {
/*
* get the list of patches in option
@@ -129,6 +140,118 @@ class Fbpatch {
}
}
}
/*
*
* OPTIONS END
* ---------------------------------------------------------------------------
*/
/*
* ---------------------------------------------------------------------------
* HIDE SHOW
* to hide the chosen elements of the form without deleting the data in acf fields
*
*/
private static $_identification_fields = array("post_title", "ID", "_ajax_linking_nonce");
private static $_post_array_begin = array();
private static $_post_identification = array();
private static $_has_post_to_overwrite = false;
private static $_was_inserted_once = false;
public static function set_post_array($array) {
self::$_post_array_begin = $array;
self::$_post_identification = array();
self::$_has_post_to_overwrite = true;
self::$_was_inserted_once = false;
}
public static function set_post_identification($array) {
error_log("-in set_post_identification");
foreach(self::$_identification_fields as $field) {
if (!isset($array[$field])) {
continue;
}
self::$_post_identification[$field] = $array[$field];
}
error_log("_post_identification: " . json_encode(self::$_post_identification));
}
/*
* check if should insert post :
* only prevent insert if the post was already inserted with divi_form_builder hook
* so we must let the post be inserted a first time, then prevent it
* 1. checks if there is a post to overwrite
* 2. checks if it has the same identifications than the form_builder post
*
*/
public static function should_prevent_insert($array) {
error_log("-in should_prevent_insert");
$is_same = false;
if (self::$_has_post_to_overwrite === false) {
error_log("_has_post_to_overwrite === false");
self::$_post_identification = array();
self::$_post_array_begin = array();;
return false;
}
/*
* check if the post identifications matches the post to overwrite
*
*/
foreach(self::$_post_identification as $key => $value) {
if (!isset($array[$key])) {
error_log("!isset array[key]");
return false;
}
// some identifications are number, but might be in string format, hence '!=' instead of '!=='
if ($array[$key] != $value) {
error_log("array[key] != value");
return false;
}
}
/*
* the post identify to the post to overwrite
* so we should insert it a first time
* then prevent it to be inserted a second time
*
*/
if (self::$_was_inserted_once === false) {
error_log("_was_inserted_once === true");
self::$_was_inserted_once = true;
return false;
}
self::$_post_array_begin = array();;
self::$_post_identification = array();
self::$_has_post_to_overwrite = false;
self::$_was_inserted_once = false;
return true;
}
/*
"post_title":"test_hugo",
"ID":41586,
"_ajax_linking_nonce":"9e6b4bfea2",
*/
/*
*
* HIDE SHOW
* ---------------------------------------------------------------------------
*/
}

View File

@@ -28,23 +28,234 @@ if (!defined('ABSPATH')) {
*
*/
function form_partner_before_process($form_id, $post_array, $form_type) {
error_log("form_id: " . json_encode($form_id));
error_log("post_array: " . json_encode($post_array));
error_log("form_type: " . json_encode($form_type));
//error_log("post_array: " . json_encode($post_array));
Fbpatch::set_post_array($post_array);
}
add_action('df_before_process', __NAMESPACE__.'\form_partner_before_process', 10, 3);
/*
* 506 : ../../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/divi-form-builder/includes/DiviFormBuilder.php
*
*/
function form_partner_before_insert($form_id, $post_array) {
error_log("form_id: " . json_encode($form_id));
error_log("post_array: " . json_encode($post_array));
//error_log("--form_id : " . json_encode($form_id));
//error_log("post_array : " . json_encode($post_array));
Fbpatch::set_post_identification($post_array);
}
add_action('df_before_insert_post', __NAMESPACE__.'\form_partner_before_insert', 10, 2);
/*
* 4179 : ../../../../wordpress_docker/volumes/wp_volume/wp-includes/post.php
*
*/
function maybe_abort_insert_post($maybe_empty, $postarr) {
error_log("- in filter maybe empty, postarr: " . json_encode($postarr));
$prevent = Fbpatch::should_prevent_insert($postarr);
error_log("prevent: " . json_encode($prevent));
return false;
}
add_filter( 'wp_insert_post_empty_content', __NAMESPACE__.'\maybe_abort_insert_post', 10, 2);
/*
* 235 : ../../../../wordpress_docker/volumes/wp_volume/wp-includes/meta.php
* $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );
*
*/
function test_attachment_filter($data, $postarr, $unsanitized_postarr, $update) {
error_log("-- in test_attachment_filter");
error_log("data: " . json_encode($data));
error_log("postarr: " . json_encode($postarr));
error_log("unsanitized_postarr: " . json_encode($unsanitized_postarr));
error_log("update: " . json_encode($update));
}
//add_filter( 'wp_insert_attachment_data', __NAMESPACE__.'\test_attachment_filter', 10, 4);
/*
* 4462 : ../../../../wordpress_docker/volumes/wp_volume/wp-includes/post.php
* $data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr, $update );
*
*/
function test_post_filter($data, $postarr, $unsanitized_postarr, $update) {
error_log("-- in test_post_filter");
error_log("data: " . json_encode($data));
error_log("postarr: " . json_encode($postarr));
error_log("unsanitized_postarr: " . json_encode($unsanitized_postarr));
error_log("update: " . json_encode($update));
return $data;
}
//add_filter('wp_insert_post_data', __NAMESPACE__.'\test_post_filter', 10, 4);
/*
* 4558 : update_post_meta( $post_id, $field, $value );
* 2552 : ../../../../wordpress_docker/volumes/wp_volume/wp-includes/post.php
* function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' )
*
*/
/*
* 4845 : ../../../../wordpress_docker/volumes/wp_volume/wp-includes/functions.php
* function wp_parse_args( $args, $defaults = array() )
*
*/
/*
* strategy 2 :
*
* 1. first collect the original post to see the fields that should be empty :
* - 305 : ../../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/divi-form-builder/includes/DiviFormBuilder.php
* - add_action('df_before_process', __NAMESPACE__.'\form_partner_before_process', 10, 3);
* 2. then hook for each entries and check if should abort the updating of the meta data
* - 235 : ../../../../wordpress_docker/volumes/wp_volume/wp-includes/meta.php
* - $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );
*
*
* strategy 1 : ABORTED -> we cannot prevent divi-form-builder to insert the post itself, since it needs the id in returns, and preventing returns an id of 0
*
* in divi form builder : ../../../../wordpress_docker/volumes/wp_volume/wp-content/plugins/divi-form-builder/includes/DiviFormBuilder.php
* 1. first collect the original post to see the fields that should be empty :
* - 305 : add_action('df_before_process', __NAMESPACE__.'\form_partner_before_process', 10, 3);
* 2. then insert the post before divi form builder does it :
* - 506 : do_action( 'df_before_insert_post', $form_id, $post_array );
* in wp : ../../../../wordpress_docker/volumes/wp_volume/wp-includes/post.php
* 3. in wp, use a filter to check that the post is inserted by divi form builder and abort
* - 4181 : apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr )
*
*/
/*
"post_title":"test_hugo",
"ID":41586,
"_ajax_linking_nonce":"9e6b4bfea2",
before process :
{
"post_status":"publish",
"field_title":["Nom commercial","Votre site Internet","Dans quelle rubrique devez-vous para\u00eetre ? ","L\\'accroche en t\u00eate de page","Pr\u00e9sentez-vous","Votre logo ","Image principale","Voulez-vous afficher cette offre ?","Dur\u00e9e de l\\'offre","Validit\u00e9 de l\\'offre","Titre","Texte de l\\'offre","Image (facultatif)","Voulez-vous afficher cette offre ?","Dur\u00e9e de l\\'offre","Validit\u00e9 de l\\'offre","Titre","Texte de l\\'offre","Image (facultatif)","Voulez-vous afficher cette offre ?","Dur\u00e9e de l\\'offre","Validit\u00e9 de l\\'offre","Titre","Texte de l\\'offre","Image (facultatif)","slug"],
"post_title":"test_hugo",
"meta_input":[
"de_fb_url_partenaire",
"de_fb_logo_partenaire",
"de_fb_afficher_offre_1",
"de_fb_duree_offre_1",
"de_fb_fin_offre_1",
"de_fb_offre_1_titre",
"de_fb_offre_1_txt",
"de_fb_offre_1_image",
"de_fb_afficher_offre_2",
"de_fb_duree_offre_2",
"de_fb_fin_offre_2",
"de_fb_offre_2_titre",
"de_fb_offre_2_txt",
"de_fb_offre_2_image",
"de_fb_afficher_offre_3",
"de_fb_duree_offre_3",
"de_fb_fin_offre_3",
"de_fb_offre_3_titre",
"de_fb_offre_3_txt",
"de_fb_offre_3_image"
],
"url_partenaire":"",
"tax_input":["de_fb_category"],
"category":"autres",
"post_excerpt":"accroche",
"post_content":"presentation",
"_ajax_linking_nonce":"9e6b4bfea2",
"logo_partenaire":"41585",
"post_thumbnail":"41584",
"afficher_offre_1":"Masquer",
"offre_1_image":"41587",
"afficher_offre_2":"Afficher",
"duree_offre_2":"Permanente",
"offre_2_titre":"titre 2",
"offre_2_txt":"offre 2",
"offre_2_image":"41588",
"afficher_offre_3":"Afficher",
"duree_offre_3":"Permanente",
"offre_3_titre":"titre 3",
"offre_3_txt":"offre 3",
"offre_3_image":"41589",
"post_name":"test_hugo",
"ID":"41586",
"form_type_confirm":""
}
in filter maybe empty
{
"post_author":24,
"post_content":"presentation",
"post_content_filtered":"",
"post_title":"test_hugo",
"post_excerpt":"accroche",
"post_status":"publish",
"post_type":"post",
"comment_status":"",
"ping_status":"",
"post_password":"",
"to_ping":"",
"pinged":"",
"post_parent":0,
"menu_order":0,
"guid":"",
"import_id":0,
"context":"",
"post_date":"2024-03-22 22:59:57",
"post_date_gmt":"",
"field_title":["Nom commercial","Votre site Internet","Dans quelle rubrique devez-vous para\u00eetre ? ","L\\'accroche en t\u00eate de page","Pr\u00e9sentez-vous","Votre logo ","Image principale","Voulez-vous afficher cette offre ?","Dur\u00e9e de l\\'offre","Validit\u00e9 de l\\'offre","Titre","Texte de l\\'offre","Image (facultatif)","Voulez-vous afficher cette offre ?","Dur\u00e9e de l\\'offre","Validit\u00e9 de l\\'offre","Titre","Texte de l\\'offre","Image (facultatif)","Voulez-vous afficher cette offre ?","Dur\u00e9e de l\\'offre","Validit\u00e9 de l\\'offre","Titre","Texte de l\\'offre","Image (facultatif)","slug"],
"meta_input":{
"url_partenaire":"",
"_url_partenaire":"field_65cb83096d020",
"logo_partenaire":"41585",
"_logo_partenaire":"field_65cb79150ac4f",
"afficher_offre_1":"Masquer",
"_afficher_offre_1":"field_65d8cf09395a5",
"duree_offre_1":[],
"fin_offre_1":[],
"offre_1_titre":[],
"offre_1_txt":[],
"offre_1_image":"41587",
"_offre_1_image":"field_65d8bc5d5bf8b",
"afficher_offre_2":"Afficher",
"_afficher_offre_2":"field_65e20fb8785ba",
"duree_offre_2":"Permanente",
"_duree_offre_2":"field_65f3fc577fec4",
"fin_offre_2":[],
"offre_2_titre":"titre 2",
"_offre_2_titre":"field_65d8bc201740b",
"offre_2_txt":"offre 2",
"_offre_2_txt":"field_65d8bc4c5bf89",
"offre_2_image":"41588",
"_offre_2_image":"field_65d8bc6e5bf8c",
"afficher_offre_3":"Afficher",
"_afficher_offre_3":"field_65d8cf48395a6",
"duree_offre_3":"Permanente",
"_duree_offre_3":"field_65f3ff78c7ffd",
"fin_offre_3":[],
"offre_3_titre":"titre 3",
"_offre_3_titre":"field_65d8bc2e644ab",
"offre_3_txt":"offre 3",
"_offre_3_txt":"field_65d8bc545bf8a",
"offre_3_image":"41589",
"_offre_3_image":"field_65d8bc8c5bf8e"
},
"tax_input":{"category":[1]},
"_ajax_linking_nonce":"9e6b4bfea2",
"post_thumbnail":"41584",
"post_name":"test_hugo",
"ID":41586,
"form_type_confirm":"",
"filter":"db"
}
*/