CouponsPage
1 year ago
CustomersPage
5 months ago
DashboardPage
1 year ago
DownloadLogsPage
1 year ago
ExportPage
5 months ago
GroupsPage
1 year ago
OrdersPage
1 year ago
PlansPage
2 months ago
SubscriptionsPage
3 months ago
TaxSettings
3 years ago
views
1 month ago
CheckListHeader.php
3 years ago
CheckoutFieldsManager.php
1 year ago
ContextualStateChangeHelper.php
3 years ago
FileDownloads.php
3 years ago
PaymentMethods.php
1 year ago
PaymentSettings.php
1 month ago
PlanIntegrationsMetabox.php
10 months ago
SettingsFieldsParser.php
1 year ago
index.php
3 years ago
SettingsFieldsParser.php
151 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages\Membership; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Models\Coupon\CouponUnit; |
| 6 | |
| 7 | class SettingsFieldsParser |
| 8 | { |
| 9 | public $config; |
| 10 | public $dbData; |
| 11 | public $field_class; |
| 12 | |
| 13 | public function __construct($config, $dbData = [], $field_class = 'ppress-plan-control') |
| 14 | { |
| 15 | $this->config = $config; |
| 16 | $this->dbData = $dbData; |
| 17 | $this->field_class = $field_class; |
| 18 | } |
| 19 | |
| 20 | protected function field_output($config) |
| 21 | { |
| 22 | $field_id = sanitize_text_field($config['id']); |
| 23 | $placeholder = esc_attr(ppress_var($config, 'placeholder', '')); |
| 24 | |
| 25 | $field_data = $this->get_field_data($field_id, $config); |
| 26 | |
| 27 | switch ($config['type']) { |
| 28 | case 'text': |
| 29 | printf('<input placeholder="%4$s" class="%3$s" name="%1$s" id="%1$s" type="text" value="%2$s">', esc_attr($field_id), esc_attr($field_data), esc_attr($this->field_class), $placeholder); |
| 30 | break; |
| 31 | case 'number': |
| 32 | printf('<input placeholder="%4$s" class="%3$s" name="%1$s" id="%1$s" type="number" value="%2$s">', esc_attr($field_id), esc_attr($field_data), esc_attr($this->field_class), $placeholder); |
| 33 | break; |
| 34 | case 'price': |
| 35 | printf('<input class="%3$s" step="any" placeholder="0.00" name="%1$s" id="%1$s" type="number" value="%2$s">', esc_attr($field_id), esc_attr($field_data), esc_attr($this->field_class)); |
| 36 | break; |
| 37 | case 'textarea': |
| 38 | printf('<textarea class="%3$s" name="%1$s" id="%1$s">%2$s</textarea>', esc_attr($field_id), esc_attr($field_data), esc_attr($this->field_class)); |
| 39 | break; |
| 40 | case 'discount': |
| 41 | printf('<span class="ppress-amount-type-wrapper"> |
| 42 | <input type="text" required="required" class="%1$s" id="amount" name="amount" value="%2$s" placeholder="0"> |
| 43 | <label for="ppress-discount-type" class="screen-reader-text">%3$s</label> |
| 44 | <select name="unit" id="ppress-discount-type"> |
| 45 | <option value="percent" %4$s>%</option> |
| 46 | <option value="flat" %5$s>%6$s</option> |
| 47 | </select> |
| 48 | </span>', |
| 49 | esc_attr($this->field_class), |
| 50 | $this->get_field_data('amount', $config), |
| 51 | esc_attr($field_id), |
| 52 | selected($this->get_field_data('unit', $config), CouponUnit::PERCENTAGE, false), |
| 53 | selected($this->get_field_data('unit', $config), CouponUnit::FLAT, false), |
| 54 | ppress_get_currency_symbol() |
| 55 | ); |
| 56 | break; |
| 57 | case 'wp_editor': |
| 58 | // Remove all TinyMCE plugins. |
| 59 | remove_all_filters('mce_buttons', 10); |
| 60 | remove_all_filters('mce_external_plugins', 10); |
| 61 | remove_all_actions('media_buttons'); |
| 62 | // add core media button back. |
| 63 | add_action('media_buttons', 'media_buttons'); |
| 64 | |
| 65 | wp_editor(wp_kses_post($field_data), sanitize_text_field($field_id), [ |
| 66 | "editor_height" => 100, |
| 67 | 'editor_class' => 'ppress-plan-control' |
| 68 | ]); |
| 69 | break; |
| 70 | case 'select': |
| 71 | if (is_array($config['options']) && ! empty($config['options'])) { |
| 72 | $is_multiple = ppress_var($config, 'multiple') === true; |
| 73 | $name = $is_multiple ? $config['id'] . '[]' : $config['id']; |
| 74 | printf('<select class="%2$s" name="%1$s" id="%3$s"%4$s>', esc_attr($name), esc_attr($this->field_class), $config['id'], $is_multiple ? ' multiple' : ''); |
| 75 | foreach ($config['options'] as $option_id => $option_name) { |
| 76 | if (is_array($field_data) || ppress_var($config, 'multiple') === true) { |
| 77 | $field_data = is_array($field_data) ? $field_data : []; |
| 78 | $selected = in_array($option_id, $field_data) ? 'selected' : ''; |
| 79 | } else { |
| 80 | $selected = selected($option_id, $field_data, false); |
| 81 | } |
| 82 | printf('<option value="%1$s" %3$s>%2$s</option>', $option_id, $option_name, $selected); |
| 83 | } |
| 84 | echo '</select>'; |
| 85 | } |
| 86 | break; |
| 87 | case 'select2': |
| 88 | if (is_array($config['options']) && ! empty($config['options'])) { |
| 89 | $is_multiple = ppress_var($config, 'multiple') === true; |
| 90 | $name = $is_multiple ? $config['id'] . '[]' : $config['id']; |
| 91 | printf('<select class="ppselect2 %2$s" name="%1$s[]" id="%3$s" multiple>', esc_attr($name), esc_attr($this->field_class), $config['id']); |
| 92 | foreach ($config['options'] as $option_id => $option_name) { |
| 93 | if (is_array($field_data) || ppress_var($config, 'multiple') === true) { |
| 94 | $selected = in_array($option_id, $field_data) ? 'selected' : ''; |
| 95 | } else { |
| 96 | $selected = selected($option_id, $field_data, false); |
| 97 | } |
| 98 | printf('<option value="%1$s" %3$s>%2$s</option>', $option_id, $option_name, $selected); |
| 99 | } |
| 100 | echo '</select>'; |
| 101 | } |
| 102 | break; |
| 103 | case 'radio': |
| 104 | if (is_array($config['options']) && ! empty($config['options'])) { |
| 105 | foreach ($config['options'] as $option_id => $option_name) { |
| 106 | printf('<label><input type="radio" name="%4$s" value="%1$s" %3$s>%2$s</label>', $option_id, $option_name, checked($option_id, $field_data, false), $field_id); |
| 107 | } |
| 108 | } |
| 109 | break; |
| 110 | case 'checkbox': |
| 111 | $checkbox_label = esc_html(ppress_var($config, 'checkbox_label', '', true)); |
| 112 | printf('<input type="hidden" name="%1$s" value="false">', $field_id); |
| 113 | printf('<label><input type="checkbox" name="%1$s" value="true"%2$s>%3$s</label>', $field_id, checked('true', $field_data, false), $checkbox_label); |
| 114 | break; |
| 115 | default: |
| 116 | do_action('ppress_admin_settings_fields_parser_field', $field_data, $config, $this); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public function get_field_data($field_id, $config) |
| 121 | { |
| 122 | return apply_filters( |
| 123 | 'ppress_admin_settings_fields_parser_field_value', |
| 124 | ppressPOST_var($field_id, $this->dbData->$field_id ?? ''), |
| 125 | $field_id, $this->dbData, $config, $this |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | public function build() |
| 130 | { |
| 131 | ?> |
| 132 | <table class="form-table"> |
| 133 | <tbody> |
| 134 | <?php foreach ($this->config as $config) : ?> |
| 135 | <tr class="form-field" id="field-role-<?= esc_attr($config['id']) ?>"> |
| 136 | <th scope="row" valign="top"> |
| 137 | <label for="<?= esc_attr($config['id']) ?>"><?= esc_html($config['label']) ?></label> |
| 138 | </th> |
| 139 | <td> |
| 140 | <?php $this->field_output($config); ?> |
| 141 | <?php if ( ! empty($config['description'])) : ?> |
| 142 | <p class="description"><?php echo wp_kses_post($config['description']); ?></p> |
| 143 | <?php endif; ?> |
| 144 | </td> |
| 145 | </tr> |
| 146 | <?php endforeach; ?> |
| 147 | </tbody> |
| 148 | </table> |
| 149 | <?php |
| 150 | } |
| 151 | } |