| 1 |
<?php |
| 2 |
if(!defined('ABSPATH')) { |
| 3 |
exit; // Exit if accessed directly. |
| 4 |
} |
| 5 |
|
| 6 |
if(!function_exists('bbp_voting_hook_setting')) { |
| 7 |
function bbp_voting_hook_setting($value) { |
| 8 |
global $bbp_voting_hooks; |
| 9 |
global $bbp_voting_default_option_values; |
| 10 |
$setting = current_filter(); |
| 11 |
$type = $bbp_voting_hooks[$setting]; |
| 12 |
$setting_value = get_option( $setting, $bbp_voting_default_option_values[$setting] ?? '' ); |
| 13 |
if($type == 'bool') { |
| 14 |
return $setting_value === 'true' ? true : false; |
| 15 |
} else { |
| 16 |
if(!empty($setting_value)) return sanitize_text_field($setting_value); |
| 17 |
return $value; |
| 18 |
} |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
if(!function_exists('bbp_voting_get_plugin_install_link')) { |
| 23 |
function bbp_voting_get_plugin_install_link( $plugin, $action = 'install-plugin' ) { |
| 24 |
return wp_nonce_url( |
| 25 |
add_query_arg( |
| 26 |
array( |
| 27 |
'action' => $action, |
| 28 |
'plugin' => $plugin |
| 29 |
), |
| 30 |
admin_url( 'update.php' ) |
| 31 |
), |
| 32 |
$action.'_'.$plugin |
| 33 |
); |
| 34 |
} |
| 35 |
} |
| 36 |
if(!function_exists('bbp_voting_get_plugin_activate_link')) { |
| 37 |
function bbp_voting_get_plugin_activate_link( $plugin, $action = 'activate' ) { |
| 38 |
if ( strpos( $plugin, '/' ) ) { |
| 39 |
$plugin = str_replace( '\/', '%2F', $plugin ); |
| 40 |
} |
| 41 |
$url = sprintf( admin_url( 'plugins.php?action=' . $action . '&plugin=%s&plugin_status=all&paged=1&s' ), $plugin ); |
| 42 |
$_REQUEST['plugin'] = $plugin; |
| 43 |
$url = wp_nonce_url( $url, $action . '-plugin_' . $plugin ); |
| 44 |
return $url; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
if(!function_exists('bbp_voting_pro_url')) { |
| 50 |
/** |
| 51 |
* Build a Pro plugin URL with UTM parameters. This is the single source of |
| 52 |
* truth for all links to bbPress Voting Pro from the free plugin. |
| 53 |
* |
| 54 |
* @param string $surface Where the click originated (used as utm_term). |
| 55 |
* Known values: 'curiosity-led', 'number-led', 'outcome-led', |
| 56 |
* 'question-led', 'feature-list', 'settings-pro-badge', |
| 57 |
* 'plugins-list', 'go-pro-banner', 'go-pro-cta'. |
| 58 |
* @param string|null $base_url Override the base URL. Default = Pro sales page. |
| 59 |
*/ |
| 60 |
function bbp_voting_pro_url( $surface = 'cta', $base_url = null ) { |
| 61 |
if ( null === $base_url ) { |
| 62 |
$base_url = 'https://wpforthewin.com/product/bbpress-voting-pro/'; |
| 63 |
} |
| 64 |
$params = array( |
| 65 |
'utm_source' => 'bbp-voting', |
| 66 |
'utm_medium' => 'admin-notice', |
| 67 |
'utm_campaign' => 'pro-upsell', |
| 68 |
'utm_term' => $surface, |
| 69 |
'utm_content' => 'cta', |
| 70 |
); |
| 71 |
$params = (array) apply_filters( 'bbp_voting_pro_url_params', $params, $surface, $base_url ); |
| 72 |
return add_query_arg( $params, $base_url ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if(!function_exists('bbp_voting_field')) { |
| 77 |
function bbp_voting_field( $key, $name, $label = '', $description = '', $type = 'bool', $pro = false ) { |
| 78 |
$name_and_id = 'name="'. $key .'" id="'. $key .'"'; |
| 79 |
$teaser = defined('BBPVOTINGPRO') ? false : $pro; |
| 80 |
$attributes = $teaser ? 'disabled' : $name_and_id; |
| 81 |
?> |
| 82 |
<tr valign="top"> |
| 83 |
<th scope="row"> |
| 84 |
<?php echo $name; ?>: |
| 85 |
<?php if($pro) { ?> |
| 86 |
<a href="<?php echo esc_url( bbp_voting_pro_url( 'settings-pro-badge' ) ); ?>" target="_blank"><span class="bbp-voting-pro-badge bbp-voting-pro-green">Pro</span></a> |
| 87 |
<?php } ?> |
| 88 |
</th> |
| 89 |
<td> |
| 90 |
<?php if($type == 'bool') { ?> |
| 91 |
<input type="checkbox" <?php echo $attributes; ?> value="true" <?php if(apply_filters($key, false) == 'true') echo 'checked'; ?> /> |
| 92 |
<?php } elseif(is_array($type)) { ?> |
| 93 |
<select <?php echo $attributes; ?>> |
| 94 |
<?php foreach($type as $option) { ?> |
| 95 |
<option value="<?php echo $option; ?>"<?php if(apply_filters($key, false) == $option) echo ' selected'; ?>><?php echo ucwords(str_replace('-', ' ', $option)); ?></option> |
| 96 |
<?php } ?> |
| 97 |
</select> |
| 98 |
<?php } else { ?> |
| 99 |
<input type="<?php echo $type; ?>" <?php echo $attributes; ?> value="<?php echo esc_attr(apply_filters($key, false)); ?>" data-lpignore="true" /> |
| 100 |
<?php } ?> |
| 101 |
<?php if(!empty($label)) { ?> |
| 102 |
<label for="<?php echo $key; ?>"><?php echo $label; ?></label> |
| 103 |
<?php } ?> |
| 104 |
<?php if(!empty($description)) { ?> |
| 105 |
<p class="description"><?php echo $description; ?></p> |
| 106 |
<?php } ?> |
| 107 |
</td> |
| 108 |
</tr> |
| 109 |
<?php |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if(!function_exists('bbp_voting_get_current_post_type')) { |
| 114 |
function bbp_voting_get_current_post_type() { |
| 115 |
$this_post_id = bbp_get_reply_id() ?: bbp_get_topic_id(); |
| 116 |
return get_post_type($this_post_id); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if(!function_exists('bbp_voting_get_post_type_by_id')) { |
| 121 |
function bbp_voting_get_post_type_by_id($post_id) { |
| 122 |
$this_post_id = bbp_get_reply_id($post_id) ?: bbp_get_topic_id($post_id); |
| 123 |
return get_post_type($this_post_id); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if(!function_exists('bbp_voting_parse_args')) { |
| 128 |
function bbp_voting_parse_args( $args, $defaults = array(), $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 = array(); |
| 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 |
} |