/** * Renders the setting markup. * * @since 3.0.0 * @wp-hook post_submitbox_misc_actions * * @param WP_Post $post Post object. * * @return void */ public function render(WP_Post $post) { $id = 'mlp-trasher'; ?> <div class="misc-pub-section misc-pub-mlp-trasher"> <?php echo \Inpsyde\MultilingualPress\nonce_field($this->nonce); ?> <label for="<?php echo esc_attr($id); ?> "> <input type="checkbox" name="<?php echo esc_attr(TrasherSettingRepository::META_KEY); ?> " value="1" id="<?php echo esc_attr($id); ?> " <?php checked($this->setting_repository->get($post->ID)); ?> > <?php _e('Send all the translations to trash when this post is trashed.', 'multilingual-press'); ?> </label> </div> <?php }
/** * Updates the trasher setting of the post with the given ID as well as all related posts. * * @since 3.0.0 * @wp-hook save_post * * @param int $post_id Post ID. * @param WP_post $post Post object. * * @return int The number of posts updated. */ public function update_settings($post_id, WP_Post $post) { if (!$this->nonce->is_valid()) { return 0; } if (!in_array($post->post_status, ['publish', 'draft'], true)) { return 0; } $value = array_key_exists(TrasherSettingRepository::META_KEY, $_POST) ? (bool) $_POST[TrasherSettingRepository::META_KEY] : false; if (!$this->setting_repository->update($post_id, $value)) { return 0; } $current_site_id = get_current_blog_id(); $related_posts = $this->content_relations->get_relations($current_site_id, $post_id, 'post'); unset($related_posts[$current_site_id]); if (!$related_posts) { return 1; } $updated_posts = 1; array_walk($related_posts, function ($post_id, $site_id) use(&$updated_posts, $value) { switch_to_blog($site_id); $updated_posts += $this->setting_repository->update($post_id, $value); restore_current_blog(); }); return $updated_posts; }
/** * Trashes all related posts. * * @since 3.0.0 * @wp-hook wp_trash_post * * @param int $post_id Post ID. * * @return int The number of related posts trashed. */ public function trash_related_posts($post_id) { if (!$this->setting_repository->get($post_id)) { return 0; } $current_site_id = get_current_blog_id(); $related_posts = $this->content_relations->get_relations($current_site_id, $post_id, 'post'); unset($related_posts[$current_site_id]); if (!$related_posts) { return 0; } $trashed_post = 0; // Temporarily remove the function to avoid recursion. $action = current_action(); remove_action($action, [$this, __FUNCTION__]); array_walk($related_posts, function ($post_id, $site_id) use(&$trashed_post) { switch_to_blog($site_id); $trashed_post += (bool) wp_trash_post($post_id); restore_current_blog(); }); // Add the function back again. add_action($action, [$this, __FUNCTION__]); return $trashed_post; }