4 Commits

Author SHA1 Message Date
asus
7738ad1863 - added role at registration with xootix
- cleaned folder
2024-03-02 16:14:36 +01:00
asus
8aeef1dca8 added inline code to plugin tools 2024-03-01 17:21:03 +01:00
asus
bd30c97194 - tests on editing author page with acf fields
- some reflexions on registrations
2024-02-29 23:45:02 +01:00
asus
6e88bba879 wip trying to modify html before it is rendered 2024-02-29 01:04:41 +01:00
24 changed files with 167 additions and 907 deletions

View File

@@ -1,7 +0,0 @@
.first_el_to_change {
border: 1px solid red;
}
#model_plugin_shortcode {
border: 1px solid blue;
padding: 10px;
}

View File

@@ -0,0 +1,6 @@
#mytext {
border: 1px solid blue;
}
#mytext_2 {
border: 1px solid green;
}

View File

@@ -8,46 +8,44 @@ Version: 1.1.0
Author URI:
*/
/**
* it means someone outside wp is accessing the file, in this case kill it.
*/
/*
* it means someone outside wp is accessing the file, in this case kill it.
*/
if (!defined('ABSPATH')) {
die('You can not access this file!');
}
/**
* plugin dir root
*/
/*
* plugin dir root
*/
include_once( plugin_dir_path(__FILE__) . '/utils/plgntls_class.php');
PLGNTLS_class::set_root_dir( plugin_dir_path(__FILE__), plugin_dir_url(__FILE__) );
/**
* general inclusions
*/
include_once(PLGNTLS_class::get_path() . '/php/utils/globals.php');
include_once(PLGNTLS_class::get_path() . '/php/utils/console_log.php');
//include_once(PLGNTLS_class::get_path() . '/php/images/image_edit_shortcode.php');
//include_once(PLGNTLS_class::get_path() . '/php/images/image-edit.php');
/*
* general inclusions
*/
include_once(PLGNTLS_class::get_path() . '/utils/globals.php');
include_once(PLGNTLS_class::get_path() . '/utils/console_log.php');
//include_once(PLGNTLS_class::get_path() . '/php/test_cache.php');
include_once(PLGNTLS_class::get_path() . '/php/paypal/paypal.php');
include_once(PLGNTLS_class::get_path() . '/php/register/partenaires.php');
/**
* test the class PLGNTLS
*/
/*
* test the class PLGNTLS
*/
add_shortcode('fipfcard_plugin', 'fipfcard_test_class_tools');
/**
*
*/
/*
*
function custom_frontend_posting_form() {
$my_image = the_field('image_ratio');
error_log("my_image");
@@ -55,287 +53,28 @@ function custom_frontend_posting_form() {
return $my_image;
}
add_shortcode('custom_frontend_posting_form', 'custom_frontend_posting_form');
// function author_shortode_test($field) {
// // global $authordata;
// // $user_id = $authordata->ID;
//
// $author_username = get_query_var('author_name');
// $author = get_user_by('slug', $author_username);
// $user_id = $author->ID;
//
// $acf_field = $field['field'];
// $acf_post_id = "user_{$user_id}";
//
// $acf_shortcode = "[acf field='" . $acf_field . "' post_id='" . $acf_post_id . "']";
//
// error_log("---");
// error_log("acf_shortcode:");
// error_log($acf_shortcode);
// // error_log("get_userdata:");
// // error_log(json_encode(get_userdata($user_id)->data->user_email));
// error_log("get_fields($acf_post_id):");
// error_log(json_encode(get_fields($acf_post_id)));
// error_log("get_field($acf_field, $acf_post_id):");
// error_log(json_encode(get_field($acf_field, $acf_post_id)));
//
// //$shortcode = "[acf field=" . $field['field'] . "]";
// wp_reset_query();
// $acf_return = do_shortcode($acf_shortcode);
// // error_log("acf_return:");
// // error_log(json_encode($acf_return));
// return $acf_return;
// // regarder option repeter de diviengine
// // peutetre plugin gratuit ?
// // plugin menu avec conditions
// }
// add_shortcode('fipf_acf', 'author_shortode_test', 10, 1);
/**
* call to paypal_shortcode_content()
*/
include_once(PLGNTLS_class::get_path() . '/php/paypal/paypal.php');
add_shortcode('fipf_paypal_shortcode', 'fipf_paypal_shortcode_content');
//function my_custom_url_handler($query)
//{
// if ($_SERVER['REQUEST_METHOD'] !== 'POST' )
// return ;
// if ($_SERVER['QUERY_STRING'] !== 'update_profile_acf' )
// return ;
// // ! add verification that user can do this edit !
//
// foreach($_POST['field_name'] as $field)
// {
// error_log("_POST[$field]");
// error_log($_POST[$field]);
// }
//}
/*
add_action('init', 'custom_action_handler');
function custom_action_handler() {
error_log("inside init :)");
if ( isset( $_GET['custom_action'] ) && $_GET['custom_action'] === 'do_something' ) {
error_log("GET :)");
}
if ( isset( $_POST['custom_action'] ) && $_POST['custom_action'] === 'do_something' ) {
error_log("POST :)");
}
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
error_log("REQUEST_METHOD === GET");
error_log("_GET");
error_log(json_encode($_GET));
}
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['submit'])) {
error_log("REQUEST_METHOD === GET && GET[SUBMIT]");
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['QUERY_STRING'] === 'custom_action=do_something' ) {
error_log("_POST['de_fb_ville']");
error_log(json_encode($_POST['de_fb_ville']));
}
*/
// custom-form-handler.php
/*
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit'])) {
error_log("REQUEST_METHOD === POST && POST[SUBMIT]");
error_log("_POST");
error_log(json_encode($_POST));
// Form submitted, handle the submission
$name = sanitize_text_field($_POST['name']);
$email = sanitize_email($_POST['email']);
// Perform validation
$errors = array();
if (empty($name)) {
$errors['name'] = 'Name is required';
}
if (!is_email($email)) {
$errors['email'] = 'Invalid email address';
}
if (empty($errors)) {
// Form data is valid, process the submission
// For example, save data to the database or send an email
// Redirect the user or display a success message
} else {
// Form data is not valid, display errors
// You might re-render the form with error messages here
}
}
}
*/
/*
[17-Feb-2024 17:54:21 UTC]
{
"SERVER_SOFTWARE":"nginx\/1.20.2",
"REQUEST_URI":"\/?custom_action=do_something",
"USER":"www-data",
"HOME":"\/home\/www-data",
"HTTP_SEC_GPC":"1",
"HTTP_SEC_FETCH_USER":"?1",
"HTTP_SEC_FETCH_SITE":"same-origin",
"HTTP_SEC_FETCH_MODE":"navigate",
"HTTP_SEC_FETCH_DEST":"document",
"HTTP_UPGRADE_INSECURE_REQUESTS":"1",
"HTTP_COOKIE":"wordpress_test_cookie=WP%20Cookie%20check; wordpress_logged_in_f31a945e73c53ab9af191e34eb33fa88=hugo%7C1708332197%7CbJvy7kRZ3UWJ2C0ZOaSuhpLCz11vHL8CA68sQv9U1ne%7C927c46e3b46a2716df0fcce98d7ccda0e158be0aad0593b2dc233de4ccad9e27; wp-settings-time-11=1708187741; mjx.menu=renderer%3ANativeMML%26%3Bsemantics%3Atrue%26%3Bcontext%3ABrowser%26%3Bzoom%3ANone",
"HTTP_REFERER":"https:\/\/local_fipfcard_plugin.com\/modif_profile\/",
"HTTP_CONNECTION":"keep-alive",
"HTTP_DNT":"1",
"HTTP_ORIGIN":"https:\/\/local_fipfcard_plugin.com",
"HTTP_CONTENT_LENGTH":"2310",
"HTTP_CONTENT_TYPE":"multipart\/form-data; boundary=---------------------------92033335430820533424009149678",
"HTTP_ACCEPT_ENCODING":"gzip, deflate, br",
"HTTP_ACCEPT_LANGUAGE":"en-US,en;q=0.5",
"HTTP_ACCEPT":"text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/avif,image\/webp,*\/*;q=0.8",
"HTTP_USER_AGENT":"Mozilla\/5.0 (X11; Ubuntu; Linux x86_64; rv:122.0) Gecko\/20100101 Firefox\/122.0",
"HTTP_HOST":"local_fipfcard_plugin.com",
"REDIRECT_STATUS":"200",
"SERVER_NAME":"local_fipfcard_plugin.com",
"SERVER_PORT":"443",
"SERVER_ADDR":"172.18.0.4",
"REMOTE_PORT":"47220",
"REMOTE_ADDR":"172.18.0.1",
"GATEWAY_INTERFACE":"CGI\/1.1",
"HTTPS":"on",
"REQUEST_SCHEME":"https",
"SERVER_PROTOCOL":"HTTP\/1.1",
"DOCUMENT_ROOT":"\/var\/www\/html",
"DOCUMENT_URI":"\/index.php",
"SCRIPT_NAME":"\/index.php",
"CONTENT_LENGTH":"2310",
"CONTENT_TYPE":"multipart\/form-data; boundary=---------------------------92033335430820533424009149678",
"REQUEST_METHOD":"POST",
"QUERY_STRING":"custom_action=do_something",
"SCRIPT_FILENAME":"\/var\/www\/html\/index.php",
"FCGI_ROLE":"RESPONDER",
"PHP_SELF":"\/index.php",
"REQUEST_TIME_FLOAT":1708192460.934234,
"REQUEST_TIME":1708192460
}
$_POST
{
"field_title":["ville_title","nom","pedagogie"],
"field_name":["de_fb_ville","de_fb_nom","de_fb_pedagogie"],
"field_id":["de_fb_ville_id","de_fb_nom","de_fb_pedagogie"],
"de_fb_ville":"gut",
"de_fb_nom":"gut",
"de_fb_pedagogie":"gut",
"form_key":"33420-0",
"unique_id":"d2564b14-290e-4865-b740-7932ae770d43",
"form_type":"custom",
"divi-form-submit":"yes",
"form_id":"modif_profile",
"form_type_confirm":""
}
*/
// add_action('parse_request', 'my_custom_url_handler');
// function my_custom_url_handler($query)
// {
// if ($_SERVER['REQUEST_METHOD'] !== 'POST' )
// return ;
// if ($_SERVER['QUERY_STRING'] !== 'update_profile_acf' )
// return ;
// // ! add verification that user can do this edit !
//
// foreach($_POST['field_name'] as $field)
// {
// error_log("_POST[$field]");
// error_log($_POST[$field]);
// }
// }
/**
* do_action( 'df_before_process', string $form_id, Array $post_array, String $form_type )
* Description: Fires after form is submitted and captcha checking is ok.
* Parameters:
* - $form_id: (String) Form ID from Form setting.
* - $post_array: (Array) Submitted fields from the form. Keys are mapped field names or custom field names (in case of Field Mapping Type is Custom).
* - $form_type : (String) Form Type
*
* wordpress_docker/volumes/wp_volume/wp-content/plugins/divi-form-builder/includes/DiviFormBuilder.php|298 col 18-35| do_action( 'df_before_process', $form_id, $post_array, $form_type );
* wordpress_docker/volumes/wp_volume/wp-content/plugins/divi-form-builder/includes/ajaxcalls/post_ajax.php|254 col 16-33| do_action( 'df_before_process', $form_id, $post_array, $form_type );
*
* check user rights :
* - https://developer.wordpress.org/reference/functions/is_user_logged_in/
* - https://stackoverflow.com/questions/19802492/check-if-current-user-is-administrator-in-wordpress
* - https://developer.wordpress.org/reference/functions/current_user_can/
*/
// function PLGNTLS_my_custom_df_form_handler($form_id, $post_array, $form_type)
// {
// error_log("in my_custom_df_form_handler");
// error_log("form_id");
// error_log($form_id);
// error_log("post_array");
// error_log(json_encode($post_array));
// error_log("form_type");
// error_log($form_type);
// }
// add_action ('df_before_process', 'PLGNTLS_my_custom_df_form_handler', 10, 3);
/**
* Hook into the 'acf/save_post' action
*/
// add_action('acf/save_post', 'handle_acf_form_submission', 5); // 20 is the priority, you can adjust it as needed
// function handle_acf_form_submission($post_id)
// {
// error_log("in acf/save_post");
// if ($_SERVER['REQUEST_METHOD'] === 'POST' )
// {
// error_log("_SERVER['REQUEST_URI']");
// error_log(json_encode($_SERVER['REQUEST_URI']));
// error_log("_SERVER['QUERY_STRING']");
// error_log(json_encode($_SERVER['QUERY_STRING']));
// error_log("_POST['de_fb_ville']");
// error_log(json_encode($_POST['de_fb_ville']));
// }
// // Check if the request is coming from the specific URL
// if ($_SERVER['QUERY_STRING'] !== '/') {
// return; // Exit if the request is not from the specific URL
// }
//
// // Check if this is an autosave
// if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
// return;
// }
//
// // Check if this is a revision
// if (wp_is_post_revision($post_id)) {
// return;
// }
//
// // Check if the current user has permission to edit the post
// if (!current_user_can('edit_post', $post_id)) {
// return;
// }
//
// // Your ACF form submission handling logic goes here
// // For example, you can retrieve form data using $_POST and update ACF fields accordingly
// if (isset($_POST['your_acf_field_name'])) {
// $field_value = sanitize_text_field($_POST['your_acf_field_name']);
// update_field('your_acf_field_name', $field_value, $post_id);
// }
// }
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
menu plugin
* menu plugin
*/
include_once(PLGNTLS_class::get_path() . '/php/menu/example_menu.php');
function fipfcard_plugin_menu()

