Skip to content

Instantly share code, notes, and snippets.

@danielbachhuber
Created March 16, 2016 12:46
Show Gist options
  • Save danielbachhuber/5fb19ff57ba273348c17 to your computer and use it in GitHub Desktop.
Save danielbachhuber/5fb19ff57ba273348c17 to your computer and use it in GitHub Desktop.
Get all of the public, protected, and private methods on WP-API classes
<?php
/**
* Get all of the public, protected, and private methods on WP-API classes
*
* Run with `wp eval-file class-methods.php`
*/
foreach( get_declared_classes() as $class ) {
if ( ! preg_match( '#WP_REST_([A-Za-z_]+)?Controller#', $class ) ) {
continue;
}
WP_CLI::log( $class );
$reflection = new ReflectionClass( $class );
$get_name = function( $methods ) use ( $class ) {
$names = array();
foreach( $methods as $method ) {
if ( $method->class !== $class ) {
continue;
}
$names[] = $method->name;
}
return $names;
};
$methods = array(
'public' => $get_name( $reflection->getMethods( ReflectionMethod::IS_PUBLIC ) ),
'protected' => $get_name( $reflection->getMethods( ReflectionMethod::IS_PROTECTED ) ),
'private' => $get_name( $reflection->getMethods( ReflectionMethod::IS_PRIVATE ) ),
);
$table = new \cli\Table;
$table->setHeaders( array( 'public', 'protected', 'private' ) );
$rows = array();
for ( $i=0; $i < 40; $i++) {
if ( empty( $methods['public'][ $i ] ) && empty( $methods['protected'][ $i ] ) && empty( $methods['private'][ $i ] ) ) {
break;
}
$row = array();
foreach( array( 'public', 'protected', 'private' ) as $type ) {
if ( isset( $methods[ $type ][ $i ] ) ) {
$row[] = $methods[ $type ][ $i ];
} else {
$row[] = '';
}
}
$rows[] = $row;
}
$table->setRows( $rows );
$table->display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment