Files
2024_WEBSITE_fipf/plugins/cipf_plugin/php/partners_page.php

308 lines
5.1 KiB
PHP

<?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!');
}
/*
* checks if the current page is owned by the logged in partner
* dont work in 'init' hook
* works in 'wp' hook
*
*/
function is_own_partner_CIPF() {
Plgntls::debug_infos(2);
$role_partner = Cipf::ROLE_PARTNER;
if (!is_partner_CIPF()) {
return false;
}
if (!is_single()) {
return false;
}
Plgntls::debug_infos();
global $post;
if (is_null($post)) {
return false;
}
$current_post_author_id = (int)($post->post_author);
$current_user_id = (int)get_current_user_id();
if ($current_user_id !== $current_post_author_id) {
return false;
}
return true;
}
/*
* action to be done at the init state of the page
*
*/
function partner_page_init_CIPF() {
Plgntls::debug_infos(2);
if (!is_partner_CIPF()) {
return;
}
Plgntls::debug_infos();
// https://developer.wordpress.org/reference/functions/get_query_var/#more-information
global $wp;
$wp->add_query_var('action');
}
add_action('init','partner_page_init_CIPF');
/*
* upload scripts and styles on partner page
*
*/
function partner_page_scripts_CIPF() {
Plgntls::debug_infos(2);
/*
* if on post, load css
* - to hide partner own stuff
* - for states
*
*/
if (!is_single()) {
return;
}
Plgntls::debug_infos();
Plgntls::add_to_front(array('css/partner_page.css'));
$post_id = get_the_ID();
display_states_css_CIPF($post_id);
/*
* then check if is partner own page
*
*/
if (!is_own_partner_CIPF()) {
return;
}
/*
* on partner own page, load css to show own stuff
*
*/
Plgntls::add_to_front(array('css/partner_page_own.css'));
}
add_action('wp_enqueue_scripts', 'partner_page_scripts_CIPF', 11);
/*
* prevent access to the post if in draft
*
*/
function restrict_partner_page_draft_CIPF() {
Plgntls::debug_infos(2);
/*
* the restrictions only concerns logged in users
* and on post (partner pages)
* -> own partners are not restricted
* -> also not admin and fipf
*
*/
if (!is_single()) {
return;
}
if (!is_user_logged_in()) {
return;
}
if (is_fipf_CIPF()) {
return;
}
if (is_admin_CIPF()) {
return;
}
if (is_own_partner_CIPF()) {
return;
}
Plgntls::debug_infos();
/*
* get the post id and object
*
*/
$post_id = get_the_ID();
$current_post = get_post($post_id);
if (is_null($current_post)) {
return;
}
/*
* if post is draft, nobody should see it,
* except own partner (but they are already out this function)
*
*/
if ($current_post->post_status === 'draft') {
redirect_home_CIPF();
}
}
add_action('template_redirect', 'restrict_partner_page_draft_CIPF');
/*
* listen to the front button to toggle page publish/draft
*
*/
function toggle_partner_page_CIPF() {
Plgntls::debug_infos(2);
$toggle_partner_page = Cipf::QUERY_TOGGLE_PARTNER_PAGE;
/*
* check if :
* - is own partner
* - has query action
* to check for 'action' query, add 'action' to query vars in 'init' hook
* -> https://developer.wordpress.org/reference/functions/get_query_var/#more-information
*
*/
if (!is_own_partner_CIPF()) {
return;
}
Plgntls::debug_infos();
$is_action_toggle = get_query_var('action', false);
if ($is_action_toggle !== $toggle_partner_page) {
return;
}
/*
* get the post id and object
*
*/
$post_id = get_the_ID();
$current_post = get_post($post_id);
if (is_null($current_post)) {
return;
}
/*
* toogle the status
*
*/
if ($current_post->post_status === 'publish') {
wp_update_post(array(
'ID' => $post_id,
'post_status' => 'draft',
));
set_page_draft_CIPF($post_id);
}
else if ($current_post->post_status === 'draft') {
wp_update_post(array(
'ID' => $post_id,
'post_status' => 'publish',
));
set_page_publish_CIPF($post_id);
}
/*
* redirects without the query
*
*/
$url = remove_query_arg('action');
wp_safe_redirect($url);
exit;
}
add_action('template_redirect', 'toggle_partner_page_CIPF', 5);
/*
* if url uses post id, ex: /?p=40772
* make redirects to its post_name version, ex: /la-fipf
*
*/
function partner_page_check_url_CIPF() {
Plgntls::debug_infos(2);
if (!is_own_partner_CIPF()) {
return;
}
Plgntls::debug_infos();
/*
* get the post name
*
*/
$post_id = get_the_ID();
$current_post = get_post($post_id);
if (is_null($current_post)) {
return;
}
/*
*
*
*/
$current_slug = trim($_SERVER['REQUEST_URI'], '/');
$slug = trim($current_post->post_name, '/');
if ($slug !== $current_slug) {
wp_safe_redirect(home_url($slug));
exit;
}
}
add_action('template_redirect', 'partner_page_check_url_CIPF', 9);
/*
* early checks on partner page
*
*/
function page_partner_check_CIPF() {
Plgntls::debug_infos(2);
// is partner own page
if (!is_own_partner_CIPF()) {
return;
}
Plgntls::debug_infos();
$post_id = get_the_ID();
$current_post = get_post($post_id);
/*
* checks if the acf state field is set accrodingly to page state
*
*/
if ($current_post->post_status === 'publish') {
set_page_publish_CIPF($post_id);
}
else if ($current_post->post_status === 'draft') {
set_page_draft_CIPF($post_id);
}
}
add_action('wp', 'page_partner_check_CIPF', 11);
?>