[ 'name' => 'Directories', 'singular_name' => 'Directory', ], 'public' => false, 'show_ui' => false, 'show_in_menu' => false, 'supports' => [ 'title' ], 'has_archive' => false, 'rewrite' => false, ]); } add_action( 'init', 'abddir_register_directory_cpt' ); /** * Default settings for any directory. */ function abddir_directory_defaults() { return [ 'type' => 'business', 'default_layout' => 'grid', 'cards_per_row' => 3, 'listings_per_page' => 12, 'show_search' => 1, 'card_style' => 'shadow', 'address_format' => 'us', ]; } /** * Get all directories (ordered by title). * * @return WP_Post[] */ function abddir_get_directories() { return get_posts([ 'post_type' => 'abddir_directory', 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', ]); } /** * Get settings for a specific directory, merged with defaults. * * @param int $dir_id Directory post ID. Pass 0 to get pure defaults. * @return array */ function abddir_get_directory_settings( $dir_id ) { $defaults = abddir_directory_defaults(); if ( ! $dir_id ) return $defaults; $meta = get_post_meta( $dir_id, 'abddir_dir_settings', true ); if ( ! is_array( $meta ) ) $meta = []; return wp_parse_args( $meta, $defaults ); } /** * Sanitize and save settings for a specific directory. * * @param int $dir_id * @param array $data Raw POST data (will be sanitized here). */ function abddir_save_directory_settings( $dir_id, $data ) { $clean = [ 'type' => sanitize_text_field( $data['type'] ?? 'business' ), 'default_layout' => sanitize_text_field( $data['default_layout'] ?? 'grid' ), 'cards_per_row' => intval( $data['cards_per_row'] ?? 3 ), 'listings_per_page' => intval( $data['listings_per_page'] ?? 12 ), 'show_search' => ! empty( $data['show_search'] ) ? 1 : 0, 'card_style' => sanitize_text_field( $data['card_style'] ?? 'shadow' ), 'address_format' => in_array( $data['address_format'] ?? 'us', [ 'us', 'international' ] ) ? sanitize_text_field( $data['address_format'] ) : 'us', ]; update_post_meta( $dir_id, 'abddir_dir_settings', $clean ); } /** * Get the published listing count for a directory. * * @param int $dir_id * @return int */ function abddir_get_directory_listing_count( $dir_id ) { $q = new WP_Query([ 'post_type' => 'abddir_listing', 'post_status' => 'publish', 'posts_per_page' => -1, 'fields' => 'ids', 'meta_query' => [[ 'key' => 'abddir_directory_id', 'value' => intval( $dir_id ), ]], ]); return $q->found_posts; } /** * Get the ID of the first available directory. * Used by the shortcode when no id= attribute is provided. * * @return int Directory post ID, or 0 if none exist. */ function abddir_get_first_directory_id() { $dirs = abddir_get_directories(); return ! empty( $dirs ) ? $dirs[0]->ID : 0; } /** * Create a new directory with the given name and type. * * @param string $name Directory display name. * @param string $type 'business' | 'member' | 'employee' * @return int|WP_Error New directory post ID on success. */ function abddir_create_directory( $name, $type = 'business' ) { $id = wp_insert_post([ 'post_type' => 'abddir_directory', 'post_title' => sanitize_text_field( $name ), 'post_status' => 'publish', ]); if ( is_wp_error( $id ) ) return $id; $defaults = abddir_directory_defaults(); $defaults['type'] = sanitize_text_field( $type ); update_post_meta( $id, 'abddir_dir_settings', $defaults ); return $id; } /** * Delete a directory and ALL of its listings (permanent delete). * * @param int $dir_id */ function abddir_delete_directory_and_listings( $dir_id ) { $listings = get_posts([ 'post_type' => 'abddir_listing', 'post_status' => 'any', 'posts_per_page' => -1, 'fields' => 'ids', 'meta_query' => [[ 'key' => 'abddir_directory_id', 'value' => intval( $dir_id ), ]], ]); foreach ( $listings as $lid ) { wp_delete_post( $lid, true ); } wp_delete_post( $dir_id, true ); } /** * Human-readable label for a directory type slug. * * @param string $type * @return string */ function abddir_directory_type_label( $type ) { $labels = [ 'business' => 'Business', 'member' => 'Member / Group', 'employee' => 'Employee', ]; return $labels[ $type ] ?? ucfirst( $type ); } // ─── Migration: single-directory → multi-directory ──────────────────────────── /** * Runs once on admin_init for any administrator. * * If no abddir_directory posts exist yet, creates a "Default Directory" that * inherits the current global abddir_settings, then assigns every existing * abddir_listing (which has no abddir_directory_id) to that directory. * * Safe to run repeatedly — guarded by the abddir_migration_done option. */ function abddir_maybe_migrate_to_multi_directory() { if ( ! current_user_can( 'manage_options' ) ) return; if ( get_option( 'abddir_migration_done' ) ) return; // If directories already exist, just mark done and return $existing = get_posts([ 'post_type' => 'abddir_directory', 'post_status' => 'publish', 'posts_per_page' => 1, 'fields' => 'ids', ]); if ( ! empty( $existing ) ) { update_option( 'abddir_migration_done', 1 ); return; } // Pull current global settings to seed the first directory $global = get_option( 'abddir_settings', [] ); $global_type = sanitize_text_field( $global['directory_type'] ?? 'business' ); $dir_id = abddir_create_directory( 'Default Directory', $global_type ); if ( is_wp_error( $dir_id ) ) return; // Merge global settings into the new directory's meta $defaults = abddir_directory_defaults(); $merged = wp_parse_args( $global, $defaults ); update_post_meta( $dir_id, 'abddir_dir_settings', $merged ); // Assign all orphaned listings (no abddir_directory_id) to this directory $orphans = get_posts([ 'post_type' => 'abddir_listing', 'post_status' => 'any', 'posts_per_page' => -1, 'fields' => 'ids', 'meta_query' => [[ 'key' => 'abddir_directory_id', 'compare' => 'NOT EXISTS', ]], ]); foreach ( $orphans as $listing_id ) { update_post_meta( $listing_id, 'abddir_directory_id', $dir_id ); } update_option( 'abddir_migration_done', 1 ); // Backfill install date if missing (existing installs before this was tracked) if ( ! get_option('abddir_install_date') ) { update_option( 'abddir_install_date', current_time('timestamp') ); } } add_action( 'admin_init', 'abddir_maybe_migrate_to_multi_directory' ); // ── Back to Listings button on the listing edit screen ──────────────────────── function abddir_back_to_listings_button() { $screen = get_current_screen(); if ( ! $screen || $screen->post_type !== 'abddir_listing' || $screen->base !== 'post' ) return; // Link back to the specific directory this listing belongs to. // Prefer dir_id passed in the URL (set by our Edit links in admin-page.php). // Fall back to post meta so manually-opened edit screens still work. $dir_id = isset( $_GET['dir_id'] ) ? intval( $_GET['dir_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only, used only to build a back-link URL if ( ! $dir_id ) { $post_id = isset( $_GET['post'] ) ? intval( $_GET['post'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only, used only to build a back-link URL $dir_id = $post_id ? intval( get_post_meta( $post_id, 'abddir_directory_id', true ) ) : 0; } if ( $dir_id ) { $url = add_query_arg([ 'page' => 'abddir-dashboard', 'view' => 'dir', 'dir_id' => $dir_id, ], admin_url('admin.php') ); } else { $url = admin_url('admin.php?page=abddir-dashboard'); } $abddir_back_btn_js = ' document.addEventListener("DOMContentLoaded", function() { var existing = document.querySelector(".wrap .page-title-action"); if ( ! existing ) return; var btn = document.createElement("a"); btn.href = ' . wp_json_encode( $url ) . '; btn.className = "page-title-action"; btn.textContent = "\u2190 Back to Listings"; existing.parentNode.insertBefore( btn, existing ); }); '; wp_add_inline_script( 'abddir-admin-js', $abddir_back_btn_js ); } add_action( 'admin_head', 'abddir_back_to_listings_button' ); // ── Default sort abddir_listing list by title A→Z ──────────────────────────────── function abddir_listing_default_sort( $query ) { if ( ! is_admin() || ! $query->is_main_query() ) return; if ( $query->get('post_type') !== 'abddir_listing' ) return; if ( ! $query->get('orderby') ) { $query->set( 'orderby', 'title' ); $query->set( 'order', 'ASC' ); } } add_action( 'pre_get_posts', 'abddir_listing_default_sort' );