AdminPage.Menu.class.php
4 years ago
AdminPage.Submenu.class.php
4 years ago
AdminPage.Themes.class.php
4 years ago
AdminPage.class.php
4 years ago
AdminPageSection.class.php
4 years ago
AdminPageSetting.Address.class.php
4 years ago
AdminPageSetting.Checkbox.class.php
4 years ago
AdminPageSetting.ColorPicker.class.php
4 years ago
AdminPageSetting.Count.class.php
4 years ago
AdminPageSetting.Editor.class.php
4 years ago
AdminPageSetting.FileUpload.class.php
4 years ago
AdminPageSetting.HTML.class.php
4 years ago
AdminPageSetting.Image.class.php
4 years ago
AdminPageSetting.InfiniteTable.class.php
4 years ago
AdminPageSetting.McApiKey.class.php
4 years ago
AdminPageSetting.McListMerge.class.php
4 years ago
AdminPageSetting.Number.class.php
4 years ago
AdminPageSetting.OpeningHours.class.php
4 years ago
AdminPageSetting.Ordering.class.php
4 years ago
AdminPageSetting.Radio.class.php
4 years ago
AdminPageSetting.Scheduler.class.php
4 years ago
AdminPageSetting.Select.class.php
4 years ago
AdminPageSetting.SelectMenu.class.php
4 years ago
AdminPageSetting.SelectPost.class.php
4 years ago
AdminPageSetting.SelectTaxonomy.class.php
4 years ago
AdminPageSetting.Text.class.php
4 years ago
AdminPageSetting.Textarea.class.php
4 years ago
AdminPageSetting.Time.class.php
4 years ago
AdminPageSetting.Toggle.class.php
4 years ago
AdminPageSetting.WarningTip.class.php
4 years ago
AdminPageSetting.class.php
4 years ago
Library.class.php
4 years ago
AdminPageSetting.McListMerge.class.php
306 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Add a setting to Simple Admin Pages to select a list and define |
| 5 | * merge fields for that list. |
| 6 | * |
| 7 | * This class is modelled on AdminPageSetting.class.php in the Simple |
| 8 | * Admin Pages library. It should work just like an extended class, but |
| 9 | * due to the way the library embeds the version into the class name, |
| 10 | * that could cause problems if the library is updated in the parent |
| 11 | * plugin. |
| 12 | * |
| 13 | * See: https://github.com/NateWr/simple-admin-pages |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | class mcfrtbAdminPageSettingMcListMerge_2_6_1 { |
| 18 | |
| 19 | /** |
| 20 | * Scripts and styles to load for this component |
| 21 | * (not used but required as part of the library) |
| 22 | */ |
| 23 | public $scripts = array(); |
| 24 | public $styles = array(); |
| 25 | |
| 26 | /** |
| 27 | * List of fields available for merging |
| 28 | */ |
| 29 | public $fields = array(); |
| 30 | |
| 31 | /** |
| 32 | * Initialize the setting |
| 33 | */ |
| 34 | public function __construct( $args ) { |
| 35 | |
| 36 | // Parse the values passed |
| 37 | $this->parse_args( $args ); |
| 38 | |
| 39 | // Get any existing value |
| 40 | $this->set_value(); |
| 41 | |
| 42 | // Set an error if the object is missing necessary data |
| 43 | if ( $this->missing_data() ) { |
| 44 | $this->set_error(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Parse the arguments passed in the construction and assign them to |
| 50 | * internal variables. This function will be overwritten for most subclasses |
| 51 | */ |
| 52 | private function parse_args( $args ) { |
| 53 | foreach ( $args as $key => $val ) { |
| 54 | switch ( $key ) { |
| 55 | |
| 56 | case 'id' : |
| 57 | $this->{$key} = esc_attr( $val ); |
| 58 | |
| 59 | case 'title' : |
| 60 | $this->{$key} = esc_attr( $val ); |
| 61 | |
| 62 | case 'fields' : |
| 63 | $this->{$key} = is_array( $val ) ? $val : array(); |
| 64 | |
| 65 | default : |
| 66 | $this->{$key} = $val; |
| 67 | |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Check for missing data when setup. |
| 74 | */ |
| 75 | private function missing_data() { |
| 76 | |
| 77 | // Required fields |
| 78 | if ( empty( $this->id ) ) { |
| 79 | $this->set_error( |
| 80 | array( |
| 81 | 'type' => 'missing_data', |
| 82 | 'data' => 'id' |
| 83 | ) |
| 84 | ); |
| 85 | } |
| 86 | if ( empty( $this->title ) ) { |
| 87 | $this->set_error( |
| 88 | array( |
| 89 | 'type' => 'missing_data', |
| 90 | 'data' => 'title' |
| 91 | ) |
| 92 | ); |
| 93 | } |
| 94 | if ( empty( $this->fields ) ) { |
| 95 | $this->set_error( |
| 96 | array( |
| 97 | 'type' => 'missing_data', |
| 98 | 'data' => 'fields' |
| 99 | ) |
| 100 | ); |
| 101 | } |
| 102 | if ( empty( $this->string_loading ) ) { |
| 103 | $this->set_error( |
| 104 | array( |
| 105 | 'type' => 'missing_data', |
| 106 | 'data' => 'string_loading' |
| 107 | ) |
| 108 | ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Set a value |
| 114 | */ |
| 115 | public function set_value( $val = null ) { |
| 116 | |
| 117 | if ( $val === null ) { |
| 118 | $option_group_value = get_option( $this->page ); |
| 119 | $val = isset( $option_group_value[ $this->id ] ) ? $option_group_value[ $this->id ] : ''; |
| 120 | } |
| 121 | |
| 122 | $this->value = $this->esc_value( $val ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Escape the value to display it in text fields and other input fields |
| 127 | */ |
| 128 | public function esc_value( $val ) { |
| 129 | |
| 130 | $value = array( |
| 131 | 'list' => '', |
| 132 | 'fields' => array(), |
| 133 | ); |
| 134 | |
| 135 | if ( empty( $val ) || empty( $val['list'] ) ) { |
| 136 | return $value; |
| 137 | } |
| 138 | |
| 139 | $value['list'] = esc_attr( $val['list'] ); |
| 140 | |
| 141 | // Escape the id/title of each merge field |
| 142 | foreach( $val['fields'] as $id => $val ) { |
| 143 | $value['fields'][$id] = esc_html( $val ); |
| 144 | } |
| 145 | |
| 146 | return $value; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Display this setting |
| 151 | */ |
| 152 | public function display_setting() { |
| 153 | |
| 154 | ?> |
| 155 | |
| 156 | <fieldset <?php $this->print_conditional_data(); ?>> |
| 157 | |
| 158 | <span class="mcf-list-select"></span> |
| 159 | |
| 160 | <span class="mcf-sap_loading"> |
| 161 | <span class="spinner"></span> |
| 162 | <span><?php echo $this->string_loading; ?></span> |
| 163 | </span> |
| 164 | |
| 165 | <?php $this->display_description(); ?> |
| 166 | |
| 167 | <div id="mcfrtb-merge-controls" data-input-name="<?php echo $this->get_input_name(); ?>"></div> |
| 168 | |
| 169 | </fieldset> |
| 170 | |
| 171 | <?php |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Display a description for this setting |
| 176 | */ |
| 177 | public function display_description() { |
| 178 | |
| 179 | if ( !empty( $this->description ) ) : ?> |
| 180 | |
| 181 | <p class="description"><?php echo $this->description; ?></p> |
| 182 | |
| 183 | <?php endif; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Determines whether this setting should be displayed, based on its |
| 188 | * conditional conditions, if any. |
| 189 | * |
| 190 | * @since 2.6 |
| 191 | */ |
| 192 | public function set_conditional_display() { |
| 193 | |
| 194 | if ( empty( $this->conditional_on ) ) { return; } |
| 195 | |
| 196 | $option_group_value = get_option( $this->page ); |
| 197 | |
| 198 | $option_group_value[ $this->conditional_on ] = isset( $option_group_value[ $this->conditional_on ] ) ? $option_group_value[ $this->conditional_on ] : false; |
| 199 | |
| 200 | if ( is_array( $option_group_value[ $this->conditional_on ] ) ) { |
| 201 | |
| 202 | $this->conditional_display = in_array( $this->conditional_on_value, $option_group_value[ $this->conditional_on ] ); |
| 203 | } |
| 204 | |
| 205 | $this->conditional_display = $this->conditional_on_value == $option_group_value[ $this->conditional_on ] ? true : false; |
| 206 | |
| 207 | if ( $this->conditional_display ) { return; } |
| 208 | |
| 209 | if ( ! empty( $this->args['class'] ) ) { |
| 210 | |
| 211 | $this->args['class'] .= ' sap-hidden'; |
| 212 | } |
| 213 | else { |
| 214 | |
| 215 | $this->args['class'] = 'sap-hidden'; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Prints conditional data tags within the input element if necessary |
| 221 | * |
| 222 | * @since 2.6 |
| 223 | */ |
| 224 | public function print_conditional_data() { |
| 225 | |
| 226 | if ( empty( $this->conditional_on ) ) { return; } |
| 227 | |
| 228 | echo 'data-conditional_on="' . esc_attr( $this->conditional_on ) . '"'; |
| 229 | echo 'data-conditional_on_value="' . esc_attr( $this->conditional_on_value ) . '"'; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Generate an option input field name, using the grouped schema. |
| 234 | */ |
| 235 | public function get_input_name() { |
| 236 | return esc_attr( $this->page ) . '[' . esc_attr( $this->id ) . ']'; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | /** |
| 241 | * Sanitize the array of text inputs for this setting |
| 242 | */ |
| 243 | public function sanitize_callback_wrapper( $values ) { |
| 244 | |
| 245 | $output = array( |
| 246 | 'list' => '', |
| 247 | 'fields' => array(), |
| 248 | ); |
| 249 | |
| 250 | // Return an empty value if we're missing anything important |
| 251 | if ( !is_array( $values ) || empty( $values ) || empty( $values['list'] ) ) { |
| 252 | return $output; |
| 253 | } |
| 254 | |
| 255 | // Sanitize the list |
| 256 | $output['list'] = sanitize_text_field( $values['list'] ); |
| 257 | |
| 258 | // Sanitize each merge field |
| 259 | $val_log = array(); |
| 260 | foreach( $values['fields'] as $id => $val ) { |
| 261 | |
| 262 | // Make sure that a merge field isn't assigned to multiple data |
| 263 | if ( !in_array( $val, $val_log ) ) { |
| 264 | $output['fields'][$id] = sanitize_text_field( $val ); |
| 265 | } |
| 266 | |
| 267 | $val_log[] = $val; |
| 268 | } |
| 269 | |
| 270 | return $output; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Add and register this setting |
| 275 | * |
| 276 | * @since 1.0 |
| 277 | */ |
| 278 | public function add_settings_field( $section_id ) { |
| 279 | |
| 280 | add_settings_field( |
| 281 | $this->id, |
| 282 | $this->title, |
| 283 | array( $this, 'display_setting' ), |
| 284 | $this->tab, |
| 285 | $section_id |
| 286 | ); |
| 287 | |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Set an error |
| 292 | * @since 1.0 |
| 293 | */ |
| 294 | public function set_error( $error ) { |
| 295 | $this->errors[] = array_merge( |
| 296 | $error, |
| 297 | array( |
| 298 | 'class' => get_class( $this ), |
| 299 | 'id' => $this->id, |
| 300 | 'backtrace' => debug_backtrace() |
| 301 | ) |
| 302 | ); |
| 303 | } |
| 304 | |
| 305 | } |
| 306 |