Files
2024_WEBSITE_fipf/plugins/fbpatch/php/fbpatch_class.php

140 lines
3.5 KiB
PHP

<?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';
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'];
public static function root_path() {
return plugin_dir_path(__DIR__);
}
public static function root_url() {
return plugin_dir_url(__DIR__);
}
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']);
}
$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])) {
continue;
}
$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');
}
}
}
}
?>