Making WordPress.org

Ticket #859: 859.patch

File 859.patch, 16.8 KB (added by JayantiC, 9 years ago)

I have added: 1) notification at top right corner for coming soon mode 2) Display notice at publish article page 3) User can't publish article in coming soon mode.

  • classes/wordcamp-coming-soon-page.php

     
    1 <?php
    2 
    3 class WordCamp_Coming_Soon_Page {
    4         protected $override_theme_template;
    5         const VERSION = '0.1';
    6        
    7         /**
    8          * Constructor
    9          */
    10         public function __construct() {
    11                 add_action( 'init',               array( $this, 'init' ), 11 );                               // after WCCSP_Settings::init()
    12                 add_action( 'wp_enqueue_scripts', array( $this, 'manage_plugin_theme_stylesheets' ), 99 );    // (hopefully) after all plugins/themes have enqueued their styles
    13                 add_action( 'wp_head',            array( $this, 'render_dynamic_styles' ) );
    14                 add_filter( 'template_include',   array( $this, 'override_theme_template' ) );
    15 
    16                 add_image_size( 'wccsp_image_medium_rectangle', 500, 300 );
    17         }
    18 
    19         /**
    20          * Initialize variables
    21          */
    22         public function init() {
    23                 $settings                      = $GLOBALS['WCCSP_Settings']->get_settings();
    24                 $this->override_theme_template = 'on' == $settings['enabled'] && ! current_user_can( 'edit_posts' );
    25         }
    26 
    27         /**
    28          * Ensure the template has a consistent base of CSS rules, regardless of the current theme or Custom CSS
    29          * Dequeue irrelevant stylesheets and use TwentyThirteen as the base style
    30          */
    31         public function manage_plugin_theme_stylesheets() {
    32                 if ( ! $this->override_theme_template ) {
    33                         return;
    34                 }
    35                
    36                 $this->dequeue_all_stylesheets();
    37                 $this->register_twentythirteen_styles();
    38 
    39                 wp_enqueue_style(
    40                         'wccsp-template',
    41                         plugins_url( '/css/template-coming-soon.css', __DIR__ ),
    42                         array( 'twentythirteen-fonts', 'genericons', 'twentythirteen-style', 'admin-bar' ),
    43                         self::VERSION
    44                 );
    45         }
    46 
    47         /**
    48          * Dequeue all enqueued stylesheets and Custom CSS
    49          */
    50         protected function dequeue_all_stylesheets() {
    51                 foreach( $GLOBALS['wp_styles']->queue as $stylesheet ) {
    52                         wp_dequeue_style( $stylesheet );
    53                 }
    54 
    55                 remove_action( 'wp_head', array( 'Jetpack_Custom_CSS', 'link_tag' ), 101 );
    56         }
    57 
    58         /**
    59          * Register TwentyThirteen's base styles
    60          */
    61         protected function register_twentythirteen_styles() {
    62                 $twentythirteen_uri = get_theme_root_uri( 'twentythirteen' ) . '/twentythirteen';
    63                
    64                 if ( ! wp_style_is( 'twentythirteen-fonts', 'registered' ) ) {
    65                         wp_register_style( 'twentythirteen-fonts', '//fonts.googleapis.com/css?family=Source+Sans+Pro%3A300%2C400%2C700%2C300italic%2C400italic%2C700italic%7CBitter%3A400%2C700&#038;subset=latin%2Clatin-ext', array(), null );
    66                 }
    67 
    68                 if ( ! wp_style_is( 'genericons', 'registered' ) ) {
    69                         wp_register_style( 'genericons', $twentythirteen_uri . '/fonts/genericons.css' );
    70                 }
    71 
    72                 if ( ! wp_style_is( 'twentythirteen-style', 'registered' ) ) {
    73                         wp_register_style( 'twentythirteen-style', $twentythirteen_uri . '/style.css' );
    74                 }
    75         }
    76 
    77         /**
    78          * Render dynamic CSS styles
    79          */
    80         public function render_dynamic_styles() {
    81                 if ( ! $this->override_theme_template ) {
    82                         return;
    83                 }
    84                
    85                 $settings = $GLOBALS['WCCSP_Settings']->get_settings();
    86                 ?>
    87                
    88                 <!-- BEGIN wordcamp-coming-soon-page -->
    89                 <style type="text/css">
    90                         html, body {
    91                                 color: <?php echo esc_html( $settings['text_color'] ); ?>;
    92                         }
    93                        
    94                         #wccsp-container,
    95                         .widget  {
    96                                 background-color: <?php echo esc_html( $settings['container_background_color'] ); ?>;
    97                         }
    98 
    99                         @media all and ( min-width: 800px ) {
    100                                 html, body {
    101                                         background-color: <?php echo esc_html( $settings['body_background_color'] ); ?>;
    102                                 }
    103                         }
    104                 </style>
    105                 <!-- END wordcamp-coming-soon-page -->
    106 
    107                 <?php
    108         }
    109 
    110         /**
    111          * Load the Coming Soon template instead of a theme template
    112          *
    113          * @param string $template
    114          * @return string
    115          */
    116         public function override_theme_template( $template ) {
    117                 if ( $this->override_theme_template ) {
    118                         $template = dirname( __DIR__ ) . '/views/template-coming-soon.php';
    119                 }
    120                
    121                 return $template;
    122         }
    123 
    124         /**
    125          * Collect all of the variables the Coming Soon template will need
    126          * Doing this here keeps the template less cluttered and more of a pure view
    127          *
    128          * @return array
    129          */
    130         function get_template_variables() {
    131                 $variables = array(
    132                         'image_url'              => $this->get_image_url(),
    133                         'dates'                  => $this->get_dates(),
    134                         'active_modules'         => Jetpack::$instance->get_active_modules(),
    135                         'contact_form_shortcode' => $this->get_contact_form_shortcode(),
    136                 );
    137 
    138                 return $variables;
    139         }
    140 
    141         /**
    142          * Retrieve the URL of the image displayed in the template
    143          *
    144          * @return string|false
    145          */
    146         public function get_image_url() {
    147                 $settings   = $GLOBALS['WCCSP_Settings']->get_settings();
    148                 $image_meta = wp_get_attachment_metadata( $settings['image_id'] );
    149                 $size       = isset( $image_meta['sizes']['wccsp_image_medium_rectangle'] ) ? 'wccsp_image_medium_rectangle' : 'full';
    150                 $image      = wp_get_attachment_image_src( $settings['image_id'], $size );
    151                
    152                 return $image ? $image[0] : false;
    153         }
    154 
    155         /**
    156          * Retrieve the dates of the WordCamp
    157          *
    158          * @return string|false
    159          */
    160         public function get_dates() {
    161                 $dates = false;
    162                 $wordcamp_post = get_wordcamp_post();
    163 
    164                 if ( isset( $wordcamp_post->ID ) ) {
    165                         if ( ! empty( $wordcamp_post->meta['Start Date (YYYY-mm-dd)'][0] ) ) {
    166                                 // translators: date format, see https://php.net/date
    167                                 $dates = date_i18n( __( 'l, F jS Y' , 'wordcamporg' ), $wordcamp_post->meta['Start Date (YYYY-mm-dd)'][0] );
    168 
    169                                 if ( ! empty( $wordcamp_post->meta['End Date (YYYY-mm-dd)'][0] ) ) {
    170                                         if ( $wordcamp_post->meta['Start Date (YYYY-mm-dd)'][0] !== $wordcamp_post->meta['End Date (YYYY-mm-dd)'][0] ) {
    171                                                 // translators: date format, see https://php.net/date
    172                                                 $dates .= ' - ' . date_i18n( __( 'l, F jS Y' , 'wordcamporg' ), $wordcamp_post->meta['End Date (YYYY-mm-dd)'][0] );
    173                                         }
    174                                 }
    175                         }
    176                 }
    177                
    178                 return $dates;
    179         }
    180 
    181         /**
    182          * Retrieve the contact form shortcode string
    183          *
    184          * We can't just create an arbitrary shortcode because of https://github.com/Automattic/jetpack/issues/102. Instead we have to use a form that's tied to a page.
    185          * This is somewhat fragile, though. It should work in most cases because the first $page that contains [contact-form] will be the one we automatically create
    186          * when the site is created, but if the organizers delete that and then add multiple forms, the wrong form could be displayed. The alternative approaches also
    187          * have problems, though, and #102 should be fixed relatively soon, so hopefully this will be good enough until it can be refactored.
    188          * todo Refactor this once #102-jetpack is fixed.
    189          *       
    190          * @return string|false
    191          */
    192         public function get_contact_form_shortcode() {
    193                 $contact_form_shortcode = false;
    194                 $shortcode_regex        = get_shortcode_regex();
    195                
    196                 $all_pages = get_posts( array(
    197                         'post_type'      => 'page',
    198                         'posts_per_page' => -1,
    199                 ) );
    200                
    201                 foreach ( $all_pages as $page ) {
    202                         preg_match_all( '/' . $shortcode_regex . '/s', $page->post_content, $matches, PREG_SET_ORDER );
    203 
    204                         foreach ( $matches as $shortcode ) {
    205                                 if ( 'contact-form' === $shortcode[2] ) {
    206                                         global $post;
    207                                         $post = $page;
    208                                         setup_postdata( $post );
    209                                        
    210                                         ob_start();
    211                                         echo do_shortcode( $shortcode[0] );
    212                                         $contact_form_shortcode = ob_get_clean();
    213                                        
    214                                         wp_reset_postdata();
    215                                         break;
    216                                 }
    217                         }
    218                 }
    219                
    220                 return $contact_form_shortcode;
    221         }
    222 } // end WordCamp_Coming_Soon_Page
     1<?php
     2
     3class WordCamp_Coming_Soon_Page {
     4        protected $override_theme_template;
     5        const VERSION = '0.1';
     6       
     7        /**
     8         * Constructor
     9         */
     10        public function __construct() {
     11                add_action( 'init',               array( $this, 'init' ), 11 );                               // after WCCSP_Settings::init()
     12                add_action( 'wp_enqueue_scripts', array( $this, 'manage_plugin_theme_stylesheets' ), 99 );    // (hopefully) after all plugins/themes have enqueued their styles
     13                add_action( 'wp_head',            array( $this, 'render_dynamic_styles' ) );
     14                add_filter( 'template_include',   array( $this, 'override_theme_template' ) );
     15
     16                add_image_size( 'wccsp_image_medium_rectangle', 500, 300 );
     17                add_action( 'admin_bar_menu',array( &$this, 'display_coming_soon_notice' ), 1000 );
     18        }
     19
     20        /**
     21         * Initialize variables
     22         */
     23        public function init() {
     24                $settings                      = $GLOBALS['WCCSP_Settings']->get_settings();
     25                $this->override_theme_template = 'on' == $settings['enabled'] && ! current_user_can( 'edit_posts' );
     26        }
     27
     28        /**
     29         * Ensure the template has a consistent base of CSS rules, regardless of the current theme or Custom CSS
     30         * Dequeue irrelevant stylesheets and use TwentyThirteen as the base style
     31         */
     32        public function manage_plugin_theme_stylesheets() {
     33                if ( ! $this->override_theme_template ) {
     34                        return;
     35                }
     36               
     37                $this->dequeue_all_stylesheets();
     38                $this->register_twentythirteen_styles();
     39
     40                wp_enqueue_style(
     41                        'wccsp-template',
     42                        plugins_url( '/css/template-coming-soon.css', __DIR__ ),
     43                        array( 'twentythirteen-fonts', 'genericons', 'twentythirteen-style', 'admin-bar' ),
     44                        self::VERSION
     45                );
     46        }
     47
     48        /**
     49         * Dequeue all enqueued stylesheets and Custom CSS
     50         */
     51        protected function dequeue_all_stylesheets() {
     52                foreach( $GLOBALS['wp_styles']->queue as $stylesheet ) {
     53                        wp_dequeue_style( $stylesheet );
     54                }
     55
     56                remove_action( 'wp_head', array( 'Jetpack_Custom_CSS', 'link_tag' ), 101 );
     57        }
     58
     59        /**
     60         * Register TwentyThirteen's base styles
     61         */
     62        protected function register_twentythirteen_styles() {
     63                $twentythirteen_uri = get_theme_root_uri( 'twentythirteen' ) . '/twentythirteen';
     64               
     65                if ( ! wp_style_is( 'twentythirteen-fonts', 'registered' ) ) {
     66                        wp_register_style( 'twentythirteen-fonts', '//fonts.googleapis.com/css?family=Source+Sans+Pro%3A300%2C400%2C700%2C300italic%2C400italic%2C700italic%7CBitter%3A400%2C700&#038;subset=latin%2Clatin-ext', array(), null );
     67                }
     68
     69                if ( ! wp_style_is( 'genericons', 'registered' ) ) {
     70                        wp_register_style( 'genericons', $twentythirteen_uri . '/fonts/genericons.css' );
     71                }
     72
     73                if ( ! wp_style_is( 'twentythirteen-style', 'registered' ) ) {
     74                        wp_register_style( 'twentythirteen-style', $twentythirteen_uri . '/style.css' );
     75                }
     76        }
     77
     78        /**
     79         * Render dynamic CSS styles
     80         */
     81        public function render_dynamic_styles() {
     82                if ( ! $this->override_theme_template ) {
     83                        return;
     84                }
     85               
     86                $settings = $GLOBALS['WCCSP_Settings']->get_settings();
     87                ?>
     88               
     89                <!-- BEGIN wordcamp-coming-soon-page -->
     90                <style type="text/css">
     91                        html, body {
     92                                color: <?php echo esc_html( $settings['text_color'] ); ?>;
     93                        }
     94                       
     95                        #wccsp-container,
     96                        .widget  {
     97                                background-color: <?php echo esc_html( $settings['container_background_color'] ); ?>;
     98                        }
     99
     100                        @media all and ( min-width: 800px ) {
     101                                html, body {
     102                                        background-color: <?php echo esc_html( $settings['body_background_color'] ); ?>;
     103                                }
     104                        }
     105                </style>
     106                <!-- END wordcamp-coming-soon-page -->
     107
     108                <?php
     109        }
     110
     111        /**
     112         * Load the Coming Soon template instead of a theme template
     113         *
     114         * @param string $template
     115         * @return string
     116         */
     117        public function override_theme_template( $template ) {
     118                if ( $this->override_theme_template ) {
     119                        $template = dirname( __DIR__ ) . '/views/template-coming-soon.php';
     120                }
     121               
     122                return $template;
     123        }
     124
     125        /**
     126         * Collect all of the variables the Coming Soon template will need
     127         * Doing this here keeps the template less cluttered and more of a pure view
     128         *
     129         * @return array
     130         */
     131        function get_template_variables() {
     132                $variables = array(
     133                        'image_url'              => $this->get_image_url(),
     134                        'dates'                  => $this->get_dates(),
     135                        'active_modules'         => Jetpack::$instance->get_active_modules(),
     136                        'contact_form_shortcode' => $this->get_contact_form_shortcode(),
     137                );
     138
     139                return $variables;
     140        }
     141
     142        /**
     143         * Retrieve the URL of the image displayed in the template
     144         *
     145         * @return string|false
     146         */
     147        public function get_image_url() {
     148                $settings   = $GLOBALS['WCCSP_Settings']->get_settings();
     149                $image_meta = wp_get_attachment_metadata( $settings['image_id'] );
     150                $size       = isset( $image_meta['sizes']['wccsp_image_medium_rectangle'] ) ? 'wccsp_image_medium_rectangle' : 'full';
     151                $image      = wp_get_attachment_image_src( $settings['image_id'], $size );
     152               
     153                return $image ? $image[0] : false;
     154        }
     155
     156        /**
     157         * Retrieve the dates of the WordCamp
     158         *
     159         * @return string|false
     160         */
     161        public function get_dates() {
     162                $dates = false;
     163                $wordcamp_post = get_wordcamp_post();
     164
     165                if ( isset( $wordcamp_post->ID ) ) {
     166                        if ( ! empty( $wordcamp_post->meta['Start Date (YYYY-mm-dd)'][0] ) ) {
     167                                // translators: date format, see https://php.net/date
     168                                $dates = date_i18n( __( 'l, F jS Y' , 'wordcamporg' ), $wordcamp_post->meta['Start Date (YYYY-mm-dd)'][0] );
     169
     170                                if ( ! empty( $wordcamp_post->meta['End Date (YYYY-mm-dd)'][0] ) ) {
     171                                        if ( $wordcamp_post->meta['Start Date (YYYY-mm-dd)'][0] !== $wordcamp_post->meta['End Date (YYYY-mm-dd)'][0] ) {
     172                                                // translators: date format, see https://php.net/date
     173                                                $dates .= ' - ' . date_i18n( __( 'l, F jS Y' , 'wordcamporg' ), $wordcamp_post->meta['End Date (YYYY-mm-dd)'][0] );
     174                                        }
     175                                }
     176                        }
     177                }
     178               
     179                return $dates;
     180        }
     181
     182        /**
     183         * Retrieve the contact form shortcode string
     184         *
     185         * We can't just create an arbitrary shortcode because of https://github.com/Automattic/jetpack/issues/102. Instead we have to use a form that's tied to a page.
     186         * This is somewhat fragile, though. It should work in most cases because the first $page that contains [contact-form] will be the one we automatically create
     187         * when the site is created, but if the organizers delete that and then add multiple forms, the wrong form could be displayed. The alternative approaches also
     188         * have problems, though, and #102 should be fixed relatively soon, so hopefully this will be good enough until it can be refactored.
     189         * todo Refactor this once #102-jetpack is fixed.
     190         *       
     191         * @return string|false
     192         */
     193        public function get_contact_form_shortcode() {
     194                $contact_form_shortcode = false;
     195                $shortcode_regex        = get_shortcode_regex();
     196               
     197                $all_pages = get_posts( array(
     198                        'post_type'      => 'page',
     199                        'posts_per_page' => -1,
     200                ) );
     201               
     202                foreach ( $all_pages as $page ) {
     203                        preg_match_all( '/' . $shortcode_regex . '/s', $page->post_content, $matches, PREG_SET_ORDER );
     204
     205                        foreach ( $matches as $shortcode ) {
     206                                if ( 'contact-form' === $shortcode[2] ) {
     207                                        global $post;
     208                                        $post = $page;
     209                                        setup_postdata( $post );
     210                                       
     211                                        ob_start();
     212                                        echo do_shortcode( $shortcode[0] );
     213                                        $contact_form_shortcode = ob_get_clean();
     214                                       
     215                                        wp_reset_postdata();
     216                                        break;
     217                                }
     218                        }
     219                }
     220               
     221                return $contact_form_shortcode;
     222        }
     223       
     224        /**
     225     * Display notice in admin bar when coming soon mode is on
     226     */
     227    function display_coming_soon_notice($str){
     228        global  $pagenow,$wp_admin_bar;
     229                wp_enqueue_script('jquery');
     230               
     231        $settings= $GLOBALS['WCCSP_Settings']->get_settings();
     232        $msg = '';
     233               
     234                $setting_url=admin_url().'options-general.php?page=wccsp_settings';
     235               
     236        if($settings['enabled']=='on'){
     237                       
     238                                $msg = __('
     239                                <style>.wc-coming-soon-info{
     240                                        background-color:#0085ba !important;
     241                                }</style>
     242                                You are in coming soon mode','coming-soon');
     243                       
     244                                if ( $pagenow == 'post-new.php' || $pagenow == 'post.php' ) {
     245                                                $class = 'notice notice-error';
     246                                                $message = __( 'You cannot pubilsh posts while in Coming Soon mode. <br />Once the site is ready for a public audience, please <a href="' . $setting_url . '">disable                   Coming Soon mode</a> and then publish this post.', 'sample-text-domain' );
     247                                               
     248                                                echo "
     249                                                <script>
     250                                                        jQuery(document).ready(function(){
     251                                                                  jQuery('#publish').click(function()
     252                                                                  {
     253                                                                                        alert('You cannot pubilsh posts while in Coming Soon mode. Once the site is ready for a public audience, please disable Coming Soon mode and then publish this post.');
     254                                                                                        setTimeout(\"jQuery('#ajax-loading').css('visibility', 'hidden');\", 100);
     255                                                                                        jQuery('[id^=\"taxonomy\"]').find('.tabs-panel').css('background', '#F96');
     256                                                                                        setTimeout(\"jQuery('#publish').removeClass('button-primary-disabled');\", 100);
     257                                                                                        return false;
     258                                                                  });
     259                                                  });
     260                                                  </script>";
     261                                               
     262                                                printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message );
     263                                        }
     264           }
     265               
     266        $wp_admin_bar->add_menu( array(
     267            'id'     => 'wordcamp-coming-soon-info',
     268            'href' => $setting_url,
     269            'parent' => 'top-secondary',
     270            'title'  => $msg,
     271            'meta'   => array( 'class' => 'wc-coming-soon-info' ),
     272        ) );
     273               
     274    }
     275       
     276} // end WordCamp_Coming_Soon_Page