View File

@@ -1,70 +0,0 @@
<div id="model_plugin_shortcode">
<ol>
<p>list get_metadata('post') :</p>
<?php
foreach($post_metadata as $meta_key => $meta_value) {
include($plgn_dir."html/templates/print_meta.html");
}
?>
</ol>
<ol>
<p>list get_post_meta() :</p>
<?php
foreach($post_meta as $meta_key => $meta_value) {
include($plgn_dir."html/templates/print_meta.html");
}
?>
</ol>
<ol>
<p>list get_metadata('user') :</p>
<?php
foreach($user_metadata as $meta_key => $meta_value) {
include($plgn_dir."html/templates/print_meta.html");
}
?>
</ol>
<ol>
<p>list get_user_meta() :</p>
<?php
foreach($user_meta as $meta_key => $meta_value) {
include($plgn_dir."html/templates/print_meta.html");
}
?>
</ol>
<ol>
<p>list get_userdata() :</p>
<?php
foreach($user_data as $meta_key => $meta_value) {
include($plgn_dir."html/templates/print_meta.html");
}
?>
</ol>
<ol>
<p>list wp_get_current_user() :</p>
<?php
foreach($current_user as $meta_key => $meta_value) {
include($plgn_dir."html/templates/print_meta.html");
}
?>
</ol>
<ol>
<p>list wp_get_current_user()->data :</p>
<?php
foreach($current_user->data as $meta_key => $meta_value) {
include($plgn_dir."html/templates/print_meta.html");
}
?>
</ol>
<p>get_field('user_email') :</p>
<p>
<?php echo $user_email ?>
</p>
<p>i am a new p</p>
<p class="first_el_to_change">to change</p>
<button id='test_ajax_1' name="ajax_button_1" value="2024">2024</button>
<?php
foreach($names as $name) {
include($plgn_dir."html/templates/example_presentation.html");
}
?>

