From 6c6817ea9f485470c0da32c7a50d035e1253d805 Mon Sep 17 00:00:00 2001 From: Maud Leray Date: Wed, 16 Aug 2023 12:00:20 +0200 Subject: [PATCH] PLANET-7177 Move Default P4 Post Type setting under Settings > Writing (#2094) This is to simplify our settings and improve UX --- src/CustomTaxonomy.php | 24 ++++----- src/Loader.php | 1 + src/Migrations/M021MigrateDefaultPostType.php | 32 +++++++++++ src/Migrator.php | 2 + src/Settings.php | 30 ----------- src/Settings/DefaultPostType.php | 53 +++++++++++++++++++ 6 files changed, 100 insertions(+), 42 deletions(-) create mode 100644 src/Migrations/M021MigrateDefaultPostType.php create mode 100644 src/Settings/DefaultPostType.php diff --git a/src/CustomTaxonomy.php b/src/CustomTaxonomy.php index 43b4220d78..c63fa33fad 100644 --- a/src/CustomTaxonomy.php +++ b/src/CustomTaxonomy.php @@ -4,6 +4,7 @@ use WP_Post; use WP_Term; +use P4\MasterTheme\Settings\DefaultPostType; /** * Class CustomTaxonomy @@ -89,8 +90,8 @@ public function create_taxonomy_metabox_markup(WP_Post $post): void $all_types = $this->get_terms(); if (-1 === $current_type) { // Assign default p4-pagetype for new POST. - $default_p4_pagetype = $this->get_default_p4_pagetype(); - $current_type = $default_p4_pagetype->slug; + $planet4_default_post_type = $this->get_planet4_default_post_type(); + $current_type = $planet4_default_post_type->slug; } wp_nonce_field('p4-save-page-type', 'p4-page-type-nonce'); @@ -208,20 +209,19 @@ public function get_multilingual_terms(): array * * @return WP_term|int|WP_Error */ - public function get_default_p4_pagetype() + public function get_planet4_default_post_type() { - $options = get_option('planet4_options'); - $default_p4_pagetype = $options['default_p4_pagetype'] ?? 0; + $planet4_default_post_type = DefaultPostType::get_option() ?? 0; - if (0 === $default_p4_pagetype) { + if (0 === $planet4_default_post_type) { // If default p4-pagetype setting not found, use taxonomy's first term. $all_terms = $this->get_terms(); - $default_p4_pagetype = $all_terms[0] ?? 0; + $planet4_default_post_type = $all_terms[0] ?? 0; } else { - $default_p4_pagetype = get_term($default_p4_pagetype, self::TAXONOMY); + $planet4_default_post_type = get_term($planet4_default_post_type, self::TAXONOMY); } - return $default_p4_pagetype; + return $planet4_default_post_type; } /** @@ -428,12 +428,12 @@ public function save_taxonomy_page_type(int $post_id, WP_Post $post): void return; } - $default_p4_pagetype = $this->get_default_p4_pagetype(); + $planet4_default_post_type = $this->get_planet4_default_post_type(); // Assign default p4-pagetype, if no term is assigned to post. if (empty($terms)) { - if ($default_p4_pagetype instanceof \WP_Term) { - wp_set_post_terms($post_id, [$default_p4_pagetype->term_id], self::TAXONOMY); + if ($planet4_default_post_type instanceof \WP_Term) { + wp_set_post_terms($post_id, [$planet4_default_post_type->term_id], self::TAXONOMY); } // Assign the first term, if more than one terms are assigned. } elseif (count($terms) > 1 && $terms[0] instanceof \WP_Term) { diff --git a/src/Loader.php b/src/Loader.php index 7cee60cf62..1f16981dad 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -90,6 +90,7 @@ private function load_services(array $services): void $this->default_services[] = MediaArchive\Rest::class; $this->default_services[] = Settings\ReadingTime::class; $this->default_services[] = Settings\CommentsGdpr::class; + $this->default_services[] = Settings\DefaultPostType::class; // Load P4 Metaboxes only when adding/editing a new Page/Post/Campaign. if ('post-new.php' === $pagenow || 'post.php' === $pagenow) { diff --git a/src/Migrations/M021MigrateDefaultPostType.php b/src/Migrations/M021MigrateDefaultPostType.php new file mode 100644 index 0000000000..6caa7ec32d --- /dev/null +++ b/src/Migrations/M021MigrateDefaultPostType.php @@ -0,0 +1,32 @@ + [ 'title' => 'Defaults content', 'fields' => [ - [ - 'name' => __('Default P4 Post Type', 'planet4-master-theme-backend'), - 'id' => 'default_p4_pagetype', - 'type' => 'pagetype_select_taxonomy', - ], - [ 'name' => __('Take Action Covers default button text', 'planet4-master-theme-backend'), 'id' => 'take_action_covers_button_text', @@ -508,7 +502,6 @@ public function hooks(): void add_filter('cmb2_render_get_informed_page_dropdown', [$this, 'p4_render_page_dropdown'], 10, 2); add_filter('cmb2_render_take_action_page_dropdown', [$this, 'p4_render_page_dropdown'], 10, 2); add_filter('cmb2_render_about_us_page_dropdown', [$this, 'p4_render_page_dropdown'], 10, 2); - add_filter('cmb2_render_pagetype_select_taxonomy', [$this, 'p4_render_pagetype_dropdown'], 10, 2); add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_assets']); add_action('admin_init', [$this, 'add_new_identity_styles_toggle_value']); @@ -615,29 +608,6 @@ public function p4_render_category_dropdown(CMB2_Field $field_args, $value): voi } // phpcs:enable SlevomatCodingStandard.Functions.UnusedParameter - /** - * Render p4-pagetype dropdown. - * - * @param CMB2_Field $field_args CMB2 field Object. - * @param mixed $value Pagetype taxonomy ID. - * phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter -- add_filter callback - */ - public function p4_render_pagetype_dropdown(CMB2_Field $field_args, $value): void - { - - wp_dropdown_categories( - [ - 'show_option_none' => __('Select Posttype', 'planet4-master-theme-backend'), - 'hide_empty' => 0, - 'orderby' => 'name', - 'selected' => $value, - 'name' => 'default_p4_pagetype', - 'taxonomy' => 'p4-page-type', - ] - ); - } - // phpcs:enable SlevomatCodingStandard.Functions.UnusedParameter - /** * Admin page markup. Mostly handled by CMB2. * diff --git a/src/Settings/DefaultPostType.php b/src/Settings/DefaultPostType.php new file mode 100644 index 0000000000..e3f3e5ce46 --- /dev/null +++ b/src/Settings/DefaultPostType.php @@ -0,0 +1,53 @@ +hooks(); + } + + public function hooks(): void + { + add_action('admin_init', [self::class, 'register_settings']); + } + + public static function register_settings(): void + { + register_setting( + 'writing', + self::KEY, + ['type' => 'integer'], + ); + + add_settings_field( + self::KEY, + __('Default P4 Post Type', 'planet4-master-theme-backend'), + [self::class, 'settings_field'], + 'writing', + ); + } + + public static function settings_field(): void + { + wp_dropdown_categories( + [ + 'show_option_none' => __('Select Post Type', 'planet4-master-theme-backend'), + 'hide_empty' => 0, + 'orderby' => 'name', + 'selected' => self::get_option(), + 'name' => self::KEY, + 'taxonomy' => 'p4-page-type', + ] + ); + } + + public static function get_option(): int + { + return get_option(self::KEY); + } +}