* @link https://www.fireplugins.com * @copyright Copyright © 2021 FirePlugins All Rights Reserved * @license GNU GPLv3 or later */ namespace FireBox\Core; if (!defined('ABSPATH')) { exit; // Exit if accessed directly. } use \FPFramework\Libs\Registry; class Previewer { /** * FireBox data. * * @var array */ public $box_data; /** * Inits Previewer * * @return void */ public function init() { if (!$this->is_preview_page()) { return false; } $this->hooks(); return true; } /** * Check if current page request meets requirements for firebox preview page. * * @return boolean */ public function is_preview_page() { // Only proceed if we have a item to preview if (empty($_GET['post_type'])) { return false; } if ($_GET['post_type'] != 'firebox') { return false; } if (empty($_GET['p'])) { return false; } if (empty($_GET['preview'])) { return false; } if (!$_GET['preview']) { return false; } // Check for logged in user. if (!\is_user_logged_in()) { return false; } if (!\current_user_can('manage_options')) { return false; } $box_id = \absint($_GET['p']); // phpcs:ignore WordPress.Security.NonceVerification.Recommended if (!$this->box_data = firebox()->box->get($box_id)) { return false; } return true; } /** * Hooks * * @return void */ public function hooks() { firebox()->box->render($this->box_data); \add_filter('the_title', [$this, 'the_title'], 100, 1); \add_filter('the_content', [$this, 'the_content'], 999); \add_filter('get_the_excerpt', [$this, 'the_content'], 999); } public function the_content() { if (!isset($this->box_data->ID)) { return ''; } if (!current_user_can('manage_options')) { return ''; } $links = []; $links[] = [ 'url' => esc_url( add_query_arg( [ 'post' => absint($this->box_data->ID), 'action' => 'edit', ], admin_url('post.php') ) ), 'text' => esc_html(firebox()->_('FB_EDIT_FIREBOX')), ]; $links[] = [ 'url' => esc_url( add_query_arg( [ 'post_type' => 'firebox', ], admin_url('edit.php') ) ), 'text' => esc_html(firebox()->_('FB_VIEW_FIREBOX_ITEMS')), ]; $content = '
'; $content .= '

'; $content .= esc_html(firebox()->_('FB_FIREBOX_PREVIEW_DESC')); if (!empty($links)) { $content .= '
'; foreach ($links as $key => $link) { $content .= '' . $link['text'] . ''; $l = array_keys($links); if (end($l) !== $key) { $content .= ' | '; } } } $content .= '

'; $content .= '
'; return $content; } /** * Customize firebox popup preview page title. * * @param string $title * * @return string */ public function the_title($title) { if (in_the_loop()) { $title = sprintf( esc_html('%s'), ! empty($this->box_data->post_title) ? sanitize_text_field($this->box_data->post_title) . ' ' . firebox()->_('FB_PREVIEW') : esc_html(firebox()->_('FB_FIREBOX_POPUP_PREVIEW')) ); } return $title; } }