setup_donation_form( $donation_form ); } /** * Given the donation form data, let's set the variables * * @since 1.5 * @access private * * @param WP_Post $donation_form WP_Post Object for the donation form. * * @return bool If the setup was successful or not. */ private function setup_donation_form( $donation_form ) { if ( ! is_object( $donation_form ) ) { return false; } if ( ! is_a( $donation_form, 'WP_Post' ) ) { return false; } if ( 'give_forms' !== $donation_form->post_type ) { return false; } foreach ( $donation_form as $key => $value ) { switch ( $key ) { default: $this->$key = $value; break; } } return true; } /** * Magic __get function to dispatch a call to retrieve a private property * * @since 1.0 * @access public * * @param string $key * * @return mixed */ public function __get( $key ) { if ( method_exists( $this, 'get_' . $key ) ) { return call_user_func( array( $this, 'get_' . $key ) ); } else { /* translators: %s: property key */ return new WP_Error( 'give-form-invalid-property', sprintf( esc_html__( 'Can\'t get property %s.', 'give' ), $key ) ); } } /** * Creates a donation form * * @since 1.5 * @access public * * @param array $data Array of attributes for a donation form. * * @return bool|int False if data isn't passed and class not instantiated for creation, or New Form ID. */ public function create( $data = array() ) { if ( $this->id != 0 ) { return false; } $defaults = array( 'post_type' => 'give_forms', 'post_status' => 'draft', 'post_title' => __( 'New Donation Form', 'give' ), ); $args = wp_parse_args( $data, $defaults ); /** * Fired before a donation form is created * * @param array $args The post object arguments used for creation. */ do_action( 'give_form_pre_create', $args ); $id = wp_insert_post( $args, true ); $donation_form = WP_Post::get_instance( $id ); /** * Fired after a donation form is created * * @param int $id The post ID of the created item. * @param array $args The post object arguments used for creation. */ do_action( 'give_form_post_create', $id, $args ); return $this->setup_donation_form( $donation_form ); } /** * Retrieve the ID * * @since 1.0 * @access public * * @return int Donation form ID. */ public function get_ID() { return $this->ID; } /** * Retrieve the donation form name * * @since 1.5 * @access public * * @return string Donation form name. */ public function get_name() { return get_the_title( $this->ID ); } /** * Retrieve the price * * @since 1.0 * @access public * * @return float Price. */ public function get_price() { if ( ! isset( $this->price ) ) { $this->price = give_maybe_sanitize_amount( give_get_meta( $this->ID, '_give_set_price', true ) ); if ( ! $this->price ) { $this->price = 0; } } /** * Override the donation form set price. * * @since 1.0 * * @param string $price The donation form price. * @param string|int $id The form ID. */ return apply_filters( 'give_get_set_price', $this->price, $this->ID ); } /** * Retrieve the minimum price. * * @since 1.3.6 * @access public * * @return float Minimum price. */ public function get_minimum_price() { if ( ! isset( $this->minimum_price ) ) { $this->minimum_price = give_get_meta( $this->ID, '_give_custom_amount_range_minimum', true ); // Give backward < 2.1 if ( empty( $this->minimum_price ) ) { $this->minimum_price = give_get_meta( $this->ID, '_give_custom_amount_minimum', true ); } if ( ! $this->is_custom_price_mode() ) { $this->minimum_price = give_get_lowest_price_option( $this->ID ); } } return apply_filters( 'give_get_set_minimum_price', $this->minimum_price, $this->ID ); } /** * Retrieve the maximum price. * * @since 2.1 * @access public * * @return float Maximum price. */ public function get_maximum_price() { if ( ! isset( $this->maximum_price ) ) { $this->maximum_price = give_get_meta( $this->ID, '_give_custom_amount_range_maximum', true, 999999.99 ); if ( ! $this->is_custom_price_mode() ) { $this->maximum_price = give_get_highest_price_option( $this->ID ); } } return apply_filters( 'give_get_set_maximum_price', $this->maximum_price, $this->ID ); } /** * Retrieve the variable prices * * @since 1.0 * @access public * * @return array Variable prices. */ public function get_prices() { if ( ! isset( $this->prices ) ) { $this->prices = give_get_meta( $this->ID, '_give_donation_levels', true ); } /** * Override multi-level prices * * @since 1.0 * * @param array $prices The array of mulit-level prices. * @param int|string $ID The ID of the form. */ return apply_filters( 'give_get_donation_levels', $this->prices, $this->ID ); } /** * Get donation form level info * * @since 2.0.6 * @access public * * @param $price_id * * @return array|null */ public function get_level_info( $price_id ) { $level_info = array(); // Bailout. if ( 'multi' !== $this->get_type() ) { return null; } elseif ( ! ( $levels = $this->get_prices() ) ) { return $level_info; } foreach ( $levels as $level ) { if ( $price_id === $level['_give_id']['level_id'] ) { $level_info = $level; break; } } return $level_info; } /** * Retrieve the goal * * @since 1.0 * @access public * * @return float Goal. */ public function get_goal() { if ( ! isset( $this->goal ) ) { $goal_format = give_get_form_goal_format( $this->ID ); if ( 'donation' === $goal_format ) { $this->goal = give_get_meta( $this->ID, '_give_number_of_donation_goal', true ); } elseif ( 'donors' === $goal_format ) { $this->goal = give_get_meta( $this->ID, '_give_number_of_donor_goal', true ); } else { $this->goal = give_get_meta( $this->ID, '_give_set_goal', true ); } if ( ! $this->goal ) { $this->goal = 0; } } return apply_filters( 'give_get_set_goal', $this->goal, $this->ID ); } /** * Determine if single price mode is enabled or disabled * * @since 1.0 * @access public * * @return bool */ public function is_single_price_mode() { $option = give_get_meta( $this->ID, '_give_price_option', true ); $ret = 0; if ( empty( $option ) || $option === 'set' ) { $ret = 1; } /** * Override the price mode for a donation when checking if is in single price mode. * * @since 1.0 * * @param bool $ret Is donation form in single price mode? * @param int|string $ID The ID of the donation form. */ return (bool) apply_filters( 'give_single_price_option_mode', $ret, $this->ID ); } /** * Determine if custom price mode is enabled or disabled * * @since 1.6 * @access public * * @return bool */ public function is_custom_price_mode() { $option = give_get_meta( $this->ID, '_give_custom_amount', true ); $ret = 0; if ( give_is_setting_enabled( $option ) ) { $ret = 1; } /** * Override the price mode for a donation when checking if is in custom price mode. * * @since 1.6 * * @param bool $ret Is donation form in custom price mode? * @param int|string $ID The ID of the donation form. */ return (bool) apply_filters( 'give_custom_price_option_mode', $ret, $this->ID ); } /** * Determine if custom price mode is enabled or disabled * * @since 1.8.18 * @access public * * @param string|float $amount Donation Amount. * * @return bool */ public function is_custom_price( $amount ) { $result = false; $amount = give_maybe_sanitize_amount( $amount ); if ( $this->is_custom_price_mode() ) { if ( 'set' === $this->get_type() ) { if ( $amount !== $this->get_price() ) { $result = true; } } elseif ( 'multi' === $this->get_type() ) { $level_amounts = array_map( 'give_maybe_sanitize_amount', wp_list_pluck( $this->get_prices(), '_give_amount' ) ); $result = ! in_array( $amount, $level_amounts ); } } /** * Filter to reset whether it is custom price or not. * * @param bool $result True/False. * @param string|float $amount Donation Amount. * @param int $this ->ID Form ID. * * @since 1.8.18 */ return (bool) apply_filters( 'give_is_custom_price', $result, $amount, $this->ID ); } /** * Has Variable Prices * * Determine if the donation form has variable prices enabled * * @since 1.0 * @access public * * @return bool */ public function has_variable_prices() { $option = give_get_meta( $this->ID, '_give_price_option', true ); $ret = 0; if ( $option === 'multi' ) { $ret = 1; } /** * Filter: Override whether the donation form has variables prices. * * @param bool $ret Does donation form have variable prices? * @param int|string $ID The ID of the donation form. */ return (bool) apply_filters( 'give_has_variable_prices', $ret, $this->ID ); } /** * Retrieve the donation form type, set or multi-level * * @since 1.5 * @access public * * @return string Type of donation form, either 'set' or 'multi'. */ public function get_type() { if ( ! isset( $this->type ) ) { $this->type = give_get_meta( $this->ID, '_give_price_option', true ); if ( empty( $this->type ) ) { $this->type = 'set'; } } return apply_filters( 'give_get_form_type', $this->type, $this->ID ); } /** * Get form tag classes. * * Provides the classes for the donation