creation of the repo fbpatch
This commit is contained in:
316
php/fbpatch_class.php
Normal file
316
php/fbpatch_class.php
Normal file
@@ -0,0 +1,316 @@
|
||||
<?php
|
||||
namespace FBPATCH;
|
||||
|
||||
/*
|
||||
* it means someone outside wp is accessing the file, in this case kill it.
|
||||
*/
|
||||
if (!defined('ABSPATH')) {
|
||||
die('You can not access this file!');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
class Fbpatch {
|
||||
const SLUG_TOOGLE_ADMIN_MENU = ['_name'=>'toogle_admin_menu_url_fbpatch', 'toggle'=>'toggle', 'show'=>'show', 'hide'=>'hide'];
|
||||
const OPTION_TOGGLE_MENU = ['_name'=>'toggle_admin_menu_option_fbpatch', 'show'=>'show', 'hide'=>'hide'];
|
||||
const NONCE = ['_name'=>'nonce_name', '_action'=>'action_name'];
|
||||
const ADMIN_POST_PATCH_CHOICE = 'add_patches';
|
||||
const ACF_DATE_CLASS = 'dfb_date';
|
||||
|
||||
/*
|
||||
* get path an url from plugin root
|
||||
*
|
||||
*/
|
||||
public static function root_path() {
|
||||
return plugin_dir_path(__DIR__);
|
||||
}
|
||||
public static function root_url() {
|
||||
return plugin_dir_url(__DIR__);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* ---------------------------------------------------------------------------
|
||||
* OPTIONS
|
||||
* these functions are used to select which patch is applied
|
||||
*
|
||||
*/
|
||||
|
||||
private static $_patches = [
|
||||
'_name'=>'fbpatch_list_of_patches',
|
||||
'dates' => ['checked'=>true, 'title'=>'dates', 'description'=>"gerer des dates pour acf dans n'importe quels formats (fonctionne bien pour divi, pas certain pour d'autres plugins)"],
|
||||
'calculations'=> ['checked'=>true, 'title'=>'calculations', 'description'=>"afficher le total des calculs dès l'ouverture des formulaires"],
|
||||
'hide_show' => ['checked'=>true, 'title'=>'masquer les offres', 'description'=>"permettre de masquer les offres en editant un formulaire, sans les supprimer"],
|
||||
'modals' => ['checked'=>false, 'title'=>'modals', 'description'=>"permettre plusieurs modals sur une meme page"],
|
||||
'urls' => ['checked'=>false, 'title'=>'urls', 'description'=>"permettre de rentrer des urls sans 'http://'"],
|
||||
];
|
||||
|
||||
private static function set_option_patches() {
|
||||
/*
|
||||
* get the list of patches in option
|
||||
* create option if needed
|
||||
*
|
||||
*/
|
||||
$raw_patches_option = get_option(self::$_patches['_name']);
|
||||
if (false === $raw_patches_option) {
|
||||
add_option(self::$_patches['_name'], '', '', 'no');
|
||||
}
|
||||
$patches_option = unserialize($raw_patches_option);
|
||||
if (empty($patches_option)) {
|
||||
$patches_option = array();
|
||||
}
|
||||
|
||||
/*
|
||||
* if the option miss patches, add them
|
||||
*
|
||||
*/
|
||||
foreach (self::$_patches as $patch => $data) {
|
||||
if ($patch === '_name') {
|
||||
continue;
|
||||
}
|
||||
if (isset($patches_option[$patch])) {
|
||||
// updates the title and the description
|
||||
$patches_option[$patch]['title'] = $data['title'];
|
||||
$patches_option[$patch]['description'] = $data['description'];
|
||||
}
|
||||
else {
|
||||
// add the option
|
||||
$patches_option[$patch] = $data;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* if the option has additional patches, delete them
|
||||
*
|
||||
*/
|
||||
foreach ($patches_option as $patch => $data) {
|
||||
if (isset(self::$_patches[$patch])) {
|
||||
continue;
|
||||
}
|
||||
unset($patches_option[$patch]);
|
||||
}
|
||||
|
||||
/*
|
||||
* change the option list with the update patches
|
||||
*
|
||||
*/
|
||||
ksort($patches_option);
|
||||
$serialize_patches_option = serialize($patches_option);
|
||||
update_option(self::$_patches['_name'], $serialize_patches_option);
|
||||
}
|
||||
public static function get_patches() {
|
||||
self::set_option_patches();
|
||||
$patches = get_option(self::$_patches['_name']);
|
||||
return unserialize($patches);
|
||||
}
|
||||
public static function set_patches($patches_on) {
|
||||
/*
|
||||
* loop through the option list and update occording to the received list
|
||||
*
|
||||
*/
|
||||
$raw_patches = get_option(self::$_patches['_name']);
|
||||
$patches_option = unserialize($raw_patches);
|
||||
foreach($patches_option as $patch => $data) {
|
||||
if (in_array($patch, $patches_on)) {
|
||||
$patches_option[$patch]['checked'] = true;
|
||||
}
|
||||
else {
|
||||
$patches_option[$patch]['checked'] = false;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* change the option list with the update patches
|
||||
*
|
||||
*/
|
||||
ksort($patches_option);
|
||||
$serialize_patches_option = serialize($patches_option);
|
||||
update_option(self::$_patches['_name'], $serialize_patches_option);
|
||||
}
|
||||
|
||||
/*
|
||||
* this function will include the files of the different patches if they are set in the options
|
||||
*
|
||||
*/
|
||||
public static function init_hook() {
|
||||
$patches = Fbpatch::get_patches();
|
||||
foreach($patches as $patch => $data) {
|
||||
if ($data['checked'] === true) {
|
||||
include_once(self::root_path() . '/php/patches/'.$patch.'.php');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* ---------------------------------------------------------------------------
|
||||
* HIDE SHOW
|
||||
* to hide the chosen elements of the form without deleting the data in acf fields
|
||||
*
|
||||
*/
|
||||
|
||||
private static $_post_id = 0;
|
||||
private static $_has_elements_to_skip = false;
|
||||
private static $_skip_elements = array();
|
||||
|
||||
public static function is_post_id($id) {
|
||||
return self::$_post_id == $id;
|
||||
}
|
||||
public static function is_to_skip($key) {
|
||||
return in_array($key, self::$_skip_elements);
|
||||
}
|
||||
|
||||
/*
|
||||
* create an array of the elements to forget
|
||||
*
|
||||
*/
|
||||
public static function set_post_elements_to_forget($id, $is_to_skip, $skip_array) {
|
||||
self::$_post_id = $id;
|
||||
self::$_has_elements_to_skip = $is_to_skip;
|
||||
self::$_skip_elements = $skip_array;
|
||||
}
|
||||
|
||||
/*
|
||||
* if there is elements to skip, add the filter
|
||||
*
|
||||
*/
|
||||
public static function init_skip_hook() {
|
||||
if (true === self::$_has_elements_to_skip) {
|
||||
add_filter("update_post_metadata", __NAMESPACE__.'\filter_elements_to_skip', 10, 5);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* after insertion finished, removes filter and reset variables
|
||||
*
|
||||
*/
|
||||
public static function end_skip_hook() {
|
||||
if (true === self::$_has_elements_to_skip) {
|
||||
remove_filter("update_post_metadata", __NAMESPACE__.'\filter_elements_to_skip');
|
||||
}
|
||||
self::$_post_id = 0;
|
||||
self::$_has_elements_to_skip = false;
|
||||
self::$_skip_elements = array();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* ---------------------------------------------------------------------------
|
||||
* DATES
|
||||
* stores the real dates in the database
|
||||
*
|
||||
*/
|
||||
private static $dates_option = 'dfb_acf_dates_option';
|
||||
|
||||
/*
|
||||
* stores acf dates by post_id
|
||||
*
|
||||
*/
|
||||
public static function update_acf_date($post_id, $acf_key, $acf_date) {
|
||||
if (empty($acf_date)) {
|
||||
return;
|
||||
}
|
||||
$dates = get_option(self::$dates_option);
|
||||
|
||||
/*
|
||||
* if option does not exists, add it with its first value
|
||||
*
|
||||
*/
|
||||
if ($dates === false) {
|
||||
$dates[$post_id] = array($acf_key => $acf_date);
|
||||
add_option(self::$dates_option, $dates, '', false);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* if the option does not contains key for this post_id, add it
|
||||
*
|
||||
*/
|
||||
if (!isset($dates[$post_id])) {
|
||||
$dates[$post_id] = array($acf_key => $acf_date);
|
||||
update_option(self::$dates_option, $dates, false);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* if the post_id dont contains this acf field yet just add it
|
||||
*
|
||||
*/
|
||||
if (!isset($dates[$post_id][$acf_key])) {
|
||||
$dates[$post_id][$acf_key] = $acf_date;
|
||||
update_option(self::$dates_option, $dates, false);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* if the acf_key already exists,
|
||||
* only update option if it is different
|
||||
*
|
||||
*/
|
||||
if ($dates[$post_id][$acf_key] !== $acf_date) {
|
||||
$dates[$post_id][$acf_key] = $acf_date;
|
||||
update_option(self::$dates_option, $dates, false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* if acf_date exists for this acf field, use it instead of the value
|
||||
* return the date in acf format 'Ymd' or false if doesnt exists
|
||||
*
|
||||
*/
|
||||
public static function get_acf_date($post_id, $acf_key) {
|
||||
$dates = get_option(self::$dates_option);
|
||||
|
||||
if ($dates === false) {
|
||||
return false;
|
||||
}
|
||||
if (!isset($dates[$post_id])) {
|
||||
return false;
|
||||
}
|
||||
if (!isset($dates[$post_id][$acf_key])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* returns the date, or false if is empty
|
||||
*
|
||||
*/
|
||||
$date = $dates[$post_id][$acf_key];
|
||||
if (empty($date)) {
|
||||
return false;
|
||||
}
|
||||
return $date;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user