$operation ) { if ( 'set_title' === $operation['op'] ) { $title = $operation['title']; } else { $content_ops[ $position ] = $operation; } } $update = array( 'ID' => $post->ID ); if ( null !== $title ) { $update['post_title'] = $title; } $results = array(); if ( $content_ops ) { $refusal = Guard::builder_refusal( $post ); if ( $refusal ) { return $refusal; } $tree = new Tree( $post->post_content ); $reason = Snapshot::post_reason( $post, $tree ); if ( null !== $reason ) { return new \WP_Error( 'searchsmith_content_not_editable', __( 'The content of this post cannot be changed by Blaze.', 'searchsmith' ), array( 'status' => 422, 'reason' => $reason, ) ); } $results = Operations::apply( $tree, $content_ops ); if ( is_wp_error( $results ) ) { return $results; } $update['post_content'] = $tree->serialize(); }//end if foreach ( $operations as $position => $operation ) { if ( ! isset( $results[ $position ] ) ) { $results[ $position ] = array( 'op' => $operation['op'] ); } } ksort( $results ); // Keep the post as it is now as a revision, outside the labelled // save, so restoring this change has something to restore. $before_revision_id = Revisions::ensure_current( $post ); list( $saved, $revision_ids ) = RevisionLabeler::during( (string) $request['change_id'], static function () use ( $update ) { return Guard::without_content_kses( static function () use ( $update ) { // wp_update_post() unslashes its input, so slash it first. return wp_update_post( wp_slash( $update ), true ); } ); } ); if ( is_wp_error( $saved ) ) { return $saved; } CachePurger::purge_post( $post->ID ); $after = get_post( $post->ID ); $result = array( 'id' => $after->ID, 'modified_gmt' => ContentEditor::modified( $after ), 'change_id' => (string) $request['change_id'], 'op_key' => (string) $request['op_key'], 'revision_ids' => $revision_ids, 'before_revision_id' => $before_revision_id, 'operations' => array_values( $results ), ) + $extra; OpKeys::record( $request['op_key'], $kind, $post->ID, $revision_ids ? $revision_ids[0] : $post->ID, $result ); return $result; } /** * Checks the operations' shape and returns them with `op` set. * * @param mixed $operations The `operations` parameter. * @return array[]|\WP_Error */ private static function normalize( $operations ) { if ( ! is_array( $operations ) || ! $operations || ! wp_is_numeric_array( $operations ) ) { return self::invalid( __( 'operations must be a non-empty list.', 'searchsmith' ) ); } if ( count( $operations ) > self::MAX_OPERATIONS ) { return self::invalid( __( 'Too many operations in one request.', 'searchsmith' ) ); } $normalized = array(); $titles = 0; foreach ( array_values( $operations ) as $position => $operation ) { if ( ! is_array( $operation ) ) { return self::invalid( __( 'Each operation must be an object.', 'searchsmith' ), $position ); } // `type` is accepted as another name for `op`. if ( ! isset( $operation['op'] ) && isset( $operation['type'] ) ) { $operation['op'] = $operation['type']; } $op = isset( $operation['op'] ) ? (string) $operation['op'] : ''; if ( ! in_array( $op, self::OPS, true ) ) { return self::invalid( __( 'Unknown operation.', 'searchsmith' ), $position ); } $operation['op'] = $op; $required = array( 'set_title' => array( 'title' ), 'replace_phrase' => array( 'path', 'hash', 'phrase', 'replacement' ), 'replace_text' => array( 'path', 'hash', 'text' ), 'insert_after' => array( 'path', 'hash', 'blocks' ), ); foreach ( $required[ $op ] as $field ) { if ( ! isset( $operation[ $field ] ) ) { /* translators: 1: a field name, 2: an operation name. */ return self::invalid( sprintf( __( '%1$s is required for %2$s.', 'searchsmith' ), $field, $op ), $position ); } if ( 'blocks' !== $field && ! is_scalar( $operation[ $field ] ) ) { /* translators: %s: a field name. */ return self::invalid( sprintf( __( '%s must be a string.', 'searchsmith' ), $field ), $position ); } } if ( 'set_title' === $op ) { ++$titles; $operation['title'] = (string) $operation['title']; if ( '' === trim( $operation['title'] ) ) { return self::invalid( __( 'The title is empty.', 'searchsmith' ), $position ); } } $normalized[ $position ] = $operation; }//end foreach if ( $titles > 1 ) { return self::invalid( __( 'Only one set_title per request.', 'searchsmith' ) ); } return $normalized; } /** * A malformed request. * * @param string $message Message. * @param int|null $position The operation's position, if one is at fault. * @return \WP_Error */ private static function invalid( $message, $position = null ) { $data = array( 'status' => 400 ); if ( null !== $position ) { $data['operation'] = $position; } return new \WP_Error( 'searchsmith_invalid_operations', $message, $data ); } }