| 1 |
<?php |
| 2 |
/** |
| 3 |
* Get the value of a settings field. |
| 4 |
* |
| 5 |
* @param string $option settings field name |
| 6 |
* @param string $section the section name this field belongs to |
| 7 |
* @param string $default default text if it's not found |
| 8 |
* |
| 9 |
* @return mixed |
| 10 |
*/ |
| 11 |
function bbpc_get_opt( $option, $default = '' ) { |
| 12 |
$options = get_option( 'bbp_core_settings' ); |
| 13 |
|
| 14 |
if ( isset( $options[ $option ] ) ) { |
| 15 |
return $options[ $option ]; |
| 16 |
} |
| 17 |
|
| 18 |
return $default; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Check if a plugin has been installed for specific number of days |
| 23 |
* |
| 24 |
* @param string $plugin_path The plugin path (e.g. 'woocommerce/woocommerce.php') |
| 25 |
* @param int $days Number of days to check against |
| 26 |
* @return bool True if plugin is installed for specified days, false otherwise |
| 27 |
*/ |
| 28 |
function bbpc_is_plugin_installed_for_days( $days, $plugin_slug='eazydocs' ) { |
| 29 |
// Get the installation timestamp of the plugin |
| 30 |
$installed_time = get_option( $plugin_slug . '_installed' ); |
| 31 |
|
| 32 |
// Ensure it's a valid timestamp |
| 33 |
if ( ! is_numeric( $installed_time ) || $installed_time <= 0 ) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
// Convert days to seconds |
| 38 |
$required_time = (int) $days * DAY_IN_SECONDS; |
| 39 |
|
| 40 |
// Get the current UTC time |
| 41 |
$current_time = time(); |
| 42 |
|
| 43 |
// Check if the plugin has been installed for the required duration |
| 44 |
return ( $current_time - $installed_time ) >= $required_time; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Check If the Page is Forum page |
| 49 |
*/ |
| 50 |
function bbpc_is_forum_page() { |
| 51 |
if ( in_array( 'bbpress', get_body_class() ) ) { |
| 52 |
return true; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Check if the pro plugin and plan is active |
| 58 |
* |
| 59 |
* @return bool|void |
| 60 |
*/ |
| 61 |
function bbpc_is_premium() { |
| 62 |
if ( class_exists('BBPCorePro') && bc_fs()->can_use_premium_code() ) { |
| 63 |
return true; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* BBP Core Admin pages |
| 69 |
* If any of the admin pages match the current page, return true. |
| 70 |
* |
| 71 |
* @return bool|void |
| 72 |
*/ |
| 73 |
function bbpc_admin_pages($admin) { |
| 74 |
$current_url = ! empty( $_GET['page'] ) ? admin_url( 'admin.php?page=' ) . sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; |
| 75 |
|
| 76 |
if ( $admin == 'admin' ){ |
| 77 |
if ( $current_url == admin_url('admin.php?page=bbp-core') ) { |
| 78 |
return true; |
| 79 |
} |
| 80 |
} elseif ( $admin == 'settings' ) { |
| 81 |
if ( $current_url == admin_url('admin.php?page=bbp-core-settings') ) { |
| 82 |
return true; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* BBP Forum Assets |
| 89 |
* Checks if the current page is a single forum or a single topic. |
| 90 |
* |
| 91 |
* @return bool True if the current page is a single forum or topic, false otherwise. |
| 92 |
*/ |
| 93 |
function bbpc_forum_assets(){ |
| 94 |
if ( bbp_is_single_forum() || bbp_is_single_topic() ) { |
| 95 |
return true; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Posts Arraty |
| 102 |
* @param object Post Type |
| 103 |
*/ |
| 104 |
function bbp_core_get_posts( $post_type = 'forum' ) { |
| 105 |
$posts = get_pages( |
| 106 |
[ |
| 107 |
'post_type' => $post_type, |
| 108 |
'parent' => 0, |
| 109 |
] |
| 110 |
); |
| 111 |
|
| 112 |
$posts_array = []; |
| 113 |
|
| 114 |
if ( $posts ) { |
| 115 |
foreach ( $posts as $post ) { |
| 116 |
$posts_array[ $post->ID ] = $post->post_title; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return $posts_array; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Limit letter |
| 125 |
* @param $string |
| 126 |
* @param $limit_length |
| 127 |
* @param string $suffix |
| 128 |
*/ |
| 129 |
function bbp_core_limit_letter( $string, $limit_length, $suffix = '...' ) { |
| 130 |
if ( strlen( $string ) > $limit_length ) { |
| 131 |
echo strip_shortcodes( substr( $string, 0, $limit_length ) . $suffix ); |
| 132 |
} else { |
| 133 |
echo strip_shortcodes( esc_html( $string ) ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Return the topic view count. |
| 139 |
* |
| 140 |
* @param int $topic_id Optional. Topic id |
| 141 |
* |
| 142 |
* @return int The view count |
| 143 |
* @uses get_post_meta() To get the view count meta |
| 144 |
* @uses bbp_get_topic_id() To get the topic id |
| 145 |
*/ |
| 146 |
function bbp_get_topic_view_count( $topic_id = 0 ) { |
| 147 |
$topic_id = bbp_get_topic_id( $topic_id ); |
| 148 |
|
| 149 |
if ( empty( $topic_id ) ) { |
| 150 |
return 0; |
| 151 |
} |
| 152 |
|
| 153 |
$views = (int) get_post_meta( $topic_id, '_btv_view_count', true ); |
| 154 |
|
| 155 |
return $views; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Output the topic view count. |
| 160 |
* |
| 161 |
* @param int $topic_id Optional. Topic id |
| 162 |
* |
| 163 |
* @uses bbp_get_topic_id() To get the topic id |
| 164 |
* @uses btv_get_topic_view_count() To get the view count for the topic |
| 165 |
*/ |
| 166 |
function bbp_topic_view_count( $topic_id = 0 ) { |
| 167 |
$topic_id = bbp_get_topic_id( $topic_id ); |
| 168 |
$view_count = bbp_get_topic_view_count( $topic_id ); |
| 169 |
return $view_count; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
/** |
| 174 |
* Get forum title |
| 175 |
* @return string |
| 176 |
*/ |
| 177 |
function bbpc_forum_title(){ |
| 178 |
$forum_id = bbp_get_forum_id(); |
| 179 |
$forum_title = get_the_title( $forum_id ); |
| 180 |
return $forum_title; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Customizer section hide from customizer |
| 185 |
*/ |
| 186 |
add_action( 'customize_register', function( $wp_customize ) { |
| 187 |
// Unset the section you want to hide |
| 188 |
$wp_customize->remove_section( 'design_fields' ); |
| 189 |
}, 20 ); |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
add_action('wp_head', function(){ |
| 195 |
if ( is_singular( 'forum' ) ){ |
| 196 |
$id = get_the_ID(); |
| 197 |
|
| 198 |
|
| 199 |
$topics = get_children( [ |
| 200 |
'post_parent' => $id, |
| 201 |
'post_type' => 'topic' |
| 202 |
] ); |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
} |
| 207 |
}); |