= $required_time; } /** * Check If the Page is Forum page */ function forumax_is_forum_page() { if ( in_array( 'bbpress', get_body_class(), true ) ) { return true; } } /** * Turn on the WordPress visual editor for bbPress * * @param array $args * @return array */ function forumax_bbp_enable_visual_editor( $args = [] ) { $args['tinymce'] = true; return $args; } add_filter( 'bbp_after_get_the_content_parse_args', 'forumax_bbp_enable_visual_editor' ); /** * Check if the pro plugin and plan is active * * @return bool|void */ function forumax_is_premium() { if ( class_exists('Forumax_Pro') && bc_fs()->can_use_premium_code() ) { return true; } } /** * Check if the promax plan is active * * @return bool|void */ function forumax_is_promax() { if ( class_exists('Forumax_Pro') && bc_fs()->can_use_premium_code() && bc_fs()->is_plan('promax') ) { return true; } } /** * Forumax Admin pages * * Checks if the current admin page matches the specified Forumax page type. * * @param string $admin The admin page type to check ('admin', 'settings', 'dashboard'). * * @return bool True if on the specified admin page, false otherwise. */ function forumax_admin_pages( $admin ) { $current_url = ! empty( $_GET['page'] ) ? admin_url( 'admin.php?page=' ) . sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; if ( 'admin' === $admin ) { if ( admin_url( 'admin.php?page=forumax-builder' ) === $current_url ) { return true; } } elseif ( 'settings' === $admin ) { if ( admin_url( 'admin.php?page=forumax-settings' ) === $current_url ) { return true; } } elseif ( 'dashboard' === $admin ) { if ( admin_url( 'admin.php?page=forumax' ) === $current_url ) { return true; } } return false; } /** * BBP Forum Assets * Checks if the current page is a single forum or a single topic. * * @return bool True if the current page is a single forum or topic, false otherwise. */ function forumax_forum_and_topic_page(){ if ( bbp_is_single_forum() || bbp_is_single_topic() || bbp_is_reply_edit()) { return true; } } /** * Posts Arraty * @param object Post Type */ function forumax_get_posts( $post_type = 'forum' ) { $posts = get_pages( [ 'post_type' => $post_type, 'parent' => 0, ] ); $posts_array = []; if ( $posts ) { foreach ( $posts as $post ) { $posts_array[ $post->ID ] = $post->post_title; } } return $posts_array; } /** * Limit letter * @param $string * @param $limit_length * @param string $suffix */ function forumax_limit_letter( $string, $limit_length, $suffix = '...' ) { if ( strlen( $string ) > $limit_length ) { echo esc_html ( strip_shortcodes( substr( $string, 0, $limit_length ) . $suffix ) ); } else { echo esc_html( $string ); } } /** * Return the topic view count. * * @param int $topic_id Optional. Topic id * * @return int The view count * @uses get_post_meta() To get the view count meta * @uses bbp_get_topic_id() To get the topic id */ function forumax_get_topic_view_count( $topic_id = 0 ) { $topic_id = bbp_get_topic_id( $topic_id ); if ( empty( $topic_id ) ) { return 0; } $views = (int) get_post_meta( $topic_id, '_btv_view_count', true ); return $views; } /** * Output the topic view count. * * @param int $topic_id Optional. Topic id * * @uses bbp_get_topic_id() To get the topic id * @uses btv_get_topic_view_count() To get the view count for the topic */ function forumax_topic_view_count( $topic_id = 0 ) { $topic_id = bbp_get_topic_id( $topic_id ); $view_count = forumax_get_topic_view_count( $topic_id ); return $view_count; } /** * Increment the topic view count. * * Increments the view count when a visitor views a topic. * Uses cookies to prevent duplicate counts within a session (1 hour). * * @param int $topic_id Topic ID. * @return bool True if view was counted, false otherwise. */ function forumax_increment_topic_view_count( $topic_id = 0 ) { $topic_id = bbp_get_topic_id( $topic_id ); if ( empty( $topic_id ) ) { return false; } // Optionally: Don't count views for logged-in admins/moderators // Uncomment the following to exclude admin views from being counted // if ( current_user_can( 'moderate' ) ) { // return false; // } // Use a cookie to prevent duplicate counts within the same session $cookie_name = 'forumax_viewed_' . $topic_id; // Check if this topic was already viewed in this session if ( isset( $_COOKIE[ $cookie_name ] ) ) { return false; } // Get current view count $current_views = (int) get_post_meta( $topic_id, '_btv_view_count', true ); // Increment the view count $new_views = $current_views + 1; // Update the view count update_post_meta( $topic_id, '_btv_view_count', $new_views ); // Set cookie to prevent duplicate counting (expires in 1 hour) setcookie( $cookie_name, '1', time() + HOUR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true ); return true; } /** * Track topic views on template redirect. * * Hooks into bbPress template redirect to track topic views * when a single topic page is loaded. */ function forumax_track_topic_views() { // Only track on single topic pages if ( ! function_exists( 'bbp_is_single_topic' ) || ! bbp_is_single_topic() ) { return; } // Get the current topic ID $topic_id = bbp_get_topic_id(); if ( ! empty( $topic_id ) ) { forumax_increment_topic_view_count( $topic_id ); } } add_action( 'bbp_template_redirect', 'forumax_track_topic_views', 20 ); /** * Get forum title * @return string */ function forumax_forum_title(){ $forum_id = bbp_get_forum_id(); $forum_title = get_the_title( $forum_id ); return $forum_title; } /** * Customizer section hide from customizer */ add_action( 'customize_register', function( $wp_customize ) { // Unset the section you want to hide $wp_customize->remove_section( 'design_fields' ); }, 20 ); /** * Get all the registered menus */ function forumax_get_registered_nav_menus() { $menus = get_registered_nav_menus(); $menu_locations = []; $empty = [ '' => esc_html__('Select Menu Location', 'forumax') ]; foreach ( $menus as $location => $description ) { $menu_locations[ $location ] = $description; } return $empty + $menu_locations; } /** * Fix Gutenberg editor support for bbPress forum and topic post types. * * Filters the post type registration arguments to enable full REST API * support, ensure thumbnail support, and force correct labels so that * Gutenberg displays the proper post type name instead of "Document". * * @param array $args Array of arguments for registering a post type. * @param string $post_type Post type key. * @return array Modified arguments. */ function forumax_fix_post_type_args( $args, $post_type ) { $post_types_config = array( 'forum' => array( 'rest_base' => 'forums', 'labels' => array( 'name' => __( 'Forums', 'bbpress' ), 'singular_name' => __( 'Forum', 'bbpress' ), ), ), 'topic' => array( 'rest_base' => 'topics', 'labels' => array( 'name' => __( 'Topics', 'bbpress' ), 'singular_name' => __( 'Topic', 'bbpress' ), ), ), ); if ( ! isset( $post_types_config[ $post_type ] ) ) { return $args; } $config = $post_types_config[ $post_type ]; // Force full REST API support for Gutenberg. $args['show_in_rest'] = true; $args['rest_base'] = $config['rest_base']; $args['rest_controller_class'] = 'WP_REST_Posts_Controller'; // Set explicit label property (singular string). $args['label'] = $config['labels']['name']; // Ensure thumbnail support is included. if ( ! empty( $args['supports'] ) && is_array( $args['supports'] ) ) { if ( ! in_array( 'thumbnail', $args['supports'], true ) ) { $args['supports'][] = 'thumbnail'; } } else { $args['supports'] = array( 'title', 'editor', 'revisions', 'thumbnail' ); } // Merge labels to prevent Gutenberg "Document" fallback. if ( ! empty( $args['labels'] ) && is_array( $args['labels'] ) ) { $args['labels'] = array_merge( $args['labels'], $config['labels'] ); } else { $args['labels'] = $config['labels']; } return $args; } add_filter( 'register_post_type_args', 'forumax_fix_post_type_args', 99, 2 ); /** * Grant bbPress post type capabilities to WordPress administrators. * * Gutenberg requests the post type REST endpoint with context=edit, which * checks custom capabilities like edit_forums and edit_topics. WordPress * administrators may not have these caps unless bbPress has explicitly * assigned them. This filter dynamically grants the required caps to * any user who can manage_options (i.e. administrators). * * @param array $allcaps All capabilities for the user. * @param array $caps Required capabilities being checked. * @param array $args Additional arguments passed to the check. * @return array Modified capabilities array. */ function forumax_grant_bbpress_caps_to_admins( $allcaps, $caps, $args ) { // Only grant to users who can manage options (administrators). if ( empty( $allcaps['manage_options'] ) ) { return $allcaps; } // bbPress forum and topic capabilities needed for REST API access. $bbpress_caps = array( 'edit_forums', 'edit_others_forums', 'publish_forums', 'read_private_forums', 'read_hidden_forums', 'delete_forums', 'delete_others_forums', 'edit_topics', 'edit_others_topics', 'publish_topics', 'read_private_topics', 'delete_topics', 'delete_others_topics', ); foreach ( $bbpress_caps as $cap ) { $allcaps[ $cap ] = true; } return $allcaps; } add_filter( 'user_has_cap', 'forumax_grant_bbpress_caps_to_admins', 10, 3 ); /** * Mutual Deactivation of Forumax and bbp-core plugins */ add_action( 'activated_plugin', function( $plugin ) { if ( ! function_exists( 'is_plugin_active' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } // All Forumax plugins $forumax_plugins = [ 'forumax/forumax.php', 'forumax-pro/forumax.php', ]; // All bbp-core plugins $bbp_core_plugins = [ 'bbp-core/bbp-core.php', 'bbp-core-pro/bbp-core.php', ]; // If activating Forumax (free or pro) → deactivate bbp-core (all versions) if ( in_array( $plugin, $forumax_plugins, true ) ) { deactivate_plugins( $bbp_core_plugins ); } // If activating bbp-core (free or pro) → deactivate Forumax (all versions) if ( in_array( $plugin, $bbp_core_plugins, true ) ) { deactivate_plugins( $forumax_plugins ); } } ); /** * Register Forum Sidebar widget area * This makes the Forum Sidebar available for any theme, not just theme-specific implementations */ add_action( 'widgets_init', function () { global $wp_registered_sidebars; // Check if the sidebar is already registered by the theme (e.g., Docy) if ( isset( $wp_registered_sidebars['forum_archive_sidebar'] ) ) { return; } register_sidebar( [ 'name' => esc_html__( 'Forumax Sidebar', 'forumax' ), 'description' => esc_html__( 'Add widgets here for the Forumax Sidebar area', 'forumax' ), 'id' => 'forum_archive_sidebar', 'before_widget' => '
', 'before_title' => '