*/
namespace RankMath\Frontend;
use RankMath\Helper;
use RankMath\Paper\Paper;
use RankMath\Traits\Hooker;
use RankMath\Traits\Shortcode;
use RankMath\Helpers\Arr;
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' );
$this->remove_shortcode( 'wpseo_breadcrumb' );
$this->remove_shortcode( 'aioseo_breadcrumbs' );
// Add 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' );
$this->add_shortcode( 'wpseo_breadcrumb', 'breadcrumb' );
$this->add_shortcode( 'aioseo_breadcrumbs', 'breadcrumb' );
// Add the Contact shortcode.
$this->add_shortcode( 'rank_math_contact_info', 'contact_info' );
// Add the Breadcrumbs shortcode.
$this->add_shortcode( 'rank_math_breadcrumb', 'breadcrumb' );
}
/**
* Get the breadcrumbs.
*
* @param array $args Arguments.
*
* @return string
*/
public function breadcrumb( $args ) {
if ( ! Helper::is_breadcrumbs_enabled() ) {
return;
}
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 = $this->get_allowed_info( $args );
wp_enqueue_style( 'rank-math-contact-info', rank_math()->assets() . 'css/rank-math-contact-info.css', null, rank_math()->version );
ob_start();
echo '
';
echo '';
/**
* Change the Contact Info HTML output.
*
* @param string $unsigned HTML output.
*/
return $this->do_filter( 'contact_info/html', ob_get_clean() );
}
/**
* Get allowed info array.
*
* @param array $args Shortcode arguments - currently only 'show'.
*
* @return array
*/
private function get_allowed_info( $args ) {
$type = Helper::get_settings( 'titles.knowledgegraph_type' );
$allowed = 'person' === $type
? [ 'name', 'email', 'person_phone', 'address' ]
: [ 'name', 'organization_description', 'email', 'address', 'hours', 'phone', 'additional_info', 'map' ];
if ( ! empty( $args['show'] ) && 'all' !== $args['show'] ) {
$allowed = array_intersect( Arr::from_string( $args['show'] ), $allowed );
}
return $allowed;
}
/**
* 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 to output the 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' );
?>
get_localized_day( $hour['day'] );
}
return $combined;
}
/**
* Retrieve the full translated weekday word.
*
* @param string $day Day to translate.
*
* @return string
*/
private function get_localized_day( $day ) {
global $wp_locale;
$hash = [
'Sunday' => 0,
'Monday' => 1,
'Tuesday' => 2,
'Wednesday' => 3,
'Thursday' => 4,
'Friday' => 5,
'Saturday' => 6,
];
return $wp_locale->get_weekday( $hash[ $day ] );
}
/**
* Output phone numbers.
*/
private function display_phone() {
$phones = Helper::get_settings( 'titles.phone_numbers' );
if ( empty( $phones ) ) {
return;
}
$choices = Helper::choices_phone_types();
foreach ( $phones as $phone ) :
if ( empty( $phone['number'] ) ) {
continue;
}
$number = esc_html( $phone['number'] );
$label = isset( $choices[ $phone['type'] ] ) ? $choices[ $phone['type'] ] : ''
?>
' . $number . '' : ''; ?>
do_filter( 'shortcode/contact/map_address', implode( ' ', $address ) );
$address = $this->do_filter( 'shortcode/contact/map_iframe_src', '//maps.google.com/maps?q=' . rawurlencode( $address ) . '&z=15&output=embed&key=' . rawurlencode( $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',
]
);
}
}