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
AdminPageSection.class.php
189 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Register, display and save a section on a custom admin menu |
| 5 | * |
| 6 | * @since 1.0 |
| 7 | * @package Simple Admin Pages |
| 8 | */ |
| 9 | |
| 10 | class sapAdminPageSection_2_6_1 { |
| 11 | |
| 12 | // Page defaults |
| 13 | public $id; // unique id for this section |
| 14 | public $title; // optional title to display above this section |
| 15 | public $description; // optional description of the section |
| 16 | public $settings = array(); // Array of settings to display in this option set |
| 17 | public $disabled = false; // whether a setting should be disabled |
| 18 | |
| 19 | // Array to store errors |
| 20 | public $errors = array(); |
| 21 | |
| 22 | /** |
| 23 | * Initialize the section |
| 24 | * @since 1.0 |
| 25 | */ |
| 26 | public function __construct( $args ) { |
| 27 | |
| 28 | // Parse the values passed |
| 29 | $this->parse_args( $args ); |
| 30 | |
| 31 | // Set an error if there is no id for this section |
| 32 | if ( !isset( $this->id ) ) { |
| 33 | $this->set_error( |
| 34 | array( |
| 35 | 'type' => 'missing_data', |
| 36 | 'data' => 'id' |
| 37 | ) |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Parse the arguments passed in the construction and assign them to |
| 45 | * internal variables. |
| 46 | * @since 1.0 |
| 47 | */ |
| 48 | private function parse_args( $args ) { |
| 49 | foreach ( $args as $key => $val ) { |
| 50 | switch ( $key ) { |
| 51 | |
| 52 | case 'id' : |
| 53 | $this->{$key} = esc_attr( $val ); |
| 54 | |
| 55 | default : |
| 56 | $this->{$key} = $val; |
| 57 | |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Add a setting to this section |
| 64 | * @since 1.0 |
| 65 | */ |
| 66 | public function add_setting( $setting ) { |
| 67 | |
| 68 | if ( !$setting ) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | if ( $this->disabled ) { |
| 73 | $setting->disabled = true; |
| 74 | } |
| 75 | |
| 76 | if ( method_exists( $setting, 'has_position' ) && $setting->has_position() ) { |
| 77 | |
| 78 | // Top |
| 79 | if ( $setting->position[0] == 'top' ) { |
| 80 | $this->settings = array_merge( array( $setting->id => $setting ), $this->settings ); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | // Position setting relative to another setting |
| 85 | if ( !empty( $setting->position[1] ) ) { |
| 86 | |
| 87 | $new_settings = array(); |
| 88 | foreach( $this->settings as $id => $current ) { |
| 89 | |
| 90 | // Above |
| 91 | if ( $setting->position[1] == $id && $setting->position[0] == 'above' ) { |
| 92 | $new_settings[ $setting->id ] = $setting; |
| 93 | } |
| 94 | |
| 95 | $new_settings[ $id ] = $current; |
| 96 | |
| 97 | // Below |
| 98 | if ( $setting->position[1] == $id && $setting->position[0] == 'below' ) { |
| 99 | $new_settings[ $setting->id ] = $setting; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | $this->settings = $new_settings; |
| 104 | |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Fallback to appending it at the end |
| 110 | $this->settings[ $setting->id ] = $setting; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Display the description for this section |
| 115 | * @since 1.0 |
| 116 | */ |
| 117 | public function display_section() { |
| 118 | |
| 119 | if ( !count( $this->settings ) ) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | if ( !empty( $this->description ) ) : |
| 124 | ?> |
| 125 | |
| 126 | <p class="description"><?php echo $this->description; ?></p> |
| 127 | |
| 128 | <?php |
| 129 | endif; |
| 130 | |
| 131 | if ( $this->disabled and isset($this->disabled_image) ) { |
| 132 | |
| 133 | ?> |
| 134 | |
| 135 | <?php echo ( isset($this->purchase_link ) ? "<div class='sap-premium-options-table-overlay'>" : '' ); ?> |
| 136 | <div class="section-disabled"> |
| 137 | <img src="<?php echo plugins_url( '../img/options-asset-lock.png', __FILE__ ); ?>" alt="Upgrade to Premium"> |
| 138 | <p>Access this section by upgrading to premium</p> |
| 139 | <a href="<?php echo $this->purchase_link; ?>" class="sap-dashboard-get-premium-widget-button" target="_blank">UPGRADE NOW</a> |
| 140 | </div> |
| 141 | <?php echo ( isset( $this->purchase_link ) ? "</div>" : '' ); ?> |
| 142 | |
| 143 | <?php |
| 144 | |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Add the settings section to the page in WordPress |
| 150 | * @since 1.0 |
| 151 | */ |
| 152 | public function add_settings_section() { |
| 153 | add_settings_section( $this->id, $this->title, array( $this, 'display_section' ), $this->get_page_slug() ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Determine the page slug to use when calling add_settings_section. |
| 158 | * |
| 159 | * Tabs should use their own ID and settings that are attached to tabs |
| 160 | * should use that tab's ID. |
| 161 | * @since 2.0 |
| 162 | */ |
| 163 | public function get_page_slug() { |
| 164 | if ( isset( $this->is_tab ) && $this->is_tab === true ) { |
| 165 | return $this->id; |
| 166 | } elseif ( isset( $this->tab ) ) { |
| 167 | return $this->tab; |
| 168 | } else { |
| 169 | return $this->page; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Set an error |
| 175 | * @since 1.0 |
| 176 | */ |
| 177 | public function set_error( $error ) { |
| 178 | $this->errors[] = array_merge( |
| 179 | $error, |
| 180 | array( |
| 181 | 'class' => get_class( $this ), |
| 182 | 'id' => $this->id, |
| 183 | 'backtrace' => debug_backtrace() |
| 184 | ) |
| 185 | ); |
| 186 | } |
| 187 | |
| 188 | } |
| 189 |