titles = $titles; } public function register() { $titles = '1' === get_option( TitleShortcodes::OPTION, '0' ); // Yoast SEO. foreach ( array( 'wpseo_metadesc', 'wpseo_opengraph_desc', 'wpseo_twitter_description' ) as $hook ) { add_filter( $hook, array( $this, 'text' ) ); } // Rank Math. add_filter( 'rank_math/frontend/description', array( $this, 'text' ) ); add_filter( 'rank_math/paper/auto_generated_description/apply_shortcode', array( $this, 'rank_math_apply_shortcode' ) ); if ( ! $titles ) { return; } foreach ( array( 'wpseo_title', 'wpseo_opengraph_title', 'wpseo_twitter_title', 'rank_math/frontend/title' ) as $hook ) { add_filter( $hook, array( $this, 'text' ) ); } add_filter( 'wpseo_schema_graph', array( $this, 'structure' ) ); add_filter( 'rank_math/json_ld', array( $this, 'structure' ), 99 ); add_filter( 'wpseo_breadcrumb_links', array( $this, 'structure' ) ); add_filter( 'rank_math/frontend/breadcrumb/items', array( $this, 'structure' ) ); } /** * A title or description with its Site Variables resolved. * * @param mixed $text Text from the SEO plugin. * @return mixed */ public function text( $text ) { return is_string( $text ) ? $this->titles->plain( $text, get_queried_object_id() ) : $text; } /** * Every string in a schema graph or breadcrumb trail, resolved. * * @param mixed $data Array from the SEO plugin. * @return mixed */ public function structure( $data ) { if ( is_string( $data ) ) { return $this->text( $data ); } if ( is_array( $data ) ) { foreach ( $data as $key => $value ) { $data[ $key ] = $this->structure( $value ); } } return $data; } /** * Rank Math removes every shortcode from the content it uses for automatic * descriptions, which left "Our phone is and …". Let it run shortcodes only * when the page's only shortcodes are Site Variables, so no other plugin's * shortcode runs because of Glovario. * * @param bool $apply Whether Rank Math runs shortcodes. * @return bool */ public function rank_math_apply_shortcode( $apply ) { $post = get_post( get_queried_object_id() ); if ( $apply || ! $post || ! is_singular() ) { return $apply; } $content = $post->post_content . ' ' . $post->post_excerpt; if ( false === strpos( $content, '[glvr' ) || ! preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches ) ) { return $apply; } return array() === array_diff( array_unique( $matches[2] ), array( 'glvr' ) ); } }