Skip to content

Commit

Permalink
馃悰 FIX: Gutenberg POST Request Filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
asharirfan committed Apr 2, 2019
1 parent d90d1bd commit cf6b26f
Showing 1 changed file with 65 additions and 27 deletions.
92 changes: 65 additions & 27 deletions classes/Sensors/Content.php
Expand Up @@ -156,9 +156,22 @@ public function post_changed( $post_id, $post, $update ) {
* 1. Rest request from Gutenberg.
* 2. Classic editor request.
* 3. Quick edit ajax request.
*
* @since 3.4
*/
if ( ! isset( $_REQUEST['classic-editor'] ) && ! defined( 'REST_REQUEST' ) && ! defined( 'DOING_AJAX' ) ) {
return;
if ( ! defined( 'REST_REQUEST' ) && ! defined( 'DOING_AJAX' ) ) {
// Either Gutenberg's second post request or classic editor's request.
if ( ! isset( $_REQUEST['classic-editor'] ) ) {
$editor_replace = get_option( 'classic-editor-replace', 'classic' );
$allow_users = get_option( 'classic-editor-allow-users', 'disallow' );

// If block editor is selected and users are not allowed to switch editors then it is Gutenberg's second request.
if ( 'block' === $editor_replace && 'disallow' === $allow_users ) {
return;
} elseif ( 'allow' === $allow_users ) { // Else if users are allowed to switch then it is Gutenberg's second request.
return;
}
}
}

if ( $update ) {
Expand Down Expand Up @@ -271,7 +284,8 @@ public function event_post_trashed( $post_id ) {
$editor_link = $this->get_editor_link( $post );

$this->plugin->alerts->Trigger(
2012, array(
2012,
array(
'PostID' => $post->ID,
'PostType' => $post->post_type,
'PostTitle' => $post->post_title,
Expand Down Expand Up @@ -300,7 +314,8 @@ public function event_post_untrashed( $post_id ) {
$editor_link = $this->get_editor_link( $post );

$this->plugin->alerts->Trigger(
2014, array(
2014,
array(
'PostID' => $post->ID,
'PostType' => $post->post_type,
'PostTitle' => $post->post_title,
Expand All @@ -326,7 +341,8 @@ public function event_publish_future( $post_id ) {
$editor_link = $this->get_editor_link( $post );

$this->plugin->alerts->Trigger(
2001, array(
2001,
array(
'PostID' => $post->ID,
'PostType' => $post->post_type,
'PostTitle' => $post->post_title,
Expand Down Expand Up @@ -426,7 +442,8 @@ public function event_category_creation( $category_id ) {
$category = get_category( $category_id );
$category_link = $this->get_taxonomy_edit_link( $category_id, 'category' );
$this->plugin->alerts->Trigger(
2023, array(
2023,
array(
'CategoryName' => $category->name,
'Slug' => $category->slug,
'CategoryLink' => $category_link,
Expand All @@ -443,7 +460,8 @@ public function event_tag_creation( $tag_id ) {
$tag = get_tag( $tag_id );
$tag_link = $this->get_taxonomy_edit_link( $tag_id );
$this->plugin->alerts->Trigger(
2121, array(
2121,
array(
'TagName' => $tag->name,
'Slug' => $tag->slug,
'TagLink' => $tag_link,
Expand All @@ -461,7 +479,8 @@ public function check_taxonomy_term_deletion( $term_id, $taxonomy ) {
if ( 'post_tag' === $taxonomy ) {
$tag = get_tag( $term_id );
$this->plugin->alerts->Trigger(
2122, array(
2122,
array(
'TagID' => $term_id,
'TagName' => $tag->name,
'Slug' => $tag->slug,
Expand All @@ -471,7 +490,8 @@ public function check_taxonomy_term_deletion( $term_id, $taxonomy ) {
$category = get_category( $term_id );
$category_link = $this->get_taxonomy_edit_link( $term_id, $taxonomy );
$this->plugin->alerts->Trigger(
2024, array(
2024,
array(
'CategoryID' => $term_id,
'CategoryName' => $category->cat_name,
'Slug' => $category->slug,
Expand Down Expand Up @@ -509,7 +529,8 @@ public function event_update_term_data( $data, $term_id, $taxonomy, $args ) {
// Update if both names are not same.
if ( $old_name !== $new_name ) {
$this->plugin->alerts->Trigger(
2123, array(
2123,
array(
'old_name' => $old_name,
'new_name' => $new_name,
'TagLink' => $term_link,
Expand All @@ -520,7 +541,8 @@ public function event_update_term_data( $data, $term_id, $taxonomy, $args ) {
// Update if both slugs are not same.
if ( $old_slug !== $new_slug ) {
$this->plugin->alerts->Trigger(
2124, array(
2124,
array(
'tag' => $new_name,
'old_slug' => $old_slug,
'new_slug' => $new_slug,
Expand All @@ -532,7 +554,8 @@ public function event_update_term_data( $data, $term_id, $taxonomy, $args ) {
// Update if both descriptions are not same.
if ( $old_desc !== $new_desc ) {
$this->plugin->alerts->Trigger(
2125, array(
2125,
array(
'tag' => $new_name,
'TagLink' => $term_link,
'old_desc' => $old_desc,
Expand All @@ -545,7 +568,8 @@ public function event_update_term_data( $data, $term_id, $taxonomy, $args ) {
// Log event if both names are not same.
if ( $old_name !== $new_name ) {
$this->plugin->alerts->Trigger(
2127, array(
2127,
array(
'old_name' => $old_name,
'new_name' => $new_name,
'cat_link' => $term_link,
Expand All @@ -556,7 +580,8 @@ public function event_update_term_data( $data, $term_id, $taxonomy, $args ) {
// Log event if both slugs are not same.
if ( $old_slug !== $new_slug ) {
$this->plugin->alerts->Trigger(
2128, array(
2128,
array(
'CategoryName' => $new_name,
'old_slug' => $old_slug,
'new_slug' => $new_slug,
Expand All @@ -580,7 +605,8 @@ public function event_update_term_data( $data, $term_id, $taxonomy, $args ) {

if ( $old_parent_name !== $new_parent_name ) {
$this->plugin->alerts->Trigger(
2052, array(
2052,
array(
'CategoryName' => $new_name,
'OldParent' => $old_parent_name,
'NewParent' => $new_parent_name,
Expand Down Expand Up @@ -617,7 +643,8 @@ public function check_template_change( $meta_id, $post_id, $meta_key, $meta_valu
if ( $old_tmpl !== $new_tmpl ) {
$editor_link = $this->get_editor_link( $post );
$this->plugin->alerts->Trigger(
2048, array(
2048,
array(
'PostID' => $post->ID,
'PostType' => $post->post_type,
'PostTitle' => $post->post_title,
Expand Down Expand Up @@ -808,7 +835,8 @@ protected function check_author_change( $oldpost, $newpost ) {
$new_author = get_userdata( $newpost->post_author );
$new_author = ( is_object( $new_author ) ) ? $new_author->user_login : 'N/A';
$this->plugin->alerts->Trigger(
2019, array(
2019,
array(
'PostID' => $oldpost->ID,
'PostType' => $oldpost->post_type,
'PostTitle' => $oldpost->post_title,
Expand Down Expand Up @@ -882,7 +910,8 @@ protected function check_parent_change( $oldpost, $newpost ) {
if ( $oldpost->post_parent !== $newpost->post_parent && 'page' === $newpost->post_type ) {
$editor_link = $this->get_editor_link( $oldpost );
$this->plugin->alerts->Trigger(
2047, array(
2047,
array(
'PostID' => $oldpost->ID,
'PostType' => $oldpost->post_type,
'PostTitle' => $oldpost->post_title,
Expand Down Expand Up @@ -910,7 +939,8 @@ protected function check_permalink_change( $old_link, $new_link, $post ) {
if ( $old_link !== $new_link ) {
$editor_link = $this->get_editor_link( $post );
$this->plugin->alerts->Trigger(
2017, array(
2017,
array(
'PostID' => $post->ID,
'PostType' => $post->post_type,
'PostTitle' => $post->post_title,
Expand Down Expand Up @@ -962,7 +992,8 @@ protected function check_visibility_change( $oldpost, $newpost, $old_status, $ne
if ( $old_visibility && $new_visibility && ( $old_visibility !== $new_visibility ) ) {
$editor_link = $this->get_editor_link( $oldpost );
$this->plugin->alerts->Trigger(
2025, array(
2025,
array(
'PostID' => $oldpost->ID,
'PostType' => $oldpost->post_type,
'PostTitle' => $oldpost->post_title,
Expand Down Expand Up @@ -995,7 +1026,8 @@ protected function check_date_change( $oldpost, $newpost ) {
if ( $from !== $to ) {
$editor_link = $this->get_editor_link( $oldpost );
$this->plugin->alerts->Trigger(
2027, array(
2027,
array(
'PostID' => $oldpost->ID,
'PostType' => $oldpost->post_type,
'PostTitle' => $oldpost->post_title,
Expand Down Expand Up @@ -1033,7 +1065,8 @@ private function check_comments_pings( $oldpost, $newpost ) {
}

$this->plugin->alerts->Trigger(
$event, array(
$event,
array(
'Type' => $type,
'PostID' => $newpost->ID,
'PostType' => $newpost->post_type,
Expand All @@ -1058,7 +1091,8 @@ private function check_comments_pings( $oldpost, $newpost ) {
}

$this->plugin->alerts->Trigger(
$event, array(
$event,
array(
'Type' => $type,
'PostID' => $newpost->ID,
'PostType' => $newpost->post_type,
Expand Down Expand Up @@ -1131,7 +1165,8 @@ protected function check_tags_change( $old_tags, $new_tags, $post ) {
$editor_link = $this->get_editor_link( $post );
$post_status = ( 'publish' === $post->post_status ) ? 'published' : $post->post_status;
$this->plugin->alerts->Trigger(
$add_event, array(
$add_event,
array(
'PostID' => $post->ID,
'PostType' => $post->post_type,
'PostStatus' => $post_status,
Expand All @@ -1149,7 +1184,8 @@ protected function check_tags_change( $old_tags, $new_tags, $post ) {
$editor_link = $this->get_editor_link( $post );
$post_status = ( 'publish' === $post->post_status ) ? 'published' : $post->post_status;
$this->plugin->alerts->Trigger(
$remove_event, array(
$remove_event,
array(
'PostID' => $post->ID,
'PostType' => $post->post_type,
'PostStatus' => $post_status,
Expand Down Expand Up @@ -1263,7 +1299,8 @@ private function check_title_change( $oldpost, $newpost ) {
if ( $oldpost->post_title !== $newpost->post_title ) {
$editor_link = $this->get_editor_link( $oldpost );
$this->plugin->alerts->Trigger(
2086, array(
2086,
array(
'PostID' => $newpost->ID,
'PostType' => $newpost->post_type,
'PostTitle' => $newpost->post_title,
Expand Down Expand Up @@ -1406,7 +1443,8 @@ public function post_opened_in_editor( $post ) {
if ( ! $this->was_triggered( $event ) ) {
$editor_link = $this->get_editor_link( $post );
$this->plugin->alerts->Trigger(
$event, array(
$event,
array(
'PostID' => $post->ID,
'PostType' => $post->post_type,
'PostTitle' => $post->post_title,
Expand Down

0 comments on commit cf6b26f

Please sign in to comment.