From a825a7f1cec1d8267e223c84d40d0a9cd42b1be1 Mon Sep 17 00:00:00 2001
From: Ulrich Pogson <grapplerulrich@gmail.com>
Date: Sat, 19 Aug 2017 12:14:10 +0200
Subject: [PATCH] stasg

---
 .../classes/wordcamp-coming-soon-page.php          | 85 ++++++++++++++++++++++
 1 file changed, 85 insertions(+)

diff --git wordcamp.org/public_html/wp-content/plugins/wordcamp-coming-soon-page/classes/wordcamp-coming-soon-page.php wordcamp.org/public_html/wp-content/plugins/wordcamp-coming-soon-page/classes/wordcamp-coming-soon-page.php
index 8a59a966..bd437e48 100644
--- wordcamp.org/public_html/wp-content/plugins/wordcamp-coming-soon-page/classes/wordcamp-coming-soon-page.php
+++ wordcamp.org/public_html/wp-content/plugins/wordcamp-coming-soon-page/classes/wordcamp-coming-soon-page.php
@@ -15,6 +15,11 @@ class WordCamp_Coming_Soon_Page {
 		add_action( 'template_redirect',  array( $this, 'disable_jetpacks_open_graph' ) );
 
 		add_image_size( 'wccsp_image_medium_rectangle', 500, 300 );
+
+		add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu_item' ), 1000 );
+		add_action( 'admin_head', array( $this, 'admin_styling' ) );
+		add_action( 'admin_notices', array( $this, 'block_new_post_admin_notice' ) );
+		add_filter( 'get_post_metadata', array( $this, 'jetpack_dont_email_post_to_subs' ), 10, 4 );
 	}
 
 	/**
@@ -293,4 +298,84 @@ class WordCamp_Coming_Soon_Page {
 
 		return $settings['introduction'];
 	}
+
+	/**
+	 * Display notice in admin bar when coming soon mode is on.
+	 *
+	 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference
+	 */
+	function admin_bar_menu_item( $wp_admin_bar ){
+		$menu_slug = add_query_arg(
+			array(
+				'autofocus[section]' => 'wccsp_live_preview',
+				'url'                => rawurlencode( add_query_arg( 'wccsp-preview', '', site_url() ) ),
+			),
+			'/customize.php'
+		);
+		$settings    = $GLOBALS['WCCSP_Settings']->get_settings();
+		$setting_url = admin_url( $menu_slug );
+
+		if ( $settings['enabled'] !== 'on' ) {
+			return;
+		}
+
+		$wp_admin_bar->add_node( array(
+			'id'     => 'wordcamp-coming-soon-info',
+			'href'   => $setting_url,
+			'parent' => 'top-secondary',
+			'title'  => __( 'Coming Soon Mode ON', 'wordcamporg' ),
+			'meta'   => array( 'class' => 'wc-coming-soon-info' ),
+		) );
+	}
+
+	public function admin_styling() {
+		echo '<style  type="text/css">#wpadminbar .wc-coming-soon-info { background: #0085ba; }</style>';
+	}
+
+	/*
+	 * Show a notice if comming soon is enabled.
+	 *
+	 * Explain to users why publishing is disabled when comming soon is enabled.
+	 *
+	 */
+	public function block_new_post_admin_notice(){
+		$menu_slug = add_query_arg(
+			array(
+				'autofocus[section]' => 'wccsp_live_preview',
+				'url'                => rawurlencode( add_query_arg( 'wccsp-preview', '', site_url() ) ),
+			),
+			'/customize.php'
+		);
+		$settings    = $GLOBALS['WCCSP_Settings']->get_settings();
+		$setting_url = admin_url( $menu_slug );
+
+		if ( $settings['enabled'] !== 'on' ) {
+			return;
+		}
+
+		$screen = get_current_screen();
+		if ( trim( $screen->id ) == 'post' ) {
+			$class = 'notice notice-info';
+			$message = sprintf( __( 'Publishing a post while in <a href="%s">Coming Soon mode</a> will not send an email to the subscribers.', 'wordcamporg' ), $setting_url );
+			printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message );
+		}
+	}
+
+	/**
+	 * Disable sending of Jetpack emails when comming soon mode is on.
+	 *
+	 * @param null|array|string $value     The value get_metadata() should return - a single metadata value,
+	 *                                     or an array of values.
+	 * @param int               $object_id Object ID.
+	 * @param string            $meta_key  Meta key.
+	 * @param bool              $single    Whether to return only the first value of the specified $meta_key.
+	 */
+	function jetpack_dont_email_post_to_subs( $value, $object_id, $meta_key, $single ) {
+		$settings = $GLOBALS['WCCSP_Settings']->get_settings();
+		if ( $settings['enabled'] !== 'on' && '_jetpack_dont_email_post_to_subs' === $meta_key ) {
+			return true;
+		}
+		return $value;
+	}
+
 } // end WordCamp_Coming_Soon_Page
-- 
2.11.0 (Apple Git-81)

