Completed
Pull Request — master (#859)
by Devin
19:40
created

price-functions.php ➔ give_is_custom_price_mode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 9.4285
ccs 0
cts 0
cp 0
crap 6
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 26 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Price Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Functions
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Checks to see if a Give form has variable prices enabled.
19
 *
20
 * @since 1.0
21
 *
22
 * @param int $form_id ID number of the form to check
23
 *
24
 * @return bool true if has variable prices, false otherwise
25
 */
26
function give_has_variable_prices( $form_id = 0 ) {
27
28 53
	if ( empty( $form_id ) ) {
29 1
		return false;
30
	}
31
32 53
	$form = new Give_Donate_Form( $form_id );
33
34 53
	return $form->has_variable_prices();
35
}
36
37
38
/**
39
 * Retrieves the variable prices for a form
40
 *
41
 * @since 1.0
42
 *
43
 * @param int $form_id ID of the Give form
44
 *
45
 * @return array Variable prices
46
 */
47
function give_get_variable_prices( $form_id = 0 ) {
48
49 35
	if ( empty( $form_id ) ) {
50
		return false;
51
	}
52
53 35
	$form = new Give_Donate_Form( $form_id );
54
55 35
	return $form->prices;
0 ignored issues
show
Documentation introduced by
The property $prices is declared private in Give_Donate_Form. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
56
57
}
58
59
60
/**
61
 * Get the default amount for multi-level forms
62
 *
63
 * @access public
64
 * @since  1.0
65
 *
66
 * @param int $form_id
67
 *
68
 * @return string $default_price
69
 */
70
function give_get_default_multilevel_amount( $form_id ) {
71 1
	$default_price = '1.00';
72 1
	$prices        = apply_filters( 'give_form_variable_prices', give_get_variable_prices( $form_id ), $form_id );
73
74 1
	foreach ( $prices as $price ) {
75
76 1
		if ( isset( $price['_give_default'] ) && $price['_give_default'] === 'default' ) {
77 1
			$default_price = $price['_give_amount'];
78 1
		}
79
80 1
	}
81
82 1
	return $default_price;
83
84
}
85
86
87
/**
88
 * Get Default Form Amount
89
 *
90
 * Grabs the default amount for set and level forms
91
 *
92
 * @param int $form_id
93
 *
94
 * @return string $default_price
95
 * @since      1.0
96
 */
97
function give_get_default_form_amount( $form_id ) {
98
99 1
	if ( give_has_variable_prices( $form_id ) ) {
100
101 1
		$default_amount = give_get_default_multilevel_amount( $form_id );
102
103 1
	} else {
104
105
		$default_amount = get_post_meta( $form_id, '_give_set_price', true );
106
107
	}
108
109 1
	return apply_filters('give_default_form_amount', $default_amount);
110
111
}
112
113
114
/**
115
 * Determine if custom price mode is enabled or disabled
116
 * This function is wrapper function to Give_Donate_Form::is_custom_price_mode()
117
 *
118
 * @since 1.6
119
 * 
120
 * @param int $form_id Form ID.
121
 *
122
 * @use Give_Donate_Form::is_custom_price_mode()
123
 * 
124
 * @return bool
125
 */
126
function give_is_custom_price_mode( $form_id = 0 ) {
127
128
	if ( empty( $form_id ) ) {
129
		return false;
130
	}
131
132
	$form = new Give_Donate_Form( $form_id );
133
134
	return $form->is_custom_price_mode();
135
}