resolver = $resolver; $this->renderer = $renderer; } public function register() { add_shortcode( 'glvr', array( $this, 'render' ) ); // WordPress strips every shortcode before trimming an automatic excerpt, // which left sentences like "Call us at ." Keep ours only while an // excerpt is built; it is then resolved with the rest of the content. add_filter( 'get_the_excerpt', array( $this, 'begin_excerpt' ), 9 ); add_filter( 'get_the_excerpt', array( $this, 'end_excerpt' ), 11 ); add_filter( 'strip_shortcodes_tagnames', array( $this, 'keep_in_excerpt' ) ); } public function begin_excerpt( $excerpt ) { $this->building_excerpt = true; return $excerpt; } public function end_excerpt( $excerpt ) { $this->building_excerpt = false; return $excerpt; } public function keep_in_excerpt( $tags ) { return $this->building_excerpt ? array_values( array_diff( (array) $tags, array( 'glvr' ) ) ) : $tags; } public function render( $attributes ) { $attributes = shortcode_atts( array( 'key' => '', 'fallback' => '', 'format' => BuiltinVariables::DEFAULT_DATE_FORMAT, 'size' => '', ), is_array( $attributes ) ? $attributes : array(), 'glvr' ); $key = sanitize_key( $attributes['key'] ); $context = array( 'source' => 'shortcode', 'image_size' => sanitize_key( $attributes['size'] ), ); if ( BuiltinVariables::CURRENT_DATE_KEY === $key ) { $context['date_format'] = BuiltinVariables::sanitize_date_format( $attributes['format'] ); } $resolved = $key ? $this->resolver->resolve( $key, $context ) : null; if ( ! $resolved ) { return esc_html( $attributes['fallback'] ); } return $this->renderer->render( $resolved['value'], $resolved['variable']['type'], $resolved['context'] ); } }