Completed
Pull Request — master (#939)
by
unknown
18:15 queued 08:54
created
core/libraries/form_sections/inputs/EE_Admin_File_Uploader_Input.input.php 1 patch
Indentation   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -14,27 +14,27 @@
 block discarded – undo
14 14
 class EE_Admin_File_Uploader_Input extends EE_Form_Input_Base
15 15
 {
16 16
 
17
-    /**
18
-     * @param array $input_settings
19
-     * @throws InvalidArgumentException
20
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
21
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
22
-     */
23
-    public function __construct($input_settings = array())
24
-    {
25
-        $this->_set_display_strategy(new EE_Admin_File_Uploader_Display_Strategy());
26
-        $this->_set_normalization_strategy(new EE_Text_Normalization());
27
-        $this->_add_validation_strategy(
28
-            LoaderFactory::getLoader()->getNew(
29
-                'EE_URL_Validation_Strategy',
30
-                array(
31
-                    isset($input_settings['validation_error_message'])
32
-                        ? $input_settings['validation_error_message']
33
-                        : null,
34
-                    false
35
-                )
36
-            )
37
-        );
38
-        parent::__construct($input_settings);
39
-    }
17
+	/**
18
+	 * @param array $input_settings
19
+	 * @throws InvalidArgumentException
20
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
21
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
22
+	 */
23
+	public function __construct($input_settings = array())
24
+	{
25
+		$this->_set_display_strategy(new EE_Admin_File_Uploader_Display_Strategy());
26
+		$this->_set_normalization_strategy(new EE_Text_Normalization());
27
+		$this->_add_validation_strategy(
28
+			LoaderFactory::getLoader()->getNew(
29
+				'EE_URL_Validation_Strategy',
30
+				array(
31
+					isset($input_settings['validation_error_message'])
32
+						? $input_settings['validation_error_message']
33
+						: null,
34
+					false
35
+				)
36
+			)
37
+		);
38
+		parent::__construct($input_settings);
39
+	}
40 40
 }
Please login to merge, or discard this patch.
core/db_models/EEM_Payment_Method.model.php 2 patches
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -44,7 +44,7 @@  discard block
 block discarded – undo
44 44
     {
45 45
         $this->singlular_item = __('Payment Method', 'event_espresso');
46 46
         $this->plural_item = __('Payment Methods', 'event_espresso');
47
-        $this->_tables = array( 'Payment_Method' => new EE_Primary_Table('esp_payment_method', 'PMD_ID') );
47
+        $this->_tables = array('Payment_Method' => new EE_Primary_Table('esp_payment_method', 'PMD_ID'));
48 48
         $this->_fields = array(
49 49
             'Payment_Method' => array(
50 50
                 'PMD_ID' => new EE_Primary_Key_Int_Field('PMD_ID', __("ID", 'event_espresso')),
@@ -78,7 +78,7 @@  discard block
 block discarded – undo
78 78
      */
79 79
     public function get_one_by_slug($slug)
80 80
     {
81
-        return $this->get_one(array( array( 'PMD_slug' => $slug ) ));
81
+        return $this->get_one(array(array('PMD_slug' => $slug)));
82 82
     }
83 83
 
84 84
 
@@ -109,7 +109,7 @@  discard block
 block discarded – undo
109 109
     public function is_valid_scope($scope)
110 110
     {
111 111
         $scopes = $this->scopes();
112
-        if (isset($scopes[ $scope ])) {
112
+        if (isset($scopes[$scope])) {
113 113
             return true;
114 114
         } else {
115 115
             return false;
@@ -127,8 +127,8 @@  discard block
 block discarded – undo
127 127
      */
128 128
     public function get_all_active($scope = null, $query_params = array())
129 129
     {
130
-        if (! isset($query_params['order_by']) && ! isset($query_params['order'])) {
131
-            $query_params['order_by'] = array( 'PMD_order' => 'ASC', 'PMD_ID' => 'ASC' );
130
+        if ( ! isset($query_params['order_by']) && ! isset($query_params['order'])) {
131
+            $query_params['order_by'] = array('PMD_order' => 'ASC', 'PMD_ID' => 'ASC');
132 132
         }
133 133
         return $this->get_all($this->_get_query_params_for_all_active($scope, $query_params));
134 134
     }
@@ -156,7 +156,7 @@  discard block
 block discarded – undo
156 156
     {
157 157
         if ($scope) {
158 158
             if ($this->is_valid_scope($scope)) {
159
-                return array_replace_recursive(array( array( 'PMD_scope' => array( 'LIKE', "%$scope%" ) ) ), $query_params);
159
+                return array_replace_recursive(array(array('PMD_scope' => array('LIKE', "%$scope%"))), $query_params);
160 160
             } else {
161 161
                 throw new EE_Error(sprintf(__("'%s' is not a valid scope for a payment method", "event_espresso"), $scope));
162 162
             }
@@ -165,9 +165,9 @@  discard block
 block discarded – undo
165 165
             $count = 0;
166 166
             foreach ($this->scopes() as $scope_name => $desc) {
167 167
                 $count++;
168
-                $acceptable_scopes[ 'PMD_scope*' . $count ] = array( 'LIKE', '%' . $scope_name . '%' );
168
+                $acceptable_scopes['PMD_scope*'.$count] = array('LIKE', '%'.$scope_name.'%');
169 169
             }
170
-            return array_replace_recursive(array( array( 'OR*active_scope' => $acceptable_scopes ) ), $query_params);
170
+            return array_replace_recursive(array(array('OR*active_scope' => $acceptable_scopes)), $query_params);
171 171
         }
172 172
     }
173 173
 
@@ -205,7 +205,7 @@  discard block
 block discarded – undo
205 205
      */
206 206
     public function get_one_of_type($type)
207 207
     {
208
-        return $this->get_one(array( array( 'PMD_type' => $type ) ));
208
+        return $this->get_one(array(array('PMD_type' => $type)));
209 209
     }
210 210
 
211 211
 
@@ -298,7 +298,7 @@  discard block
 block discarded – undo
298 298
         $usable_payment_methods = array();
299 299
         foreach ($payment_methods as $key => $payment_method) {
300 300
             if (EE_Payment_Method_Manager::instance()->payment_method_type_exists($payment_method->type())) {
301
-                $usable_payment_methods[ $key ] = $payment_method;
301
+                $usable_payment_methods[$key] = $payment_method;
302 302
                 // some payment methods enqueue their scripts in EE_PMT_*::__construct
303 303
                 // which is kinda a no-no (just because it's being constructed doesn't mean we need to enqueue
304 304
                 // its scripts). but for backwards-compat we should continue to do that
@@ -312,7 +312,7 @@  discard block
 block discarded – undo
312 312
                     $payment_method
313 313
                 );
314 314
                 new PersistentAdminNotice(
315
-                    'auto-deactivated-' . $payment_method->type(),
315
+                    'auto-deactivated-'.$payment_method->type(),
316 316
                     sprintf(
317 317
                         __(
318 318
                             'The payment method %1$s was automatically deactivated because it appears its associated Event Espresso Addon was recently deactivated.%2$sIt can be reactivated on the %3$sPlugins admin page%4$s, then you can reactivate the payment method.',
@@ -320,7 +320,7 @@  discard block
 block discarded – undo
320 320
                         ),
321 321
                         $payment_method->admin_name(),
322 322
                         '<br />',
323
-                        '<a href="' . admin_url('plugins.php') . '">',
323
+                        '<a href="'.admin_url('plugins.php').'">',
324 324
                         '</a>'
325 325
                     ),
326 326
                     true
@@ -345,7 +345,7 @@  discard block
 block discarded – undo
345 345
         // give addons a chance to override what payment methods are chosen based on the transaction
346 346
         return apply_filters(
347 347
             'FHEE__EEM_Payment_Method__get_all_for_transaction__payment_methods',
348
-            $this->get_all_active($scope, array( 'group_by' => 'PMD_type' )),
348
+            $this->get_all_active($scope, array('group_by' => 'PMD_type')),
349 349
             $transaction,
350 350
             $scope
351 351
         );
@@ -369,7 +369,7 @@  discard block
 block discarded – undo
369 369
             0 => array(
370 370
                 'Payment.Registration.REG_ID' => $registration_id,
371 371
             ),
372
-            'order_by' => array( 'Payment.PAY_ID' => 'DESC' )
372
+            'order_by' => array('Payment.PAY_ID' => 'DESC')
373 373
         );
374 374
         return $this->get_one($query_params);
375 375
     }
Please login to merge, or discard this patch.
Indentation   +359 added lines, -359 removed lines patch added patch discarded remove patch
@@ -20,363 +20,363 @@
 block discarded – undo
20 20
 class EEM_Payment_Method extends EEM_Base
21 21
 {
22 22
 
23
-    const scope_cart = 'CART';
24
-
25
-    const scope_admin = 'ADMIN';
26
-
27
-    const scope_api = 'API';
28
-
29
-    /**
30
-     *
31
-     * @type EEM_Payment_Method
32
-     */
33
-    protected static $_instance = null;
34
-
35
-
36
-
37
-    /**
38
-     * private constructor to prevent direct creation
39
-     * @Constructor
40
-     * @access   protected
41
-     * @return EEM_Payment_Method
42
-     */
43
-    protected function __construct($timezone = null)
44
-    {
45
-        $this->singlular_item = __('Payment Method', 'event_espresso');
46
-        $this->plural_item = __('Payment Methods', 'event_espresso');
47
-        $this->_tables = array( 'Payment_Method' => new EE_Primary_Table('esp_payment_method', 'PMD_ID') );
48
-        $this->_fields = array(
49
-            'Payment_Method' => array(
50
-                'PMD_ID' => new EE_Primary_Key_Int_Field('PMD_ID', __("ID", 'event_espresso')),
51
-                'PMD_type' => new EE_Plain_Text_Field('PMD_type', __("Payment Method Type", 'event_espresso'), false, 'Admin_Only'),
52
-                'PMD_name' => new EE_Plain_Text_Field('PMD_name', __("Name", 'event_espresso'), false),
53
-                'PMD_desc' => new EE_Post_Content_Field('PMD_desc', __("Description", 'event_espresso'), false, ''),
54
-                'PMD_admin_name' => new EE_Plain_Text_Field('PMD_admin_name', __("Admin-Only Name", 'event_espresso'), true),
55
-                'PMD_admin_desc' => new EE_Post_Content_Field('PMD_admin_desc', __("Admin-Only Description", 'event_espresso'), true),
56
-                'PMD_slug' => new EE_Slug_Field('PMD_slug', __("Slug", 'event_espresso'), false),
57
-                'PMD_order' => new EE_Integer_Field('PMD_order', __("Order", 'event_espresso'), false, 0),
58
-                'PMD_debug_mode' => new EE_Boolean_Field('PMD_debug_mode', __("Debug Mode On?", 'event_espresso'), false, false),
59
-                'PMD_wp_user' => new EE_WP_User_Field('PMD_wp_user', __("Payment Method Creator ID", 'event_espresso'), false),
60
-                'PMD_open_by_default' => new EE_Boolean_Field('PMD_open_by_default', __("Open by Default?", 'event_espresso'), false, false), 'PMD_button_url' => new EE_Plain_Text_Field('PMD_button_url', __("Button URL", 'event_espresso'), true, ''),
61
-                'PMD_scope' => new EE_Serialized_Text_Field('PMD_scope', __("Usable From?", 'event_espresso'), false, array()), // possible values currently are 'CART','ADMIN','API'
62
-        ) );
63
-        $this->_model_relations = array(
64
-            'Payment' => new EE_Has_Many_Relation(),
65
-            'Currency' => new EE_HABTM_Relation('Currency_Payment_Method'),
66
-            'Transaction' => new EE_Has_Many_Relation(),
67
-            'WP_User' => new EE_Belongs_To_Relation(),
68
-        );
69
-        parent::__construct($timezone);
70
-    }
71
-
72
-
73
-
74
-    /**
75
-     * Gets one by the slug provided
76
-     * @param string $slug
77
-     * @return EE_Payment_Method
78
-     */
79
-    public function get_one_by_slug($slug)
80
-    {
81
-        return $this->get_one(array( array( 'PMD_slug' => $slug ) ));
82
-    }
83
-
84
-
85
-
86
-    /**
87
-     * Gets all the acceptable scopes for payment methods.
88
-     * Keys are their names as store din the DB, and values are nice names for displaying them
89
-     * @return array
90
-     */
91
-    public function scopes()
92
-    {
93
-        return apply_filters(
94
-            'FHEE__EEM_Payment_Method__scopes',
95
-            array(
96
-                self::scope_cart        => __("Front-end Registration Page", 'event_espresso'),
97
-                self::scope_admin   => __("Admin Registration Page (no online processing)", 'event_espresso')
98
-            )
99
-        );
100
-    }
101
-
102
-
103
-
104
-    /**
105
-     * Determines if this is an valid scope
106
-     * @param string $scope like one of EEM_Payment_Method::instance()->scopes()
107
-     * @return boolean
108
-     */
109
-    public function is_valid_scope($scope)
110
-    {
111
-        $scopes = $this->scopes();
112
-        if (isset($scopes[ $scope ])) {
113
-            return true;
114
-        } else {
115
-            return false;
116
-        }
117
-    }
118
-
119
-
120
-
121
-    /**
122
-     * Gets all active payment methods
123
-     * @param string $scope one of
124
-     * @param array  $query_params
125
-     * @throws EE_Error
126
-     * @return EE_Payment_Method[]
127
-     */
128
-    public function get_all_active($scope = null, $query_params = array())
129
-    {
130
-        if (! isset($query_params['order_by']) && ! isset($query_params['order'])) {
131
-            $query_params['order_by'] = array( 'PMD_order' => 'ASC', 'PMD_ID' => 'ASC' );
132
-        }
133
-        return $this->get_all($this->_get_query_params_for_all_active($scope, $query_params));
134
-    }
135
-
136
-    /**
137
-     * Counts all active gateways in the specified scope
138
-     * @param string $scope one of EEM_Payment_Method::scope_*
139
-     * @param array $query_params
140
-     * @return int
141
-     */
142
-    public function count_active($scope = null, $query_params = array())
143
-    {
144
-        return $this->count($this->_get_query_params_for_all_active($scope, $query_params));
145
-    }
146
-
147
-    /**
148
-     * Creates the $query_params that can be passed into any EEM_Payment_Method as their $query_params
149
-     * argument to get all active for a given scope
150
-     * @param string $scope one of the constants EEM_Payment_Method::scope_*
151
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
152
-     * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
153
-     * @throws EE_Error
154
-     */
155
-    protected function _get_query_params_for_all_active($scope = null, $query_params = array())
156
-    {
157
-        if ($scope) {
158
-            if ($this->is_valid_scope($scope)) {
159
-                return array_replace_recursive(array( array( 'PMD_scope' => array( 'LIKE', "%$scope%" ) ) ), $query_params);
160
-            } else {
161
-                throw new EE_Error(sprintf(__("'%s' is not a valid scope for a payment method", "event_espresso"), $scope));
162
-            }
163
-        } else {
164
-            $acceptable_scopes = array();
165
-            $count = 0;
166
-            foreach ($this->scopes() as $scope_name => $desc) {
167
-                $count++;
168
-                $acceptable_scopes[ 'PMD_scope*' . $count ] = array( 'LIKE', '%' . $scope_name . '%' );
169
-            }
170
-            return array_replace_recursive(array( array( 'OR*active_scope' => $acceptable_scopes ) ), $query_params);
171
-        }
172
-    }
173
-
174
-    /**
175
-     * Creates the $query_params that can be passed into any EEM_Payment_Method as their $query_params
176
-     * argument to get all active for a given scope
177
-     * @param string $scope one of the constants EEM_Payment_Method::scope_*
178
-     * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
179
-     * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
180
-     * @throws EE_Error
181
-     */
182
-    public function get_query_params_for_all_active($scope = null, $query_params = array())
183
-    {
184
-        return $this->_get_query_params_for_all_active($scope, $query_params);
185
-    }
186
-
187
-
188
-    /**
189
-     * Gets one active payment method. see @get_all_active for documentation
190
-     * @param string $scope
191
-     * @param array  $query_params
192
-     * @return EE_Payment_Method
193
-     */
194
-    public function get_one_active($scope = null, $query_params = array())
195
-    {
196
-        return $this->get_one($this->_get_query_params_for_all_active($scope, $query_params));
197
-    }
198
-
199
-
200
-
201
-    /**
202
-     * Gets one payment method of that type, regardless of whether its active or not
203
-     * @param string $type
204
-     * @return EE_Payment_Method
205
-     */
206
-    public function get_one_of_type($type)
207
-    {
208
-        return $this->get_one(array( array( 'PMD_type' => $type ) ));
209
-    }
210
-
211
-
212
-
213
-    /**
214
-     * Overrides parent ot also check by the slug
215
-     * @see EEM_Base::ensure_is_obj()
216
-     * @param string|int|EE_Payment_Method $base_class_obj_or_id
217
-     * @param boolean                      $ensure_is_in_db
218
-     * @return EE_Payment_Method
219
-     * @throws EE_Error
220
-     */
221
-    public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = false)
222
-    {
223
-        // first: check if it's a slug
224
-        if (is_string($base_class_obj_or_id)) {
225
-            $obj = $this->get_one_by_slug($base_class_obj_or_id);
226
-            if ($obj) {
227
-                return $obj;
228
-            }
229
-        }
230
-        // ok so it wasn't a slug we were passed. try the usual then (ie, it's an object or an ID)
231
-        try {
232
-            return parent::ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db);
233
-        } catch (EE_Error $e) {
234
-            // handle it outside the catch
235
-        }
236
-        throw new EE_Error(sprintf(__("'%s' is neither a Payment Method ID, slug, nor object.", "event_espresso"), $base_class_obj_or_id));
237
-    }
238
-
239
-
240
-
241
-    /**
242
-     * Gets the ID of this object, or if its a string finds the object's id
243
-     * associated with that slug
244
-     * @param mixed $base_obj_or_id_or_slug
245
-     * @return int
246
-     */
247
-    public function ensure_is_ID($base_obj_or_id_or_slug)
248
-    {
249
-        if (is_string($base_obj_or_id_or_slug)) {
250
-            // assume it's a slug
251
-            $base_obj_or_id_or_slug = $this->get_one_by_slug($base_obj_or_id_or_slug);
252
-        }
253
-        return parent::ensure_is_ID($base_obj_or_id_or_slug);
254
-    }
255
-
256
-
257
-
258
-    /**
259
-     * Verifies the button urls on all the passed payment methods have a valid button url. If not, resets them to their default.
260
-     * @param EE_Payment_Method[] $payment_methods. If NULL is provided defaults to all payment methods active in the cart
261
-     */
262
-    public function verify_button_urls($payment_methods = null)
263
-    {
264
-        $payment_methods = is_array($payment_methods) ? $payment_methods : $this->get_all_active(EEM_Payment_Method::scope_cart);
265
-        foreach ($payment_methods as $payment_method) {
266
-            try {
267
-                // If there is really no button URL at all, or if the button URLs still point to decaf folder even
268
-                // though this is a caffeinated install, reset it to the default.
269
-                $current_button_url = $payment_method->button_url();
270
-                if (empty($current_button_url)
271
-                || (
272
-                        strpos($current_button_url, 'decaf') !== false
273
-                        && strpos($payment_method->type_obj()->default_button_url(), 'decaf') === false
274
-                    )
275
-                ) {
276
-                    $payment_method->save(
277
-                        [
278
-                            'PMD_button_url' => $payment_method->type_obj()->default_button_url()
279
-                        ]
280
-                    );
281
-                }
282
-            } catch (EE_Error $e) {
283
-                $payment_method->set_active(false);
284
-            }
285
-        }
286
-    }
287
-
288
-
289
-
290
-    /**
291
-     * Overrides parent to not only turn wpdb results into EE_Payment_Method objects,
292
-     * but also verifies the payment method type of each is a usable object. If not,
293
-     * deactivate it, sets a notification, and deactivates it
294
-     *
295
-     * @param array $rows
296
-     * @return EE_Payment_Method[]
297
-     * @throws InvalidDataTypeException
298
-     */
299
-    protected function _create_objects($rows = array())
300
-    {
301
-        EE_Registry::instance()->load_lib('Payment_Method_Manager');
302
-        $payment_methods = parent::_create_objects($rows);
303
-        /* @var $payment_methods EE_Payment_Method[] */
304
-        $usable_payment_methods = array();
305
-        foreach ($payment_methods as $key => $payment_method) {
306
-            if (EE_Payment_Method_Manager::instance()->payment_method_type_exists($payment_method->type())) {
307
-                $usable_payment_methods[ $key ] = $payment_method;
308
-                // some payment methods enqueue their scripts in EE_PMT_*::__construct
309
-                // which is kinda a no-no (just because it's being constructed doesn't mean we need to enqueue
310
-                // its scripts). but for backwards-compat we should continue to do that
311
-                $payment_method->type_obj();
312
-            } elseif ($payment_method->active()) {
313
-                // only deactivate and notify the admin if the payment is active somewhere
314
-                $payment_method->deactivate();
315
-                $payment_method->save();
316
-                do_action(
317
-                    'AHEE__EEM_Payment_Method___create_objects_auto_deactivated_payment_method',
318
-                    $payment_method
319
-                );
320
-                new PersistentAdminNotice(
321
-                    'auto-deactivated-' . $payment_method->type(),
322
-                    sprintf(
323
-                        __(
324
-                            'The payment method %1$s was automatically deactivated because it appears its associated Event Espresso Addon was recently deactivated.%2$sIt can be reactivated on the %3$sPlugins admin page%4$s, then you can reactivate the payment method.',
325
-                            'event_espresso'
326
-                        ),
327
-                        $payment_method->admin_name(),
328
-                        '<br />',
329
-                        '<a href="' . admin_url('plugins.php') . '">',
330
-                        '</a>'
331
-                    ),
332
-                    true
333
-                );
334
-            }
335
-        }
336
-        return $usable_payment_methods;
337
-    }
338
-
339
-
340
-
341
-    /**
342
-     * Gets all the payment methods which can be used for transaction
343
-     * (according to the relations between payment methods and events, and
344
-     * the currencies used for the transaction and their relation to payment methods)
345
-     * @param EE_Transaction $transaction
346
-     * @param string    $scope @see EEM_Payment_Method::get_all_for_events
347
-     * @return EE_Payment_Method[]
348
-     */
349
-    public function get_all_for_transaction($transaction, $scope)
350
-    {
351
-        // give addons a chance to override what payment methods are chosen based on the transaction
352
-        return apply_filters(
353
-            'FHEE__EEM_Payment_Method__get_all_for_transaction__payment_methods',
354
-            $this->get_all_active($scope, array( 'group_by' => 'PMD_type' )),
355
-            $transaction,
356
-            $scope
357
-        );
358
-    }
359
-
360
-
361
-    /**
362
-     * Returns the payment method used for the last payment made for a registration.
363
-     *
364
-     * Note: if an offline payment method was selected on the related transaction then this will have no payment methods returned.
365
-     * It will ONLY return a payment method for a PAYMENT recorded against the registration.
366
-     *
367
-     * @param EE_Registration|int $registration_or_reg_id  Either the EE_Registration object or the id for the registration.
368
-     * @return EE_Payment|null
369
-     */
370
-    public function get_last_used_for_registration($registration_or_reg_id)
371
-    {
372
-        $registration_id = EEM_Registration::instance()->ensure_is_ID($registration_or_reg_id);
373
-
374
-        $query_params = array(
375
-            0 => array(
376
-                'Payment.Registration.REG_ID' => $registration_id,
377
-            ),
378
-            'order_by' => array( 'Payment.PAY_ID' => 'DESC' )
379
-        );
380
-        return $this->get_one($query_params);
381
-    }
23
+	const scope_cart = 'CART';
24
+
25
+	const scope_admin = 'ADMIN';
26
+
27
+	const scope_api = 'API';
28
+
29
+	/**
30
+	 *
31
+	 * @type EEM_Payment_Method
32
+	 */
33
+	protected static $_instance = null;
34
+
35
+
36
+
37
+	/**
38
+	 * private constructor to prevent direct creation
39
+	 * @Constructor
40
+	 * @access   protected
41
+	 * @return EEM_Payment_Method
42
+	 */
43
+	protected function __construct($timezone = null)
44
+	{
45
+		$this->singlular_item = __('Payment Method', 'event_espresso');
46
+		$this->plural_item = __('Payment Methods', 'event_espresso');
47
+		$this->_tables = array( 'Payment_Method' => new EE_Primary_Table('esp_payment_method', 'PMD_ID') );
48
+		$this->_fields = array(
49
+			'Payment_Method' => array(
50
+				'PMD_ID' => new EE_Primary_Key_Int_Field('PMD_ID', __("ID", 'event_espresso')),
51
+				'PMD_type' => new EE_Plain_Text_Field('PMD_type', __("Payment Method Type", 'event_espresso'), false, 'Admin_Only'),
52
+				'PMD_name' => new EE_Plain_Text_Field('PMD_name', __("Name", 'event_espresso'), false),
53
+				'PMD_desc' => new EE_Post_Content_Field('PMD_desc', __("Description", 'event_espresso'), false, ''),
54
+				'PMD_admin_name' => new EE_Plain_Text_Field('PMD_admin_name', __("Admin-Only Name", 'event_espresso'), true),
55
+				'PMD_admin_desc' => new EE_Post_Content_Field('PMD_admin_desc', __("Admin-Only Description", 'event_espresso'), true),
56
+				'PMD_slug' => new EE_Slug_Field('PMD_slug', __("Slug", 'event_espresso'), false),
57
+				'PMD_order' => new EE_Integer_Field('PMD_order', __("Order", 'event_espresso'), false, 0),
58
+				'PMD_debug_mode' => new EE_Boolean_Field('PMD_debug_mode', __("Debug Mode On?", 'event_espresso'), false, false),
59
+				'PMD_wp_user' => new EE_WP_User_Field('PMD_wp_user', __("Payment Method Creator ID", 'event_espresso'), false),
60
+				'PMD_open_by_default' => new EE_Boolean_Field('PMD_open_by_default', __("Open by Default?", 'event_espresso'), false, false), 'PMD_button_url' => new EE_Plain_Text_Field('PMD_button_url', __("Button URL", 'event_espresso'), true, ''),
61
+				'PMD_scope' => new EE_Serialized_Text_Field('PMD_scope', __("Usable From?", 'event_espresso'), false, array()), // possible values currently are 'CART','ADMIN','API'
62
+		) );
63
+		$this->_model_relations = array(
64
+			'Payment' => new EE_Has_Many_Relation(),
65
+			'Currency' => new EE_HABTM_Relation('Currency_Payment_Method'),
66
+			'Transaction' => new EE_Has_Many_Relation(),
67
+			'WP_User' => new EE_Belongs_To_Relation(),
68
+		);
69
+		parent::__construct($timezone);
70
+	}
71
+
72
+
73
+
74
+	/**
75
+	 * Gets one by the slug provided
76
+	 * @param string $slug
77
+	 * @return EE_Payment_Method
78
+	 */
79
+	public function get_one_by_slug($slug)
80
+	{
81
+		return $this->get_one(array( array( 'PMD_slug' => $slug ) ));
82
+	}
83
+
84
+
85
+
86
+	/**
87
+	 * Gets all the acceptable scopes for payment methods.
88
+	 * Keys are their names as store din the DB, and values are nice names for displaying them
89
+	 * @return array
90
+	 */
91
+	public function scopes()
92
+	{
93
+		return apply_filters(
94
+			'FHEE__EEM_Payment_Method__scopes',
95
+			array(
96
+				self::scope_cart        => __("Front-end Registration Page", 'event_espresso'),
97
+				self::scope_admin   => __("Admin Registration Page (no online processing)", 'event_espresso')
98
+			)
99
+		);
100
+	}
101
+
102
+
103
+
104
+	/**
105
+	 * Determines if this is an valid scope
106
+	 * @param string $scope like one of EEM_Payment_Method::instance()->scopes()
107
+	 * @return boolean
108
+	 */
109
+	public function is_valid_scope($scope)
110
+	{
111
+		$scopes = $this->scopes();
112
+		if (isset($scopes[ $scope ])) {
113
+			return true;
114
+		} else {
115
+			return false;
116
+		}
117
+	}
118
+
119
+
120
+
121
+	/**
122
+	 * Gets all active payment methods
123
+	 * @param string $scope one of
124
+	 * @param array  $query_params
125
+	 * @throws EE_Error
126
+	 * @return EE_Payment_Method[]
127
+	 */
128
+	public function get_all_active($scope = null, $query_params = array())
129
+	{
130
+		if (! isset($query_params['order_by']) && ! isset($query_params['order'])) {
131
+			$query_params['order_by'] = array( 'PMD_order' => 'ASC', 'PMD_ID' => 'ASC' );
132
+		}
133
+		return $this->get_all($this->_get_query_params_for_all_active($scope, $query_params));
134
+	}
135
+
136
+	/**
137
+	 * Counts all active gateways in the specified scope
138
+	 * @param string $scope one of EEM_Payment_Method::scope_*
139
+	 * @param array $query_params
140
+	 * @return int
141
+	 */
142
+	public function count_active($scope = null, $query_params = array())
143
+	{
144
+		return $this->count($this->_get_query_params_for_all_active($scope, $query_params));
145
+	}
146
+
147
+	/**
148
+	 * Creates the $query_params that can be passed into any EEM_Payment_Method as their $query_params
149
+	 * argument to get all active for a given scope
150
+	 * @param string $scope one of the constants EEM_Payment_Method::scope_*
151
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
152
+	 * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
153
+	 * @throws EE_Error
154
+	 */
155
+	protected function _get_query_params_for_all_active($scope = null, $query_params = array())
156
+	{
157
+		if ($scope) {
158
+			if ($this->is_valid_scope($scope)) {
159
+				return array_replace_recursive(array( array( 'PMD_scope' => array( 'LIKE', "%$scope%" ) ) ), $query_params);
160
+			} else {
161
+				throw new EE_Error(sprintf(__("'%s' is not a valid scope for a payment method", "event_espresso"), $scope));
162
+			}
163
+		} else {
164
+			$acceptable_scopes = array();
165
+			$count = 0;
166
+			foreach ($this->scopes() as $scope_name => $desc) {
167
+				$count++;
168
+				$acceptable_scopes[ 'PMD_scope*' . $count ] = array( 'LIKE', '%' . $scope_name . '%' );
169
+			}
170
+			return array_replace_recursive(array( array( 'OR*active_scope' => $acceptable_scopes ) ), $query_params);
171
+		}
172
+	}
173
+
174
+	/**
175
+	 * Creates the $query_params that can be passed into any EEM_Payment_Method as their $query_params
176
+	 * argument to get all active for a given scope
177
+	 * @param string $scope one of the constants EEM_Payment_Method::scope_*
178
+	 * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
179
+	 * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
180
+	 * @throws EE_Error
181
+	 */
182
+	public function get_query_params_for_all_active($scope = null, $query_params = array())
183
+	{
184
+		return $this->_get_query_params_for_all_active($scope, $query_params);
185
+	}
186
+
187
+
188
+	/**
189
+	 * Gets one active payment method. see @get_all_active for documentation
190
+	 * @param string $scope
191
+	 * @param array  $query_params
192
+	 * @return EE_Payment_Method
193
+	 */
194
+	public function get_one_active($scope = null, $query_params = array())
195
+	{
196
+		return $this->get_one($this->_get_query_params_for_all_active($scope, $query_params));
197
+	}
198
+
199
+
200
+
201
+	/**
202
+	 * Gets one payment method of that type, regardless of whether its active or not
203
+	 * @param string $type
204
+	 * @return EE_Payment_Method
205
+	 */
206
+	public function get_one_of_type($type)
207
+	{
208
+		return $this->get_one(array( array( 'PMD_type' => $type ) ));
209
+	}
210
+
211
+
212
+
213
+	/**
214
+	 * Overrides parent ot also check by the slug
215
+	 * @see EEM_Base::ensure_is_obj()
216
+	 * @param string|int|EE_Payment_Method $base_class_obj_or_id
217
+	 * @param boolean                      $ensure_is_in_db
218
+	 * @return EE_Payment_Method
219
+	 * @throws EE_Error
220
+	 */
221
+	public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = false)
222
+	{
223
+		// first: check if it's a slug
224
+		if (is_string($base_class_obj_or_id)) {
225
+			$obj = $this->get_one_by_slug($base_class_obj_or_id);
226
+			if ($obj) {
227
+				return $obj;
228
+			}
229
+		}
230
+		// ok so it wasn't a slug we were passed. try the usual then (ie, it's an object or an ID)
231
+		try {
232
+			return parent::ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db);
233
+		} catch (EE_Error $e) {
234
+			// handle it outside the catch
235
+		}
236
+		throw new EE_Error(sprintf(__("'%s' is neither a Payment Method ID, slug, nor object.", "event_espresso"), $base_class_obj_or_id));
237
+	}
238
+
239
+
240
+
241
+	/**
242
+	 * Gets the ID of this object, or if its a string finds the object's id
243
+	 * associated with that slug
244
+	 * @param mixed $base_obj_or_id_or_slug
245
+	 * @return int
246
+	 */
247
+	public function ensure_is_ID($base_obj_or_id_or_slug)
248
+	{
249
+		if (is_string($base_obj_or_id_or_slug)) {
250
+			// assume it's a slug
251
+			$base_obj_or_id_or_slug = $this->get_one_by_slug($base_obj_or_id_or_slug);
252
+		}
253
+		return parent::ensure_is_ID($base_obj_or_id_or_slug);
254
+	}
255
+
256
+
257
+
258
+	/**
259
+	 * Verifies the button urls on all the passed payment methods have a valid button url. If not, resets them to their default.
260
+	 * @param EE_Payment_Method[] $payment_methods. If NULL is provided defaults to all payment methods active in the cart
261
+	 */
262
+	public function verify_button_urls($payment_methods = null)
263
+	{
264
+		$payment_methods = is_array($payment_methods) ? $payment_methods : $this->get_all_active(EEM_Payment_Method::scope_cart);
265
+		foreach ($payment_methods as $payment_method) {
266
+			try {
267
+				// If there is really no button URL at all, or if the button URLs still point to decaf folder even
268
+				// though this is a caffeinated install, reset it to the default.
269
+				$current_button_url = $payment_method->button_url();
270
+				if (empty($current_button_url)
271
+				|| (
272
+						strpos($current_button_url, 'decaf') !== false
273
+						&& strpos($payment_method->type_obj()->default_button_url(), 'decaf') === false
274
+					)
275
+				) {
276
+					$payment_method->save(
277
+						[
278
+							'PMD_button_url' => $payment_method->type_obj()->default_button_url()
279
+						]
280
+					);
281
+				}
282
+			} catch (EE_Error $e) {
283
+				$payment_method->set_active(false);
284
+			}
285
+		}
286
+	}
287
+
288
+
289
+
290
+	/**
291
+	 * Overrides parent to not only turn wpdb results into EE_Payment_Method objects,
292
+	 * but also verifies the payment method type of each is a usable object. If not,
293
+	 * deactivate it, sets a notification, and deactivates it
294
+	 *
295
+	 * @param array $rows
296
+	 * @return EE_Payment_Method[]
297
+	 * @throws InvalidDataTypeException
298
+	 */
299
+	protected function _create_objects($rows = array())
300
+	{
301
+		EE_Registry::instance()->load_lib('Payment_Method_Manager');
302
+		$payment_methods = parent::_create_objects($rows);
303
+		/* @var $payment_methods EE_Payment_Method[] */
304
+		$usable_payment_methods = array();
305
+		foreach ($payment_methods as $key => $payment_method) {
306
+			if (EE_Payment_Method_Manager::instance()->payment_method_type_exists($payment_method->type())) {
307
+				$usable_payment_methods[ $key ] = $payment_method;
308
+				// some payment methods enqueue their scripts in EE_PMT_*::__construct
309
+				// which is kinda a no-no (just because it's being constructed doesn't mean we need to enqueue
310
+				// its scripts). but for backwards-compat we should continue to do that
311
+				$payment_method->type_obj();
312
+			} elseif ($payment_method->active()) {
313
+				// only deactivate and notify the admin if the payment is active somewhere
314
+				$payment_method->deactivate();
315
+				$payment_method->save();
316
+				do_action(
317
+					'AHEE__EEM_Payment_Method___create_objects_auto_deactivated_payment_method',
318
+					$payment_method
319
+				);
320
+				new PersistentAdminNotice(
321
+					'auto-deactivated-' . $payment_method->type(),
322
+					sprintf(
323
+						__(
324
+							'The payment method %1$s was automatically deactivated because it appears its associated Event Espresso Addon was recently deactivated.%2$sIt can be reactivated on the %3$sPlugins admin page%4$s, then you can reactivate the payment method.',
325
+							'event_espresso'
326
+						),
327
+						$payment_method->admin_name(),
328
+						'<br />',
329
+						'<a href="' . admin_url('plugins.php') . '">',
330
+						'</a>'
331
+					),
332
+					true
333
+				);
334
+			}
335
+		}
336
+		return $usable_payment_methods;
337
+	}
338
+
339
+
340
+
341
+	/**
342
+	 * Gets all the payment methods which can be used for transaction
343
+	 * (according to the relations between payment methods and events, and
344
+	 * the currencies used for the transaction and their relation to payment methods)
345
+	 * @param EE_Transaction $transaction
346
+	 * @param string    $scope @see EEM_Payment_Method::get_all_for_events
347
+	 * @return EE_Payment_Method[]
348
+	 */
349
+	public function get_all_for_transaction($transaction, $scope)
350
+	{
351
+		// give addons a chance to override what payment methods are chosen based on the transaction
352
+		return apply_filters(
353
+			'FHEE__EEM_Payment_Method__get_all_for_transaction__payment_methods',
354
+			$this->get_all_active($scope, array( 'group_by' => 'PMD_type' )),
355
+			$transaction,
356
+			$scope
357
+		);
358
+	}
359
+
360
+
361
+	/**
362
+	 * Returns the payment method used for the last payment made for a registration.
363
+	 *
364
+	 * Note: if an offline payment method was selected on the related transaction then this will have no payment methods returned.
365
+	 * It will ONLY return a payment method for a PAYMENT recorded against the registration.
366
+	 *
367
+	 * @param EE_Registration|int $registration_or_reg_id  Either the EE_Registration object or the id for the registration.
368
+	 * @return EE_Payment|null
369
+	 */
370
+	public function get_last_used_for_registration($registration_or_reg_id)
371
+	{
372
+		$registration_id = EEM_Registration::instance()->ensure_is_ID($registration_or_reg_id);
373
+
374
+		$query_params = array(
375
+			0 => array(
376
+				'Payment.Registration.REG_ID' => $registration_id,
377
+			),
378
+			'order_by' => array( 'Payment.PAY_ID' => 'DESC' )
379
+		);
380
+		return $this->get_one($query_params);
381
+	}
382 382
 }
Please login to merge, or discard this patch.