creation of the repo fbpatch

This commit is contained in:
asus
2024-07-17 15:30:32 +02:00
parent 1a071bf16f
commit 551dd7eee4
16 changed files with 1265 additions and 0 deletions

38
menu/admin_menu.php Normal file
View File

@@ -0,0 +1,38 @@
<?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!');
}
/*
* creates the plugin menu
*
*/
function plugin_menu() {
$menu_page_title = 'fbpatch';
$menu_title = 'fbpatch';
$menu_capability = 'manage_options';
$menu_slug = 'fbpatch-plugin';
$menu_callback = __NAMESPACE__.'\plugin_content';
\FBPATCH\toggle_menu($menu_page_title, $menu_title, $menu_capability, $menu_slug, $menu_callback);
}
add_action('admin_menu', __NAMESPACE__.'\plugin_menu');
?>

108
menu/admin_menu_toggle.php Normal file
View File

@@ -0,0 +1,108 @@
<?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!');
}
function toggle_menu($menu_page_title, $menu_title, $menu_capability, $menu_slug, $menu_callback) {
$toggle_menu = Fbpatch::OPTION_TOGGLE_MENU;
if (false === get_option($toggle_menu['_name'])) {
add_option($toggle_menu['_name'], $toggle_menu['hide'], '', 'no');
}
$toggle = get_option($toggle_menu['_name']);
if ($toggle === $toggle_menu['hide']) {
remove_menu_page($menu_slug);
}
else if ($toggle === $toggle_menu['show']) {
add_menu_page($menu_page_title, $menu_title, $menu_capability, $menu_slug, $menu_callback);
}
}
/*
* add link under the plugin in the plugins admin page
*
*/
function add_link_to_plugin($links) {
$slug_toggle = Fbpatch::SLUG_TOOGLE_ADMIN_MENU;
$toggle_menu = Fbpatch::OPTION_TOGGLE_MENU;
$toggle = get_option($toggle_menu['_name']);
if ($toggle === $toggle_menu['hide']) {
$links[] = '<a href="/'.$slug_toggle['_name'].'?'.$slug_toggle['toggle'].'='.$slug_toggle['show'].'">show menu</a>';
}
else if ($toggle === $toggle_menu['show']) {
$links[] = '<a href="/'.$slug_toggle['_name'].'?'.$slug_toggle['toggle'].'='.$slug_toggle['hide'].'">hide menu</a>';
}
return $links;
}
add_filter('plugin_action_links_fbpatch/fbpatch.php', __NAMESPACE__.'\add_link_to_plugin');
/*
* handle the toggle menu when url is reached
*
*/
function toggle_plugin_menu() {
$slug_toggle = Fbpatch::SLUG_TOOGLE_ADMIN_MENU;
$toggle_menu = Fbpatch::OPTION_TOGGLE_MENU;
global $wp;
$current_slug = $wp->request;
if ($current_slug !== $slug_toggle['_name']) {
return;
}
$show = null;
if (!isset($_GET)) {
$show = null;
}
else if (empty($_GET)) {
$show = null;
}
if (!isset($_GET[$slug_toggle['toggle']])) {
$show = null;
}
else if ($_GET[$slug_toggle['toggle']] === $slug_toggle['show']) {
$show = true;
}
else if ($_GET[$slug_toggle['toggle']] === $slug_toggle['hide']) {
$show = false;
}
if ($show === true) {
update_option($toggle_menu['_name'], $toggle_menu['show']);
}
else if ($show === false) {
update_option($toggle_menu['_name'], $toggle_menu['hide']);
}
$plugins_menu_url = admin_url('plugins.php');
wp_redirect($plugins_menu_url, 301);
exit;
}
add_action('template_redirect', __NAMESPACE__.'\toggle_plugin_menu');
?>

104
menu/menu_content.php Normal file
View File

@@ -0,0 +1,104 @@
<?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!');
}
/*
* the construction of the admin menu page
*
*/
function plugin_content() {
$patches = Fbpatch::get_patches();
$nonce = Fbpatch::NONCE;
$admin_post_patches = Fbpatch::ADMIN_POST_PATCH_CHOICE;
ob_start();
include(Fbpatch::root_path() . '/html/menu.html');
$html = ob_get_clean();
echo $html;
}
/*
* use this hook 'admin_post_{$action}' to receive a form post
* https://developer.wordpress.org/reference/hooks/admin_post_action/
*
* add the url to the action atrtibute of form, and the value of the action in an hidden input
* <form method="POST" action="<?php echo admin_url( 'admin-post.php' ); ?>">
* <input type="hidden" name="action" value="<?php echo $admin_post_patches; ?>">
*
*/
function patches_choice() {
$nonce = Fbpatch::NONCE;
if (!isset($_POST[$nonce['_name']])) {
\FBPATCH\redirect_menu_referer($_POST);
exit;
}
if (!wp_verify_nonce($_POST[$nonce['_name']], $nonce['_action'])) {
\FBPATCH\redirect_menu_referer($_POST);
exit;
}
/*
*
*
[24-Mar-2024 12:24:08 UTC] -> _POST {
"action":"add_patches",
"nonce_name":"7eeb560dc0",
"_wp_http_referer":"\/wp-admin\/admin.php?page=fbpatch-plugin",
"hide_show":"on"
}
*/
$pathes_on = array();
foreach($_POST as $key => $value) {
if ($value !== 'on') {
continue;
}
$pathes_on[] = $key;
}
Fbpatch::set_patches($pathes_on);
\FBPATCH\redirect_menu_referer($_POST);
}
add_action('admin_post_'.Fbpatch::ADMIN_POST_PATCH_CHOICE, __NAMESPACE__.'\patches_choice');
function redirect_menu_referer($post) {
if (!isset($post)) {
wp_redirect(admin_url(), 301);
exit;
}
if (is_null($post)) {
wp_redirect(admin_url(), 301);
exit;
}
if (empty($post)) {
wp_redirect(admin_url(), 301);
exit;
}
if (!isset($post['_wp_http_referer'])) {
wp_redirect(admin_url(), 301);
exit;
}
wp_redirect(home_url($post['_wp_http_referer']), 301);
exit;
}
?>