function setUp() { parent::setUp(); $this->notices = charitable_get_notices(); // Clear out all existing notices $this->notices->clear(); }
/** * Validate the submitted credit card details. * * @param boolean $valid * @param string $gateway * @param mixed[] $values * @return boolean * @access public * @static * @since 1.0.0 */ public static function validate_donation($valid, $gateway, $values) { if ('paypal' != $gateway) { return $valid; } $settings = charitable_get_option('gateways_paypal', array()); $email = trim($settings['paypal_email']); /* Make sure that the keys are set. */ if (empty($email)) { charitable_get_notices()->add_error(__('Missing PayPal email address. Unable to proceed with payment.', 'charitable')); return false; } return $valid; }
/** * Process donation. * * @since 1.0.0 * @param mixed $return * @param int $donation_id * @param Charitable_Donation_Processor $processor * @param string $gateway * @return mixed array or boolean */ public static function pronamic_process_donation($return, $donation_id, $processor, $charitable_gateway) { $payment_method = $charitable_gateway->payment_method; $config_id = $charitable_gateway->get_value('config_id'); $gateway = Pronamic_WP_Pay_Plugin::get_gateway($config_id); if (!$gateway) { return false; } // Data $data = new Pronamic_WP_Pay_Extensions_Charitable_PaymentData($donation_id, $processor, $charitable_gateway); $gateway->set_payment_method($payment_method); $payment = Pronamic_WP_Pay_Plugin::start($config_id, $gateway, $data, $payment_method); $error = $gateway->get_error(); if (is_wp_error($error)) { charitable_get_notices()->add_error($error->get_error_message()); return false; } return array('redirect' => $payment->get_pay_redirect_url(), 'safe' => false); }
/** * Check the passed fields to ensure that all required fields have been submitted. * * @param array $fields * @param array $submitted * @return boolean * @access public * @since 1.0.0 */ public function check_required_fields($fields, $submitted = array()) { if (empty($submitted)) { $submitted = $this->get_submitted_values(); } $ret = true; $missing = array(); foreach ($this->get_required_fields($fields) as $key => $field) { /* We already have a value for this field. */ if (!empty($field['value'])) { continue; } $exists = isset($submitted[$key]); /* Verify that a value was provided. */ if ($exists) { $value = $submitted[$key]; $exists = !empty($value) || is_string($value) && strlen($value); } /* If a value was not provided, check if it's in the $_FILES array. */ if (!$exists) { $exists = 'picture' == $field['type'] && isset($_FILES[$key]) && !empty($_FILES[$key]['name']); } $exists = apply_filters('charitable_required_field_exists', $exists, $key, $field, $submitted, $this); if (!$exists) { $label = isset($field['label']) ? $field['label'] : $key; $missing[] = $label; } } $missing = apply_filters('charitable_form_missing_fields', $missing, $this, $fields, $submitted); if (count($missing)) { $missing_fields = implode('</li><li>', $missing); charitable_get_notices()->add_error(sprintf('<p>%s</p><ul class="error-list"><li>%s</li></ul>', __('There were problems with your form submission. The following required fields were not filled out:', 'charitable'), $missing_fields)); $ret = false; } return $ret; }
/** * Render any notices. * * @param array $notices * @return void * @since 1.4.0 */ function charitable_template_notices($notices = array()) { if (empty($notices)) { $notices = charitable_get_notices()->get_notices(); } charitable_template('form-fields/notices.php', array('notices' => $notices)); }
/** * Save core fields of the user (i.e. the wp_users data) * * @uses wp_insert_user * @param array $submitted * @return int User ID * @access public * @since 1.0.0 */ public function update_core_user($submitted) { $core_fields = array_intersect(array_keys($submitted), $this->get_core_keys()); if (empty($core_fields)) { return 0; } $values = array(); /* If we're updating an active user, set the ID */ if (0 !== $this->ID) { $values['ID'] = $this->ID; } foreach ($core_fields as $field) { $values[$field] = $submitted[$field]; } /* Insert the user */ if (0 == $this->ID) { if (!isset($values['user_pass']) || strlen($values['user_pass']) == 0) { charitable_get_notices()->add_error('<strong>ERROR:</strong> Password field is required.'); return false; } if (!isset($values['user_login'])) { $values['user_login'] = $values['user_email']; } /** * `wp_insert_user` calls `sanitize_user` internally - make the * same call here so `$values[ 'user_login' ]` matches what is * eventually saved to the database */ $values['user_login'] = sanitize_user($values['user_login'], true); $user_id = wp_insert_user($values); if (is_wp_error($user_id)) { charitable_get_notices()->add_errors_from_wp_error($user_id); return false; } $this->init(self::get_data_by('id', $user_id)); $signon = Charitable_User::signon($values['user_login'], $values['user_pass']); if (is_wp_error($signon)) { charitable_get_notices()->add_errors_from_wp_error($signon); return false; } do_action('charitable_after_insert_user', $user_id, $values); } else { $values['ID'] = $this->ID; $user_id = wp_update_user($values); } /* If there was an error when inserting or updating the user, lodge the error. */ if (is_wp_error($user_id)) { charitable_get_notices()->add_errors_from_wp_error($user_id); return false; } do_action('charitable_after_save_user', $user_id, $values); return $user_id; }
/** * Add the all notices to the session. * * @return void * @access public * @since 1.0.0 */ public function add_notices() { $this->set('notices', charitable_get_notices()->get_notices()); }
/** * Send the password reset email. * * @return bool|WP_Error True: when finish. WP_Error on error * @access public * @static * @since 1.4.0 */ public static function retrieve_password() { $form = new Charitable_Forgot_Password_Form(); if (!$form->validate_nonce()) { return; } if (empty($_POST['user_login'])) { charitable_get_notices()->add_error(__('<strong>ERROR</strong>: Enter a username or email address.', 'charitable')); return; } elseif (strpos($_POST['user_login'], '@')) { $user = get_user_by('email', trim($_POST['user_login'])); } else { $login = trim($_POST['user_login']); $user = get_user_by('login', $login); } do_action('lostpassword_post'); /* If we are missing user data, proceed no further. */ if (!$user) { charitable_get_notices()->add_error(__('<strong>ERROR</strong>: Invalid username or email.', 'charitable')); return; } /* Prepare the email. */ $email = new Charitable_Email_Password_Reset(array('user' => $user)); $reset_link = $email->get_reset_link(); /* Make sure that the reset link was generated correctly. */ if (is_wp_error($reset_link)) { charitable_get_notices()->add_errors_from_wp_error($reset_link); return; } $sent = $email->send(); if (!$sent) { charitable_get_notices()->add_error(__('We were unable to send your password reset email.', 'charitable')); return; } charitable_get_notices()->add_success(__('Your password reset request has been received. Please check your email for a link to reset your password.', 'charitable')); charitable_get_session()->add_notices(); $redirect_url = esc_url_raw(charitable_get_permalink('login_page')); wp_safe_redirect($redirect_url); exit; }
/** * Returns true if required fields are missing. * * @param array $required_fields * @return boolean * @access protected * @since 1.0.0 */ protected function is_missing_required_fields($required_fields) { if (is_user_logged_in()) { return false; } if (is_null($this->get_submitted_value('gateway'))) { charitable_get_notices()->add_error(sprintf('<p>%s</p>', __('Your donation could not be processed. No payment gateway was selected.', 'charitable'))); return false; } return !$this->check_required_fields($required_fields); }
/** * Changes a password if the current password is correct and the repeat matches the new password. * * @return boolean * @access public * @since 1.4.0 */ public function validate_password_change() { /* The current password must be correct. */ if (false == wp_check_password($_POST['current_pass'], $this->get_user()->user_pass)) { charitable_get_notices()->add_error('Current password is incorrect.', 'charitable'); return false; } /* The new password must match the repeat (if set). */ if (isset($_POST['user_pass_repeat']) && $_POST['user_pass_repeat'] != $_POST['user_pass']) { charitable_get_notices()->add_error('New passwords did not match.', 'charitable'); return false; } return true; }
/** * Inserts a new donation. * * This method is designed to be completely form agnostic. * * We use this when integrating third-party systems like Easy Digital Downloads and * WooCommerce. * * @param mixed[] $values * @return int $donation_id Returns 0 in case of failure. Positive donation ID otherwise. * @access public * @since 1.0.0 */ public function save_donation(array $values) { /** * @hook charitable_donation_values */ $this->donation_data = apply_filters('charitable_donation_values', $values); if (!$this->get_campaign_donations_data()) { _doing_it_wrong(__METHOD__, 'A donation cannot be inserted without an array of campaigns being donated to.', '1.0.0'); return 0; } if (!$this->is_valid_user_data()) { _doing_it_wrong(__METHOD__, 'A donation cannot be inserted without valid user data.', '1.0.0'); return 0; } /** * @hook charitable_before_save_donation */ do_action('charitable_before_save_donation', $this); $donation_id = wp_insert_post($this->parse_donation_data()); $this->set_donation_key(); if (is_wp_error($donation_id)) { charitable_get_notices()->add_errors_from_wp_error($donation_id); return 0; } if (0 == $donation_id) { charitable_get_notices()->add_error(__('We were unable to save the donation. Please try again.', 'charitable')); return 0; } $this->save_campaign_donations($donation_id); $this->save_donation_meta($donation_id); $this->update_donation_log($donation_id, __('Donation created.', 'charitable')); if (!is_admin()) { charitable_get_session()->add_donation_key($this->get_donation_data_value('donation_key')); } /** * @hook charitable_after_save_donation */ do_action('charitable_after_save_donation', $donation_id, $this); return $donation_id; }
/** * Save core fields of the user (i.e. the wp_users data) * * @uses wp_insert_user * @param array $submitted * @return int User ID * @access public * @since 1.0.0 */ public function update_core_user($submitted) { $core_fields = array_intersect(array_keys($submitted), $this->get_core_keys()); if (empty($core_fields)) { return 0; } $values = array(); /* If we're updating an active user, set the ID */ if (0 !== $this->ID) { $values['ID'] = $this->ID; } foreach ($core_fields as $field) { $values[$field] = $submitted[$field]; } /* Insert the user */ if (0 == $this->ID) { if (!isset($values['user_pass'])) { $values['user_pass'] = wp_generate_password(); } if (!isset($values['user_login'])) { $values['user_login'] = $values['user_email']; } $user_id = wp_insert_user($values); if (is_wp_error($user_id)) { charitable_get_notices()->add_errors_from_wp_error($user_id); return 0; } $this->init(self::get_data_by('id', $user_id)); do_action('charitable_after_insert_user', $user_id, $values); } else { $values['ID'] = $this->ID; $user_id = wp_update_user($values); } /* If there was an error when inserting or updating the user, lodge the error. */ if (is_wp_error($user_id)) { charitable_get_notices()->add_errors_from_wp_error($user_id); } do_action('charitable_after_save_user', $user_id, $values); return $user_id; }
/** * Get the reset key and login from the cookie. * * @return void * @access protected * @since 1.4.0 */ protected function parse_reset_key() { $this->key = null; $this->login = null; if (!isset($_COOKIE['wp-resetpass-' . COOKIEHASH])) { return; } $cookie = $_COOKIE['wp-resetpass-' . COOKIEHASH]; if (!strpos($cookie, ':')) { return; } $cookie_parts = explode(':', wp_unslash($cookie), 2); list($login, $key) = array_map('sanitize_text_field', $cookie_parts); $user = check_password_reset_key($key, $login); if (is_wp_error($user)) { charitable_get_notices()->add_errors_from_wp_error($user); Charitable_User_Management::get_instance()->set_reset_cookie(); return; } /* Reset key / login is correct, display reset password form with hidden key / login values */ $this->key = $key; $this->login = $login; }
/** * Redirect the user after the gateway has processed the donation. * * @uses Charitable_Donation_Processor::get_redirection_after_gateway_processing() * * @param mixed $gateway_processing * @return void * @access private * @since 1.3.0 */ private function redirect_after_gateway_processing($gateway_processing) { $redirect_url = $this->get_redirection_after_gateway_processing($gateway_processing); /* If the gateway processing failed, add the error notices to the session. */ if (false == $gateway_processing) { /* Log the failed payment. */ $this->update_donation_log($this->donation_id, sprintf(__('Payment failed with errors: %s', 'charitable'), PHP_EOL . implode(PHP_EOL, charitable_get_notices()->get_errors()))); charitable_get_session()->add_notices(); } /* Set the redirect status to use. */ $status = isset($gateway_processing['status']) ? $gateway_processing['status'] : 302; /** * If the gateway processing returned an array with a directive to NOT * use wp_safe_redirect, use wp_redirect instead. */ if (isset($gateway_processing['safe']) && false == $gateway_processing['safe']) { wp_redirect($redirect_url, $status); die; } wp_safe_redirect($redirect_url, $status); die; }
function setUp() { parent::setUp(); $this->notices = charitable_get_notices(); }
/** * Check the passed fields to ensure that all required fields have been submitted. * * @param array $fields * @param array $submitted * @return boolean * @access public * @since 1.0.0 */ public function check_required_fields($fields, $submitted = array()) { if (empty($submitted)) { $submitted = $this->get_submitted_values(); } $missing = array(); foreach ($this->get_required_fields($fields) as $key => $field) { /* We already have a value for this field. */ if (!empty($field['value'])) { continue; } $exists = isset($submitted[$key]) && !empty($submitted[$key]); $exists = $exists || 'picture' == $field['type'] && isset($_FILES[$key]) && !empty($_FILES[$key]['name']); $exists = apply_filters('charitable_required_field_exists', $exists, $key, $field, $submitted, $this); if (!$exists) { $label = isset($field['label']) ? $field['label'] : $key; $missing[] = $label; } } if (count($missing)) { $missing_fields = implode('</li><li>', $missing); charitable_get_notices()->add_error(sprintf('<p>%s</p><ul class="error-list"><li>%s</li></ul>', __('There were problems with your form submission. The following required fields were not filled out:', 'charitable'), $missing_fields)); return false; } return true; }
/** * Save the submitted donation. * * @return int|false If successful, this returns the donation ID. If unsuccessful, returns false. * @access public * @since 1.0.0 */ public function save_donation() { $campaign_id = charitable_get_current_campaign_id(); if (!$campaign_id) { return 0; } if (!$this->validate_nonce()) { return 0; } /* Set the donation amount */ $campaign_id = $this->get_campaign()->ID; $amount = parent::get_donation_amount(); if (0 == $amount && !apply_filters('charitable_permit_empty_donations', false)) { charitable_get_notices()->add_error(__('No donation amount was set.', 'charitable')); return false; } /* Create or update the donation object in the session, with the current campaign ID. */ charitable_get_session()->add_donation($campaign_id, $amount); do_action('charitable_donation_amount_form_submit', $campaign_id, $amount); return true; }
/** * Check if a failed user login attempt originated from Charitable login form. * * If so redirect user to Charitable login page. * * @param WP_User|WP_Error $user_or_error * @param string $username * @return WP_User|void * @access public * @since 1.4.0 */ public function maybe_redirect_at_authenticate($user_or_error, $username) { if ('POST' != $_SERVER['REQUEST_METHOD']) { return $user_or_error; } if (!is_wp_error($user_or_error)) { return $user_or_error; } if (!isset($_POST['charitable']) || !$_POST['charitable']) { return $user_or_error; } foreach ($user_or_error->errors as $code => $error) { /* Make sure the error messages link to our forgot password page, not WordPress' */ switch ($code) { case 'invalid_email': $error = __('<strong>ERROR</strong>: Invalid email address.', 'charitable') . ' <a href="' . esc_url(charitable_get_permalink('forgot_password_page')) . '">' . __('Lost your password?') . '</a>'; break; case 'incorrect_password': $error = sprintf(__('<strong>ERROR</strong>: The password you entered for the email address %s is incorrect.'), '<strong>' . $email . '</strong>') . ' <a href="' . esc_url(charitable_get_permalink('forgot_password_page')) . '">' . __('Lost your password?') . '</a>'; break; default: $error = $error[0]; } charitable_get_notices()->add_error($error); } charitable_get_session()->add_notices(); $redirect_url = charitable_get_permalink('login_page'); if (strlen($username)) { $redirect_url = add_query_arg('username', $username, $redirect_url); } wp_safe_redirect(esc_url_raw($redirect_url)); exit; }
function test_charitable_get_notices() { $this->assertInstanceOf('Charitable_Notices', charitable_get_notices()); }
/** * Update registration after form submission. * * @return void * @access public * @static * @since 1.0.0 */ public static function save_registration() { $form = new Charitable_Registration_Form(); if (!$form->validate_nonce() || !$form->validate_honeypot()) { charitable_get_notices()->add_error(__('There was an error with processing your form submission. Please reload the page and try again.', 'charitable')); return; } $fields = $form->get_fields(); $valid = $form->check_required_fields($fields); if (!$valid) { return; } $submitted = apply_filters('charitable_registration_values', $_POST, $fields, $form); if (!isset($submitted['user_email']) || !is_email($submitted['user_email'])) { charitable_get_notices()->add_error(sprintf(__('%s is not a valid email address.', 'charitable'), $submitted['user_email'])); return false; } $user = new Charitable_User(); $user_id = $user->update_profile($submitted, array_keys($fields)); /** * If the user was successfully created, redirect to the login redirect URL. * If there was a problem, this simply falls through and keeps the user on the * registration page. */ if ($user_id) { wp_safe_redirect(charitable_get_login_redirect_url()); exit; } }