From 9fb76e0c4d07d05b9eebee2467027655d2b0504b Mon Sep 17 00:00:00 2001 From: Matt Bernhardt Date: Fri, 17 Mar 2023 17:28:46 -0400 Subject: [PATCH] Abstraction rabbit hole This demonstrates an apparent way to get around the code repetition in the mitlib-post-FOO plugins which descend from the base mitlib-post plugin. --- .gitignore | 1 + .../mu-plugins/mitlib-post/src/class-base.php | 45 +++++++++++++++++++ .../mitlib-post-notebooks.php | 26 +++++++++++ .../src/class-notebook.php | 24 ++++++++++ 4 files changed, 96 insertions(+) create mode 100644 web/app/plugins/mitlib-post-notebooks/mitlib-post-notebooks.php create mode 100644 web/app/plugins/mitlib-post-notebooks/src/class-notebook.php diff --git a/.gitignore b/.gitignore index 6f398e70..734fd12e 100644 --- a/.gitignore +++ b/.gitignore @@ -82,6 +82,7 @@ web/app/plugins/* !web/app/plugins/mitlib-post-exhibits !web/app/plugins/mitlib-post-experts !web/app/plugins/mitlib-post-locations +!web/app/plugins/mitlib-post-notebooks !web/app/plugins/mitlib-pull-events !web/app/plugins/mitlib-pull-hours diff --git a/web/app/mu-plugins/mitlib-post/src/class-base.php b/web/app/mu-plugins/mitlib-post/src/class-base.php index 82437a4f..f027da85 100644 --- a/web/app/mu-plugins/mitlib-post/src/class-base.php +++ b/web/app/mu-plugins/mitlib-post/src/class-base.php @@ -12,6 +12,51 @@ * Defines the Base class for custom post types. */ class Base { + /** + * Generic define function that relies on constants defined in descendant + * classes. + */ + public static function generic() { + $labels = array( + 'name' => _x( static::PLURAL, 'Post Type General Name', 'text_domain' ), + 'singular_name' => _x( static::SINGULAR, 'Post Type Singular Name', 'text_domain' ), + 'menu_name' => __( static::PLURAL, 'text_domain' ), + 'name_admin_bar' => __( static::SINGULAR, 'text_domain' ), + 'parent_item_colon' => __( static::SINGULAR, 'text_domain' ), + 'all_items' => __( 'All ' . static::PLURAL, 'text_domain' ), + 'add_new_item' => __( 'Add New ' . static::SINGULAR, 'text_domain' ), + 'add_new' => __( 'New ' . static::SINGULAR, 'text_domain' ), + 'new_item' => __( 'New ' . static::SINGULAR, 'text_domain' ), + 'edit_item' => __( 'Edit ' . static::SINGULAR, 'text_domain' ), + 'update_item' => __( 'Update ' . static::SINGULAR, 'text_domain' ), + 'view_item' => __( 'View ' . static::SINGULAR, 'text_domain' ), + 'search_items' => __( 'Search ' . static::PLURAL, 'text_domain' ), + 'not_found' => __( 'No ' . static::PLURAL . ' found', 'text_domain' ), + 'not_found_in_trash' => __( 'No ' . static::PLURAL . ' found in Trash', 'text_domain' ), + ); + $args = array( + 'label' => __( static::SINGULAR, 'text_domain' ), + 'description' => __( static::SINGULAR, 'text_domain' ), + 'labels' => $labels, + 'supports' => array( 'title', 'editor', 'page-attributes', 'thumbnail' ), + 'taxonomies' => array( 'category', 'post_tag', 'event' ), + 'hierarchical' => true, + 'public' => true, + 'show_ui' => true, + 'show_in_menu' => true, + 'menu_position' => 6, + 'menu_icon' => 'dashicons-calendar-alt', + 'show_in_admin_bar' => true, + 'show_in_nav_menus' => true, + 'can_export' => true, + 'has_archive' => true, + 'exclude_from_search' => false, + 'publicly_queryable' => true, + 'capability_type' => 'post', + ); + register_post_type( strtolower( static::SINGULAR ), $args ); + } + /** * Called during acf/settings/load_json * diff --git a/web/app/plugins/mitlib-post-notebooks/mitlib-post-notebooks.php b/web/app/plugins/mitlib-post-notebooks/mitlib-post-notebooks.php new file mode 100644 index 00000000..65e3f3c8 --- /dev/null +++ b/web/app/plugins/mitlib-post-notebooks/mitlib-post-notebooks.php @@ -0,0 +1,26 @@ +