ID === $post->ID ) { return self::refusal( 'searchsmith_same_post', __( 'Blocks can only be copied from another post.', 'searchsmith' ) ); } if ( 'trash' === $source->post_status ) { return new \WP_Error( 'searchsmith_trashed', __( 'The source post is in the trash.', 'searchsmith' ), array( 'status' => 409 ) ); } $expected = Guard::parse_gmt( (string) $request['source_expected_modified_gmt'] ); if ( null === $expected ) { return new \WP_Error( 'searchsmith_invalid_version', __( 'source_expected_modified_gmt is not a valid date.', 'searchsmith' ), array( 'status' => 400 ) ); } if ( ContentEditor::modified_mysql( $source ) !== $expected ) { return new \WP_Error( 'searchsmith_source_stale', __( 'The source post changed since it was read.', 'searchsmith' ), array( 'status' => 409, 'modified_gmt' => ContentEditor::modified( $source ), ) ); } $tree = new Tree( $source->post_content ); if ( Environment::builder_for_post( $source->ID ) || $tree->is_classic() || ! $tree->round_trips() ) { return self::refusal( 'searchsmith_source_not_copyable', __( 'Blocks cannot be copied exactly from the source post.', 'searchsmith' ) ); } $requested = is_array( $request['blocks'] ) ? array_values( $request['blocks'] ) : array(); if ( ! $requested || count( $requested ) > self::MAX_BLOCKS ) { return new \WP_Error( 'searchsmith_invalid_blocks', __( 'blocks must list between 1 and 100 source blocks.', 'searchsmith' ), array( 'status' => 400 ) ); } $entries = $tree->entries(); $paths = array(); $parsed = array(); foreach ( $requested as $position => $wanted ) { $path = is_array( $wanted ) && isset( $wanted['path'] ) ? (string) $wanted['path'] : ''; $hash = is_array( $wanted ) && isset( $wanted['hash'] ) ? (string) $wanted['hash'] : ''; if ( ! isset( $entries[ $path ] ) ) { return self::refusal( 'searchsmith_path_not_found', __( 'No block at this path in the source post.', 'searchsmith' ), array( 'block' => $position ) ); } if ( ! hash_equals( Tree::hash( $entries[ $path ]['block'] ), $hash ) ) { return new \WP_Error( 'searchsmith_block_changed', __( 'A source block changed since it was read.', 'searchsmith' ), array( 'status' => 409, 'block' => $position, ) ); } foreach ( $paths as $other ) { if ( 0 === strpos( $path . '.', $other . '.' ) || 0 === strpos( $other . '.', $path . '.' ) ) { return self::refusal( 'searchsmith_overlapping_blocks', __( 'A block and a block inside it cannot both be copied.', 'searchsmith' ), array( 'block' => $position ) ); } } $paths[] = $path; $parsed[] = $entries[ $path ]['block']; }//end foreach return ContentOperations::perform( $post, $request, array( array( 'op' => 'insert_after', 'path' => (string) $request['after'], 'hash' => (string) $request['after_hash'], 'parsed' => $parsed, ), ), 'copy_blocks', array( 'source_id' => $source->ID, 'source_paths' => $paths, ) ); } /** * A refusal. * * @param string $code Error code. * @param string $message Message. * @param array $data Extra data. * @return \WP_Error */ private static function refusal( $code, $message, array $data = array() ) { return new \WP_Error( $code, $message, array_merge( array( 'status' => 422 ), $data ) ); } }