| 1 |
<?php |
| 2 |
/** |
| 3 |
* Front-end Actions |
| 4 |
* |
| 5 |
* @package Give |
| 6 |
* @subpackage Functions |
| 7 |
* @copyright Copyright (c) 2016, GiveWP |
| 8 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 |
* @since 1.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
/** |
| 19 |
* Look for Give core add-ons update after every third day |
| 20 |
* |
| 21 |
* @since 2.5.11 |
| 22 |
*/ |
| 23 |
Give_Cron::add_thricely_event( 'give_refresh_licenses' ); |
| 24 |
|
| 25 |
/** |
| 26 |
* Hooks Give actions, when present in the $_GET superglobal. Every give_action |
| 27 |
* present in $_GET is called using WordPress's do_action function. These |
| 28 |
* functions are called on init. |
| 29 |
* |
| 30 |
* @since 1.0 |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
function give_get_actions() { |
| 35 |
|
| 36 |
$get_data = give_clean( $_GET ); // WPCS: input var ok, sanitization ok, CSRF ok. |
| 37 |
|
| 38 |
$_get_action = ! empty( $get_data['give_action'] ) ? $get_data['give_action'] : null; |
| 39 |
|
| 40 |
// Add backward compatibility to give-action param ( $_GET ). |
| 41 |
if ( empty( $_get_action ) ) { |
| 42 |
$_get_action = ! empty( $get_data['give-action'] ) ? $get_data['give-action'] : null; |
| 43 |
} |
| 44 |
|
| 45 |
if ( isset( $_get_action ) ) { |
| 46 |
/** |
| 47 |
* Fires in WordPress init or admin init, when give_action is present in $_GET. |
| 48 |
* |
| 49 |
* @since 1.0 |
| 50 |
* |
| 51 |
* @param array $_GET Array of HTTP GET variables. |
| 52 |
*/ |
| 53 |
do_action( "give_{$_get_action}", $get_data ); |
| 54 |
} |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
add_action( 'init', 'give_get_actions' ); |
| 59 |
|
| 60 |
/** |
| 61 |
* Hooks Give actions, when present in the $_POST super global. Every give_action |
| 62 |
* present in $_POST is called using WordPress's do_action function. These |
| 63 |
* functions are called on init. |
| 64 |
* |
| 65 |
* @since 1.0 |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
function give_post_actions() { |
| 70 |
|
| 71 |
$post_data = give_clean( $_POST ); // WPCS: input var ok, sanitization ok, CSRF ok. |
| 72 |
|
| 73 |
$_post_action = ! empty( $post_data['give_action'] ) ? $post_data['give_action'] : null; |
| 74 |
|
| 75 |
// Add backward compatibility to give-action param ( $_POST ). |
| 76 |
if ( empty( $_post_action ) ) { |
| 77 |
$_post_action = ! empty( $post_data['give-action'] ) ? $post_data['give-action'] : null; |
| 78 |
} |
| 79 |
|
| 80 |
if ( isset( $_post_action ) ) { |
| 81 |
/** |
| 82 |
* Fires in WordPress init or admin init, when give_action is present in $_POST. |
| 83 |
* |
| 84 |
* @since 1.0 |
| 85 |
* |
| 86 |
* @param array $_POST Array of HTTP POST variables. |
| 87 |
*/ |
| 88 |
do_action( "give_{$_post_action}", $post_data ); |
| 89 |
} |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
add_action( 'init', 'give_post_actions' ); |
| 94 |
|
| 95 |
/** |
| 96 |
* Connect WordPress user with Donor. |
| 97 |
* |
| 98 |
* @param int $user_id User ID. |
| 99 |
* @param array $user_data User Data. |
| 100 |
* |
| 101 |
* @since 4.16.6 Only auto-link when registered via the donation-checkout flow. |
| 102 |
* @since 1.7 |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
function give_connect_donor_to_wpuser( $user_id, $user_data ) { |
| 107 |
if ( empty( $user_data['give_donation_checkout_registration'] ) ) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
/* @var Give_Donor $donor */ |
| 112 |
$donor = new Give_Donor( $user_data['user_email'] ); |
| 113 |
|
| 114 |
// Validate donor id and check if do nor is already connect to wp user or not. |
| 115 |
if ( $donor->id && ! $donor->user_id ) { |
| 116 |
|
| 117 |
// Update donor user_id. |
| 118 |
if ( $donor->update( [ 'user_id' => $user_id ] ) ) { |
| 119 |
$donor_note = sprintf( esc_html__( 'WordPress user #%1$d is connected to #%2$d', 'give' ), $user_id, $donor->id ); |
| 120 |
$donor->add_note( $donor_note ); |
| 121 |
|
| 122 |
// Update user_id meta in payments. |
| 123 |
// if( ! empty( $donor->payment_ids ) && ( $donations = explode( ',', $donor->payment_ids ) ) ) { |
| 124 |
// foreach ( $donations as $donation ) { |
| 125 |
// give_update_meta( $donation, '_give_payment_user_id', $user_id ); |
| 126 |
// } |
| 127 |
// } |
| 128 |
// Do not need to update user_id in payment because we will get user id from donor id now. |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
add_action( 'give_insert_user', 'give_connect_donor_to_wpuser', 10, 2 ); |
| 134 |
|
| 135 |
|
| 136 |
/** |
| 137 |
* Processing after donor batch export complete |
| 138 |
* |
| 139 |
* @since 1.8 |
| 140 |
* |
| 141 |
* @param $data |
| 142 |
*/ |
| 143 |
function give_donor_batch_export_complete( $data ) { |
| 144 |
// Remove donor ids cache. |
| 145 |
if ( |
| 146 |
isset( $data['class'] ) |
| 147 |
&& 'Give_Batch_Donors_Export' === $data['class'] |
| 148 |
&& ! empty( $data['forms'] ) |
| 149 |
&& isset( $data['give_export_option']['query_id'] ) |
| 150 |
) { |
| 151 |
Give_Cache::delete( Give_Cache::get_key( $data['give_export_option']['query_id'] ) ); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
add_action( 'give_file_export_complete', 'give_donor_batch_export_complete' ); |
| 156 |
|
| 157 |
|
| 158 |
/** |
| 159 |
* Set Donation Amount for Multi Level Donation Forms |
| 160 |
* |
| 161 |
* @param int $form_id Donation Form ID. |
| 162 |
* |
| 163 |
* @since 1.8.9 |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
function give_set_donation_levels_max_min_amount( $form_id ) { |
| 168 |
if ( |
| 169 |
( 'set' === $_POST['_give_price_option'] ) || |
| 170 |
( in_array( '_give_donation_levels', $_POST ) && count( $_POST['_give_donation_levels'] ) <= 0 ) || |
| 171 |
! ( $donation_levels_amounts = wp_list_pluck( $_POST['_give_donation_levels'], '_give_amount' ) ) |
| 172 |
) { |
| 173 |
// Delete old meta. |
| 174 |
give_delete_meta( $form_id, '_give_levels_minimum_amount' ); |
| 175 |
give_delete_meta( $form_id, '_give_levels_maximum_amount' ); |
| 176 |
|
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
// Sanitize donation level amounts. |
| 181 |
$donation_levels_amounts = array_map( 'give_maybe_sanitize_amount', $donation_levels_amounts ); |
| 182 |
|
| 183 |
$min_amount = min( $donation_levels_amounts ); |
| 184 |
$max_amount = max( $donation_levels_amounts ); |
| 185 |
|
| 186 |
// Set Minimum and Maximum amount for Multi Level Donation Forms. |
| 187 |
give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount_for_db( $min_amount ) : 0 ); |
| 188 |
give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount_for_db( $max_amount ) : 0 ); |
| 189 |
} |
| 190 |
|
| 191 |
add_action( 'give_pre_process_give_forms_meta', 'give_set_donation_levels_max_min_amount', 30 ); |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* Save donor address when donation complete |
| 196 |
* |
| 197 |
* @since 2.0 |
| 198 |
* |
| 199 |
* @param int $payment_id |
| 200 |
*/ |
| 201 |
function _give_save_donor_billing_address( $payment_id ) { |
| 202 |
$donor_id = absint( give_get_payment_donor_id( $payment_id ) ); |
| 203 |
|
| 204 |
// Bailout |
| 205 |
if ( ! $donor_id ) { |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
/* @var Give_Donor $donor */ |
| 210 |
$donor = new Give_Donor( $donor_id ); |
| 211 |
|
| 212 |
// Save address. |
| 213 |
$donor->add_address( 'billing[]', give_get_donation_address( $payment_id ) ); |
| 214 |
} |
| 215 |
|
| 216 |
add_action( 'give_complete_donation', '_give_save_donor_billing_address', 9999 ); |
| 217 |
|
| 218 |
/** |
| 219 |
* Verify addon dependency before addon update |
| 220 |
* |
| 221 |
* @since 4.9.0 rename function - PHP 8 compatibility |
| 222 |
* @since 4.1.0 add bailout for GiveWP to protect it from licensing issues |
| 223 |
* @since 2.1.4 |
| 224 |
* |
| 225 |
* @param $error |
| 226 |
* @param $hook_extra |
| 227 |
* |
| 228 |
* @return WP_Error |
| 229 |
*/ |
| 230 |
function give_verify_addon_dependency_before_update( $error, $hook_extra ) { |
| 231 |
// Bailout. |
| 232 |
if ( |
| 233 |
is_wp_error( $error ) |
| 234 |
|| ! array_key_exists( 'plugin', $hook_extra ) |
| 235 |
) { |
| 236 |
return $error; |
| 237 |
} |
| 238 |
|
| 239 |
// If the plugin is GiveWP then bypass the License check altogether. If there's a problem with licensing it's |
| 240 |
// important that we can always upgrade GiveWP. |
| 241 |
$plugin_base = strtolower( $hook_extra['plugin'] ); |
| 242 |
if ( GIVE_PLUGIN_BASENAME === $plugin_base ) { |
| 243 |
return $error; |
| 244 |
} |
| 245 |
|
| 246 |
$licensed_addon = array_map( 'strtolower', Give_License::get_licensed_addons() ); |
| 247 |
|
| 248 |
// Skip if not a Give addon. |
| 249 |
if ( ! in_array( $plugin_base, $licensed_addon ) ) { |
| 250 |
return $error; |
| 251 |
} |
| 252 |
|
| 253 |
// Load file. |
| 254 |
if ( ! class_exists( 'Give_Readme_Parser' ) ) { |
| 255 |
require_once GIVE_PLUGIN_DIR . 'includes/class-give-readme-parser.php'; |
| 256 |
} |
| 257 |
|
| 258 |
$plugin_base = strtolower( $plugin_base ); |
| 259 |
$plugin_slug = str_replace( '.php', '', basename( $plugin_base ) ); |
| 260 |
|
| 261 |
$url = give_get_addon_readme_url( $plugin_slug ); |
| 262 |
|
| 263 |
$parser = new Give_Readme_Parser( $url ); |
| 264 |
$give_min_version = $parser->requires_at_least(); |
| 265 |
|
| 266 |
if ( version_compare( GIVE_VERSION, $give_min_version, '<' ) ) { |
| 267 |
return new WP_Error( |
| 268 |
'Give_Addon_Update_Error', |
| 269 |
sprintf( |
| 270 |
__( 'GiveWP version %s is required to update this add-on.', 'give' ), |
| 271 |
$give_min_version |
| 272 |
) |
| 273 |
); |
| 274 |
} |
| 275 |
|
| 276 |
return $error; |
| 277 |
} |
| 278 |
|
| 279 |
add_filter( 'upgrader_pre_install', 'give_verify_addon_dependency_before_update', 10, 2 ); |
| 280 |
|
| 281 |
/** |
| 282 |
* Function to add suppress_filters param if WPML add-on is activated. |
| 283 |
* |
| 284 |
* @since 4.9.0 rename function - PHP 8 compatibility |
| 285 |
* @since 2.1.4 |
| 286 |
* |
| 287 |
* @param array WP query argument for Total Goal. |
| 288 |
* |
| 289 |
* @return array WP query argument for Total Goal. |
| 290 |
*/ |
| 291 |
function give_wpml_total_goal_shortcode_agrs( $args ) { |
| 292 |
$args['suppress_filters'] = true; |
| 293 |
|
| 294 |
return $args; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Function to remove WPML post where filter in goal total amount shortcode. |
| 299 |
* |
| 300 |
* @since 4.9.0 rename function - PHP 8 compatibility |
| 301 |
* @since 2.1.4 |
| 302 |
* @global SitePress $sitepress |
| 303 |
*/ |
| 304 |
function give_remove_wpml_parse_query_filter() { |
| 305 |
global $sitepress; |
| 306 |
remove_action( 'parse_query', [ $sitepress, 'parse_query' ] ); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
/** |
| 311 |
* Function to add WPML post where filter in goal total amount shortcode. |
| 312 |
* |
| 313 |
* @since 4.9.0 rename function - PHP 8 compatibility |
| 314 |
* @since 2.1.4 |
| 315 |
* @global SitePress $sitepress |
| 316 |
*/ |
| 317 |
function give_add_wpml_parse_query_filter() { |
| 318 |
global $sitepress; |
| 319 |
add_action( 'parse_query', [ $sitepress, 'parse_query' ] ); |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Action all the hook that add support for WPML. |
| 324 |
* |
| 325 |
* @since 2.1.4 |
| 326 |
*/ |
| 327 |
function give_add_support_for_wpml() { |
| 328 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 329 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 330 |
} |
| 331 |
|
| 332 |
if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) { |
| 333 |
|
| 334 |
add_filter( 'give_totals_goal_shortcode_query_args', 'give_wpml_total_goal_shortcode_agrs'); |
| 335 |
|
| 336 |
// @see https://wpml.org/forums/topic/problem-with-query-filter-in-get_posts-function/#post-271309 |
| 337 |
add_action( 'give_totals_goal_shortcode_before_render', 'give_remove_wpml_parse_query_filter', 99 ); |
| 338 |
add_action( 'give_totals_goal_shortcode_after_render', 'give_add_wpml_parse_query_filter', 99 ); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
add_action( 'give_init', 'give_add_support_for_wpml', 1000 ); |
| 343 |
|
| 344 |
/** |
| 345 |
* Backward compatibility for email_access property |
| 346 |
* Note: only for internal purpose |
| 347 |
* |
| 348 |
* @todo: Need to decide when to remove this backward compatibility. |
| 349 |
* We decided to load Give()->email_access on for frontend but some of email tags is still using this. Since we have option to resend email in admin then |
| 350 |
* this cause of fatal error because that property does not load in backend. This is a temporary solution to prevent fatal error when resend receipt. |
| 351 |
* ref: https://github.com/impress-org/give/issues/4068 |
| 352 |
* |
| 353 |
* @since 2.4.5 |
| 354 |
*/ |
| 355 |
function give_set_email_access_property() { |
| 356 |
if ( ! ( Give()->email_access instanceof Give_Email_Access ) ) { |
| 357 |
require_once GIVE_PLUGIN_DIR . 'includes/class-give-email-access.php'; |
| 358 |
Give()->email_access = new Give_Email_Access(); |
| 359 |
} |
| 360 |
} |
| 361 |
add_action( 'give_email_links', 'give_set_email_access_property', -1 ); |
| 362 |
add_action( 'give_donation-receipt_email_notification', 'give_set_email_access_property', -1 ); |
| 363 |
|