entries(); // Resolve and check every target against the snapshot first. $targets = array(); foreach ( $operations as $position => $operation ) { $entry = self::target( $entries, $operation, $position ); if ( is_wp_error( $entry ) ) { return $entry; } $targets[ $position ] = $entry; } // Then work out every change, still without touching the tree: // replacements on a working copy of each block, in order, so two // phrases in one block both apply; inserts collected by target. $working = array(); $inserts = array(); foreach ( $operations as $position => $operation ) { $entry = $targets[ $position ]; $path = $entry['path']; if ( 'insert_after' === $operation['op'] ) { // Blocks copied from another post of the site arrive parsed; // blocks Blaze wrote are checked and cleaned. $blocks = isset( $operation['parsed'] ) ? $operation['parsed'] : NewBlocks::prepare( isset( $operation['blocks'] ) ? $operation['blocks'] : array() ); if ( is_wp_error( $blocks ) ) { return self::with_position( $blocks, $position ); } $inserts[ $path ]['indices'] = $entry['indices']; $inserts[ $path ]['blocks'] = array_merge( isset( $inserts[ $path ]['blocks'] ) ? $inserts[ $path ]['blocks'] : array(), $blocks ); $inserts[ $path ]['ops'][] = array( 'position' => $position, 'count' => count( $blocks ), ); continue; } $block = isset( $working[ $path ] ) ? $working[ $path ] : $entry['block']; $changed = 'replace_phrase' === $operation['op'] ? self::replace_phrase( $block, $operation ) : self::replace_text( $block, $operation ); if ( is_wp_error( $changed ) ) { return self::with_position( $changed, $position ); } $working[ $path ] = $changed; }//end foreach // Apply. Replacements don't move anything. Inserts go deepest first // and, within one parent, last first, so no insert shifts the // array indices of another target. foreach ( $working as $path => $block ) { $target = &$tree->block_at( $entries[ $path ]['indices'] ); $target = $block; unset( $target ); } uasort( $inserts, static function ( $a, $b ) { $depth = count( $b['indices'] ) - count( $a['indices'] ); return 0 !== $depth ? $depth : end( $b['indices'] ) - end( $a['indices'] ); } ); foreach ( $inserts as $insert ) { $tree->insert_after( $insert['indices'], $insert['blocks'] ); } return self::results( $operations, $targets, $inserts ); } /** * The snapshot entry an operation targets, after checking it may. * * @param array[] $entries Snapshot entries keyed by path. * @param array $operation The operation. * @param int $position Its position in the request. * @return array|\WP_Error */ private static function target( array $entries, array $operation, $position ) { $path = isset( $operation['path'] ) ? (string) $operation['path'] : ''; if ( ! isset( $entries[ $path ] ) ) { return self::refusal( 'searchsmith_path_not_found', __( 'No block at this path.', 'searchsmith' ), $position, 422, array( 'path' => $path ) ); } $entry = $entries[ $path ]; $hash = isset( $operation['hash'] ) ? (string) $operation['hash'] : ''; if ( ! hash_equals( Tree::hash( $entry['block'] ), $hash ) ) { return self::refusal( 'searchsmith_block_changed', __( 'The block changed since it was read.', 'searchsmith' ), $position, 409, array( 'path' => $path, 'hash' => Tree::hash( $entry['block'] ), ) ); } if ( 'insert_after' === $operation['op'] ) { if ( ! $entry['insertable'] ) { return self::refusal( 'searchsmith_cannot_insert_here', __( 'Blocks cannot be inserted after this block.', 'searchsmith' ), $position, 422, array( 'path' => $path ) ); } return $entry; } if ( ! $entry['editable'] ) { return self::refusal( 'searchsmith_block_not_editable', __( 'This block cannot be changed.', 'searchsmith' ), $position, 422, array( 'path' => $path, 'reason' => $entry['reason'], ) ); } return $entry; } /** * Replaces one occurrence of a phrase in a block. * * @param array $block Parsed block. * @param array $operation The operation. * @return array|\WP_Error The changed block. */ private static function replace_phrase( array $block, array $operation ) { $replacement = isset( $operation['replacement'] ) ? (string) $operation['replacement'] : ''; if ( Text::has_registered_shortcode( $replacement ) ) { return new \WP_Error( 'searchsmith_shortcode_in_text', __( 'The new text may not contain a shortcode.', 'searchsmith' ), array( 'status' => 422 ) ); } $found = Text::find_phrase( Tree::own_parts( $block ), isset( $operation['phrase'] ) ? (string) $operation['phrase'] : '' ); if ( is_wp_error( $found ) ) { return $found; } $part = $block['innerContent'][ $found['part'] ]; $block['innerContent'][ $found['part'] ] = substr( $part, 0, $found['start'] ) . Text::escape( $replacement ) . substr( $part, $found['end'] ); return self::with_inner_html( $block ); } /** * Replaces all the text of a paragraph, heading or list item. * * @param array $block Parsed block. * @param array $operation The operation. * @return array|\WP_Error The changed block. */ private static function replace_text( array $block, array $operation ) { $text = isset( $operation['text'] ) ? (string) $operation['text'] : ''; if ( '' === trim( $text ) ) { return new \WP_Error( 'searchsmith_empty_text', __( 'The new text is empty.', 'searchsmith' ), array( 'status' => 400 ) ); } if ( Text::has_registered_shortcode( $text ) ) { return new \WP_Error( 'searchsmith_shortcode_in_text', __( 'The new text may not contain a shortcode.', 'searchsmith' ), array( 'status' => 422 ) ); } $html = Tree::own_html( $block ); if ( ! in_array( $block['blockName'], self::TEXT_REPLACEABLE, true ) || ! empty( $block['innerBlocks'] ) || 1 === preg_match( '/<(a|img|picture|video|audio|iframe|svg|object|embed)\b/i', $html ) ) { return new \WP_Error( 'searchsmith_text_not_replaceable', __( 'Only a paragraph, heading or list item with no link, image or shortcode can have all its text replaced.', 'searchsmith' ), array( 'status' => 422 ) ); } if ( 1 !== count( $block['innerContent'] ) || ! preg_match( '#^(\s*<(p|h[1-6]|li)\b[^>]*>)(.*)(\s*)$#is', (string) $block['innerContent'][0], $matches ) ) { return new \WP_Error( 'searchsmith_unexpected_markup', __( 'The block markup is not a single paragraph, heading or list item.', 'searchsmith' ), array( 'status' => 422 ) ); } $block['innerContent'][0] = $matches[1] . Text::escape( $text ) . $matches[4]; return self::with_inner_html( $block ); } /** * Recomputes a block's `innerHTML` from its `innerContent`. * * @param array $block Parsed block. * @return array */ private static function with_inner_html( array $block ) { $block['innerHTML'] = Tree::own_html( $block ); return $block; } /** * One result per operation. An insert reports the paths its new blocks * have in the post as saved. * * @param array[] $operations The operations. * @param array[] $targets Their snapshot entries. * @param array[] $inserts Inserts by target path. * @return array[] */ private static function results( array $operations, array $targets, array $inserts ) { // How many blocks go after each block, by the parent's original path. $added = array(); foreach ( $inserts as $path => $insert ) { $parts = explode( '.', (string) $path ); $index = (int) array_pop( $parts ); $added[ implode( '.', $parts ) ][ $index ] = count( $insert['blocks'] ); } $results = array(); foreach ( $operations as $position => $operation ) { $path = $targets[ $position ]['path']; $result = array( 'op' => $operation['op'], 'path' => $path, ); if ( 'insert_after' === $operation['op'] ) { $result['inserted_paths'] = self::inserted_paths( $path, $position, $inserts[ $path ]['ops'], $added ); } $results[ $position ] = $result; } return $results; } /** * The paths one insert's new blocks have after every insert. * * @param string $path The insert's target path in the snapshot. * @param int $position The insert's position in the request. * @param array[] $ops Every insert after that target, in order. * @param array $added Inserted block counts by parent path and sibling index. * @return string[] */ private static function inserted_paths( $path, $position, array $ops, array $added ) { $parent = self::moved( explode( '.', $path ), $added ); $next = (int) array_pop( $parent ) + 1; $paths = array(); foreach ( $ops as $op ) { if ( $op['position'] === $position ) { for ( $i = 0; $i < $op['count']; $i++ ) { $paths[] = implode( '.', array_merge( $parent, array( (string) ( $next + $i ) ) ) ); } break; } $next += $op['count']; } return $paths; } /** * Where a block of the snapshot sits after the inserts. * * @param string[] $parts Path parts in the snapshot. * @param array $added Inserted block counts by parent path and sibling index. * @return string[] Path parts after the inserts. */ private static function moved( array $parts, array $added ) { $moved = array(); foreach ( $parts as $depth => $part ) { $parent = implode( '.', array_slice( $parts, 0, $depth ) ); $index = (int) $part; $shift = 0; if ( isset( $added[ $parent ] ) ) { foreach ( $added[ $parent ] as $sibling => $count ) { if ( $sibling < $index ) { $shift += $count; } } } $moved[] = (string) ( $index + $shift ); } return $moved; } /** * Builds a refusal naming the operation it concerns. * * @param string $code Error code. * @param string $message Message. * @param int $position The operation's position in the request. * @param int $status HTTP status. * @param array $data Extra data. * @return \WP_Error */ private static function refusal( $code, $message, $position, $status, array $data = array() ) { return new \WP_Error( $code, $message, array_merge( array( 'status' => $status ), $data, array( 'operation' => $position ) ) ); } /** * Adds the operation's position to an error. * * @param \WP_Error $error The error. * @param int $position The operation's position in the request. * @return \WP_Error */ private static function with_position( \WP_Error $error, $position ) { $data = (array) $error->get_error_data(); return new \WP_Error( $error->get_error_code(), $error->get_error_message(), array_merge( $data, array( 'operation' => $position ) ) ); } }