*/ namespace RankMath\Frontend; use RankMath\Helper; use RankMath\Paper\Paper; use RankMath\Traits\Hooker; use RankMath\Traits\Shortcode; defined( 'ABSPATH' ) || exit; /** * Shortcodes class. */ class Shortcodes { use Hooker, Shortcode; /** * The Constructor. */ public function __construct() { $this->action( 'init', 'init' ); } /** * Initialize. */ public function init() { // Remove Yoast shortcodes. $this->remove_shortcode( 'wpseo_address' ); $this->remove_shortcode( 'wpseo_map' ); $this->remove_shortcode( 'wpseo_opening_hours' ); // Yoast compatibility shortcodes. $this->add_shortcode( 'wpseo_address', 'yoast_address' ); $this->add_shortcode( 'wpseo_map', 'yoast_map' ); $this->add_shortcode( 'wpseo_opening_hours', 'yoast_opening_hours' ); // Contact shortcode. $this->add_shortcode( 'rank_math_contact_info', 'contact_info' ); // Breadcrumb shortcode. $this->add_shortcode( 'rank_math_breadcrumb', 'breadcrumb' ); } /** * Get the breadcrumb * * @param array $args Arguments. * * @return string */ public function breadcrumb( $args ) { return Breadcrumbs::get()->get_breadcrumb( $args ); } /** * Contact info shortcode, displays nicely formatted contact informations. * * @param array $args Optional. Shortcode arguments - currently only 'show' * parameter, which is a comma-separated list of elements to show. * @return string Shortcode output. */ public function contact_info( $args ) { $args = shortcode_atts( [ 'show' => 'all', 'class' => '', ], $args, 'contact-info' ); $allowed = [ 'address', 'hours', 'phone', 'social', 'map' ]; if ( 'person' === Helper::get_settings( 'titles.knowledgegraph_type' ) ) { $allowed = [ 'address' ]; } if ( ! empty( $args['show'] ) && 'all' !== $args['show'] ) { $allowed = array_intersect( array_map( 'trim', explode( ',', $args['show'] ) ), $allowed ); } wp_enqueue_style( 'rank-math-contact-info', rank_math()->assets() . 'css/rank-math-contact-info.css', null, rank_math()->version ); ob_start(); echo '
'; foreach ( $allowed as $element ) { $method = 'display_' . $element; if ( method_exists( $this, $method ) ) { echo '
'; $this->$method(); echo '
'; } } echo '
'; echo '
'; return ob_get_clean(); } /** * Get contact info container classes. * * @param array $allowed Allowed elements. * @param array $extra_class Shortcode arguments. * @return string */ private function get_contact_classes( $allowed, $extra_class ) { $classes = [ 'rank-math-contact-info', $extra_class ]; foreach ( $allowed as $elem ) { $classes[] = sanitize_html_class( 'show-' . $elem ); } if ( count( $allowed ) === 1 ) { $classes[] = sanitize_html_class( 'show-' . $elem . '-only' ); } return join( ' ', array_filter( $classes ) ); } /** * Output address. */ private function display_address() { $address = Helper::get_settings( 'titles.local_address' ); if ( false === $address ) { return; } $format = nl2br( Helper::get_settings( 'titles.local_address_format' ) ); /** * Allow developer to change the address part format. * * @param string $parts_format String format how to output address part. */ $parts_format = $this->do_filter( 'shortcode/contact/address_parts_format', '%2$s' ); $hash = [ 'streetAddress' => 'address', 'addressLocality' => 'locality', 'addressRegion' => 'region', 'postalCode' => 'postalcode', 'addressCountry' => 'country', ]; ?>
$tag ) { $value = ''; if ( isset( $address[ $key ] ) && ! empty( $address[ $key ] ) ) { $value = sprintf( $parts_format, $tag, $address[ $key ] ); } $format = str_replace( "{{$tag}}", $value, $format ); } echo $format; ?>
get_hours_combined( $hours ); $format = Helper::get_settings( 'titles.opening_hours_format' ); ?>
$days ) { if ( $format ) { $hours = explode( '-', $time ); $time = isset( $hours[1] ) ? date( 'g:i a', strtotime( $hours[0] ) ) . '-' . date( 'g:i a', strtotime( $hours[1] ) ) : $time; } $time = str_replace( '-', ' – ', $time ); printf( '
%1$s%2$s
', join( ', ', $days ), $time ); } ?>
' . $number . '' : ''; ?>
'Facebook', 'twitter' => 'Twitter', 'google_places' => 'Google Places', 'yelp' => 'Yelp', 'foursquare' => 'FourSquare', 'flickr' => 'Flickr', 'reddit' => 'Reddit', 'linkedin' => 'LinkedIn', 'instagram' => 'Instagram', 'youtube' => 'YouTube', 'pinterest' => 'Pinterest', 'soundcloud' => 'SoundClound', 'tumblr' => 'Tumblr', 'myspace' => 'MySpace', ]; ?>
$label ) : if ( $url = Helper::get_settings( 'titles.social_url_' . $id ) ) : // phpcs:ignore ?>
do_filter( 'shortcode/contact/map_address', implode( ' ', $address ) ); $address = $this->do_filter( 'shortcode/contact/map_iframe_src', '//maps.google.com/maps?q=' . urlencode( $address ) . '&z=15&output=embed&key=' . urlencode( Helper::get_settings( 'titles.maps_api_key' ) ) ); ?> '0', 'hide_address' => '0', 'show_state' => '1', 'show_country' => '1', 'show_phone' => '1', 'show_phone_2' => '1', 'show_fax' => '1', 'show_email' => '1', 'show_url' => '0', 'show_vat' => '0', 'show_tax' => '0', 'show_coc' => '0', 'show_price_range' => '0', 'show_logo' => '0', 'show_opening_hours' => '0', ], $args, 'wpseo_address' ); $show = [ 'address' ]; if ( 1 === absint( $atts['show_phone'] ) ) { $show[] = 'phone'; } if ( 1 === absint( $atts['show_opening_hours'] ) ) { $show[] = 'hours'; } return $this->contact_info( [ 'show' => join( ',', $show ), 'class' => 'wpseo_address_compat', ] ); } /** * Yoast map compatibility functionality. * * @param array $args Array of arguments. * @return string */ public function yoast_map( $args ) { return $this->contact_info( [ 'show' => 'map', 'class' => 'wpseo_map_compat', ] ); } /** * Yoast opening hours compatibility functionality. * * @param array $args Array of arguments. * @return string */ public function yoast_opening_hours( $args ) { return $this->contact_info( [ 'show' => 'hours', 'class' => 'wpseo_opening_hours_compat', ] ); } }