*/ final class NoPub { /** * The unique identifier of this plugin. * * @since 2.0.0 * @access protected * @var string $plugin_name The string used to uniquely identify this plugin. */ protected $plugin_name; /** * View manager * * @since 3.2.0 * @access private * @var View $views View manager instance */ private $views; /** * NoPub constructor. * * @since 2.0.0 * @param string $plugin_name The name of this plugin. */ public function __construct( $plugin_name ) { $this->plugin_name = $plugin_name; $this->views = new View(); } /** * Register meta box hooks * * @since 3.2.0 * @return void */ public function register_meta_box() { add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) ); add_action( 'save_post', array( $this, 'save_meta_box' ) ); } /** * Get page type * * @return string * @since 2.0.0 */ public function get_page_type() { $page_type = ''; if ( is_page() ) { $page_type = 'page'; } elseif ( is_single() ) { $page_type = 'post'; } elseif ( is_category() ) { $page_type = 'category'; } elseif ( is_tag() ) { $page_type = 'post_tag'; } return $page_type; } /** * Add meta box for Actirise Ad Control * * @since 3.2.0 * @return void */ public function add_meta_box() { if ( ! Helpers::is_user_authenticated() ) { return; } $post_types = get_post_types( array( 'public' => true ) ); if ( isset( $post_types['attachment'] ) ) { unset( $post_types['attachment'] ); } foreach ( $post_types as $post_type ) { add_meta_box( 'actirise-ad-control', __( 'Actirise', 'actirise' ), array( $this, 'render_meta_box' ), $post_type, 'side', 'low' ); } } /** * Render the meta box content using template * * @since 3.2.0 * @param \WP_Post $post The post object. * @return void */ public function render_meta_box( $post ) { $ads_enabled = $this->ads_enabled( $post->ID ); $entry = $this->get_nopub_entry( $post->ID ); $start_date = ( $entry && isset( $entry->startDate ) && is_string( $entry->startDate ) && $entry->startDate ) ? $entry->startDate : ''; $end_date = ( $entry && isset( $entry->endDate ) && is_string( $entry->endDate ) && $entry->endDate ) ? $entry->endDate : ''; // Datetime-local inputs are edited in the site timezone; convert the stored UTC // window to site-local wall-clock for pre-filling, and build the display label. $start_local = '' !== $start_date ? get_date_from_gmt( str_replace( array( 'T', 'Z' ), array( ' ', '' ), $start_date ), 'Y-m-d\TH:i' ) : ''; $end_local = '' !== $end_date ? get_date_from_gmt( str_replace( array( 'T', 'Z' ), array( ' ', '' ), $end_date ), 'Y-m-d\TH:i' ) : ''; $status_label = $entry ? Helpers::format_window( $entry, __( 'unlimited', 'actirise' ) ) : ''; $now_local = get_date_from_gmt( gmdate( 'Y-m-d H:i:s' ), 'Y-m-d\TH:i' ); $this->views->render( 'meta-box-ad-control', array( 'post_id' => $post->ID, 'ads_enabled' => $ads_enabled, 'start_local' => $start_local, 'end_local' => $end_local, 'status_label' => $status_label, 'now_local' => $now_local, ) ); } /** * Save meta box data * * @since 3.2.0 * @param int $post_id The post ID. * @return void */ public function save_meta_box( $post_id ) { if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } if ( ! isset( $_POST['actirise_ad_control_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['actirise_ad_control_nonce'] ) ), 'actirise_ad_control_nonce' ) ) { return; } $ads_enabled_flag = isset( $_POST['actirise_ads_enabled'] ) ? sanitize_text_field( wp_unslash( $_POST['actirise_ads_enabled'] ) ) : '1'; $start = Helpers::wp_local_to_utc( isset( $_POST['actirise_nopub_start'] ) ? sanitize_text_field( wp_unslash( $_POST['actirise_nopub_start'] ) ) : '' ); $end = Helpers::wp_local_to_utc( isset( $_POST['actirise_nopub_end'] ) ? sanitize_text_field( wp_unslash( $_POST['actirise_nopub_end'] ) ) : '' ); // Treat invalid input as "not provided". if ( false === $start ) { $start = null; } if ( false === $end ) { $end = null; } // Drop an end that is not strictly after the start. if ( null !== $start && null !== $end && strtotime( $end ) <= strtotime( $start ) ) { $end = null; } // Plain-toggle / no-JS fallback: excluding with no explicit start = exclude now. if ( '0' === $ads_enabled_flag && null === $start ) { $start = gmdate( 'Y-m-d\TH:i:s\Z' ); } $post = get_post( $post_id ); if ( ! $post ) { return; } if ( null !== $start ) { $this->add_to_nopub( $post, $start, $end ); } else { $this->remove_from_nopub( $post_id ); } } /** * Add or update a post in the nopub list (upsert by id). * * @since 3.2.0 * @param \WP_Post $post The post object. * @param string $start_date Canonical UTC start (Y-m-d\TH:i:s\Z). * @param string|null $end_date Canonical UTC end, or null for unlimited. * @return void */ private function add_to_nopub( $post, $start_date, $end_date ) { $no_pub = Options::get( 'nopub', array() ); if ( ! is_array( $no_pub ) ) { $no_pub = array(); } $thumbnail_url = get_the_post_thumbnail_url( $post->ID, 'thumbnail' ); $entry = (object) array( 'id' => $post->ID, 'title' => $post->post_title, 'url' => get_permalink( $post->ID ), 'type' => $post->post_type === 'page' ? 'page' : 'post', 'summary' => substr( $post->post_content, 0, 100 ), 'publishedDate' => $post->post_date, 'createdAt' => current_time( 'mysql' ), 'image' => $thumbnail_url ? $thumbnail_url : '', 'startDate' => $start_date, 'endDate' => $end_date, ); $found = false; foreach ( $no_pub as $key => $item ) { if ( is_object( $item ) && property_exists( $item, 'id' ) && is_numeric( $item->id ) && (int) $item->id === (int) $post->ID ) { if ( property_exists( $item, 'createdAt' ) && $item->createdAt ) { $entry->createdAt = $item->createdAt; } $no_pub[ $key ] = $entry; $found = true; break; } } if ( ! $found ) { $no_pub[] = $entry; } Options::update( 'nopub', array_values( $no_pub ) ); } /** * Remove a post from the nopub list * * @since 3.2.0 * @param int $post_id The post ID. * @return void */ private function remove_from_nopub( $post_id ) { $no_pub = Options::get( 'nopub', array() ); if ( ! is_array( $no_pub ) ) { return; } $no_pub = array_filter( $no_pub, function ( $item ) use ( $post_id ) { return ! ( is_object( $item ) && property_exists( $item, 'id' ) && is_numeric( $item->id ) && (int) $item->id === $post_id ); } ); $no_pub = array_values( $no_pub ); Options::update( 'nopub', $no_pub ); } /** * Get the nopub entry for a post id, or null. * * @since 4.3.0 * @param int $post_id The post ID. * @return object|null */ private function get_nopub_entry( $post_id ) { $no_pub = Options::get( 'nopub', array() ); if ( ! is_array( $no_pub ) ) { return null; } foreach ( $no_pub as $item ) { if ( is_object( $item ) && property_exists( $item, 'id' ) && is_numeric( $item->id ) && (int) $item->id === (int) $post_id ) { return $item; } } return null; } /** * Remove entries whose endDate has passed. Writes only when the list changed. * * @since 4.3.0 * @return array The remaining (active + scheduled) entries. */ public function prune_expired() { $no_pub = Options::get( 'nopub', array() ); if ( ! is_array( $no_pub ) ) { return array(); } /** @var array<\stdClass> $no_pub */ $now = time(); $kept = array(); foreach ( $no_pub as $item ) { if ( is_object( $item ) && isset( $item->endDate ) && is_string( $item->endDate ) && $item->endDate ) { $end = strtotime( $item->endDate ); if ( false !== $end && $now >= $end ) { continue; } } $kept[] = $item; } if ( count( $kept ) !== count( $no_pub ) ) { Options::update( 'nopub', $kept ); } return $kept; } /** * Build the exclusion list for the admin client: prunes expired entries, then adds a * `windowLabel` (site-timezone, site date/time format) to each entry for display. * The label is a view-only field and is never persisted. * * @since 4.3.0 * @return array */ public function get_list_for_client() { $unlimited = __( 'unlimited', 'actirise' ); $list = array(); foreach ( $this->prune_expired() as $item ) { if ( ! is_object( $item ) ) { continue; } $list[] = (object) array_merge( (array) $item, array( 'windowLabel' => Helpers::format_window( $item, $unlimited ) ) ); } return $list; } /** * Check if ads are enabled for a post or current page * * @since 3.2.0 * @param int|null $post_id The post ID. If null, checks current page context. * @return bool True if ads are enabled, false otherwise. */ public function ads_enabled( $post_id = null ) { /** @var mixed $no_pub */ $no_pub = Options::get( 'nopub', array() ); if ( ! is_array( $no_pub ) || count( $no_pub ) === 0 ) { return true; } if ( $post_id !== null ) { foreach ( $no_pub as $item ) { if ( is_object( $item ) && property_exists( $item, 'id' ) && is_numeric( $item->id ) && (int) $item->id === $post_id ) { if ( Helpers::exclusion_in_effect( $item ) ) { return false; } } } return true; } $page_type = $this->get_page_type(); if ( $page_type === '' ) { return true; } $no_pub = array_filter( $no_pub, function ( $item ) use ( $page_type ) { return is_object( $item ) && isset( $item->type ) && $item->type === $page_type; } ); $queried_object_id = get_queried_object_id(); $no_pub = array_filter( $no_pub, function ( $item ) use ( $queried_object_id ) { return isset( $item->id ) && $item->id === $queried_object_id && Helpers::exclusion_in_effect( $item ); } ); return count( $no_pub ) === 0; } }