wip adding fbpatch plugin
This commit is contained in:
29
plugins/fbpatch/fbpatch.php
Normal file
29
plugins/fbpatch/fbpatch.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/*
|
||||
Plugin Name: fbpatch_plugin
|
||||
Plugin URI:
|
||||
Description: some patchs for the form_builder plugin's bugs
|
||||
Author: hugogogo
|
||||
Version: 0.1.0
|
||||
Author URI:
|
||||
*/
|
||||
|
||||
/*
|
||||
* it means someone outside wp is accessing the file, in this case kill it.
|
||||
*/
|
||||
if (!defined('ABSPATH')) {
|
||||
die('You can not access this file!');
|
||||
}
|
||||
|
||||
|
||||
|
||||
include_once(plugin_dir_path(__FILE__) . '/php/fbpatch_class.php');
|
||||
include_once(plugin_dir_path(__FILE__) . '/php/calculations.php');
|
||||
|
||||
include_once(plugin_dir_path(__FILE__) . '/menu/admin_menu.php');
|
||||
include_once(plugin_dir_path(__FILE__) . '/menu/admin_menu_toggle.php');
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
70
plugins/fbpatch/js/calculations.js
Normal file
70
plugins/fbpatch/js/calculations.js
Normal file
@@ -0,0 +1,70 @@
|
||||
|
||||
function patch_form_calculation_CIPF() {
|
||||
let form_calculation = document.querySelector('form.fb_form.multistep');
|
||||
if (form_calculation === null)
|
||||
return;
|
||||
|
||||
/*
|
||||
* finds the input:checked in the element .calculate_field
|
||||
* and trigger the event 'change' on it
|
||||
* - this event 'change' is added by form builder in divi-form-calc-min.js :
|
||||
* $(document).on(
|
||||
* 'change',
|
||||
* '.calculate_field input:not([type="hidden"]), .calculate_field select',
|
||||
* function () {
|
||||
* ...
|
||||
* }
|
||||
* );
|
||||
*
|
||||
*/
|
||||
function trigger_change(element) {
|
||||
/*
|
||||
* jquery version
|
||||
*
|
||||
let inputs = $(element).find('input:checked');
|
||||
inputs.trigger('change');
|
||||
*/
|
||||
|
||||
/*
|
||||
* js version
|
||||
*
|
||||
*/
|
||||
let inputs = element.querySelectorAll('input:checked');
|
||||
// loop through inputs and trigger 'change' event on each
|
||||
inputs.forEach(function(input) {
|
||||
// Triggering 'change' event
|
||||
let change_event = new Event('change', {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
});
|
||||
input.dispatchEvent(change_event);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// create an observer on form to check if child nodes are modified
|
||||
const observer_form = new MutationObserver(wait_for_calculation_class);
|
||||
observer_form.observe(form_calculation, {
|
||||
subtree: true,
|
||||
attributes: true,
|
||||
});
|
||||
|
||||
// observe mutations to see if they include the addition of class .calculate_field
|
||||
// if the class is added, call the function that might trigger the change event on it
|
||||
function wait_for_calculation_class(mutationsList) {
|
||||
mutationsList.forEach((mutation) => {
|
||||
// check if class where added
|
||||
if (mutation.type !== 'attributes')
|
||||
return;
|
||||
if (mutation.attributeName !== 'class')
|
||||
return;
|
||||
// check if added class is .calculate_field
|
||||
let target = mutation.target;
|
||||
if (target.classList.contains('calculate_field')) {
|
||||
// If the class is added, trigger the 'change' event
|
||||
trigger_change(target);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
patch_form_calculation_CIPF();
|
||||
48
plugins/fbpatch/menu/admin_menu.php
Normal file
48
plugins/fbpatch/menu/admin_menu.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?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() {
|
||||
echo "<p>hello</p>";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* menu plugin
|
||||
*/
|
||||
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
plugins/fbpatch/menu/admin_menu_toggle.php
Normal file
108
plugins/fbpatch/menu/admin_menu_toggle.php
Normal 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']);
|
||||
}
|
||||
|
||||
$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');
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
27
plugins/fbpatch/php/calculations.php
Normal file
27
plugins/fbpatch/php/calculations.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?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 add_form_builder_calculations_patch() {
|
||||
$handle = 'form_builder_calculations_patch';
|
||||
$url = plugin_dir_url(__DIR__) . '/js/calculations.js';
|
||||
$dependencies = array('de_fb_calc');
|
||||
$version = null;
|
||||
$defer = true;
|
||||
wp_enqueue_script($handle, $url, $dependencies, $version, $defer);
|
||||
}
|
||||
add_action('wp_enqueue_scripts', __NAMESPACE__.'\add_form_builder_calculations_patch', 22);
|
||||
|
||||
|
||||
|
||||
?>
|
||||
30
plugins/fbpatch/php/fbpatch_class.php
Normal file
30
plugins/fbpatch/php/fbpatch_class.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?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'];
|
||||
|
||||
//private static $_is_
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user