<?php

class ShortcodeHelper {
  private static $shortcodes = [];

  static function register_hooks() {
    add_filter("pre_do_shortcode_tag", ["ShortcodeHelper", "before_shortcode"], 1, 4);
    add_filter("do_shortcode_tag", ["ShortcodeHelper", "after_shortcode"], 999999, 4);
  }

  static function before_shortcode($result, $tag, $attr, $m) {
    array_push(self::$shortcodes, ["tag" => $tag, "attr" => $attr]);
    return $result;
  }

  public static function after_shortcode($output, $tag, $attr, $m) {
    if (count(self::$shortcodes)) {
      array_pop(self::$shortcodes);
    }
    return $output;
  }

  public static function current_shortcode() {
    $count = count(self::$shortcodes);
    if ($count) {
      return self::$shortcodes[$count - 1];
    }
    return null;
  }
}

function current_shortcode() {
  return ShortcodeHelper::current_shortcode();
}
