class-do-css.php
6 years ago
class-enqueue-css.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
167 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 | |
| 23 | if ( ! empty( $_POST ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 24 | add_action( 'wp_ajax_generateblocks_regenerate_css_files', array( $this, 'regenerate_css_files' ) ); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Add our Dashboard menu item. |
| 30 | */ |
| 31 | public function add_menu() { |
| 32 | $settings = add_options_page( |
| 33 | __( 'Settings', 'generateblocks' ), |
| 34 | __( 'GenerateBlocks', 'generateblocks' ), |
| 35 | 'manage_options', |
| 36 | 'generateblocks-settings', |
| 37 | array( $this, 'settings_page' ) |
| 38 | ); |
| 39 | |
| 40 | add_action( "admin_print_scripts-$settings", array( $this, 'enqueue_scripts' ) ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Enqueue our scripts. |
| 45 | */ |
| 46 | public function enqueue_scripts() { |
| 47 | wp_enqueue_script( |
| 48 | 'generateblocks-settings', |
| 49 | GENERATEBLOCKS_DIR_URL . 'assets/js/scripts.js', |
| 50 | array( 'jquery' ), |
| 51 | filemtime( GENERATEBLOCKS_DIR . 'assets/js/scripts.js' ), |
| 52 | true |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Save our settings. |
| 58 | */ |
| 59 | public function save() { |
| 60 | if ( isset( $_POST['generateblocks_settings'] ) ) { |
| 61 | if ( ! check_admin_referer( 'generateblocks_settings', 'generateblocks_settings' ) ) { |
| 62 | wp_die( esc_html( __( 'Security check failed.', 'generateblocks' ) ) ); |
| 63 | } |
| 64 | |
| 65 | if ( ! current_user_can( 'manage_options' ) ) { |
| 66 | wp_die( esc_html( __( 'Security check failed.', 'generateblocks' ) ) ); |
| 67 | } |
| 68 | |
| 69 | $settings = get_option( 'generateblocks', array() ); |
| 70 | $values = $_POST['generateblocks']; |
| 71 | |
| 72 | if ( isset( $values['css_print_method'] ) ) { |
| 73 | $settings['css_print_method'] = sanitize_key( $values['css_print_method'] ); |
| 74 | } |
| 75 | |
| 76 | update_option( 'generateblocks', $settings ); |
| 77 | |
| 78 | wp_safe_redirect( admin_url( 'admin.php?page=generateblocks-settings' ) ); |
| 79 | exit; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Regenerate our CSS files. |
| 85 | */ |
| 86 | public function regenerate_css_files() { |
| 87 | check_ajax_referer( 'generateblocks_regenerate_css_files', '_nonce' ); |
| 88 | |
| 89 | if ( ! current_user_can( 'manage_options' ) ) { |
| 90 | wp_send_json_error( __( 'Security check failed.', 'generateblocks' ) ); |
| 91 | } |
| 92 | |
| 93 | update_option( 'generateblocks_dynamic_css_posts', array() ); |
| 94 | |
| 95 | wp_send_json_success(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Output our Dashboard HTML. |
| 100 | * |
| 101 | * @since 0.1 |
| 102 | */ |
| 103 | public function settings_page() { |
| 104 | ?> |
| 105 | <div class="wrap gblocks-dashboard-wrap"> |
| 106 | <div class="gblocks-dashboard-header"> |
| 107 | <h1><?php esc_html_e( 'Settings', 'generateblocks' ); ?></h1> |
| 108 | |
| 109 | <?php generateblocks_dashboard_navigation(); ?> |
| 110 | </div> |
| 111 | |
| 112 | <div class="gblocks-settings-content"> |
| 113 | <form action="options.php" method="post"> |
| 114 | <?php |
| 115 | wp_nonce_field( 'generateblocks_settings', 'generateblocks_settings' ); |
| 116 | ?> |
| 117 | <table class="form-table" role="presentation"> |
| 118 | <tbody> |
| 119 | <tr> |
| 120 | <th scope="row"> |
| 121 | <?php esc_html_e( 'CSS Print Method', 'generateblocks' ); ?> |
| 122 | </th> |
| 123 | <td> |
| 124 | <select name="generateblocks[css_print_method]"> |
| 125 | <option value="file"<?php selected( 'file', generateblocks_get_option( 'css_print_method' ) ); ?>><?php esc_html_e( 'External File', 'generateblocks' ); ?></option> |
| 126 | <option value="inline"<?php selected( 'inline', generateblocks_get_option( 'css_print_method' ) ); ?>><?php esc_html_e( 'Inline Embedding', 'generateblocks' ); ?></option> |
| 127 | </select> |
| 128 | <p><?php esc_html_e( 'Generating your CSS in external files is better for overall performance.', 'generateblocks' ); ?></p> |
| 129 | </td> |
| 130 | </tr> |
| 131 | <tr> |
| 132 | <th scope="row"> |
| 133 | <?php esc_html_e( 'Regenerate CSS', 'generateblocks' ); ?> |
| 134 | </th> |
| 135 | <td> |
| 136 | <?php |
| 137 | printf( |
| 138 | '<button data-nonce="%s" class="button generateblocks-button-spinner" id="generateblocks-regenerate-css-files-button">%s</button>', |
| 139 | wp_create_nonce( 'generateblocks_regenerate_css_files' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 140 | esc_html__( 'Regenerate Files', 'generateblocks' ) |
| 141 | ); |
| 142 | ?> |
| 143 | <p><?php esc_html_e( 'Force your external CSS files to regenerate next time their page is loaded.', 'generateblocks' ); ?></p> |
| 144 | </td> |
| 145 | </tr> |
| 146 | <?php |
| 147 | /** |
| 148 | * Do generateblocks_settings_fields hook. |
| 149 | * |
| 150 | * @since 1.0 |
| 151 | */ |
| 152 | do_action( 'generateblocks_settings_fields' ); |
| 153 | ?> |
| 154 | </tbody> |
| 155 | </table> |
| 156 | <?php |
| 157 | submit_button(); |
| 158 | ?> |
| 159 | </form> |
| 160 | </div> |
| 161 | </div> |
| 162 | <?php |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | new GenerateBlocks_Settings(); |
| 167 |