store = $store ? $store : new WpChangeStore(); $this->stdin = $stdin; } /** * Clear the change rows that a static build used, and add rows for the URLs to try again. * * ## OPTIONS * * [--file=] * : The change file that the engine wrote, or "-" for STDIN. Necessary. * * ## EXAMPLES * * wp flywp static changes clear --file=- < changes.json * * @param array $args Positional arguments. * @param array $assoc_args Named arguments. * * @return void */ public function __invoke( $args, $assoc_args ) { $this->execute( $assoc_args ); } /** * {@inheritdoc} */ public function run( array $assoc_args ) { $file = isset( $assoc_args['file'] ) ? (string) $assoc_args['file'] : ''; $changes = json_decode( $this->read( $file ), true ); if ( ! is_array( $changes ) || ! isset( $changes['max_change_id'] ) || ! is_int( $changes['max_change_id'] ) ) { return $this->result( sprintf( 'Unable to read the change file "%s".', $file ) ); } $errors = []; foreach ( isset( $changes['retry_urls'] ) ? (array) $changes['retry_urls'] : [] as $url ) { if ( ! $this->store->insert( 'url', null, null, (string) $url, 'build_error' ) ) { $errors[] = 'Some failed URLs were not added to the change log. The next incremental build does not try them again.'; break; } } if ( $changes['max_change_id'] > 0 && ! $this->store->clear_up_to( $changes['max_change_id'] ) ) { $errors[] = sprintf( 'The change log was not cleared up to row %d. The next incremental build does these changes again.', $changes['max_change_id'] ); } return $this->result( $errors ? implode( ' ', $errors ) : null ); } /** * Read the change file. The FlyWP deploy script sends it through STDIN: the build folder is on the host, * and the site container has no mount of it. * * @param string $file A path, or "-" for STDIN. * * @return string The file content, or an empty string when the file cannot be read. */ private function read( $file ) { if ( $file === '-' ) { return (string) file_get_contents( $this->stdin ); } return $file !== '' && is_readable( $file ) ? (string) file_get_contents( $file ) : ''; } }