- updated states to work for user_id and post_id
- added states for partner page state publish-draft
This commit is contained in:
@@ -9,6 +9,36 @@ if (!defined('ABSPATH')) {
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* utility function that checks if the current page is owned by the logged in partner
|
||||
*
|
||||
*/
|
||||
function is_own_partner() {
|
||||
Plgntls::debug_infos(2);
|
||||
$role_partner = Plgntls::ROLE_PARTNER;
|
||||
|
||||
if (!is_single()) {
|
||||
return false;
|
||||
}
|
||||
if (!is_user_logged_in()) {
|
||||
return false;
|
||||
}
|
||||
if (!current_user_can($role_partner)) {
|
||||
return false;
|
||||
}
|
||||
global $post;
|
||||
if (is_null($post)) {
|
||||
return false;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -18,35 +48,26 @@ if (!defined('ABSPATH')) {
|
||||
*/
|
||||
function partner_page_scripts_CIPF() {
|
||||
Plgntls::debug_infos(2);
|
||||
$role_partner = Plgntls::ROLE_PARTNER;
|
||||
|
||||
/*
|
||||
* if on post, load css
|
||||
* - to hide partner own stuff
|
||||
* - for states
|
||||
*
|
||||
*/
|
||||
if (!is_single()) {
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* if on post, load css to hide partner own stuff
|
||||
*
|
||||
*/
|
||||
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_user_logged_in()) {
|
||||
return;
|
||||
}
|
||||
if (!current_user_can($role_partner)) {
|
||||
return;
|
||||
}
|
||||
global $post;
|
||||
if (is_null($post)) {
|
||||
return;
|
||||
}
|
||||
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) {
|
||||
if (!is_own_partner()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -61,6 +82,106 @@ add_action('wp_enqueue_scripts', 'partner_page_scripts_CIPF', 11);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* listen to the front button to toggle page publish/draft
|
||||
*
|
||||
*/
|
||||
function toggle_partner_page_CIPF() {
|
||||
Plgntls::debug_infos();
|
||||
$toggle_partner_page = Plgntls::QUERY_TOGGLE_PARTNER_PAGE;
|
||||
|
||||
/*
|
||||
* check if :
|
||||
* - is own partner
|
||||
* - has query action
|
||||
*
|
||||
*/
|
||||
if (!is_own_partner()) {
|
||||
return;
|
||||
}
|
||||
Plgntls::debug_infos();
|
||||
if (!isset($_GET['action'])) {
|
||||
return;
|
||||
}
|
||||
if ($_GET['action'] !== $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');
|
||||
error_log("url: " . $url);
|
||||
wp_safe_redirect($url);
|
||||
exit;
|
||||
|
||||
|
||||
}
|
||||
add_action('template_redirect', 'toggle_partner_page_CIPF');
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* early checks on partner page
|
||||
*
|
||||
*/
|
||||
function page_partner_check_CIPF() {
|
||||
Plgntls::debug_infos(2);
|
||||
|
||||
// is partner own page
|
||||
if (!is_own_partner()) {
|
||||
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);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user