NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Hooks.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Automatic filter binding.
  4. */
  5. class scbHooks {
  6. private static $mangle_name;
  7. /**
  8. * Adds.
  9. *
  10. * @param string $class
  11. *
  12. * @return void
  13. */
  14. public static function add( $class ) {
  15. self::_do( 'add_filter', $class );
  16. }
  17. /**
  18. * Removes.
  19. *
  20. * @param string $class
  21. *
  22. * @return void
  23. */
  24. public static function remove( $class ) {
  25. self::_do( 'remove_filter', $class );
  26. }
  27. /**
  28. * Prints debug.
  29. *
  30. * @param string $class
  31. * @param string $mangle_name (optional)
  32. *
  33. * @return void
  34. */
  35. public static function debug( $class, $mangle_name = false ) {
  36. self::$mangle_name = $mangle_name;
  37. echo "<pre>";
  38. self::_do( array( __CLASS__, '_print' ), $class );
  39. echo "</pre>";
  40. }
  41. /**
  42. * Prints.
  43. *
  44. * @param string $tag
  45. * @param array $callback
  46. * @param int $prio
  47. * @param int $argc
  48. *
  49. * @return void
  50. */
  51. private static function _print( $tag, $callback, $prio, $argc ) {
  52. $static = ! is_object( $callback[0] );
  53. if ( self::$mangle_name ) {
  54. $class = $static ? '__CLASS__' : '$this';
  55. } else if ( $static ) {
  56. $class = "'" . $callback[0] . "'";
  57. } else {
  58. $class = '$' . get_class( $callback[0] );
  59. }
  60. $func = "array( $class, '$callback[1]' )";
  61. echo "add_filter( '$tag', $func";
  62. if ( $prio != 10 || $argc > 1 ) {
  63. echo ", $prio";
  64. if ( $argc > 1 ) {
  65. echo ", $argc";
  66. }
  67. }
  68. echo " );\n";
  69. }
  70. /**
  71. * Processes.
  72. *
  73. * @param string $action
  74. * @param string $class
  75. *
  76. * @return void
  77. */
  78. private static function _do( $action, $class ) {
  79. $reflection = new ReflectionClass( $class );
  80. foreach ( $reflection->getMethods() as $method ) {
  81. if ( $method->isPublic() && ! $method->isConstructor() ) {
  82. $comment = $method->getDocComment();
  83. if ( preg_match( '/@nohook[ \t\*\n]+/', $comment ) ) {
  84. continue;
  85. }
  86. preg_match_all( '/@hook:?\s+([^\s]+)/', $comment, $matches ) ? $matches[1] : $method->name;
  87. if ( empty( $matches[1] ) ) {
  88. $hooks = array( $method->name );
  89. } else {
  90. $hooks = $matches[1];
  91. }
  92. $priority = preg_match( '/@priority:?\s+(\d+)/', $comment, $matches ) ? $matches[1] : 10;
  93. foreach ( $hooks as $hook ) {
  94. call_user_func( $action, $hook, array( $class, $method->name ), $priority, $method->getNumberOfParameters() );
  95. }
  96. }
  97. }
  98. }
  99. }