| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Core Admin Functions |
| 4 |
* |
| 5 |
* General core functions available only within the admin area. |
| 6 |
* |
| 7 |
* @package Charitable/Functions/Admin |
| 8 |
* @version 1.0.0 |
| 9 |
* @author David Bisset |
| 10 |
* @copyright Copyright (c) 2022, WP Charitable LLC |
| 11 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 12 |
*/ |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Load a view from the admin/views folder. |
| 21 |
* |
| 22 |
* If the view is not found, an Exception will be thrown. |
| 23 |
* |
| 24 |
* Example usage: charitable_admin_view('metaboxes/campaign-title'); |
| 25 |
* |
| 26 |
* @since 1.0.0 |
| 27 |
* |
| 28 |
* @param string $view The view to display. |
| 29 |
* @param array $view_args Optional. Arguments to pass through to the view itself. |
| 30 |
* @return boolean True if the view exists and was rendered. False otherwise. |
| 31 |
*/ |
| 32 |
function charitable_admin_view( $view, $view_args = array(), $return_html = false ) { |
| 33 |
$base_path = array_key_exists( 'base_path', $view_args ) ? $view_args['base_path'] : charitable()->get_path( 'admin' ) . 'views/'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Filter the path to the view. |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
* |
| 40 |
* @param string $path The default path. |
| 41 |
* @param string $view The view. |
| 42 |
* @param array $view_args View args. |
| 43 |
*/ |
| 44 |
$filename = apply_filters( 'charitable_admin_view_path', $base_path . $view . '.php', $view, $view_args ); |
| 45 |
|
| 46 |
if ( ! is_readable( $filename ) ) { |
| 47 |
charitable_get_deprecated()->doing_it_wrong( |
| 48 |
__FUNCTION__, |
| 49 |
sprintf( |
| 50 |
/* translators: %s: Filename of passed view */ |
| 51 |
__( 'Passed view (%s) not found or is not readable.', 'charitable' ), |
| 52 |
$filename |
| 53 |
), |
| 54 |
'1.0.0' |
| 55 |
); |
| 56 |
|
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
ob_start(); |
| 61 |
|
| 62 |
include( $filename ); |
| 63 |
|
| 64 |
if ( $return_html ) { |
| 65 |
$output = ob_get_contents(); |
| 66 |
ob_end_clean(); |
| 67 |
return $output; |
| 68 |
} |
| 69 |
|
| 70 |
ob_end_flush(); |
| 71 |
|
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns the Charitable_Settings helper. |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
* |
| 80 |
* @return Charitable_Settings |
| 81 |
*/ |
| 82 |
function charitable_get_admin_settings() { |
| 83 |
return Charitable_Settings::get_instance(); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns the Charitable_Admin_Notices helper. |
| 88 |
* |
| 89 |
* @since 1.4.6 |
| 90 |
* |
| 91 |
* @return Charitable_Admin_Notices |
| 92 |
*/ |
| 93 |
function charitable_get_admin_notices() { |
| 94 |
return charitable()->registry()->get( 'admin_notices' ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns whether we are currently viewing the Charitable settings area. |
| 99 |
* |
| 100 |
* @since 1.2.0 |
| 101 |
* |
| 102 |
* @param string $tab Optional. If passed, the function will also check that we are on the given tab. |
| 103 |
* @return boolean |
| 104 |
*/ |
| 105 |
function charitable_is_settings_view( $tab = '' ) { |
| 106 |
if ( ! empty( $_POST ) ) { |
| 107 |
$is_settings = array_key_exists( 'option_page', $_POST ) && 'charitable_settings' === $_POST['option_page']; |
| 108 |
|
| 109 |
if ( ! $is_settings || empty( $tab ) ) { |
| 110 |
return $is_settings; |
| 111 |
} |
| 112 |
|
| 113 |
return array_key_exists( 'charitable_settings', $_POST ) && array_key_exists( $tab, $_POST['charitable_settings'] ); |
| 114 |
} |
| 115 |
|
| 116 |
$is_settings = isset( $_GET['page'] ) && 'charitable-settings' == $_GET['page']; |
| 117 |
|
| 118 |
if ( ! $is_settings || empty( $tab ) ) { |
| 119 |
return $is_settings; |
| 120 |
} |
| 121 |
|
| 122 |
/* The general tab can be loaded when tab is not set. */ |
| 123 |
if ( 'general' == $tab ) { |
| 124 |
return ! isset( $_GET['tab'] ) || 'general' == $_GET['tab']; |
| 125 |
} |
| 126 |
|
| 127 |
return isset( $_GET['tab'] ) && $tab == $_GET['tab']; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Print out the settings fields for a particular settings section. |
| 132 |
* |
| 133 |
* This is based on WordPress' do_settings_fields but allows the possibility |
| 134 |
* of leaving out a field lable/title, for fullwidth fields. |
| 135 |
* |
| 136 |
* @see do_settings_fields |
| 137 |
* |
| 138 |
* @since 1.0.0 |
| 139 |
* |
| 140 |
* @global $wp_settings_fields Storage array of settings fields and their pages/sections |
| 141 |
* |
| 142 |
* @param string $page Slug title of the admin page who's settings fields you want to show. |
| 143 |
* @param string $section Slug title of the settings section who's fields you want to show. |
| 144 |
* @return string |
| 145 |
*/ |
| 146 |
function charitable_do_settings_fields( $page, $section ) { |
| 147 |
global $wp_settings_fields; |
| 148 |
|
| 149 |
if ( ! isset( $wp_settings_fields[ $page ][ $section ] ) ) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
foreach ( (array) $wp_settings_fields[ $page ][ $section ] as $field ) { |
| 154 |
$class = ''; |
| 155 |
|
| 156 |
if ( ! empty( $field['args']['class'] ) ) { |
| 157 |
$class = ' class="' . esc_attr( $field['args']['class'] ) . '"'; |
| 158 |
} |
| 159 |
|
| 160 |
echo "<tr{$class}>"; |
| 161 |
|
| 162 |
if ( ! empty( $field['args']['label_for'] ) ) { |
| 163 |
echo '<th scope="row"><label for="' . esc_attr( $field['args']['label_for'] ) . '">' . $field['title'] . '</label></th>'; |
| 164 |
echo '<td>'; |
| 165 |
call_user_func( $field['callback'], $field['args'] ); |
| 166 |
echo '</td>'; |
| 167 |
} elseif ( ! empty( $field['title'] ) ) { |
| 168 |
echo '<th scope="row">' . $field['title'] . '</th>'; |
| 169 |
echo '<td>'; |
| 170 |
call_user_func( $field['callback'], $field['args'] ); |
| 171 |
echo '</td>'; |
| 172 |
} else { |
| 173 |
echo '<td colspan="2" class="charitable-fullwidth">'; |
| 174 |
call_user_func( $field['callback'], $field['args'] ); |
| 175 |
echo '</td>'; |
| 176 |
} |
| 177 |
|
| 178 |
echo '</tr>'; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Add new tab to the Charitable settings area. |
| 184 |
* |
| 185 |
* @since 1.3.0 |
| 186 |
* |
| 187 |
* @param string[] $tabs |
| 188 |
* @param string $key |
| 189 |
* @param string $name |
| 190 |
* @param mixed[] $args |
| 191 |
* @return string[] |
| 192 |
*/ |
| 193 |
function charitable_add_settings_tab( $tabs, $key, $name, $args = array() ) { |
| 194 |
$defaults = array( |
| 195 |
'index' => 3, |
| 196 |
); |
| 197 |
|
| 198 |
$args = wp_parse_args( $args, $defaults ); |
| 199 |
$keys = array_keys( $tabs ); |
| 200 |
$values = array_values( $tabs ); |
| 201 |
|
| 202 |
array_splice( $keys, $args['index'], 0, $key ); |
| 203 |
array_splice( $values, $args['index'], 0, $name ); |
| 204 |
|
| 205 |
return array_combine( $keys, $values ); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Return the donation actions class. |
| 210 |
* |
| 211 |
* @since 1.5.0 |
| 212 |
* |
| 213 |
* @return Charitable_Donation_Admin_Actions |
| 214 |
*/ |
| 215 |
function charitable_get_donation_actions() { |
| 216 |
return Charitable_Admin::get_instance()->get_donation_actions(); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
/** |
| 221 |
* Adds the "Upgrade to Pro" menu item to the very end of the submenu. |
| 222 |
* |
| 223 |
* @since 1.7.0 |
| 224 |
*/ |
| 225 |
function charitable_add_upgrade_item() { |
| 226 |
global $submenu; |
| 227 |
|
| 228 |
if ( charitable_is_pro() ) { |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
$submenu['charitable'][99] = array( |
| 233 |
__( 'Upgrade to Pro', 'chariable' ), |
| 234 |
'manage_options', |
| 235 |
charitable_ga_url( |
| 236 |
'https://wpcharitable.com/lite-vs-pro/', |
| 237 |
urlencode( 'Admin Menu Link'), |
| 238 |
urlencode( 'Upgrade to Pro' ) |
| 239 |
) |
| 240 |
); |
| 241 |
} |
| 242 |
add_action('admin_menu', 'charitable_add_upgrade_item'); |
| 243 |
|
| 244 |
|
| 245 |
/** |
| 246 |
* Outputs "please rate" text. |
| 247 |
* |
| 248 |
* @since 1.7.0 |
| 249 |
* |
| 250 |
* @param string $footer_text Footer text. |
| 251 |
* @return string |
| 252 |
*/ |
| 253 |
function charitable_add_footer_text( $footer_text ) { |
| 254 |
if ( ! charitable_is_admin_screen() ) { |
| 255 |
return $footer_text; |
| 256 |
} |
| 257 |
|
| 258 |
return sprintf( |
| 259 |
/* translators: %1$s Opening strong tag, do not translate. %2$s Closing strong tag, do not translate. %3$s Opening anchor tag, do not translate. %4$s Closing anchor tag, do not translate. */ |
| 260 |
__( 'Please rate %1$sWP Charitable%2$s %3$s� |
| 261 |
� |
| 262 |
� |
| 263 |
� |
| 264 |
� |
| 265 |
%4$s on %3$sWordPress.org%4$s to help us spread the word. Thank you from the WP Charitable team!', 'stripe' ), |
| 266 |
'<strong>', |
| 267 |
'</strong>', |
| 268 |
'<a href="https://wordpress.org/support/plugin/charitable/reviews/?filter=5#new-post" rel="noopener noreferrer" target="_blank">', |
| 269 |
'</a>' |
| 270 |
); |
| 271 |
} |
| 272 |
add_filter( 'admin_footer_text', 'charitable_add_footer_text' ); |
| 273 |
|
| 274 |
/** |
| 275 |
* Check if a screen is a plugin admin view. |
| 276 |
* Returns the screen id if true, false (bool) if not. |
| 277 |
* |
| 278 |
* @since 1.7.0 |
| 279 |
* |
| 280 |
* @return string|bool |
| 281 |
*/ |
| 282 |
function charitable_is_admin_screen() { |
| 283 |
$screen = \get_current_screen(); |
| 284 |
|
| 285 |
if ( |
| 286 |
'charitable' === $screen->post_type || |
| 287 |
'campaign' === $screen->post_type || |
| 288 |
'charitable' === $screen->parent_file |
| 289 |
) { |
| 290 |
return 'charitable'; |
| 291 |
} |
| 292 |
|
| 293 |
if ( isset( $_GET['page'] ) ) { |
| 294 |
if ( 'charitable' == $_GET['page'] ) { |
| 295 |
return 'charitable'; |
| 296 |
} |
| 297 |
|
| 298 |
// if ( 'charitable_settings' == $_GET['page'] ) { |
| 299 |
// return 'charitable_settings'; |
| 300 |
// } |
| 301 |
|
| 302 |
// if ( 'charitable_system_status' == $_GET['page'] ) { |
| 303 |
// return 'charitable_system_status'; |
| 304 |
// } |
| 305 |
} |
| 306 |
|
| 307 |
return false; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Appends UTM parameters to a given URL. |
| 312 |
* |
| 313 |
* @since 1.7.0 |
| 314 |
* |
| 315 |
* @param string $base_url Base URL. |
| 316 |
* @param string $utm_medium utm_medium parameter. |
| 317 |
* @param string $utm_content Optional. utm_content parameter. |
| 318 |
* @return string $url Full Google Analytics campaign URL. |
| 319 |
*/ |
| 320 |
function charitable_ga_url( $base_url, $utm_medium, $utm_content = false ) { |
| 321 |
/** |
| 322 |
* Filters the UTM campaign for generated links. |
| 323 |
* |
| 324 |
* @since 3.0.0 |
| 325 |
* |
| 326 |
* @param string $utm_campaign |
| 327 |
*/ |
| 328 |
$utm_campaign = apply_filters( 'charitable_utm_campaign', 'WP+Charitable' ); |
| 329 |
|
| 330 |
$args = array( |
| 331 |
'utm_source' => 'WordPress', |
| 332 |
'utm_campaign' => $utm_campaign, |
| 333 |
'utm_medium' => $utm_medium, |
| 334 |
); |
| 335 |
|
| 336 |
if ( ! empty( $utm_content ) ) { |
| 337 |
$args['utm_content'] = $utm_content; |
| 338 |
} |
| 339 |
|
| 340 |
return esc_url( add_query_arg( $args, $base_url ) ); |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* URL for upgrading to Pro (or another Pro licecnse). |
| 345 |
* |
| 346 |
* @since 1.7.0 |
| 347 |
* |
| 348 |
* @param string $utm_medium utm_medium parameter. |
| 349 |
* @param string $utm_content Optional. utm_content parameter. |
| 350 |
* @return string |
| 351 |
*/ |
| 352 |
function charitable_pro_upgrade_url( $utm_medium, $utm_content = '' ) { |
| 353 |
return apply_filters( |
| 354 |
'charitable_upgrade_link', |
| 355 |
charitable_ga_url( |
| 356 |
'https://wpcharitable.com/lite-vs-pro/', |
| 357 |
urlencode( $utm_medium ), |
| 358 |
urlencode( $utm_content ) |
| 359 |
), |
| 360 |
$utm_medium, |
| 361 |
$utm_content |
| 362 |
); |
| 363 |
} |