*/ namespace RankMath\Local_Seo; use RankMath\Post; use RankMath\Helper; use RankMath\Traits\Ajax; use RankMath\Traits\Hooker; use MyThemeShop\Helpers\Str; use RankMath\Sitemap\Router; defined( 'ABSPATH' ) || exit; /** * KML_File class. */ class KML_File { use Ajax, Hooker; /** * The Constructor. */ public function __construct() { $this->action( 'init', 'init', 1 ); $this->filter( 'rank_math/sitemap/index', 'add_local_sitemap' ); $this->filter( 'rank_math/sitemap/local/content', 'local_sitemap_content' ); $this->filter( 'rank_math/sitemap/locations/content', 'kml_file_content' ); $this->action( 'cmb2_save_options-page_fields_rank-math-options-titles_options', 'update_sitemap', 25, 2 ); } /** * Set up rewrite rules. */ public function init() { add_rewrite_rule( 'locations\.kml$', 'index.php?sitemap=locations', 'top' ); } /** * Add the Local SEO Sitemap to the sitemap index. * * @return string $xml The sitemap index with the Local SEO Sitemap added. */ public function add_local_sitemap() { $xml = $this->newline( '' ); $xml .= $this->newline( '' . Router::get_base_url( 'local-sitemap.xml' ) . '' ); $xml .= $this->newline( '' . $this->get_modified_date() . '' ); $xml .= $this->newline( '' ); return $xml; } /** * The content of the Local SEO Sitemap. * * @return string $urlset Local SEO Sitemap XML content. */ public function local_sitemap_content() { $urlset = ' ' . Router::get_base_url( 'locations.kml' ) . ' ' . $this->get_modified_date() . ' '; return $urlset; } /** * Generate the KML file contents. * * @return string $kml KML file content. */ public function kml_file_content() { $business = $this->get_local_seo_data(); $business_name = esc_html( $business['business_name'] ); $business_url = esc_url( $business['business_url'] ); $address = ! empty( $business['address'] ) ? implode( ', ', array_filter( $business['address'] ) ) : ''; $kml = $this->newline( '' ); $kml .= $this->newline( '', 1 ); $kml .= $this->newline( 'Locations for ' . $business_name . '', 2 ); $kml .= $this->newline( '1', 2 ); if ( ! empty( $business['author'] ) ) { $kml .= $this->newline( '', 2 ); $kml .= $this->newline( '' . $business['author'] . '', 3 ); $kml .= $this->newline( '', 2 ); } if ( ! empty( $business_url ) ) { $kml .= $this->newline( '', 2 ); } $kml .= $this->newline( '', 2 ); $kml .= $this->newline( '', 3 ); $kml .= $this->newline( '', 3 ); $kml .= $this->newline( '
', 3 ); $kml .= $this->newline( '', 3 ); $kml .= $this->newline( '', 3 ); $kml .= $this->newline( '', 3 ); $kml .= $this->newline( '' . $business['coords']['lat'] . '', 4 ); $kml .= $this->newline( '' . $business['coords']['long'] . '', 4 ); $kml .= $this->newline( '0', 4 ); $kml .= $this->newline( '', 4 ); $kml .= $this->newline( '0', 4 ); $kml .= $this->newline( '', 3 ); $kml .= $this->newline( '', 3 ); $kml .= $this->newline( '' . $business['coords']['long'] . ',' . $business['coords']['lat'] . '', 4 ); $kml .= $this->newline( '', 3 ); $kml .= $this->newline( '
', 2 ); $kml .= $this->newline( '
', 1 ); $kml .= $this->newline( '
' ); return $kml; } /** * Update the sitemap when the Local SEO settings are changed. * * @param int $object_id The ID of the current object. * @param array $updated Array of field IDs that were updated. * Will only include field IDs that had values change. */ public function update_sitemap( $object_id, $updated ) { $local_seo_fields = [ 'knowledgegraph_name', 'url', 'email', 'local_address', 'local_business_type', 'opening_hours', 'phone_numbers', 'price_range', 'geo', ]; if ( count( array_intersect( $local_seo_fields, $updated ) ) ) { update_option( 'rank_math_local_seo_update', date( 'c' ) ); \RankMath\Sitemap\Sitemap::ping_search_engines( Router::get_base_url( 'local-sitemap.xml' ) ); } } /** * Get the Local SEO data. * * @return array */ private function get_local_seo_data() { $geo = Str::to_arr( Helper::get_settings( 'titles.geo' ) ); $cords = [ 'lat' => isset( $geo[0] ) ? $geo[0] : '', 'long' => isset( $geo[1] ) ? $geo[1] : '', ]; $phone_numbers = Helper::get_settings( 'titles.phone_numbers' ); $number = ! empty( $phone_numbers ) && isset( $phone_numbers[0]['number'] ) ? $phone_numbers[0]['number'] : ''; $locations = [ 'business_name' => Helper::get_settings( 'titles.knowledgegraph_name' ), 'business_description' => get_option( 'blogname' ) . ' - ' . get_option( 'blogdescription' ), 'business_email' => Helper::get_settings( 'titles.email' ), 'business_phone' => $number, 'business_url' => Helper::get_settings( 'titles.url' ), 'address' => Helper::get_settings( 'titles.local_address' ), 'coords' => $cords, 'author' => get_option( 'blogname' ), ]; return $this->do_filter( 'sitemap/locations/data', $locations ); } /** * Get the Modified Date. * * @return $date */ private function get_modified_date() { if ( ! $date = get_option( 'rank_math_local_seo_update' ) ) { // phpcs:ignore $date = date( 'c' ); } return $date; } /** * Write a newline with indent count. * * @param string $content Content to write. * @param integer $indent Count of indent. * * @return string */ private function newline( $content, $indent = 0 ) { return str_repeat( "\t", $indent ) . $content . "\n"; } }