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.McApiKey.class.php
274 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Add a setting to Simple Admin Pages to register and verify a |
| 5 | * MailChimp API key |
| 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 mcfrtbAdminPageSettingMcApiKey_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 | * Initialize the setting |
| 28 | */ |
| 29 | public function __construct( $args ) { |
| 30 | |
| 31 | // Parse the values passed |
| 32 | $this->parse_args( $args ); |
| 33 | |
| 34 | // Get any existing value |
| 35 | $this->set_value(); |
| 36 | |
| 37 | // Set an error if the object is missing necessary data |
| 38 | if ( $this->missing_data() ) { |
| 39 | $this->set_error(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Parse the arguments passed in the construction and assign them to |
| 45 | * internal variables. This function will be overwritten for most subclasses |
| 46 | */ |
| 47 | private function parse_args( $args ) { |
| 48 | foreach ( $args as $key => $val ) { |
| 49 | switch ( $key ) { |
| 50 | |
| 51 | case 'id' : |
| 52 | $this->{$key} = esc_attr( $val ); |
| 53 | |
| 54 | case 'title' : |
| 55 | $this->{$key} = esc_attr( $val ); |
| 56 | |
| 57 | default : |
| 58 | $this->{$key} = $val; |
| 59 | |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Check for missing data when setup. |
| 66 | */ |
| 67 | private function missing_data() { |
| 68 | |
| 69 | // Required fields |
| 70 | if ( empty( $this->id ) ) { |
| 71 | $this->set_error( |
| 72 | array( |
| 73 | 'type' => 'missing_data', |
| 74 | 'data' => 'id' |
| 75 | ) |
| 76 | ); |
| 77 | } |
| 78 | if ( empty( $this->title ) ) { |
| 79 | $this->set_error( |
| 80 | array( |
| 81 | 'type' => 'missing_data', |
| 82 | 'data' => 'title' |
| 83 | ) |
| 84 | ); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Set a value |
| 90 | */ |
| 91 | public function set_value( $val = null ) { |
| 92 | |
| 93 | if ( $val === null ) { |
| 94 | $option_group_value = get_option( $this->page ); |
| 95 | $val = isset( $option_group_value[ $this->id ] ) ? $option_group_value[ $this->id ] : ''; |
| 96 | } |
| 97 | |
| 98 | $this->value = $this->esc_value( $val ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Escape the value to display it in text fields and other input fields |
| 103 | */ |
| 104 | public function esc_value( $val ) { |
| 105 | |
| 106 | $value = array( |
| 107 | 'api_key' => '', |
| 108 | 'status' => false, |
| 109 | ); |
| 110 | |
| 111 | if ( empty( $val ) || empty( $val['api_key'] ) ) { |
| 112 | return $value; |
| 113 | } |
| 114 | |
| 115 | $value['api_key'] = esc_attr( $val['api_key'] ); |
| 116 | $value['status'] = (bool) $val['status']; |
| 117 | |
| 118 | return $value; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Display this setting |
| 123 | */ |
| 124 | public function display_setting() { |
| 125 | ?> |
| 126 | |
| 127 | <fieldset <?php $this->print_conditional_data(); ?>> |
| 128 | |
| 129 | <input name="<?php echo $this->get_input_name(); ?>[api_key]" type="text" id="<?php echo $this->get_input_name(); ?>[api_key]" value="<?php echo $this->value['api_key']; ?>"<?php echo !empty( $this->placeholder ) ? ' placeholder="' . esc_attr( $this->placeholder ) . '"' : ''; ?> class="regular-text"> |
| 130 | |
| 131 | <?php if ( !empty( $this->value['api_key'] ) && $this->value['status'] === true ) : ?> |
| 132 | <span class="mcfrtb-status mcfrtb-status-connected"><?php echo $this->string_status_connected; ?></span> |
| 133 | <?php elseif( !empty( $this->value['api_key'] ) ) : ?> |
| 134 | <span class="mcfrtb-status mcfrtb-status-error"><?php echo $this->string_status_error; ?></span> |
| 135 | <?php endif; ?> |
| 136 | |
| 137 | <input name="<?php echo $this->get_input_name(); ?>[status]" type="hidden" id="<?php echo $this->get_input_name(); ?>[status]" value="<?php echo $this->value['status']; ?>"<?php echo !empty( $this->placeholder ) ? ' placeholder="' . esc_attr( $this->placeholder ) . '"' : ''; ?>> |
| 138 | |
| 139 | </fieldset> |
| 140 | |
| 141 | <?php |
| 142 | |
| 143 | $this->display_description(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Display a description for this setting |
| 148 | */ |
| 149 | public function display_description() { |
| 150 | |
| 151 | if ( !empty( $this->description ) ) : ?> |
| 152 | |
| 153 | <p class="description"><?php echo $this->description; ?></p> |
| 154 | |
| 155 | <?php endif; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Generate an option input field name, using the grouped schema. |
| 160 | */ |
| 161 | public function get_input_name() { |
| 162 | return esc_attr( $this->page ) . '[' . esc_attr( $this->id ) . ']'; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Determines whether this setting should be displayed, based on its |
| 167 | * conditional conditions, if any. |
| 168 | * |
| 169 | * @since 2.6 |
| 170 | */ |
| 171 | public function set_conditional_display() { |
| 172 | |
| 173 | if ( empty( $this->conditional_on ) ) { return; } |
| 174 | |
| 175 | $option_group_value = get_option( $this->page ); |
| 176 | |
| 177 | $option_group_value[ $this->conditional_on ] = isset( $option_group_value[ $this->conditional_on ] ) ? $option_group_value[ $this->conditional_on ] : false; |
| 178 | |
| 179 | if ( is_array( $option_group_value[ $this->conditional_on ] ) ) { |
| 180 | |
| 181 | $this->conditional_display = in_array( $this->conditional_on_value, $option_group_value[ $this->conditional_on ] ); |
| 182 | } |
| 183 | |
| 184 | $this->conditional_display = $this->conditional_on_value == $option_group_value[ $this->conditional_on ] ? true : false; |
| 185 | |
| 186 | if ( $this->conditional_display ) { return; } |
| 187 | |
| 188 | if ( ! empty( $this->args['class'] ) ) { |
| 189 | |
| 190 | $this->args['class'] .= ' sap-hidden'; |
| 191 | } |
| 192 | else { |
| 193 | |
| 194 | $this->args['class'] = 'sap-hidden'; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Prints conditional data tags within the input element if necessary |
| 200 | * |
| 201 | * @since 2.6 |
| 202 | */ |
| 203 | public function print_conditional_data() { |
| 204 | |
| 205 | if ( empty( $this->conditional_on ) ) { return; } |
| 206 | |
| 207 | echo 'data-conditional_on="' . esc_attr( $this->conditional_on ) . '"'; |
| 208 | echo 'data-conditional_on_value="' . esc_attr( $this->conditional_on_value ) . '"'; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | /** |
| 213 | * Sanitize the array of text inputs for this setting |
| 214 | */ |
| 215 | public function sanitize_callback_wrapper( $values ) { |
| 216 | // global $ulb_controller; |
| 217 | |
| 218 | $output = array( |
| 219 | 'api_key' => '', |
| 220 | 'status' => false, |
| 221 | ); |
| 222 | |
| 223 | // Return an empty key and status if the values don't look right |
| 224 | if ( !is_array( $values ) || empty( $values ) || empty( $values['api_key'] ) ) { |
| 225 | return $output; |
| 226 | } |
| 227 | |
| 228 | // Sanitize the API key |
| 229 | $output['api_key'] = sanitize_text_field( $values['api_key'] ); |
| 230 | |
| 231 | // $ulb_controller->mailchimp->load_api( $output['api_key'] ); |
| 232 | |
| 233 | // Check for a valid API key |
| 234 | // $output['status'] = $ulb_controller->mailchimp->is_valid_api_key(); |
| 235 | |
| 236 | $output['status'] = strpos( $output['api_key'], '-' ) === false ? false : true; |
| 237 | |
| 238 | return $output; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Add and register this setting |
| 243 | * |
| 244 | * @since 1.0 |
| 245 | */ |
| 246 | public function add_settings_field( $section_id ) { |
| 247 | |
| 248 | add_settings_field( |
| 249 | $this->id, |
| 250 | $this->title, |
| 251 | array( $this, 'display_setting' ), |
| 252 | $this->tab, |
| 253 | $section_id |
| 254 | ); |
| 255 | |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Set an error |
| 260 | * @since 1.0 |
| 261 | */ |
| 262 | public function set_error( $error ) { |
| 263 | $this->errors[] = array_merge( |
| 264 | $error, |
| 265 | array( |
| 266 | 'class' => get_class( $this ), |
| 267 | 'id' => $this->id, |
| 268 | 'backtrace' => debug_backtrace() |
| 269 | ) |
| 270 | ); |
| 271 | } |
| 272 | |
| 273 | } |
| 274 |