diff --git a/plugins/cipf_plugin/cipf_plugin.php b/plugins/cipf_plugin/cipf_plugin.php index f70ebc9..24e135c 100644 --- a/plugins/cipf_plugin/cipf_plugin.php +++ b/plugins/cipf_plugin/cipf_plugin.php @@ -40,14 +40,15 @@ include_once(Plgntls::root_path() . 'php/partners_register.php'); include_once(Plgntls::root_path() . 'php/partners_page.php'); include_once(Plgntls::root_path() . 'php/partners_form.php'); // utils -//include_once(Plgntls::root_path() . '/php/menus.php'); -include_once(Plgntls::root_path() . 'php/states.php'); -include_once(Plgntls::root_path() . 'php/redirections.php'); -include_once(Plgntls::root_path() . 'php/display_css.php'); -include_once(Plgntls::root_path() . 'php/payments.php'); -include_once(Plgntls::root_path() . 'php/random_posts.php'); -include_once(Plgntls::root_path() . 'php/scheduled_events.php'); -include_once(Plgntls::root_path() . 'php/emails.php'); +include_once(Plgntls::root_path() . 'php/_utils_states.php'); +include_once(Plgntls::root_path() . 'php/_utils_redirections.php'); +include_once(Plgntls::root_path() . 'php/_utils_display_css.php'); +include_once(Plgntls::root_path() . 'php/_utils_checks_roles.php'); +// actions +include_once(Plgntls::root_path() . 'php/_actions_payments.php'); +include_once(Plgntls::root_path() . 'php/_actions_random_posts.php'); +include_once(Plgntls::root_path() . 'php/_actions_scheduled_events.php'); +include_once(Plgntls::root_path() . 'php/_actions_emails.php'); // admin include_once(Plgntls::root_path() . 'php/admin_hide_bar.php'); include_once(Plgntls::root_path() . 'php/admin_user_profil.php'); diff --git a/plugins/cipf_plugin/php/emails.php b/plugins/cipf_plugin/php/_actions_emails.php similarity index 100% rename from plugins/cipf_plugin/php/emails.php rename to plugins/cipf_plugin/php/_actions_emails.php diff --git a/plugins/cipf_plugin/php/payments.php b/plugins/cipf_plugin/php/_actions_payments.php similarity index 100% rename from plugins/cipf_plugin/php/payments.php rename to plugins/cipf_plugin/php/_actions_payments.php diff --git a/plugins/cipf_plugin/php/random_posts.php b/plugins/cipf_plugin/php/_actions_random_posts.php similarity index 100% rename from plugins/cipf_plugin/php/random_posts.php rename to plugins/cipf_plugin/php/_actions_random_posts.php diff --git a/plugins/cipf_plugin/php/scheduled_events.php b/plugins/cipf_plugin/php/_actions_scheduled_events.php similarity index 100% rename from plugins/cipf_plugin/php/scheduled_events.php rename to plugins/cipf_plugin/php/_actions_scheduled_events.php diff --git a/plugins/cipf_plugin/php/_utils_checks_roles.php b/plugins/cipf_plugin/php/_utils_checks_roles.php new file mode 100644 index 0000000..92b69e2 --- /dev/null +++ b/plugins/cipf_plugin/php/_utils_checks_roles.php @@ -0,0 +1,81 @@ + 'post', + 'author' => $current_user_id, + 'posts_per_page' => 1, + 'post_status' => array('publish', 'draft'), + ); + $post = get_posts($args); + if (empty($post)) { + return false; + } + else { + return reset($post); + } +} + + + + +/* +* to use is_user_logged_in at early init hook +* 1162 : wordpress_docker/volumes/wp_volume/wp-includes/pluggable.php +* +*/ +function is_user_logged_in_CIPF() { + Plgntls::debug_infos(2); + if (function_exists('is_user_logged_in')) { + return is_user_logged_in(); + } + else { + $user = wp_get_current_user(); + return $user->exists(); + } + Plgntls::debug_infos(); +} + + + +/* +* to use current_user_can at early init hook +* 877 : wordpress_docker/volumes/wp_volume/wp-includes/capabilities.php +* +*/ +function current_user_can_CIPF($capability) { + Plgntls::debug_infos(2); + if (function_exists('current_user_can')) { + return current_user_can($capability); + } + else { + $user = wp_get_current_user(); + return user_can($user, $capability); + } + Plgntls::debug_infos(); +} + + + + + + + +?> diff --git a/plugins/cipf_plugin/php/display_css.php b/plugins/cipf_plugin/php/_utils_display_css.php similarity index 100% rename from plugins/cipf_plugin/php/display_css.php rename to plugins/cipf_plugin/php/_utils_display_css.php diff --git a/plugins/cipf_plugin/php/redirections.php b/plugins/cipf_plugin/php/_utils_redirections.php similarity index 73% rename from plugins/cipf_plugin/php/redirections.php rename to plugins/cipf_plugin/php/_utils_redirections.php index 202e060..ac785d3 100644 --- a/plugins/cipf_plugin/php/redirections.php +++ b/plugins/cipf_plugin/php/_utils_redirections.php @@ -18,7 +18,8 @@ function redirect_home_CIPF(){ Plgntls::debug_infos(); // Set up nocache headers before redirecting : https://developer.wordpress.org/reference/functions/wp_safe_redirect/#user-contributed-notes - nocache_headers(); + // i dont really know what it does, but it creates a warning sometimes : 'PHP Warning: Cannot modify - header information headers already sent in /var/www/html/wp-includes/pluggable.php' + //nocache_headers(); wp_redirect(home_url(), 301); exit; @@ -31,7 +32,7 @@ function redirect_home_CIPF(){ * redirects new prof to commande * */ -function redirect_command_CIPF(){ +function redirect_prof_command_CIPF(){ Plgntls::debug_infos(); $slug_command_card = Cipf::SLUG_COMMAND_CARD; @@ -82,25 +83,12 @@ function redirection_profil_CIPF(){ * */ if (current_user_can($role_partner)) { - $args = array( - 'post_type' => 'post', - 'author' => $current_user_id, - 'posts_per_page' => 1, - 'post_status' => array('publish', 'draft'), - ); - $posts = get_posts($args); - - /* - * if post exists, redirects to it - * otherwise redirects to post creation - * - */ - if (empty($posts)) { + $partner_post = has_partner_post(); + if (false === $partner_post) { $redirect_url = $partner_page_creation; } else { - $query = reset($posts); - $redirect_url = get_permalink($query->ID); + $redirect_url = get_permalink($partner_post->ID); } wp_redirect($redirect_url, 301); exit; @@ -150,6 +138,31 @@ add_action('template_redirect', 'redirection_page_CIPF'); +/* +* checks if partner current user is partner +* works as soon as 'init' hook +* +* first hook to use is...() is parse_query +* -> https://developer.wordpress.org/apis/hooks/action-reference/ +* - after 'init', before 'wp' +* but 'init' already has set user, so we can recreate the functions +* +*/ +function redirects_home_if_not_partner() { + Plgntls::debug_infos(2); + $role_partner = Cipf::ROLE_PARTNER; + + if (!is_user_logged_in_CIPF()) { + redirect_home_CIPF(); + } + if (!current_user_can_CIPF($role_partner)) { + redirect_home_CIPF(); + } + Plgntls::debug_infos(); +} + + + ?> diff --git a/plugins/cipf_plugin/php/states.php b/plugins/cipf_plugin/php/_utils_states.php similarity index 100% rename from plugins/cipf_plugin/php/states.php rename to plugins/cipf_plugin/php/_utils_states.php diff --git a/plugins/cipf_plugin/php/menus.php b/plugins/cipf_plugin/php/menus.php deleted file mode 100644 index 1e3c4ee..0000000 --- a/plugins/cipf_plugin/php/menus.php +++ /dev/null @@ -1,44 +0,0 @@ - Menus - * 2. dans le code ci-dessous, changer la valeur de $menu_title pour correspondre au titre du menu - * 3. et si besoin changer la valeur de $menu_redirect pour choisir la page de redirection : - * - si laissée vide, la redirection se fera sur la page de connexion de wordpress - * - avec $current_url la redirection se fera sur la page actuelle - * - avec $base_url on redirige vers la page d'accueil du site (l'url sans chemin supplementaire) - * cette variable $base_url peut etre utilisee pour construire une autre url : - * - $menu_redirect = $base_url -> https://le_site_actuel.com/ - * - $menu_redirect = $base_url . 'contact' -> https://le_site_actuel.com/contact - * - $menu_redirect = $current_url -> https://le_site_actuel.com/la_meme_page - * - $menu_redirect = 'www.un_autre_site.net/contact' -> https://www.un_autre_site.net/contact - */ -function change_menu_logout($items){ - Plgntls::debug_infos(); - $menu_title = 'special logout'; - - // quelques urls utiles : - $base_url = home_url(); - $current_url = home_url( $_SERVER['REQUEST_URI'] ); - - $menu_redirect = ''; - foreach($items as $item){ - if( $item->title === $menu_title){ - $item->url = wp_nonce_url( wp_logout_url( $menu_redirect ), 'log-out' ); - } - } - return $items; -} -add_filter('wp_nav_menu_objects', 'change_menu_logout'); - - -?> diff --git a/plugins/cipf_plugin/php/partners_form.php b/plugins/cipf_plugin/php/partners_form.php index 9fb0b8a..96b05c5 100644 --- a/plugins/cipf_plugin/php/partners_form.php +++ b/plugins/cipf_plugin/php/partners_form.php @@ -9,6 +9,44 @@ if (!defined('ABSPATH')) { +/* +* the earliest hook to use is_..() is parse_query +* -> https://developer.wordpress.org/apis/hooks/action-reference/ +* +*/ +function is_partner_form_creation_page_CIPF() { + Plgntls::debug_infos(); + $slug_partner_create_page = Cipf::SLUG_PARTNER_CREATE_PAGE; + + /* + * only for the partner form creation page + * + */ + if (!is_page($slug_partner_create_page)) { + return false; + } + + return true; +} + + + +/* +* action to be done at the init state of the page +* cannot check if user is logged in or partner at this step +* +*/ +function partner_form_creation_page_init_CIPF() { + Plgntls::debug_infos(2); + + redirects_home_if_not_partner(); + + // https://developer.wordpress.org/reference/functions/get_query_var/#more-information + global $wp; + $wp->add_query_var('pid'); +} +add_action('init','partner_form_creation_page_init_CIPF'); + @@ -18,33 +56,37 @@ if (!defined('ABSPATH')) { */ function partner_form_creation_page_CIPF() { Plgntls::debug_infos(2); - $role_partner = Cipf::ROLE_PARTNER; - $slug_partner_create_page = Cipf::SLUG_PARTNER_CREATE_PAGE; - /* - * only for the partner form creation page - * - */ - if (!is_page($slug_partner_create_page)) { + redirects_home_if_not_partner(); + if (!is_partner_form_creation_page_CIPF()) { return; } Plgntls::debug_infos(); /* - * redirect anyone that is not a partner + * -> if partner don't have page yet && this is not edit page -> let him access it + * if partner dont' have page yet && this is edit page -> redirect him + * if partner have page && this is not edit page -> redirect him + * -> if partner have page && this is edit page && this edit its page -> let him access it + * if partner have page && this is edit page && this dont edit its page -> redirect him + * + * to check for pid, add 'pid' to query vars in 'init' hook + * -> https://developer.wordpress.org/reference/functions/get_query_var/#more-information * */ - if (!is_user_logged_in()) { - redirect_home_CIPF(); + $is_edit_id = get_query_var('pid', false); + $partner_post = has_partner_post(); + if (false === $partner_post) { + if (false === $is_edit_id) { + return; + } } - if (!current_user_can($role_partner)) { - redirect_home_CIPF(); + else { + $post_id = $partner_post->ID; + if ($is_edit_id == $post_id) { + return; + } } - - /* - * if a partner already has a page, redirect it - * - */ redirection_profil_CIPF(); } add_action('template_redirect', 'partner_form_creation_page_CIPF'); @@ -62,5 +104,4 @@ add_action('template_redirect', 'partner_form_creation_page_CIPF'); - ?> diff --git a/plugins/cipf_plugin/php/partners_page.php b/plugins/cipf_plugin/php/partners_page.php index 77798a7..16ee50f 100644 --- a/plugins/cipf_plugin/php/partners_page.php +++ b/plugins/cipf_plugin/php/partners_page.php @@ -17,7 +17,6 @@ function is_own_partner() { Plgntls::debug_infos(2); $role_partner = Cipf::ROLE_PARTNER; -error_log("in toggle A"); /* * is_single() will only return true for published post * so instead checks for both is_singular and post_type @@ -34,27 +33,22 @@ error_log("post type: " . get_post_type()); } */ -error_log("in toggle B"); if (!is_user_logged_in()) { return false; } -error_log("in toggle C"); if (!current_user_can($role_partner)) { return false; } -error_log("in toggle D"); global $post; if (is_null($post)) { return false; } -error_log("in toggle E"); Plgntls::debug_infos(); $current_post_author = (int)($post->post_author); $current_user_id = (int)get_current_user_id(); if ($current_user_id !== $current_post_author) { return false; } -error_log("in toggle F"); return true; } @@ -115,20 +109,16 @@ function toggle_partner_page_CIPF() { * - has query action * */ -error_log("in toggle 1"); if (!is_own_partner()) { return; } -error_log("in toggle 2"); Plgntls::debug_infos(); if (!isset($_GET['action'])) { return; } -error_log("in toggle 3"); if ($_GET['action'] !== $toggle_partner_page) { return; } -error_log("in toggle 4"); /* * get the post id and object @@ -139,7 +129,6 @@ error_log("in toggle 4"); if (is_null($current_post)) { return; } -error_log("in toggle 5"); /* * toogle the status diff --git a/plugins/cipf_plugin/php/profs_profil.php b/plugins/cipf_plugin/php/profs_profil.php index 54aeab1..3ef63ad 100644 --- a/plugins/cipf_plugin/php/profs_profil.php +++ b/plugins/cipf_plugin/php/profs_profil.php @@ -121,7 +121,7 @@ function prof_profil_redirects_CIPF() { * */ if (is_account_new_CIPF()) { - redirect_command_CIPF(); + redirect_prof_command_CIPF(); } } add_action('template_redirect', 'prof_profil_redirects_CIPF', 11);