Completed
Push — pr-1740 ( cf8792 )
by Ravinder
306:55 queued 289:15
created

price-functions.php ➔ give_get_variable_price_ids()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
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     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
 * 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
	if ( empty( $form_id ) ) {
29
		return false;
30
	}
31
32
	$form = new Give_Donate_Form( $form_id );
33
34
	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|bool Variable prices
46
 */
47
function give_get_variable_prices( $form_id = 0 ) {
48
49
	if ( empty( $form_id ) ) {
50
		return false;
51
	}
52
53
	$form = new Give_Donate_Form( $form_id );
54
55
	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
 * Retrieves the variable price ids for a form
61
 *
62
 * @since 1.8.8
63
 *
64
 * @param int $form_id ID of the Give form
65
 *
66
 * @return array Variable prices
67
 */
68
function give_get_variable_price_ids( $form_id = 0 ) {
69
	if( ! ( $prices = give_get_variable_prices( $form_id ) ) ) {
70
		return array();
71
	}
72
73
	$price_ids = array();
74
	foreach ( $prices as $price ){
75
		$price_ids[] = $price['_give_id']['level_id'];
76
	}
77
78
	return $price_ids;
79
}
80
81
82
/**
83
 * Get the default amount for multi-level forms
84
 *
85
 * @access public
86
 * @since  1.0
87
 *
88
 * @param int $form_id
89
 *
90
 * @return string $default_price
91
 */
92
function give_get_default_multilevel_amount( $form_id ) {
93
	$default_price = '1.00';
94
	$prices        = apply_filters( 'give_form_variable_prices', give_get_variable_prices( $form_id ), $form_id );
95
96
	foreach ( $prices as $price ) {
97
98
		if ( isset( $price['_give_default'] ) && $price['_give_default'] === 'default' ) {
99
			$default_price = $price['_give_amount'];
100
		}
101
102
	}
103
104
	return $default_price;
105
106
}
107
108
109
/**
110
 * Get Default Form Amount
111
 *
112
 * Grabs the default amount for set and level forms
113
 *
114
 * @param int $form_id
115
 *
116
 * @return string $default_price
117
 * @since      1.0
118
 */
119
function give_get_default_form_amount( $form_id ) {
120
121
	if ( give_has_variable_prices( $form_id ) ) {
122
123
		$default_amount = give_get_default_multilevel_amount( $form_id );
124
125
	} else {
126
127
		$default_amount = give_get_meta( $form_id, '_give_set_price', true );
128
129
	}
130
131
	return apply_filters( 'give_default_form_amount', $default_amount );
132
133
}
134
135
136
/**
137
 * Determine if custom price mode is enabled or disabled.
138
 *
139
 * This function is wrapper function to Give_Donate_Form::is_custom_price_mode()
140
 *
141
 * @since 1.6
142
 *
143
 * @param int $form_id Form ID.
144
 *
145
 * @use   Give_Donate_Form::is_custom_price_mode()
146
 *
147
 * @return bool
148
 */
149
function give_is_custom_price_mode( $form_id = 0 ) {
150
151
	if ( empty( $form_id ) ) {
152
		return false;
153
	}
154
155
	$form = new Give_Donate_Form( $form_id );
156
157
	return $form->is_custom_price_mode();
158
}