getProfile(); // Entity $this->entity = null; // Settings $this->settings = Config::general(); } public function getProfile(): PluginProfile { return Profile::getInstance(); } public function tag(): string { return static::SHORTCODE; } protected function registerShortcode(): void { $tag = $this->tag(); if ($tag === '') return; $this->shortcode($tag); } /** * @param string $tag * @return void */ protected function shortcode(string $tag): void { add_shortcode($tag, function($atts) use ($tag) { $atts = $this->shortcodeAttributes($atts, $tag); Assets::enqueueFrontend([in_array($tag, ['daytidy-booking', 'daytidy-seg-booking'], true) ? 'booking' : 'single-offering']); // Generate and Return Output return Kses::page('
' . $this->entity($atts)->output($atts) . '
'); }); } /** * @return mixed[] */ protected function defaultShortcodeAttributes(): array { return [ 'id' => 0, ]; } /** * @param mixed $atts * @return mixed[] */ protected function shortcodeAttributes($atts, string $tag): array { if (!is_array($atts)) $atts = []; $atts = shortcode_atts($this->defaultShortcodeAttributes(), $atts, $tag); $atts['id'] = absint($atts['id'] ?? 0); return (array) apply_filters( 'DayTidy/F__'.str_replace('\\', '/', get_class($this)).'__shortcodeAttributes/atts', $atts, $tag, $this ); } /** * @param mixed[] $args * @return SegmentInterface */ public function entity(array $args): SegmentInterface { // Get Entity ID from Arguments $post_id = Arr::ternary($args, 'id', 0); // Get Entity ID from Single Offering target context if (!$post_id && isset($_GET[PageResolver::QUERY_OFFERING])) { $post_id = $this->targetOfferingId(); if (!$post_id) return $this; } // Get Entity ID from WP Query if (!$post_id) { $post = get_post(); $post_id = $post instanceof WP_Post ? $post->ID : 0; } // Get Entity ID from DayTidy Request if (!$post_id) { // Booking Request $request = Request::get(); $post_id = $request && isset($request->offering) ? $request->offering : 0; } // Set Entity $this->entity = $this->getEntityById((int) $post_id); return $this; } private function targetOfferingId(): int { $post_id = absint(wp_unslash($_GET[PageResolver::QUERY_OFFERING])); if ($post_id <= 0) return 0; if (get_post_type($post_id) !== OfferingPost::PT || get_post_status($post_id) !== 'publish') return 0; return $post_id; } /** * Render output of Segment * * @param string $view * @param mixed[] $args * @return string */ public function render(string $view, array $args = []): string { // Append Segment & Entity $args = array_merge($args, ['segment' => $this, 'entity' => $this->entity]); // Generate & Filter Output return apply_filters( 'DayTidy/F__'.str_replace('\\', '/', get_class($this)).'__render/output', self::view()->render($view, $args) ); } abstract public function output(array $args = []): string; abstract public function getEntityById(int $id): ?PostInterface; }