使WordPress成为核心

变更集54130


忽略:
时间戳:
2022年11月9日晚上10:33:29(20个月(之前)
作者:
蚂蚁党
消息:

Autosave/REST API:锁定编辑时,阻止自动保存覆盖更改。

以前,当用户被禁止在块编辑器中编辑帖子时,自动保存功能被允许覆盖已获得控制权的编辑器所做的更改。此修补程序支持锁定状态,防止自动保存与其他内容编辑器冲突。

Props jhart35、adamsilverstein、sathapulse、chanthaboune、primetimejas、joemgill、kadamwhite。
修复#55659.

位置:
大旅行箱
文件夹:
2已编辑

图例:

未修改
补充
远离的
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php

    51962兰特 54130兰特  
    221221$用户id=获取当前用户id();
    222222
    223 if(('draft'===$post->post_status||'auto-draft'==$post->post-status)&&$post->post_author==$user_id){
     223//我们需要检查邮件锁定,以确保原始作者没有打开浏览器选项卡。
     224if(!function_exists('wp_check_post_lock')){
     225需要一次ABSPATH。”wp-admin/includes/post.php';
     226        }
     227
     228$post_lock=wp_check_post_lock($post->ID);
     229$is_draft='draft'===$post->post_status||'auto-draft'===$post->post_status;
     230
     231if($isdraft&&(int)$post->postaauthor===$userid&&$post_lock){
    224232//为同一作者起草文章:自动保存会更新文章,而不会创建修订。
    225233//将post对象转换为数组并添加斜杠,wp_update_post()需要转义数组。
  • trunk/tests/phpunit/tests/rest-api/rest-autosaves-controller.php

    54058兰特 54130卢比  
    608608$this->assertNotEquals(“垃圾”,get_post(self::$draft_page_id)->comment_status);
    609609    }
     610
     611    /**
     612*测试确保原始作者的autosave在被第二作者接管后不会覆盖更改。
     613     *
     614*@门票55659
     615     */
     616公共函数test_rest_autosave_draft_post_locked_to_different_author(){
     617
     618//由编辑创建帖子。
     619$post_data=数组(
     620“post_content”=>“测试帖子内容”,
     621“post_title”=>“测试文章标题”,
     622“post_excerpt”=>“测试后摘录”,
     623“post_author”=>自我::$editor_id,
     624“post_status”=>“草稿”,
     625        );
     626$post_id=wp_insert_post($post_data);
     627
     628//将post锁设置为contributor,模拟接管post。
     629wp_set_current_user(自身::$contribor_id);
     630wp_set_post_lock($post_id);
     631
     632//使用参与者的新数据更新帖子。
     633$updated_post_data=数组(
     634“ID”=>$post_ID,
     635“post_content”=>“来自参与者的新帖子内容”,
     636“post_title”=>“新建帖子标题”,
     637        );
     638wp_update_post($更新后数据);
     639
     640//将当前用户设置为编辑器,并使用一些新数据启动自动保存。
     641wp_set_current_user(自身::$editor_id);
     642$autosave_data=数组(
     643'id'=>$post_id,
     644“content”=>“更新的文章内容”,
     645“摘录”=>“要测试的新摘录”,
     646'title'=>$post_data['post_title'],
     647        );
     648
     649//像古腾堡那样,通过REST API启动自动保存。
     650$request=新的WP_REST_request('POST','/WP/v2/posts/'.self::$POST_id.'/autosaves');
     651$request->add_header('content-type','application/json');
     652$request->set_body(wp_json_encode($autosave_data));
     653
     654$response=rest_get_server()->分派($request);
     655$new_data=$response->get_data();
     656
     657//测试帖子的当前版本。
     658$current_post=获取post($post_id);
     659
     660//来自autosave的新数据应将其父ID设置为原始post ID。
     661$this->assertSame($post_id,$new_data['parent']);
     662
     663//文章标题和内容仍然应该是作者的更新版本。
     664$this->assertSame($current_post->post_title,$updated_post_data['post_title']);
     665$this->assertSame($current_post->post_content,$updated_post_data['post_content']);
     666
     667//摘录本应保持不变。
     668$this->assertSame($current_post->post_excerpt,$post_data['post_excherpt']);
     669
     670$autosave_post=wp_get_post_autosave($post_id);
     671
     672//有更改。
     673$this->assertSame($autosave_data['content'],$autosave post->post_content);
     674
     675wp_delete_post($post_id);
     676    }
    610677}
注:请参见TracChangeset(跟踪变更集)获取有关使用变更集查看器的帮助。