View File

@@ -1,22 +0,0 @@
<?php
foreach($ages as $age) {
?>
<p>age <?php echo esc_html($age); ?></p>
<?php
}
?>
<p class="third_el_to_change">to change</p>
<p>aaaaaand this is the end</p>
<form method="post" action="<?php echo esc_url( wp_registration_url() ); ?>">
<input type="text" name="user_login" placeholder="username">
<input type="email" name="user_email" placeholder="email">
<input type="password" name="user_pass" placeholder="password">
<button type="submit">register</button>
</form>
<form method="post" action="<?php echo esc_url( wp_lostpassword_url() ); ?>">
<input type="hidden" id="user_login" value="rexe">
<input type="text" name="pass1" data-reveal="1" data-pw="i0#chBo*S1Br5iDY" placeholder="new password">
<input type="hidden" name="rp_key" value="WO8qGALMbbKKsEEhXsOQ">
<button type="submit">generate</button>
</form>
</div>

View File

@@ -1,3 +0,0 @@
<input type='text' id='image_id' placeholder="image_id" value="33555">
<button id='edit_image'>edit image</button>
<div id="media_editor"></div>

View File

@@ -1,2 +0,0 @@
<p>hello <?php echo esc_html($name); ?></p>
<p class="second_el_to_change">to change</p>

