Addons.php
8 years ago
Columns.php
8 years ago
Help.php
8 years ago
Settings.php
8 years ago
Upgrade.php
8 years ago
Welcome.php
8 years ago
Settings.php
241 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class AC_Admin_Page_Settings extends AC_Admin_Page { |
| 8 | |
| 9 | const SETTINGS_NAME = 'cpac_general_options'; |
| 10 | |
| 11 | const SETTINGS_GROUP = 'cpac-general-settings'; |
| 12 | |
| 13 | private $options; |
| 14 | |
| 15 | public function __construct() { |
| 16 | $this |
| 17 | ->set_slug( 'settings' ) |
| 18 | ->set_label( __( 'Settings', 'codepress-admin-columns' ) ); |
| 19 | |
| 20 | $this->options = get_option( self::SETTINGS_NAME ); |
| 21 | |
| 22 | register_setting( self::SETTINGS_GROUP, self::SETTINGS_NAME ); |
| 23 | |
| 24 | add_filter( 'option_page_capability_' . self::SETTINGS_GROUP, array( $this, 'set_capability' ) ); |
| 25 | add_action( 'admin_init', array( $this, 'handle_column_request' ) ); |
| 26 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) ); |
| 27 | } |
| 28 | |
| 29 | public function admin_scripts() { |
| 30 | if ( $this->is_current_screen() ) { |
| 31 | wp_enqueue_style( 'ac-admin-page-settings', AC()->get_plugin_url() . 'assets/css/admin-page-settings' . AC()->minified() . '.css', array(), AC()->get_version() ); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | public function set_capability() { |
| 36 | return 'manage_admin_columns'; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param string $key |
| 41 | */ |
| 42 | public function attr_name( $key ) { |
| 43 | echo esc_attr( self::SETTINGS_NAME . '[' . sanitize_key( $key ) . ']' ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param $key |
| 48 | * |
| 49 | * @return false|string When '0' there are no options stored. |
| 50 | */ |
| 51 | public function get_option( $key ) { |
| 52 | return isset( $this->options[ $key ] ) ? $this->options[ $key ] : false; |
| 53 | } |
| 54 | |
| 55 | private function is_empty_options() { |
| 56 | return false === $this->options; |
| 57 | } |
| 58 | |
| 59 | public function delete_options() { |
| 60 | delete_option( self::SETTINGS_NAME ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return bool |
| 65 | */ |
| 66 | public function show_edit_button() { |
| 67 | return $this->is_empty_options() || $this->get_option( 'show_edit_button' ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Deletes all stored column settings. Does not delete general settings. |
| 72 | */ |
| 73 | private function delete_all_column_settings() { |
| 74 | global $wpdb; |
| 75 | |
| 76 | $sql = " |
| 77 | DELETE |
| 78 | FROM $wpdb->options |
| 79 | WHERE option_name LIKE %s"; |
| 80 | |
| 81 | $wpdb->query( $wpdb->prepare( $sql, AC_ListScreen::OPTIONS_KEY . '%' ) ); |
| 82 | |
| 83 | // @since 3.0 |
| 84 | do_action( 'ac/restore_all_columns' ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @since 1.0 |
| 89 | */ |
| 90 | public function handle_column_request() { |
| 91 | if ( ! AC()->user_can_manage_admin_columns() || ! $this->is_current_screen() ) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | switch ( filter_input( INPUT_POST, 'ac_action' ) ) : |
| 96 | |
| 97 | case 'restore_all' : |
| 98 | if ( $this->verify_nonce( 'restore-all' ) ) { |
| 99 | $this->delete_all_column_settings(); |
| 100 | |
| 101 | AC()->notice( __( 'Default settings succesfully restored.', 'codepress-admin-columns' ), 'updated' ); |
| 102 | } |
| 103 | break; |
| 104 | |
| 105 | endswitch; |
| 106 | } |
| 107 | |
| 108 | public function single_checkbox( $args = array() ) { |
| 109 | $defaults = array( |
| 110 | 'name' => '', |
| 111 | 'label' => '', |
| 112 | 'instructions' => '', |
| 113 | 'default_value' => false, |
| 114 | ); |
| 115 | |
| 116 | $args = (object) wp_parse_args( $args, $defaults ); |
| 117 | |
| 118 | $current_value = $this->is_empty_options() ? $args->default_value : $this->get_option( $args->name ); |
| 119 | ?> |
| 120 | <p> |
| 121 | <label for="<?php echo $args->name; ?>"> |
| 122 | <input name="<?php $this->attr_name( $args->name ); ?>" id="<?php echo $args->name; ?>" type="checkbox" value="1" <?php checked( $current_value, '1' ); ?>> |
| 123 | <?php echo $args->label; ?> |
| 124 | </label> |
| 125 | <?php if ( $args->instructions ) : ?> |
| 126 | <a class="ac-pointer instructions" rel="pointer-<?php echo $args->name; ?>" data-pos="right"> |
| 127 | <?php _e( 'Instructions', 'codepress-admin-columns' ); ?> |
| 128 | </a> |
| 129 | <?php endif; ?> |
| 130 | </p> |
| 131 | <?php if ( $args->instructions ) : ?> |
| 132 | <div id="pointer-<?php echo $args->name; ?>" style="display:none;"> |
| 133 | <h3><?php _e( 'Notice', 'codepress-admin-columns' ); ?></h3> |
| 134 | <?php echo $args->instructions; ?> |
| 135 | </div> |
| 136 | <?php |
| 137 | endif; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @param bool $type |
| 142 | * |
| 143 | * @return string |
| 144 | */ |
| 145 | public function get_default_text( $type = 'on' ) { |
| 146 | $string = __( 'off', 'codepress-admin-columns' ); |
| 147 | |
| 148 | if ( 'on' === $type ) { |
| 149 | $string = __( 'on', 'codepress-admin-columns' ); |
| 150 | } |
| 151 | |
| 152 | return sprintf( __( "Default is %s.", 'codepress-admin-columns' ), '<code>' . $string . '</code>' ); |
| 153 | } |
| 154 | |
| 155 | public function display() { ?> |
| 156 | <table class="form-table ac-form-table settings"> |
| 157 | <tbody> |
| 158 | <tr class="general"> |
| 159 | <th scope="row"> |
| 160 | <h2><?php _e( 'General Settings', 'codepress-admin-columns' ); ?></h2> |
| 161 | <p><?php _e( 'Customize your Admin Columns settings.', 'codepress-admin-columns' ); ?></p> |
| 162 | </th> |
| 163 | <td> |
| 164 | <form method="post" action="options.php"> |
| 165 | |
| 166 | <?php settings_fields( self::SETTINGS_GROUP ); ?> |
| 167 | |
| 168 | <?php |
| 169 | $this->single_checkbox( array( |
| 170 | 'name' => 'show_edit_button', |
| 171 | 'label' => sprintf( __( "Show %s button on table screen.", 'codepress-admin-columns' ), '"' . __( 'Edit columns', 'codepress-admin-columns' ) . '"' ) . ' ' . $this->get_default_text( 'on' ), |
| 172 | 'default_value' => '1', |
| 173 | ) ); |
| 174 | ?> |
| 175 | |
| 176 | <?php do_action( 'ac/settings/general', $this ); ?> |
| 177 | |
| 178 | <p> |
| 179 | <input type="submit" class="button" value="<?php _e( 'Save' ); ?>"/> |
| 180 | </p> |
| 181 | </form> |
| 182 | </td> |
| 183 | </tr> |
| 184 | |
| 185 | <?php |
| 186 | |
| 187 | /** Allow plugins to add their own custom settings to the settings page. */ |
| 188 | if ( $groups = apply_filters( 'ac/settings/groups', array() ) ) { |
| 189 | |
| 190 | foreach ( $groups as $id => $group ) { |
| 191 | |
| 192 | $title = isset( $group['title'] ) ? $group['title'] : ''; |
| 193 | $description = isset( $group['description'] ) ? $group['description'] : ''; |
| 194 | |
| 195 | ?> |
| 196 | |
| 197 | <tr> |
| 198 | <th scope="row"> |
| 199 | <h2><?php echo esc_html( $title ); ?></h2> |
| 200 | |
| 201 | <p><?php echo $description; ?></p> |
| 202 | </th> |
| 203 | <td> |
| 204 | <?php |
| 205 | |
| 206 | /** Use this Hook to add additional fields to the group */ |
| 207 | do_action( "ac/settings/group/" . $id ); |
| 208 | |
| 209 | ?> |
| 210 | </td> |
| 211 | </tr> |
| 212 | |
| 213 | <?php |
| 214 | } |
| 215 | } |
| 216 | ?> |
| 217 | |
| 218 | <tr class="restore"> |
| 219 | <th scope="row"> |
| 220 | <h2><?php _e( 'Restore Settings', 'codepress-admin-columns' ); ?></h2> |
| 221 | <p><?php _e( 'This will delete all column settings and restore the default settings.', 'codepress-admin-columns' ); ?></p> |
| 222 | </th> |
| 223 | <td> |
| 224 | <form method="post"> |
| 225 | |
| 226 | <?php $this->nonce_field( 'restore-all' ); ?> |
| 227 | |
| 228 | <input type="hidden" name="ac_action" value="restore_all"> |
| 229 | <input type="submit" class="button" name="ac-restore-defaults" value="<?php echo esc_attr( __( 'Restore default settings', 'codepress-admin-columns' ) ); ?>" onclick="return confirm('<?php echo esc_js( __( "Warning! ALL saved admin columns data will be deleted. This cannot be undone. 'OK' to delete, 'Cancel' to stop", 'codepress-admin-columns' ) ); ?>');"> |
| 230 | </form> |
| 231 | </td> |
| 232 | </tr> |
| 233 | |
| 234 | </tbody> |
| 235 | </table> |
| 236 | |
| 237 | <?php |
| 238 | } |
| 239 | |
| 240 | } |
| 241 |