| 1 |
<?php |
| 2 |
|
| 3 |
namespace Buddypress\CLI\Command; |
| 4 |
|
| 5 |
use WP_CLI; |
| 6 |
use Scaffold_Command; |
| 7 |
|
| 8 |
/** |
| 9 |
* Scaffold BuddyPress unit tests. |
| 10 |
* |
| 11 |
* ## EXAMPLE |
| 12 |
* |
| 13 |
* # Scaffold BuddyPress specific tests. |
| 14 |
* $ wp bp scaffold tests sample-plugin |
| 15 |
* Success: Created BuddyPress test files. |
| 16 |
* |
| 17 |
* @since 2.0 |
| 18 |
*/ |
| 19 |
class Scaffold extends Scaffold_Command { |
| 20 |
|
| 21 |
/** |
| 22 |
* Default dependency check for a BuddyPress CLI command. |
| 23 |
*/ |
| 24 |
public static function check_dependencies() { |
| 25 |
if ( ! class_exists( 'Buddypress' ) ) { |
| 26 |
WP_CLI::error( 'The BuddyPress plugin is not active.' ); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Plugin scaffold command. |
| 32 |
* |
| 33 |
* ## OPTIONS |
| 34 |
* |
| 35 |
* <slug> |
| 36 |
* : The slug of the BuddyPress plugin. |
| 37 |
* |
| 38 |
* [--force] |
| 39 |
* : Whether to overwrite files. |
| 40 |
* |
| 41 |
* ## EXAMPLES |
| 42 |
* |
| 43 |
* # Scaffold BuddyPress specific tests. |
| 44 |
* $ wp bp scaffold plugin sample-test |
| 45 |
* Success: Created BuddyPress test files. |
| 46 |
* |
| 47 |
* # Scaffold BuddyPress specific tests. |
| 48 |
* $ wp bp scaffold tests another-sample-test |
| 49 |
* Success: Created BuddyPress test files. |
| 50 |
* |
| 51 |
* @subcommand tests |
| 52 |
*/ |
| 53 |
public function plugin( $args, $assoc_args ) { |
| 54 |
$wp_filesystem = $this->init_wp_filesystem(); |
| 55 |
$target_dir = WP_PLUGIN_DIR . "/{$args[0]}"; |
| 56 |
|
| 57 |
if ( ! is_dir( $target_dir ) ) { |
| 58 |
WP_CLI::error( "Invalid plugin slug specified. No such target directory '{$target_dir}'." ); |
| 59 |
} |
| 60 |
|
| 61 |
$error_msg = $this->check_target_directory( $target_dir ); |
| 62 |
if ( ! empty( $error_msg ) ) { |
| 63 |
WP_CLI::error( "Invalid plugin slug specified. {$error_msg}" ); |
| 64 |
} |
| 65 |
|
| 66 |
$to_copy = [ |
| 67 |
'install-bp-tests.sh' => "{$target_dir}/bin", |
| 68 |
'bootstrap-buddypress.php' => "{$target_dir}/tests", |
| 69 |
]; |
| 70 |
|
| 71 |
foreach ( $to_copy as $file => $dir ) { |
| 72 |
$file_name = "$dir/$file"; |
| 73 |
|
| 74 |
$prompt = WP_CLI\Utils\get_flag_value( $assoc_args, 'force' ); |
| 75 |
|
| 76 |
// Prompt it. |
| 77 |
$should_write_file = $this->prompt_if_files_will_be_overwritten( $file_name, $prompt ); |
| 78 |
|
| 79 |
if ( false === $should_write_file ) { |
| 80 |
continue; |
| 81 |
} |
| 82 |
|
| 83 |
$files_written[] = $file_name; |
| 84 |
|
| 85 |
$wp_filesystem->copy( self::get_template_path( $file ), $file_name, true ); |
| 86 |
|
| 87 |
if ( 'install-bp-tests.sh' === $file ) { |
| 88 |
if ( ! $wp_filesystem->chmod( "$dir/$file", 0755 ) ) { |
| 89 |
WP_CLI::warning( "Couldn't mark 'install-bp-tests.sh' as executable." ); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
$this->log_whether_files_written( |
| 95 |
$files_written, |
| 96 |
'All BuddyPress test files were skipped.', |
| 97 |
'Created BuddyPress test files.' |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Checks that the `$target_dir` is a child directory of the WP themes or plugins directory, depending on `$type`. |
| 103 |
* |
| 104 |
* @param string $type "theme" or "plugin" |
| 105 |
* @param string $target_dir The theme/plugin directory to check. |
| 106 |
* @return null|string Returns null on success, error message on error. |
| 107 |
*/ |
| 108 |
public function check_target_directory( $target_dir ) { |
| 109 |
$parent_dir = dirname( self::canonicalize_path( str_replace( '\\', '/', $target_dir ) ) ); |
| 110 |
|
| 111 |
if ( str_replace( '\\', '/', WP_PLUGIN_DIR ) !== $parent_dir ) { |
| 112 |
return sprintf( 'The target directory \'%1$s\' is not in \'%2$s\'.', $target_dir, WP_PLUGIN_DIR ); |
| 113 |
} |
| 114 |
|
| 115 |
// Success. |
| 116 |
return null; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Canonicalizes a path. |
| 121 |
* |
| 122 |
* @param string $path Path. |
| 123 |
* @return string |
| 124 |
*/ |
| 125 |
public static function canonicalize_path( $path ) { |
| 126 |
if ( '' === $path || '/' === $path ) { |
| 127 |
return $path; |
| 128 |
} |
| 129 |
|
| 130 |
if ( '.' === substr( $path, -1 ) ) { |
| 131 |
$path .= '/'; |
| 132 |
} |
| 133 |
|
| 134 |
$output = []; |
| 135 |
|
| 136 |
foreach ( explode( '/', $path ) as $segment ) { |
| 137 |
if ( '..' === $segment ) { |
| 138 |
array_pop( $output ); |
| 139 |
} elseif ( '.' !== $segment ) { |
| 140 |
$output[] = $segment; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return implode( '/', $output ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Gets the template path based on installation type. |
| 149 |
* |
| 150 |
* @return string Template path. |
| 151 |
*/ |
| 152 |
public static function get_template_path( $template ) { |
| 153 |
$command_root = WP_CLI\Utils\phar_safe_path( dirname( __DIR__ ) ); |
| 154 |
$template_path = "{$command_root}/src/templates/{$template}"; |
| 155 |
|
| 156 |
if ( ! file_exists( $template_path ) ) { |
| 157 |
WP_CLI::error( "Couldn't find {$template}" ); |
| 158 |
} |
| 159 |
|
| 160 |
return $template_path; |
| 161 |
} |
| 162 |
} |
| 163 |
|