review_notice.php 3.8 KB
<?php

class CPAC_Review_Notice {

	const OPTION_INSTALL_DATE     = 'cpac-install-timestamp';
	const OPTION_ADMIN_NOTICE_KEY = 'cpac-hide-review-notice';

	private $days_since_install;
	private $cpac;

	function __construct( $cpac ) {

		register_activation_hook( __FILE__, array( $this, 'insert_install_timestamp' ) );

		// show notice after x days of installing
		$this->days_since_install = 30; // 30 days

		add_action( 'admin_init', array( $this, 'maybe_display_review_notice' ) );
		add_action( 'wp_ajax_cpac_hide_review_notice', array( $this, 'ajax_hide_review_notice' ) );
	}

	public function insert_install_timestamp() {
		add_site_option( self::OPTION_INSTALL_DATE, time() );
		return time();
	}

	private function get_install_timestamp() {
		$timestamp = get_site_option( self::OPTION_INSTALL_DATE, '' );
		if ( '' == $timestamp ) {
			$timestamp = $this->insert_install_timestamp();
		}
		return $timestamp;
	}

	public function maybe_display_review_notice() {
		if ( current_user_can( 'manage_admin_columns' ) && ( ! get_user_meta( get_current_user_id(), self::OPTION_ADMIN_NOTICE_KEY, true ) ) ) {
			if ( ( time() - ( 86400 * absint( $this->days_since_install ) ) ) >= $this->get_install_timestamp() ) {
				add_action( 'admin_notices', array( $this, 'display_admin_review_notice' ) );
			}
		}
	}

	public function ajax_hide_review_notice() {
		update_user_meta( get_current_user_id(), self::OPTION_ADMIN_NOTICE_KEY, '1', true );
	}

	public function display_admin_review_notice() {
		$screen = get_current_screen();

		// only display on settings and plugins page
		if ( ! $screen || ! in_array( $screen->parent_base, array( 'options-general', 'plugins' ) ) ) {
			return false;
		}

		$product = __( 'Admin Columns', 'codepress-admin-columns' );

		if ( defined( 'ACP_VERSION' ) ) {
			$product = __( 'Admin Columns Pro', 'codepress-admin-columns' );
		}
		?>

		<style type="text/css">
			body .wrap .cpac_message {
				position: relative;
				padding-right: 40px;
			}
			.cpac_message .spinner.right {
				visibility: visible;
				display: block;
				right: 8px;
				text-decoration: none;
				text-align: right;
				position: absolute;
				top: 50%;
				margin-top: -10px;
			}
			.cpac_message .spinner.inline {
				display: inline-block;
				position: absolute;
				margin: 4px 0 0 4px;
				padding: 0;
				float: none;
			}
			.cpac_message .hide-notice {
				right: 8px;
				text-decoration: none;
				width: 32px;
				text-align: right;
				position: absolute;
				top: 50%;
				height: 32px;
				margin-top: -16px;
			}
			.cpac_message .hide-notice:before {
				display: block;
				content: '\f335';
				font-family: 'Dashicons';
				margin: .5em 0;
				padding: 2px;
			}
			.cpac_message .buttons {
				margin-top: 8px;
			}
			.cpac_message .help {
				display: none;
			}
		</style>
		<script type="text/javascript">
			jQuery( function( $ ) {
				$( document ).ready( function() {
					$( '.updated a.hide-review-notice' ).click( function( e ) {
						e.preventDefault();

						var el = $( this ).parents( '.cpac_message' );
						var el_close = el.find( '.hide-notice' );
						var soft = $( this ).hasClass( 'hide-review-notice-soft' );

						if ( soft ) {
							el.find( '.info' ).slideUp();
							el.find( '.help' ).slideDown();
						}
						else {
							el_close.hide();
							el_close.after( '<div class="spinner right"></div>' );
							el.find( '.spinner' ).show();
						}

						$.post( ajaxurl, {
							'action': 'cpac_hide_review_notice'
						}, function( data ) {
							if ( ! soft ) {
								el.find( '.spinner' ).remove();
								el.slideUp();
							}
						} );

						return false;
					} );
				} );
			} );
		</script>
		<?php
	}
}