Edit: class-project-dashboard.php
<?php /** * Project Enhancement Dashboard Class * * Handles customization of the Divi 'project' custom post type. * * @package Divi_Essential * @subpackage Includes */ namespace Divi_Essential\Includes; defined( 'ABSPATH' ) || exit; // Exit if accessed directly /** * Project_Dashboard Class */ class Project_Dashboard { const PROJECT_ENHANCE_OPTION_KEY = 'dnxte_project_enhance_options'; /** * Constructor */ public function __construct() { // Register hooks for admin UI add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); add_action( 'wp_ajax_dnxte_project_enhance_action', array( $this, 'save_project_enhance_settings' ) ); // Register filters for CPT and taxonomy modification // These need to run before the post types are registered (priority -1) add_filter( 'et_project_posttype_args', array( $this, 'modify_project_cpt_args' ), -1 ); add_filter( 'et_project_posttype_rewrite_args', array( $this, 'modify_project_rewrite_args' ), -1 ); add_filter( 'register_taxonomy_args', array( $this, 'modify_project_taxonomy_args' ), -1, 2 ); // Check if we need to flush rewrite rules add_action( 'admin_init', array( $this, 'maybe_flush_rewrite_rules' ) ); } /** * Enqueue scripts and styles */ public function enqueue_scripts( $hook ): void { // Only load on our settings page if ( 'divi-essential_page_dnxte-customizable-project-posts' !== $hook ) { return; } wp_enqueue_style( 'dashicons' ); wp_enqueue_style( 'dnxte-project-dashboard', DIVI_ESSENTIAL_ASSETS . 'admin/css/project-dashboard.css', array(), DIVI_ESSENTIAL_VERSION ); wp_enqueue_script( 'dnxte-project-dashboard', DIVI_ESSENTIAL_ASSETS . 'admin/js/project-dashboard.js', array( 'jquery' ), DIVI_ESSENTIAL_VERSION, true ); wp_localize_script( 'dnxte-project-dashboard', 'dnxte_project_dashboard', array( 'nonce' => wp_create_nonce( 'dnxte_project_enhance_nonce' ), 'ajaxurl' => admin_url( 'admin-ajax.php' ), ) ); } /** * Save project enhance settings via AJAX */ public function save_project_enhance_settings(): void { // Verify nonce if ( ! isset( $_POST['security'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['security'] ) ), 'dnxte_project_enhance_nonce' ) ) { wp_send_json_error( 'Invalid security token.' ); } // Check permissions if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( 'You do not have permission to update these settings.' ); } // Get current settings to compare for changes $current_options = get_option( self::PROJECT_ENHANCE_OPTION_KEY, array() ); // Sanitize and save options $options = array( 'dnxte_hide_projects' => isset( $_POST['dnxte_hide_projects'] ) ? (int) $_POST['dnxte_hide_projects'] : 0, 'dnxte_rename_projects_cpt' => isset( $_POST['dnxte_rename_projects_cpt'] ) ? (int) $_POST['dnxte_rename_projects_cpt'] : 0, 'dnxte_project_singular_name' => isset( $_POST['dnxte_project_singular_name'] ) ? sanitize_text_field( wp_unslash( $_POST['dnxte_project_singular_name'] ) ) : 'Project', 'dnxte_project_plural_name' => isset( $_POST['dnxte_project_plural_name'] ) ? sanitize_text_field( wp_unslash( $_POST['dnxte_project_plural_name'] ) ) : 'Projects', 'dnxte_project_slug' => isset( $_POST['dnxte_project_slug'] ) ? sanitize_title( wp_unslash( $_POST['dnxte_project_slug'] ) ) : 'project', 'dnxte_project_category_name' => isset( $_POST['dnxte_project_category_name'] ) ? sanitize_text_field( wp_unslash( $_POST['dnxte_project_category_name'] ) ) : 'Project Category', 'dnxte_project_category_slug' => isset( $_POST['dnxte_project_category_slug'] ) ? sanitize_title( wp_unslash( $_POST['dnxte_project_category_slug'] ) ) : 'project-category', 'dnxte_project_tag_name' => isset( $_POST['dnxte_project_tag_name'] ) ? sanitize_text_field( wp_unslash( $_POST['dnxte_project_tag_name'] ) ) : 'Project Tag', 'dnxte_project_tag_slug' => isset( $_POST['dnxte_project_tag_slug'] ) ? sanitize_title( wp_unslash( $_POST['dnxte_project_tag_slug'] ) ) : 'project-tag', 'dnxte_project_dashboard_icon' => isset( $_POST['dnxte_project_dashboard_icon'] ) ? sanitize_text_field( wp_unslash( $_POST['dnxte_project_dashboard_icon'] ) ) : 'dashicons-portfolio', ); // Check if slug has changed $slug_changed = false; if ( empty( $current_options ) || ( ( $current_options['dnxte_project_slug'] ?? 'project' ) !== $options['dnxte_project_slug'] ) || ( ( $current_options['dnxte_project_category_slug'] ?? 'project-category' ) !== $options['dnxte_project_category_slug'] ) || ( ( $current_options['dnxte_project_tag_slug'] ?? 'project-tag' ) !== $options['dnxte_project_tag_slug'] ) ) { $slug_changed = true; } // Update options update_option( self::PROJECT_ENHANCE_OPTION_KEY, $options ); // Flag that we need to flush rewrite rules if slug changed if ( $slug_changed ) { update_option( 'dnxte_flush_needed', true ); } wp_send_json_success( array( 'message' => 'Settings saved successfully. Please refresh the page to see changes.', ) ); } /** * Flush rewrite rules if needed */ public function maybe_flush_rewrite_rules(): void { if ( get_option( 'dnxte_flush_needed' ) ) { flush_rewrite_rules(); delete_option( 'dnxte_flush_needed' ); } } /** * Modify project CPT arguments * * This hooks into the Divi et_project_posttype_args filter */ public function modify_project_cpt_args( array $args ): array { $options = get_option( self::PROJECT_ENHANCE_OPTION_KEY, array() ); // If we should hide projects, set show_ui to false if ( ! empty( $options['dnxte_hide_projects'] ) ) { $args['show_ui'] = false; $args['show_in_menu'] = false; $args['show_in_admin_bar'] = false; $args['show_in_nav_menus'] = false; return $args; } // Check if we should rename the CPT if ( empty( $options['dnxte_rename_projects_cpt'] ) ) { return $args; } $singular = ! empty( $options['dnxte_project_singular_name'] ) ? $options['dnxte_project_singular_name'] : 'Project'; $plural = ! empty( $options['dnxte_project_plural_name'] ) ? $options['dnxte_project_plural_name'] : 'Projects'; $icon = ! empty( $options['dnxte_project_dashboard_icon'] ) ? $options['dnxte_project_dashboard_icon'] : 'dashicons-portfolio'; // Create a complete set of labels $args['labels'] = array( 'name' => $plural, 'singular_name' => $singular, 'menu_name' => $plural, 'name_admin_bar' => $singular, 'add_new' => esc_html__( 'Add New', 'et_builder' ), 'add_new_item' => sprintf( esc_html__( 'Add New %s', 'et_builder' ), $singular ), 'edit_item' => sprintf( esc_html__( 'Edit %s', 'et_builder' ), $singular ), 'new_item' => sprintf( esc_html__( 'New %s', 'et_builder' ), $singular ), 'all_items' => sprintf( esc_html__( 'All %s', 'et_builder' ), $plural ), 'view_item' => sprintf( esc_html__( 'View %s', 'et_builder' ), $singular ), 'search_items' => sprintf( esc_html__( 'Search %s', 'et_builder' ), $plural ), 'not_found' => esc_html__( 'Nothing found', 'et_builder' ), 'not_found_in_trash' => esc_html__( 'Nothing found in Trash', 'et_builder' ), 'parent_item_colon' => '', ); // Update menu icon if ( ! empty( $icon ) ) { $args['menu_icon'] = $icon; } return $args; } /** * Modify project rewrite arguments * * This hooks specifically into the Divi rewrite filter */ public function modify_project_rewrite_args( array $rewrite_args ): array { $options = get_option( self::PROJECT_ENHANCE_OPTION_KEY, array() ); // Only modify if we should rename if ( empty( $options['dnxte_rename_projects_cpt'] ) ) { return $rewrite_args; } $slug = ! empty( $options['dnxte_project_slug'] ) ? $options['dnxte_project_slug'] : 'project'; // Update rewrite rules $rewrite_args['slug'] = $slug; return $rewrite_args; } /** * Modify project taxonomy arguments */ public function modify_project_taxonomy_args( array $args, string $taxonomy ): array { // Only modify project taxonomies if ( ! in_array( $taxonomy, array( 'project_category', 'project_tag' ), true ) ) { return $args; } $options = get_option( self::PROJECT_ENHANCE_OPTION_KEY, array() ); // If we should hide projects, hide the taxonomies too if ( ! empty( $options['dnxte_hide_projects'] ) ) { $args['show_ui'] = false; $args['show_in_menu'] = false; $args['show_in_nav_menus'] = false; return $args; } // Check if we should rename the CPT and taxonomies if ( empty( $options['dnxte_rename_projects_cpt'] ) ) { return $args; } // Update taxonomy labels based on the custom taxonomy names if ( 'project_category' === $taxonomy ) { $cat_name = ! empty( $options['dnxte_project_category_name'] ) ? $options['dnxte_project_category_name'] : 'Project Category'; $cat_slug = ! empty( $options['dnxte_project_category_slug'] ) ? $options['dnxte_project_category_slug'] : 'project-category'; $args['labels'] = array( 'name' => $cat_name . 's', // Pluralize 'singular_name' => $cat_name, 'search_items' => "Search $cat_name" . 's', 'all_items' => "All $cat_name" . 's', 'parent_item' => "Parent $cat_name", 'parent_item_colon' => "Parent $cat_name:", 'edit_item' => "Edit $cat_name", 'update_item' => "Update $cat_name", 'add_new_item' => "Add New $cat_name", 'new_item_name' => "New $cat_name Name", 'menu_name' => $cat_name . 's', 'not_found' => sprintf( esc_html__( "You currently don't have any %s.", 'et_builder' ), $cat_name . 's' ), ); $args['rewrite'] = array( 'slug' => $cat_slug, 'with_front' => true, 'hierarchical' => true, ); } if ( 'project_tag' === $taxonomy ) { $tag_name = ! empty( $options['dnxte_project_tag_name'] ) ? $options['dnxte_project_tag_name'] : 'Project Tag'; $tag_slug = ! empty( $options['dnxte_project_tag_slug'] ) ? $options['dnxte_project_tag_slug'] : 'project-tag'; $args['labels'] = array( 'name' => $tag_name . 's', // Pluralize 'singular_name' => $tag_name, 'search_items' => "Search $tag_name" . 's', 'all_items' => "All $tag_name" . 's', 'parent_item' => "Parent $tag_name", 'parent_item_colon' => "Parent $tag_name:", 'edit_item' => "Edit $tag_name", 'update_item' => "Update $tag_name", 'add_new_item' => "Add New $tag_name", 'new_item_name' => "New $tag_name Name", 'menu_name' => $tag_name . 's', ); $args['rewrite'] = array( 'slug' => $tag_slug, 'with_front' => true, 'hierarchical' => false, ); } return $args; } } // Initialize the class new Project_Dashboard();
Save
Back