From 93e00c30995de16997691d39d95192de1961b44d Mon Sep 17 00:00:00 2001 From: Guido Scialfa Date: Sat, 28 Jul 2018 13:22:23 +0200 Subject: [PATCH] Do not show edit post submit actions for custom post types not translatable #310 --- src/inc/controllers/Mlp_Dashboard_Widget.php | 35 ++++++++++++++ src/inc/controllers/Mlp_Trasher.php | 50 +++++++++++++++++++- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/inc/controllers/Mlp_Dashboard_Widget.php b/src/inc/controllers/Mlp_Dashboard_Widget.php index feafa56b..6785d962 100644 --- a/src/inc/controllers/Mlp_Dashboard_Widget.php +++ b/src/inc/controllers/Mlp_Dashboard_Widget.php @@ -5,6 +5,14 @@ */ class Mlp_Dashboard_Widget { + /** + * @var string[] + */ + private $allowed_post_types = array( + 'post', + 'page', + ); + /** * @var Inpsyde_Nonce_Validator */ @@ -66,6 +74,10 @@ public function post_submitbox_misc_actions() { $is_translated = $this->is_translated( $post_id ); + if ( ! $this->isAllowedPost( $post_id ) ) { + return; + } + /** * Filters the visibility of the 'Translation completed' checkbox. * @@ -261,4 +273,27 @@ private function is_translated( $post_id ) { return false; } + + /** + * Check if post type is allowed to be translated. + * + * @param $post_id + * + * @return bool + */ + private function isAllowedPost( $post_id ) { + + $allowed = apply_filters( + 'mlp_allowed_post_types', + $this->allowed_post_types, + $this + ); + + $post = get_post( (int) $post_id ); + if ( ! $post ) { + return false; + } + + return in_array( $post->post_type, $allowed, true ); + } } diff --git a/src/inc/controllers/Mlp_Trasher.php b/src/inc/controllers/Mlp_Trasher.php index 2afca659..4fadecb9 100644 --- a/src/inc/controllers/Mlp_Trasher.php +++ b/src/inc/controllers/Mlp_Trasher.php @@ -5,6 +5,14 @@ */ class Mlp_Trasher { + /** + * @var string[] + */ + private $allowed_post_types = array( + 'post', + 'page', + ); + /** * @var Mlp_Module_Manager_Interface */ @@ -65,9 +73,14 @@ public function initialize() { */ public function post_submitbox_misc_actions() { - $post_id = absint( filter_input( INPUT_GET, 'post' ) ); + $post_id = $this->get_post_id(); + + if ( ! $this->isAllowedPost( $post_id ) ) { + return; + } + if ( $post_id ) { - // old key + // old key. $trash_the_other_posts = (int) get_post_meta( $post_id, 'trash_the_other_posts', true ); if ( 1 !== $trash_the_other_posts ) { @@ -195,4 +208,37 @@ private function register_setting() { 'description' => $description, ) ); } + + /** + * Returns the current post ID, or 0 on failure. + * + * @return int + */ + private function get_post_id() { + + return absint( filter_input( INPUT_GET, 'post' ) ); + } + + /** + * Check if post type is allowed to be translated. + * + * @param $post_id + * + * @return bool + */ + private function isAllowedPost( $post_id ) { + + $allowed = apply_filters( + 'mlp_allowed_post_types', + $this->allowed_post_types, + $this + ); + + $post = get_post( (int) $post_id ); + if ( ! $post ) { + return false; + } + + return in_array( $post->post_type, $allowed, true ); + } }