View File

@@ -1,4 +0,0 @@
<li>
"<?php echo esc_html($meta_key); ?>" :
<?php echo esc_html(json_encode($meta_value)); ?>
</li>

View File

@@ -1,20 +0,0 @@
console.log("PLGNTLS_data:");
console.log(PLGNTLS_data);
const title = document.querySelector(".first_el_to_change");
title.innerHTML = "--- coucou ;) " + PLGNTLS_data.myvar_1;
const ajax_button_1 = document.querySelector("#test_ajax_1");
ajax_button_1.addEventListener('click', () => {
PLGNTLS_data.ajax(ajax_button_1, 'get_data')
.then((response) => response.json())
.then((data) => {
console.log("datou: ");
console.log(data);
})
.catch((error) => {
console.log("error: ");
console.log(error);
})
;
});

View File

@@ -1,3 +0,0 @@
const title2 = document.querySelector(".second_el_to_change");
title2.innerHTML = "--- ho boy !";

View File

@@ -1,3 +0,0 @@
const title3 = document.querySelector(".third_el_to_change");
title3.innerHTML = "--- bye bye, " + PLGNTLS_data.myvar_2;

View File

@@ -10,8 +10,18 @@ if (!defined('ABSPATH')) {
function fipfcard_plugin_content() {
$fipfcard = new PLGNTLS_class();
$my_css = '
#mytext {
background-color: lightblue;
}
#mytext_2 {
background-color: lightgreen;
}
';
echo $fipfcard->add_to_front( array(
array("js/menu/example_menu.js", 'type'=>'module'),
"css/menu/menu.css",
"PLGNTLS_menu_css" => array('css'=>$my_css),
"js/menu/example_menu_2.js",
"html/menu/example_menu.html",
));

View File

