PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 1.2.9
Forumax – AI Powered Advanced Community Forum Plugin v1.2.9
2.4.4 2.4.3 2.4.2 2.4.1 2.4.0 trunk 1.0.8 1.1.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 2.0.0 2.1.0 All 29 releases
bbp-core / includes / features / bbp_voting / helpers.php

helpers.php in Forumax – AI Powered Advanced Community Forum Plugin 1.2.9, at includes/features/bbp_voting/helpers.php

159 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly.
4 }
5
6 if ( ! function_exists( 'bbp_voting_get_plugin_install_link' ) ) {
7 function bbp_voting_get_plugin_install_link( $plugin, $action = 'install-plugin' ) {
8 return esc_url(
9 wp_nonce_url(
10 add_query_arg(
11 [
12 'action' => $action,
13 'plugin' => $plugin,
14 ],
15 admin_url( 'update.php' )
16 ),
17 $action . '_' . $plugin
18 )
19 );
20 }
21 }
22
23
24 if ( ! function_exists( 'bbp_voting_get_plugin_activate_link' ) ) {
25 function bbp_voting_get_plugin_activate_link( $plugin, $action = 'activate' ) {
26 if ( strpos( $plugin, '/' ) ) {
27 $plugin = str_replace( '\/', '%2F', $plugin );
28 }
29 $url = sprintf( admin_url( 'plugins.php?action=' . $action . '&plugin=%s&plugin_status=all&paged=1&s' ), $plugin );
30 $_REQUEST['plugin'] = $plugin;
31 $url = wp_nonce_url( $url, $action . '-plugin_' . $plugin );
32 return $url;
33 }
34 }
35
36
37 if ( ! function_exists( 'bbp_voting_field' ) ) {
38 function bbp_voting_field( $key, $name, $label = '', $description = '', $type = 'bool', $pro = false ) {
39 $name_and_id = 'name="' . $key . '" id="' . $key . '"';
40 $teaser = defined( 'BBPVOTINGPRO' ) ? false : $pro;
41 $attributes = $teaser ? 'disabled' : $name_and_id;
42 ?>
43 <tr valign="top">
44 <th scope="row">
45 <?php echo $name; ?>:
46 <?php if ( $pro ) { ?>
47 <a href="https://wpforthewin.com/product/bbpress-voting-pro/" target="_blank"><span class="bbp-voting-pro-badge bbp-voting-pro-green">Pro</span></a>
48 <?php } ?>
49 </th>
50 <td>
51 <?php if ( $type == 'bool' ) { ?>
52 <input type="checkbox" <?php echo $attributes; ?> value="true"
53 <?php
54 if ( get_option( $key ) == 'true' ) {
55 echo 'checked';}
56 ?>
57 />
58 <?php } elseif ( is_array( $type ) ) { ?>
59 <select <?php echo $attributes; ?>>
60 <?php foreach ( $type as $option ) { ?>
61 <option value="<?php echo $option; ?>"
62 <?php
63 if ( get_option( $key ) == $option ) {
64 echo ' selected';}
65 ?>
66 ><?php echo ucwords( str_replace( '-', ' ', $option ) ); ?></option>
67 <?php } ?>
68 </select>
69 <?php } else { ?>
70 <input type="<?php echo $type; ?>" <?php echo $attributes; ?> value="<?php echo get_option( $key ); ?>" data-lpignore="true" />
71 <?php } ?>
72 <?php if ( ! empty( $label ) ) { ?>
73 <label for="<?php echo $key; ?>"><?php echo $label; ?></label>
74 <?php } ?>
75 <?php if ( ! empty( $description ) ) { ?>
76 <p class="description"><?php echo $description; ?></p>
77 <?php } ?>
78 </td>
79 </tr>
80 <?php
81 }
82 }
83
84 if ( ! function_exists( 'bbp_voting_get_current_post_type' ) ) {
85 function bbp_voting_get_current_post_type() {
86 $this_post_id = bbp_get_reply_id() ?: bbp_get_topic_id();
87 return get_post_type( $this_post_id );
88 }
89 }
90
91 if ( ! function_exists( 'bbp_voting_get_post_type_by_id' ) ) {
92 function bbp_voting_get_post_type_by_id( $post_id ) {
93 $this_post_id = bbp_get_reply_id( $post_id ) ?: bbp_get_topic_id( $post_id );
94 return get_post_type( $this_post_id );
95 }
96 }
97
98 if ( ! class_exists( 'WilsonConfidenceIntervalCalculator' ) ) {
99 class WilsonConfidenceIntervalCalculator {
100 /**
101 * Computed value for confidence (z)
102 *
103 * These values were computed using Ruby's Statistics2.pnormaldist function
104 * 1.645 = 90% confidence
105 * 1.959964 = 95.0% confidence
106 * 2.241403 = 97.5% confidence
107 */
108 public function getScore( int $positiveVotes, int $totalVotes, float $confidence = 1.645 ) : float {
109 return (float) $totalVotes ? $this->lowerBound( $positiveVotes, $totalVotes, $confidence ) : 0;
110 }
111 private function lowerBound( int $positiveVotes, int $totalVotes, float $confidence ) : float {
112 $phat = 1.0 * $positiveVotes / $totalVotes;
113 $numerator = $this->calculationNumerator( $totalVotes, $confidence, $phat );
114 $denominator = $this->calculationDenominator( $totalVotes, $confidence );
115 return $numerator / $denominator;
116 }
117 private function calculationDenominator( int $total, float $z ) : float {
118 return 1 + $z * $z / $total;
119 }
120 private function calculationNumerator( int $total, float $z, float $phat ) : float {
121 return $phat + $z * $z / ( 2 * $total ) - $z * sqrt( ( $phat * ( 1 - $phat ) + $z * $z / ( 4 * $total ) ) / $total );
122 }
123 }
124 }
125
126
127 if ( ! function_exists( 'bbp_voting_parse_args' ) ) {
128 function bbp_voting_parse_args( $args, $defaults = [], $filter_key = '' ) {
129
130 // Setup a temporary array from $args
131 if ( is_object( $args ) ) {
132 $r = get_object_vars( $args );
133 } elseif ( is_array( $args ) ) {
134 $r =& $args;
135 } else {
136 $r = [];
137 wp_parse_str( $args, $r );
138 }
139
140 // Passively filter the args before the parse
141 if ( ! empty( $filter_key ) ) {
142 $r = apply_filters( "bbp_voting_before_{$filter_key}_parse_args", $r, $args, $defaults );
143 }
144
145 // Parse
146 if ( is_array( $defaults ) && ! empty( $defaults ) ) {
147 $r = array_merge( $defaults, $r );
148 }
149
150 // Aggressively filter the args after the parse
151 if ( ! empty( $filter_key ) ) {
152 $r = apply_filters( "bbp_voting_after_{$filter_key}_parse_args", $r, $args, $defaults );
153 }
154
155 // Return the parsed results
156 return $r;
157 }
158 }
159