Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack
File: class.jetpack-cli.php
* Creates the essential files in Jetpack to build a Gutenberg block.
[2000] Fix | Delete
*
[2001] Fix | Delete
* @param array $args Positional parameters. Only one is used, that corresponds to the block title.
[2002] Fix | Delete
* @param array $assoc_args Associative parameters defined in the scaffold() method.
[2003] Fix | Delete
*/
[2004] Fix | Delete
public function block( $args, $assoc_args ) {
[2005] Fix | Delete
if ( ! isset( $args[1] ) ) {
[2006] Fix | Delete
WP_CLI::error( esc_html__( 'The title parameter is required.', 'jetpack' ) . ' 👻' );
[2007] Fix | Delete
exit( 1 );
[2008] Fix | Delete
}
[2009] Fix | Delete
[2010] Fix | Delete
$title = ucwords( $args[1] );
[2011] Fix | Delete
[2012] Fix | Delete
$slug = isset( $assoc_args['slug'] )
[2013] Fix | Delete
? $assoc_args['slug']
[2014] Fix | Delete
: sanitize_title( $title );
[2015] Fix | Delete
[2016] Fix | Delete
$next_version = "\x24\x24next-version$$"; // Escapes to hide the string from tools/replace-next-version-tag.sh
[2017] Fix | Delete
[2018] Fix | Delete
$variation_options = array( 'production', 'experimental', 'beta' );
[2019] Fix | Delete
$variation = ( isset( $assoc_args['variation'] ) && in_array( $assoc_args['variation'], $variation_options, true ) )
[2020] Fix | Delete
? $assoc_args['variation']
[2021] Fix | Delete
: 'beta';
[2022] Fix | Delete
[2023] Fix | Delete
if ( preg_match( '#^jetpack/#', $slug ) ) {
[2024] Fix | Delete
$slug = preg_replace( '#^jetpack/#', '', $slug );
[2025] Fix | Delete
}
[2026] Fix | Delete
[2027] Fix | Delete
if ( ! preg_match( '/^[a-z][a-z0-9\-]*$/', $slug ) ) {
[2028] Fix | Delete
WP_CLI::error( esc_html__( 'Invalid block slug. They can contain only lowercase alphanumeric characters or dashes, and start with a letter', 'jetpack' ) . ' 👻' );
[2029] Fix | Delete
}
[2030] Fix | Delete
[2031] Fix | Delete
global $wp_filesystem;
[2032] Fix | Delete
if ( ! WP_Filesystem() ) {
[2033] Fix | Delete
WP_CLI::error( esc_html__( "Can't write files", 'jetpack' ) . ' 😱' );
[2034] Fix | Delete
}
[2035] Fix | Delete
[2036] Fix | Delete
$path = JETPACK__PLUGIN_DIR . "extensions/blocks/$slug";
[2037] Fix | Delete
[2038] Fix | Delete
if ( $wp_filesystem->exists( $path ) && $wp_filesystem->is_dir( $path ) ) {
[2039] Fix | Delete
/* translators: %s is path to the conflicting block */
[2040] Fix | Delete
WP_CLI::error( sprintf( esc_html__( 'Name conflicts with the existing block %s', 'jetpack' ), $path ) . ' ⛔️' );
[2041] Fix | Delete
exit( 1 );
[2042] Fix | Delete
}
[2043] Fix | Delete
[2044] Fix | Delete
$wp_filesystem->mkdir( $path );
[2045] Fix | Delete
[2046] Fix | Delete
$keywords = isset( $assoc_args['keywords'] )
[2047] Fix | Delete
? array_map(
[2048] Fix | Delete
function ( $keyword ) {
[2049] Fix | Delete
return trim( $keyword );
[2050] Fix | Delete
},
[2051] Fix | Delete
array_slice( explode( ',', $assoc_args['keywords'] ), 0, 3 )
[2052] Fix | Delete
)
[2053] Fix | Delete
: array();
[2054] Fix | Delete
[2055] Fix | Delete
$files = array(
[2056] Fix | Delete
"$path/block.json" => self::render_block_file(
[2057] Fix | Delete
'block-block-json',
[2058] Fix | Delete
array(
[2059] Fix | Delete
'slug' => $slug,
[2060] Fix | Delete
'title' => wp_json_encode( $title, JSON_UNESCAPED_UNICODE ),
[2061] Fix | Delete
'description' => isset( $assoc_args['description'] )
[2062] Fix | Delete
? wp_json_encode( $assoc_args['description'], JSON_UNESCAPED_UNICODE )
[2063] Fix | Delete
: wp_json_encode( $title, JSON_UNESCAPED_UNICODE ),
[2064] Fix | Delete
'nextVersion' => $next_version,
[2065] Fix | Delete
'keywords' => wp_json_encode( $keywords, JSON_UNESCAPED_UNICODE ),
[2066] Fix | Delete
)
[2067] Fix | Delete
),
[2068] Fix | Delete
"$path/$slug.php" => self::render_block_file(
[2069] Fix | Delete
'block-register-php',
[2070] Fix | Delete
array(
[2071] Fix | Delete
'nextVersion' => $next_version,
[2072] Fix | Delete
'title' => $title,
[2073] Fix | Delete
'underscoredTitle' => str_replace( ' ', '_', $title ),
[2074] Fix | Delete
)
[2075] Fix | Delete
),
[2076] Fix | Delete
"$path/editor.js" => self::render_block_file( 'block-editor-js' ),
[2077] Fix | Delete
"$path/editor.scss" => self::render_block_file(
[2078] Fix | Delete
'block-editor-scss',
[2079] Fix | Delete
array(
[2080] Fix | Delete
'slug' => $slug,
[2081] Fix | Delete
'title' => $title,
[2082] Fix | Delete
)
[2083] Fix | Delete
),
[2084] Fix | Delete
"$path/edit.js" => self::render_block_file(
[2085] Fix | Delete
'block-edit-js',
[2086] Fix | Delete
array(
[2087] Fix | Delete
'title' => $title,
[2088] Fix | Delete
'className' => str_replace( ' ', '', ucwords( str_replace( '-', ' ', $slug ) ) ),
[2089] Fix | Delete
)
[2090] Fix | Delete
),
[2091] Fix | Delete
);
[2092] Fix | Delete
[2093] Fix | Delete
$files_written = array();
[2094] Fix | Delete
[2095] Fix | Delete
foreach ( $files as $filename => $contents ) {
[2096] Fix | Delete
if ( $wp_filesystem->put_contents( $filename, $contents ) ) {
[2097] Fix | Delete
$files_written[] = $filename;
[2098] Fix | Delete
} else {
[2099] Fix | Delete
/* translators: %s is a file name */
[2100] Fix | Delete
WP_CLI::error( sprintf( esc_html__( 'Error creating %s', 'jetpack' ), $filename ) );
[2101] Fix | Delete
}
[2102] Fix | Delete
}
[2103] Fix | Delete
[2104] Fix | Delete
if ( empty( $files_written ) ) {
[2105] Fix | Delete
WP_CLI::log( esc_html__( 'No files were created', 'jetpack' ) );
[2106] Fix | Delete
} else {
[2107] Fix | Delete
// Load index.json and insert the slug of the new block in its block variation array.
[2108] Fix | Delete
$block_list_path = JETPACK__PLUGIN_DIR . 'extensions/index.json';
[2109] Fix | Delete
$block_list = $wp_filesystem->get_contents( $block_list_path );
[2110] Fix | Delete
if ( empty( $block_list ) ) {
[2111] Fix | Delete
/* translators: %s is the path to the file with the block list */
[2112] Fix | Delete
WP_CLI::error( sprintf( esc_html__( 'Error fetching contents of %s', 'jetpack' ), $block_list_path ) );
[2113] Fix | Delete
} elseif ( false === stripos( $block_list, $slug ) ) {
[2114] Fix | Delete
$new_block_list = json_decode( $block_list );
[2115] Fix | Delete
$new_block_list->{ $variation }[] = $slug;
[2116] Fix | Delete
[2117] Fix | Delete
// Format the JSON to match our coding standards.
[2118] Fix | Delete
$new_block_list_formatted = wp_json_encode( $new_block_list, JSON_PRETTY_PRINT ) . "\n";
[2119] Fix | Delete
$new_block_list_formatted = preg_replace_callback(
[2120] Fix | Delete
// Find all occurrences of multiples of 4 spaces a the start of the line.
[2121] Fix | Delete
'/^((?: )+)/m',
[2122] Fix | Delete
function ( $matches ) {
[2123] Fix | Delete
// Replace each occurrence of 4 spaces with a tab character.
[2124] Fix | Delete
return str_repeat( "\t", substr_count( $matches[0], ' ' ) );
[2125] Fix | Delete
},
[2126] Fix | Delete
$new_block_list_formatted
[2127] Fix | Delete
);
[2128] Fix | Delete
[2129] Fix | Delete
if ( ! $wp_filesystem->put_contents( $block_list_path, $new_block_list_formatted ) ) {
[2130] Fix | Delete
/* translators: %s is the path to the file with the block list */
[2131] Fix | Delete
WP_CLI::error( sprintf( esc_html__( 'Error writing new %s', 'jetpack' ), $block_list_path ) );
[2132] Fix | Delete
}
[2133] Fix | Delete
}
[2134] Fix | Delete
[2135] Fix | Delete
if ( 'beta' === $variation || 'experimental' === $variation ) {
[2136] Fix | Delete
$block_constant = sprintf(
[2137] Fix | Delete
/* translators: the placeholder is a constant name */
[2138] Fix | Delete
esc_html__( 'To load the block, add the constant JETPACK_BLOCKS_VARIATION set to %1$s to your wp-config.php file', 'jetpack' ),
[2139] Fix | Delete
$variation
[2140] Fix | Delete
);
[2141] Fix | Delete
} else {
[2142] Fix | Delete
$block_constant = '';
[2143] Fix | Delete
}
[2144] Fix | Delete
[2145] Fix | Delete
WP_CLI::success(
[2146] Fix | Delete
sprintf(
[2147] Fix | Delete
/* translators: the placeholders are a human readable title, and a series of words separated by dashes */
[2148] Fix | Delete
esc_html__( 'Successfully created block %1$s with slug %2$s', 'jetpack' ) . ' 🎉' . "\n" .
[2149] Fix | Delete
"--------------------------------------------------------------------------------------------------------------------\n" .
[2150] Fix | Delete
/* translators: the placeholder is a directory path */
[2151] Fix | Delete
esc_html__( 'The files were created at %3$s', 'jetpack' ) . "\n" .
[2152] Fix | Delete
esc_html__( 'To start using the block, build the blocks with pnpm run build-extensions', 'jetpack' ) . "\n" .
[2153] Fix | Delete
/* translators: the placeholder is a file path */
[2154] Fix | Delete
esc_html__( 'The block slug has been added to the %4$s list at %5$s', 'jetpack' ) . "\n" .
[2155] Fix | Delete
'%6$s' . "\n" .
[2156] Fix | Delete
/* translators: the placeholder is a URL */
[2157] Fix | Delete
"\n" . esc_html__( 'Read more at %7$s', 'jetpack' ) . "\n",
[2158] Fix | Delete
$title,
[2159] Fix | Delete
$slug,
[2160] Fix | Delete
$path,
[2161] Fix | Delete
$variation,
[2162] Fix | Delete
$block_list_path,
[2163] Fix | Delete
$block_constant,
[2164] Fix | Delete
'https://github.com/Automattic/jetpack/blob/trunk/projects/plugins/jetpack/extensions/README.md#developing-block-editor-extensions-in-jetpack'
[2165] Fix | Delete
) . '--------------------------------------------------------------------------------------------------------------------'
[2166] Fix | Delete
);
[2167] Fix | Delete
}
[2168] Fix | Delete
}
[2169] Fix | Delete
[2170] Fix | Delete
/**
[2171] Fix | Delete
* Built the file replacing the placeholders in the template with the data supplied.
[2172] Fix | Delete
*
[2173] Fix | Delete
* @param string $template Template.
[2174] Fix | Delete
* @param array $data Data.
[2175] Fix | Delete
* @return string mixed
[2176] Fix | Delete
*/
[2177] Fix | Delete
private static function render_block_file( $template, $data = array() ) {
[2178] Fix | Delete
return \WP_CLI\Utils\mustache_render( JETPACK__PLUGIN_DIR . "wp-cli-templates/$template.mustache", $data );
[2179] Fix | Delete
}
[2180] Fix | Delete
}
[2181] Fix | Delete
[2182] Fix | Delete
// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move these functions to some other file.
[2183] Fix | Delete
[2184] Fix | Delete
/**
[2185] Fix | Delete
* Standard "ask for permission to continue" function.
[2186] Fix | Delete
* If action cancelled, ask if they need help.
[2187] Fix | Delete
*
[2188] Fix | Delete
* Written outside of the class so it's not listed as an executable command w/ 'wp jetpack'
[2189] Fix | Delete
*
[2190] Fix | Delete
* @param bool $flagged false = normal option | true = flagged by get_jetpack_options_for_reset().
[2191] Fix | Delete
* @param string $error_msg Error message.
[2192] Fix | Delete
*/
[2193] Fix | Delete
function jetpack_cli_are_you_sure( $flagged = false, $error_msg = false ) {
[2194] Fix | Delete
$cli = new Jetpack_CLI();
[2195] Fix | Delete
[2196] Fix | Delete
// Default cancellation message.
[2197] Fix | Delete
if ( ! $error_msg ) {
[2198] Fix | Delete
$error_msg =
[2199] Fix | Delete
__( 'Action cancelled. Have a question?', 'jetpack' )
[2200] Fix | Delete
. ' '
[2201] Fix | Delete
. $cli->green_open
[2202] Fix | Delete
. 'jetpack.com/support'
[2203] Fix | Delete
. $cli->color_close;
[2204] Fix | Delete
}
[2205] Fix | Delete
[2206] Fix | Delete
if ( ! $flagged ) {
[2207] Fix | Delete
$prompt_message = _x( 'Are you sure? This cannot be undone. Type "yes" to continue:', '"yes" is a command - do not translate.', 'jetpack' );
[2208] Fix | Delete
} else {
[2209] Fix | Delete
$prompt_message = _x( 'Are you sure? Modifying this option may disrupt your Jetpack connection. Type "yes" to continue.', '"yes" is a command - do not translate.', 'jetpack' );
[2210] Fix | Delete
}
[2211] Fix | Delete
[2212] Fix | Delete
WP_CLI::line( $prompt_message );
[2213] Fix | Delete
$handle = fopen( 'php://stdin', 'r' );
[2214] Fix | Delete
$line = fgets( $handle );
[2215] Fix | Delete
if ( 'yes' !== trim( $line ) ) {
[2216] Fix | Delete
WP_CLI::error( $error_msg );
[2217] Fix | Delete
}
[2218] Fix | Delete
}
[2219] Fix | Delete
[2220] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function