base_setup(); $this->base_hooks(); } /** * Initialize Page. * * @return self */ public static function init() { if ( is_null( static::$instance ) ) { static::$instance = new static(); } return static::$instance; } /** * Setup * * @return void */ private function base_setup() { if ( method_exists( $this, 'setup' ) ) { $this->setup(); } } /** * Get Global Flag. * * @return string */ public static function get_global_flag() { return static::get_shortcode_name() . '-global-shortcode-trigger'; } /** * Get Inside Flag. * * @return string */ public static function get_inside_flag() { return static::get_shortcode_name() . '-inside-shortcode-trigger'; } /** * Hooks. * * @return void */ private function base_hooks() { add_action( 'init', array( $this, 'setup_shortcode' ) ); if ( method_exists( $this, 'hooks' ) ) { $this->hooks(); } } /** * Setup Shortcode. * * @return void */ public function setup_shortcode() { add_shortcode( static::get_shortcode_name(), array( $this, 'shortcode_callback' ) ); } /** * Get Shortcode. * * @param array $attrs * @return string */ public function get_shortcode( $attrs = array() ) { return '[' . $this->get_shortcode_name() . ' ' . $this->custom_attributes_html( $attrs ) . ' ]'; } /** * Do Shortcode. * * @param array $attrs * @return void */ public function do_shortcode( $attrs, $includes_inputs = false ) { $attrs = $this->handle_shortcode_attrs( $attrs ); if ( false === $attrs ) { return; } if ( $includes_inputs ) { echo do_shortcode( $this->get_shortcode( $attrs ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } else { echo wp_kses_post( do_shortcode( $this->get_shortcode( $attrs ) ) ); } } /** * Get Custom Attributes HTML. * * @return string */ protected function custom_attributes_html( $attrs = array() ) { $attributes = ''; foreach ( $attrs as $key => $value ) { $attributes .= esc_attr( $key ) . '="' . esc_attr( $value ) . '" '; } return $attributes; } /** * Set Shortcode name. * * @return string */ abstract public static function get_shortcode_name(); /** * Shortcode Callback. * * @return void */ abstract protected function _shortcode_callback( $attrs, $content ); /** * Shortcode Callback Wrapper. * * @return string */ public function shortcode_callback( $attrs, $content, $shortcode_tag ) { $attrs = (array) $attrs; $attrs = $this->handle_shortcode_attrs( $attrs ); if ( false === $attrs ) { return ''; } $this->_shortcode_start(); ob_start(); // Shortcode Start. $this->shortcode_wrapper_start(); // Shortcode Content. $this->_shortcode_callback( $attrs, $content ); // Shortcode End. $this->shortcode_wrapper_end(); $result = ob_get_clean(); $this->_shortcode_end(); return $result; } /** * Prepare Shortcode Attributes - Sanitize - Check Required attrs. * * @param array $attrs * @return array|false */ private function handle_shortcode_attrs( $attrs = array() ) { $filtered_attrs = array(); foreach ( $attrs as $key => $value ) { $key = sanitize_text_field( wp_unslash( $key ) ); $value = sanitize_text_field( wp_unslash( $value ) ); $filtered_attrs[ $key ] = $value; } if ( ! empty( $this->required_shortcode_attributes ) && ( count( array_intersect( $this->required_shortcode_attributes, array_keys( $filtered_attrs ) ) ) !== count( $this->required_shortcode_attributes ) ) ) { return false; } $attrs = shortcode_atts( $this->get_attrs_defaults(), $attrs, $this->get_shortcode_name() ); return $attrs; } /** * Get Attributes and their defautls pair. * * @return array */ private function get_attrs_defaults() { $default_attrs = array(); foreach ( $this->shortcode_attributes as $attr_arr ) { if ( isset( $attr_arr['default_val'] ) ) { $default_attrs[ $attr_arr['name'] ] = $attr_arr['default_val']; } } return $default_attrs; } /** * Shortcode Wrapper Start. * * @return void */ private function shortcode_wrapper_start() { if ( $this->include_wrapper ) : ?>
include_wrapper ) : ?>
shortcode_start(); } } /** * After Shortcode Content. * * @return void */ private function _shortcode_end() { unset( $GLOBALS[ self::get_inside_flag() ] ); if ( method_exists( $this, 'shortcode_end' ) ) { $this->shortcode_end(); } } /** * Check if current page has the shortcode. * * @return boolean */ public static function has_shortcode() { return ! empty( $GLOBALS[ self::get_global_flag() ] ); } /** * Check if inside the shortcode. * * @return boolean */ public static function is_in_shortcode() { return ! empty( $GLOBALS[ self::get_inside_flag() ] ); } /** * Print Shortcode HTML and attributes details. * * @return void */ public function print_shortcode_details( $attrs = array() ) { ?>
shortcode_title ) ) : ?>

shortcode_title ); ?>

shortcode_subtitle ) ) : ?> shortcode_subtitle ); ?>
custom_attributes_html( $attrs ) . ' ]' ); ?> shortcode_attributes ) ) : ?>
shortcode_attributes ) ) : ?>
shortcode_attributes as $attribute ) : ?>