@@ -38,6 +38,10 @@ add_action('template_redirect', 'check_paypal_request');
/**
* call to paypal_shortcode_content()
*/
function fipf_paypal_shortcode_content()
{
$fipfcard_paypal = new PLGNTLS_class();
@@ -67,6 +71,9 @@ function fipf_paypal_shortcode_content()
return $added_to_front;
}
add_shortcode('fipf_paypal_shortcode', 'fipf_paypal_shortcode_content');
/**
@@ -100,5 +107,4 @@ function fipf_routes_endpoints()
add_action('rest_api_init', 'fipf_routes_endpoints');
?>

View File

@@ -0,0 +1,15 @@
<?php
function add_partenaires_PLGNTLS($customer_data){
$current_url = $_SERVER['HTTP_REFERER'];
$path_brut = parse_url($current_url, PHP_URL_PATH);
$path = trim($path_brut, '/');
if ($path === 'creation-du-compte-partenaire')
$customer_data['role'] = 'partenaire';
return $customer_data;
}
add_filter( 'xoo_el_register_new_customer_data', 'add_partenaires_PLGNTLS', 10, 1 );
?>

View File

@@ -1,33 +0,0 @@
<?php
/**
* 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 prevent_page_caching( $headers ) {
error_log("in prevent_page_caching");
if (is_page('test-form-modals')) {
error_log("in test-form-modals");
$headers['Cache-Control'] = 'no-cache, no-store, must-revalidate';
$headers['Pragma'] = 'no-cache';
$headers['Expires'] = '0';
}
return $headers;
}
add_filter( 'nocache_headers', 'prevent_page_caching' );
function add_custom_meta_tags() {
error_log("add_custom_meta_tags");
echo '<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">';
echo '<meta http-equiv="Pragma" content="no-cache">';
echo '<meta http-equiv="Expires" content="0">';
}
add_action('wp_head', 'add_custom_meta_tags');
?>

View File

@@ -1,74 +0,0 @@
<?php
/**
* 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 fipfcard_test_class_tools()
{
$fipfcard_tools = new PLGNTLS_class();
$myvar_1 = "I am one";
$myvar_2 = "I am two";
$names = ["hugo", "camille"];
$ages = ["13", "34", "56"];
$data = wp_get_current_user();
$data = $data->data;
$data = $data->user_email;
//error_log("data->data");
//error_log($data);
//delete_post_meta(get_the_ID(), "_data_user_email");
//delete_post_meta(get_the_ID(), "_data");
//add_post_meta(get_the_ID(), "data_user_email", $data);
$post_metadata = get_metadata( 'post', get_the_ID() );
$post_meta = get_post_meta( get_the_ID() );
$user_metadata = get_metadata( 'user', get_current_user_id() );
$user_meta = get_user_meta( get_current_user_id() );
$acf_get_fields = get_fields( get_the_ID() );
$user_data = get_userdata( get_current_user_id() );
$current_user = wp_get_current_user();
$user_email = get_field('user_email', 'MarieM');
return $fipfcard_tools->add_to_front
(
array
(
"css/example_style.css",
"js/example_init.js",
"js/example_script2.js",
"js/example_script3.js",
"html/example_index.html",
"html/example_index2.html",
),
compact
(
"myvar_1",
"myvar_2",
"post_metadata",
"post_meta",
"user_metadata",
"user_meta",
"acf_get_fields",
"user_data",
"current_user",
"user_email",
"names",
"ages",
)
);
}
?>

View File

@@ -1,163 +0,0 @@
<?php
/**
* 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 that add the ajax script to front
no real needs to check if already included :
- $handle is uniq so it will not be re-enqueued
the first enqueued version would be kept
- if we used add_var_to_front() (which use wp_add_inline_script())
the vars would be added twice
leading to js syntaxe error (redeclaraiton of 'let' or 'const')
- but we use wp_localize_script() so the object will be overwritten
it's not a real pbm
(what is more efficient, check for double or overwritte object ?)
*/
function fipfcard_add_ajax_post() {
global $fipfcard_first_script;
global $fipfcard_ajax_file;
$file = fipfcard_init_file($fipfcard_ajax_file);
// // check if ajax script was already enqueued
// global $wp_scripts;
// $already_enqueued = array_search($file->handle, $wp_scripts->queue);
// if ($already_enqueued !== false)
// return ;
$fipfcard_first_script = $file->handle;
wp_enqueue_script( $file->handle, $file->url, '', $file->version, true);
$_url = admin_url( 'admin-ajax.php' );
$_nonce = wp_create_nonce( 'wp-pageviews-nonce' );
$vars = compact("_url","_nonce",);
// add_var_to_front($vars);
$object_name = "fipfcard_ajax";
wp_localize_script($file->handle, $object_name, $vars);
}
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@param string : name of the file, with its path from its extension directory
- from js/ root for .js files
- from css/ root for .css files
@return object / null :
- null if file is not js or css
- or an object with all the necessary infos :
- ext : name.js -> "js"
- basename : name.js -> "name"
- handle : name.js -> "name_js"
- url : url to file in wordpress
- path : path to file in server
- version : used to avoid browser caching
*/
function fipfcard_init_file($file_name) {
$file = (object)[];
$file->ext = pathinfo($file_name, PATHINFO_EXTENSION);
if ($file->ext === "js")
$dir_path = 'js/';
else if ($file->ext === "css")
$dir_path = 'css/';
else
return null;
$file->basename = pathinfo($file_name, PATHINFO_FILENAME);
$file->handle = str_replace(".", "_", $file_name);
$file->url = FIPFCARD_PLUGIN_URL.$dir_path.$file_name;
$file->path = FIPFCARD_PLUGIN_DIR.$dir_path.$file_name;
$file->version = date("ymd-Gis", filemtime($file->path));
return $file;
}
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@param array : list of files :
- with their path from root of their type of file (ex: from js/ to .js files)
- and with their extension
@param boolean
- to add ajax script and variables
- default to true
*/
function fipfcard_add_files_to_front($files_arr, $add_ajax = true) {
//wp_enqueue_script(<give_it_a_name>, /url/to/file, [depends on], version, defer? );
//wp_enqueue_style( <give_it_a_name>, /url/to/file, [depends on], version, media );
global $fipfcard_first_script;
if ($add_ajax === true)
fipfcard_add_ajax_post();
$previous_css_basename = '';
$previous_js_basename = '';
foreach ($files_arr as $file_name) {
$file = fipfcard_init_file($file_name);
if ($file->ext === "js") {
if (is_null($fipfcard_first_script))
$fipfcard_first_script = $file->handle;
wp_enqueue_script( $file->handle, $file->url, $previous_js_basename, $file->version, true);
$previous_js_basename = $file->basename;
}
else if ($file->ext === "css") {
wp_enqueue_style( $file->handle, $file->url, $previous_css_basename, $file->version, '');
$previous_css_basename = $file->basename;
}
}
}
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
pass variables to js front as global variables
@param array : list of key => value
with the key being name of the variable, like this :
'my_var' => 'value',
simpler way to do it is to use compact when calling the function :
add_var_to_front(compact("var1", "var2", "var3"));
@param string (optionnal) : name of first embended script that need these variables
(it will be available to this script and all followings)
this name is the filename + "_" + extension :
init.js -> init_js
*/
function fipfcard_add_var_to_front($vars, $handle = null) {
if (is_null($handle)) {
global $fipfcard_first_script;
$handle = $fipfcard_first_script;
}
extract($vars);
foreach ($vars as $key => $var)
{
$js_var = 'let '.$key.' = '.json_encode($var).';';
wp_add_inline_script($handle, $js_var, 'before');
}
// the other way with localize has multiple incidences :
// - it creates an object from wich you can access the variables
// - so if you call it again wiht the same name, it will overwrite the previous
// {
// $handle = pathinfo($handle, PATHINFO_FILENAME);
// $object_name = "php_data";
//
// wp_localize_script($handle, $object_name, $vars);
// }
}
?>

