class-do-css.php
6 years ago
class-enqueue-css.php
6 years ago
class-plugin-update.php
6 years ago
class-settings.php
6 years ago
dashboard.php
6 years ago
defaults.php
6 years ago
functions.php
6 years ago
general.php
6 years ago
generate-css.php
6 years ago
class-settings.php
206 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Our settings page. |
| 4 | * |
| 5 | * @package GenerateBlocks |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Build our settings page. |
| 14 | */ |
| 15 | class GenerateBlocks_Settings { |
| 16 | /** |
| 17 | * Constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | add_action( 'admin_menu', array( $this, 'add_menu' ) ); |
| 21 | add_action( 'admin_init', array( $this, 'save' ) ); |
| 22 | add_action( 'generateblocks_before_settings_form', array( $this, 'admin_notices' ) ); |
| 23 | |
| 24 | if ( ! empty( $_POST ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Just checking, false positive. |
| 25 | add_action( 'wp_ajax_generateblocks_regenerate_css_files', array( $this, 'regenerate_css_files' ) ); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Add our Dashboard menu item. |
| 31 | */ |
| 32 | public function add_menu() { |
| 33 | $settings = add_options_page( |
| 34 | __( 'Settings', 'generateblocks' ), |
| 35 | __( 'GenerateBlocks', 'generateblocks' ), |
| 36 | 'manage_options', |
| 37 | 'generateblocks-settings', |
| 38 | array( $this, 'settings_page' ) |
| 39 | ); |
| 40 | |
| 41 | add_action( "admin_print_scripts-$settings", array( $this, 'enqueue_scripts' ) ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Enqueue our scripts. |
| 46 | */ |
| 47 | public function enqueue_scripts() { |
| 48 | wp_enqueue_script( |
| 49 | 'generateblocks-settings', |
| 50 | GENERATEBLOCKS_DIR_URL . 'assets/js/scripts.js', |
| 51 | array( 'jquery' ), |
| 52 | filemtime( GENERATEBLOCKS_DIR . 'assets/js/scripts.js' ), |
| 53 | true |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Save our settings. |
| 59 | */ |
| 60 | public function save() { |
| 61 | if ( isset( $_POST['generateblocks_settings'] ) ) { |
| 62 | if ( ! check_admin_referer( 'generateblocks_settings', 'generateblocks_settings' ) ) { |
| 63 | wp_die( esc_html( __( 'Security check failed.', 'generateblocks' ) ) ); |
| 64 | } |
| 65 | |
| 66 | if ( ! current_user_can( 'manage_options' ) ) { |
| 67 | wp_die( esc_html( __( 'Security check failed.', 'generateblocks' ) ) ); |
| 68 | } |
| 69 | |
| 70 | $settings = get_option( 'generateblocks', array() ); |
| 71 | $values = $_POST['generateblocks']; |
| 72 | |
| 73 | if ( isset( $values['css_print_method'] ) ) { |
| 74 | $settings['css_print_method'] = sanitize_key( $values['css_print_method'] ); |
| 75 | } |
| 76 | |
| 77 | if ( isset( $values['color_component_display'] ) ) { |
| 78 | $settings['color_component_display'] = sanitize_key( $values['color_component_display'] ); |
| 79 | } |
| 80 | |
| 81 | update_option( 'generateblocks', $settings ); |
| 82 | |
| 83 | wp_safe_redirect( admin_url( 'admin.php?page=generateblocks-settings&settings-updated=true' ) ); |
| 84 | exit; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Regenerate our CSS files. |
| 90 | */ |
| 91 | public function regenerate_css_files() { |
| 92 | check_ajax_referer( 'generateblocks_regenerate_css_files', '_nonce' ); |
| 93 | |
| 94 | if ( ! current_user_can( 'manage_options' ) ) { |
| 95 | wp_send_json_error( __( 'Security check failed.', 'generateblocks' ) ); |
| 96 | } |
| 97 | |
| 98 | update_option( 'generateblocks_dynamic_css_posts', array() ); |
| 99 | |
| 100 | wp_send_json_success(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Output our Dashboard HTML. |
| 105 | * |
| 106 | * @since 0.1 |
| 107 | */ |
| 108 | public function settings_page() { |
| 109 | ?> |
| 110 | <div class="wrap gblocks-dashboard-wrap"> |
| 111 | <div class="gblocks-dashboard-header"> |
| 112 | <div class="gblocks-dashboard-header-content"> |
| 113 | <h1><?php esc_html_e( 'Settings', 'generateblocks' ); ?></h1> |
| 114 | </div> |
| 115 | <?php generateblocks_dashboard_navigation(); ?> |
| 116 | </div> |
| 117 | |
| 118 | <div class="gblocks-settings-content"> |
| 119 | <?php do_action( 'generateblocks_before_settings_form' ); ?> |
| 120 | <form action="options.php" method="post"> |
| 121 | <?php |
| 122 | wp_nonce_field( 'generateblocks_settings', 'generateblocks_settings' ); |
| 123 | ?> |
| 124 | <table class="form-table" role="presentation"> |
| 125 | <tbody> |
| 126 | <tr> |
| 127 | <th scope="row"> |
| 128 | <?php esc_html_e( 'CSS Print Method', 'generateblocks' ); ?> |
| 129 | </th> |
| 130 | <td> |
| 131 | <select name="generateblocks[css_print_method]"> |
| 132 | <option value="file"<?php selected( 'file', generateblocks_get_option( 'css_print_method' ) ); ?>><?php esc_html_e( 'External File', 'generateblocks' ); ?></option> |
| 133 | <option value="inline"<?php selected( 'inline', generateblocks_get_option( 'css_print_method' ) ); ?>><?php esc_html_e( 'Inline Embedding', 'generateblocks' ); ?></option> |
| 134 | </select> |
| 135 | <p><?php esc_html_e( 'Generating your CSS in external files is better for overall performance.', 'generateblocks' ); ?></p> |
| 136 | </td> |
| 137 | </tr> |
| 138 | <tr> |
| 139 | <th scope="row"> |
| 140 | <?php esc_html_e( 'Regenerate CSS', 'generateblocks' ); ?> |
| 141 | </th> |
| 142 | <td> |
| 143 | <?php |
| 144 | printf( |
| 145 | '<button data-nonce="%s" class="button generateblocks-button-spinner" id="generateblocks-regenerate-css-files-button">%s</button>', |
| 146 | esc_html( wp_create_nonce( 'generateblocks_regenerate_css_files' ) ), |
| 147 | esc_html__( 'Regenerate Files', 'generateblocks' ) |
| 148 | ); |
| 149 | ?> |
| 150 | <p><?php esc_html_e( 'Force your external CSS files to regenerate next time their page is loaded.', 'generateblocks' ); ?></p> |
| 151 | </td> |
| 152 | </tr> |
| 153 | <tr> |
| 154 | <th scope="row"> |
| 155 | <?php esc_html_e( 'Color Component Display', 'generateblocks' ); ?> |
| 156 | </th> |
| 157 | <td> |
| 158 | <select name="generateblocks[color_component_display]"> |
| 159 | <option value="palette"<?php selected( 'palette', generateblocks_get_option( 'color_component_display' ) ); ?>><?php esc_html_e( 'Color Palette', 'generateblocks' ); ?></option> |
| 160 | <option value="custom-colors"<?php selected( 'custom-colors', generateblocks_get_option( 'color_component_display' ) ); ?>><?php esc_html_e( 'Custom Colors', 'generateblocks' ); ?></option> |
| 161 | </select> |
| 162 | <p><?php esc_html_e( 'Choose what the Color Component displays by default.', 'generateblocks' ); ?></p> |
| 163 | </td> |
| 164 | </tr> |
| 165 | <?php |
| 166 | /** |
| 167 | * Do generateblocks_settings_fields hook. |
| 168 | * |
| 169 | * @since 1.0 |
| 170 | */ |
| 171 | do_action( 'generateblocks_settings_fields' ); |
| 172 | ?> |
| 173 | </tbody> |
| 174 | </table> |
| 175 | <?php |
| 176 | submit_button(); |
| 177 | ?> |
| 178 | </form> |
| 179 | </div> |
| 180 | </div> |
| 181 | <?php |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Add a message when settings are saved. |
| 186 | * |
| 187 | * @since 1.1 |
| 188 | */ |
| 189 | public function admin_notices() { |
| 190 | $screen = get_current_screen(); |
| 191 | |
| 192 | if ( 'settings_page_generateblocks-settings' !== $screen->base ) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | if ( isset( $_GET['settings-updated'] ) && 'true' == $_GET['settings-updated'] ) { // phpcs:ignore -- Just checking, false positive. |
| 197 | printf( |
| 198 | '<div class="notice notice-success inline"><p><strong>Settings saved.</strong></p></div>', |
| 199 | esc_html__( 'Settings saved.', 'generateblocks' ) |
| 200 | ); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | new GenerateBlocks_Settings(); |
| 206 |