*/ 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'] : ''; ?>
  • All
  • Images
  • Videos
  • News
  • Maps
  • More
About 43,70,000 results (0.32 seconds) 

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 .= '' . sprintf( esc_html__( 'Rating: %s', 'rank-math' ), esc_html( $rating ) ) . ''; 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 '
' . $preview . '
'; } /** * Get mobile preview. * * @param array $data Snippet data array. * @param int $rating Ratings. * @param int $rating_count Rating count. * @return string */ private function get_mobile_preview( $data, $rating, $rating_count ) { $labels = [ 'price' => esc_html__( 'Price', 'rank-math' ), 'price_range' => esc_html__( 'Price range', 'rank-math' ), 'time' => esc_html__( 'Cooking time', 'rank-math' ), 'calories' => esc_html__( 'Calories', 'rank-math' ), 'in_stock' => esc_html__( 'In Stock', 'rank-math' ), 'event_date' => esc_html__( 'Date', 'rank-math' ), 'event_place' => esc_html__( 'Location', 'rank-math' ), ]; $preview = ''; if ( $rating ) { $preview .= ''; $preview .= ''; $preview .= esc_html__( 'Rating', 'rank-math' ); $preview .= ''; $preview .= '' . esc_html( $rating ) . ''; $preview .= $this->get_ratings( $rating ); if ( $rating_count ) { $preview .= '(' . esc_html( $rating_count ) . ')'; } $preview .= ''; } foreach ( $data as $key => $value ) { if ( ! $value || 'event_name' === $key ) { continue; } $preview .= ''; if ( isset( $labels[ $key ] ) ) { $preview .= ''; $preview .= $labels[ $key ]; $preview .= ''; } if ( 'in_stock' !== $key ) { $preview .= '' . esc_html( $value ) . ''; } $preview .= ''; } return '
' . $preview . '
'; } /** * Get Star Ratings. * * @param int $rating Rating count. */ private function get_ratings( $rating ) { $html = ''; $rating = $rating * 20; for ( $i = 1; $i <= 5; $i++ ) { $html .= ''; } $html .= '
'; for ( $i = 1; $i <= 5; $i++ ) { $html .= ''; } return '
' . $html . '
'; } /** * Get Snippet Data for SERP preview. * * @return array */ private function get_snippet_data() { global $post; setup_postdata( $post ); // Get rich snippet. $snippet = get_post_meta( $post->ID, 'rank_math_rich_snippet', true ); $wp_review_total = get_post_meta( $post->ID, 'wp_review_total', true ); if ( ! in_array( $snippet, [ 'recipe', 'product', 'event', 'restaurant', 'review', 'service', 'software' ], true ) && ! $wp_review_total ) { return false; } $snippet_data = [ 'type' => $snippet ]; if ( 'product' === $post->post_type && Conditional::is_woocommerce_active() ) { $product = wc_get_product( $post->ID ); $snippet_data['data'] = [ 'price' => $product->get_price(), 'currency' => get_woocommerce_currency_symbol(), 'in_stock' => $product->get_stock_status(), ]; } else { if ( 'recipe' === $snippet ) { $hash = [ 'rating' => 'recipe_rating', 'time' => 'recipe_totaltime', 'calories' => 'recipe_calories', ]; } elseif ( 'product' === $snippet ) { $hash = [ 'price' => 'product_price', 'currency' => 'product_currency', 'in_stock' => 'product_instock', ]; } elseif ( 'event' === $snippet ) { $hash = [ 'event_date' => 'event_startdate', 'event_name' => 'name', 'event_place' => 'event_address', ]; } elseif ( 'restaurant' === $snippet ) { $hash = [ 'price_range' => 'local_price_range', ]; } else { $hash = [ 'rating' => $snippet . '_rating_value', 'rating_count' => $snippet . '_rating_count', ]; } foreach ( $hash as $key => $value ) { $value = get_post_meta( $post->ID, 'rank_math_snippet_' . $value, true ); if ( ! $value ) { continue; } if ( 'event_place' === $key ) { $value = implode( ', ', array_filter( $value ) ); } if ( 'event_date' === $key ) { $value = date_i18n( 'j M Y', $value ); } $snippet_data['data'][ $key ] = $value; } } // Get rating. if ( ! isset( $snippet_data['rating'] ) && function_exists( 'wp_review_show_total' ) ) { $wp_review_type_star = 'star' === get_post_meta( $post->ID, 'wp_review_type', true ); if ( $wp_review_total && $wp_review_type_star ) { $snippet_data['data']['rating'] = $wp_review_total; } } if ( ! isset( $snippet_data['rating'] ) && Conditional::is_woocommerce_active() && 'product' === $post->post_type ) { $product = wc_get_product( $post->ID ); if ( $product->get_rating_count() > 0 ) { $snippet_data['data']['rating'] = $product->get_average_rating(); $snippet_data['data']['rating_count'] = (string) $product->get_rating_count(); } } return $snippet_data; } }