View File

@@ -1,46 +0,0 @@
<?php
/**
* it means someone outside wp is accessing the file, in this case kill it.
*/
if (!defined('ABSPATH')) {
die('You can not access this file!');
}
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@param two arguments :
1. html files to include in front
- can be a string of 1 filename
- or an array of strings of filenames
( https://stackoverflow.com/q/4747876/9497573 )
- it's probably better to only add 1 file, and let it include other files
2. list of variables to make available to this files
- in the form of key => val
- recommanded to do it with compact()
ex: create_html( "file.html", compact("var1","var2",) );
ex: create_html( array("file1.html", "file2.html"), array("var1"=>"value") );
@return a string of html code
using ob_start() and ob_get_clean()
allows to have php expansion inside the html loaded
in opposition to the methode file_get_contents()
https://stackoverflow.com/a/4402045/9497573
*/
function fipfcard_create_html($files, $vars = null) {
$files = (array)$files;
$html_dir = FIPFCARD_PLUGIN_DIR.'html/';
if (!is_null($vars))
extract($vars);
ob_start();
foreach($files as $file) {
include($html_dir.$file);
}
$html = ob_get_clean();
return $html;
}
?>

View File

@@ -8,6 +8,9 @@
*
* PLGNTLS means PLUGIN TOOLS
*
* .add_to_front() : this method is made to add files and codes all at once
* otherwise use the wp functions (wp_enqueue_*, wp_add_inline_*, etc)
*
*
* ex:
*
@@ -16,34 +19,31 @@
* $var_2 = 'value_2';
* $var_3 = 'value_3';
* return $my_plugin_class->add_to_front(
* array(
* 'path/to/style.css' // those files are added to front
* 'path/to/script.js' // in the order you put them
* 'http://my_url.com' // it can be urls
* 'path/to/file.html' // html are just rendered then returned
* array( // files added in order by types
* 'path/to/style1.css', //
* 'path/to/script1.js', //
* 'path/to/style2.css', // -> depends on style1.css
* 'http://my_url1.com', //
* 'path/to/script2.js', // -> depends on script1.js
* 'path/to/file1.html', // | will be returned
* 'path/to/file2.html', // -> | as expanded html
* 'path/to/file3.html', // | in the order included
* 'handle_name' => 'path/to/script3.js', // -> depends on the script with handle 'handle_name'
* array( //
* 'path/to/script4.js', // | add a script with attributes
* 'attribute_1' => 'value_1', // -> | first element of array must be
* 'attribute_2' => 'value_2', // | the script, without explicit key
* ), //
* array( 'js' => 'var_js' ), // -> add inline js, only first element will be used
* array( 'css' => 'var_css' ), // -> add inline css, only first element will be used
* ),
* compact( // these variables are added to html and js files
* 'var_1', // in html files you can access them directly
* 'var_2', // in js files they are properties of object PLGNTLS_data
* 'var_3', // like PLGNTLS_data.var_1;
* 'var_1', // - in html files you can access them directly
* 'var_2', // - in js files they are properties of object PLGNTLS_data
* 'var_3', // like PLGNTLS_data.var_1;
* )
* );
*
* complete syntax to include js scripts :
*
* key => value
* 1: 'src'
* 2: 'dependence' => 'src'
* 3: array( 'src', 'attr_1' => 'value_1', ... )
* 4: 'dependence' => array( 'src', 'attr_1' => 'value_1', ... )
*
* -> 'src' is required
* -> 'dependence' is optional
* explicit key is always a dependence
* -> value can be a string 'src', or an array('srcs', ...)
* if value is array, first element is 'src' and following
* are attributes for <script> html elements
*
*/
class PLGNTLS_class
@@ -52,10 +52,10 @@ class PLGNTLS_class
private static $_root_url;
private $_first_script;
private $_first_style;
private $_prefix;
private $_js_dependencies;
private $_css_dependencies;
private $_scripts_modules;
private $_scripts_attributes;
/**
@@ -63,9 +63,9 @@ class PLGNTLS_class
public function __construct() {
$this->_prefix = "PLGNTLS";
$this->_first_script = null;
$this->_first_style = null;
$this->_js_dependencies = array();
$this->_css_dependencies = array();
$this->_scripts_modules = array();
$this->_scripts_attributes = array();
}
@@ -95,8 +95,6 @@ class PLGNTLS_class
if (!is_null($srcs_arr))
$this->add_fetch($srcs_arr, $vars);
array_push($this->_scripts_modules, 'PLGNTLS_example_menu_js', 'PLGNTLS_plgntls_fetch_js');
$srcs = array();
foreach($srcs_arr as $src_key => $src_value) {
$init = $this->init_src($src_key, $src_value);
@@ -234,7 +232,13 @@ class PLGNTLS_class
$previous_css_basename = '';
$previous_js_basename = '';
foreach ($srcs_arr as $src) {
if (in_array($src->ext, array("js", "url"))) {
if ($src->inline !== null) {
if ($src->inline === "js")
$this->add_inline_script($src);
else if ($src->inline === "css")
$this->add_inline_style($src);
}
else if (in_array($src->ext, array("js", "url"))) {
$this->add_script($src, $previous_js_basename);
$previous_js_basename = $src->handle;
}
@@ -246,13 +250,16 @@ class PLGNTLS_class
// https://developer.wordpress.org/reference/hooks/wp_script_attributes/
// https://wordpress.stackexchange.com/questions/66843/attach-a-private-function-at-a-hook
add_filter( 'wp_script_attributes', fn($attr)=>$this->add_type_module($attr), 10, 1 );
add_filter( 'wp_script_attributes', fn($attr)=>$this->add_attributes_to_script($attr), 10, 1 );
/*
* uncomment to print all enqueued scripts, can be usefull
* uncomment to print all enqueued files, can be usefull
global $wp_scripts;
error_log("wp_scripts->queue:");
error_log(json_encode($wp_scripts->queue));
global $wp_styles;
error_log("wp_styles->queue:");
error_log(json_encode($wp_styles->queue));
*/
}
private function add_script($script, $previous_js_basename) {
@@ -263,14 +270,28 @@ class PLGNTLS_class
wp_enqueue_script( $script->handle, $script->url, $depends_on, $script->version, true);
}
private function add_style($style, $previous_css_basename) {
if (is_null($this->_first_style))
$this->_first_style = $style->handle;
$depends_on = $this->check_dependencies($style, $previous_css_basename);
if ($depends_on !== null)
wp_enqueue_style( $style->handle, $style->url, $depends_on, $style->version, '');
}
private function add_inline_script($src) {
$handle = $src->depends;
if ($handle === null)
$handle = $this->_first_script;
wp_add_inline_script($src->depends, $src->src, 'before');
}
private function add_inline_style($src) {
error_log("inside add_inline_style");
$handle = $src->depends;
if ($handle === null)
$handle = $this->_first_style;
wp_add_inline_style($src->depends, $src->src);
}
private function add_type_module($attr) {
private function add_attributes_to_script($attr) {
if (empty($attr['id']))
return $attr;
$handle = $attr['id'];
@@ -296,7 +317,7 @@ class PLGNTLS_class
if ($already_enqueued !== false)
return null;
}
else if ($script->ext === "css")
else if ($src->ext === "css")
{
global $wp_styles;
$already_enqueued = array_search($src->handle, $wp_styles->queue);
@@ -330,20 +351,52 @@ class PLGNTLS_class
* 6. version : used to avoid browser caching
* 7. depends : string if depends on a handle, or ''
* 8. attr : associative array of html attribute like 'type'=>'module'
* 9. inline : array("js" => "name.js") -> js
* array("css" => "name.css") -> css
*/
private function init_src($key, $value) {
if (empty($value))
return null;
$src = (object)[];
$src = (object)[
'src' => null,
'ext' => null,
'basename' => null,
'handle' => null,
'url' => null,
'path' => null,
'version' => null,
'depends' => null,
'attr' => null,
'inline' => null,
];
// 7. depends
$src->depends = '';
if (is_string($key))
$src->depends = $key;
// 0. src
// 8. attr
// 9. inline
// first element of array is used, so must not be empty
// value => ['path/to/file', 'key1'=>'value1', 'key2'=>'value2']
// src => 'path/to/file'
// attr => ['key1'=>'value1', 'key2'=>'value2']
if (is_array($value)){
$src->src = array_shift($value);
$src->attr = $value;
$first_key = array_keys($value)[0];
if (empty($value[$first_key]))
return null;
if ($first_key === 0) { // is a script file or url with attributes
$src->src = array_shift($value);
$src->attr = $value;
}
else if ($first_key === "js" || $first_key === "css") { // is an inline code
$src->src = $value[$first_key];
$src->inline = $first_key;
return $src; // inline only needs 'depends', 'src' and 'inline'
}
}
else
{
else {
$src->src = $value;
$src->attr = null;
}
@@ -384,14 +437,12 @@ class PLGNTLS_class
$src->version = date("ymd-Gis", filemtime($src->path));
}
// 7. depends
$src->depends = '';
if (is_string($key))
$src->depends = $key;
// if ext is 'js' or 'url' and attr is not empty
// also add to global variable to access in 'wp_script_attributes' filter
if ($src->attr !== null) {
$this->_scripts_attributes[$src->handle.'-js'] = $src->attr;
if ($src->ext === 'js' || $src->ext === 'url') {
if ($src->attr !== null) {
$this->_scripts_attributes[$src->handle.'-js'] = $src->attr;
}
}
return $src;

View File

@@ -1,117 +0,0 @@
/**
* function that create an ajax post action
* - PLGNTLS_data.ajax_nonce and PLGNTLS_data.ajax_url
* are passed from the class PLGNTLS_class
*/
/*
// OLD VERSION
console.log("PLGNTLS_data");
console.log(PLGNTLS_data);
*/
function PLGNTLS_ajax(action, data_obj) {
const data = new FormData();
data.append("action", action);
data.append("_ajax_nonce", PLGNTLS_data.ajax_nonce);
// for (const key in data_obj)
// {
// data.append(key, data_obj[key]);
// }
// console.log("data:", data);
data.append(data_obj);
return fetch(PLGNTLS_data.ajax_url, {
method: "POST",
credentials: "same-origin",
hedears: {"Content-Type": "application/json"},
body: JSON.stringify(data),
});
}
// https://stackoverflow.com/q/52657814/9497573
function is_plain_object(obj)
{
if (!obj)
return false;
if (obj.constructor !== Object)
return false;
if (Object.getPrototypeOf(obj) !== Object.prototype)
return false;
return true;
}
/*
function PLGNTLS_fetch(src = null, options = null)
{
if (src === null)
return null;
if (options === null)
{
const data = new FormData();
data.append('action', src);
data.append('_ajax_nonce', PLGNTLS_data.ajax_nonce);
options = {
method: 'POST',
credentials: 'same-origin',
body: data,
};
}
else if (is_plain_object(options))
{
// check for method, default POST
if (!Object.hasOwn(options, 'method'))
options.method = 'POST';
// check for credentials, default same-origin
if (!Object.hasOwn(options, 'credentials'))
options.credentials = 'same-origin';
// check for body, default contains action and nonce
if (!Object.hasOwn(options, 'body'))
options.body = new FormData();
// add action and nonce in options.body
if (options.body instanceof FormData)
{
// dont check if action and nonce already exist, need new anyway
options.body.set('action', src);
options.body.set('_ajax_nonce', PLGNTLS_data.ajax_nonce);
}
else
{
// should think a better strategy : https://stackoverflow.com/q/20295080/9497573
// const data = {};
// if (is_plain_object(options.body))
// {
// for (const key in options.body)
// data[key] = options.body[key];
// }
// else
// data.data = options.body;
// data.action = src;
// data._ajax_nonce = PLGNTLS_data.ajax_nonce;
// options.body = JSON.stringify(data);
// cannot work if fetch use :
// headers: {"Content-Type": "application/json"},
const data = new FormData( );
data.append( 'action', src );
data.append( '_ajax_nonce', PLGNTLS_data.ajax_nonce );
if ( is_plain_object( options.body ) )
{
for ( const key in options.body )
data.append( key, JSON.stringify(options.body[key]) );
}
// is string : https://stackoverflow.com/q/4059147/9497573
else if ( typeof options.body === 'string' || options.body instanceof String )
data.append( 'data', options.body );
else
data.append('data', JSON.stringify(options.body));
options.body = data;
}
}
else
throw new Error('options not plain object or formData');
return fetch(PLGNTLS_data.ajax_url, options);
}
*/

Submodule private updated: 987b3a522f...8b8c17b979