Making WordPress.org


Ignore:
Timestamp:
08/09/2016 02:53:51 PM (10 years ago)
Author:
iandunn
Message:

WordCamp Coming Soon Page: Implement new design and migrate to Customizer.

Fixes #334
Props melchoyce, melsenc, ryelle

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-coming-soon-page/classes/wccsp-settings.php

    r3780 r3784  
    11<?php
     2
     3/*
     4 * todo
     5 * Now that using Customizer, this class isn't really needed and doesn't make sense. Its functions can be moved
     6 * into wordcamp-coming-soon-page.php and wccsp-customizer.php
     7 */
    28
    39class WCCSP_Settings {
     
    1117                add_action( 'admin_menu',                   array( $this, 'register_settings_pages' ) );
    1218                add_action( 'init',                         array( $this, 'init' ) );
    13                 add_action( 'admin_init',                   array( $this, 'register_settings' ) );
    14                 add_action( 'admin_enqueue_scripts',        array( $this, 'enqueue_scripts' ) );
    15                 add_action( 'admin_notices',                array( $this, 'render_admin_notices' ) );
    1619                add_action( 'update_option_wccsp_settings', array( $this, 'clear_static_page_cache' ) );
    1720        }
     
    3235                $defaults = array(
    3336                        'enabled'                    => 'off',        // so that sites created before the plugin was deployed won't display the home page when the plugin is activated
    34                         'body_background_color'      => '#666666',
    35                         'container_background_color' => '#FFFFFF',
    36                         'text_color'                 => '#000000',
     37                        'body_background_color'      => '#0073aa',
    3738                        'image_id'                   => 0,
     39                        'background_id'              => 0,
     40                        'container_background_color' => '#FFFFFF', // deprecated
     41                        'text_color'                 => '#000000', // deprecated
    3842                );
    3943
     
    4751
    4852        /**
    49          * Register and enqueue the JavaScript we need for the Settings screen
     53         * Get the URL for the Coming Soon section in the Customizer
    5054         *
    51          * @param string $screen
     55         * @return string
    5256         */
    53         public function enqueue_scripts( $hook_suffix ) {
    54                 if ( 'settings_page_wccsp_settings' != $hook_suffix ) {
    55                         return;
    56                 }
    57 
    58                 wp_register_script(
    59                         'wccsp-settings',
    60                         plugins_url( '/javascript/wccsp-settings.js', __DIR__ ),
    61                         array( 'jquery', 'media-upload', 'media-views' ),
    62                         WordCamp_Coming_Soon_Page::VERSION
     57        public function get_customizer_section_url() {
     58                $url = add_query_arg(
     59                        array(
     60                                'autofocus[section]' => 'wccsp_live_preview',
     61                                'url'                => rawurlencode( add_query_arg( 'wccsp-preview', '', site_url() ) ),
     62                        ),
     63                        admin_url( 'customize.php' )
    6364                );
    6465
    65                 wp_enqueue_media();
    66                 wp_enqueue_script( 'wccsp-settings' );
     66                return $url;
    6767        }
    6868
    6969        /**
    70          * Adds pages to the Admin Panel menu
     70         * Add a link to the Settings menu
     71         *
     72         * Even though this lives in the Customizer, having a link in the regular admin menus helps with
     73         * discoverability.
    7174         */
    7275        public function register_settings_pages() {
     
    7679                        __( 'Coming Soon', 'wordcamporg' ),
    7780                        self::REQUIRED_CAPABILITY,
    78                         'wccsp_settings',
    79                         array( $this, 'markup_settings_page' )
     81                        $this->get_customizer_section_url()
    8082                );
    81         }
    82 
    83         /**
    84          * Creates the markup for the Settings page
    85          */
    86         public function markup_settings_page() {
    87                 if ( current_user_can( self::REQUIRED_CAPABILITY ) ) {
    88                         require_once( dirname( __DIR__ ) . '/views/settings-screen.php' );
    89                 } else {
    90                         wp_die( __( 'Access denied.', 'wordcamporg' ) );
    91                 }
    92         }
    93 
    94         /**
    95          * Registers settings sections, fields and settings
    96          */
    97         public function register_settings() {
    98                 add_settings_section(
    99                         'wccsp_default',
    100                         '',
    101                         array( $this, 'markup_section_headers' ),
    102                         'wccsp_settings'
    103                 );
    104 
    105 
    106                 add_settings_field(
    107                         'wccsp_enabled',
    108                         __( 'Enabled', 'wordcamporg' ),
    109                         array( $this, 'markup_fields' ),
    110                         'wccsp_settings',
    111                         'wccsp_default',
    112                         array( 'label_for' => 'wccsp_enabled_true' )
    113                 );
    114 
    115                 add_settings_field(
    116                         'wccsp_body_background_color',
    117                         __( 'Body Background Color', 'wordcamporg' ),
    118                         array( $this, 'markup_fields' ),
    119                         'wccsp_settings',
    120                         'wccsp_default',
    121                         array( 'label_for' => 'wccsp_body_background_color' )
    122                 );
    123 
    124                 add_settings_field(
    125                         'wccsp_container_background_color',
    126                         __( 'Container Background Color', 'wordcamporg' ),
    127                         array( $this, 'markup_fields' ),
    128                         'wccsp_settings',
    129                         'wccsp_default',
    130                         array( 'label_for' => 'wccsp_container_background_color' )
    131                 );
    132 
    133                 add_settings_field(
    134                         'wccsp_text_color',
    135                         __( 'Text Color', 'wordcamporg' ),
    136                         array( $this, 'markup_fields' ),
    137                         'wccsp_settings',
    138                         'wccsp_default',
    139                         array( 'label_for' => 'wccsp_text_color' )
    140                 );
    141 
    142                 add_settings_field(
    143                         'wccsp_image_id',
    144                         __( 'Image', 'wordcamporg' ),
    145                         array( $this, 'markup_fields' ),
    146                         'wccsp_settings',
    147                         'wccsp_default',
    148                         array( 'label_for' => 'wccsp_image_id' )
    149                 );
    150 
    151 
    152                 register_setting(
    153                         'wccsp_settings',
    154                         'wccsp_settings',
    155                         array( $this, 'validate_settings' )
    156                 );
    157         }
    158 
    159         /**
    160          * Adds the section introduction text to the Settings page
    161          *
    162          * @param array $section
    163          */
    164         public function markup_section_headers( $section ) {
    165                 require( dirname( __DIR__ ) . '/views/settings-section-headers.php' );
    166         }
    167 
    168         /**
    169          * Delivers the markup for settings fields
    170          *
    171          * @param array $field
    172          */
    173         public function markup_fields( $field ) {
    174                 switch ( $field['label_for'] ) {
    175                         case 'wccsp_image_id':
    176                                 $image = wp_get_attachment_image_src( $this->settings['image_id'], 'medium' );
    177                         break;
    178                 }
    179 
    180                 require( dirname( __DIR__ ) . '/views/settings-fields.php' );
    181         }
    182 
    183         /**
    184          * Validates submitted setting values before they get saved to the database.
    185          *
    186          * @param array $new_settings
    187          * @return array
    188          */
    189         public function validate_settings( $new_settings ) {
    190                 $new_settings = shortcode_atts( $this->settings, $new_settings );
    191 
    192                 if ( 'on' != $new_settings['enabled'] ) {
    193                         $new_settings['enabled'] = 'off';
    194                 }
    195 
    196                 $new_settings['body_background_color']      = sanitize_text_field( $new_settings['body_background_color'] );
    197                 $new_settings['container_background_color'] = sanitize_text_field( $new_settings['container_background_color'] );
    198                 $new_settings['text_color']                 = sanitize_text_field( $new_settings['text_color'] );
    199 
    200                 $new_settings['image_id'] = absint( $new_settings['image_id'] );
    201 
    202                 return $new_settings;
    20383        }
    20484
     
    21797         */
    21898        public function render_admin_notices() {
    219                 $current_screen = get_current_screen();
    220 
    221                 if ( 'settings_page_wccsp_settings' != $current_screen->id ) {
    222                         return;
    223                 }
    224 
    22599                $active_modules            = Jetpack::$instance->get_active_modules();
    226100                $inactive_required_modules = array();
     
    236110                }
    237111
     112                ob_start();
     113
    238114                if ( $inactive_required_modules ) {
    239115                        require_once( dirname( __DIR__ ) . '/views/settings-admin-notices.php' );
    240116                }
     117
     118                return ob_get_clean();
    241119        }
    242120} // end WCCSP_Settings
Note: See TracChangeset for help on using the changeset viewer.