Server : Apache System : Linux server.lienzindia.com 4.18.0-348.7.1.el8_5.x86_64 #1 SMP Wed Dec 22 13:25:12 UTC 2021 x86_64 User : plutus ( 1007) PHP Version : 7.4.33 Disable Function : NONE Directory : /home/plutus/public_html/wp-content/themes/vrm/inc/ |
Upload File : |
<?php declare(strict_types=1); namespace TotalTheme; use WPEX_Card; use TotalTheme\Title; use TotalTheme\Page\Header as Page_Header; \defined( 'ABSPATH' ) || exit; /** * Replace_Vars Class. */ final class Replace_Vars { /** * Check if functionality is enabled. * * @return bool $check */ protected function is_enabled(): bool { return (bool) \apply_filters( 'totaltheme/replace_vars/is_enabled', true ); } /** * Replace `{{variable_placeholders}}` with their correct value. * * @param string $text The string to replace the variables in. * @param array $args Arguments to pass to the replacement. * * @return mixed $replaced_value */ public function replace( string $text, array $args = [] ): string { if ( ! \is_string( $text ) || ! $this->is_enabled() || ! \str_contains( $text, '{{' ) || ! \str_contains( $text, '}}' ) ) { return $text; } return \preg_replace_callback( "/\{\{([^}]*)}\}/", [ $this, 'handle_replacements' ], $text ); } /** * Returns the array of variables and their values. * * @return array */ private function get_vars(): array { $vars = [ 'current_url' => '', 'post_id' => '', 'post_rating' => '', 'post_author' => '', 'post_date' => '', 'post_modified' => '', 'post_title' => '', 'post_slug' => '', 'post_subheading' => '', 'post_excerpt' => '', 'post_content' => '', 'title' => '', 'taxonomy' => '', 'term_id' => '', 'term_name' => '', 'permalink' => '', 'category' => '', 'primary_term_id' => '', 'paged' => '', 'post_count' => '', 'card_icon' => '', 'card_running_count' => '', // @todo ? // 'author_first_name' => '', // 'author_last_name' => '', // 'post_year' => '', // 'post_month' => '', // 'post_day' => '', // 'currentdate' => '', // 'currentyear' => '', // 'currentmonth' => '', // 'currentday' => '', // 'max_num_pages' => '', ]; /** * Filters the available vars. * * @param array $vars List of vars. */ $vars = (array) \apply_filters( 'totaltheme/replace_vars/vars', $vars ); return $vars; } /** * Handles the variable replacement. * * @param array $matches The matches returned by preg_replace_callback * @return mixed */ private function handle_replacements( $matches ) { $vars = $this->get_vars(); $replacement = $matches[0]; $has_args = $this->var_has_args( $matches[1] ?? '' ); if ( \array_key_exists( $matches[1], $vars ) || $has_args ) { if ( ! $has_args && ! empty( $vars[$matches[1]] ) ) { $replacement = \is_callable( $vars[$matches[1]] ) ? \call_user_func( $vars[$matches[1]] ) : $vars[$matches[1]]; } else { $method_suffix = $has_args ? \strtok( $matches[1], '_' ) : $matches[1]; $method = 'get_' . $method_suffix; if ( \method_exists( $this, $method ) ) { if ( $has_args ) { $replacement = $this->$method( $matches[1] ); } else { $replacement = $this->$method(); } } } } if ( \is_null( $replacement ) || false === $replacement ) { $replacement = ''; } elseif ( ! \is_scalar( $replacement ) ) { $replacement = $matches[0]; } return $replacement; } /** * Checks if the current variable has args. * * @return bool */ private function var_has_args( string $var ): bool { return ( \str_starts_with( $var, 'acf_' ) || \str_starts_with( $var, 'cf_' ) ); } /** * Get the current URL. * * @return string */ private function get_current_url(): string { return (string) \wpex_get_current_url(); } /** * Get the title var value. * * @return string */ private function get_title(): string { if ( ( \in_the_loop() || \get_query_var( 'wpex_card_object' ) ) ) { return (string) $this->get_post_title(); } else { return Title::instance()->get(); } } /** * Get the post_title var value. * * @return string */ private function get_post_title(): string { return (string) \get_the_title(); } /** * Get the post id. * * @return int */ private function get_post_id(): int { return (int) \get_the_ID(); } /** * Get Post Rating. * * @return string */ private function get_post_rating(): string { $rating = \get_post_meta( \get_the_ID(), 'wpex_post_rating', true ); if ( ! $rating && \function_exists( 'wc_get_product' ) && 'product' === \get_post_type() ) { $product = \wc_get_product( get_the_ID() ); if ( $product && \is_callable( [ $product, 'get_average_rating' ] ) ) { $rating = $product->get_average_rating(); } } return (string) $rating; } /** * Get the category name. * * @return string */ private function get_category(): string { return (string) \wpex_get_first_term_name(); } /** * Get the primary term id. * * @return int */ private function get_primary_term_id(): ?int { return \wpex_get_first_term_id(); } /** * Get the post date. * * @return string */ private function get_post_date(): string { return (string) \get_the_date(); } /** * Get the post modified date. * * @return string */ private function get_post_modified(): string { return (string) \get_the_modified_date(); } /** * Get post slug. * * @return string */ private function get_post_slug(): string { return (string) \get_post_field( 'post_name', \get_post() ); } /** * Get the post permalink. * * @return string */ private function get_permalink(): string { return (string) \get_permalink(); } /** * Get the post author. * * @return string */ private function get_post_author(): string { return (string) \get_the_author(); } /** * Get the post subheading. * * @return string */ private function get_post_subheading(): string { return Page_Header::get_subheading(); } /** * Get the post content. * * @return string */ private function get_post_content(): string { $content = \get_the_content(); if ( ! \str_contains( $content, '{{post_content}}' ) ) { return \wpex_the_content( $content ); } else { return ''; } } /** * Get the post excerpt. * * @return string */ private function get_post_excerpt(): string { return (string) \get_the_excerpt() ?: ''; } /** * Get current taxonomy name. * * @return string|null */ private function get_taxonomy(): ?string { $obj = \get_queried_object(); if ( \is_a( $obj, 'WP_Term' ) ) { $tax = \get_taxonomy( $obj->taxonomy ); if ( \is_a( $tax, 'WP_Taxonomy' ) ) { return (string) $tax->labels->singular_name; } } return ''; } /** * Get current taxonomy term id * * @return int|null */ private function get_term_id(): ?int { return get_queried_object()->term_id ?? null; } /** * Get current taxonomy term name. * * @return string */ private function get_term_name(): string { $obj = get_queried_object(); return ( \is_a( $obj, 'WP_Term' ) && isset( $obj->name ) ) ? (string) $obj->name : ''; } /** * Get post count. * * @return int */ private function get_post_count(): ?int { global $wp_query; return ! empty( $wp_query->found_posts ) ? (int) $wp_query->found_posts : null; } /** * Get paged text. * * @return string */ private function get_paged(): string { if ( \is_paged() ) { $paged = \get_query_var( 'paged' ) ?: \get_query_var( 'page' ) ?: 1; if ( $paged > 1 ) { global $wp_query; $max_num_pages = $wp_query->max_num_pages ?? 0; if ( $max_num_pages ) { return \sprintf( \esc_html__( 'Page %s of %s' ), $paged, $max_num_pages ); } else { return \sprintf( \esc_html__( 'Page %s' ), $paged ); } } } return ''; } /** * Get card Icon * * @return string */ private function get_card_icon(): string { return ( $card_instance = WPEX_Card::instance() ) ? (string) $card_instance->get_the_icon() : ''; } /** * Get card running count. * * @return int */ private function get_card_running_count(): int { return ( $card_instance = WPEX_Card::instance() ) ? (int) $card_instance->get_var( 'running_count' ) : 1; } /** * Get custom field value. * * @return mixed */ private function get_cf( string $var ) { $key = \str_replace( 'cf_', '', $var ); if ( $key ) { if ( \function_exists( 'get_field' ) ) { return \get_field( $key ); } return \get_post_meta( \get_the_ID(), $key, true ); } } /** * Get acf field value. * * @param string $var * @return mixed */ private function get_acf( string $var ) { $key = \str_replace( 'acf_', '', $var ); if ( $key ) { if ( ! \function_exists( 'get_field' ) ) { return "{{{$var}}}"; } return \get_field( $key ) ?: ''; } } }