array( 'title' => 'Login', 'slug' => 'aimslms-login', 'shortcode' => '[aimslms_login]', ), 'aimslms_register_page' => array( 'title' => 'Register', 'slug' => 'aimslms-register', 'shortcode' => '[aimslms_register]', ), 'aimslms_forgot_page' => array( 'title' => 'Forgot Password', 'slug' => 'aimslms-forgot-password', 'shortcode' => '[aimslms_forgot_password]', ), 'aimslms_account_page' => array( 'title' => 'My Account', 'slug' => 'aimslms-my-account', 'shortcode' => '[aimslms_student_dashboard]', ), 'aimslms_courses_page' => array( 'title' => 'Courses', 'slug' => 'aimslms-courses', 'shortcode' => '[aimslms_courses]', ), 'aimslms_checkout_page' => array( 'title' => 'Checkout', 'slug' => 'aimslms-checkout', 'shortcode' => '[aimslms_checkout]', ), 'aimslms_order_received_page' => array( 'title' => 'Order Received', 'slug' => 'aimslms-order-received', 'shortcode' => '[aimslms_order_received]', ), ); } /** * Create pages on plugin activation */ public static function create_pages() { foreach ( self::get_pages() as $option_key => $page ) { $page_id = get_option( $option_key ); if ( $page_id && get_post( $page_id ) ) { continue; } $existing = get_page_by_path( $page['slug'] ); if ( $existing ) { update_option( $option_key, $existing->ID ); continue; } $new_page_id = wp_insert_post( array( 'post_title' => wp_strip_all_tags( $page['title'] ), 'post_name' => $page['slug'], 'post_content' => $page['shortcode'], 'post_status' => 'publish', 'post_type' => 'page', ) ); if ( ! is_wp_error( $new_page_id ) ) { update_option( $option_key, $new_page_id ); } } } /** * Option keys (from get_pages()) that should be linked from the site's * navigation menu. The other auto-created pages (login, register, * forgot password, checkout, order received) stay accessible by URL * but are intentionally left out of the menu. */ private static function get_menu_option_keys() { return array( 'aimslms_account_page', 'aimslms_courses_page' ); } /** * Get the post IDs of the auto-created pages that should stay * URL-accessible but never appear in any automatic page listing * (nav menus, the Page List block, wp_list_pages() widgets, etc.). * * @return int[] Page post IDs. */ private static function get_utility_page_ids() { $menu_keys = self::get_menu_option_keys(); $ids = array(); foreach ( self::get_pages() as $option_key => $page ) { if ( in_array( $option_key, $menu_keys, true ) ) { continue; } $page_id = get_option( $option_key ); if ( $page_id ) { $ids[] = (int) $page_id; } } return $ids; } /** * Filters `wp_list_pages_excludes` (classic Pages widgets, wp_list_pages()) * to keep the plugin's utility pages out of the listing on the front end. * * @param array $excludes Page IDs already excluded. * @return array Page IDs to exclude, with the utility pages added. */ public static function exclude_utility_page_ids( $excludes ) { if ( is_admin() ) { return $excludes; } return array_merge( (array) $excludes, self::get_utility_page_ids() ); } /** * Filters `get_pages()` results (used by the core Page List block, * wp_dropdown_pages(), etc.) to strip out the plugin's utility pages * on the front end, without affecting the admin page pickers. * * @param WP_Post[] $pages Pages returned by get_pages(). * @param array $parsed_args Arguments passed to get_pages(). * @return WP_Post[] Filtered pages list. */ public static function filter_get_pages( $pages, $parsed_args = array() ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- required by the get_pages filter signature. if ( is_admin() || empty( $pages ) ) { return $pages; } $utility_ids = self::get_utility_page_ids(); if ( empty( $utility_ids ) ) { return $pages; } return array_values( array_filter( $pages, static function ( $page ) use ( $utility_ids ) { return ! in_array( (int) $page->ID, $utility_ids, true ); } ) ); } /** * Self-heals menus on sites where `aimslms_menu_items_initialized` was * already set before this menu-scoping logic existed (so utility pages * may have been linked into a classic menu or block navigation by an * older version of the plugin). Runs once. */ public static function maybe_cleanup_menus() { if ( get_option( 'aimslms_menu_cleanup_v1' ) ) { return; } self::remove_utility_pages_from_menus(); update_option( 'aimslms_menu_cleanup_v1', 1 ); } /** * Removes any menu item (classic nav menu or block wp_navigation entity) * that links to one of the plugin's utility pages, leaving My Account * and Courses (and anything the site owner added themselves) untouched. */ public static function remove_utility_pages_from_menus() { $utility_ids = self::get_utility_page_ids(); if ( empty( $utility_ids ) ) { return; } // Classic menus. $menus = wp_get_nav_menus(); foreach ( $menus as $menu ) { $items = wp_get_nav_menu_items( $menu->term_id ); if ( ! $items ) { continue; } foreach ( $items as $item ) { if ( 'post_type' === $item->type && 'page' === $item->object && in_array( (int) $item->object_id, $utility_ids, true ) ) { wp_delete_post( $item->ID, true ); } } } // Block-theme wp_navigation entities. $navigations = get_posts( array( 'post_type' => 'wp_navigation', 'post_status' => array( 'publish', 'draft' ), 'numberposts' => -1, ) ); foreach ( $navigations as $navigation ) { $content = $navigation->post_content; $updated = preg_replace_callback( '//', static function ( $matches ) use ( $utility_ids ) { $block_attrs = json_decode( $matches[1], true ); if ( isset( $block_attrs['id'] ) && in_array( (int) $block_attrs['id'], $utility_ids, true ) ) { return ''; } return $matches[0]; }, $content ); if ( null !== $updated && $updated !== $content ) { wp_update_post( array( 'ID' => $navigation->ID, 'post_content' => $updated, ) ); } } } /** * Add the My Account and Courses pages to the site's navigation. * Runs once on activation; does nothing on later activations so a site * owner's own menu edits (e.g. removing an item) are respected. * * Handles both classic themes (a classic wp_nav_menu, usable via * Appearance → Menus or a Legacy Menu block) and block/FSE themes * (a dedicated "Aims Infosoft LMS Menu" wp_navigation entity, selectable * from any Navigation block). Neither path touches a theme's existing * menu/navigation - a site using the default Page List block in its * header will still show every page there; swapping that block for a * Navigation block pointed at "Aims Infosoft LMS Menu" is a template * decision left to the site owner. */ public static function add_pages_to_menu() { if ( get_option( 'aimslms_menu_items_initialized' ) ) { return; } if ( current_theme_supports( 'menus' ) ) { self::add_pages_to_classic_menu(); } if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) { self::add_pages_to_block_navigation(); } update_option( 'aimslms_menu_items_initialized', 1 ); } /** * Add My Account + Courses to a classic wp_nav_menu. */ private static function add_pages_to_classic_menu() { $menu_id = self::get_or_create_target_menu(); if ( ! $menu_id ) { return; } $existing_items = wp_get_nav_menu_items( $menu_id ); $linked_page_ids = array(); if ( $existing_items ) { foreach ( $existing_items as $item ) { if ( 'post_type' === $item->type && 'page' === $item->object ) { $linked_page_ids[] = (int) $item->object_id; } } } foreach ( self::get_menu_option_keys() as $option_key ) { $page_id = get_option( $option_key ); if ( ! $page_id || ! get_post( $page_id ) ) { continue; } if ( in_array( (int) $page_id, $linked_page_ids, true ) ) { continue; } wp_update_nav_menu_item( $menu_id, 0, array( 'menu-item-object-id' => $page_id, 'menu-item-object' => 'page', 'menu-item-type' => 'post_type', 'menu-item-title' => get_the_title( $page_id ), 'menu-item-status' => 'publish', ) ); } } /** * Add My Account + Courses to a dedicated "Aims Infosoft LMS Menu" * wp_navigation entity, without touching whatever navigation the theme * is already using. */ private static function add_pages_to_block_navigation() { $nav_title = __( 'Aims Infosoft LMS Menu', 'aims-infosoft-lms' ); $existing = get_posts( array( 'post_type' => 'wp_navigation', 'post_status' => array( 'publish', 'draft' ), 'title' => $nav_title, 'numberposts' => 1, ) ); $nav_post_id = $existing ? $existing[0]->ID : 0; $content = $nav_post_id ? $existing[0]->post_content : ''; foreach ( self::get_menu_option_keys() as $option_key ) { $page_id = get_option( $option_key ); if ( ! $page_id || ! get_post( $page_id ) ) { continue; } if ( false !== strpos( $content, '"id":' . (int) $page_id . ',' ) || false !== strpos( $content, '"id":' . (int) $page_id . '}' ) ) { continue; } $content .= sprintf( '', wp_json_encode( array( 'label' => get_the_title( $page_id ), 'type' => 'page', 'id' => (int) $page_id, 'url' => get_permalink( $page_id ), 'kind' => 'post-type', ) ) ); } if ( $nav_post_id ) { wp_update_post( array( 'ID' => $nav_post_id, 'post_content' => $content, ) ); } else { wp_insert_post( array( 'post_type' => 'wp_navigation', 'post_title' => $nav_title, 'post_content' => $content, 'post_status' => 'publish', ) ); } } /** * Find a menu to add items to: the menu already assigned to the * 'primary' theme location if one exists, otherwise the menu assigned * to the first registered location, otherwise a newly created menu * (left unassigned to any location so nothing existing is overridden). * * @return int|false Menu term ID, or false if none could be found/created. */ private static function get_or_create_target_menu() { $locations = get_nav_menu_locations(); if ( ! empty( $locations['primary'] ) ) { return (int) $locations['primary']; } if ( ! empty( $locations ) ) { return (int) reset( $locations ); } $menu_name = __( 'Aims Infosoft LMS Menu', 'aims-infosoft-lms' ); $existing = wp_get_nav_menu_object( $menu_name ); if ( $existing ) { return (int) $existing->term_id; } $menu_id = wp_create_nav_menu( $menu_name ); if ( is_wp_error( $menu_id ) ) { return false; } $registered_locations = get_registered_nav_menus(); if ( ! empty( $registered_locations ) ) { $first_location = array_key_first( $registered_locations ); $locations = get_nav_menu_locations(); $locations[ $first_location ] = $menu_id; set_theme_mod( 'nav_menu_locations', $locations ); } return (int) $menu_id; } /** * Replace the content of every auto-created page with a friendly * "plugin unavailable" notice, backing up the original shortcode * content first so it can be restored if the plugin is reactivated. * Does not delete any page, option, or other data. */ public static function show_unavailable_notice() { $message = '

' . esc_html__( 'This plugin is not available or has been deactivated. Please contact the site administrator.', 'aims-infosoft-lms' ) . '

'; foreach ( self::get_pages() as $option_key => $page ) { $page_id = get_option( $option_key ); if ( ! $page_id || ! get_post( $page_id ) ) { continue; } if ( get_post_meta( $page_id, '_aimslms_notice_active', true ) ) { continue; } $post = get_post( $page_id ); update_post_meta( $page_id, '_aimslms_original_content', $post->post_content ); update_post_meta( $page_id, '_aimslms_notice_active', 1 ); wp_update_post( array( 'ID' => $page_id, 'post_content' => $message, ) ); } } /** * Restore the original shortcode content on any auto-created page that * currently has the "plugin unavailable" notice showing. Runs on * (re)activation so previously deactivated sites come back working. */ public static function restore_pages() { foreach ( self::get_pages() as $option_key => $page ) { $page_id = get_option( $option_key ); if ( ! $page_id || ! get_post( $page_id ) ) { continue; } if ( ! get_post_meta( $page_id, '_aimslms_notice_active', true ) ) { continue; } $original_content = get_post_meta( $page_id, '_aimslms_original_content', true ); wp_update_post( array( 'ID' => $page_id, 'post_content' => ( '' !== $original_content ) ? $original_content : $page['shortcode'], ) ); delete_post_meta( $page_id, '_aimslms_notice_active' ); delete_post_meta( $page_id, '_aimslms_original_content' ); } } }