{"action":"created","issue":{"url":"https://api.github.com/repos/siteorigin/siteorigin-panels/issues/505","repository_url":"https://api.github.com/repos/siteorigin/siteorigin-panels","labels_url":"https://api.github.com/repos/siteorigin/siteorigin-panels/issues/505/labels{/name}","comments_url":"https://api.github.com/repos/siteorigin/siteorigin-panels/issues/505/comments","events_url":"https://api.github.com/repos/siteorigin/siteorigin-panels/issues/505/events","html_url":"https://github.com/siteorigin/siteorigin-panels/issues/505","id":289352342,"number":505,"title":"Preview bugged for post creator when there is an autosave from another user","user":{"login":"Sharsie","id":17074367,"avatar_url":"https://avatars3.githubusercontent.com/u/17074367?v=4","gravatar_id":"","url":"https://api.github.com/users/Sharsie","html_url":"https://github.com/Sharsie","followers_url":"https://api.github.com/users/Sharsie/followers","following_url":"https://api.github.com/users/Sharsie/following{/other_user}","gists_url":"https://api.github.com/users/Sharsie/gists{/gist_id}","starred_url":"https://api.github.com/users/Sharsie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Sharsie/subscriptions","organizations_url":"https://api.github.com/users/Sharsie/orgs","repos_url":"https://api.github.com/users/Sharsie/repos","events_url":"https://api.github.com/users/Sharsie/events{/privacy}","received_events_url":"https://api.github.com/users/Sharsie/received_events","type":"User","site_admin":false},"labels":[{"id":156138835,"url":"https://api.github.com/repos/siteorigin/siteorigin-panels/labels/bug","name":"bug","color":"fc2929","default":true},{"id":306579497,"url":"https://api.github.com/repos/siteorigin/siteorigin-panels/labels/priority-2","name":"priority-2","color":"fbca04","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2018-01-17T17:37:38Z","updated_at":"2018-05-04T14:03:59Z","closed_at":null,"author_association":"NONE","body":"If there are multiple users working on a post and one of them creates an autosave (and then for example closes the window so the lock expires), the other user (not sure if it is always the owner of the post) will always be presented with an autosave of the other user.\r\n\r\n**How to reproduce:**\r\n1) Create a post with user A, add site origin editor and add some content\r\n2) Save draft, Log out, delete lock from DB in wp_postmeta (emulates user closing the window and lock expiring)\r\n3) Log in as user B\r\n4) Edit the post changing some of the contents, click Preview to generate an autosave\r\n5) Log out, delete lock from DB\r\n6) Log in as user A, edit the post\r\n7) Change the contents\r\n8) Click preview and you should be presented with the user B's version of the autosave\r\n\r\nEven if you save the post as draft and then preview it, you will again be presented with B's autosave\r\n\r\n**Possible solution:**\r\nhttps://github.com/siteorigin/siteorigin-panels/blob/develop/siteorigin-panels.php#L279\r\n```diff\r\n\t\tif ( $preview ) {\r\n-\t\t\t$preview_post = wp_get_post_autosave( $post_id );\r\n+\t\t\t$preview_post = wp_get_post_autosave( $post_id, get_current_user_id() );\r\n\t\t\tif ( ! empty( $preview_post ) ) {\r\n\t\t\t\t$post_id = $preview_post->ID;\r\n\t\t\t}\r\n\t\t}\r\n```\r\n\r\nBecause there is a check in `wp_get_post_autosave` whether the autosave belongs to the user previewing the post, it will retrieve the first autosave entry no matter if it belongs to the current user or not. This post is then used inside the `generate_post_content` function and the content is generated from the autosave instead of the latest version\r\n"},"comment":{"url":"https://api.github.com/repos/siteorigin/siteorigin-panels/issues/comments/386611547","html_url":"https://github.com/siteorigin/siteorigin-panels/issues/505#issuecomment-386611547","issue_url":"https://api.github.com/repos/siteorigin/siteorigin-panels/issues/505","id":386611547,"user":{"login":"Sharsie","id":17074367,"avatar_url":"https://avatars3.githubusercontent.com/u/17074367?v=4","gravatar_id":"","url":"https://api.github.com/users/Sharsie","html_url":"https://github.com/Sharsie","followers_url":"https://api.github.com/users/Sharsie/followers","following_url":"https://api.github.com/users/Sharsie/following{/other_user}","gists_url":"https://api.github.com/users/Sharsie/gists{/gist_id}","starred_url":"https://api.github.com/users/Sharsie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Sharsie/subscriptions","organizations_url":"https://api.github.com/users/Sharsie/orgs","repos_url":"https://api.github.com/users/Sharsie/repos","events_url":"https://api.github.com/users/Sharsie/events{/privacy}","received_events_url":"https://api.github.com/users/Sharsie/received_events","type":"User","site_admin":false},"created_at":"2018-05-04T14:03:59Z","updated_at":"2018-05-04T14:03:59Z","author_association":"NONE","body":"Hello guys, so far I am testing it with a filter hook outside of the plugin. It seems to do what we want for now:\r\n\r\n```php\r\nadd_filter(\"siteorigin_panels_filter_content_enabled\", function() {\r\n        global $preview;\r\n        if ($preview) {\r\n            $previewPost = wp_get_post_autosave( get_the_ID(), get_current_user_id() );\r\n\r\n            if (!empty($previewPost)) {\r\n                $autosaveDate = \\date_create($previewPost->post_modified_gmt);\r\n                $parent = get_post($previewPost->post_parent);\r\n                $parentDate = \\date_create($parent->post_modified_gmt);\r\n\r\n                if ($autosaveDate < $parentDate) {\r\n                    \\wp_delete_post($previewPost->ID);\r\n                }\r\n            }\r\n        }\r\n\r\n        return $status;\r\n});\r\n```\r\n\r\nSomething along these lines could be a solution (really needs a doublecheck and maybe optimalization, just wrote it without thinking really :)\r\nThe empty check for parent post might not be needed, not sure, needs testing (e.g. autosave of a new post)\r\n\r\n```diff\r\n\t\tif ( $preview ) {\r\n\t\t\t$preview_post = wp_get_post_autosave( $post_id, get_current_user_id() );\r\n\t\t\tif ( ! empty( $preview_post ) ) {\r\n+\t\t\t    $parentPost = get_post($preview_post->post_parent);\r\n+\t\t\t    if ( ! empty( $parentPost ) ) {\r\n+                                $autosaveDate = date_create($preview_post->post_modified_gmt);\r\n+                                $parentDate = date_create($parentPost->post_modified_gmt);\r\n+                                if ($parentDate && $autosaveDate && $autosaveDate > $parentDate) {\r\n+                                     $post_id = $preview_post->ID;\r\n+                                 }\r\n+                             } else {\r\n                                 $post_id = $preview_post->ID;\r\n+                             }\r\n\t\t\t}\r\n\t\t}\r\n```"}}
{"actor":{"display_login":"Sharsie"}}