HEX
Server: Apache
System: Linux webd001.cluster127.gra.hosting.ovh.net 6.18.42-ovh-vps-grsec-zfs+ #1 SMP PREEMPT_DYNAMIC Wed Aug 5 15:59:48 CEST 2026 x86_64
User: mciraet (192513)
PHP: 8.2.31
Disabled: _dyuweyrj4,_dyuweyrj4r,dl
Upload Files
File: /home/mciraet/www/wp-content/themes/movedo/includes/admin/grve-remote-messages.php
<?php
/**
 * Reusable GRVE Remote Message Handler
 */

class Movedo_Grve_Remote_Messages {

	private $slug;
	private $remote_urls;
	private $transient_key;
	private $meta_key;

	public function __construct( $theme_slug ) {
		$this->slug = sanitize_key( $theme_slug );
		$this->transient_key = "movedo_grve_messages_{$this->slug}";
		$this->meta_key      = "movedo_grve_dismissed_messages_{$this->slug}";

		$this->remote_urls = [
			"https://greativesweb.github.io/repository/messages/{$this->slug}.json",
			'https://greativesweb.github.io/repository/messages/global.json'
		];

		add_action( 'admin_notices', [ $this, 'show_messages' ] );
		add_action( 'wp_ajax_movedo_grve_dismiss_message', [ $this, 'ajax_dismiss_message' ] );
		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
	}

	public function fetch_messages() {
		$force_check = ! empty( $_GET['force-check'] );
		$cached = get_transient( $this->transient_key );
		if ( $cached && ! $force_check ) {
			return $cached;
		}


		$all_messages = [];

		foreach ( $this->remote_urls as $url ) {
			$response = wp_remote_get( $url );

			if ( is_wp_error( $response ) || $response['response']['code'] !== 200 ) {
				continue;
			}

			$body = $response['body'];
			$data = json_decode( $body, true );

			if ( json_last_error() === JSON_ERROR_NONE && isset( $data['messages'] ) && is_array( $data['messages'] ) ) {
				$all_messages = array_merge( $all_messages, $data['messages'] );
			}
		}

		set_transient( $this->transient_key, $all_messages, HOUR_IN_SECONDS );
		return $all_messages;
	}

	public function show_messages() {
		if ( ! current_user_can( 'manage_options' ) ) return;

		$messages  = $this->fetch_messages();
		$dismissed = get_user_meta( get_current_user_id(), $this->meta_key, true );
		$dismissed = is_array( $dismissed ) ? $dismissed : [];
		$today = date( 'Y-m-d' );

		foreach ( $messages as $msg ) {
			if (
				! isset( $msg['id'], $msg['message'], $msg['type'] ) ||
				in_array( $msg['id'], $dismissed, true ) ||
				( isset( $msg['expires'] ) && $msg['expires'] < $today )
			) {
				continue;
			}

			$type    = esc_attr( $msg['type'] );
			$id      = esc_attr( $msg['id'] );
			$title   = isset( $msg['title'] ) ? '<strong>' . esc_html( $msg['title'] ) . ':</strong> ' : '';
			$message = esc_html( $msg['message'] );
			$link    = isset( $msg['link'] ) ? ' <a href="' . esc_url( $msg['link'] ) . '" target="_blank">Read more</a>' : '';

			echo '<div class="notice notice-' . $type . ' is-dismissible grve-remote-message" data-message-id="' . $id . '">';
			echo '<p>' . $title . $message . $link . '</p>';
			echo '</div>';
		}
	}

	public function enqueue_scripts() {
		wp_enqueue_script(
			'movedo-grve-dismiss-message',
			get_template_directory_uri() . '/includes/js/grve-dismiss-message.js',
			[ 'jquery' ],
			null,
			true
		);

		wp_localize_script( 'movedo-grve-dismiss-message', 'movedo_grve_remote_message', [
			'ajaxurl' => admin_url( 'admin-ajax.php' ),
			'nonce'   => wp_create_nonce( 'movedo_grve_dismiss_message' ),
		] );
	}

	public function ajax_dismiss_message() {
		check_ajax_referer( 'movedo_grve_dismiss_message', 'nonce' );

		$message_id = sanitize_text_field( $_POST['message_id'] ?? '' );
		if ( ! $message_id ) {
			wp_send_json_error();
		}

		$user_id   = get_current_user_id();
		$dismissed = get_user_meta( $user_id, $this->meta_key, true );
		$dismissed = is_array( $dismissed ) ? $dismissed : [];

		if ( ! in_array( $message_id, $dismissed, true ) ) {
			$dismissed[] = $message_id;
			update_user_meta( $user_id, $this->meta_key, $dismissed );
		}

		wp_send_json_success();
	}
}

function movedo_grve_init_remote_messages() {
	if ( is_admin() && defined( 'MOVEDO_GRVE_THEME_SLUG' ) ) {
		new Movedo_Grve_Remote_Messages( MOVEDO_GRVE_THEME_SLUG );
	}
}
add_action( 'init', 'movedo_grve_init_remote_messages' );


//Omit closing PHP tag to avoid accidental whitespace output errors.