|null Camps pendents de la proposta en previsualització. */ private $fields = null; /** @var int ID del post que s'està previsualitzant. */ private $post_id = 0; public function register() { add_action('wp', array($this, 'maybe_activate')); } /** * Activa la previsualització si la petició és vàlida (nonce + permisos). */ public function maybe_activate() { if (empty($_GET['abency_preview']) || ! is_singular()) { return; } $local_id = (int) $_GET['abency_preview']; $nonce = isset($_GET['_wpnonce']) ? sanitize_text_field(wp_unslash($_GET['_wpnonce'])) : ''; if (! wp_verify_nonce($nonce, 'abency_preview_' . $local_id)) { return; } $proposal = Abency_Proposals::get($local_id); if (! $proposal) { return; } $this->post_id = (int) $proposal['wp_post_id']; if (! current_user_can('edit_post', $this->post_id)) { return; } if (get_queried_object_id() !== $this->post_id) { return; } $this->fields = $proposal['fields']; add_filter('the_title', array($this, 'filter_title'), 20, 2); add_filter('the_content', array($this, 'filter_content'), 20); add_filter('get_the_excerpt', array($this, 'filter_excerpt'), 20); add_action('wp_footer', array($this, 'preview_banner')); } public function filter_title($title, $id = 0) { if ($id === $this->post_id && in_the_loop() && isset($this->fields['title'])) { return esc_html($this->fields['title']); } return $title; } public function filter_content($content) { if (get_the_ID() === $this->post_id && in_the_loop() && isset($this->fields['content'])) { return wp_kses_post($this->fields['content']); } return $content; } public function filter_excerpt($excerpt) { if (get_the_ID() === $this->post_id && isset($this->fields['excerpt'])) { return esc_html($this->fields['excerpt']); } return $excerpt; } public function preview_banner() { echo '
' . esc_html__('Preview of a change proposed by Abency. This content is NOT published.', 'abency-connector') . '
'; } }