*/ namespace RankMath\Admin; use RankMath\CMB2; use RankMath\Helper; use RankMath\Rewrite; use MyThemeShop\Helpers\Param; use MyThemeShop\Helpers\WordPress; use MyThemeShop\Helpers\Conditional; defined( 'ABSPATH' ) || exit; /** * Serp_Preview class. */ class Serp_Preview { /** * Display SERP preview. */ public function display() { $method = 'get_' . CMB2::current_object_type() . '_data'; $data = $this->$method(); if ( 'post' === CMB2::current_object_type() && Helper::is_module_active( 'rich-snippet' ) ) { $snippet_preview = $this->get_snippet_html(); } $snippet_type = isset( $snippet_preview['type'] ) ? $snippet_preview['type'] : ''; $desktop_preview = isset( $snippet_preview['desktop'] ) ? $snippet_preview['desktop'] : ''; $mobile_preview = isset( $snippet_preview['mobile'] ) ? $snippet_preview['mobile'] : ''; ?>
ID, null, null ); $permalink = $permalink_format; if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) { $permalink = get_permalink( $post ); } elseif ( 'auto-draft' === $post->post_status && 'post' === $post->post_type ) { $post_temp = $post; $post_temp->post_status = 'publish'; $permalink = get_permalink( $post_temp, true ); $permalink_format = $permalink; } else { $permalink = str_replace( [ '%pagename%', '%postname%' ], ( $post->post_name ? $post->post_name : sanitize_title( $post->post_title ) ), $permalink_format ); } $url = untrailingslashit( esc_url( $permalink ) ); return compact( 'title_format', 'desc_format', 'url', 'permalink', 'permalink_format' ); } /** * Get term data for SERP preview. * * @return array */ private function get_term_data() { global $taxnow, $wp_rewrite; $term_id = Param::request( 'tag_ID', 0, FILTER_VALIDATE_INT ); $term = get_term( $term_id, $taxnow, OBJECT, 'edit' ); $taxonomy = get_taxonomy( $term->taxonomy ); $title_format = Helper::get_settings( "titles.tax_{$term->taxonomy}_title" ); $desc_format = Helper::get_settings( "titles.tax_{$term->taxonomy}_description" ); $title_format = $title_format ? $title_format : '%term%'; // Get the permalink. $permalink = untrailingslashit( esc_url( get_term_link( $term_id, $term->taxonomy ) ) ); $termlink = $wp_rewrite->get_extra_permastruct( $term->taxonomy ); // Pretty permalinks disabled. if ( empty( $termlink ) ) { $permalink_format = $permalink; } else { $slugs = $this->get_ancestors( $term_id, $term->taxonomy ); $termlink = $this->get_termlink( $termlink, $term->taxonomy ); $slugs[] = '%postname%'; $termlink = str_replace( "%$term->taxonomy%", implode( '/', $slugs ), $termlink ); $permalink_format = home_url( user_trailingslashit( $termlink, 'category' ) ); } $url = untrailingslashit( esc_url( $permalink ) ); return compact( 'title_format', 'desc_format', 'url', 'permalink', 'permalink_format' ); } /** * Filter term link. * * @param string $termlink Term Link. * @param string $taxonomy Taxonomy name. * * @return string */ private function get_termlink( $termlink, $taxonomy ) { if ( 'category' === $taxonomy && Helper::get_settings( 'general.strip_category_base' ) ) { $termlink = str_replace( '/category/', '', $termlink ); } if ( Conditional::is_woocommerce_active() && 'product_cat' === $taxonomy && Helper::get_settings( 'general.wc_remove_category_base' ) ) { $termlink = str_replace( 'product-category', '', $termlink ); } return $termlink; } /** * Whether to add ancestors in taxonomy page. * * @param int $term_id Term ID. * @param string $taxonomy Taxonomy name. * * @return array */ private function get_ancestors( $term_id, $taxonomy ) { $slugs = []; if ( Conditional::is_woocommerce_active() && 'product_cat' === $taxonomy && Helper::get_settings( 'general.wc_remove_category_parent_slugs' ) ) { return $slugs; } $ancestors = get_ancestors( $term_id, $taxonomy, 'taxonomy' ); foreach ( (array) $ancestors as $ancestor ) { $ancestor_term = get_term( $ancestor, $taxonomy ); $slugs[] = $ancestor_term->slug; } return array_reverse( $slugs ); } /** * Get user data for SERP preview. * * @return array */ private function get_user_data() { global $user_id, $wp_rewrite; $title_format = Helper::get_settings( 'titles.author_archive_title' ); $desc_format = Helper::get_settings( 'titles.author_archive_description' ); $title_format = $title_format ? $title_format : '%author%'; Rewrite::change_author_base(); $permalink = untrailingslashit( esc_url( get_author_posts_url( $user_id ) ) ); $link = $wp_rewrite->get_author_permastruct(); $permalink_format = empty( $link ) ? $permalink : $this->get_author_permalink( $link ); $url = untrailingslashit( esc_url( $permalink ) ); return compact( 'title_format', 'desc_format', 'url', 'permalink', 'permalink_format' ); } /** * Get user permalink * * @param string $link Permalink structure. * @return string */ private function get_author_permalink( $link ) { $link = str_replace( '%author%', '%postname%', $link ); return home_url( user_trailingslashit( $link ) ); } /** * Get Snippet HTML for SERP preview. */ private function get_snippet_html() { $snippet_data = $this->get_snippet_data(); if ( ! $snippet_data || ! isset( $snippet_data['data'] ) ) { return false; } $data = $snippet_data['data']; $rating = isset( $data['rating'] ) ? $data['rating'] : ''; $rating_count = isset( $data['rating_count'] ) ? $data['rating_count'] : ''; unset( $data['rating'] ); unset( $data['rating_count'] ); if ( isset( $data['price'] ) && isset( $data['currency'] ) ) { $data['price'] = $data['currency'] . $data['price']; unset( $data['currency'] ); } $html = [ 'type' => $snippet_data['type'], 'desktop' => $this->get_desktop_preview( $data, $rating, $rating_count ), 'mobile' => $this->get_mobile_preview( $data, $rating, $rating_count ), ]; return $html; } /** * Get desktop preview. * * @param array $data Snippet data array. * @param int $rating Ratings. * @param int $rating_count Rating count. * @return string */ private function get_desktop_preview( $data, $rating, $rating_count ) { $preview = ''; $labels = [ 'price_range' => esc_html__( 'Price range: ', 'rank-math' ), 'calories' => esc_html__( 'Calories: ', 'rank-math' ), 'in_stock' => esc_html__( 'In stock', 'rank-math' ), ]; if ( $rating ) { $preview .= $this->get_ratings( $rating ); /* translators: total reviews */ $preview .= ''; if ( $rating_count ) { /* translators: total reviews */ $preview .= ' - ' . sprintf( esc_html__( '%s reviews', 'rank-math' ), esc_html( $rating_count ) ) . ''; } } foreach ( $data as $key => $value ) { if ( ! $value ) { continue; } if ( ! in_array( $key, [ 'event_date', 'event_place', 'event_name' ], true ) ) { $preview .= ' - '; } $preview .= ''; if ( isset( $labels[ $key ] ) ) { $preview .= $labels[ $key ]; } if ( 'in_stock' !== $key ) { $preview .= $value; } $preview .= ''; } return '