PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.11.3
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.11.3
1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 All 211 releases
charitable / includes / fields / class-charitable-field-sorter.php

class-charitable-field-sorter.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.11.3, at includes/fields/class-charitable-field-sorter.php

153 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A class that is responsible for taking a set of fields and sorting them by priority.
4 *
5 * @package Charitable/Classes/Charitable_Field_Sorter
6 * @author David Bisset
7 * @copyright Copyright (c) 2023, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.6.0
10 * @version 1.6.27
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Field_Sorter' ) ) :
19
20 /**
21 * Charitable_Field_Sorter
22 *
23 * @since 1.6.0
24 */
25 class Charitable_Field_Sorter {
26
27 /**
28 * The fields to be sorted.
29 *
30 * @since 1.6.0
31 *
32 * @var array
33 */
34 private $fields;
35
36 /**
37 * Registry.
38 *
39 * @since 1.6.0
40 *
41 * @var Charitable_Field_Registry
42 */
43 private $registry;
44
45 /**
46 * The field property we are sorting by.
47 *
48 * @since 1.6.0
49 *
50 * @var string
51 */
52 private $property;
53
54 /**
55 * Create class object.
56 *
57 * @since 1.6.0
58 *
59 * @param array $fields The fields to be sorted.
60 * @param Charitable_Field_Registry $registry The registry that the fields are included in.
61 * @param string $property The field property we are sorting by.
62 */
63 public function __construct( $fields, Charitable_Field_Registry $registry, $property ) {
64 $this->property = $property;
65 $this->registry = $registry;
66 $this->fields = $fields;
67
68 array_walk( $fields, array( $this, 'set_field_priority' ) );
69 }
70
71 /**
72 * Set a field's priority.
73 *
74 * @since 1.6.0
75 *
76 * @param Charitable_Field $field The field to set the priority for.
77 * @return void
78 */
79 public function set_field_priority( $field ) {
80 $settings = $field->{$this->property};
81
82 /* The field was defined with a priority, so we will use that as default. */
83 $default = array_key_exists( 'priority', $settings ) ? $settings['priority'] : false;
84 $priority = $this->calculate_field_priority( $settings, $default );
85
86 $field->set( $this->property, 'priority', $priority );
87 }
88
89 /**
90 * Determine a field's priority based on its `show_after` and `show_before` settings.
91 *
92 * @since 1.6.0
93 * @since 1.6.27 Added $default parameter for priority.
94 *
95 * @param array $settings The field settings.
96 * @param int|false $default The default priority.
97 * @return int
98 */
99 private function calculate_field_priority( $settings, $default ) {
100 $after = $this->get_referenced_field( 'show_after', $settings );
101 $before = $this->get_referenced_field( 'show_before', $settings );
102
103 /* If the field was set to show after a certain field and before another field. */
104 if ( $after && $before ) {
105 return ( $after->{$this->property}['priority'] + $before->{$this->property}['priority'] ) / 2;
106 }
107
108 $property = $this->property; // Store the property name in a variable.
109
110 if ( $after && isset( $after->$property['priority'] ) && ! empty( $after->$property['priority'] ) ) {
111 return $after->{$this->property}['priority'] + 0.5;
112 }
113
114 if ( $before && isset( $before->$property['priority'] ) && ! empty( $before->$property['priority'] ) ) {
115 return $before->{$this->property}['priority'] - 0.5;
116 }
117
118 /* Neither show_after or show_before was set, so return the default if one was set. */
119 if ( $default ) {
120 return $default;
121 }
122
123 /* Otherwise, put it 2 after the most recently registered field. */
124 foreach ( array_reverse( $this->fields ) as $field ) {
125 if ( array_key_exists( 'priority', $field->{$this->property} ) ) {
126 return $field->{$this->property}['priority'] + 2;
127 }
128 }
129
130 /* Still no luck? Set priority to 2. */
131 return 2;
132 }
133
134 /**
135 * Return the field referenced by a `show_after` or `show_before` setting.
136 *
137 * @since 1.6.0
138 *
139 * @param string $setting The setting used for the reference.
140 * @param array $settings The field settings.
141 * @return array|false False if the field does not exist or the setting was not set.
142 */
143 private function get_referenced_field( $setting, $settings ) {
144 if ( ! array_key_exists( $setting, $settings ) ) {
145 return false;
146 }
147
148 return array_key_exists( $settings[ $setting ], $this->fields ) ? $this->fields[ $settings[ $setting ] ] : false;
149 }
150 }
151
152 endif;
153